Remove use of replace_with crate for managing element lifecycles
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
76c07fb232
commit
e080739d57
3 changed files with 33 additions and 35 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -2172,7 +2172,6 @@ dependencies = [
|
||||||
"png 0.16.8",
|
"png 0.16.8",
|
||||||
"postage",
|
"postage",
|
||||||
"rand 0.8.3",
|
"rand 0.8.3",
|
||||||
"replace_with",
|
|
||||||
"resvg",
|
"resvg",
|
||||||
"seahash",
|
"seahash",
|
||||||
"serde 1.0.125",
|
"serde 1.0.125",
|
||||||
|
@ -3927,12 +3926,6 @@ dependencies = [
|
||||||
"winapi 0.3.9",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "replace_with"
|
|
||||||
version = "0.1.7"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e3a8614ee435691de62bcffcf4a66d91b3594bf1428a5722e79103249a095690"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "resvg"
|
name = "resvg"
|
||||||
version = "0.14.0"
|
version = "0.14.0"
|
||||||
|
|
|
@ -19,7 +19,6 @@ pathfinder_color = "0.5"
|
||||||
pathfinder_geometry = "0.5"
|
pathfinder_geometry = "0.5"
|
||||||
postage = { version = "0.4.1", features = ["futures-traits"] }
|
postage = { version = "0.4.1", features = ["futures-traits"] }
|
||||||
rand = "0.8.3"
|
rand = "0.8.3"
|
||||||
replace_with = "0.1.7"
|
|
||||||
resvg = "0.14"
|
resvg = "0.14"
|
||||||
seahash = "4.1"
|
seahash = "4.1"
|
||||||
serde = { version = "1.0.125", features = ["derive"] }
|
serde = { version = "1.0.125", features = ["derive"] }
|
||||||
|
|
|
@ -34,8 +34,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
use core::panic;
|
use core::panic;
|
||||||
use json::ToJson;
|
use json::ToJson;
|
||||||
use replace_with::replace_with_or_abort;
|
use std::{any::Any, borrow::Cow, mem};
|
||||||
use std::{any::Any, borrow::Cow};
|
|
||||||
|
|
||||||
trait AnyElement {
|
trait AnyElement {
|
||||||
fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F;
|
fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F;
|
||||||
|
@ -115,6 +114,7 @@ pub trait Element {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Lifecycle<T: Element> {
|
pub enum Lifecycle<T: Element> {
|
||||||
|
Empty,
|
||||||
Init {
|
Init {
|
||||||
element: T,
|
element: T,
|
||||||
},
|
},
|
||||||
|
@ -139,8 +139,9 @@ pub struct ElementBox {
|
||||||
|
|
||||||
impl<T: Element> AnyElement for Lifecycle<T> {
|
impl<T: Element> AnyElement for Lifecycle<T> {
|
||||||
fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F {
|
fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F {
|
||||||
let mut result = None;
|
let result;
|
||||||
replace_with_or_abort(self, |me| match me {
|
*self = match mem::take(self) {
|
||||||
|
Lifecycle::Empty => unreachable!(),
|
||||||
Lifecycle::Init { mut element }
|
Lifecycle::Init { mut element }
|
||||||
| Lifecycle::PostLayout { mut element, .. }
|
| Lifecycle::PostLayout { mut element, .. }
|
||||||
| Lifecycle::PostPaint { mut element, .. } => {
|
| Lifecycle::PostPaint { mut element, .. } => {
|
||||||
|
@ -148,7 +149,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
|
||||||
debug_assert!(size.x().is_finite());
|
debug_assert!(size.x().is_finite());
|
||||||
debug_assert!(size.y().is_finite());
|
debug_assert!(size.y().is_finite());
|
||||||
|
|
||||||
result = Some(size);
|
result = size;
|
||||||
Lifecycle::PostLayout {
|
Lifecycle::PostLayout {
|
||||||
element,
|
element,
|
||||||
constraint,
|
constraint,
|
||||||
|
@ -156,8 +157,8 @@ impl<T: Element> AnyElement for Lifecycle<T> {
|
||||||
layout,
|
layout,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
result.unwrap()
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn after_layout(&mut self, cx: &mut AfterLayoutContext) {
|
fn after_layout(&mut self, cx: &mut AfterLayoutContext) {
|
||||||
|
@ -175,27 +176,25 @@ impl<T: Element> AnyElement for Lifecycle<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext) {
|
fn paint(&mut self, origin: Vector2F, cx: &mut PaintContext) {
|
||||||
replace_with_or_abort(self, |me| {
|
*self = if let Lifecycle::PostLayout {
|
||||||
if let Lifecycle::PostLayout {
|
mut element,
|
||||||
mut element,
|
constraint,
|
||||||
|
size,
|
||||||
|
mut layout,
|
||||||
|
} = mem::take(self)
|
||||||
|
{
|
||||||
|
let bounds = RectF::new(origin, size);
|
||||||
|
let paint = element.paint(bounds, &mut layout, cx);
|
||||||
|
Lifecycle::PostPaint {
|
||||||
|
element,
|
||||||
constraint,
|
constraint,
|
||||||
size,
|
bounds,
|
||||||
mut layout,
|
layout,
|
||||||
} = me
|
paint,
|
||||||
{
|
|
||||||
let bounds = RectF::new(origin, size);
|
|
||||||
let paint = element.paint(bounds, &mut layout, cx);
|
|
||||||
Lifecycle::PostPaint {
|
|
||||||
element,
|
|
||||||
constraint,
|
|
||||||
bounds,
|
|
||||||
layout,
|
|
||||||
paint,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
panic!("invalid element lifecycle state");
|
|
||||||
}
|
}
|
||||||
});
|
} else {
|
||||||
|
panic!("invalid element lifecycle state");
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool {
|
fn dispatch_event(&mut self, event: &Event, cx: &mut EventContext) -> bool {
|
||||||
|
@ -215,7 +214,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
|
||||||
|
|
||||||
fn size(&self) -> Vector2F {
|
fn size(&self) -> Vector2F {
|
||||||
match self {
|
match self {
|
||||||
Lifecycle::Init { .. } => panic!("invalid element lifecycle state"),
|
Lifecycle::Empty | Lifecycle::Init { .. } => panic!("invalid element lifecycle state"),
|
||||||
Lifecycle::PostLayout { size, .. } => *size,
|
Lifecycle::PostLayout { size, .. } => *size,
|
||||||
Lifecycle::PostPaint { bounds, .. } => bounds.size(),
|
Lifecycle::PostPaint { bounds, .. } => bounds.size(),
|
||||||
}
|
}
|
||||||
|
@ -223,6 +222,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
|
||||||
|
|
||||||
fn metadata(&self) -> Option<&dyn Any> {
|
fn metadata(&self) -> Option<&dyn Any> {
|
||||||
match self {
|
match self {
|
||||||
|
Lifecycle::Empty => unreachable!(),
|
||||||
Lifecycle::Init { element }
|
Lifecycle::Init { element }
|
||||||
| Lifecycle::PostLayout { element, .. }
|
| Lifecycle::PostLayout { element, .. }
|
||||||
| Lifecycle::PostPaint { element, .. } => element.metadata(),
|
| Lifecycle::PostPaint { element, .. } => element.metadata(),
|
||||||
|
@ -257,6 +257,12 @@ impl<T: Element> AnyElement for Lifecycle<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: Element> Default for Lifecycle<T> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::Empty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ElementBox {
|
impl ElementBox {
|
||||||
pub fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F {
|
pub fn layout(&mut self, constraint: SizeConstraint, cx: &mut LayoutContext) -> Vector2F {
|
||||||
self.element.layout(constraint, cx)
|
self.element.layout(constraint, cx)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue