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/// Moves the cursor of the widget with the given [`Id`] to the end.
62pub fn move_cursor_to_end<T>(id: impl Into<Id>) -> Task<T> {
63    task::effect(Action::widget(operation::text_input::move_cursor_to_end(
64        id.into(),
65    )))
66}
67
68/// Moves the cursor of the widget with the given [`Id`] to the front.
69pub fn move_cursor_to_front<T>(id: impl Into<Id>) -> Task<T> {
70    task::effect(Action::widget(operation::text_input::move_cursor_to_front(
71        id.into(),
72    )))
73}
74
75/// Moves the cursor of the widget with the given [`Id`] to the provided position.
76pub fn move_cursor_to<T>(id: impl Into<Id>, position: usize) -> Task<T> {
77    task::effect(Action::widget(operation::text_input::move_cursor_to(
78        id.into(),
79        position,
80    )))
81}
82
83/// Selects all the content of the widget with the given [`Id`].
84pub fn select_all<T>(id: impl Into<Id>) -> Task<T> {
85    task::effect(Action::widget(operation::text_input::select_all(id.into())))
86}
87
88/// Selects the given content range of the widget with the given [`Id`].
89pub fn select_range<T>(id: impl Into<Id>, start: usize, end: usize) -> Task<T> {
90    task::effect(Action::widget(operation::text_input::select_range(
91        id.into(),
92        start,
93        end,
94    )))
95}