diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/src/overlay.rs | 16 | ||||
-rw-r--r-- | core/src/overlay/element.rs | 37 | ||||
-rw-r--r-- | core/src/overlay/group.rs | 26 | ||||
-rw-r--r-- | core/src/renderer.rs | 15 | ||||
-rw-r--r-- | core/src/renderer/null.rs | 4 | ||||
-rw-r--r-- | core/src/text.rs | 6 | ||||
-rw-r--r-- | core/src/widget/text.rs | 10 |
7 files changed, 82 insertions, 32 deletions
diff --git a/core/src/overlay.rs b/core/src/overlay.rs index 1fa994e4..2e05db93 100644 --- a/core/src/overlay.rs +++ b/core/src/overlay.rs @@ -91,9 +91,23 @@ where /// /// By default, it returns true if the bounds of the `layout` contain /// the `cursor_position`. - fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { + fn is_over( + &self, + layout: Layout<'_>, + _renderer: &Renderer, + cursor_position: Point, + ) -> bool { layout.bounds().contains(cursor_position) } + + /// Returns the nested overlay of the [`Overlay`], if there is any. + fn overlay<'a>( + &'a mut self, + _layout: Layout<'_>, + _renderer: &Renderer, + ) -> Option<Element<'a, Message, Renderer>> { + None + } } /// Returns a [`Group`] of overlay [`Element`] children. diff --git a/core/src/overlay/element.rs b/core/src/overlay/element.rs index be67fc76..c2134343 100644 --- a/core/src/overlay/element.rs +++ b/core/src/overlay/element.rs @@ -112,8 +112,22 @@ where } /// Returns true if the cursor is over the [`Element`]. - pub fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { - self.overlay.is_over(layout, cursor_position) + pub fn is_over( + &self, + layout: Layout<'_>, + renderer: &Renderer, + cursor_position: Point, + ) -> bool { + self.overlay.is_over(layout, renderer, cursor_position) + } + + /// Returns the nested overlay of the [`Element`], if there is any. + pub fn overlay<'b>( + &'b mut self, + layout: Layout<'_>, + renderer: &Renderer, + ) -> Option<Element<'b, Message, Renderer>> { + self.overlay.overlay(layout, renderer) } } @@ -248,7 +262,22 @@ where self.content.draw(renderer, theme, style, layout, cursor) } - fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { - self.content.is_over(layout, cursor_position) + fn is_over( + &self, + layout: Layout<'_>, + renderer: &Renderer, + cursor_position: Point, + ) -> bool { + self.content.is_over(layout, renderer, cursor_position) + } + + fn overlay<'b>( + &'b mut self, + layout: Layout<'_>, + renderer: &Renderer, + ) -> Option<Element<'b, B, Renderer>> { + self.content + .overlay(layout, renderer) + .map(|overlay| overlay.map(self.mapper)) } } diff --git a/core/src/overlay/group.rs b/core/src/overlay/group.rs index e770abf8..deffaad0 100644 --- a/core/src/overlay/group.rs +++ b/core/src/overlay/group.rs @@ -147,11 +147,33 @@ where }); } - fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { + fn is_over( + &self, + layout: Layout<'_>, + renderer: &Renderer, + cursor_position: Point, + ) -> bool { self.children .iter() .zip(layout.children()) - .any(|(child, layout)| child.is_over(layout, cursor_position)) + .any(|(child, layout)| { + child.is_over(layout, renderer, cursor_position) + }) + } + + fn overlay<'b>( + &'b mut self, + layout: Layout<'_>, + renderer: &Renderer, + ) -> Option<overlay::Element<'b, Message, Renderer>> { + let children = self + .children + .iter_mut() + .zip(layout.children()) + .filter_map(|(child, layout)| child.overlay(layout, renderer)) + .collect::<Vec<_>>(); + + (!children.is_empty()).then(|| Group::with_children(children).overlay()) } } diff --git a/core/src/renderer.rs b/core/src/renderer.rs index 7c73d2e4..1b327e56 100644 --- a/core/src/renderer.rs +++ b/core/src/renderer.rs @@ -5,26 +5,13 @@ mod null; #[cfg(debug_assertions)] pub use null::Null; -use crate::layout; -use crate::{Background, BorderRadius, Color, Element, Rectangle, Vector}; +use crate::{Background, BorderRadius, Color, Rectangle, Vector}; /// A component that can be used by widgets to draw themselves on a screen. pub trait Renderer: Sized { /// The supported theme of the [`Renderer`]. type Theme; - /// Lays out the elements of a user interface. - /// - /// You should override this if you need to perform any operations before or - /// after layouting. For instance, trimming the measurements cache. - fn layout<Message>( - &mut self, - element: &Element<'_, Message, Self>, - limits: &layout::Limits, - ) -> layout::Node { - element.as_widget().layout(self, limits) - } - /// Draws the primitives recorded in the given closure in a new layer. /// /// The layer will clip its contents to the provided `bounds`. diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index f0cc952e..5d49699e 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -64,8 +64,8 @@ impl text::Renderer for Null { _font: Font, _bounds: Size, _shaping: text::Shaping, - ) -> (f32, f32) { - (0.0, 20.0) + ) -> Size { + Size::new(0.0, 20.0) } fn hit_test( diff --git a/core/src/text.rs b/core/src/text.rs index c154cc27..fc8aa20e 100644 --- a/core/src/text.rs +++ b/core/src/text.rs @@ -163,7 +163,7 @@ pub trait Renderer: crate::Renderer { font: Self::Font, bounds: Size, shaping: Shaping, - ) -> (f32, f32); + ) -> Size; /// Measures the width of the text as if it were laid out in a single line. fn measure_width( @@ -173,7 +173,7 @@ pub trait Renderer: crate::Renderer { font: Self::Font, shaping: Shaping, ) -> f32 { - let (width, _) = self.measure( + let bounds = self.measure( content, size, LineHeight::Absolute(Pixels(size)), @@ -182,7 +182,7 @@ pub trait Renderer: crate::Renderer { shaping, ); - width + bounds.width } /// Tests whether the provided point is within the boundaries of text diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index e934a2f5..79df2b02 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -5,7 +5,7 @@ use crate::mouse; use crate::renderer; use crate::text; use crate::widget::Tree; -use crate::{Color, Element, Layout, Length, Pixels, Rectangle, Size, Widget}; +use crate::{Color, Element, Layout, Length, Pixels, Rectangle, Widget}; use std::borrow::Cow; @@ -139,18 +139,16 @@ where let size = self.size.unwrap_or_else(|| renderer.default_size()); - let bounds = limits.max(); - - let (width, height) = renderer.measure( + let bounds = renderer.measure( &self.content, size, self.line_height, self.font.unwrap_or_else(|| renderer.default_font()), - bounds, + limits.max(), self.shaping, ); - let size = limits.resolve(Size::new(width, height)); + let size = limits.resolve(bounds); layout::Node::new(size) } |