diff --git a/plugins/diopser/src/editor/button.rs b/plugins/diopser/src/editor/button.rs index 9384d113..81c96473 100644 --- a/plugins/diopser/src/editor/button.rs +++ b/plugins/diopser/src/editor/button.rs @@ -23,6 +23,10 @@ use std::sync::Arc; #[derive(Lens)] pub struct SafeModeButton>> { lens: L, + + /// The number of (fractional) scrolled lines that have not yet been turned into parameter + /// change events. This is needed to support trackpads with smooth scrolling. + scrolled_lines: f32, } impl>> SafeModeButton { @@ -31,14 +35,16 @@ impl>> SafeModeButton { where T: ToString, { - Self { lens: lens.clone() } - .build(cx, move |cx| { - Label::new(cx, label); - }) - .checked(lens.map(|v| v.load(Ordering::Relaxed))) - // We'll pretend this is a param-button, so this class is used for assigning a unique - // color - .class("safe-mode") + Self { + lens: lens.clone(), + scrolled_lines: 0.0, + } + .build(cx, move |cx| { + Label::new(cx, label); + }) + .checked(lens.map(|v| v.load(Ordering::Relaxed))) + // We'll pretend this is a param-button, so this class is used for assigning a unique color + .class("safe-mode") } } @@ -50,8 +56,8 @@ impl>> View for SafeModeButton { fn event(&mut self, cx: &mut EventContext, event: &mut Event) { event.map(|window_event, meta| match window_event { - WindowEvent::MouseDown(MouseButton::Left) // We don't need special double and triple click handling + WindowEvent::MouseDown(MouseButton::Left) | WindowEvent::MouseDoubleClick(MouseButton::Left) | WindowEvent::MouseTripleClick(MouseButton::Left) => { // We can just unconditionally toggle the boolean here @@ -60,6 +66,21 @@ impl>> View for SafeModeButton { meta.consume(); } + WindowEvent::MouseScroll(_scroll_x, scroll_y) => { + self.scrolled_lines += scroll_y; + + if self.scrolled_lines.abs() >= 1.0 { + let atomic = self.lens.get(cx); + + if self.scrolled_lines >= 1.0 { + atomic.store(true, Ordering::SeqCst); + self.scrolled_lines -= 1.0; + } else { + atomic.store(false, Ordering::SeqCst); + self.scrolled_lines += 1.0; + } + } + } _ => {} }); }