Merge pull request #1695 from nicksenger/widgets/container-ids

Provide widgets IDs to `Operation::container`
This commit is contained in:
Héctor Ramón 2023-02-16 16:32:07 +01:00 committed by GitHub
commit e3fbaed12f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 17 deletions

View file

@ -5,7 +5,7 @@ use crate::layout;
use crate::mouse; use crate::mouse;
use crate::overlay; use crate::overlay;
use crate::renderer; use crate::renderer;
use crate::widget::{Operation, Tree}; use crate::widget::{self, Operation, Tree};
use crate::{ use crate::{
Background, Clipboard, Color, Element, Layout, Length, Padding, Point, Background, Clipboard, Color, Element, Layout, Length, Padding, Point,
Rectangle, Shell, Widget, Rectangle, Shell, Widget,
@ -24,6 +24,7 @@ where
Renderer: crate::Renderer, Renderer: crate::Renderer,
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
{ {
id: Option<Id>,
padding: Padding, padding: Padding,
width: Length, width: Length,
height: Length, height: Length,
@ -46,6 +47,7 @@ where
T: Into<Element<'a, Message, Renderer>>, T: Into<Element<'a, Message, Renderer>>,
{ {
Container { Container {
id: None,
padding: Padding::ZERO, padding: Padding::ZERO,
width: Length::Shrink, width: Length::Shrink,
height: 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`]. /// Sets the [`Padding`] of the [`Container`].
pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self { pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
self.padding = padding.into(); self.padding = padding.into();
@ -172,14 +180,17 @@ where
renderer: &Renderer, renderer: &Renderer,
operation: &mut dyn Operation<Message>, operation: &mut dyn Operation<Message>,
) { ) {
operation.container(None, &mut |operation| { operation.container(
self.content.as_widget().operate( self.id.as_ref().map(|id| &id.0),
&mut tree.children[0], &mut |operation| {
layout.children().next().unwrap(), self.content.as_widget().operate(
renderer, &mut tree.children[0],
operation, layout.children().next().unwrap(),
); renderer,
}); operation,
);
},
);
} }
fn on_event( 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
}
}

View file

@ -208,14 +208,17 @@ where
operation.scrollable(state, self.id.as_ref().map(|id| &id.0)); operation.scrollable(state, self.id.as_ref().map(|id| &id.0));
operation.container(None, &mut |operation| { operation.container(
self.content.as_widget().operate( self.id.as_ref().map(|id| &id.0),
&mut tree.children[0], &mut |operation| {
layout.children().next().unwrap(), self.content.as_widget().operate(
renderer, &mut tree.children[0],
operation, layout.children().next().unwrap(),
); renderer,
}); operation,
);
},
);
} }
fn on_event( fn on_event(