2022-08-05 05:15:41 +02:00
|
|
|
//! Query or update internal widget state.
|
2022-08-04 03:55:41 +02:00
|
|
|
pub mod focusable;
|
|
|
|
|
pub mod scrollable;
|
|
|
|
|
|
|
|
|
|
pub use focusable::Focusable;
|
|
|
|
|
pub use scrollable::Scrollable;
|
|
|
|
|
|
2022-07-28 02:46:51 +02:00
|
|
|
use crate::widget::Id;
|
|
|
|
|
|
2022-08-05 05:15:41 +02:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
|
|
/// A piece of logic that can traverse the widget tree of an application in
|
|
|
|
|
/// order to query or update some widget state.
|
2022-07-28 02:46:51 +02:00
|
|
|
pub trait Operation<T> {
|
2022-08-05 05:15:41 +02:00
|
|
|
/// Operates on a widget that contains other widgets.
|
|
|
|
|
///
|
|
|
|
|
/// The `operate_on_children` function can be called to return control to
|
|
|
|
|
/// the widget tree and keep traversing it.
|
2022-07-28 02:46:51 +02:00
|
|
|
fn container(
|
|
|
|
|
&mut self,
|
|
|
|
|
id: Option<&Id>,
|
2022-07-28 03:53:47 +02:00
|
|
|
operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
|
2022-07-28 02:46:51 +02:00
|
|
|
);
|
|
|
|
|
|
2022-08-05 05:15:41 +02:00
|
|
|
/// Operates on a widget that can be focused.
|
2022-08-04 03:55:41 +02:00
|
|
|
fn focusable(&mut self, _state: &mut dyn Focusable, _id: Option<&Id>) {}
|
|
|
|
|
|
2022-08-05 05:15:41 +02:00
|
|
|
/// Operates on a widget that can be scrolled.
|
2022-08-04 03:55:41 +02:00
|
|
|
fn scrollable(&mut self, _state: &mut dyn Scrollable, _id: Option<&Id>) {}
|
2022-07-28 02:46:51 +02:00
|
|
|
|
2022-08-05 05:15:41 +02:00
|
|
|
/// Finishes the [`Operation`] and returns its [`Outcome`].
|
2022-07-28 02:46:51 +02:00
|
|
|
fn finish(&self) -> Outcome<T> {
|
|
|
|
|
Outcome::None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 05:15:41 +02:00
|
|
|
/// The result of an [`Operation`].
|
2022-07-28 02:46:51 +02:00
|
|
|
pub enum Outcome<T> {
|
2022-08-05 05:15:41 +02:00
|
|
|
/// The [`Operation`] produced no result.
|
2022-07-28 02:46:51 +02:00
|
|
|
None,
|
2022-08-05 05:15:41 +02:00
|
|
|
|
|
|
|
|
/// The [`Operation`] produced some result.
|
2022-07-28 02:46:51 +02:00
|
|
|
Some(T),
|
2022-08-05 05:15:41 +02:00
|
|
|
|
|
|
|
|
/// The [`Operation`] needs to be followed by another [`Operation`].
|
2022-07-28 02:46:51 +02:00
|
|
|
Chain(Box<dyn Operation<T>>),
|
|
|
|
|
}
|
2022-08-05 05:15:41 +02:00
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for Outcome<T>
|
|
|
|
|
where
|
|
|
|
|
T: fmt::Debug,
|
|
|
|
|
{
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
match self {
|
|
|
|
|
Self::None => write!(f, "Outcome::None"),
|
|
|
|
|
Self::Some(output) => write!(f, "Outcome::Some({:?})", output),
|
|
|
|
|
Self::Chain(_) => write!(f, "Outcome::Chain(...)"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|