Add scrolling support for the safe mode button
This commit is contained in:
parent
fd85155fbc
commit
316d9221df
|
@ -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,14 +35,16 @@ impl<L: Lens<Target = Arc<AtomicBool>>> SafeModeButton<L> {
|
||||||
where
|
where
|
||||||
T: ToString,
|
T: ToString,
|
||||||
{
|
{
|
||||||
Self { lens: lens.clone() }
|
Self {
|
||||||
.build(cx, move |cx| {
|
lens: lens.clone(),
|
||||||
Label::new(cx, label);
|
scrolled_lines: 0.0,
|
||||||
})
|
}
|
||||||
.checked(lens.map(|v| v.load(Ordering::Relaxed)))
|
.build(cx, move |cx| {
|
||||||
// We'll pretend this is a param-button, so this class is used for assigning a unique
|
Label::new(cx, label);
|
||||||
// color
|
})
|
||||||
.class("safe-mode")
|
.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<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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue