From 68d2c4d053cce6f2bbf08bece3c0c7064857f412 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sat, 4 Jan 2025 17:20:52 +0100 Subject: [PATCH] feat(win32): add mouse wheel support --- src/win32/gui.zig | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/win32/gui.zig b/src/win32/gui.zig index ec60582..c857387 100644 --- a/src/win32/gui.zig +++ b/src/win32/gui.zig @@ -210,6 +210,7 @@ const State = struct { maybe_d2d: ?D2d = null, erase_bg_done: bool = false, text_format_editor: ddui.TextFormatCache(Dpi, createTextFormatEditor) = .{}, + scroll_delta: isize = 0, // these fields should only be accessed inside the global mutex shared_screen_arena: std.heap.ArenaAllocator, @@ -604,6 +605,43 @@ fn sendMouse( } } +fn sendMouseWheel( + hwnd: win32.HWND, + wparam: win32.WPARAM, + lparam: win32.LPARAM, +) void { + const point = ddui.pointFromLparam(lparam); + const state = stateFromHwnd(hwnd); + const dpi = win32.dpiFromHwnd(hwnd); + const text_format = state.text_format_editor.getOrCreate(Dpi{ .value = dpi }); + const cell_size = getCellSize(dpi, text_format); + const cell = cellFromPos(cell_size, point.x, point.y); + const cell_offset = cellOffsetFromPos(cell_size, point.x, point.y); + // const fwKeys = win32.loword(wparam); + state.scroll_delta += @as(i16, @bitCast(win32.hiword(wparam))); + while (@abs(state.scroll_delta) > win32.WHEEL_DELTA) { + const button = blk: { + if (state.scroll_delta > 0) { + state.scroll_delta -= win32.WHEEL_DELTA; + break :blk @intFromEnum(input.mouse.BUTTON4); + } + state.scroll_delta += win32.WHEEL_DELTA; + break :blk @intFromEnum(input.mouse.BUTTON5); + }; + + state.pid.send(.{ + "RDR", + "B", + input.event.press, + button, + cell.x, + cell.y, + cell_offset.x, + cell_offset.y, + }) catch |e| onexit(e); + } +} + fn sendKey( hwnd: win32.HWND, kind: enum { @@ -737,6 +775,10 @@ fn WndProc( sendMouse(hwnd, .right_up, lparam); return 0; }, + win32.WM_MOUSEWHEEL => { + sendMouseWheel(hwnd, wparam, lparam); + return 0; + }, win32.WM_KEYDOWN => { sendKey(hwnd, .press, wparam, lparam); return 0;