From 6adc037f40841b305522daad43b9a37891436c6b Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Mon, 12 Feb 2024 17:44:21 +0100 Subject: [PATCH] feat(segmented_button): add `scrollable_focus` to toggle focus scrolling --- src/widget/segmented_button/widget.rs | 81 ++++++++++++++------------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/src/widget/segmented_button/widget.rs b/src/widget/segmented_button/widget.rs index e3bee4aa..7a888e20 100644 --- a/src/widget/segmented_button/widget.rs +++ b/src/widget/segmented_button/widget.rs @@ -63,6 +63,8 @@ where pub(super) id: Option, /// The icon used for the close button. pub(super) close_icon: Icon, + /// Scrolling switches focus between tabs. + pub(super) scrollable_focus: bool, /// Show the close icon only when item is hovered. pub(super) show_close_icon_on_hover: bool, /// Padding around a button. @@ -115,6 +117,7 @@ where model, id: None, close_icon: icon::from_name("window-close-symbolic").size(16).icon(), + scrollable_focus: false, show_close_icon_on_hover: false, button_padding: [4, 4, 4, 4], button_height: 32, @@ -541,58 +544,60 @@ where } } - if let Some(on_activate) = self.on_activate.as_ref() { - if let Event::Mouse(mouse::Event::WheelScrolled { delta }) = event { - let current = Instant::now(); + if self.scrollable_focus { + if let Some(on_activate) = self.on_activate.as_ref() { + if let Event::Mouse(mouse::Event::WheelScrolled { delta }) = event { + let current = Instant::now(); - // Permit successive scroll wheel events only after a given delay. - if state.wheel_timestamp.map_or(true, |previous| { - current.duration_since(previous) > Duration::from_millis(250) - }) { - state.wheel_timestamp = Some(current); + // Permit successive scroll wheel events only after a given delay. + if state.wheel_timestamp.map_or(true, |previous| { + current.duration_since(previous) > Duration::from_millis(250) + }) { + state.wheel_timestamp = Some(current); - match delta { - ScrollDelta::Lines { y, .. } | ScrollDelta::Pixels { y, .. } => { - let mut activate_key = None; + match delta { + ScrollDelta::Lines { y, .. } | ScrollDelta::Pixels { y, .. } => { + let mut activate_key = None; - if y < 0.0 { - let mut prev_key = Entity::null(); + if y < 0.0 { + let mut prev_key = Entity::null(); - for key in self.model.order.iter().copied() { - if self.model.is_active(key) && !prev_key.is_null() { - activate_key = Some(prev_key); - } - - if self.model.is_enabled(key) { - prev_key = key; - } - } - } else if y > 0.0 { - let mut buttons = self.model.order.iter().copied(); - while let Some(key) = buttons.next() { - if self.model.is_active(key) { - for key in buttons { - if self.model.is_enabled(key) { - activate_key = Some(key); - break; - } + for key in self.model.order.iter().copied() { + if self.model.is_active(key) && !prev_key.is_null() { + activate_key = Some(prev_key); + } + + if self.model.is_enabled(key) { + prev_key = key; + } + } + } else if y > 0.0 { + let mut buttons = self.model.order.iter().copied(); + while let Some(key) = buttons.next() { + if self.model.is_active(key) { + for key in buttons { + if self.model.is_enabled(key) { + activate_key = Some(key); + break; + } + } + break; } - break; } } - } - if let Some(key) = activate_key { - shell.publish(on_activate(key)); - return event::Status::Captured; + if let Some(key) = activate_key { + shell.publish(on_activate(key)); + return event::Status::Captured; + } } } } } } + } else { + state.hovered = Item::None; } - } else { - state.hovered = Item::None; } if state.focused {