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:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-12 23:15:47 -04:00
1 parent c1880288a5
commit ee1b8ad375
13 files changed
+274 -37

No files matched your search

+40
View File
@@ -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);
});
});
}
+24 -13
View File
@@ -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),
}
}
+1
View File
@@ -8,6 +8,7 @@ pub mod tasks;
mod theme;
pub mod updates;
pub mod watching;
pub mod wheel;
pub use opening::{launch_files, on_open};
#[cfg(any(test, feature = "test-support"))]
+85
View File
@@ -0,0 +1,85 @@
//! Ctrl (Cmd on macOS) and the wheel step a zoom.
use gpui::{
App, DispatchPhase, HitboxBehavior, IntoElement, ScrollDelta, ScrollWheelEvent, Styled as _,
Window, canvas,
};
/// Trackpad pixels one step takes.
const STEP: f32 = 50.0;
/// Steps `event` makes, plus is larger; `carry` keeps trackpad rest.
pub fn steps(event: &ScrollWheelEvent, carry: &mut f32) -> i32 {
match event.delta {
ScrollDelta::Lines(lines) => match lines.y {
0.0 => 0,
y => y.signum() as i32,
},
ScrollDelta::Pixels(point) => {
*carry += f32::from(point.y);
let whole = (*carry / STEP).trunc();
*carry -= whole * STEP;
whole as i32
}
}
}
/// Over its parent, takes a zooming wheel before the parent scrolls.
pub fn zoom_layer(
on: impl Fn(&ScrollWheelEvent, &mut Window, &mut App) + 'static,
) -> impl IntoElement {
canvas(
|bounds, window, _| window.insert_hitbox(bounds, HitboxBehavior::Normal),
move |_, hitbox, window, _| {
window.on_mouse_event(move |event: &ScrollWheelEvent, phase, window, cx| {
if phase == DispatchPhase::Capture
&& event.modifiers.secondary()
&& hitbox.is_hovered(window)
{
cx.stop_propagation();
on(event, window, cx);
}
})
},
)
.absolute()
.top_0()
.left_0()
.size_full()
}
#[cfg(test)]
mod tests {
use gpui::{ScrollDelta, ScrollWheelEvent, point, px};
use super::steps;
fn wheel(delta: ScrollDelta) -> ScrollWheelEvent {
ScrollWheelEvent {
delta,
..Default::default()
}
}
#[test]
fn a_notch_is_one_step_and_pixels_add_up() {
let mut carry = 0.0;
assert_eq!(
steps(&wheel(ScrollDelta::Lines(point(0.0, 3.0))), &mut carry),
1
);
assert_eq!(
steps(&wheel(ScrollDelta::Lines(point(0.0, -1.0))), &mut carry),
-1
);
assert_eq!(
steps(&wheel(ScrollDelta::Lines(point(2.0, 0.0))), &mut carry),
0
);
let pixels = |y| wheel(ScrollDelta::Pixels(point(px(0.0), px(y))));
assert_eq!(steps(&pixels(30.0), &mut carry), 0);
assert_eq!(steps(&pixels(30.0), &mut carry), 1);
assert_eq!(carry, 10.0);
assert_eq!(steps(&pixels(-120.0), &mut carry), -2);
}
}