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::{
8    AbsoluteOffset, RelativeOffset,
9};
10
11/// Snaps the scrollable with the given [`Id`] to the provided [`RelativeOffset`].
12pub fn snap_to<T>(id: impl Into<Id>, offset: RelativeOffset) -> Task<T> {
13    task::effect(Action::widget(operation::scrollable::snap_to(
14        id.into(),
15        offset,
16    )))
17}
18
19/// Snaps the scrollable with the given [`Id`] to the [`RelativeOffset::END`].
20pub fn snap_to_end<T>(id: impl Into<Id>) -> Task<T> {
21    task::effect(Action::widget(operation::scrollable::snap_to(
22        id.into(),
23        RelativeOffset::END,
24    )))
25}
26
27/// Scrolls the scrollable with the given [`Id`] to the provided [`AbsoluteOffset`].
28pub fn scroll_to<T>(id: impl Into<Id>, offset: AbsoluteOffset) -> Task<T> {
29    task::effect(Action::widget(operation::scrollable::scroll_to(
30        id.into(),
31        offset,
32    )))
33}
34
35/// Scrolls the scrollable with the given [`Id`] by the provided [`AbsoluteOffset`].
36pub fn scroll_by<T>(id: impl Into<Id>, offset: AbsoluteOffset) -> Task<T> {
37    task::effect(Action::widget(operation::scrollable::scroll_by(
38        id.into(),
39        offset,
40    )))
41}
42
43/// Focuses the previous focusable widget.
44pub fn focus_previous<T>() -> Task<T> {
45    task::effect(Action::widget(operation::focusable::focus_previous()))
46}
47
48/// Focuses the next focusable widget.
49pub fn focus_next<T>() -> Task<T> {
50    task::effect(Action::widget(operation::focusable::focus_next()))
51}
52
53/// Returns whether the widget with the given [`Id`] is focused or not.
54pub fn is_focused(id: impl Into<Id>) -> Task<bool> {
55    task::widget(operation::focusable::is_focused(id.into()))
56}
57
58/// Focuses the widget with the given [`Id`].
59pub fn focus<T>(id: impl Into<Id>) -> Task<T> {
60    task::effect(Action::widget(operation::focusable::focus(id.into())))
61}
62
63/// Moves the cursor of the widget with the given [`Id`] to the end.
64pub fn move_cursor_to_end<T>(id: impl Into<Id>) -> Task<T> {
65    task::effect(Action::widget(operation::text_input::move_cursor_to_end(
66        id.into(),
67    )))
68}
69
70/// Moves the cursor of the widget with the given [`Id`] to the front.
71pub fn move_cursor_to_front<T>(id: impl Into<Id>) -> Task<T> {
72    task::effect(Action::widget(operation::text_input::move_cursor_to_front(
73        id.into(),
74    )))
75}
76
77/// Moves the cursor of the widget with the given [`Id`] to the provided position.
78pub fn move_cursor_to<T>(id: impl Into<Id>, position: usize) -> Task<T> {
79    task::effect(Action::widget(operation::text_input::move_cursor_to(
80        id.into(),
81        position,
82    )))
83}
84
85/// Selects all the content of the widget with the given [`Id`].
86pub fn select_all<T>(id: impl Into<Id>) -> Task<T> {
87    task::effect(Action::widget(operation::text_input::select_all(id.into())))
88}