1
0
Fork 0

Add scrolling support for the safe mode button

This commit is contained in:
Robbert van der Helm 2022-11-08 21:25:24 +01:00
parent fd85155fbc
commit 316d9221df

View file

@ -23,6 +23,10 @@ use std::sync::Arc;
#[derive(Lens)] #[derive(Lens)]
pub struct SafeModeButton<L: Lens<Target = Arc<AtomicBool>>> { pub struct SafeModeButton<L: Lens<Target = Arc<AtomicBool>>> {
lens: L, 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<L: Lens<Target = Arc<AtomicBool>>> SafeModeButton<L> { impl<L: Lens<Target = Arc<AtomicBool>>> SafeModeButton<L> {
@ -31,13 +35,15 @@ impl<L: Lens<Target = Arc<AtomicBool>>> SafeModeButton<L> {
where where
T: ToString, T: ToString,
{ {
Self { lens: lens.clone() } Self {
lens: lens.clone(),
scrolled_lines: 0.0,
}
.build(cx, move |cx| { .build(cx, move |cx| {
Label::new(cx, label); Label::new(cx, label);
}) })
.checked(lens.map(|v| v.load(Ordering::Relaxed))) .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 // We'll pretend this is a param-button, so this class is used for assigning a unique color
// color
.class("safe-mode") .class("safe-mode")
} }
} }
@ -50,8 +56,8 @@ impl<L: Lens<Target = Arc<AtomicBool>>> View for SafeModeButton<L> {
fn event(&mut self, cx: &mut EventContext, event: &mut Event) { fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
event.map(|window_event, meta| match window_event { event.map(|window_event, meta| match window_event {
WindowEvent::MouseDown(MouseButton::Left)
// We don't need special double and triple click handling // We don't need special double and triple click handling
WindowEvent::MouseDown(MouseButton::Left)
| WindowEvent::MouseDoubleClick(MouseButton::Left) | WindowEvent::MouseDoubleClick(MouseButton::Left)
| WindowEvent::MouseTripleClick(MouseButton::Left) => { | WindowEvent::MouseTripleClick(MouseButton::Left) => {
// We can just unconditionally toggle the boolean here // We can just unconditionally toggle the boolean here
@ -60,6 +66,21 @@ impl<L: Lens<Target = Arc<AtomicBool>>> View for SafeModeButton<L> {
meta.consume(); 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;
}
}
}
_ => {} _ => {}
}); });
} }