From 6a683b603d555c9d7f589f99738d4151192b91ef Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Thu, 9 Feb 2023 20:57:26 -0800 Subject: [PATCH 1/4] scrollable: provide ID to operation.container --- native/src/widget/scrollable.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 822860364d..2de722e473 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -208,14 +208,17 @@ where operation.scrollable(state, self.id.as_ref().map(|id| &id.0)); - operation.container(None, &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }, + ); } fn on_event( From 84a6038961a5ebd1366ae8c6ba9e44be07d37f16 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Thu, 9 Feb 2023 21:16:12 -0800 Subject: [PATCH 2/4] provide ID to operation.container in applicable widgets --- native/src/widget/button.rs | 53 ++++++++++++++++++++++++------ native/src/widget/column.rs | 59 +++++++++++++++++++++++++++------- native/src/widget/container.rs | 53 ++++++++++++++++++++++++------ native/src/widget/pane_grid.rs | 53 ++++++++++++++++++++++++------ native/src/widget/row.rs | 59 +++++++++++++++++++++++++++------- 5 files changed, 226 insertions(+), 51 deletions(-) diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index b4276317f6..d1537b1006 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -8,7 +8,7 @@ use crate::overlay; use crate::renderer; use crate::touch; use crate::widget::tree::{self, Tree}; -use crate::widget::Operation; +use crate::widget::{self, Operation}; use crate::{ Background, Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle, Shell, Vector, Widget, @@ -56,6 +56,7 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet, { + id: Option, content: Element<'a, Message, Renderer>, on_press: Option, width: Length, @@ -72,6 +73,7 @@ where /// Creates a new [`Button`] with the given content. pub fn new(content: impl Into>) -> Self { Button { + id: None, content: content.into(), on_press: None, width: Length::Shrink, @@ -81,6 +83,12 @@ where } } + /// Sets the [`Id`] of the [`Button`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the width of the [`Button`]. pub fn width(mut self, width: Length) -> Self { self.width = width; @@ -172,14 +180,17 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container(None, &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }, + ); } fn on_event( @@ -453,3 +464,27 @@ pub fn mouse_interaction( mouse::Interaction::default() } } + +/// The identifier of a [`Button`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs index 5ad4d85892..5cecd2b823 100644 --- a/native/src/widget/column.rs +++ b/native/src/widget/column.rs @@ -4,7 +4,7 @@ use crate::layout; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{Operation, Tree}; +use crate::widget::{self, Operation, Tree}; use crate::{ Alignment, Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget, @@ -13,6 +13,7 @@ use crate::{ /// A container that distributes its contents vertically. #[allow(missing_debug_implementations)] pub struct Column<'a, Message, Renderer> { + id: Option, spacing: u16, padding: Padding, width: Length, @@ -33,6 +34,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { children: Vec>, ) -> Self { Column { + id: None, spacing: 0, padding: Padding::ZERO, width: Length::Shrink, @@ -43,6 +45,12 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { } } + /// Sets the [`Id`] of the [`Column`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the vertical spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -148,17 +156,20 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container(None, &mut |operation| { - self.children - .iter() - .zip(&mut tree.children) - .zip(layout.children()) - .for_each(|((child, state), layout)| { - child - .as_widget() - .operate(state, layout, renderer, operation); - }) - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.children + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .for_each(|((child, state), layout)| { + child + .as_widget() + .operate(state, layout, renderer, operation); + }) + }, + ); } fn on_event( @@ -262,3 +273,27 @@ where Self::new(column) } } + +/// The identifier of a [`Column`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index cdf1c85925..c82b8be20c 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -5,7 +5,7 @@ use crate::layout; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{Operation, Tree}; +use crate::widget::{self, Operation, Tree}; use crate::{ Background, Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget, @@ -24,6 +24,7 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet, { + id: Option, padding: Padding, width: Length, height: Length, @@ -46,6 +47,7 @@ where T: Into>, { Container { + id: None, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, @@ -58,6 +60,12 @@ where } } + /// Sets the [`Id`] of the [`Container`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the [`Padding`] of the [`Container`]. pub fn padding>(mut self, padding: P) -> Self { self.padding = padding.into(); @@ -172,14 +180,17 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container(None, &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }, + ); } fn on_event( @@ -333,3 +344,27 @@ pub fn draw_background( ); } } + +/// The identifier of a [`Container`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index eb04c0bac1..6a65754e29 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -101,6 +101,7 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet + container::StyleSheet, { + id: Option, contents: Contents<'a, Content<'a, Message, Renderer>>, width: Length, height: Length, @@ -147,6 +148,7 @@ where }; Self { + id: None, contents, width: Length::Fill, height: Length::Fill, @@ -158,6 +160,12 @@ where } } + /// Sets the [`Id`] of the [`PaneGrid`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the width of the [`PaneGrid`]. pub fn width(mut self, width: Length) -> Self { self.width = width; @@ -297,15 +305,18 @@ where renderer: &Renderer, operation: &mut dyn widget::Operation, ) { - operation.container(None, &mut |operation| { - self.contents - .iter() - .zip(&mut tree.children) - .zip(layout.children()) - .for_each(|(((_pane, content), state), layout)| { - content.operate(state, layout, renderer, operation); - }) - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.contents + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .for_each(|(((_pane, content), state), layout)| { + content.operate(state, layout, renderer, operation); + }) + }, + ); } fn on_event( @@ -996,3 +1007,27 @@ impl<'a, T> Contents<'a, T> { matches!(self, Self::Maximized(..)) } } + +/// The identifier of a [`PaneGrid`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs index 108e98e4a5..1ea670b0bc 100644 --- a/native/src/widget/row.rs +++ b/native/src/widget/row.rs @@ -4,7 +4,7 @@ use crate::layout::{self, Layout}; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{Operation, Tree}; +use crate::widget::{self, Operation, Tree}; use crate::{ Alignment, Clipboard, Element, Length, Padding, Point, Rectangle, Shell, Widget, @@ -13,6 +13,7 @@ use crate::{ /// A container that distributes its contents horizontally. #[allow(missing_debug_implementations)] pub struct Row<'a, Message, Renderer> { + id: Option, spacing: u16, padding: Padding, width: Length, @@ -32,6 +33,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { children: Vec>, ) -> Self { Row { + id: None, spacing: 0, padding: Padding::ZERO, width: Length::Shrink, @@ -41,6 +43,12 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { } } + /// Sets the [`Id`] of the [`Row`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the horizontal spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -137,17 +145,20 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container(None, &mut |operation| { - self.children - .iter() - .zip(&mut tree.children) - .zip(layout.children()) - .for_each(|((child, state), layout)| { - child - .as_widget() - .operate(state, layout, renderer, operation); - }) - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.children + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .for_each(|((child, state), layout)| { + child + .as_widget() + .operate(state, layout, renderer, operation); + }) + }, + ); } fn on_event( @@ -251,3 +262,27 @@ where Self::new(row) } } + +/// The identifier of a [`Row`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} From d05ac38159dc2828b6e5b0d416802d8ef4be80d2 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Fri, 10 Feb 2023 14:46:03 -0800 Subject: [PATCH 3/4] Revert "provide ID to operation.container in applicable widgets" This reverts commit 8f9550bcf7c1cebbf90e80683761375406ca6139. --- native/src/widget/button.rs | 53 ++++++------------------------ native/src/widget/column.rs | 59 +++++++--------------------------- native/src/widget/container.rs | 53 ++++++------------------------ native/src/widget/pane_grid.rs | 53 ++++++------------------------ native/src/widget/row.rs | 59 +++++++--------------------------- 5 files changed, 51 insertions(+), 226 deletions(-) diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index d1537b1006..b4276317f6 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -8,7 +8,7 @@ use crate::overlay; use crate::renderer; use crate::touch; use crate::widget::tree::{self, Tree}; -use crate::widget::{self, Operation}; +use crate::widget::Operation; use crate::{ Background, Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle, Shell, Vector, Widget, @@ -56,7 +56,6 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet, { - id: Option, content: Element<'a, Message, Renderer>, on_press: Option, width: Length, @@ -73,7 +72,6 @@ where /// Creates a new [`Button`] with the given content. pub fn new(content: impl Into>) -> Self { Button { - id: None, content: content.into(), on_press: None, width: Length::Shrink, @@ -83,12 +81,6 @@ where } } - /// Sets the [`Id`] of the [`Button`]. - pub fn id(mut self, id: Id) -> Self { - self.id = Some(id); - self - } - /// Sets the width of the [`Button`]. pub fn width(mut self, width: Length) -> Self { self.width = width; @@ -180,17 +172,14 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container( - self.id.as_ref().map(|id| &id.0), - &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }, - ); + operation.container(None, &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }); } fn on_event( @@ -464,27 +453,3 @@ pub fn mouse_interaction( mouse::Interaction::default() } } - -/// The identifier of a [`Button`]. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Id(widget::Id); - -impl Id { - /// Creates a custom [`Id`]. - pub fn new(id: impl Into>) -> Self { - Self(widget::Id::new(id)) - } - - /// Creates a unique [`Id`]. - /// - /// This function produces a different [`Id`] every time it is called. - pub fn unique() -> Self { - Self(widget::Id::unique()) - } -} - -impl From for widget::Id { - fn from(id: Id) -> Self { - id.0 - } -} diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs index 5cecd2b823..5ad4d85892 100644 --- a/native/src/widget/column.rs +++ b/native/src/widget/column.rs @@ -4,7 +4,7 @@ use crate::layout; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{self, Operation, Tree}; +use crate::widget::{Operation, Tree}; use crate::{ Alignment, Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget, @@ -13,7 +13,6 @@ use crate::{ /// A container that distributes its contents vertically. #[allow(missing_debug_implementations)] pub struct Column<'a, Message, Renderer> { - id: Option, spacing: u16, padding: Padding, width: Length, @@ -34,7 +33,6 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { children: Vec>, ) -> Self { Column { - id: None, spacing: 0, padding: Padding::ZERO, width: Length::Shrink, @@ -45,12 +43,6 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { } } - /// Sets the [`Id`] of the [`Column`]. - pub fn id(mut self, id: Id) -> Self { - self.id = Some(id); - self - } - /// Sets the vertical spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -156,20 +148,17 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container( - self.id.as_ref().map(|id| &id.0), - &mut |operation| { - self.children - .iter() - .zip(&mut tree.children) - .zip(layout.children()) - .for_each(|((child, state), layout)| { - child - .as_widget() - .operate(state, layout, renderer, operation); - }) - }, - ); + operation.container(None, &mut |operation| { + self.children + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .for_each(|((child, state), layout)| { + child + .as_widget() + .operate(state, layout, renderer, operation); + }) + }); } fn on_event( @@ -273,27 +262,3 @@ where Self::new(column) } } - -/// The identifier of a [`Column`]. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Id(widget::Id); - -impl Id { - /// Creates a custom [`Id`]. - pub fn new(id: impl Into>) -> Self { - Self(widget::Id::new(id)) - } - - /// Creates a unique [`Id`]. - /// - /// This function produces a different [`Id`] every time it is called. - pub fn unique() -> Self { - Self(widget::Id::unique()) - } -} - -impl From for widget::Id { - fn from(id: Id) -> Self { - id.0 - } -} diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index c82b8be20c..cdf1c85925 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -5,7 +5,7 @@ use crate::layout; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{self, Operation, Tree}; +use crate::widget::{Operation, Tree}; use crate::{ Background, Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget, @@ -24,7 +24,6 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet, { - id: Option, padding: Padding, width: Length, height: Length, @@ -47,7 +46,6 @@ where T: Into>, { Container { - id: None, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, @@ -60,12 +58,6 @@ where } } - /// Sets the [`Id`] of the [`Container`]. - pub fn id(mut self, id: Id) -> Self { - self.id = Some(id); - self - } - /// Sets the [`Padding`] of the [`Container`]. pub fn padding>(mut self, padding: P) -> Self { self.padding = padding.into(); @@ -180,17 +172,14 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container( - self.id.as_ref().map(|id| &id.0), - &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }, - ); + operation.container(None, &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }); } fn on_event( @@ -344,27 +333,3 @@ pub fn draw_background( ); } } - -/// The identifier of a [`Container`]. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Id(widget::Id); - -impl Id { - /// Creates a custom [`Id`]. - pub fn new(id: impl Into>) -> Self { - Self(widget::Id::new(id)) - } - - /// Creates a unique [`Id`]. - /// - /// This function produces a different [`Id`] every time it is called. - pub fn unique() -> Self { - Self(widget::Id::unique()) - } -} - -impl From for widget::Id { - fn from(id: Id) -> Self { - id.0 - } -} diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 6a65754e29..eb04c0bac1 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -101,7 +101,6 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet + container::StyleSheet, { - id: Option, contents: Contents<'a, Content<'a, Message, Renderer>>, width: Length, height: Length, @@ -148,7 +147,6 @@ where }; Self { - id: None, contents, width: Length::Fill, height: Length::Fill, @@ -160,12 +158,6 @@ where } } - /// Sets the [`Id`] of the [`PaneGrid`]. - pub fn id(mut self, id: Id) -> Self { - self.id = Some(id); - self - } - /// Sets the width of the [`PaneGrid`]. pub fn width(mut self, width: Length) -> Self { self.width = width; @@ -305,18 +297,15 @@ where renderer: &Renderer, operation: &mut dyn widget::Operation, ) { - operation.container( - self.id.as_ref().map(|id| &id.0), - &mut |operation| { - self.contents - .iter() - .zip(&mut tree.children) - .zip(layout.children()) - .for_each(|(((_pane, content), state), layout)| { - content.operate(state, layout, renderer, operation); - }) - }, - ); + operation.container(None, &mut |operation| { + self.contents + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .for_each(|(((_pane, content), state), layout)| { + content.operate(state, layout, renderer, operation); + }) + }); } fn on_event( @@ -1007,27 +996,3 @@ impl<'a, T> Contents<'a, T> { matches!(self, Self::Maximized(..)) } } - -/// The identifier of a [`PaneGrid`]. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Id(widget::Id); - -impl Id { - /// Creates a custom [`Id`]. - pub fn new(id: impl Into>) -> Self { - Self(widget::Id::new(id)) - } - - /// Creates a unique [`Id`]. - /// - /// This function produces a different [`Id`] every time it is called. - pub fn unique() -> Self { - Self(widget::Id::unique()) - } -} - -impl From for widget::Id { - fn from(id: Id) -> Self { - id.0 - } -} diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs index 1ea670b0bc..108e98e4a5 100644 --- a/native/src/widget/row.rs +++ b/native/src/widget/row.rs @@ -4,7 +4,7 @@ use crate::layout::{self, Layout}; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{self, Operation, Tree}; +use crate::widget::{Operation, Tree}; use crate::{ Alignment, Clipboard, Element, Length, Padding, Point, Rectangle, Shell, Widget, @@ -13,7 +13,6 @@ use crate::{ /// A container that distributes its contents horizontally. #[allow(missing_debug_implementations)] pub struct Row<'a, Message, Renderer> { - id: Option, spacing: u16, padding: Padding, width: Length, @@ -33,7 +32,6 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { children: Vec>, ) -> Self { Row { - id: None, spacing: 0, padding: Padding::ZERO, width: Length::Shrink, @@ -43,12 +41,6 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { } } - /// Sets the [`Id`] of the [`Row`]. - pub fn id(mut self, id: Id) -> Self { - self.id = Some(id); - self - } - /// Sets the horizontal spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -145,20 +137,17 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container( - self.id.as_ref().map(|id| &id.0), - &mut |operation| { - self.children - .iter() - .zip(&mut tree.children) - .zip(layout.children()) - .for_each(|((child, state), layout)| { - child - .as_widget() - .operate(state, layout, renderer, operation); - }) - }, - ); + operation.container(None, &mut |operation| { + self.children + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .for_each(|((child, state), layout)| { + child + .as_widget() + .operate(state, layout, renderer, operation); + }) + }); } fn on_event( @@ -262,27 +251,3 @@ where Self::new(row) } } - -/// The identifier of a [`Row`]. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Id(widget::Id); - -impl Id { - /// Creates a custom [`Id`]. - pub fn new(id: impl Into>) -> Self { - Self(widget::Id::new(id)) - } - - /// Creates a unique [`Id`]. - /// - /// This function produces a different [`Id`] every time it is called. - pub fn unique() -> Self { - Self(widget::Id::unique()) - } -} - -impl From for widget::Id { - fn from(id: Id) -> Self { - id.0 - } -} From 273c9be00f80ba97b0f1330d035a2f9e073464a2 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Fri, 10 Feb 2023 14:51:59 -0800 Subject: [PATCH 4/4] container: allow specification of ID and provide to `Operation::container` --- native/src/widget/container.rs | 53 ++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index cdf1c85925..c82b8be20c 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -5,7 +5,7 @@ use crate::layout; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{Operation, Tree}; +use crate::widget::{self, Operation, Tree}; use crate::{ Background, Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget, @@ -24,6 +24,7 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet, { + id: Option, padding: Padding, width: Length, height: Length, @@ -46,6 +47,7 @@ where T: Into>, { Container { + id: None, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, @@ -58,6 +60,12 @@ where } } + /// Sets the [`Id`] of the [`Container`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the [`Padding`] of the [`Container`]. pub fn padding>(mut self, padding: P) -> Self { self.padding = padding.into(); @@ -172,14 +180,17 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container(None, &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }, + ); } fn on_event( @@ -333,3 +344,27 @@ pub fn draw_background( ); } } + +/// The identifier of a [`Container`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +}