From fbe60feb7ebb27486ac90c40df4094098f7c74a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 23 Aug 2025 02:04:30 +0200 Subject: [PATCH] Remove `Id` for `container`, `scrollable`, and `text_input` --- core/src/widget/id.rs | 6 +++ examples/scrollable/src/main.rs | 33 ++++-------- examples/todos/src/main.rs | 4 +- examples/visible_bounds/src/main.rs | 16 +++--- examples/websocket/src/main.rs | 9 ++-- selector/src/lib.rs | 2 +- widget/src/container.rs | 38 ++----------- widget/src/scrollable.rs | 57 +++++++------------- widget/src/text_input.rs | 82 +++++++---------------------- 9 files changed, 68 insertions(+), 179 deletions(-) diff --git a/core/src/widget/id.rs b/core/src/widget/id.rs index e03ded9d..2e7331c0 100644 --- a/core/src/widget/id.rs +++ b/core/src/widget/id.rs @@ -29,6 +29,12 @@ impl From<&'static str> for Id { } } +impl From for Id { + fn from(value: String) -> Self { + Self::new(value) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] enum Internal { Unique(usize), diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 087f8e51..7ebe46c7 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -4,11 +4,6 @@ use iced::widget::{ }; use iced::{Border, Center, Color, Element, Fill, Task, Theme}; -use std::sync::LazyLock; - -static SCROLLABLE_ID: LazyLock = - LazyLock::new(scrollable::Id::unique); - pub fn main() -> iced::Result { iced::application( ScrollableDemo::default, @@ -65,19 +60,13 @@ impl ScrollableDemo { self.current_scroll_offset = scrollable::RelativeOffset::START; self.scrollable_direction = direction; - scrollable::snap_to( - SCROLLABLE_ID.clone(), - self.current_scroll_offset, - ) + scrollable::snap_to(SCROLLABLE, self.current_scroll_offset) } Message::AlignmentChanged(alignment) => { self.current_scroll_offset = scrollable::RelativeOffset::START; self.anchor = alignment; - scrollable::snap_to( - SCROLLABLE_ID.clone(), - self.current_scroll_offset, - ) + scrollable::snap_to(SCROLLABLE, self.current_scroll_offset) } Message::ScrollbarWidthChanged(width) => { self.scrollbar_width = width; @@ -97,18 +86,12 @@ impl ScrollableDemo { Message::ScrollToBeginning => { self.current_scroll_offset = scrollable::RelativeOffset::START; - scrollable::snap_to( - SCROLLABLE_ID.clone(), - self.current_scroll_offset, - ) + scrollable::snap_to(SCROLLABLE, self.current_scroll_offset) } Message::ScrollToEnd => { self.current_scroll_offset = scrollable::RelativeOffset::END; - scrollable::snap_to( - SCROLLABLE_ID.clone(), - self.current_scroll_offset, - ) + scrollable::snap_to(SCROLLABLE, self.current_scroll_offset) } Message::Scrolled(viewport) => { self.current_scroll_offset = viewport.relative_offset(); @@ -226,7 +209,7 @@ impl ScrollableDemo { )) .width(Fill) .height(Fill) - .id(SCROLLABLE_ID.clone()) + .id(SCROLLABLE) .on_scroll(Message::Scrolled), Direction::Horizontal => scrollable( row![ @@ -252,7 +235,7 @@ impl ScrollableDemo { )) .width(Fill) .height(Fill) - .id(SCROLLABLE_ID.clone()) + .id(SCROLLABLE) .on_scroll(Message::Scrolled), Direction::Multi => scrollable( //horizontal content @@ -299,7 +282,7 @@ impl ScrollableDemo { }) .width(Fill) .height(Fill) - .id(SCROLLABLE_ID.clone()) + .id(SCROLLABLE) .on_scroll(Message::Scrolled), }); @@ -348,3 +331,5 @@ fn progress_bar_custom_style(theme: &Theme) -> progress_bar::Style { border: Border::default(), } } + +const SCROLLABLE: &str = "scrollable"; diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 5dab262c..78ed4c5e 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -305,8 +305,8 @@ pub enum TaskMessage { } impl Task { - fn text_input_id(i: usize) -> text_input::Id { - text_input::Id::new(format!("task-{i}")) + fn text_input_id(i: usize) -> widget::Id { + widget::Id::new(format!("task-{i}")) } fn new(description: String) -> Self { diff --git a/examples/visible_bounds/src/main.rs b/examples/visible_bounds/src/main.rs index 8e5e4a07..0029de55 100644 --- a/examples/visible_bounds/src/main.rs +++ b/examples/visible_bounds/src/main.rs @@ -41,9 +41,9 @@ impl Example { Task::none() } Message::Scrolled | Message::WindowResized => Task::batch(vec![ - container::visible_bounds(OUTER_CONTAINER.clone()) + container::visible_bounds(OUTER_CONTAINER) .map(Message::OuterBoundsFetched), - container::visible_bounds(INNER_CONTAINER.clone()) + container::visible_bounds(INNER_CONTAINER) .map(Message::InnerBoundsFetched), ]), Message::OuterBoundsFetched(outer_bounds) => { @@ -113,7 +113,7 @@ impl Example { text("Scroll me!"), vertical_space().height(400), container(text("I am the outer container!")) - .id(OUTER_CONTAINER.clone()) + .id(OUTER_CONTAINER) .padding(40) .style(container::rounded_box), vertical_space().height(400), @@ -122,7 +122,7 @@ impl Example { text("Scroll me!"), vertical_space().height(400), container(text("I am the inner container!")) - .id(INNER_CONTAINER.clone()) + .id(INNER_CONTAINER) .padding(40) .style(container::rounded_box), vertical_space().height(400), @@ -157,9 +157,5 @@ impl Example { } } -use std::sync::LazyLock; - -static OUTER_CONTAINER: LazyLock = - LazyLock::new(|| container::Id::new("outer")); -static INNER_CONTAINER: LazyLock = - LazyLock::new(|| container::Id::new("inner")); +const OUTER_CONTAINER: &str = "outer"; +const INNER_CONTAINER: &str = "inner"; diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index 055455de..4376a1a1 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -6,8 +6,6 @@ use iced::widget::{ }; use iced::{Center, Element, Fill, Subscription, Task, color}; -use std::sync::LazyLock; - pub fn main() -> iced::Result { iced::application(WebSocket::new, WebSocket::update, WebSocket::view) .subscription(WebSocket::subscription) @@ -76,7 +74,7 @@ impl WebSocket { self.messages.push(message); scrollable::snap_to( - MESSAGE_LOG.clone(), + MESSAGE_LOG, scrollable::RelativeOffset::END, ) } @@ -105,7 +103,7 @@ impl WebSocket { column(self.messages.iter().map(text).map(Element::from)) .spacing(10), ) - .id(MESSAGE_LOG.clone()) + .id(MESSAGE_LOG) .height(Fill) .spacing(10) .into() @@ -142,5 +140,4 @@ enum State { Connected(echo::Connection), } -static MESSAGE_LOG: LazyLock = - LazyLock::new(scrollable::Id::unique); +const MESSAGE_LOG: &str = "message_log"; diff --git a/selector/src/lib.rs b/selector/src/lib.rs index 2163c502..e9a36076 100644 --- a/selector/src/lib.rs +++ b/selector/src/lib.rs @@ -78,7 +78,7 @@ impl Selector for Id { } fn description(&self) -> String { - format!("id == \"{:?}\"", self) + format!("id == {:?}", self) } } diff --git a/widget/src/container.rs b/widget/src/container.rs index 7b73831e..f3b1c8d1 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -67,7 +67,7 @@ pub struct Container< Theme: Catalog, Renderer: core::Renderer, { - id: Option, + id: Option, padding: Padding, width: Length, height: Length, @@ -108,7 +108,7 @@ where } /// Sets the [`Id`] of the [`Container`]. - pub fn id(mut self, id: impl Into) -> Self { + pub fn id(mut self, id: impl Into) -> Self { self.id = Some(id.into()); self } @@ -284,7 +284,7 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container(self.id.as_ref().map(|id| &id.0), layout.bounds()); + operation.container(self.id.as_ref(), layout.bounds()); operation.traverse(&mut |operation| { self.content.as_widget().operate( tree, @@ -457,39 +457,9 @@ 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 - } -} - -impl From<&'static str> for Id { - fn from(value: &'static str) -> Self { - Id::new(value) - } -} - /// Produces a [`Task`] that queries the visible screen bounds of the /// [`Container`] with the given [`Id`]. -pub fn visible_bounds(_id: impl Into) -> Task> { +pub fn visible_bounds(_id: impl Into) -> Task> { todo!() } diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 0b682a78..e5c0a728 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -74,7 +74,7 @@ pub struct Scrollable< Theme: Catalog, Renderer: core::Renderer, { - id: Option, + id: Option, width: Length, height: Length, direction: Direction, @@ -150,7 +150,7 @@ where } /// Sets the [`Id`] of the [`Scrollable`]. - pub fn id(mut self, id: impl Into) -> Self { + pub fn id(mut self, id: impl Into) -> Self { self.id = Some(id.into()); self } @@ -542,7 +542,7 @@ where state.translation(self.direction, bounds, content_bounds); operation.scrollable( - self.id.as_ref().map(|id| &id.0), + self.id.as_ref(), bounds, content_bounds, translation, @@ -1262,59 +1262,38 @@ where } } -/// The identifier of a [`Scrollable`]. -#[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 - } -} - -impl From<&'static str> for Id { - fn from(id: &'static str) -> Self { - Self::new(id) - } -} - /// Produces a [`Task`] that snaps the [`Scrollable`] with the given [`Id`] /// to the provided [`RelativeOffset`]. -pub fn snap_to(id: impl Into, offset: RelativeOffset) -> Task { +pub fn snap_to( + id: impl Into, + offset: RelativeOffset, +) -> Task { task::effect(Action::widget(operation::scrollable::snap_to( - id.into().0, + id.into(), offset, ))) } /// Produces a [`Task`] that scrolls the [`Scrollable`] with the given [`Id`] /// to the provided [`AbsoluteOffset`]. -pub fn scroll_to(id: impl Into, offset: AbsoluteOffset) -> Task { +pub fn scroll_to( + id: impl Into, + offset: AbsoluteOffset, +) -> Task { task::effect(Action::widget(operation::scrollable::scroll_to( - id.into().0, + id.into(), offset, ))) } /// Produces a [`Task`] that scrolls the [`Scrollable`] with the given [`Id`] /// by the provided [`AbsoluteOffset`]. -pub fn scroll_by(id: impl Into, offset: AbsoluteOffset) -> Task { +pub fn scroll_by( + id: impl Into, + offset: AbsoluteOffset, +) -> Task { task::effect(Action::widget(operation::scrollable::scroll_by( - id.into().0, + id.into(), offset, ))) } diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index c7ba113c..a40cd149 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -106,7 +106,7 @@ pub struct TextInput< Theme: Catalog, Renderer: text::Renderer, { - id: Option, + id: Option, placeholder: String, value: Value, is_secure: bool, @@ -157,7 +157,7 @@ where } /// Sets the [`Id`] of the [`TextInput`]. - pub fn id(mut self, id: impl Into) -> Self { + pub fn id(mut self, id: impl Into) -> Self { self.id = Some(id.into()); self } @@ -688,17 +688,8 @@ where ) { let state = tree.state.downcast_mut::>(); - operation.focusable( - self.id.as_ref().map(|id| &id.0), - layout.bounds(), - state, - ); - - operation.text_input( - self.id.as_ref().map(|id| &id.0), - layout.bounds(), - state, - ); + operation.text_input(self.id.as_ref(), layout.bounds(), state); + operation.focusable(self.id.as_ref(), layout.bounds(), state); } fn update( @@ -1454,82 +1445,47 @@ pub enum Side { Right, } -/// The identifier of a [`TextInput`]. -#[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 - } -} - -impl From<&'static str> for Id { - fn from(id: &'static str) -> Self { - Self::new(id) - } -} - -impl From for Id { - fn from(id: String) -> Self { - Self::new(id) - } -} - /// Produces a [`Task`] that returns whether the [`TextInput`] with the given [`Id`] is focused or not. -pub fn is_focused(id: impl Into) -> Task { - task::widget(operation::focusable::is_focused(id.into().into())) +pub fn is_focused(id: impl Into) -> Task { + task::widget(operation::focusable::is_focused(id.into())) } /// Produces a [`Task`] that focuses the [`TextInput`] with the given [`Id`]. -pub fn focus(id: impl Into) -> Task { - task::effect(Action::widget(operation::focusable::focus(id.into().0))) +pub fn focus(id: impl Into) -> Task { + task::effect(Action::widget(operation::focusable::focus(id.into()))) } /// Produces a [`Task`] that moves the cursor of the [`TextInput`] with the given [`Id`] to the /// end. -pub fn move_cursor_to_end(id: impl Into) -> Task { +pub fn move_cursor_to_end(id: impl Into) -> Task { task::effect(Action::widget(operation::text_input::move_cursor_to_end( - id.into().0, + id.into(), ))) } /// Produces a [`Task`] that moves the cursor of the [`TextInput`] with the given [`Id`] to the /// front. -pub fn move_cursor_to_front(id: impl Into) -> Task { +pub fn move_cursor_to_front(id: impl Into) -> Task { task::effect(Action::widget(operation::text_input::move_cursor_to_front( - id.into().0, + id.into(), ))) } /// Produces a [`Task`] that moves the cursor of the [`TextInput`] with the given [`Id`] to the /// provided position. -pub fn move_cursor_to(id: impl Into, position: usize) -> Task { +pub fn move_cursor_to( + id: impl Into, + position: usize, +) -> Task { task::effect(Action::widget(operation::text_input::move_cursor_to( - id.into().0, + id.into(), position, ))) } /// Produces a [`Task`] that selects all the content of the [`TextInput`] with the given [`Id`]. -pub fn select_all(id: impl Into) -> Task { - task::effect(Action::widget(operation::text_input::select_all( - id.into().0, - ))) +pub fn select_all(id: impl Into) -> Task { + task::effect(Action::widget(operation::text_input::select_all(id.into()))) } /// The state of a [`TextInput`].