diff --git a/src/Debug.zig b/src/Debug.zig index 707b7020..54e477a7 100644 --- a/src/Debug.zig +++ b/src/Debug.zig @@ -313,8 +313,15 @@ pub fn show(self: *Debug) void { self.options_editor_open = false; } - if (dvui.button(@src(), if (debug_target == .mouse_until_click) "Stop (Or Left Click)" else "Debug Under Mouse (until click)", .{}, .{})) { - debug_target = if (debug_target == .mouse_until_click) .none else .mouse_until_click; + { + var hbox = dvui.box(@src(), .{ .dir = .horizontal }, .{ .expand = .horizontal }); + defer hbox.deinit(); + + dvui.label(@src(), "Mouse {any}", .{dvui.mouseType()}, .{ .gravity_x = 1.0 }); + + if (dvui.button(@src(), if (debug_target == .mouse_until_click) "Stop (Or Left Click)" else "Debug Under Mouse (until click)", .{}, .{})) { + debug_target = if (debug_target == .mouse_until_click) .none else .mouse_until_click; + } } if (dvui.button(@src(), if (debug_target == .mouse_until_esc) "Stop (Or Press Esc)" else "Debug Under Mouse (until esc)", .{}, .{})) { diff --git a/src/Window.zig b/src/Window.zig index e881d724..e7087c8a 100644 --- a/src/Window.zig +++ b/src/Window.zig @@ -43,9 +43,9 @@ event_num: u16 = 0, // Start off screen so nothing is highlighted on the first frame mouse_pt: Point.Physical = .{ .x = -1, .y = -1 }, mouse_pt_prev: Point.Physical = .{ .x = -1, .y = -1 }, -mouse_type_hint: dvui.enums.MouseTypeHint = .unknown, -mouse_type_hint_min: [2]f32 = .{ 99999, 99999 }, -mouse_type_hint_last_wheel_ns: ?i128 = null, +mouse_type: dvui.enums.MouseType = .unknown, +mouse_type_min: [2]f32 = .{ 99999, 99999 }, +mouse_type_last_wheel_ns: ?i128 = null, /// Holds the current state of the modifiers from the most /// recently added key event. Used for adding modifiers to /// mouse events @@ -818,10 +818,6 @@ pub fn addEventPointer(self: *Self, opts: AddEventPointerOptions) std.mem.Alloca return ret; } -pub fn mouseTypeHint(self: *const Self) dvui.enums.MouseTypeHint { - return self.mouse_type_hint; -} - fn scrollWheelIndicated(min_batch_seen: f32) bool { const eps: f32 = 0.008; if (min_batch_seen >= 99990.0) return false; @@ -834,26 +830,24 @@ fn scrollWheelIndicated(min_batch_seen: f32) bool { return false; } -fn determineMouseHint(self: *Self, dir: dvui.enums.Direction, raw_delta_abs_hint: f32) void { - if (std.math.isNan(raw_delta_abs_hint) or raw_delta_abs_hint <= 0) return; +fn determineMouseType(self: *Self, dir: dvui.enums.Direction, raw_delta: f32) void { + const delta_abs = @abs(raw_delta); + if (std.math.isNan(delta_abs) or delta_abs == 0) return; const now = std.Io.Clock.boot.now(dvui.io).nanoseconds; const stale = blk: { - if (self.mouse_type_hint_last_wheel_ns) |prev| { + if (self.mouse_type_last_wheel_ns) |prev| { break :blk (now - prev > std.time.ns_per_s); } else break :blk true; }; - self.mouse_type_hint_last_wheel_ns = now; + self.mouse_type_last_wheel_ns = now; if (stale) - self.mouse_type_hint_min = .{ 99999, 99999 }; + self.mouse_type_min = .{ 99999, 99999 }; const ax: usize = if (dir == .horizontal) 0 else 1; - self.mouse_type_hint_min[ax] = @min(self.mouse_type_hint_min[ax], raw_delta_abs_hint); - - const b = self.mouse_type_hint_min; - const discrete = scrollWheelIndicated(b[0]) or scrollWheelIndicated(b[1]); - self.mouse_type_hint = if (discrete) .mouse else .trackpad; + self.mouse_type_min[ax] = @min(self.mouse_type_min[ax], delta_abs); + self.mouse_type = if (scrollWheelIndicated(self.mouse_type_min[ax])) .mouse else .trackpad; } /// Add a mouse wheel event. Positive ticks means scrolling up / scrolling right. @@ -861,17 +855,15 @@ fn determineMouseHint(self: *Self, dir: dvui.enums.Direction, raw_delta_abs_hint /// If the shift key is being held, any vertical scroll will be transformed to /// horizontal. /// +/// When `raw_delta` is non-null, dvui will use it to guess the mouse type (see +/// `dvui.mouseType`). It should be the OS-reported magnitude +/// (pixels/lines-equivalent wheel delta, same as DOM `wheel` events). +/// /// This can be called outside begin/end. You should add all the events /// for a frame either before begin() or just after begin() and before /// calling normal dvui widgets. end() clears the event list. -pub fn addEventMouseWheel(self: *Self, ticks: f32, dir: dvui.enums.Direction) std.mem.Allocator.Error!bool { - return self.addEventMouseWheelWithHint(ticks, dir, null); -} - -/// Same as `addEventMouseWheel`. When `raw_delta_abs_hint` is non-null, refines `mouseTypeHint` from the -/// absolute OS-reported magnitude (pixels/lines-equivalent wheel delta, same conventions as DOM `wheel` events). -pub fn addEventMouseWheelWithHint(self: *Self, ticks: f32, dir: dvui.enums.Direction, raw_delta_abs_hint: ?f32) std.mem.Allocator.Error!bool { - if (raw_delta_abs_hint) |h| determineMouseHint(self, dir, h); +pub fn addEventMouseWheel(self: *Self, ticks: f32, dir: dvui.enums.Direction, raw_delta: ?f32) std.mem.Allocator.Error!bool { + if (raw_delta) |h| determineMouseType(self, dir, h); self.positionMouseEventRemove(); diff --git a/src/backends/dx11.zig b/src/backends/dx11.zig index a60c5b77..623feeeb 100644 --- a/src/backends/dx11.zig +++ b/src/backends/dx11.zig @@ -1349,7 +1349,7 @@ pub fn wndProc( const float_delta: f32 = @floatFromInt(delta); const wheel_delta: f32 = @floatFromInt(win32.WHEEL_DELTA); const ticks = float_delta / wheel_delta * dvui.scroll_speed; - _ = stateFromHwnd(hwnd).dvui_window.addEventMouseWheelWithHint( + _ = stateFromHwnd(hwnd).dvui_window.addEventMouseWheel( switch (msg) { win32.WM_MOUSEWHEEL => ticks, win32.WM_MOUSEHWHEEL => -ticks, @@ -1360,7 +1360,7 @@ pub fn wndProc( win32.WM_MOUSEHWHEEL => .horizontal, else => unreachable, }, - @abs(float_delta), + float_delta, ) catch {}; return 0; }, diff --git a/src/backends/glfw.zig b/src/backends/glfw.zig index bb3b2010..6d1dc9da 100644 --- a/src/backends/glfw.zig +++ b/src/backends/glfw.zig @@ -520,14 +520,14 @@ fn handleScrollEvent(dvui_window: *dvui.Window, window: *zglfw.Window, xrel: f64 const ctx: *@This() = dvui_window.backend.impl; const scrollx: f32 = @floatCast(xrel * dvui.scroll_speed); const scrolly: f32 = @floatCast(yrel * dvui.scroll_speed); - const hint_x: ?f32 = if (xrel != 0.0) @abs(@as(f32, @floatCast(xrel))) else null; - const hint_y: ?f32 = if (yrel != 0.0) @abs(@as(f32, @floatCast(yrel))) else null; - const consumed_x = dvui_window.addEventMouseWheelWithHint(scrollx, .horizontal, hint_x) catch |err| { + const hint_x: ?f32 = if (xrel != 0.0) @as(f32, @floatCast(xrel)) else null; + const hint_y: ?f32 = if (yrel != 0.0) @as(f32, @floatCast(yrel)) else null; + const consumed_x = dvui_window.addEventMouseWheel(scrollx, .horizontal, hint_x) catch |err| { log.err("Encountered error when adding event! Err: {}", .{err}); if (ctx.userScrollCallback) |callback| callback(window, xrel, yrel); return; }; - const consumed_y = dvui_window.addEventMouseWheelWithHint(scrolly, .vertical, hint_y) catch |err| { + const consumed_y = dvui_window.addEventMouseWheel(scrolly, .vertical, hint_y) catch |err| { log.err("Encountered error when adding event! Err: {}", .{err}); if (ctx.userScrollCallback) |callback| callback(window, xrel, yrel); return; diff --git a/src/backends/mac_scroll_monitor.m b/src/backends/mac_scroll_monitor.m index 4267d12b..976fa100 100644 --- a/src/backends/mac_scroll_monitor.m +++ b/src/backends/mac_scroll_monitor.m @@ -13,8 +13,7 @@ * scroll event, so users who switch between a trackpad and a mouse mid-session get * accurate classification on the very next scroll. */ -static int g_has_value = 0; -static int g_is_precise = 0; +static int g_is_precise = -1; void dvui_mac_scroll_monitor_install(void) { static int installed = 0; @@ -23,7 +22,6 @@ void dvui_mac_scroll_monitor_install(void) { [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskScrollWheel handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) { g_is_precise = [event hasPreciseScrollingDeltas] ? 1 : 0; - g_has_value = 1; return event; }]; } @@ -31,6 +29,5 @@ void dvui_mac_scroll_monitor_install(void) { /* Returns -1 if no scroll event has been seen yet, 0 for classic mouse wheel, * 1 for trackpad / Magic Trackpad / Magic Mouse (any precise-deltas source). */ int dvui_mac_scroll_monitor_last_precise(void) { - if (!g_has_value) return -1; return g_is_precise; } diff --git a/src/backends/raylib-c.zig b/src/backends/raylib-c.zig index 5cbeddd1..d57e0028 100644 --- a/src/backends/raylib-c.zig +++ b/src/backends/raylib-c.zig @@ -671,14 +671,14 @@ pub fn addAllEvents(self: *RaylibBackend, win: *dvui.Window) !void { //scroll wheel movement const scroll_wheel = c.GetMouseWheelMoveV(); if (scroll_wheel.x != 0) { - if (try win.addEventMouseWheelWithHint(scroll_wheel.x * dvui.scroll_speed, .horizontal, @abs(scroll_wheel.x))) disable_raylib_input = true; + if (try win.addEventMouseWheel(scroll_wheel.x * dvui.scroll_speed, .horizontal, scroll_wheel.x)) disable_raylib_input = true; if (self.log_events) { std.debug.print("raylib event Mouse Wheel: {}\n", .{scroll_wheel}); } } if (scroll_wheel.y != 0) { - if (try win.addEventMouseWheelWithHint(scroll_wheel.y * dvui.scroll_speed, .vertical, @abs(scroll_wheel.y))) disable_raylib_input = true; + if (try win.addEventMouseWheel(scroll_wheel.y * dvui.scroll_speed, .vertical, scroll_wheel.y)) disable_raylib_input = true; if (self.log_events) { std.debug.print("raylib event Mouse Wheel: {}\n", .{scroll_wheel}); diff --git a/src/backends/raylib-zig.zig b/src/backends/raylib-zig.zig index 8c494bee..bff2896f 100644 --- a/src/backends/raylib-zig.zig +++ b/src/backends/raylib-zig.zig @@ -680,14 +680,14 @@ pub fn addAllEvents(self: *RaylibBackend, win: *dvui.Window) !void { //scroll wheel movement const scroll_wheel = raylib.getMouseWheelMoveV(); if (scroll_wheel.x != 0) { - if (try win.addEventMouseWheelWithHint(scroll_wheel.x * dvui.scroll_speed, .horizontal, @abs(scroll_wheel.x))) disable_raylib_input = true; + if (try win.addEventMouseWheel(scroll_wheel.x * dvui.scroll_speed, .horizontal, scroll_wheel.x)) disable_raylib_input = true; if (self.log_events) { std.debug.print("raylib event Mouse Wheel: {}\n", .{scroll_wheel}); } } if (scroll_wheel.y != 0) { - if (try win.addEventMouseWheelWithHint(scroll_wheel.y * dvui.scroll_speed, .vertical, @abs(scroll_wheel.y))) disable_raylib_input = true; + if (try win.addEventMouseWheel(scroll_wheel.y * dvui.scroll_speed, .vertical, scroll_wheel.y)) disable_raylib_input = true; if (self.log_events) { std.debug.print("raylib event Mouse Wheel: {}\n", .{scroll_wheel}); diff --git a/src/backends/sdl.zig b/src/backends/sdl.zig index 8853d263..0bab979a 100644 --- a/src/backends/sdl.zig +++ b/src/backends/sdl.zig @@ -1227,25 +1227,19 @@ pub fn addEvent(self: *SDLBackend, win: *dvui.Window, event: c.SDL_Event) !bool // On macOS we override the magnitude heuristic with the OS-side flag from // the NSEvent monitor (see init / mac_scroll_monitor.m). Updates per scroll // event so users switching between a trackpad and a mouse mid-session get - // the right classification immediately. Skip `addEventMouseWheelWithHint` - // on this branch so its heuristic doesn't clobber what we just set. + // the right classification immediately. const skip_heuristic = sdl3 and builtin.os.tag.isDarwin() and blk: { const v = dvui_mac_scroll_monitor_last_precise(); if (v < 0) break :blk false; - win.mouse_type_hint = if (v != 0) .trackpad else .mouse; + win.mouse_type = if (v != 0) .trackpad else .mouse; break :blk true; }; var ret = false; // sdl says x positive means to the right, where as y positive // means up, so we negate x so that down and right match - if (skip_heuristic) { - if (ticks_x != 0) ret = try win.addEventMouseWheel(-ticks_x * dvui.scroll_speed * mac_wheel_scale, .horizontal); - if (ticks_y != 0) ret = try win.addEventMouseWheel(ticks_y * dvui.scroll_speed * mac_wheel_scale, .vertical); - } else { - if (ticks_x != 0) ret = try win.addEventMouseWheelWithHint(-ticks_x * dvui.scroll_speed * mac_wheel_scale, .horizontal, @abs(ticks_x)); - if (ticks_y != 0) ret = try win.addEventMouseWheelWithHint(ticks_y * dvui.scroll_speed * mac_wheel_scale, .vertical, @abs(ticks_y)); - } + if (ticks_x != 0) ret = try win.addEventMouseWheel(-ticks_x * dvui.scroll_speed * mac_wheel_scale, .horizontal, if (skip_heuristic) null else ticks_x); + if (ticks_y != 0) ret = try win.addEventMouseWheel(ticks_y * dvui.scroll_speed * mac_wheel_scale, .vertical, if (skip_heuristic) null else ticks_y); return ret; }, if (sdl3) c.SDL_EVENT_FINGER_DOWN else c.SDL_FINGERDOWN => { diff --git a/src/backends/sdl3gpu.zig b/src/backends/sdl3gpu.zig index 789141e4..be119246 100644 --- a/src/backends/sdl3gpu.zig +++ b/src/backends/sdl3gpu.zig @@ -1585,8 +1585,8 @@ pub fn addEvent(self: *SDLBackend, win: *dvui.Window, event: c.SDL_Event) !bool var ret = false; // sdl says x positive means to the right, where as y positive // means up, so we negate x so that down and right match - if (ticks_x != 0) ret = try win.addEventMouseWheelWithHint(-ticks_x * dvui.scroll_speed, .horizontal, @abs(ticks_x)); - if (ticks_y != 0) ret = try win.addEventMouseWheelWithHint(ticks_y * dvui.scroll_speed, .vertical, @abs(ticks_y)); + if (ticks_x != 0) ret = try win.addEventMouseWheel(-ticks_x * dvui.scroll_speed, .horizontal, ticks_x); + if (ticks_y != 0) ret = try win.addEventMouseWheel(ticks_y * dvui.scroll_speed, .vertical, ticks_y); return ret; }, c.SDL_EVENT_FINGER_DOWN => { diff --git a/src/backends/web.zig b/src/backends/web.zig index 9efbc91c..b60d0b31 100644 --- a/src/backends/web.zig +++ b/src/backends/web.zig @@ -281,7 +281,7 @@ fn add_event_raw(w: *dvui.Window, which: u8, int1: u32, int2: u32, float1: f32, 1 => _ = try w.addEventMouseMotion(.{ .pt = .{ .x = float1, .y = float2 } }), 2 => _ = try w.addEventMouseButton(buttonFromJS(int1), .press), 3 => _ = try w.addEventMouseButton(buttonFromJS(int1), .release), - 4 => _ = try w.addEventMouseWheelWithHint(float1 * dvui.scroll_speed, if (int1 > 0) .vertical else .horizontal, if (int2 != 0) float2 else null), + 4 => _ = try w.addEventMouseWheel(float1 * dvui.scroll_speed, if (int1 > 0) .vertical else .horizontal, if (int2 != 0) float2 else null), 5 => { const str = @as([*]u8, @ptrFromInt(int1))[0..int2]; _ = try w.addEventKey(.{ diff --git a/src/backends/wio.zig b/src/backends/wio.zig index 80cf12e5..8a25d757 100644 --- a/src/backends/wio.zig +++ b/src/backends/wio.zig @@ -210,14 +210,8 @@ pub fn addEvent(self: *@This(), win: *dvui.Window, event: wio.Event) !bool { const scale = self.pixelSize().w / self.windowSize().w; return try win.addEventMouseMotion(.{ .pt = .{ .x = x * scale, .y = y * scale } }); }, - .scroll_vertical => |ticks| { - const t: f32 = @floatFromInt(ticks); - return try win.addEventMouseWheelWithHint(-t * dvui.scroll_speed, .vertical, @abs(t)); - }, - .scroll_horizontal => |ticks| { - const t: f32 = @floatFromInt(ticks); - return try win.addEventMouseWheelWithHint(-t * dvui.scroll_speed, .horizontal, @abs(t)); - }, + .scroll_vertical => |ticks| return try win.addEventMouseWheel(-ticks * dvui.scroll_speed, .vertical, ticks), + .scroll_horizontal => |ticks| return try win.addEventMouseWheel(-ticks * dvui.scroll_speed, .horizontal, ticks), .touch => |touch| { const button = touchIdToDvuiButton(touch.id) orelse return false; const xnorm = @as(f32, @floatFromInt(touch.x)) / self.size_natural.w; diff --git a/src/dvui.zig b/src/dvui.zig index 9f9de548..4921e235 100644 --- a/src/dvui.zig +++ b/src/dvui.zig @@ -401,11 +401,16 @@ pub fn currentWindow() *Window { return current_window orelse @panic("current_window was null, most dvui functions must be between Window.begin/end"); } -/// Rolling best-effort pointer guess from wheel delta shapes (smooth vs discrete). -/// Meaningful after backends pass raw delta magnitudes with `Window.addEventMouseWheelWithHint`. +/// Guess which type of pointing device is being used (touchpad vs. mouse). +/// Meaningful after a mouse wheel event when backends pass raw deltas to +/// `Window.addEventMouseWheel`. +/// +/// Some backends can detect a switch between touchpad and mouse instantly, +/// others require a 1 second gap. +/// /// Only valid between `Window.begin` and `Window.end`. -pub fn getMouseTypeHint() enums.MouseTypeHint { - return currentWindow().mouseTypeHint(); +pub fn mouseType() enums.MouseType { + return currentWindow().mouse_type; } /// Allocates space for a widget to the alloc stack, or the arena diff --git a/src/enums.zig b/src/enums.zig index fdb661e9..00e7b3a6 100644 --- a/src/enums.zig +++ b/src/enums.zig @@ -11,9 +11,10 @@ pub const DialogButtonOrder = enum { ok_cancel, }; -/// Best-effort guess from scroll deltas (smooth trackpad-style vs discrete wheel ticks). -/// Backend code should call `Window.addEventMouseWheelWithHint`; see `dvui.getMouseTypeHint`. -pub const MouseTypeHint = enum { +/// Set by backend through `Window.addEventMouseWheel`. Usually a best-effort +/// guess from scroll deltas (smooth trackpad-style vs discrete wheel ticks). +/// See `dvui.mouseType`. +pub const MouseType = enum { unknown, /// Classic mouse wheel-style quantization (within the accuracy of OS-reported deltas). mouse,