R3.2: Ctrl+wheel zooms one step per notch; version 0.1.4 [skip ci]
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
c1880288a5
commit
ee1b8ad375
13 files changed
+274
-37
No files matched your search
@@ -425,3 +425,43 @@ fn zooming_keeps_the_pointed_spot_and_walks_the_steps() {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ctrl_and_a_notch_zoom_one_step_about_the_pointer() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let cx = gpui::TestAppContext::single();
|
||||
cx.update(|app| {
|
||||
let (view, _) = live_view(app, &install);
|
||||
view.update(app, |view, cx| {
|
||||
view.viewport = (800.0, 600.0);
|
||||
view.origin = gpui::point(gpui::px(0.0), gpui::px(0.0));
|
||||
view.doc_size = (60_000, 80_000);
|
||||
view.scroll = (3_000, 9_000);
|
||||
view.set_zoom(1.0, cx);
|
||||
let at = gpui::point(gpui::px(300.0), gpui::px(200.0));
|
||||
let wheel = |modifiers, y| gpui::ScrollWheelEvent {
|
||||
position: at,
|
||||
delta: gpui::ScrollDelta::Lines(gpui::point(0.0, y)),
|
||||
modifiers,
|
||||
..Default::default()
|
||||
};
|
||||
let other = match cfg!(target_os = "macos") {
|
||||
true => gpui::Modifiers { control: true, ..Default::default() },
|
||||
false => gpui::Modifiers { platform: true, ..Default::default() },
|
||||
};
|
||||
assert!(!view.wheel_zoom(&wheel(other, 3.0), cx), "only Ctrl, or Cmd on macOS");
|
||||
assert_eq!(view.zoom(), 1.0);
|
||||
let spot = |view: &LokView| view.scroll.1 + view.geom.twips_of(200.0);
|
||||
let before = spot(view);
|
||||
assert!(view.wheel_zoom(&wheel(gpui::Modifiers::secondary_key(), 3.0), cx));
|
||||
assert_eq!(view.zoom(), 1.25, "a three-line notch is one step");
|
||||
assert!((spot(view) - before).abs() <= 1, "the pointed spot stays");
|
||||
view.wheel_zoom(&wheel(gpui::Modifiers::secondary_key(), -1.0), cx);
|
||||
view.wheel_zoom(&wheel(gpui::Modifiers::secondary_key(), -1.0), cx);
|
||||
assert_eq!(view.zoom(), 0.8);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -32,12 +32,7 @@ impl LokView {
|
||||
|
||||
/// The next step in or out from the zoom now.
|
||||
pub fn zoom_step(&mut self, larger: bool, cx: &mut Context<Self>) {
|
||||
let now = self.geom.zoom;
|
||||
let next = match larger {
|
||||
true => ZOOM_STEPS.into_iter().find(|step| *step > now + 0.001).unwrap_or(MOST),
|
||||
false => ZOOM_STEPS.into_iter().rev().find(|step| *step < now - 0.001).unwrap_or(LEAST),
|
||||
};
|
||||
self.zoom_at(next, None, cx);
|
||||
self.zoom_at(next_step(self.geom.zoom, larger), None, cx);
|
||||
}
|
||||
|
||||
/// Zooms keeping `at` (window pixels) still; none keeps the middle.
|
||||
@@ -70,17 +65,25 @@ impl LokView {
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Command with the wheel zooms; otherwise it scrolls.
|
||||
/// Ctrl (Cmd on macOS) with the wheel zooms; else it scrolls.
|
||||
pub(super) fn on_wheel(&mut self, event: &ScrollWheelEvent, window: &mut Window, cx: &mut Context<Self>) {
|
||||
if !event.modifiers.platform {
|
||||
return self.on_scroll(event, window, cx);
|
||||
if !self.wheel_zoom(event, cx) {
|
||||
self.on_scroll(event, window, cx);
|
||||
}
|
||||
let dy = match event.delta {
|
||||
ScrollDelta::Pixels(point) => f32::from(point.y),
|
||||
ScrollDelta::Lines(point) => point.y * 40.0,
|
||||
}
|
||||
|
||||
/// A notch is one step; a trackpad zooms smoothly.
|
||||
pub(super) fn wheel_zoom(&mut self, event: &ScrollWheelEvent, cx: &mut Context<Self>) -> bool {
|
||||
if !event.modifiers.secondary() {
|
||||
return false;
|
||||
}
|
||||
let zoom = match event.delta {
|
||||
ScrollDelta::Lines(lines) if lines.y == 0.0 => return true,
|
||||
ScrollDelta::Lines(lines) => next_step(self.geom.zoom, lines.y > 0.0),
|
||||
ScrollDelta::Pixels(point) => self.geom.zoom * (f32::from(point.y) / 200.0).exp(),
|
||||
};
|
||||
let zoom = self.geom.zoom * (dy / 200.0).exp();
|
||||
self.zoom_at(zoom, Some(event.position), cx);
|
||||
true
|
||||
}
|
||||
|
||||
/// A trackpad pinch zooms about its centre.
|
||||
@@ -89,3 +92,11 @@ impl LokView {
|
||||
self.zoom_at(zoom, Some(event.position), cx);
|
||||
}
|
||||
}
|
||||
|
||||
/// The next step in or out from `now`.
|
||||
fn next_step(now: f32, larger: bool) -> f32 {
|
||||
match larger {
|
||||
true => ZOOM_STEPS.into_iter().find(|step| *step > now + 0.001).unwrap_or(MOST),
|
||||
false => ZOOM_STEPS.into_iter().rev().find(|step| *step < now - 0.001).unwrap_or(LEAST),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user