diff --git a/crates/gpui3/src/scene.rs b/crates/gpui3/src/scene.rs index 5f55dd73fb..fb66da7f5c 100644 --- a/crates/gpui3/src/scene.rs +++ b/crates/gpui3/src/scene.rs @@ -7,12 +7,12 @@ use smallvec::SmallVec; // Exported to metal pub type PointF = Point; -pub type StackingOrder = SmallVec<[u32; 16]>; +pub type LayerId = SmallVec<[u32; 16]>; #[derive(Debug)] pub struct Scene { pub(crate) scale_factor: f32, - pub(crate) layers: BTreeMap, + pub(crate) layers: BTreeMap, } impl Scene { @@ -30,7 +30,7 @@ impl Scene { } } - pub fn insert(&mut self, stacking_order: StackingOrder, primitive: impl Into) { + pub fn insert(&mut self, stacking_order: LayerId, primitive: impl Into) { let layer = self.layers.entry(stacking_order).or_default(); let primitive = primitive.into(); diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rs index 9e9b4c36b6..50533f8f28 100644 --- a/crates/gpui3/src/window.rs +++ b/crates/gpui3/src/window.rs @@ -1,8 +1,8 @@ use crate::{ - px, AnyView, AppContext, AvailableSpace, Bounds, Context, Effect, Element, EntityId, FontId, - GlyphId, GlyphRasterParams, Handle, Hsla, IsZero, LayoutId, MainThread, MainThreadOnly, - MonochromeSprite, Pixels, PlatformAtlas, PlatformWindow, Point, Reference, Scene, Size, - StackContext, StackingOrder, Style, TaffyLayoutEngine, WeakHandle, WindowOptions, + px, AnyView, AppContext, AvailableSpace, Bounds, Context, Corners, Effect, Element, EntityId, + FontId, GlyphId, GlyphRasterParams, Handle, Hsla, IsZero, LayerId, LayoutId, MainThread, + MainThreadOnly, MonochromeSprite, Pixels, PlatformAtlas, PlatformWindow, Point, Reference, + Scene, Size, StackContext, Style, TaffyLayoutEngine, WeakHandle, WindowOptions, SUBPIXEL_VARIANTS, }; use anyhow::Result; @@ -22,7 +22,8 @@ pub struct Window { layout_engine: TaffyLayoutEngine, pub(crate) root_view: Option>, mouse_position: Point, - current_layer_id: StackingOrder, + current_layer_id: LayerId, + content_mask_stack: Vec, pub(crate) scene: Scene, pub(crate) dirty: bool, } @@ -64,12 +65,19 @@ impl Window { root_view: None, mouse_position, current_layer_id: SmallVec::new(), + content_mask_stack: Vec::new(), scene: Scene::new(scale_factor), dirty: true, } } } +#[derive(Clone, Debug)] +pub struct ContentMask { + bounds: Bounds, + corner_radii: Corners, +} + pub struct WindowContext<'a, 'w> { app: Reference<'a, AppContext>, window: Reference<'w, Window>, @@ -145,10 +153,41 @@ impl<'a, 'w> WindowContext<'a, 'w> { result } - pub fn current_layer_id(&self) -> StackingOrder { + pub fn clip( + &mut self, + bounds: Bounds, + corner_radii: Corners, + f: impl FnOnce(&mut Self) -> R, + ) -> R { + let clip_mask = ContentMask { + bounds, + corner_radii, + }; + + self.window.content_mask_stack.push(clip_mask); + let result = f(self); + self.window.content_mask_stack.pop(); + result + } + + pub fn current_layer_id(&self) -> LayerId { self.window.current_layer_id.clone() } + pub fn current_clipping_mask(&self) -> ContentMask { + self.window + .content_mask_stack + .last() + .cloned() + .unwrap_or_else(|| ContentMask { + bounds: Bounds { + origin: Point::default(), + size: self.window.content_size, + }, + corner_radii: Default::default(), + }) + } + pub fn run_on_main( &self, f: impl FnOnce(&mut MainThread) -> R + Send + 'static, diff --git a/crates/storybook2/src/workspace.rs b/crates/storybook2/src/workspace.rs index c3e815242f..15f26e52a5 100644 --- a/crates/storybook2/src/workspace.rs +++ b/crates/storybook2/src/workspace.rs @@ -4,8 +4,8 @@ use crate::{ themes::rose_pine_dawn, }; use gpui3::{ - black, div, img, svg, view, white, Context, Element, ParentElement, RootView, StyleHelpers, - View, ViewContext, WindowContext, + div, img, svg, view, Context, Element, ParentElement, RootView, StyleHelpers, View, + ViewContext, WindowContext, }; pub struct Workspace {