From 66078493e6f893b8fc0a8da5adaec313bd532163 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 22 Nov 2022 11:07:25 -0800 Subject: [PATCH] mac: get default click repeat interval from NSEvent --- src/config.zig | 3 ++- src/os/main.zig | 1 + src/os/mouse.zig | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/os/mouse.zig diff --git a/src/config.zig b/src/config.zig index 633d328e4..0029878ad 100644 --- a/src/config.zig +++ b/src/config.zig @@ -5,6 +5,7 @@ const ArenaAllocator = std.heap.ArenaAllocator; const inputpkg = @import("input.zig"); const passwd = @import("passwd.zig"); const terminal = @import("terminal/main.zig"); +const internal_os = @import("os/main.zig"); const log = std.log.scoped(.config); @@ -327,7 +328,7 @@ pub const Config = struct { // Default our click interval if (self.@"click-repeat-interval" == 0) { - self.@"click-repeat-interval" = 500; + self.@"click-repeat-interval" = internal_os.clickInterval() orelse 500; } } }; diff --git a/src/os/main.zig b/src/os/main.zig index 38f90133f..132764dd7 100644 --- a/src/os/main.zig +++ b/src/os/main.zig @@ -4,3 +4,4 @@ pub usingnamespace @import("file.zig"); pub usingnamespace @import("locale.zig"); pub usingnamespace @import("macos_version.zig"); +pub usingnamespace @import("mouse.zig"); diff --git a/src/os/mouse.zig b/src/os/mouse.zig new file mode 100644 index 000000000..cff78d4dc --- /dev/null +++ b/src/os/mouse.zig @@ -0,0 +1,24 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const assert = std.debug.assert; +const objc = @import("objc"); + +const log = std.log.scoped(.os); + +/// The system-configured double-click interval if its available. +pub fn clickInterval() ?u32 { + // On macOS, we can ask the system. + if (comptime builtin.target.isDarwin()) { + const NSEvent = objc.Class.getClass("NSEvent") orelse { + log.err("NSEvent class not found. Can't get click interval.", .{}); + return null; + }; + + // Get the interval and convert to ms + const interval = NSEvent.msgSend(f64, objc.sel("doubleClickInterval"), .{}); + const ms = @floatToInt(u32, @ceil(interval * 1000)); + return ms; + } + + return null; +}