From de5468e2144bf6cc05575c58e0bf0358aa463e85 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 29 Jun 2023 11:47:28 -0700 Subject: [PATCH] scale x, speed up scrolling --- macos/Sources/Ghostty/SurfaceView.swift | 6 +++- src/Surface.zig | 37 +++++++++++++++++++------ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/macos/Sources/Ghostty/SurfaceView.swift b/macos/Sources/Ghostty/SurfaceView.swift index fd239ab7b..48467e264 100644 --- a/macos/Sources/Ghostty/SurfaceView.swift +++ b/macos/Sources/Ghostty/SurfaceView.swift @@ -299,7 +299,11 @@ extension Ghostty { if event.hasPreciseScrollingDeltas { mods = 1 - // TODO(mitchellh): do we have to scale the x/y here? + // We do a 2x speed multiplier. This is subjective, it "feels" better to me. + x *= 2; + y *= 2; + + // TODO(mitchellh): do we have to scale the x/y here by window scale factor? } // Determine our momentum value diff --git a/src/Surface.zig b/src/Surface.zig index 7afb469ae..6cf4e28c4 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -1342,11 +1342,32 @@ pub fn scrollCallback( }; }; - // Positive is right - const x_sign: isize = if (xoff < 0) -1 else 1; - const x_delta_unsigned: usize = if (xoff == 0) 0 else 1; - const x_delta: isize = x_sign * @intCast(isize, x_delta_unsigned); - log.info("scroll: delta_y={} delta_x={}", .{ y.delta, x_delta }); + // For detailed comments see the y calculation above. + const x: ScrollAmount = if (xoff == 0) .{} else x: { + if (!scroll_mods.precision) { + const x_sign: isize = if (xoff < 0) -1 else 1; + const x_delta_unsigned: usize = 1; + const x_delta: isize = x_sign * @intCast(isize, x_delta_unsigned); + break :x .{ .sign = x_sign, .delta_unsigned = x_delta_unsigned, .delta = x_delta }; + } + + const poff = self.mouse.pending_scroll_x + (xoff * -1); + const cell_size = self.cell_size.width; + if (@fabs(poff) < cell_size) { + self.mouse.pending_scroll_x = poff; + break :x .{}; + } + + const amount = poff / cell_size; + self.mouse.pending_scroll_x = poff - (amount * cell_size); + + break :x .{ + .delta_unsigned = @intFromFloat(usize, @fabs(amount)), + .delta = @intFromFloat(isize, amount), + }; + }; + + log.info("scroll: delta_y={} delta_x={}", .{ y.delta, x.delta }); { self.renderer_state.mutex.lock(); @@ -1376,9 +1397,9 @@ pub fn scrollCallback( } } - if (x_delta_unsigned > 0) { - const seq = if (x_delta < 0) "\x1bOC" else "\x1bOD"; - for (0..x_delta_unsigned) |_| { + if (x.delta_unsigned > 0) { + const seq = if (x.delta < 0) "\x1bOC" else "\x1bOD"; + for (0..x.delta_unsigned) |_| { _ = self.io_thread.mailbox.push(.{ .write_stable = seq, }, .{ .forever = {} });