diff --git a/Cargo.lock b/Cargo.lock index 15956dd5a026157a779d05dcb5803873f1d32385..1b911e961ba3499bda8e26b58fd490b468a983ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3160,6 +3160,7 @@ dependencies = [ "smol", "sqlez", "sum_tree", + "taffy", "time 0.3.24", "tiny-skia", "usvg", @@ -3178,6 +3179,15 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "grid" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0634107a3a005070dd73e27e74ecb691a94e9e5ba7829f434db7fbf73a6b5c47" +dependencies = [ + "no-std-compat", +] + [[package]] name = "h2" version = "0.3.20" @@ -4598,6 +4608,12 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "no-std-compat" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b93853da6d84c2e3c7d730d6473e8817692dd89be387eb01b94d7f108ecb5b8c" + [[package]] name = "node_runtime" version = "0.1.0" @@ -5299,6 +5315,7 @@ dependencies = [ "serde", "simplelog", "smallvec", + "taffy", "util", ] @@ -7643,6 +7660,18 @@ dependencies = [ "winx", ] +[[package]] +name = "taffy" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3540ec65df399929a04a485feb50144475735920cc47eaf8eba09c70b1df4055" +dependencies = [ + "arrayvec 0.7.4", + "grid", + "num-traits", + "slotmap", +] + [[package]] name = "take-until" version = "0.2.0" diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 0ea4ff758bd1edb040d56d1ad6b70656117c182f..c8c1c92ce86b6828f49e5b0c323d266ff3bbae27 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -585,7 +585,7 @@ impl EditorElement { visible_bounds: RectF, layout: &mut LayoutState, editor: &mut Editor, - cx: &mut ViewContext, + cx: &mut PaintContext, ) { let line_height = layout.position_map.line_height; @@ -740,7 +740,7 @@ impl EditorElement { visible_bounds: RectF, layout: &mut LayoutState, editor: &mut Editor, - cx: &mut ViewContext, + cx: &mut PaintContext, ) { let style = &self.style; let local_replica_id = editor.replica_id(cx); @@ -1317,7 +1317,7 @@ impl EditorElement { visible_bounds: RectF, layout: &mut LayoutState, editor: &mut Editor, - cx: &mut ViewContext, + cx: &mut PaintContext, ) { let scroll_position = layout.position_map.snapshot.scroll_position(); let scroll_left = scroll_position.x() * layout.position_map.em_width; diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index 5bd7d0318439d2cbd46667f585be37ae25efa33e..6ebed0ed979c31b4c9761251292abc025a707e07 100644 --- a/crates/gpui/Cargo.toml +++ b/crates/gpui/Cargo.toml @@ -52,6 +52,7 @@ tiny-skia = "0.5" usvg = { version = "0.14", features = [] } uuid = { version = "1.1.2", features = ["v4"] } waker-fn = "1.1.0" +taffy = "0.3.12" [build-dependencies] bindgen = "0.65.1" diff --git a/crates/gpui/playground/Cargo.toml b/crates/gpui/playground/Cargo.toml index db60eda1bb8104106c88553b76f7eb772cb67ea5..043e916aa1d796ff7ebc2ae97bd60a2045c3f718 100644 --- a/crates/gpui/playground/Cargo.toml +++ b/crates/gpui/playground/Cargo.toml @@ -16,6 +16,7 @@ optional_struct = "0.3.1" serde.workspace = true simplelog = "0.9" smallvec.workspace = true +taffy = "0.3.12" util = { path = "../../util" } [dev-dependencies] diff --git a/crates/gpui/src/app/window.rs b/crates/gpui/src/app/window.rs index a1805006e7c48862291636636207772be82c228e..49d600d6dda5e5b96ec4dddcddd2a2c712b278a4 100644 --- a/crates/gpui/src/app/window.rs +++ b/crates/gpui/src/app/window.rs @@ -33,12 +33,14 @@ use std::{ mem, ops::{Deref, DerefMut, Range, Sub}, }; +use taffy::Taffy; use util::ResultExt; use uuid::Uuid; use super::{Reference, ViewMetadata}; pub struct Window { + layout_engine: Taffy, pub(crate) root_view: Option, pub(crate) focused_view_id: Option, pub(crate) parents: HashMap, @@ -73,6 +75,7 @@ impl Window { let titlebar_height = platform_window.titlebar_height(); let appearance = platform_window.appearance(); let mut window = Self { + layout_engine: Taffy::new(), root_view: None, focused_view_id: None, parents: Default::default(), @@ -207,6 +210,10 @@ impl<'a> WindowContext<'a> { } } + pub fn layout_engine(&mut self) -> &mut Taffy { + &mut self.window.layout_engine + } + pub fn remove_window(&mut self) { self.removed = true; } diff --git a/crates/gpui/src/elements.rs b/crates/gpui/src/elements.rs index e603b6a1e2a38a8bced41c5bf9f9bca03f2c4f84..0b600a02b9335e6d62bfe49034fa1bce674e7719 100644 --- a/crates/gpui/src/elements.rs +++ b/crates/gpui/src/elements.rs @@ -215,7 +215,7 @@ trait AnyElementState { origin: Vector2F, visible_bounds: RectF, view: &mut V, - cx: &mut ViewContext, + cx: &mut PaintContext, ); fn rect_for_text_range( @@ -288,7 +288,7 @@ impl> AnyElementState for ElementState { origin: Vector2F, visible_bounds: RectF, view: &mut V, - cx: &mut ViewContext, + cx: &mut PaintContext, ) { *self = match mem::take(self) { ElementState::PostLayout { @@ -464,7 +464,7 @@ impl AnyElement { origin: Vector2F, visible_bounds: RectF, view: &mut V, - cx: &mut ViewContext, + cx: &mut PaintContext, ) { self.state.paint(scene, origin, visible_bounds, view, cx); } @@ -637,7 +637,9 @@ impl AnyRootElement for RootElement { .ok_or_else(|| anyhow!("paint called on a root element for a dropped view"))?; view.update(cx, |view, cx| { - self.element.paint(scene, origin, visible_bounds, view, cx); + let mut cx = PaintContext::new(cx); + self.element + .paint(scene, origin, visible_bounds, view, &mut cx); Ok(()) }) }