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