From 03b34931383e701c39c653a7662a616fe21a0947 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 14 Oct 2021 16:07:22 +0700 Subject: Remove trait-specific draw logic in `iced_native` --- native/src/widget/pane_grid.rs | 170 +++++++++++++---------------------------- 1 file changed, 51 insertions(+), 119 deletions(-) (limited to 'native/src/widget/pane_grid.rs') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 26a72409..b4a8cdb1 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -32,7 +32,6 @@ use crate::event::{self, Event}; use crate::layout; use crate::mouse; use crate::overlay; -use crate::row; use crate::touch; use crate::{ Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector, @@ -480,66 +479,56 @@ where layout: Layout<'_>, cursor_position: Point, viewport: &Rectangle, - ) -> Renderer::Output { - let picked_split = self - .state - .picked_split() - .and_then(|(split, axis)| { - let bounds = layout.bounds(); - - let splits = self - .state - .split_regions(f32::from(self.spacing), bounds.size()); - - let (_axis, region, ratio) = splits.get(&split)?; - - let region = axis.split_line_bounds( - *region, - *ratio, - f32::from(self.spacing), - ); - - Some((axis, region + Vector::new(bounds.x, bounds.y), true)) - }) - .or_else(|| match self.on_resize { - Some((leeway, _)) => { - let bounds = layout.bounds(); - - let relative_cursor = Point::new( - cursor_position.x - bounds.x, - cursor_position.y - bounds.y, - ); - - let splits = self - .state - .split_regions(f32::from(self.spacing), bounds.size()); - - let (_split, axis, region) = hovered_split( - splits.iter(), - f32::from(self.spacing + leeway), - relative_cursor, - )?; - - Some(( - axis, - region + Vector::new(bounds.x, bounds.y), - false, - )) - } - None => None, - }); - - self::Renderer::draw( - renderer, - defaults, - &self.elements, - self.state.picked_pane(), - picked_split, - layout, - &self.style, - cursor_position, - viewport, - ) + ) { + // let picked_split = self + // .state + // .picked_split() + // .and_then(|(split, axis)| { + // let bounds = layout.bounds(); + + // let splits = self + // .state + // .split_regions(f32::from(self.spacing), bounds.size()); + + // let (_axis, region, ratio) = splits.get(&split)?; + + // let region = axis.split_line_bounds( + // *region, + // *ratio, + // f32::from(self.spacing), + // ); + + // Some((axis, region + Vector::new(bounds.x, bounds.y), true)) + // }) + // .or_else(|| match self.on_resize { + // Some((leeway, _)) => { + // let bounds = layout.bounds(); + + // let relative_cursor = Point::new( + // cursor_position.x - bounds.x, + // cursor_position.y - bounds.y, + // ); + + // let splits = self + // .state + // .split_regions(f32::from(self.spacing), bounds.size()); + + // let (_split, axis, region) = hovered_split( + // splits.iter(), + // f32::from(self.spacing + leeway), + // relative_cursor, + // )?; + + // Some(( + // axis, + // region + Vector::new(bounds.x, bounds.y), + // false, + // )) + // } + // None => None, + // }); + + // TODO } fn hash_layout(&self, state: &mut Hasher) { @@ -578,69 +567,12 @@ where pub trait Renderer: crate::Renderer + container::Renderer + Sized { /// The style supported by this renderer. type Style: Default; - - /// Draws a [`PaneGrid`]. - /// - /// It receives: - /// - the elements of the [`PaneGrid`] - /// - the [`Pane`] that is currently being dragged - /// - the [`Axis`] that is currently being resized - /// - the [`Layout`] of the [`PaneGrid`] and its elements - /// - the cursor position - fn draw( - &mut self, - defaults: &Self::Defaults, - content: &[(Pane, Content<'_, Message, Self>)], - dragging: Option<(Pane, Point)>, - resizing: Option<(Axis, Rectangle, bool)>, - layout: Layout<'_>, - style: &::Style, - cursor_position: Point, - viewport: &Rectangle, - ) -> Self::Output; - - /// Draws a [`Pane`]. - /// - /// It receives: - /// - the [`TitleBar`] of the [`Pane`], if any - /// - the [`Content`] of the [`Pane`] - /// - the [`Layout`] of the [`Pane`] and its elements - /// - the cursor position - fn draw_pane( - &mut self, - defaults: &Self::Defaults, - bounds: Rectangle, - style: &::Style, - title_bar: Option<(&TitleBar<'_, Message, Self>, Layout<'_>)>, - body: (&Element<'_, Message, Self>, Layout<'_>), - cursor_position: Point, - viewport: &Rectangle, - ) -> Self::Output; - - /// Draws a [`TitleBar`]. - /// - /// It receives: - /// - the bounds, style of the [`TitleBar`] - /// - the style of the [`TitleBar`] - /// - the content of the [`TitleBar`] with its layout - /// - the controls of the [`TitleBar`] with their [`Layout`], if any - /// - the cursor position - fn draw_title_bar( - &mut self, - defaults: &Self::Defaults, - bounds: Rectangle, - style: &::Style, - content: (&Element<'_, Message, Self>, Layout<'_>), - controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>, - cursor_position: Point, - viewport: &Rectangle, - ) -> Self::Output; } impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where - Renderer: 'a + self::Renderer + row::Renderer, + Renderer: 'a + self::Renderer, Message: 'a, { fn from( -- cgit From edea093350e1b576e2b7db50c525e7fa5c3bea9f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 15:19:04 +0700 Subject: Move `Defaults` from `iced_graphics` to `iced_native` --- native/src/widget/pane_grid.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'native/src/widget/pane_grid.rs') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index b4a8cdb1..1e63fdeb 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -32,6 +32,7 @@ use crate::event::{self, Event}; use crate::layout; use crate::mouse; use crate::overlay; +use crate::renderer; use crate::touch; use crate::{ Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector, @@ -475,7 +476,7 @@ where fn draw( &self, renderer: &mut Renderer, - defaults: &Renderer::Defaults, + style: &renderer::Style, layout: Layout<'_>, cursor_position: Point, viewport: &Rectangle, -- cgit From d61cb58d92b6fcd520f665deb093f3747ffd5e5c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Oct 2021 15:36:32 +0700 Subject: Wire up `container` styling to `iced_native` --- native/src/widget/pane_grid.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'native/src/widget/pane_grid.rs') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 1e63fdeb..afecd681 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -27,7 +27,6 @@ pub use split::Split; pub use state::State; pub use title_bar::TitleBar; -use crate::container; use crate::event::{self, Event}; use crate::layout; use crate::mouse; @@ -318,7 +317,7 @@ pub struct ResizeEvent { impl<'a, Message, Renderer> Widget for PaneGrid<'a, Message, Renderer> where - Renderer: self::Renderer + container::Renderer, + Renderer: self::Renderer, { fn width(&self) -> Length { self.width @@ -565,7 +564,7 @@ where /// able to use a [`PaneGrid`] in your user interface. /// /// [renderer]: crate::renderer -pub trait Renderer: crate::Renderer + container::Renderer + Sized { +pub trait Renderer: crate::Renderer + Sized { /// The style supported by this renderer. type Style: Default; } -- cgit From 41394b4e90a81a43c796c070e706e6aa4d8652bc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 25 Oct 2021 15:37:28 +0700 Subject: Implement `Widget::draw` for `PaneGrid` --- native/src/widget/pane_grid.rs | 217 +++++++++++++++++++++++++++-------------- 1 file changed, 143 insertions(+), 74 deletions(-) (limited to 'native/src/widget/pane_grid.rs') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index afecd681..cb386959 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -34,10 +34,12 @@ use crate::overlay; use crate::renderer; use crate::touch; use crate::{ - Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector, - Widget, + Clipboard, Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, + Vector, Widget, }; +pub use iced_style::pane_grid::{Line, StyleSheet}; + /// A collection of panes distributed using either vertical or horizontal splits /// to completely fill the space available. /// @@ -88,7 +90,7 @@ use crate::{ /// .on_resize(10, Message::PaneResized); /// ``` #[allow(missing_debug_implementations)] -pub struct PaneGrid<'a, Message, Renderer: self::Renderer> { +pub struct PaneGrid<'a, Message, Renderer> { state: &'a mut state::Internal, elements: Vec<(Pane, Content<'a, Message, Renderer>)>, width: Length, @@ -97,12 +99,12 @@ pub struct PaneGrid<'a, Message, Renderer: self::Renderer> { on_click: Option Message + 'a>>, on_drag: Option Message + 'a>>, on_resize: Option<(u16, Box Message + 'a>)>, - style: ::Style, + style_sheet: &'a dyn StyleSheet, } impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> where - Renderer: self::Renderer, + Renderer: crate::Renderer, { /// Creates a [`PaneGrid`] with the given [`State`] and view function. /// @@ -129,7 +131,7 @@ where on_click: None, on_drag: None, on_resize: None, - style: Default::default(), + style_sheet: Default::default(), } } @@ -189,18 +191,15 @@ where } /// Sets the style of the [`PaneGrid`]. - pub fn style( - mut self, - style: impl Into<::Style>, - ) -> Self { - self.style = style.into(); + pub fn style(mut self, style: &'a dyn StyleSheet) -> Self { + self.style_sheet = style; self } } impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> where - Renderer: self::Renderer, + Renderer: crate::Renderer, { fn click_pane( &mut self, @@ -317,7 +316,7 @@ pub struct ResizeEvent { impl<'a, Message, Renderer> Widget for PaneGrid<'a, Message, Renderer> where - Renderer: self::Renderer, + Renderer: crate::Renderer, { fn width(&self) -> Length { self.width @@ -480,55 +479,136 @@ where cursor_position: Point, viewport: &Rectangle, ) { - // let picked_split = self - // .state - // .picked_split() - // .and_then(|(split, axis)| { - // let bounds = layout.bounds(); - - // let splits = self - // .state - // .split_regions(f32::from(self.spacing), bounds.size()); - - // let (_axis, region, ratio) = splits.get(&split)?; - - // let region = axis.split_line_bounds( - // *region, - // *ratio, - // f32::from(self.spacing), - // ); - - // Some((axis, region + Vector::new(bounds.x, bounds.y), true)) - // }) - // .or_else(|| match self.on_resize { - // Some((leeway, _)) => { - // let bounds = layout.bounds(); - - // let relative_cursor = Point::new( - // cursor_position.x - bounds.x, - // cursor_position.y - bounds.y, - // ); - - // let splits = self - // .state - // .split_regions(f32::from(self.spacing), bounds.size()); - - // let (_split, axis, region) = hovered_split( - // splits.iter(), - // f32::from(self.spacing + leeway), - // relative_cursor, - // )?; - - // Some(( - // axis, - // region + Vector::new(bounds.x, bounds.y), - // false, - // )) - // } - // None => None, - // }); - - // TODO + let picked_pane = self.state.picked_pane(); + + let picked_split = self + .state + .picked_split() + .and_then(|(split, axis)| { + let bounds = layout.bounds(); + + let splits = self + .state + .split_regions(f32::from(self.spacing), bounds.size()); + + let (_axis, region, ratio) = splits.get(&split)?; + + let region = axis.split_line_bounds( + *region, + *ratio, + f32::from(self.spacing), + ); + + Some((axis, region + Vector::new(bounds.x, bounds.y), true)) + }) + .or_else(|| match self.on_resize { + Some((leeway, _)) => { + let bounds = layout.bounds(); + + let relative_cursor = Point::new( + cursor_position.x - bounds.x, + cursor_position.y - bounds.y, + ); + + let splits = self + .state + .split_regions(f32::from(self.spacing), bounds.size()); + + let (_split, axis, region) = hovered_split( + splits.iter(), + f32::from(self.spacing + leeway), + relative_cursor, + )?; + + Some(( + axis, + region + Vector::new(bounds.x, bounds.y), + false, + )) + } + None => None, + }); + + let pane_cursor_position = if picked_pane.is_some() { + // TODO: Remove once cursor availability is encoded in the type + // system + Point::new(-1.0, -1.0) + } else { + cursor_position + }; + + for ((id, pane), layout) in self.elements.iter().zip(layout.children()) + { + match picked_pane { + Some((dragging, origin)) if *id == dragging => { + let bounds = layout.bounds(); + + renderer.with_layer( + Rectangle { + x: cursor_position.x - origin.x, + y: cursor_position.y - origin.y, + width: bounds.width + 0.5, + height: bounds.height + 0.5, + }, + Vector::new(0, 0), + |renderer| { + pane.draw( + renderer, + style, + layout, + pane_cursor_position, + viewport, + ); + }, + ); + } + _ => { + pane.draw( + renderer, + style, + layout, + pane_cursor_position, + viewport, + ); + } + } + } + + if let Some((axis, split_region, is_picked)) = picked_split { + let highlight = if is_picked { + self.style_sheet.picked_split() + } else { + self.style_sheet.hovered_split() + }; + + if let Some(highlight) = highlight { + renderer.fill_rectangle(renderer::Quad { + bounds: match axis { + Axis::Horizontal => Rectangle { + x: split_region.x, + y: (split_region.y + + (split_region.height - highlight.width) + / 2.0) + .round(), + width: split_region.width, + height: highlight.width, + }, + Axis::Vertical => Rectangle { + x: (split_region.x + + (split_region.width - highlight.width) / 2.0) + .round(), + y: split_region.y, + width: highlight.width, + height: split_region.height, + }, + }, + background: highlight.color.into(), + border_radius: 0.0, + border_width: 0.0, + border_color: Color::TRANSPARENT, + }); + } + } } fn hash_layout(&self, state: &mut Hasher) { @@ -558,21 +638,10 @@ where } } -/// The renderer of a [`PaneGrid`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`PaneGrid`] in your user interface. -/// -/// [renderer]: crate::renderer -pub trait Renderer: crate::Renderer + Sized { - /// The style supported by this renderer. - type Style: Default; -} - impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where - Renderer: 'a + self::Renderer, + Renderer: 'a + crate::Renderer, Message: 'a, { fn from( -- cgit From 4a11cbd99445338619dfaf1f327dbc25b2983cb7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 25 Oct 2021 16:16:35 +0700 Subject: Implement `Widget::mouse_interaction` for `PaneGrid` ... and fix rendering of drag interaction in `PaneGrid` by introducing an explicit `with_translation` method to `Renderer` and simplifying the `with_layer` and `Clip` primitive. --- native/src/widget/pane_grid.rs | 57 +++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 15 deletions(-) (limited to 'native/src/widget/pane_grid.rs') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index cb386959..5f0e73e4 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -471,6 +471,33 @@ where .fold(event_status, event::Status::merge) } + fn mouse_interaction( + &self, + layout: Layout<'_>, + viewport: &Rectangle, + cursor_position: Point, + ) -> mouse::Interaction { + if self.state.picked_pane().is_some() { + return mouse::Interaction::Grab; + } + + if let Some((_, axis)) = self.state.picked_split() { + return match axis { + Axis::Horizontal => mouse::Interaction::ResizingHorizontally, + Axis::Vertical => mouse::Interaction::ResizingVertically, + }; + } + + self.elements + .iter() + .zip(layout.children()) + .map(|((_pane, content), layout)| { + content.mouse_interaction(layout, viewport, cursor_position) + }) + .max() + .unwrap_or_default() + } + fn draw( &self, renderer: &mut Renderer, @@ -543,22 +570,22 @@ where Some((dragging, origin)) if *id == dragging => { let bounds = layout.bounds(); - renderer.with_layer( - Rectangle { - x: cursor_position.x - origin.x, - y: cursor_position.y - origin.y, - width: bounds.width + 0.5, - height: bounds.height + 0.5, - }, - Vector::new(0, 0), + renderer.with_translation( + cursor_position + - Point::new( + bounds.x + origin.x, + bounds.y + origin.y, + ), |renderer| { - pane.draw( - renderer, - style, - layout, - pane_cursor_position, - viewport, - ); + renderer.with_layer(bounds, |renderer| { + pane.draw( + renderer, + style, + layout, + pane_cursor_position, + viewport, + ); + }); }, ); } -- cgit From 0aafcde0ef1533c9eeba0379de8c0082e30c7504 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 15:35:12 +0700 Subject: Remove `widget` module re-exports in `iced_native` --- native/src/widget/pane_grid.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'native/src/widget/pane_grid.rs') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 5f0e73e4..22c18c05 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -62,10 +62,10 @@ pub use iced_style::pane_grid::{Line, StyleSheet}; /// ## Example /// /// ``` -/// # use iced_native::{pane_grid, Text}; +/// # use iced_native::widget::{pane_grid, Text}; /// # /// # type PaneGrid<'a, Message> = -/// # iced_native::PaneGrid<'a, Message, iced_native::renderer::Null>; +/// # iced_native::widget::PaneGrid<'a, Message, iced_native::renderer::Null>; /// # /// enum PaneState { /// SomePane, -- cgit From 89d50c0243fcb70442c78bbff207d0328b33e158 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 17:04:44 +0700 Subject: Reintroduce `Box` for `style_sheet` in `PaneGrid` --- native/src/widget/pane_grid.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'native/src/widget/pane_grid.rs') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 22c18c05..48243c0b 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -99,7 +99,7 @@ pub struct PaneGrid<'a, Message, Renderer> { on_click: Option Message + 'a>>, on_drag: Option Message + 'a>>, on_resize: Option<(u16, Box Message + 'a>)>, - style_sheet: &'a dyn StyleSheet, + style_sheet: Box, } impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> @@ -191,8 +191,8 @@ where } /// Sets the style of the [`PaneGrid`]. - pub fn style(mut self, style: &'a dyn StyleSheet) -> Self { - self.style_sheet = style; + pub fn style(mut self, style: impl Into>) -> Self { + self.style_sheet = style.into(); self } } -- cgit From 631e95ee0be01dc7f5e5183e1429972aee37787f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 2 Nov 2021 15:03:29 +0700 Subject: Move `viewport` argument to last position in `mouse_interaction` methods This keeps the order of the arguments consistent with `draw`. --- native/src/widget/pane_grid.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'native/src/widget/pane_grid.rs') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 48243c0b..d98369e4 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -474,8 +474,8 @@ where fn mouse_interaction( &self, layout: Layout<'_>, - viewport: &Rectangle, cursor_position: Point, + viewport: &Rectangle, ) -> mouse::Interaction { if self.state.picked_pane().is_some() { return mouse::Interaction::Grab; @@ -492,7 +492,7 @@ where .iter() .zip(layout.children()) .map(|((_pane, content), layout)| { - content.mouse_interaction(layout, viewport, cursor_position) + content.mouse_interaction(layout, cursor_position, viewport) }) .max() .unwrap_or_default() -- cgit From 023aded2772f0cd6abd716fe5c8624d5d22e21fa Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 4 Nov 2021 19:22:29 +0700 Subject: Rename `fill_rectangle` to `fill_quad` in `Renderer` --- native/src/widget/pane_grid.rs | 49 ++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 23 deletions(-) (limited to 'native/src/widget/pane_grid.rs') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index d98369e4..20616ed4 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -609,31 +609,34 @@ where }; if let Some(highlight) = highlight { - renderer.fill_rectangle(renderer::Quad { - bounds: match axis { - Axis::Horizontal => Rectangle { - x: split_region.x, - y: (split_region.y - + (split_region.height - highlight.width) - / 2.0) - .round(), - width: split_region.width, - height: highlight.width, - }, - Axis::Vertical => Rectangle { - x: (split_region.x - + (split_region.width - highlight.width) / 2.0) - .round(), - y: split_region.y, - width: highlight.width, - height: split_region.height, + renderer.fill_quad( + renderer::Quad { + bounds: match axis { + Axis::Horizontal => Rectangle { + x: split_region.x, + y: (split_region.y + + (split_region.height - highlight.width) + / 2.0) + .round(), + width: split_region.width, + height: highlight.width, + }, + Axis::Vertical => Rectangle { + x: (split_region.x + + (split_region.width - highlight.width) + / 2.0) + .round(), + y: split_region.y, + width: highlight.width, + height: split_region.height, + }, }, + border_radius: 0.0, + border_width: 0.0, + border_color: Color::TRANSPARENT, }, - background: highlight.color.into(), - border_radius: 0.0, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }); + highlight.color, + ); } } } -- cgit