diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs index ad2337d8..622d700b 100644 --- a/examples/editor/src/main.rs +++ b/examples/editor/src/main.rs @@ -1,8 +1,8 @@ use iced::highlighter; use iced::keyboard; use iced::widget::{ - self, button, center_x, column, container, horizontal_space, pick_list, - row, text, text_editor, toggler, tooltip, + button, center_x, column, container, horizontal_space, operation, + pick_list, row, text, text_editor, toggler, tooltip, }; use iced::{Center, Element, Fill, Font, Task, Theme}; @@ -59,7 +59,7 @@ impl Editor { )), Message::FileOpened, ), - widget::focus_next(), + operation::focus_next(), ]), ) } diff --git a/examples/markdown/src/main.rs b/examples/markdown/src/main.rs index b7195055..42302b9e 100644 --- a/examples/markdown/src/main.rs +++ b/examples/markdown/src/main.rs @@ -5,8 +5,8 @@ use iced::clipboard; use iced::highlighter; use iced::time::{self, Instant, milliseconds}; use iced::widget::{ - self, button, center_x, container, horizontal_space, hover, image, - markdown, right, row, scrollable, sensor, text_editor, toggler, + button, center_x, container, horizontal_space, hover, image, markdown, + operation, right, row, scrollable, sensor, text_editor, toggler, }; use iced::window; use iced::{ @@ -78,7 +78,7 @@ impl Markdown { theme: Theme::TokyoNight, now: Instant::now(), }, - widget::focus_next(), + operation::focus_next(), ) } @@ -140,10 +140,7 @@ impl Markdown { pending: self.raw.text(), }; - scrollable::snap_to( - "preview", - scrollable::RelativeOffset::END, - ) + operation::snap_to_end("preview") } else { self.mode = Mode::Preview; diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index b4355004..54fcb1af 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -2,8 +2,8 @@ use iced::event::{self, Event}; use iced::keyboard; use iced::keyboard::key; use iced::widget::{ - self, button, center, column, container, horizontal_space, mouse_area, - opaque, pick_list, row, stack, text, text_input, + button, center, column, container, horizontal_space, mouse_area, opaque, + operation, pick_list, row, stack, text, text_input, }; use iced::{Bottom, Color, Element, Fill, Subscription, Task}; @@ -43,7 +43,7 @@ impl App { match message { Message::ShowModal => { self.show_modal = true; - widget::focus_next() + operation::focus_next() } Message::HideModal => { self.hide_modal(); @@ -75,9 +75,9 @@ impl App { .. }) => { if modifiers.shift() { - widget::focus_previous() + operation::focus_previous() } else { - widget::focus_next() + operation::focus_next() } } Event::Keyboard(keyboard::Event::KeyPressed { diff --git a/examples/multi_window/src/main.rs b/examples/multi_window/src/main.rs index d940b9da..f7c57d2a 100644 --- a/examples/multi_window/src/main.rs +++ b/examples/multi_window/src/main.rs @@ -1,6 +1,6 @@ use iced::widget::{ - button, center, center_x, column, container, horizontal_space, scrollable, - text, text_input, + button, center, center_x, column, container, horizontal_space, operation, + scrollable, text, text_input, }; use iced::window; use iced::{ @@ -85,7 +85,7 @@ impl Example { } Message::WindowOpened(id) => { let window = Window::new(self.windows.len() + 1); - let focus_input = text_input::focus(format!("input-{id}")); + let focus_input = operation::focus(format!("input-{id}")); self.windows.insert(id, window); diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 7ebe46c7..e354bac8 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -1,6 +1,6 @@ use iced::widget::{ - button, column, container, horizontal_space, progress_bar, radio, row, - scrollable, slider, text, vertical_space, + button, column, container, horizontal_space, operation, progress_bar, + radio, row, scrollable, slider, text, vertical_space, }; use iced::{Border, Center, Color, Element, Fill, Task, Theme}; @@ -60,13 +60,13 @@ impl ScrollableDemo { self.current_scroll_offset = scrollable::RelativeOffset::START; self.scrollable_direction = direction; - scrollable::snap_to(SCROLLABLE, self.current_scroll_offset) + operation::snap_to(SCROLLABLE, self.current_scroll_offset) } Message::AlignmentChanged(alignment) => { self.current_scroll_offset = scrollable::RelativeOffset::START; self.anchor = alignment; - scrollable::snap_to(SCROLLABLE, self.current_scroll_offset) + operation::snap_to(SCROLLABLE, self.current_scroll_offset) } Message::ScrollbarWidthChanged(width) => { self.scrollbar_width = width; @@ -86,12 +86,12 @@ impl ScrollableDemo { Message::ScrollToBeginning => { self.current_scroll_offset = scrollable::RelativeOffset::START; - scrollable::snap_to(SCROLLABLE, self.current_scroll_offset) + operation::snap_to(SCROLLABLE, self.current_scroll_offset) } Message::ScrollToEnd => { self.current_scroll_offset = scrollable::RelativeOffset::END; - scrollable::snap_to(SCROLLABLE, self.current_scroll_offset) + operation::snap_to(SCROLLABLE, self.current_scroll_offset) } Message::Scrolled(viewport) => { self.current_scroll_offset = viewport.relative_offset(); diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index e825f4f3..4fdd4cf9 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -2,7 +2,7 @@ use iced::event::{self, Event}; use iced::keyboard; use iced::keyboard::key; use iced::widget::{ - self, button, center, column, pick_list, row, slider, text, text_input, + button, center, column, operation, pick_list, row, slider, text, text_input, }; use iced::{Center, Element, Fill, Subscription, Task}; @@ -83,11 +83,11 @@ impl App { key: keyboard::Key::Named(key::Named::Tab), modifiers, .. - })) if modifiers.shift() => widget::focus_previous(), + })) if modifiers.shift() => operation::focus_previous(), Message::Event(Event::Keyboard(keyboard::Event::KeyPressed { key: keyboard::Key::Named(key::Named::Tab), .. - })) => widget::focus_next(), + })) => operation::focus_next(), Message::Event(_) => Task::none(), } } diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 78ed4c5e..3e15e492 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -1,7 +1,7 @@ use iced::keyboard; use iced::widget::{ - self, Text, button, center, center_x, checkbox, column, keyed_column, row, - scrollable, text, text_input, + self, Text, button, center, center_x, checkbox, column, keyed_column, + operation, row, scrollable, text, text_input, }; use iced::window; use iced::{ @@ -91,7 +91,7 @@ impl Todos { _ => {} } - text_input::focus("new-task") + operation::focus("new-task") } Todos::Loaded(state) => { let mut saved = false; @@ -132,8 +132,8 @@ impl Todos { if should_focus { let id = Task::text_input_id(i); Command::batch(vec![ - text_input::focus(id.clone()), - text_input::select_all(id), + operation::focus(id.clone()), + operation::select_all(id), ]) } else { Command::none() @@ -150,9 +150,9 @@ impl Todos { } Message::TabPressed { shift } => { if shift { - widget::focus_previous() + operation::focus_previous() } else { - widget::focus_next() + operation::focus_next() } } Message::ToggleFullscreen(mode) => window::get_latest() diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs index 4376a1a1..a868be70 100644 --- a/examples/websocket/src/main.rs +++ b/examples/websocket/src/main.rs @@ -2,7 +2,7 @@ mod echo; use iced::futures::stream; use iced::widget::{ - self, button, center, column, row, scrollable, text, text_input, + button, center, column, operation, row, scrollable, text, text_input, }; use iced::{Center, Element, Fill, Subscription, Task, color}; @@ -34,7 +34,7 @@ impl WebSocket { new_message: String::new(), state: State::Disconnected, }, - widget::focus_next(), + operation::focus_next(), ) } @@ -73,10 +73,7 @@ impl WebSocket { echo::Event::MessageReceived(message) => { self.messages.push(message); - scrollable::snap_to( - MESSAGE_LOG, - scrollable::RelativeOffset::END, - ) + operation::snap_to_end(MESSAGE_LOG) } }, Message::Server => Task::none(), diff --git a/src/lib.rs b/src/lib.rs index 180db986..aa19c6ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -326,7 +326,7 @@ //! //! Tasks can also be used to interact with the iced runtime. Some modules //! expose functions that create tasks for different purposes—like [changing -//! window settings](window#functions), [focusing a widget](widget::focus_next), or +//! window settings](window#functions), [focusing a widget](widget::operation::focus_next), or //! [querying its visible bounds](widget::container::visible_bounds). //! //! Like futures and streams, tasks expose [a monadic interface](Task::then)—but they can also be diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 60627c50..fbebde3b 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -15,8 +15,6 @@ use crate::pick_list::{self, PickList}; use crate::progress_bar::{self, ProgressBar}; use crate::radio::{self, Radio}; use crate::rule::{self, Rule}; -use crate::runtime::Action; -use crate::runtime::task::{self, Task}; use crate::scrollable::{self, Scrollable}; use crate::slider::{self, Slider}; use crate::text::{self, Text}; @@ -2036,16 +2034,6 @@ where crate::Shader::new(program) } -/// Focuses the previous focusable widget. -pub fn focus_previous() -> Task { - task::effect(Action::widget(operation::focusable::focus_previous())) -} - -/// Focuses the next focusable widget. -pub fn focus_next() -> Task { - task::effect(Action::widget(operation::focusable::focus_next())) -} - /// Creates a new [`MouseArea`]. pub fn mouse_area<'a, Message, Theme, Renderer>( widget: impl Into>, diff --git a/widget/src/lib.rs b/widget/src/lib.rs index fc64d938..ec480889 100644 --- a/widget/src/lib.rs +++ b/widget/src/lib.rs @@ -25,6 +25,7 @@ pub mod container; pub mod float; pub mod grid; pub mod keyed; +pub mod operation; pub mod overlay; pub mod pane_grid; pub mod pick_list; diff --git a/widget/src/operation.rs b/widget/src/operation.rs new file mode 100644 index 00000000..db24e1f2 --- /dev/null +++ b/widget/src/operation.rs @@ -0,0 +1,88 @@ +//! Change internal widget state. +use crate::Id; +use crate::core::widget::operation; +use crate::runtime::task; +use crate::runtime::{Action, Task}; + +pub use crate::core::widget::operation::scrollable::{ + AbsoluteOffset, RelativeOffset, +}; + +/// Snaps the scrollable with the given [`Id`] to the provided [`RelativeOffset`]. +pub fn snap_to(id: impl Into, offset: RelativeOffset) -> Task { + task::effect(Action::widget(operation::scrollable::snap_to( + id.into(), + offset, + ))) +} + +/// Snaps the scrollable with the given [`Id`] to the [`RelativeOffset::END`]. +pub fn snap_to_end(id: impl Into) -> Task { + task::effect(Action::widget(operation::scrollable::snap_to( + id.into(), + RelativeOffset::END, + ))) +} + +/// Scrolls the scrollable with the given [`Id`] to the provided [`AbsoluteOffset`]. +pub fn scroll_to(id: impl Into, offset: AbsoluteOffset) -> Task { + task::effect(Action::widget(operation::scrollable::scroll_to( + id.into(), + offset, + ))) +} + +/// Scrolls the scrollable with the given [`Id`] by the provided [`AbsoluteOffset`]. +pub fn scroll_by(id: impl Into, offset: AbsoluteOffset) -> Task { + task::effect(Action::widget(operation::scrollable::scroll_by( + id.into(), + offset, + ))) +} + +/// Focuses the previous focusable widget. +pub fn focus_previous() -> Task { + task::effect(Action::widget(operation::focusable::focus_previous())) +} + +/// Focuses the next focusable widget. +pub fn focus_next() -> Task { + task::effect(Action::widget(operation::focusable::focus_next())) +} + +/// Returns whether the widget with the given [`Id`] is focused or not. +pub fn is_focused(id: impl Into) -> Task { + task::widget(operation::focusable::is_focused(id.into())) +} + +/// Focuses the widget with the given [`Id`]. +pub fn focus(id: impl Into) -> Task { + task::effect(Action::widget(operation::focusable::focus(id.into()))) +} + +/// Moves the cursor of the widget with the given [`Id`] to the end. +pub fn move_cursor_to_end(id: impl Into) -> Task { + task::effect(Action::widget(operation::text_input::move_cursor_to_end( + id.into(), + ))) +} + +/// Moves the cursor of the widget with the given [`Id`] to the front. +pub fn move_cursor_to_front(id: impl Into) -> Task { + task::effect(Action::widget(operation::text_input::move_cursor_to_front( + id.into(), + ))) +} + +/// Moves the cursor of the widget with the given [`Id`] to the provided position. +pub fn move_cursor_to(id: impl Into, position: usize) -> Task { + task::effect(Action::widget(operation::text_input::move_cursor_to( + id.into(), + position, + ))) +} + +/// Selects all the content of the widget with the given [`Id`]. +pub fn select_all(id: impl Into) -> Task { + task::effect(Action::widget(operation::text_input::select_all(id.into()))) +} diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs index 67242334..07b56f5d 100644 --- a/widget/src/scrollable.rs +++ b/widget/src/scrollable.rs @@ -37,8 +37,6 @@ use crate::core::{ Length, Padding, Pixels, Point, Rectangle, Shell, Size, Theme, Vector, Widget, }; -use crate::runtime::Action; -use crate::runtime::task::{self, Task}; pub use operation::scrollable::{AbsoluteOffset, RelativeOffset}; @@ -1262,42 +1260,6 @@ where } } -/// Produces a [`Task`] that snaps the [`Scrollable`] with the given [`widget::Id`] -/// to the provided [`RelativeOffset`]. -pub fn snap_to( - id: impl Into, - offset: RelativeOffset, -) -> Task { - task::effect(Action::widget(operation::scrollable::snap_to( - id.into(), - offset, - ))) -} - -/// Produces a [`Task`] that scrolls the [`Scrollable`] with the given [`widget::Id`] -/// to the provided [`AbsoluteOffset`]. -pub fn scroll_to( - id: impl Into, - offset: AbsoluteOffset, -) -> Task { - task::effect(Action::widget(operation::scrollable::scroll_to( - id.into(), - offset, - ))) -} - -/// Produces a [`Task`] that scrolls the [`Scrollable`] with the given [`widget::Id`] -/// by the provided [`AbsoluteOffset`]. -pub fn scroll_by( - id: impl Into, - offset: AbsoluteOffset, -) -> Task { - task::effect(Action::widget(operation::scrollable::scroll_by( - id.into(), - offset, - ))) -} - fn notify_scroll( state: &mut State, on_scroll: &Option Message + '_>>, diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 9d39e02c..ea4de989 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -61,8 +61,6 @@ use crate::core::{ Length, Padding, Pixels, Point, Rectangle, Shell, Size, Theme, Vector, Widget, }; -use crate::runtime::Action; -use crate::runtime::task::{self, Task}; /// A field that can be filled with text. /// @@ -1445,49 +1443,6 @@ pub enum Side { Right, } -/// Produces a [`Task`] that returns whether the [`TextInput`] with the given [`widget::Id`] is focused or not. -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 [`widget::Id`]. -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 [`widget::Id`] to the -/// end. -pub fn move_cursor_to_end(id: impl Into) -> Task { - task::effect(Action::widget(operation::text_input::move_cursor_to_end( - id.into(), - ))) -} - -/// Produces a [`Task`] that moves the cursor of the [`TextInput`] with the given [`widget::Id`] to the -/// front. -pub fn move_cursor_to_front(id: impl Into) -> Task { - task::effect(Action::widget(operation::text_input::move_cursor_to_front( - id.into(), - ))) -} - -/// Produces a [`Task`] that moves the cursor of the [`TextInput`] with the given [`widget::Id`] to the -/// provided position. -pub fn move_cursor_to( - id: impl Into, - position: usize, -) -> Task { - task::effect(Action::widget(operation::text_input::move_cursor_to( - id.into(), - position, - ))) -} - -/// Produces a [`Task`] that selects all the content of the [`TextInput`] with the given [`widget::Id`]. -pub fn select_all(id: impl Into) -> Task { - task::effect(Action::widget(operation::text_input::select_all(id.into()))) -} - /// The state of a [`TextInput`]. #[derive(Debug, Default, Clone)] pub struct State {