From f1ebc6953e0cd61432b06230c7b4453d3ff2364b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 19 Feb 2023 09:46:50 -0800 Subject: [PATCH] macos: mouse scroll events --- include/ghostty.h | 1 + macos/Sources/TerminalSurfaceView.swift | 13 +++++++++++++ src/App.zig | 4 ++++ src/apprt/embedded.zig | 7 +++++++ 4 files changed, 25 insertions(+) diff --git a/include/ghostty.h b/include/ghostty.h index 3fb7b3616..9bb88d65a 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -231,6 +231,7 @@ void ghostty_surface_key(ghostty_surface_t, ghostty_input_action_e, ghostty_inpu void ghostty_surface_char(ghostty_surface_t, uint32_t); void ghostty_surface_mouse_button(ghostty_surface_t, ghostty_input_mouse_state_e, ghostty_input_mouse_button_e, ghostty_input_mods_e); void ghostty_surface_mouse_pos(ghostty_surface_t, double, double); +void ghostty_surface_mouse_scroll(ghostty_surface_t, double, double); #ifdef __cplusplus } diff --git a/macos/Sources/TerminalSurfaceView.swift b/macos/Sources/TerminalSurfaceView.swift index 6b75d8125..ac3a05500 100644 --- a/macos/Sources/TerminalSurfaceView.swift +++ b/macos/Sources/TerminalSurfaceView.swift @@ -329,6 +329,19 @@ class TerminalSurfaceView_Real: NSView, NSTextInputClient, ObservableObject { self.mouseMoved(with: event) } + override func scrollWheel(with event: NSEvent) { + guard let surface = self.surface else { return } + + var x = event.scrollingDeltaX + var y = event.scrollingDeltaY + if event.hasPreciseScrollingDeltas { + x *= 0.1 + y *= 0.1 + } + + ghostty_surface_mouse_scroll(surface, x, y) + } + override func keyDown(with event: NSEvent) { guard let surface = self.surface else { return } let key = Self.keycodes[event.keyCode] ?? GHOSTTY_KEY_INVALID diff --git a/src/App.zig b/src/App.zig index c7fc751d8..5857154c0 100644 --- a/src/App.zig +++ b/src/App.zig @@ -458,4 +458,8 @@ pub const CAPI = struct { export fn ghostty_surface_mouse_pos(win: *Window, x: f64, y: f64) void { win.window.cursorPosCallback(x, y); } + + export fn ghostty_surface_mouse_scroll(win: *Window, x: f64, y: f64) void { + win.window.scrollCallback(x, y); + } }; diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index 0e88a032a..8a1f345b0 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -175,6 +175,13 @@ pub const Window = struct { }; } + pub fn scrollCallback(self: *const Window, xoff: f64, yoff: f64) void { + self.core_win.scrollCallback(xoff, yoff) catch |err| { + log.err("error in scroll callback err={}", .{err}); + return; + }; + } + pub fn cursorPosCallback(self: *Window, x: f64, y: f64) void { // Convert our unscaled x/y to scaled. self.cursor_pos = self.core_win.window.cursorPosToPixels(.{