Move all operations to widget::operation module

This commit is contained in:
Héctor Ramón Jiménez 2025-08-23 03:54:54 +02:00
parent 885d45f435
commit 34a42b5ad4
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
14 changed files with 124 additions and 136 deletions

View file

@ -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<T>() -> Task<T> {
task::effect(Action::widget(operation::focusable::focus_previous()))
}
/// Focuses the next focusable widget.
pub fn focus_next<T>() -> Task<T> {
task::effect(Action::widget(operation::focusable::focus_next()))
}
/// Creates a new [`MouseArea`].
pub fn mouse_area<'a, Message, Theme, Renderer>(
widget: impl Into<Element<'a, Message, Theme, Renderer>>,

View file

@ -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;

88
widget/src/operation.rs Normal file
View file

@ -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<T>(id: impl Into<Id>, offset: RelativeOffset) -> Task<T> {
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<T>(id: impl Into<Id>) -> Task<T> {
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<T>(id: impl Into<Id>, offset: AbsoluteOffset) -> Task<T> {
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<T>(id: impl Into<Id>, offset: AbsoluteOffset) -> Task<T> {
task::effect(Action::widget(operation::scrollable::scroll_by(
id.into(),
offset,
)))
}
/// Focuses the previous focusable widget.
pub fn focus_previous<T>() -> Task<T> {
task::effect(Action::widget(operation::focusable::focus_previous()))
}
/// Focuses the next focusable widget.
pub fn focus_next<T>() -> Task<T> {
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<Id>) -> Task<bool> {
task::widget(operation::focusable::is_focused(id.into()))
}
/// Focuses the widget with the given [`Id`].
pub fn focus<T>(id: impl Into<Id>) -> Task<T> {
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<T>(id: impl Into<Id>) -> Task<T> {
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<T>(id: impl Into<Id>) -> Task<T> {
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<T>(id: impl Into<Id>, position: usize) -> Task<T> {
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<T>(id: impl Into<Id>) -> Task<T> {
task::effect(Action::widget(operation::text_input::select_all(id.into())))
}

View file

@ -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<T>(
id: impl Into<widget::Id>,
offset: RelativeOffset,
) -> Task<T> {
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<T>(
id: impl Into<widget::Id>,
offset: AbsoluteOffset,
) -> Task<T> {
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<T>(
id: impl Into<widget::Id>,
offset: AbsoluteOffset,
) -> Task<T> {
task::effect(Action::widget(operation::scrollable::scroll_by(
id.into(),
offset,
)))
}
fn notify_scroll<Message>(
state: &mut State,
on_scroll: &Option<Box<dyn Fn(Viewport) -> Message + '_>>,

View file

@ -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<widget::Id>) -> Task<bool> {
task::widget(operation::focusable::is_focused(id.into()))
}
/// Produces a [`Task`] that focuses the [`TextInput`] with the given [`widget::Id`].
pub fn focus<T>(id: impl Into<widget::Id>) -> Task<T> {
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<T>(id: impl Into<widget::Id>) -> Task<T> {
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<T>(id: impl Into<widget::Id>) -> Task<T> {
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<T>(
id: impl Into<widget::Id>,
position: usize,
) -> Task<T> {
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<T>(id: impl Into<widget::Id>) -> Task<T> {
task::effect(Action::widget(operation::text_input::select_all(id.into())))
}
/// The state of a [`TextInput`].
#[derive(Debug, Default, Clone)]
pub struct State<P: text::Paragraph> {