Introduce black_box and chain in widget::operation

This commit is contained in:
Héctor Ramón Jiménez 2024-08-08 01:25:00 +02:00
parent d5ffe98ce9
commit 422568dee4
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
31 changed files with 232 additions and 67 deletions

View file

@ -1,5 +1,5 @@
//! Operate on widgets that can be focused.
use crate::widget::operation::{Operation, Outcome};
use crate::widget::operation::{self, Operation, Outcome};
use crate::widget::Id;
use crate::Rectangle;
@ -58,19 +58,12 @@ pub fn focus<T>(target: Id) -> impl Operation<T> {
/// Produces an [`Operation`] that generates a [`Count`] and chains it with the
/// provided function to build a new [`Operation`].
pub fn count<T, O>(f: fn(Count) -> O) -> impl Operation<T>
where
O: Operation<T> + 'static,
{
struct CountFocusable<O> {
pub fn count() -> impl Operation<Count> {
struct CountFocusable {
count: Count,
next: fn(Count) -> O,
}
impl<T, O> Operation<T> for CountFocusable<O>
where
O: Operation<T> + 'static,
{
impl Operation<Count> for CountFocusable {
fn focusable(&mut self, state: &mut dyn Focusable, _id: Option<&Id>) {
if state.is_focused() {
self.count.focused = Some(self.count.total);
@ -83,26 +76,25 @@ where
&mut self,
_id: Option<&Id>,
_bounds: Rectangle,
operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
operate_on_children: &mut dyn FnMut(&mut dyn Operation<Count>),
) {
operate_on_children(self);
}
fn finish(&self) -> Outcome<T> {
Outcome::Chain(Box::new((self.next)(self.count)))
fn finish(&self) -> Outcome<Count> {
Outcome::Some(self.count)
}
}
CountFocusable {
count: Count::default(),
next: f,
}
}
/// Produces an [`Operation`] that searches for the current focused widget, and
/// - if found, focuses the previous focusable widget.
/// - if not found, focuses the last focusable widget.
pub fn focus_previous<T>() -> impl Operation<T> {
pub fn focus_previous() -> impl Operation {
struct FocusPrevious {
count: Count,
current: usize,
@ -136,13 +128,13 @@ pub fn focus_previous<T>() -> impl Operation<T> {
}
}
count(|count| FocusPrevious { count, current: 0 })
operation::chain(count(), |count| FocusPrevious { count, current: 0 })
}
/// Produces an [`Operation`] that searches for the current focused widget, and
/// - if found, focuses the next focusable widget.
/// - if not found, focuses the first focusable widget.
pub fn focus_next<T>() -> impl Operation<T> {
pub fn focus_next() -> impl Operation {
struct FocusNext {
count: Count,
current: usize,
@ -170,7 +162,7 @@ pub fn focus_next<T>() -> impl Operation<T> {
}
}
count(|count| FocusNext { count, current: 0 })
operation::chain(count(), |count| FocusNext { count, current: 0 })
}
/// Produces an [`Operation`] that searches for the current focused widget