diff options
Diffstat (limited to 'src/gui/gui.zig')
| -rw-r--r-- | src/gui/gui.zig | 182 |
1 files changed, 151 insertions, 31 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 4e1f76dc..60cc6112 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -15,6 +15,7 @@ const message = pardes.Messages.Message; const file_watch = @import("../file_watch.zig"); const deck = @import("deck.zig"); const pet = @import("pet.zig"); +const Trackpad = @import("Trackpad.zig"); const JumpTrail = @import("../jump_trail.zig"); const Art = @import("../Art.zig"); const fonts = @import("../fonts.zig"); // the fallback faces @@ -576,6 +577,8 @@ const Gui = struct { words: Words = .{}, touch: Touch = .{}, + /// A Mac trackpad's gestures (macos_trackpad.m decodes them). + trackpad: Trackpad = .{}, /// The workspace tag's pet (Pet), on the window's own clock. pet_state: pet.State = .{}, /// The cursor's jump trail (JumpTrail), on the window's own clock. @@ -613,6 +616,8 @@ const Gui = struct { /// A touchpad's fraction toward the next turn sent to a program that /// owns the wheel (terminal.ownsWheel). program_wheel: f32 = 0, + /// A trackpad's horizontal fraction toward the next turn (wheelAt). + program_wheel_x: f32 = 0, glide: Glide = .{}, /// PARDES_FRAMES_DEMO: the timedemo (its script's steps), its next step /// and when it started; null when none runs. @@ -942,6 +947,7 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di } _ = c.SDL_SetHint("SDL_JOYSTICK_HIDAPI", "1"); // SDL_HINT_JOYSTICK_HIDAPI _ = c.SDL_SetHint("SDL_JOYSTICK_HIDAPI_STEAMDECK", "1"); // ..._STEAMDECK + if (comptime builtin.os.tag == .macos) _ = c.SDL_SetHint(c.SDL_HINT_MAC_SCROLL_MOMENTUM, "1"); if (!c.SDL_Init(c.SDL_INIT_VIDEO | c.SDL_INIT_GAMEPAD)) { log.err("SDL_Init: {s}", .{c.SDL_GetError()}); return error.SdlInit; @@ -987,6 +993,7 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di return error.SdlInit; }; var window_slot: ?*c.SDL_Window = window; + if (comptime builtin.os.tag == .macos) c.ui_macos_trackpad_attach(window); defer if (window_slot) |open| c.SDL_DestroyWindow(open); if (want_at) |at| _ = c.SDL_SetWindowPosition(window, at[0], at[1]); updateDisplayScale(window); @@ -1228,6 +1235,7 @@ fn closeWindow(g: *Gui) void { /// size the last one had. False when the display will not give one. fn reopenWindow(g: *Gui) bool { const window = c.SDL_CreateWindow("pardes", g.window_w, g.window_h, g.win_flags) orelse return false; + if (comptime builtin.os.tag == .macos) c.ui_macos_trackpad_attach(window); if (g.soft_present) { g.soft_renderer = c.SDL_CreateRenderer(window, null) orelse { c.SDL_DestroyWindow(window); @@ -2022,6 +2030,12 @@ const Input = struct { }; fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { + if (comptime builtin.os.tag == .macos) { + if (sev.type == c.ui_macos_trackpad_event_type()) { + dispatchTrackpad(g, in, @ptrCast(@alignCast(sev))); + return; + } + } // An input's time, for its latency to the frame that answers it // (FrameStats): SDL's stamp, moved onto the monotonic clock. switch (sev.type) { @@ -2066,21 +2080,7 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { c.SDLK_MINUS, c.SDLK_UNDERSCORE, c.SDLK_KP_MINUS => -font_px_step, else => 0, }; - if (step != 0) { - const want = std.math.clamp(g.px + step, font_px_min, font_px_max); - if (want != g.px) { - g.px = want; - // A client's view is sized by its next loop, as its input. - if (in.remote) |r| { - refitFont(g, null); - r.link.dirty = true; - } else { - refitFont(g, in.core); - observeGuiFont(g, in.core); - } - } - return; - } + if (step != 0) return setFontPx(g, in, std.math.clamp(g.px + step, font_px_min, font_px_max)); keyDown(in, sev.key.key, sev.key.mod); }, c.SDL_EVENT_KEY_UP => { @@ -2102,7 +2102,11 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { }, c.SDL_EVENT_MOUSE_BUTTON_DOWN, c.SDL_EVENT_MOUSE_BUTTON_UP => { const b = sev.button; - const button: pardes.Mouse.Button = switch (b.button) { + const raw_button = if (comptime builtin.os.tag == .macos) + (if (b.down) g.trackpad.press(b.button) else g.trackpad.release(b.button) orelse return) + else + b.button; + const button: pardes.Mouse.Button = switch (raw_button) { 1 => .left, 2 => .middle, 3 => .right, @@ -2120,7 +2124,7 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { }, c.SDL_EVENT_MOUSE_MOTION => { const m = sev.motion; - const held: ?pardes.Mouse.Button = if ((m.state & c.SDL_BUTTON_LMASK) != 0) + const physical_held: ?pardes.Mouse.Button = if ((m.state & c.SDL_BUTTON_LMASK) != 0) .left else if ((m.state & c.SDL_BUTTON_MMASK) != 0) .middle @@ -2128,6 +2132,16 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { .right else null; + // A trackpad's latched button (Trackpad.press), not SDL's. + const held: ?pardes.Mouse.Button = if (comptime builtin.os.tag == .macos) + (if (g.trackpad.forced) null else if (g.trackpad.button) |b| switch (b) { + 1 => .left, + 2 => .middle, + 3 => .right, + else => physical_held, + } else physical_held) + else + physical_held; const mc = pointAt(g, in.core, m.x, m.y); in.post(.{ .mouse = pointerMouse(held orelse .none, if (held != null) .drag else .motion, mc, pointerMods()) }); }, @@ -2139,6 +2153,9 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { }, c.SDL_EVENT_FINGER_DOWN, c.SDL_EVENT_FINGER_MOTION, c.SDL_EVENT_FINGER_UP, c.SDL_EVENT_FINGER_CANCELED => { const f = sev.tfinger; + // Indirect Mac touches are decoded by AppKit at the pointer, + // never as absolute positions across the window. + if (comptime builtin.os.tag == .macos) if (f.touchID == c.SDL_MOUSE_TOUCHID) return; const finger: Finger = .{ .kind = switch (sev.type) { c.SDL_EVENT_FINGER_DOWN => .down, @@ -2159,6 +2176,8 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { const fch: f32 = @floatFromInt(g.cell_h); handleFinger(&g.touch, in, finger, win_w, win_h, fcw, fch, @floatFromInt(g.tagline_width)); }, + // The window's font size; a Linux touchpad's pinch stays unused. + c.SDL_EVENT_PINCH_BEGIN, c.SDL_EVENT_PINCH_UPDATE => if (comptime builtin.os.tag == .macos) setFontPx(g, in, Trackpad.pinch(g.px, sev.pinch.scale)), c.SDL_EVENT_GAMEPAD_ADDED => { if (g.gamepad == null) g.gamepad = c.SDL_OpenGamepad(sev.gdevice.which); }, @@ -2391,20 +2410,91 @@ fn wheelAt(g: *Gui, in: *Input, w: WheelTurn, mc: MouseCell) void { } } } - if (w.x != 0) in.post(.{ .mouse = .{ - .button = if (w.x > 0) .wheel_right else .wheel_left, - .kind = .press, - .col = mc.col, - .row = mc.row, - .tag_hit = mc.tag_hit, - .body_hit = mc.body_hit, - .ctrl = w.mods.ctrl, - .shift = w.mods.shift, - .alt = w.mods.alt, - } }); + var horizontal: i32 = if (w.x == 0 or !std.math.isFinite(w.x)) 0 else if (w.x > 0) 1 else -1; + if (w.precise and std.math.isFinite(w.x)) { + g.program_wheel_x += std.math.clamp(w.x, -64, 64); + horizontal = @intFromFloat(@trunc(g.program_wheel_x)); + g.program_wheel_x -= @floatFromInt(horizontal); + } + while (horizontal != 0) { + in.post(.{ .mouse = .{ + .button = if (horizontal > 0) .wheel_right else .wheel_left, + .kind = .press, + .col = mc.col, + .row = mc.row, + .tag_hit = mc.tag_hit, + .body_hit = mc.body_hit, + .ctrl = w.mods.ctrl, + .shift = w.mods.shift, + .alt = w.mods.alt, + } }); + horizontal += if (horizontal > 0) -1 else 1; + } } -const WheelTurn = struct { x: f32 = 0, y: f32 = 0, integer_y: i32 = 0, mods: PointerMods = .{} }; +/// `precise`: a Mac trackpad's columns (dispatchTrackpad), whose +/// horizontal fraction is kept toward the next turn. +const WheelTurn = struct { x: f32 = 0, y: f32 = 0, integer_y: i32 = 0, precise: bool = false, mods: PointerMods = .{} }; + +/// The window's font at `want` pixels: a key's step, a pinch. A client's +/// view is sized by its next loop, as its input. +fn setFontPx(g: *Gui, in: *Input, want: f32) void { + if (want == g.px) return; + g.px = want; + if (in.remote) |r| { + refitFont(g, null); + r.link.dirty = true; + } else { + refitFont(g, in.core); + observeGuiFont(g, in.core); + } +} + +/// A Mac trackpad's gesture, decoded by macos_trackpad.m (Trackpad.zig's +/// policy): fingers, a pinch's zoom, a swipe's Back/Forward, a rotation's +/// n/N, a force click's Look, and a two-finger scroll by the pixel. +fn dispatchTrackpad(g: *Gui, in: *Input, ev: *const c.UITrackpadEvent) void { + g.input_seen = true; + switch (ev.kind) { + c.UI_TRACKPAD_FINGERS => g.trackpad.countFingers(@intCast(@max(ev.fingers, 0))), + c.UI_TRACKPAD_PINCH => if (g.trackpad.magnify(g.px, ev.a, ev.phase)) |want| setFontPx(g, in, want), + c.UI_TRACKPAD_HISTORY => { + if (ev.a == 0 or !std.math.isFinite(ev.a)) return; + const back = ev.a < 0; + const jumps = &in.core.view().jumps; + const can_jump = jumps.n > 0 and (if (back) jumps.cur > 0 else jumps.cur + 1 < jumps.n); + in.post(.{ .command = if (back) "Back" else "Forward" }); + if (comptime builtin.os.tag == .macos) if (can_jump) c.ui_macos_trackpad_haptic(); + }, + c.UI_TRACKPAD_ROTATE => { + const pane = in.core.panes[in.core.view().active] orelse return; + // Search-step keys never become text in insert/raw mode. + if (pane.body.mode != .normal) return; + var ticks = g.trackpad.rotate(ev.a, ev.phase); + while (ticks != 0) { + in.post(.{ .key = .{ .cp = if (ticks > 0) 'N' else 'n' } }); + if (comptime builtin.os.tag == .macos) c.ui_macos_trackpad_haptic(); + ticks += if (ticks > 0) -1 else 1; + } + }, + c.UI_TRACKPAD_PRESSURE => { + const previous = g.trackpad.force(ev.a) orelse return; + const mc = mouseCell(g, in.core, ev.x, ev.y); + in.post(.{ .mouse = pointerMouse(if (previous == 2) .middle else .left, .release, mc, pointerMods()) }); + in.post(.{ .mouse = pointerMouse(.right, .press, mc, pointerMods()) }); + in.post(.{ .mouse = pointerMouse(.right, .release, mc, pointerMods()) }); + }, + c.UI_TRACKPAD_SCROLL => { + const mc = pointAt(g, in.core, ev.x, ev.y); + // AppKit reports points; the grid is measured in backing pixels. + const geometry = windowGeometry(g.window); + const rows = ev.b * geometry.pixel_h / geometry.window_h / @as(f32, @floatFromInt(g.cell_h)); + const cols = -ev.a * geometry.pixel_w / geometry.window_w / @as(f32, @floatFromInt(g.cell_w)); + wheelAt(g, in, .{ .x = cols, .y = rows, .precise = true, .mods = pointerMods() }, mc); + }, + else => {}, + } +} /// The modifiers held with a pointer event, as the core's Mouse carries them. const PointerMods = struct { ctrl: bool = false, shift: bool = false, alt: bool = false, super: bool = false }; @@ -2652,10 +2742,11 @@ const Shell = struct { fn split(s: *Shell, sev: *const c.SDL_Event) bool { if (sev.type != c.SDL_EVENT_USER) s.mail_only = false; if (!s.peers.recording) return false; - switch (sev.type) { + const native_gesture = if (comptime builtin.os.tag == .macos) sev.type == c.ui_macos_trackpad_event_type() else false; + if (!native_gesture) switch (sev.type) { c.SDL_EVENT_KEY_DOWN, c.SDL_EVENT_TEXT_INPUT, c.SDL_EVENT_MOUSE_BUTTON_DOWN, c.SDL_EVENT_MOUSE_BUTTON_UP, c.SDL_EVENT_MOUSE_MOTION, c.SDL_EVENT_MOUSE_WHEEL, c.SDL_EVENT_WINDOW_FOCUS_GAINED, c.SDL_EVENT_WINDOW_FOCUS_LOST, c.SDL_EVENT_WINDOW_MOUSE_LEAVE, c.SDL_EVENT_FINGER_DOWN, c.SDL_EVENT_FINGER_MOTION, c.SDL_EVENT_FINGER_UP => {}, else => return false, - } + }; if (s.took_input) { s.held = sev.*; return true; @@ -8858,3 +8949,32 @@ test "compact tag layers fill available width and clip at their physical edge" { try std.testing.expectApproxEqAbs(@as(f32, 0), instances[4].x1, 0.0001); try std.testing.expectApproxEqAbs(@as(f32, 6) / atlas_w, instances[4].u1 - instances[4].u0, 0.0001); } + +test "trackpad rotation never types search keys into an insert-mode file or raw terminal" { + const gpa = std.testing.allocator; + const core = try pardes.Pardes.init(gpa, .{ .cols = 60, .rows = 16 }); + defer core.deinit(); + _ = try core.setTestFile("unchanged\n"); + var client: Client = .{ .link = undefined, .io = std.testing.io, .gpa = gpa, .texts = .init(gpa) }; + defer client.deinit(); + var in: Input = .{ .core = core, .remote = &client }; + var g: Gui = undefined; + g.trackpad = .{}; + var ev: c.UITrackpadEvent = std.mem.zeroes(c.UITrackpadEvent); + ev.kind = c.UI_TRACKPAD_ROTATE; + ev.a = 20; + const pane = core.panes[core.view().active].?; + pane.body.mode = .insert; + dispatchTrackpad(&g, &in, &ev); + pane.body.mode = .tty; + dispatchTrackpad(&g, &in, &ev); + try std.testing.expectEqual(@as(usize, 0), client.pending.items.len); + ev.kind = c.UI_TRACKPAD_HISTORY; + ev.a = -1; + dispatchTrackpad(&g, &in, &ev); + ev.a = 1; + dispatchTrackpad(&g, &in, &ev); + try std.testing.expectEqual(@as(usize, 2), client.pending.items.len); + try std.testing.expectEqualStrings("Back", client.pending.items[0].command); + try std.testing.expectEqualStrings("Forward", client.pending.items[1].command); +} |
