diff options
author | 2023-02-16 16:32:07 +0100 | |
---|---|---|
committer | 2023-02-16 16:32:07 +0100 | |
commit | e3fbaed12fcb7b2aaa33c42e60cf675d707bc197 (patch) | |
tree | dac901e758f1a2c433135b9d63ccc4c7d60934c0 /native | |
parent | 0cb72f69716adc82ad85a0ee7120edb6e653b0c0 (diff) | |
parent | 273c9be00f80ba97b0f1330d035a2f9e073464a2 (diff) | |
download | iced-e3fbaed12fcb7b2aaa33c42e60cf675d707bc197.tar.gz iced-e3fbaed12fcb7b2aaa33c42e60cf675d707bc197.tar.bz2 iced-e3fbaed12fcb7b2aaa33c42e60cf675d707bc197.zip |
Merge pull request #1695 from nicksenger/widgets/container-ids
Provide widgets IDs to `Operation::container`
Diffstat (limited to 'native')
-rw-r--r-- | native/src/widget/container.rs | 53 | ||||
-rw-r--r-- | native/src/widget/scrollable.rs | 19 |
2 files changed, 55 insertions, 17 deletions
diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index cdf1c859..c82b8be2 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<Id>, padding: Padding, width: Length, height: Length, @@ -46,6 +47,7 @@ where T: Into<Element<'a, Message, Renderer>>, { 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<P: Into<Padding>>(mut self, padding: P) -> Self { self.padding = padding.into(); @@ -172,14 +180,17 @@ where renderer: &Renderer, operation: &mut dyn Operation<Message>, ) { - 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<Renderer>( ); } } + +/// 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<std::borrow::Cow<'static, str>>) -> 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<Id> for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 82286036..2de722e4 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( |