Skip to main content

iced_runtime/widget/
operation.rs

1//! Change internal widget state.
2use crate::core::widget::Id;
3use crate::core::widget::operation;
4use crate::task;
5use crate::{Action, Task};
6
7pub use crate::core::widget::operation::scrollable::{AbsoluteOffset, RelativeOffset};
8
9/// Snaps the scrollable with the given [`Id`] to the provided [`RelativeOffset`].
10pub fn snap_to<T>(id: impl Into<Id>, offset: impl Into<RelativeOffset<Option<f32>>>) -> Task<T> {
11    task::effect(Action::widget(operation::scrollable::snap_to(
12        id.into(),
13        offset.into(),
14    )))
15}
16
17/// Snaps the scrollable with the given [`Id`] to the [`RelativeOffset::END`].
18pub fn snap_to_end<T>(id: impl Into<Id>) -> Task<T> {
19    task::effect(Action::widget(operation::scrollable::snap_to(
20        id.into(),
21        RelativeOffset::END.into(),
22    )))
23}
24
25/// Scrolls the scrollable with the given [`Id`] to the provided [`AbsoluteOffset`].
26pub fn scroll_to<T>(id: impl Into<Id>, offset: impl Into<AbsoluteOffset<Option<f32>>>) -> Task<T> {
27    task::effect(Action::widget(operation::scrollable::scroll_to(
28        id.into(),
29        offset.into(),
30    )))
31}
32
33/// Scrolls the scrollable with the given [`Id`] by the provided [`AbsoluteOffset`].
34pub fn scroll_by<T>(id: impl Into<Id>, offset: AbsoluteOffset) -> Task<T> {
35    task::effect(Action::widget(operation::scrollable::scroll_by(
36        id.into(),
37        offset,
38    )))
39}
40
41/// Focuses the previous focusable widget.
42pub fn focus_previous<T>() -> Task<T> {
43    task::effect(Action::widget(operation::focusable::focus_previous()))
44}
45
46/// Focuses the next focusable widget.
47pub fn focus_next<T>() -> Task<T> {
48    task::effect(Action::widget(operation::focusable::focus_next()))
49}
50
51/// Returns whether the widget with the given [`Id`] is focused or not.
52pub fn is_focused(id: impl Into<Id>) -> Task<bool> {
53    task::widget(operation::focusable::is_focused(id.into()))
54}
55
56/// Focuses the widget with the given [`Id`].
57pub fn focus<T>(id: impl Into<Id>) -> Task<T> {
58    task::effect(Action::widget(operation::focusable::focus(id.into())))
59}
60
61/// Focuses the next focusable widget within a specific container.
62///
63/// Only cycles focus among descendants of the container with the
64/// given [`Id`]. Used for modal dialog focus trapping.
65pub fn focus_next_within<T>(target: impl Into<Id>) -> Task<T> {
66    task::effect(Action::widget(operation::focusable::focus_next_within(
67        target.into(),
68    )))
69}
70
71/// Focuses the previous focusable widget within a specific container.
72///
73/// Only cycles focus among descendants of the container with the
74/// given [`Id`]. Used for modal dialog focus trapping.
75pub fn focus_previous_within<T>(target: impl Into<Id>) -> Task<T> {
76    task::effect(Action::widget(operation::focusable::focus_previous_within(
77        target.into(),
78    )))
79}
80
81/// Moves the cursor of the widget with the given [`Id`] to the end.
82pub fn move_cursor_to_end<T>(id: impl Into<Id>) -> Task<T> {
83    task::effect(Action::widget(operation::text_input::move_cursor_to_end(
84        id.into(),
85    )))
86}
87
88/// Moves the cursor of the widget with the given [`Id`] to the front.
89pub fn move_cursor_to_front<T>(id: impl Into<Id>) -> Task<T> {
90    task::effect(Action::widget(operation::text_input::move_cursor_to_front(
91        id.into(),
92    )))
93}
94
95/// Moves the cursor of the widget with the given [`Id`] to the provided position.
96pub fn move_cursor_to<T>(id: impl Into<Id>, position: usize) -> Task<T> {
97    task::effect(Action::widget(operation::text_input::move_cursor_to(
98        id.into(),
99        position,
100    )))
101}
102
103/// Selects all the content of the widget with the given [`Id`].
104pub fn select_all<T>(id: impl Into<Id>) -> Task<T> {
105    task::effect(Action::widget(operation::text_input::select_all(id.into())))
106}
107
108/// Selects the given content range of the widget with the given [`Id`].
109pub fn select_range<T>(id: impl Into<Id>, start: usize, end: usize) -> Task<T> {
110    task::effect(Action::widget(operation::text_input::select_range(
111        id.into(),
112        start,
113        end,
114    )))
115}
116
117/// Returns the [`Id`] of the currently focused widget, if any.
118///
119/// The resulting [`Task`] produces the focused widget's [`Id`].
120/// If no widget is focused, the task produces no value.
121pub fn find_focused() -> Task<Id> {
122    task::widget(operation::focusable::find_focused())
123}