From 79e1e1a7471767e43c832ba98219c9de387b089b Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 2 Oct 2023 13:16:10 -0600 Subject: [PATCH] Checkpoint --- crates/gpui3/src/elements/div.rs | 3 +-- crates/gpui3/src/elements/img.rs | 3 ++- crates/gpui3/src/geometry.rs | 44 +++++++++++++++++++++++++++----- crates/gpui3/src/style.rs | 44 +++++++++++++++----------------- crates/gpui3/src/window.rs | 4 +++ 5 files changed, 65 insertions(+), 33 deletions(-) diff --git a/crates/gpui3/src/elements/div.rs b/crates/gpui3/src/elements/div.rs index c763619de1..6bae65c5a2 100644 --- a/crates/gpui3/src/elements/div.rs +++ b/crates/gpui3/src/elements/div.rs @@ -55,7 +55,7 @@ impl Element for Div { let Layout { order, bounds } = layout; let style = self.computed_style(); - style.paint_background(bounds, cx); + style.paint(order, bounds, cx); let overflow = &style.overflow; // // todo!("support only one dimension being hidden") // if style.overflow.y != Overflow::Visible || style.overflow.x != Overflow::Visible { @@ -69,7 +69,6 @@ impl Element for Div { } else { self.paint_children(overflow, state, cx)?; } - style.paint_foreground(bounds, cx); self.handle_scroll(order, bounds, style.overflow.clone(), child_layouts, cx); diff --git a/crates/gpui3/src/elements/img.rs b/crates/gpui3/src/elements/img.rs index 0bef59e422..63f3e78d3d 100644 --- a/crates/gpui3/src/elements/img.rs +++ b/crates/gpui3/src/elements/img.rs @@ -49,9 +49,10 @@ impl Element for Img { cx: &mut crate::ViewContext, ) -> Result<()> { let style = self.computed_style(); + let order = layout.order; let bounds = layout.bounds; - style.paint_background(bounds, cx); + style.paint(order, bounds, cx); // if let Some(uri) = &self.uri { // let image_future = cx.image_cache.get(uri.clone()); diff --git a/crates/gpui3/src/geometry.rs b/crates/gpui3/src/geometry.rs index c587096e7c..4f187f3252 100644 --- a/crates/gpui3/src/geometry.rs +++ b/crates/gpui3/src/geometry.rs @@ -307,6 +307,24 @@ unsafe impl Zeroable for Edges {} unsafe impl Pod for Edges {} +impl Edges { + pub fn map U>(&self, f: F) -> Edges { + Edges { + top: f(&self.top), + right: f(&self.right), + bottom: f(&self.bottom), + left: f(&self.left), + } + } + + pub fn any bool>(&self, predicate: F) -> bool { + predicate(&self.top) + || predicate(&self.right) + || predicate(&self.bottom) + || predicate(&self.left) + } +} + impl Edges { pub fn auto() -> Self { Self { @@ -358,12 +376,6 @@ impl Edges { } } -impl Edges { - pub fn is_empty(&self) -> bool { - self.top == px(0.) && self.right == px(0.) && self.bottom == px(0.) && self.left == px(0.) - } -} - #[derive(Refineable, Clone, Default, Debug)] #[refineable(debug)] #[repr(C)] @@ -374,6 +386,17 @@ pub struct Corners { pub bottom_left: T, } +impl Corners { + pub fn map U>(&self, f: F) -> Corners { + Corners { + top_left: f(&self.top_left), + top_right: f(&self.top_right), + bottom_right: f(&self.bottom_right), + bottom_left: f(&self.bottom_left), + } + } +} + impl> Mul for Corners { type Output = Self; @@ -537,6 +560,15 @@ pub enum AbsoluteLength { Rems(Rems), } +impl AbsoluteLength { + pub fn is_zero(&self) -> bool { + match self { + AbsoluteLength::Pixels(px) => px.0 == 0., + AbsoluteLength::Rems(rems) => rems.0 == 0., + } + } +} + impl From for AbsoluteLength { fn from(pixels: Pixels) -> Self { AbsoluteLength::Pixels(pixels) diff --git a/crates/gpui3/src/style.rs b/crates/gpui3/src/style.rs index a8bac7ed78..863736e4a3 100644 --- a/crates/gpui3/src/style.rs +++ b/crates/gpui3/src/style.rs @@ -1,7 +1,7 @@ use crate::{ phi, rems, AbsoluteLength, Bounds, Corners, CornersRefinement, DefiniteLength, Edges, EdgesRefinement, Font, FontFeatures, FontStyle, FontWeight, Hsla, Length, Pixels, Point, - PointRefinement, Rems, Result, RunStyle, SharedString, Size, SizeRefinement, ViewContext, + PointRefinement, Quad, Rems, Result, RunStyle, SharedString, Size, SizeRefinement, ViewContext, WindowContext, }; use refineable::Refineable; @@ -180,23 +180,28 @@ impl Style { } /// Paints the background of an element styled with this style. - pub fn paint_background(&self, _bounds: Bounds, cx: &mut ViewContext) { - let _rem_size = cx.rem_size(); - if let Some(_color) = self.fill.as_ref().and_then(Fill::color) { - todo!(); + pub fn paint(&self, order: u32, bounds: Bounds, cx: &mut ViewContext) { + let rem_size = cx.rem_size(); + + let background_color = self.fill.as_ref().and_then(Fill::color); + if background_color.is_some() || self.is_border_visible() { + cx.scene().insert(Quad { + order, + bounds, + clip_bounds: bounds, // todo! + clip_corner_radii: self.corner_radii.map(|length| length.to_pixels(rem_size)), + background: background_color.unwrap_or_default(), + border_color: self.border_color.unwrap_or_default(), + corner_radii: self.corner_radii.map(|length| length.to_pixels(rem_size)), + border_widths: self.border_widths.map(|length| length.to_pixels(rem_size)), + }); } } - /// Paints the foreground of an element styled with this style. - pub fn paint_foreground(&self, _bounds: Bounds, cx: &mut ViewContext) { - let rem_size = cx.rem_size(); - - if let Some(_color) = self.border_color { - let border = self.border_widths.to_pixels(rem_size); - if !border.is_empty() { - todo!(); - } - } + fn is_border_visible(&self) -> bool { + self.border_color + .map_or(false, |color| !color.is_transparent()) + && self.border_widths.any(|length| !length.is_zero()) } } @@ -271,15 +276,6 @@ impl From for Fill { } } -#[derive(Clone, Refineable, Default, Debug)] -#[refineable(debug)] -pub struct CornerRadii { - pub top_left: AbsoluteLength, - pub top_right: AbsoluteLength, - pub bottom_left: AbsoluteLength, - pub bottom_right: AbsoluteLength, -} - impl From for HighlightStyle { fn from(other: TextStyle) -> Self { Self::from(&other) diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rs index 25b4dc1df1..ccbbb247d2 100644 --- a/crates/gpui3/src/window.rs +++ b/crates/gpui3/src/window.rs @@ -121,6 +121,10 @@ impl<'a, 'w> WindowContext<'a, 'w> { self.window.mouse_position } + pub fn scene(&mut self) -> &mut Scene { + &mut self.window.scene + } + pub(crate) fn draw(&mut self) -> Result<()> { let unit_entity = self.unit_entity.clone(); self.update_entity(&unit_entity, |_, cx| {