diff options
author | 2022-08-04 03:24:44 +0200 | |
---|---|---|
committer | 2022-08-04 03:24:44 +0200 | |
commit | 6eb3dd7e5edc8847875c288c41d1dec8b1dad06e (patch) | |
tree | d4b8ff42a57d520e90fe8d854d83cc3a9377e06f /native | |
parent | 54ad92ce913629d1d1f623f4b14d51244554a59c (diff) | |
download | iced-6eb3dd7e5edc8847875c288c41d1dec8b1dad06e.tar.gz iced-6eb3dd7e5edc8847875c288c41d1dec8b1dad06e.tar.bz2 iced-6eb3dd7e5edc8847875c288c41d1dec8b1dad06e.zip |
Implement `focus_previous` operation
Diffstat (limited to 'native')
-rw-r--r-- | native/src/widget/operation.rs | 47 |
1 files changed, 39 insertions, 8 deletions
diff --git a/native/src/widget/operation.rs b/native/src/widget/operation.rs index 5a0f0c18..caf7ba1c 100644 --- a/native/src/widget/operation.rs +++ b/native/src/widget/operation.rs @@ -109,13 +109,13 @@ where } } -pub fn focus_next<T>() -> impl Operation<T> { - struct FocusNext { +pub fn focus_previous<T>() -> impl Operation<T> { + struct FocusPrevious { count: FocusCount, current: usize, } - impl<T> Operation<T> for FocusNext { + impl<T> Operation<T> for FocusPrevious { fn focusable( &mut self, state: &mut dyn state::Focusable, @@ -126,14 +126,45 @@ pub fn focus_next<T>() -> impl Operation<T> { } match self.count.focused { + None if self.current == self.count.total - 1 => state.focus(), + Some(0) if self.current == 0 => state.unfocus(), + Some(0) => {} + Some(focused) if focused == self.current => state.unfocus(), + Some(focused) if focused - 1 == self.current => state.focus(), + _ => {} + } + + self.current += 1; + } + + fn container( + &mut self, + _id: Option<&Id>, + operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>), + ) { + operate_on_children(self) + } + } + + count_focusable(|count| FocusPrevious { count, current: 0 }) +} + +pub fn focus_next<T>() -> impl Operation<T> { + struct FocusNext { + count: FocusCount, + current: usize, + } + + impl<T> Operation<T> for FocusNext { + fn focusable( + &mut self, + state: &mut dyn state::Focusable, + _id: Option<&Id>, + ) { + match self.count.focused { None if self.current == 0 => state.focus(), Some(focused) if focused == self.current => state.unfocus(), Some(focused) if focused + 1 == self.current => state.focus(), - Some(focused) - if focused == self.count.total - 1 && self.current == 0 => - { - state.focus() - } _ => {} } |