From e0fdc7d0aa08a00f7d53b7f0687afdeb7e2c31db Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 11 Sep 2023 09:00:59 -0600 Subject: [PATCH] Eliminate PaintContext --- crates/collab_ui/src/collab_titlebar_item.rs | 6 +- crates/collab_ui/src/face_pile.rs | 4 +- crates/editor/src/element.rs | 20 ++--- crates/gpui/examples/corner_radii.rs | 2 +- crates/gpui/src/app.rs | 86 +++---------------- crates/gpui/src/app/window.rs | 6 +- crates/gpui/src/elements.rs | 31 ++----- crates/gpui/src/elements/align.rs | 4 +- crates/gpui/src/elements/canvas.rs | 8 +- crates/gpui/src/elements/clipped.rs | 4 +- crates/gpui/src/elements/component.rs | 4 +- crates/gpui/src/elements/constrained_box.rs | 4 +- crates/gpui/src/elements/container.rs | 4 +- crates/gpui/src/elements/empty.rs | 4 +- crates/gpui/src/elements/expanded.rs | 4 +- crates/gpui/src/elements/flex.rs | 7 +- crates/gpui/src/elements/hook.rs | 4 +- crates/gpui/src/elements/image.rs | 4 +- crates/gpui/src/elements/keystroke_label.rs | 2 +- crates/gpui/src/elements/label.rs | 4 +- crates/gpui/src/elements/list.rs | 8 +- .../gpui/src/elements/mouse_event_handler.rs | 6 +- crates/gpui/src/elements/overlay.rs | 4 +- crates/gpui/src/elements/resizable.rs | 7 +- crates/gpui/src/elements/stack.rs | 4 +- crates/gpui/src/elements/svg.rs | 3 +- crates/gpui/src/elements/text.rs | 4 +- crates/gpui/src/elements/tooltip.rs | 4 +- crates/gpui/src/elements/uniform_list.rs | 4 +- crates/gpui2/src/adapter.rs | 12 +-- crates/gpui2/src/element.rs | 9 +- crates/gpui2/src/elements/div.rs | 7 +- crates/gpui2/src/elements/hoverable.rs | 3 +- crates/gpui2/src/elements/img.rs | 2 +- crates/gpui2/src/elements/pressable.rs | 3 +- crates/gpui2/src/elements/svg.rs | 2 +- crates/gpui2/src/elements/text.rs | 3 +- crates/gpui2/src/gpui2.rs | 1 - crates/gpui2/src/interactive.rs | 4 +- crates/gpui2/src/paint_context.rs | 50 ----------- crates/gpui2/src/style.rs | 6 +- crates/gpui2/src/view_context.rs | 34 +++++++- crates/gpui2_macros/src/derive_element.rs | 2 +- crates/gpui_macros/src/gpui_macros.rs | 2 +- crates/storybook/src/theme.rs | 7 +- crates/terminal_view/src/terminal_element.rs | 6 +- crates/workspace/src/pane.rs | 6 +- crates/workspace/src/pane_group.rs | 4 +- crates/workspace/src/status_bar.rs | 6 +- 49 files changed, 156 insertions(+), 269 deletions(-) delete mode 100644 crates/gpui2/src/paint_context.rs diff --git a/crates/collab_ui/src/collab_titlebar_item.rs b/crates/collab_ui/src/collab_titlebar_item.rs index a12e7b8663..f0e09e139e 100644 --- a/crates/collab_ui/src/collab_titlebar_item.rs +++ b/crates/collab_ui/src/collab_titlebar_item.rs @@ -13,8 +13,8 @@ use gpui::{ geometry::{rect::RectF, vector::vec2f, PathBuilder}, json::{self, ToJson}, platform::{CursorStyle, MouseButton}, - AppContext, Entity, ImageData, ModelHandle, PaintContext, Subscription, View, ViewContext, - ViewHandle, WeakViewHandle, + AppContext, Entity, ImageData, ModelHandle, Subscription, View, ViewContext, ViewHandle, + WeakViewHandle, }; use picker::PickerEvent; use project::{Project, RepositoryEntry}; @@ -1176,7 +1176,7 @@ impl Element for AvatarRibbon { _: RectF, _: &mut Self::LayoutState, _: &mut CollabTitlebarItem, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let mut path = PathBuilder::new(); path.reset(bounds.lower_left()); diff --git a/crates/collab_ui/src/face_pile.rs b/crates/collab_ui/src/face_pile.rs index ef0368dd32..5017666f7b 100644 --- a/crates/collab_ui/src/face_pile.rs +++ b/crates/collab_ui/src/face_pile.rs @@ -7,7 +7,7 @@ use gpui::{ }, json::ToJson, serde_json::{self, json}, - AnyElement, Axis, Element, PaintContext, View, ViewContext, + AnyElement, Axis, Element, View, ViewContext, }; pub(crate) struct FacePile { @@ -57,7 +57,7 @@ impl Element for FacePile { visible_bounds: RectF, _layout: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 7a82352245..b7e34fda53 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -32,8 +32,8 @@ use gpui::{ json::{self, ToJson}, platform::{CursorStyle, Modifiers, MouseButton, MouseButtonEvent, MouseMovedEvent}, text_layout::{self, Line, RunStyle, TextLayoutCache}, - AnyElement, Axis, CursorRegion, Element, EventContext, FontCache, MouseRegion, PaintContext, - Quad, SizeConstraint, ViewContext, WindowContext, + AnyElement, Axis, CursorRegion, Element, EventContext, FontCache, MouseRegion, Quad, + SizeConstraint, ViewContext, WindowContext, }; use itertools::Itertools; use json::json; @@ -635,7 +635,7 @@ impl EditorElement { visible_bounds: RectF, layout: &mut LayoutState, editor: &mut Editor, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { let line_height = layout.position_map.line_height; @@ -778,7 +778,7 @@ impl EditorElement { visible_bounds: RectF, layout: &mut LayoutState, editor: &mut Editor, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { let style = &self.style; let scroll_position = layout.position_map.snapshot.scroll_position(); @@ -1351,7 +1351,7 @@ impl EditorElement { visible_bounds: RectF, layout: &mut LayoutState, editor: &mut Editor, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { let scroll_position = layout.position_map.snapshot.scroll_position(); let scroll_left = scroll_position.x() * layout.position_map.em_width; @@ -2570,7 +2570,7 @@ impl Element for EditorElement { visible_bounds: RectF, layout: &mut Self::LayoutState, editor: &mut Editor, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); cx.scene().push_layer(Some(visible_bounds)); @@ -3340,13 +3340,7 @@ mod tests { // Don't panic. let bounds = RectF::new(Default::default(), size); editor.update(cx, |editor, cx| { - element.paint( - bounds, - bounds, - &mut state, - editor, - &mut PaintContext::new(cx), - ); + element.paint(bounds, bounds, &mut state, editor, cx); }); } diff --git a/crates/gpui/examples/corner_radii.rs b/crates/gpui/examples/corner_radii.rs index c2f6b99ceb..75ea3aeec6 100644 --- a/crates/gpui/examples/corner_radii.rs +++ b/crates/gpui/examples/corner_radii.rs @@ -53,7 +53,7 @@ impl gpui::Element for CornersElement { _: pathfinder_geometry::rect::RectF, _: &mut Self::LayoutState, _: &mut V, - cx: &mut gpui::PaintContext, + cx: &mut gpui::ViewContext, ) -> Self::PaintState { cx.scene().push_quad(Quad { bounds, diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 5fc7f54a7a..c95c0a6105 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -3404,6 +3404,16 @@ impl<'a, 'b, V: 'static> ViewContext<'a, 'b, V> { .or_default() .push(self_view_id); } + + pub fn paint_layer(&mut self, clip_bounds: Option, f: F) -> R + where + F: FnOnce(&mut Self) -> R, + { + self.scene().push_layer(clip_bounds); + let result = f(self); + self.scene().pop_layer(); + result + } } impl ViewContext<'_, '_, V> { @@ -3495,82 +3505,6 @@ impl BorrowWindowContext for ViewContext<'_, '_, V> { } } -pub struct PaintContext<'a, 'b, 'c, V> { - pub view_context: &'c mut ViewContext<'a, 'b, V>, -} - -impl<'a, 'b, 'c, V> PaintContext<'a, 'b, 'c, V> { - pub fn new(view_context: &'c mut ViewContext<'a, 'b, V>) -> Self { - Self { view_context } - } - - pub fn paint_layer(&mut self, clip_bounds: Option, f: F) -> R - where - F: FnOnce(&mut Self) -> R, - { - self.scene().push_layer(clip_bounds); - let result = f(self); - self.scene().pop_layer(); - result - } -} - -impl<'a, 'b, 'c, V> Deref for PaintContext<'a, 'b, 'c, V> { - type Target = ViewContext<'a, 'b, V>; - - fn deref(&self) -> &Self::Target { - &self.view_context - } -} - -impl DerefMut for PaintContext<'_, '_, '_, V> { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.view_context - } -} - -impl BorrowAppContext for PaintContext<'_, '_, '_, V> { - fn read_with T>(&self, f: F) -> T { - BorrowAppContext::read_with(&*self.view_context, f) - } - - fn update T>(&mut self, f: F) -> T { - BorrowAppContext::update(&mut *self.view_context, f) - } -} - -impl BorrowWindowContext for PaintContext<'_, '_, '_, V> { - type Result = T; - - fn read_window(&self, window: AnyWindowHandle, f: F) -> Self::Result - where - F: FnOnce(&WindowContext) -> T, - { - BorrowWindowContext::read_window(self.view_context, window, f) - } - - fn read_window_optional(&self, window: AnyWindowHandle, f: F) -> Option - where - F: FnOnce(&WindowContext) -> Option, - { - BorrowWindowContext::read_window_optional(self.view_context, window, f) - } - - fn update_window(&mut self, window: AnyWindowHandle, f: F) -> Self::Result - where - F: FnOnce(&mut WindowContext) -> T, - { - BorrowWindowContext::update_window(self.view_context, window, f) - } - - fn update_window_optional(&mut self, window: AnyWindowHandle, f: F) -> Option - where - F: FnOnce(&mut WindowContext) -> Option, - { - BorrowWindowContext::update_window_optional(self.view_context, window, f) - } -} - pub struct EventContext<'a, 'b, 'c, V> { view_context: &'c mut ViewContext<'a, 'b, V>, pub(crate) handled: bool, diff --git a/crates/gpui/src/app/window.rs b/crates/gpui/src/app/window.rs index 96ff9988c0..e17795910f 100644 --- a/crates/gpui/src/app/window.rs +++ b/crates/gpui/src/app/window.rs @@ -16,8 +16,8 @@ use crate::{ text_layout::TextLayoutCache, util::post_inc, Action, AnyView, AnyViewHandle, AnyWindowHandle, AppContext, BorrowAppContext, - BorrowWindowContext, Effect, Element, Entity, Handle, MouseRegion, MouseRegionId, PaintContext, - SceneBuilder, Subscription, View, ViewContext, ViewHandle, WindowInvalidation, + BorrowWindowContext, Effect, Element, Entity, Handle, MouseRegion, MouseRegionId, SceneBuilder, + Subscription, View, ViewContext, ViewHandle, WindowInvalidation, }; use anyhow::{anyhow, bail, Result}; use collections::{HashMap, HashSet}; @@ -1703,7 +1703,7 @@ impl Element for ChildView { visible_bounds: RectF, _: &mut Self::LayoutState, _: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { if let Some(mut rendered_view) = cx.window.rendered_views.remove(&self.view_id) { rendered_view diff --git a/crates/gpui/src/elements.rs b/crates/gpui/src/elements.rs index 94373485ba..cf01a1ddc1 100644 --- a/crates/gpui/src/elements.rs +++ b/crates/gpui/src/elements.rs @@ -34,7 +34,7 @@ use crate::{ rect::RectF, vector::{vec2f, Vector2F}, }, - json, Action, Entity, PaintContext, SizeConstraint, TypeTag, View, ViewContext, WeakViewHandle, + json, Action, Entity, SizeConstraint, TypeTag, View, ViewContext, WeakViewHandle, WindowContext, }; use anyhow::{anyhow, Result}; @@ -68,7 +68,7 @@ pub trait Element: 'static { visible_bounds: RectF, layout: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState; fn rect_for_text_range( @@ -267,7 +267,7 @@ trait AnyElementState { origin: Vector2F, visible_bounds: RectF, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ); fn rect_for_text_range( @@ -347,7 +347,7 @@ impl> AnyElementState for ElementState { origin: Vector2F, visible_bounds: RectF, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { *self = match mem::take(self) { ElementState::PostLayout { @@ -357,13 +357,7 @@ impl> AnyElementState for ElementState { mut layout, } => { let bounds = RectF::new(origin, size); - let paint = element.paint( - bounds, - visible_bounds, - &mut layout, - view, - &mut PaintContext::new(cx), - ); + let paint = element.paint(bounds, visible_bounds, &mut layout, view, cx); ElementState::PostPaint { element, constraint, @@ -381,13 +375,7 @@ impl> AnyElementState for ElementState { .. } => { let bounds = RectF::new(origin, bounds.size()); - let paint = element.paint( - bounds, - visible_bounds, - &mut layout, - view, - &mut PaintContext::new(cx), - ); + let paint = element.paint(bounds, visible_bounds, &mut layout, view, cx); ElementState::PostPaint { element, constraint, @@ -520,7 +508,7 @@ impl AnyElement { origin: Vector2F, visible_bounds: RectF, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { self.state.paint(origin, visible_bounds, view, cx); } @@ -582,7 +570,7 @@ impl Element for AnyElement { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { self.paint(bounds.origin(), visible_bounds, view, cx); } @@ -674,8 +662,7 @@ impl AnyRootElement for RootElement { .ok_or_else(|| anyhow!("paint called on a root element for a dropped view"))?; view.update(cx, |view, cx| { - let mut cx = PaintContext::new(cx); - self.element.paint(origin, visible_bounds, view, &mut cx); + self.element.paint(origin, visible_bounds, view, cx); Ok(()) }) } diff --git a/crates/gpui/src/elements/align.rs b/crates/gpui/src/elements/align.rs index daec48deb8..ba302f4094 100644 --- a/crates/gpui/src/elements/align.rs +++ b/crates/gpui/src/elements/align.rs @@ -1,6 +1,6 @@ use crate::{ geometry::{rect::RectF, vector::Vector2F}, - json, AnyElement, Element, PaintContext, SizeConstraint, ViewContext, + json, AnyElement, Element, SizeConstraint, ViewContext, }; use json::ToJson; @@ -68,7 +68,7 @@ impl Element for Align { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let my_center = bounds.size() / 2.; let my_target = my_center + my_center * self.alignment; diff --git a/crates/gpui/src/elements/canvas.rs b/crates/gpui/src/elements/canvas.rs index ec6beaafeb..494d9747c5 100644 --- a/crates/gpui/src/elements/canvas.rs +++ b/crates/gpui/src/elements/canvas.rs @@ -3,7 +3,7 @@ use std::marker::PhantomData; use super::Element; use crate::{ json::{self, json}, - PaintContext, ViewContext, + ViewContext, }; use json::ToJson; use pathfinder_geometry::{ @@ -15,7 +15,7 @@ pub struct Canvas(F, PhantomData); impl Canvas where - F: FnMut(RectF, RectF, &mut V, &mut PaintContext), + F: FnMut(RectF, RectF, &mut V, &mut ViewContext), { pub fn new(f: F) -> Self { Self(f, PhantomData) @@ -24,7 +24,7 @@ where impl Element for Canvas where - F: 'static + FnMut(RectF, RectF, &mut V, &mut PaintContext), + F: 'static + FnMut(RectF, RectF, &mut V, &mut ViewContext), { type LayoutState = (); type PaintState = (); @@ -54,7 +54,7 @@ where visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { self.0(bounds, visible_bounds, view, cx) } diff --git a/crates/gpui/src/elements/clipped.rs b/crates/gpui/src/elements/clipped.rs index 50fe3dc4df..3bd16306bc 100644 --- a/crates/gpui/src/elements/clipped.rs +++ b/crates/gpui/src/elements/clipped.rs @@ -3,7 +3,7 @@ use std::ops::Range; use pathfinder_geometry::{rect::RectF, vector::Vector2F}; use serde_json::json; -use crate::{json, AnyElement, Element, PaintContext, SizeConstraint, ViewContext}; +use crate::{json, AnyElement, Element, SizeConstraint, ViewContext}; pub struct Clipped { child: AnyElement, @@ -34,7 +34,7 @@ impl Element for Clipped { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { cx.scene().push_layer(Some(bounds)); let state = self.child.paint(bounds.origin(), visible_bounds, view, cx); diff --git a/crates/gpui/src/elements/component.rs b/crates/gpui/src/elements/component.rs index a663574f43..1d52ede456 100644 --- a/crates/gpui/src/elements/component.rs +++ b/crates/gpui/src/elements/component.rs @@ -2,7 +2,7 @@ use std::{any::Any, marker::PhantomData}; use pathfinder_geometry::{rect::RectF, vector::Vector2F}; -use crate::{AnyElement, Element, PaintContext, SizeConstraint, ViewContext}; +use crate::{AnyElement, Element, SizeConstraint, ViewContext}; use super::Empty; @@ -302,7 +302,7 @@ impl + 'static> Element for ComponentAdap visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { self.element .as_mut() diff --git a/crates/gpui/src/elements/constrained_box.rs b/crates/gpui/src/elements/constrained_box.rs index 5a693869e5..0b49b0951d 100644 --- a/crates/gpui/src/elements/constrained_box.rs +++ b/crates/gpui/src/elements/constrained_box.rs @@ -5,7 +5,7 @@ use serde_json::json; use crate::{ geometry::{rect::RectF, vector::Vector2F}, - json, AnyElement, Element, PaintContext, SizeConstraint, ViewContext, + json, AnyElement, Element, SizeConstraint, ViewContext, }; pub struct ConstrainedBox { @@ -154,7 +154,7 @@ impl Element for ConstrainedBox { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { cx.scene().push_layer(Some(visible_bounds)); self.child.paint(bounds.origin(), visible_bounds, view, cx); diff --git a/crates/gpui/src/elements/container.rs b/crates/gpui/src/elements/container.rs index caa9af457c..c2d8fd38bd 100644 --- a/crates/gpui/src/elements/container.rs +++ b/crates/gpui/src/elements/container.rs @@ -10,7 +10,7 @@ use crate::{ json::ToJson, platform::CursorStyle, scene::{self, CornerRadii, CursorRegion, Quad}, - AnyElement, Element, PaintContext, SizeConstraint, ViewContext, + AnyElement, Element, SizeConstraint, ViewContext, }; use schemars::JsonSchema; use serde::Deserialize; @@ -391,7 +391,7 @@ impl Element for Container { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let quad_bounds = RectF::from_points( bounds.origin() + vec2f(self.style.margin.left, self.style.margin.top), diff --git a/crates/gpui/src/elements/empty.rs b/crates/gpui/src/elements/empty.rs index 65acdf3295..4344199278 100644 --- a/crates/gpui/src/elements/empty.rs +++ b/crates/gpui/src/elements/empty.rs @@ -6,7 +6,7 @@ use crate::{ vector::{vec2f, Vector2F}, }, json::{json, ToJson}, - PaintContext, ViewContext, + ViewContext, }; use crate::{Element, SizeConstraint}; @@ -56,7 +56,7 @@ impl Element for Empty { _: RectF, _: &mut Self::LayoutState, _: &mut V, - _: &mut PaintContext, + _: &mut ViewContext, ) -> Self::PaintState { } diff --git a/crates/gpui/src/elements/expanded.rs b/crates/gpui/src/elements/expanded.rs index 9dd301b202..0cafa3f119 100644 --- a/crates/gpui/src/elements/expanded.rs +++ b/crates/gpui/src/elements/expanded.rs @@ -2,7 +2,7 @@ use std::ops::Range; use crate::{ geometry::{rect::RectF, vector::Vector2F}, - json, AnyElement, Element, PaintContext, SizeConstraint, ViewContext, + json, AnyElement, Element, SizeConstraint, ViewContext, }; use serde_json::json; @@ -60,7 +60,7 @@ impl Element for Expanded { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { self.child.paint(bounds.origin(), visible_bounds, view, cx); } diff --git a/crates/gpui/src/elements/flex.rs b/crates/gpui/src/elements/flex.rs index b7d69fff61..cdce0423fd 100644 --- a/crates/gpui/src/elements/flex.rs +++ b/crates/gpui/src/elements/flex.rs @@ -2,8 +2,7 @@ use std::{any::Any, cell::Cell, f32::INFINITY, ops::Range, rc::Rc}; use crate::{ json::{self, ToJson, Value}, - AnyElement, Axis, Element, ElementStateHandle, PaintContext, SizeConstraint, Vector2FExt, - ViewContext, + AnyElement, Axis, Element, ElementStateHandle, SizeConstraint, Vector2FExt, ViewContext, }; use pathfinder_geometry::{ rect::RectF, @@ -264,7 +263,7 @@ impl Element for Flex { visible_bounds: RectF, remaining_space: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); @@ -454,7 +453,7 @@ impl Element for FlexItem { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { self.child.paint(bounds.origin(), visible_bounds, view, cx) } diff --git a/crates/gpui/src/elements/hook.rs b/crates/gpui/src/elements/hook.rs index 5b0c5c4c48..8797899f03 100644 --- a/crates/gpui/src/elements/hook.rs +++ b/crates/gpui/src/elements/hook.rs @@ -3,7 +3,7 @@ use std::ops::Range; use crate::{ geometry::{rect::RectF, vector::Vector2F}, json::json, - AnyElement, Element, PaintContext, SizeConstraint, ViewContext, + AnyElement, Element, SizeConstraint, ViewContext, }; pub struct Hook { @@ -51,7 +51,7 @@ impl Element for Hook { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { self.child.paint(bounds.origin(), visible_bounds, view, cx); } diff --git a/crates/gpui/src/elements/image.rs b/crates/gpui/src/elements/image.rs index a0b710187c..7e0c7d5daa 100644 --- a/crates/gpui/src/elements/image.rs +++ b/crates/gpui/src/elements/image.rs @@ -5,7 +5,7 @@ use crate::{ vector::{vec2f, Vector2F}, }, json::{json, ToJson}, - scene, Element, ImageData, PaintContext, SizeConstraint, ViewContext, + scene, Element, ImageData, SizeConstraint, ViewContext, }; use schemars::JsonSchema; use serde::Deserialize; @@ -95,7 +95,7 @@ impl Element for Image { _: RectF, layout: &mut Self::LayoutState, _: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { if let Some(data) = layout { cx.scene().push_image(scene::Image { diff --git a/crates/gpui/src/elements/keystroke_label.rs b/crates/gpui/src/elements/keystroke_label.rs index 7f32f86c5e..5ebb9ea688 100644 --- a/crates/gpui/src/elements/keystroke_label.rs +++ b/crates/gpui/src/elements/keystroke_label.rs @@ -65,7 +65,7 @@ impl Element for KeystrokeLabel { visible_bounds: RectF, element: &mut AnyElement, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { element.paint(bounds.origin(), visible_bounds, view, cx); } diff --git a/crates/gpui/src/elements/label.rs b/crates/gpui/src/elements/label.rs index 8ff2f15883..d8e6bd3ea6 100644 --- a/crates/gpui/src/elements/label.rs +++ b/crates/gpui/src/elements/label.rs @@ -8,7 +8,7 @@ use crate::{ }, json::{ToJson, Value}, text_layout::{Line, RunStyle}, - Element, PaintContext, SizeConstraint, ViewContext, + Element, SizeConstraint, ViewContext, }; use schemars::JsonSchema; use serde::Deserialize; @@ -162,7 +162,7 @@ impl Element for Label { visible_bounds: RectF, line: &mut Self::LayoutState, _: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); line.paint(bounds.origin(), visible_bounds, bounds.size().y(), cx) diff --git a/crates/gpui/src/elements/list.rs b/crates/gpui/src/elements/list.rs index 0b9b018311..a23b6fc5e3 100644 --- a/crates/gpui/src/elements/list.rs +++ b/crates/gpui/src/elements/list.rs @@ -4,7 +4,7 @@ use crate::{ vector::{vec2f, Vector2F}, }, json::json, - AnyElement, Element, MouseRegion, PaintContext, SizeConstraint, ViewContext, + AnyElement, Element, MouseRegion, SizeConstraint, ViewContext, }; use std::{cell::RefCell, collections::VecDeque, fmt::Debug, ops::Range, rc::Rc}; use sum_tree::{Bias, SumTree}; @@ -253,7 +253,7 @@ impl Element for List { visible_bounds: RectF, scroll_top: &mut ListOffset, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { let visible_bounds = visible_bounds.intersection(bounds).unwrap_or_default(); cx.scene().push_layer(Some(visible_bounds)); @@ -643,7 +643,7 @@ impl<'a> sum_tree::SeekTarget<'a, ListItemSummary, ListItemSummary> for Height { #[cfg(test)] mod tests { use super::*; - use crate::{elements::Empty, geometry::vector::vec2f, Entity, PaintContext}; + use crate::{elements::Empty, geometry::vector::vec2f, Entity}; use rand::prelude::*; use std::env; @@ -949,7 +949,7 @@ mod tests { (self.size, ()) } - fn paint(&mut self, _: RectF, _: RectF, _: &mut (), _: &mut V, _: &mut PaintContext) { + fn paint(&mut self, _: RectF, _: RectF, _: &mut (), _: &mut V, _: &mut ViewContext) { unimplemented!() } diff --git a/crates/gpui/src/elements/mouse_event_handler.rs b/crates/gpui/src/elements/mouse_event_handler.rs index 672b8011c9..0a632ba382 100644 --- a/crates/gpui/src/elements/mouse_event_handler.rs +++ b/crates/gpui/src/elements/mouse_event_handler.rs @@ -10,8 +10,8 @@ use crate::{ CursorRegion, HandlerSet, MouseClick, MouseClickOut, MouseDown, MouseDownOut, MouseDrag, MouseHover, MouseMove, MouseMoveOut, MouseScrollWheel, MouseUp, MouseUpOut, }, - AnyElement, Element, EventContext, MouseRegion, MouseState, PaintContext, SizeConstraint, - TypeTag, ViewContext, + AnyElement, Element, EventContext, MouseRegion, MouseState, SizeConstraint, TypeTag, + ViewContext, }; use serde_json::json; use std::ops::Range; @@ -281,7 +281,7 @@ impl Element for MouseEventHandler { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { if self.above { self.child.paint(bounds.origin(), visible_bounds, view, cx); diff --git a/crates/gpui/src/elements/overlay.rs b/crates/gpui/src/elements/overlay.rs index 081d018b25..11956bc211 100644 --- a/crates/gpui/src/elements/overlay.rs +++ b/crates/gpui/src/elements/overlay.rs @@ -3,7 +3,7 @@ use std::ops::Range; use crate::{ geometry::{rect::RectF, vector::Vector2F}, json::ToJson, - AnyElement, Axis, Element, MouseRegion, PaintContext, SizeConstraint, ViewContext, + AnyElement, Axis, Element, MouseRegion, SizeConstraint, ViewContext, }; use serde_json::json; @@ -141,7 +141,7 @@ impl Element for Overlay { _: RectF, size: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { let (anchor_position, mut bounds) = match self.position_mode { OverlayPositionMode::Window => { diff --git a/crates/gpui/src/elements/resizable.rs b/crates/gpui/src/elements/resizable.rs index 343ef92486..123225cc64 100644 --- a/crates/gpui/src/elements/resizable.rs +++ b/crates/gpui/src/elements/resizable.rs @@ -7,8 +7,7 @@ use serde_json::json; use crate::{ geometry::rect::RectF, platform::{CursorStyle, MouseButton}, - AnyElement, AppContext, Axis, Element, MouseRegion, PaintContext, SizeConstraint, TypeTag, - View, ViewContext, + AnyElement, AppContext, Axis, Element, MouseRegion, SizeConstraint, TypeTag, View, ViewContext, }; #[derive(Copy, Clone, Debug)] @@ -116,7 +115,7 @@ impl Element for Resizable { visible_bounds: pathfinder_geometry::rect::RectF, constraint: &mut SizeConstraint, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { cx.scene().push_stacking_context(None, None); @@ -252,7 +251,7 @@ impl Element for BoundsProvider { visible_bounds: pathfinder_geometry::rect::RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut crate::PaintContext, + cx: &mut crate::ViewContext, ) -> Self::PaintState { cx.update_default_global::(|map, _| { map.0.insert(TypeTag::new::

(), (bounds, visible_bounds)); diff --git a/crates/gpui/src/elements/stack.rs b/crates/gpui/src/elements/stack.rs index aac71595dd..ad5080907b 100644 --- a/crates/gpui/src/elements/stack.rs +++ b/crates/gpui/src/elements/stack.rs @@ -3,7 +3,7 @@ use std::ops::Range; use crate::{ geometry::{rect::RectF, vector::Vector2F}, json::{self, json, ToJson}, - AnyElement, Element, PaintContext, SizeConstraint, ViewContext, + AnyElement, Element, SizeConstraint, ViewContext, }; /// Element which renders it's children in a stack on top of each other. @@ -56,7 +56,7 @@ impl Element for Stack { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { for child in &mut self.children { cx.scene().push_layer(None); diff --git a/crates/gpui/src/elements/svg.rs b/crates/gpui/src/elements/svg.rs index d6b0bc8d10..7f67719d8a 100644 --- a/crates/gpui/src/elements/svg.rs +++ b/crates/gpui/src/elements/svg.rs @@ -1,6 +1,5 @@ use super::constrain_size_preserving_aspect_ratio; use crate::json::ToJson; -use crate::PaintContext; use crate::{ color::Color, geometry::{ @@ -73,7 +72,7 @@ impl Element for Svg { _visible_bounds: RectF, svg: &mut Self::LayoutState, _: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { if let Some(svg) = svg.clone() { cx.scene().push_icon(scene::Icon { diff --git a/crates/gpui/src/elements/text.rs b/crates/gpui/src/elements/text.rs index 0e154d80ff..c823840692 100644 --- a/crates/gpui/src/elements/text.rs +++ b/crates/gpui/src/elements/text.rs @@ -7,7 +7,7 @@ use crate::{ }, json::{ToJson, Value}, text_layout::{Line, RunStyle, ShapedBoundary}, - Element, FontCache, PaintContext, SizeConstraint, TextLayoutCache, ViewContext, WindowContext, + Element, FontCache, SizeConstraint, TextLayoutCache, ViewContext, WindowContext, }; use log::warn; use serde_json::json; @@ -169,7 +169,7 @@ impl Element for Text { visible_bounds: RectF, layout: &mut Self::LayoutState, _: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let mut origin = bounds.origin(); let empty = Vec::new(); diff --git a/crates/gpui/src/elements/tooltip.rs b/crates/gpui/src/elements/tooltip.rs index d16752c022..59892b6279 100644 --- a/crates/gpui/src/elements/tooltip.rs +++ b/crates/gpui/src/elements/tooltip.rs @@ -6,7 +6,7 @@ use crate::{ fonts::TextStyle, geometry::{rect::RectF, vector::Vector2F}, json::json, - Action, Axis, ElementStateHandle, PaintContext, SizeConstraint, Task, TypeTag, ViewContext, + Action, Axis, ElementStateHandle, SizeConstraint, Task, TypeTag, ViewContext, }; use schemars::JsonSchema; use serde::Deserialize; @@ -207,7 +207,7 @@ impl Element for Tooltip { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { self.child.paint(bounds.origin(), visible_bounds, view, cx); if let Some(tooltip) = self.tooltip.as_mut() { diff --git a/crates/gpui/src/elements/uniform_list.rs b/crates/gpui/src/elements/uniform_list.rs index d3463b1d2c..037b003b20 100644 --- a/crates/gpui/src/elements/uniform_list.rs +++ b/crates/gpui/src/elements/uniform_list.rs @@ -6,7 +6,7 @@ use crate::{ }, json::{self, json}, platform::ScrollWheelEvent, - AnyElement, MouseRegion, PaintContext, ViewContext, + AnyElement, MouseRegion, ViewContext, }; use json::ToJson; use std::{cell::RefCell, cmp, ops::Range, rc::Rc}; @@ -276,7 +276,7 @@ impl Element for UniformList { visible_bounds: RectF, layout: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let visible_bounds = visible_bounds.intersection(bounds).unwrap_or_default(); diff --git a/crates/gpui2/src/adapter.rs b/crates/gpui2/src/adapter.rs index 0a4133e073..c36966d722 100644 --- a/crates/gpui2/src/adapter.rs +++ b/crates/gpui2/src/adapter.rs @@ -1,4 +1,4 @@ -use crate::{paint_context::PaintContext, ViewContext}; +use crate::ViewContext; use gpui::{geometry::rect::RectF, LayoutEngine, LayoutId}; use util::ResultExt; @@ -40,13 +40,13 @@ impl gpui::Element for AdapterElement { _visible_bounds: RectF, layout_data: &mut Option<(LayoutEngine, LayoutId)>, view: &mut V, - legacy_cx: &mut gpui::PaintContext, + cx: &mut gpui::ViewContext, ) -> Self::PaintState { let (layout_engine, layout_id) = layout_data.take().unwrap(); - legacy_cx.push_layout_engine(layout_engine); - let mut cx = PaintContext::new(legacy_cx); - self.0.paint(view, bounds.origin(), &mut cx); - *layout_data = legacy_cx.pop_layout_engine().zip(Some(layout_id)); + cx.push_layout_engine(layout_engine); + self.0 + .paint(view, bounds.origin(), &mut ViewContext::new(cx)); + *layout_data = cx.pop_layout_engine().zip(Some(layout_id)); debug_assert!(layout_data.is_some()); } diff --git a/crates/gpui2/src/element.rs b/crates/gpui2/src/element.rs index d2a8efee83..ba90a7d7df 100644 --- a/crates/gpui2/src/element.rs +++ b/crates/gpui2/src/element.rs @@ -1,4 +1,3 @@ -pub use crate::paint_context::PaintContext; pub use crate::ViewContext; use anyhow::Result; use gpui::geometry::vector::Vector2F; @@ -22,7 +21,7 @@ pub trait Element: 'static + IntoElement { parent_origin: Vector2F, layout: &Layout, state: &mut Self::PaintState, - cx: &mut PaintContext, + cx: &mut ViewContext, ) where Self: Sized; @@ -40,7 +39,7 @@ pub trait Element: 'static + IntoElement { /// Used to make ElementState into a trait object, so we can wrap it in AnyElement. trait AnyStatefulElement { fn layout(&mut self, view: &mut V, cx: &mut ViewContext) -> Result; - fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext); + fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut ViewContext); } /// A wrapper around an element that stores its layout state. @@ -105,7 +104,7 @@ impl> AnyStatefulElement for StatefulElement { result } - fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext) { + fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut ViewContext) { self.phase = match std::mem::take(&mut self.phase) { ElementPhase::PostLayout { layout_id, @@ -149,7 +148,7 @@ impl AnyElement { self.0.layout(view, cx) } - pub fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext) { + pub fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut ViewContext) { self.0.paint(view, parent_origin, cx) } } diff --git a/crates/gpui2/src/elements/div.rs b/crates/gpui2/src/elements/div.rs index 598b564ab1..885b14f2dd 100644 --- a/crates/gpui2/src/elements/div.rs +++ b/crates/gpui2/src/elements/div.rs @@ -3,7 +3,6 @@ use std::{cell::Cell, rc::Rc}; use crate::{ element::{AnyElement, Element, IntoElement, Layout, ParentElement}, hsla, - paint_context::PaintContext, style::{CornerRadii, Overflow, Style, StyleHelpers, Styleable}, InteractionHandlers, Interactive, ViewContext, }; @@ -69,7 +68,7 @@ impl Element for Div { parent_origin: Vector2F, layout: &Layout, child_layouts: &mut Vec, - cx: &mut PaintContext, + cx: &mut ViewContext, ) where Self: Sized, { @@ -167,7 +166,7 @@ impl Div { bounds: RectF, overflow: Point, child_layout_ids: &[LayoutId], - cx: &mut PaintContext, + cx: &mut ViewContext, ) { if overflow.y == Overflow::Scroll || overflow.x == Overflow::Scroll { let mut scroll_max = Vector2F::zero(); @@ -214,7 +213,7 @@ impl Div { } } - fn paint_inspector(&self, parent_origin: Vector2F, layout: &Layout, cx: &mut PaintContext) { + fn paint_inspector(&self, parent_origin: Vector2F, layout: &Layout, cx: &mut ViewContext) { let style = self.styles.merged(); let bounds = layout.bounds + parent_origin; diff --git a/crates/gpui2/src/elements/hoverable.rs b/crates/gpui2/src/elements/hoverable.rs index ba8109038f..eec4b4ff61 100644 --- a/crates/gpui2/src/elements/hoverable.rs +++ b/crates/gpui2/src/elements/hoverable.rs @@ -1,7 +1,6 @@ use crate::{ element::{AnyElement, Element, IntoElement, Layout, ParentElement}, interactive::{InteractionHandlers, Interactive}, - paint_context::PaintContext, style::{Style, StyleHelpers, Styleable}, ViewContext, }; @@ -59,7 +58,7 @@ impl + Styleable> Element for Hoverable { parent_origin: Vector2F, layout: &Layout, paint_state: &mut Self::PaintState, - cx: &mut PaintContext, + cx: &mut ViewContext, ) where Self: Sized, { diff --git a/crates/gpui2/src/elements/img.rs b/crates/gpui2/src/elements/img.rs index 829a6d1116..866f2fa971 100644 --- a/crates/gpui2/src/elements/img.rs +++ b/crates/gpui2/src/elements/img.rs @@ -53,7 +53,7 @@ impl Element for Img { parent_origin: Vector2F, layout: &gpui::Layout, _: &mut Self::PaintState, - cx: &mut crate::paint_context::PaintContext, + cx: &mut crate::ViewContext, ) where Self: Sized, { diff --git a/crates/gpui2/src/elements/pressable.rs b/crates/gpui2/src/elements/pressable.rs index 1b696e7ef6..960342a5e6 100644 --- a/crates/gpui2/src/elements/pressable.rs +++ b/crates/gpui2/src/elements/pressable.rs @@ -1,7 +1,6 @@ use crate::{ element::{AnyElement, Element, IntoElement, Layout, ParentElement}, interactive::{InteractionHandlers, Interactive}, - paint_context::PaintContext, style::{Style, StyleHelpers, Styleable}, ViewContext, }; @@ -59,7 +58,7 @@ impl + Styleable> Element for Pressable { parent_origin: Vector2F, layout: &Layout, paint_state: &mut Self::PaintState, - cx: &mut PaintContext, + cx: &mut ViewContext, ) where Self: Sized, { diff --git a/crates/gpui2/src/elements/svg.rs b/crates/gpui2/src/elements/svg.rs index c17cdbe3c6..07f31d8b6e 100644 --- a/crates/gpui2/src/elements/svg.rs +++ b/crates/gpui2/src/elements/svg.rs @@ -49,7 +49,7 @@ impl Element for Svg { parent_origin: Vector2F, layout: &Layout, _: &mut Self::PaintState, - cx: &mut crate::paint_context::PaintContext, + cx: &mut crate::ViewContext, ) where Self: Sized, { diff --git a/crates/gpui2/src/elements/text.rs b/crates/gpui2/src/elements/text.rs index 3b3acb4523..6f89375df0 100644 --- a/crates/gpui2/src/elements/text.rs +++ b/crates/gpui2/src/elements/text.rs @@ -1,6 +1,5 @@ use crate::{ element::{Element, IntoElement, Layout}, - paint_context::PaintContext, ViewContext, }; use anyhow::Result; @@ -71,7 +70,7 @@ impl Element for Text { parent_origin: Vector2F, layout: &Layout, paint_state: &mut Self::PaintState, - cx: &mut PaintContext, + cx: &mut ViewContext, ) { let bounds = layout.bounds + parent_origin; diff --git a/crates/gpui2/src/gpui2.rs b/crates/gpui2/src/gpui2.rs index 11d5279ec1..355697595f 100644 --- a/crates/gpui2/src/gpui2.rs +++ b/crates/gpui2/src/gpui2.rs @@ -3,7 +3,6 @@ pub mod color; pub mod element; pub mod elements; pub mod interactive; -pub mod paint_context; pub mod style; pub mod view; pub mod view_context; diff --git a/crates/gpui2/src/interactive.rs b/crates/gpui2/src/interactive.rs index aa96272860..4533e7dbec 100644 --- a/crates/gpui2/src/interactive.rs +++ b/crates/gpui2/src/interactive.rs @@ -6,7 +6,7 @@ use gpui::{ use smallvec::SmallVec; use std::{cell::Cell, rc::Rc}; -use crate::element::PaintContext; +use crate::ViewContext; pub trait Interactive { fn interaction_handlers(&mut self) -> &mut InteractionHandlers; @@ -121,7 +121,7 @@ pub struct InteractionHandlers { } impl InteractionHandlers { - pub fn paint(&self, order: u32, bounds: RectF, cx: &mut PaintContext) { + pub fn paint(&self, order: u32, bounds: RectF, cx: &mut ViewContext) { for handler in self.mouse_down.iter().cloned() { cx.on_event(order, move |view, event: &MouseButtonEvent, cx| { if event.is_down && bounds.contains_point(event.position) { diff --git a/crates/gpui2/src/paint_context.rs b/crates/gpui2/src/paint_context.rs deleted file mode 100644 index 9f8548d519..0000000000 --- a/crates/gpui2/src/paint_context.rs +++ /dev/null @@ -1,50 +0,0 @@ -use anyhow::{anyhow, Result}; -use derive_more::{Deref, DerefMut}; -pub use gpui::taffy::tree::NodeId; -use gpui::{ - scene::EventHandler, EventContext, Layout, LayoutId, PaintContext as LegacyPaintContext, -}; -use std::{any::TypeId, rc::Rc}; - -#[derive(Deref, DerefMut)] -pub struct PaintContext<'a, 'b, 'c, 'd, V> { - #[deref] - #[deref_mut] - pub(crate) legacy_cx: &'d mut LegacyPaintContext<'a, 'b, 'c, V>, -} - -impl<'a, 'b, 'c, 'd, V: 'static> PaintContext<'a, 'b, 'c, 'd, V> { - pub fn new(legacy_cx: &'d mut LegacyPaintContext<'a, 'b, 'c, V>) -> Self { - Self { legacy_cx } - } - - pub fn on_event( - &mut self, - order: u32, - handler: impl Fn(&mut V, &E, &mut EventContext) + 'static, - ) { - let view = self.weak_handle(); - - self.scene().event_handlers.push(EventHandler { - order, - handler: Rc::new(move |event, window_cx| { - if let Some(view) = view.upgrade(window_cx) { - view.update(window_cx, |view, view_cx| { - let mut event_cx = EventContext::new(view_cx); - handler(view, event.downcast_ref().unwrap(), &mut event_cx); - event_cx.bubble - }) - } else { - true - } - }), - event_type: TypeId::of::(), - }) - } - - pub(crate) fn computed_layout(&mut self, layout_id: LayoutId) -> Result { - self.layout_engine() - .ok_or_else(|| anyhow!("no layout engine present"))? - .computed_layout(layout_id) - } -} diff --git a/crates/gpui2/src/style.rs b/crates/gpui2/src/style.rs index 8a02dda5d7..e3e0f5b0c4 100644 --- a/crates/gpui2/src/style.rs +++ b/crates/gpui2/src/style.rs @@ -2,7 +2,7 @@ use crate::{ color::Hsla, elements::hoverable::{hoverable, Hoverable}, elements::pressable::{pressable, Pressable}, - paint_context::PaintContext, + ViewContext, }; pub use fonts::Style as FontStyle; pub use fonts::Weight as FontWeight; @@ -164,7 +164,7 @@ impl Style { } /// Paints the background of an element styled with this style. - pub fn paint_background(&self, bounds: RectF, cx: &mut PaintContext) { + pub fn paint_background(&self, bounds: RectF, cx: &mut ViewContext) { let rem_size = cx.rem_size(); if let Some(color) = self.fill.as_ref().and_then(Fill::color) { cx.scene().push_quad(gpui::Quad { @@ -177,7 +177,7 @@ impl Style { } /// Paints the foreground of an element styled with this style. - pub fn paint_foreground(&self, bounds: RectF, cx: &mut PaintContext) { + pub fn paint_foreground(&self, bounds: RectF, cx: &mut ViewContext) { let rem_size = cx.rem_size(); if let Some(color) = self.border_color { diff --git a/crates/gpui2/src/view_context.rs b/crates/gpui2/src/view_context.rs index 43d8093240..d6c0960a34 100644 --- a/crates/gpui2/src/view_context.rs +++ b/crates/gpui2/src/view_context.rs @@ -1,7 +1,9 @@ +use std::{any::TypeId, rc::Rc}; + use crate::{element::LayoutId, style::Style}; use anyhow::{anyhow, Result}; use derive_more::{Deref, DerefMut}; -use gpui::{geometry::Size, MeasureParams}; +use gpui::{geometry::Size, scene::EventHandler, EventContext, Layout, MeasureParams}; pub use gpui::{taffy::tree::NodeId, ViewContext as LegacyViewContext}; #[derive(Deref, DerefMut)] @@ -44,4 +46,34 @@ impl<'a, 'b, 'c, V: 'static> ViewContext<'a, 'b, 'c, V> { Ok(layout_id) } + + pub fn on_event( + &mut self, + order: u32, + handler: impl Fn(&mut V, &E, &mut EventContext) + 'static, + ) { + let view = self.weak_handle(); + + self.scene().event_handlers.push(EventHandler { + order, + handler: Rc::new(move |event, window_cx| { + if let Some(view) = view.upgrade(window_cx) { + view.update(window_cx, |view, view_cx| { + let mut event_cx = EventContext::new(view_cx); + handler(view, event.downcast_ref().unwrap(), &mut event_cx); + event_cx.bubble + }) + } else { + true + } + }), + event_type: TypeId::of::(), + }) + } + + pub(crate) fn computed_layout(&mut self, layout_id: LayoutId) -> Result { + self.layout_engine() + .ok_or_else(|| anyhow!("no layout engine present"))? + .computed_layout(layout_id) + } } diff --git a/crates/gpui2_macros/src/derive_element.rs b/crates/gpui2_macros/src/derive_element.rs index a769437676..304caa99e6 100644 --- a/crates/gpui2_macros/src/derive_element.rs +++ b/crates/gpui2_macros/src/derive_element.rs @@ -80,7 +80,7 @@ pub fn derive_element(input: TokenStream) -> TokenStream { parent_origin: gpui2::Vector2F, _: &gpui2::element::Layout, rendered_element: &mut Self::PaintState, - cx: &mut gpui2::element::PaintContext, + cx: &mut gpui2::ViewContext, ) { rendered_element.paint(view, parent_origin, cx); } diff --git a/crates/gpui_macros/src/gpui_macros.rs b/crates/gpui_macros/src/gpui_macros.rs index f685730744..aa55c27eaa 100644 --- a/crates/gpui_macros/src/gpui_macros.rs +++ b/crates/gpui_macros/src/gpui_macros.rs @@ -342,7 +342,7 @@ pub fn element_derive(input: TokenStream) -> TokenStream { visible_bounds: gpui::geometry::rect::RectF, element: &mut gpui::elements::AnyElement, view: &mut V, - cx: &mut gpui::PaintContext, + cx: &mut gpui::ViewContext, ) { element.paint(bounds.origin(), visible_bounds, view, cx); } diff --git a/crates/storybook/src/theme.rs b/crates/storybook/src/theme.rs index 45327e1ffc..0a86a61499 100644 --- a/crates/storybook/src/theme.rs +++ b/crates/storybook/src/theme.rs @@ -1,7 +1,6 @@ use gpui2::{ - color::Hsla, - element::{Element, PaintContext}, - serde_json, AppContext, IntoElement, Vector2F, ViewContext, WindowContext, + color::Hsla, element::Element, serde_json, AppContext, IntoElement, Vector2F, ViewContext, + WindowContext, }; use serde::{de::Visitor, Deserialize, Deserializer}; use std::{collections::HashMap, fmt, marker::PhantomData}; @@ -162,7 +161,7 @@ impl> Element for Themed { parent_origin: Vector2F, layout: &gpui2::Layout, state: &mut Self::PaintState, - cx: &mut PaintContext, + cx: &mut ViewContext, ) where Self: Sized, { diff --git a/crates/terminal_view/src/terminal_element.rs b/crates/terminal_view/src/terminal_element.rs index 30bea09627..30dbccf455 100644 --- a/crates/terminal_view/src/terminal_element.rs +++ b/crates/terminal_view/src/terminal_element.rs @@ -10,8 +10,8 @@ use gpui::{ platform::{CursorStyle, MouseButton}, serde_json::json, text_layout::{Line, RunStyle}, - AnyElement, Element, EventContext, FontCache, ModelContext, MouseRegion, PaintContext, Quad, - SizeConstraint, TextLayoutCache, ViewContext, WeakModelHandle, WindowContext, + AnyElement, Element, EventContext, FontCache, ModelContext, MouseRegion, Quad, SizeConstraint, + TextLayoutCache, ViewContext, WeakModelHandle, WindowContext, }; use itertools::Itertools; use language::CursorShape; @@ -733,7 +733,7 @@ impl Element for TerminalElement { visible_bounds: RectF, layout: &mut Self::LayoutState, view: &mut TerminalView, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index 69d4b1da00..23541e1f06 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -25,8 +25,8 @@ use gpui::{ keymap_matcher::KeymapContext, platform::{CursorStyle, MouseButton, NavigationDirection, PromptLevel}, Action, AnyViewHandle, AnyWeakViewHandle, AppContext, AsyncAppContext, Entity, EventContext, - ModelHandle, MouseRegion, PaintContext, Quad, Task, View, ViewContext, ViewHandle, - WeakViewHandle, WindowContext, + ModelHandle, MouseRegion, Quad, Task, View, ViewContext, ViewHandle, WeakViewHandle, + WindowContext, }; use project::{Project, ProjectEntryId, ProjectPath}; use serde::Deserialize; @@ -2011,7 +2011,7 @@ impl Element for PaneBackdrop { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut V, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let background = theme::current(cx).editor.background; diff --git a/crates/workspace/src/pane_group.rs b/crates/workspace/src/pane_group.rs index c3729d0976..bffdce0f3e 100644 --- a/crates/workspace/src/pane_group.rs +++ b/crates/workspace/src/pane_group.rs @@ -594,7 +594,7 @@ mod element { json::{self, ToJson}, platform::{CursorStyle, MouseButton}, scene::MouseDrag, - AnyElement, Axis, CursorRegion, Element, EventContext, MouseRegion, PaintContext, RectFExt, + AnyElement, Axis, CursorRegion, Element, EventContext, MouseRegion, RectFExt, SizeConstraint, Vector2FExt, ViewContext, }; @@ -855,7 +855,7 @@ mod element { visible_bounds: RectF, remaining_space: &mut Self::LayoutState, view: &mut Workspace, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let can_resize = settings::get::(cx).active_pane_magnification == 1.; let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); diff --git a/crates/workspace/src/status_bar.rs b/crates/workspace/src/status_bar.rs index 0da5162353..b62dae2114 100644 --- a/crates/workspace/src/status_bar.rs +++ b/crates/workspace/src/status_bar.rs @@ -8,8 +8,8 @@ use gpui::{ vector::{vec2f, Vector2F}, }, json::{json, ToJson}, - AnyElement, AnyViewHandle, Entity, PaintContext, SizeConstraint, Subscription, View, - ViewContext, ViewHandle, WindowContext, + AnyElement, AnyViewHandle, Entity, SizeConstraint, Subscription, View, ViewContext, ViewHandle, + WindowContext, }; pub trait StatusItemView: View { @@ -230,7 +230,7 @@ impl Element for StatusBarElement { visible_bounds: RectF, _: &mut Self::LayoutState, view: &mut StatusBar, - cx: &mut PaintContext, + cx: &mut ViewContext, ) -> Self::PaintState { let origin_y = bounds.upper_right().y(); let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default();