summaryrefslogtreecommitdiff
path: root/src/gui/Trackpad.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/Trackpad.zig')
-rw-r--r--src/gui/Trackpad.zig142
1 files changed, 0 insertions, 142 deletions
diff --git a/src/gui/Trackpad.zig b/src/gui/Trackpad.zig
deleted file mode 100644
index ef28d211..00000000
--- a/src/gui/Trackpad.zig
+++ /dev/null
@@ -1,142 +0,0 @@
-//! Mac trackpad policy, after AppKit decoding. Buttons use SDL's 1/2/3.
-const std = @import("std");
-const Trackpad = @This();
-
-fingers: usize = 0,
-stream: ?u8 = null,
-button: ?u8 = null,
-forced: bool = false,
-rotation: f32 = 0,
-rotation_total: f32 = 0,
-gesture: enum { none, pinch, rotate } = .none,
-pinch_base: f32 = 0,
-pinch_total: f32 = 1,
-
-pub fn countFingers(t: *Trackpad, count: usize) void {
- if (count < 2 or t.fingers < 2) {
- t.gesture = .none;
- t.rotation_total = 0;
- t.pinch_base = 0;
- t.pinch_total = 1;
- }
- t.fingers = count;
-}
-
-pub fn press(t: *Trackpad, stream: u8) u8 {
- if (t.fingers == 0 or stream > 3) return stream;
- t.stream = stream;
- t.button = if (t.fingers >= 2) 2 else stream;
- t.forced = false;
- return t.button.?;
-}
-
-pub fn release(t: *Trackpad, stream: u8) ?u8 {
- if (t.stream != stream) return stream;
- defer {
- t.stream = null;
- t.button = null;
- t.forced = false;
- }
- return if (t.forced) null else t.button;
-}
-
-/// The caller releases this button before issuing one Look click. The
-/// physical release afterwards is swallowed, so it cannot become a chord.
-pub fn force(t: *Trackpad, stage: f32) ?u8 {
- if (stage < 2 or t.forced or t.button == null or t.button == 3) return null;
- t.forced = true;
- return t.button;
-}
-
-pub fn rotate(t: *Trackpad, degrees: f32, phase: u32) i32 {
- if (phase == 1 or phase == 3) {
- t.rotation = 0;
- t.rotation_total = 0;
- }
- if (phase == 3 or !std.math.isFinite(degrees)) return 0;
- if (t.gesture == .pinch) return 0;
- t.rotation_total += std.math.clamp(degrees, -640, 640);
- if (@abs(t.rotation_total) >= 3) t.gesture = .rotate;
- const next = t.rotation + std.math.clamp(degrees, -640, 640);
- const ticks: i32 = @intFromFloat(@trunc(next / 10));
- t.rotation = next - @as(f32, @floatFromInt(ticks)) * 10;
- return ticks;
-}
-
-/// Ignore small magnification noise, especially while twisting. Once a
-/// deliberate pinch wins, half the logarithmic gain makes fine zoom easier.
-pub fn magnify(t: *Trackpad, size: f32, scale: f32, phase: u32) ?f32 {
- if (phase == 1 or t.pinch_base == 0) {
- t.pinch_base = size;
- t.pinch_total = 1;
- }
- if (phase == 3) {
- t.pinch_base = 0;
- if (t.gesture == .pinch) t.gesture = .none;
- return null;
- }
- if (t.gesture == .rotate or !std.math.isFinite(scale) or scale <= 0) return null;
- t.pinch_total = std.math.clamp(t.pinch_total * scale, 0.01, 100);
- if (t.gesture == .none) {
- if (t.pinch_total > 1 / @as(f32, 1.1) and t.pinch_total < 1.1) return null;
- t.gesture = .pinch;
- }
- return std.math.clamp(t.pinch_base * @sqrt(t.pinch_total), 8, 72);
-}
-
-pub fn pinch(size: f32, scale: f32) f32 {
- if (!std.math.isFinite(scale) or scale <= 0) return size;
- return std.math.clamp(size * scale, 8, 72);
-}
-
-test "Mac trackpad multi-finger buttons stay latched after the fingers lift" {
- var t: Trackpad = .{};
- try std.testing.expectEqual(@as(u8, 3), t.press(3)); // real mouse
- try std.testing.expectEqual(@as(?u8, 3), t.release(3));
- for ([_]usize{ 2, 3 }) |n| {
- t.fingers = n;
- try std.testing.expectEqual(@as(u8, 2), t.press(3));
- t.fingers = 0;
- try std.testing.expectEqual(@as(?u8, 2), t.release(3));
- }
-}
-
-test "Mac trackpad force click upgrades Exec once without a paste chord or stray release" {
- var t: Trackpad = .{ .fingers = 2 };
- try std.testing.expectEqual(@as(u8, 2), t.press(3));
- try std.testing.expect(t.force(1) == null);
- try std.testing.expectEqual(@as(?u8, 2), t.force(2));
- try std.testing.expect(t.force(2) == null);
- try std.testing.expect(t.release(3) == null);
- t.fingers = 1;
- try std.testing.expectEqual(@as(u8, 1), t.press(1));
- try std.testing.expectEqual(@as(?u8, 1), t.release(1));
-}
-
-test "Mac trackpad rotate and pinch reject invalid input and reset cancelled gestures" {
- var t: Trackpad = .{};
- try std.testing.expectEqual(@as(i32, 0), t.rotate(7, 1));
- try std.testing.expectEqual(@as(i32, 1), t.rotate(5, 0));
- try std.testing.expectEqual(@as(i32, -1), t.rotate(-12, 0));
- try std.testing.expectEqual(@as(i32, 0), t.rotate(9, 3));
- try std.testing.expectEqual(@as(f32, 0), t.rotation);
- try std.testing.expectEqual(@as(f32, 18), pinch(18, 1));
- try std.testing.expectEqual(@as(f32, 36), pinch(18, 2));
- try std.testing.expectEqual(@as(f32, 72), pinch(18, 100));
- try std.testing.expectEqual(@as(f32, 18), pinch(18, std.math.nan(f32)));
-}
-
-test "trackpad pinch ignores twist noise and uses gentler deliberate zoom" {
- var t: Trackpad = .{};
- t.countFingers(2);
- try std.testing.expect(t.magnify(18, 1, 1) == null);
- for (0..5) |_| try std.testing.expect(t.magnify(18, 1.01, 0) == null);
- _ = t.rotate(7, 1);
- try std.testing.expect(t.magnify(18, 1.3, 0) == null);
- t.countFingers(0);
- t.countFingers(2);
- const zoom = t.magnify(18, 1.2, 1) orelse return error.PinchNotRecognized;
- try std.testing.expect(zoom > 18 and zoom < 20.5);
- try std.testing.expectEqual(@as(i32, 0), t.rotate(30, 1));
- try std.testing.expect(t.magnify(zoom, 1, 3) == null);
-}