Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

feat: add trackpad swipe navigation to viewer apps

Petar Margetićf67d829b8274
diff --git a/src/files/Client.zig b/src/files/Client.zig
index 4e2461a..7bb2711 100644
--- a/src/files/Client.zig
+++ b/src/files/Client.zig
@@ -57,6 +57,7 @@ drag_source: ?*ClipboardSource = null,
drag_serial: u32 = 0,
drag_icon: ?DragIcon = null,
axis_source: wl.Pointer.AxisSource = .wheel,
+finger_navigation: @import("ui").trackpad_navigation.Gesture = .{},
axis_delta: f64 = 0,
horizontal_delta: f64 = 0,
horizontal_notches: ?f64 = null,
diff --git a/src/files/client_input.zig b/src/files/client_input.zig
index abae88d..ccc1ab6 100644
--- a/src/files/client_input.zig
+++ b/src/files/client_input.zig
@@ -25,6 +25,7 @@ pub fn pointerEvent(_: *wl.Pointer, event: wl.Pointer.Event, self: *Client) void
},
.leave => {
self.inside = false;
+ self.finger_navigation.reset();
self.app.text_dragging = false;
self.app.cancelDrag();
self.app.handleMotion(-1, -1);
@@ -38,12 +39,42 @@ pub fn pointerEvent(_: *wl.Pointer, event: wl.Pointer.Event, self: *Client) void
self.handlePointerButton(ev.button, ev.state == .pressed);
},
.axis => |ev| {
- if (ev.axis == .vertical_scroll) {
+ if (self.axis_source == .finger) {
+ self.finger_navigation.add(
+ ev.axis == .horizontal_scroll,
+ ev.value.toDouble(),
+ );
+ if (ev.axis == .vertical_scroll) self.axis_delta += ev.value.toDouble();
+ } else if (ev.axis == .vertical_scroll) {
self.axis_delta += ev.value.toDouble();
- } else self.horizontal_delta += ev.value.toDouble();
+ } else {
+ self.horizontal_delta += ev.value.toDouble();
+ }
if (self.pointer.?.getVersion() < 5) self.flushScroll();
},
- .axis_source => |ev| self.axis_source = ev.axis_source,
+ .axis_source => |ev| {
+ if (self.axis_source != ev.axis_source) self.finger_navigation.reset();
+ self.axis_source = ev.axis_source;
+ },
+ .axis_stop => |ev| {
+ if (self.axis_source == .finger) {
+ if (ev.axis == .horizontal_scroll) {
+ self.flushScroll();
+ if (self.finger_navigation.stop(true)) |gesture| {
+ if (gesture.direction) |direction| {
+ switch (direction) {
+ .back => self.app.back(),
+ .forward => self.app.forward(),
+ }
+ } else {
+ self.app.handleHorizontalScroll(gesture.horizontal_delta);
+ }
+ }
+ } else {
+ _ = self.finger_navigation.stop(false);
+ }
+ }
+ },
.axis_discrete => |ev| {
if (ev.axis == .vertical_scroll) self.axis_notches = (self.axis_notches orelse 0) + @as(f64, @floatFromInt(ev.discrete)) else self.horizontal_notches = (self.horizontal_notches orelse 0) + @as(f64, @floatFromInt(ev.discrete));
},
@@ -51,12 +82,13 @@ pub fn pointerEvent(_: *wl.Pointer, event: wl.Pointer.Event, self: *Client) void
if (ev.axis == .vertical_scroll) self.axis_notches = (self.axis_notches orelse 0) + @as(f64, @floatFromInt(ev.value120)) / 120.0 else self.horizontal_notches = (self.horizontal_notches orelse 0) + @as(f64, @floatFromInt(ev.value120)) / 120.0;
},
.frame => self.flushScroll(),
- else => {},
}
}
pub fn flushScroll(self: *Client) void {
- if (self.axis_source == .wheel or self.axis_source == .wheel_tilt) {
+ if (self.axis_source == .finger) {
+ if (self.axis_delta != 0) self.app.handleScroll(self.axis_delta);
+ } else if (self.axis_source == .wheel or self.axis_source == .wheel_tilt) {
const pixels_per_notch = 100.0;
const pixels = if (self.axis_notches) |n| n * pixels_per_notch else self.axis_delta * (pixels_per_notch / 15.0);
self.app.handleWheel(pixels, app_mod.nowMs());
diff --git a/src/images/main.zig b/src/images/main.zig
index d27376b..3f5daa6 100644
--- a/src/images/main.zig
+++ b/src/images/main.zig
@@ -55,6 +55,8 @@ pub const Client = struct {
density: f64 = 1,
seat: ?*wl.Seat = null,
pointer: ?*wl.Pointer = null,
+ axis_source: wl.Pointer.AxisSource = .wheel,
+ finger_navigation: @import("ui").trackpad_navigation.Gesture = .{},
keyboard: ?*wl.Keyboard = null,
input_serial: u32 = 0,
wm_base: ?*xdg.WmBase = null,
@@ -370,6 +372,7 @@ pub const Client = struct {
},
.leave => {
self.inside = false;
+ self.finger_navigation.reset();
self.app.cancelDrag();
},
.motion => |ev| {
@@ -379,11 +382,32 @@ pub const Client = struct {
self.input_serial = ev.serial;
self.handlePointerButton(ev.button, ev.state == .pressed);
},
+ .axis_source => |ev| {
+ if (self.axis_source != ev.axis_source) self.finger_navigation.reset();
+ self.axis_source = ev.axis_source;
+ },
.axis => |ev| {
- if (ev.axis == .vertical_scroll or self.app.overFilmstrip()) {
+ if (self.axis_source == .finger and !self.app.overFilmstrip()) {
+ self.finger_navigation.add(
+ ev.axis == .horizontal_scroll,
+ ev.value.toDouble(),
+ );
+ if (ev.axis == .vertical_scroll) {
+ self.app.handleScroll(ev.value.toDouble());
+ }
+ } else if (ev.axis == .vertical_scroll or self.app.overFilmstrip()) {
self.app.handleScroll(ev.value.toDouble());
}
},
+ .axis_stop => |ev| {
+ if (self.axis_source == .finger) {
+ if (self.finger_navigation.stop(ev.axis == .horizontal_scroll)) |gesture| {
+ if (gesture.direction) |direction| {
+ self.app.perform(if (direction == .back) .previous else .next);
+ }
+ }
+ }
+ },
else => {},
}
}
diff --git a/src/pdf/client_input.zig b/src/pdf/client_input.zig
index eb3c3e5..6560719 100644
--- a/src/pdf/client_input.zig
+++ b/src/pdf/client_input.zig
@@ -20,6 +20,7 @@ pub fn pointerEvent(_: *wl.Pointer, event: wl.Pointer.Event, self: *Client) void
},
.leave => {
self.inside = false;
+ self.finger_navigation.reset();
self.app.cancelDrag();
},
.motion => |ev| {
@@ -29,17 +30,45 @@ pub fn pointerEvent(_: *wl.Pointer, event: wl.Pointer.Event, self: *Client) void
self.input_serial = ev.serial;
self.handlePointerButton(ev.button, ev.state == .pressed);
},
+ .axis_source => |ev| {
+ if (self.axis_source != ev.axis_source) self.finger_navigation.reset();
+ self.axis_source = ev.axis_source;
+ },
.axis => |ev| {
- if (ev.axis == .vertical_scroll) {
+ const delta = ev.value.toDouble();
+ if (self.axis_source == .finger and !self.app.ctrl and !self.app.shift) {
+ self.finger_navigation.add(ev.axis == .horizontal_scroll, delta);
+ if (ev.axis == .vertical_scroll) self.app.handleScroll(delta);
+ } else if (ev.axis == .vertical_scroll) {
if (self.app.ctrl) {
- self.app.handleZoomScroll(ev.value.toDouble());
+ self.app.handleZoomScroll(delta);
} else if (self.app.shift) {
- self.app.handleHorizontalScroll(ev.value.toDouble());
+ self.app.handleHorizontalScroll(delta);
} else {
- self.app.handleScroll(ev.value.toDouble());
+ self.app.handleScroll(delta);
}
} else if (ev.axis == .horizontal_scroll) {
- self.app.handleHorizontalScroll(ev.value.toDouble());
+ self.app.handleHorizontalScroll(delta);
+ }
+ },
+ .axis_stop => |ev| {
+ if (self.axis_source == .finger) {
+ if (self.finger_navigation.stop(ev.axis == .horizontal_scroll)) |gesture| {
+ if (self.app.dialog == .none) {
+ if (gesture.direction) |direction| {
+ if (self.app.n_pages > 0) {
+ switch (direction) {
+ .back => if (self.app.current_page > 0)
+ self.app.goToPage(self.app.current_page - 1),
+ .forward => if (self.app.current_page < self.app.n_pages - 1)
+ self.app.goToPage(self.app.current_page + 1),
+ }
+ }
+ } else {
+ self.app.handleHorizontalScroll(gesture.horizontal_delta);
+ }
+ }
+ }
}
},
else => {},
diff --git a/src/pdf/main.zig b/src/pdf/main.zig
index 6002814..1bc87f1 100644
--- a/src/pdf/main.zig
+++ b/src/pdf/main.zig
@@ -42,6 +42,8 @@ pub const Client = struct {
scale: i32 = 1,
seat: ?*wl.Seat = null,
pointer: ?*wl.Pointer = null,
+ axis_source: wl.Pointer.AxisSource = .wheel,
+ finger_navigation: @import("ui").trackpad_navigation.Gesture = .{},
keyboard: ?*wl.Keyboard = null,
input_serial: u32 = 0,
wm_base: ?*xdg.WmBase = null,
diff --git a/src/ui/mod.zig b/src/ui/mod.zig
index f10103b..b07c2ae 100644
--- a/src/ui/mod.zig
+++ b/src/ui/mod.zig
@@ -6,6 +6,7 @@ pub const paint = @import("paint.zig");
pub const window_chrome = @import("window_chrome.zig");
pub const theme = @import("theme.zig");
pub const input = @import("input.zig");
+pub const trackpad_navigation = @import("trackpad_navigation.zig");
pub const widgets = struct {
pub const scrollbar = @import("widgets/scrollbar.zig");
diff --git a/src/ui/trackpad_navigation.zig b/src/ui/trackpad_navigation.zig
new file mode 100644
index 0000000..ccfc429
--- /dev/null
+++ b/src/ui/trackpad_navigation.zig
@@ -0,0 +1,96 @@
+//! Classifies horizontal finger scrolling as app navigation.
+const std = @import("std");
+
+pub const Direction = enum { back, forward };
+
+pub const Result = struct {
+ direction: ?Direction,
+ horizontal_delta: f64,
+};
+
+pub const Gesture = struct {
+ horizontal: f64 = 0,
+ vertical: f64 = 0,
+
+ pub const minimum_horizontal_distance: f64 = 80.0;
+ pub const horizontal_dominance: f64 = 1.5;
+
+ pub fn reset(self: *Gesture) void {
+ self.* = .{};
+ }
+
+ /// Accumulates one finger-source axis delta.
+ pub fn add(self: *Gesture, horizontal_axis: bool, delta: f64) void {
+ if (horizontal_axis) {
+ self.horizontal += delta;
+ } else {
+ self.vertical += delta;
+ }
+ }
+
+ /// A vertical stop clears a vertical-only gesture. A horizontal stop
+ /// classifies the complete movement and returns its original delta so
+ /// callers can preserve ordinary horizontal scrolling when it is not
+ /// a navigation gesture.
+ pub fn stop(self: *Gesture, horizontal_axis: bool) ?Result {
+ if (!horizontal_axis) {
+ if (self.horizontal == 0) self.vertical = 0;
+ return null;
+ }
+
+ const horizontal = self.horizontal;
+ const direction: ?Direction = if (
+ @abs(horizontal) >= minimum_horizontal_distance and
+ @abs(horizontal) > @abs(self.vertical) * horizontal_dominance
+ )
+ (if (horizontal < 0) .back else .forward)
+ else
+ null;
+
+ self.reset();
+ return .{
+ .direction = direction,
+ .horizontal_delta = horizontal,
+ };
+ }
+};
+
+test "rightward finger movement navigates back" {
+ var gesture: Gesture = .{};
+ gesture.add(true, -110);
+ gesture.add(false, 12);
+ const result = gesture.stop(true).?;
+ try std.testing.expectEqual(Direction.back, result.direction.?);
+}
+
+test "leftward finger movement navigates forward" {
+ var gesture: Gesture = .{};
+ gesture.add(true, 105);
+ const result = gesture.stop(true).?;
+ try std.testing.expectEqual(Direction.forward, result.direction.?);
+}
+
+test "short horizontal movement remains scrolling" {
+ var gesture: Gesture = .{};
+ gesture.add(true, 50);
+ const result = gesture.stop(true).?;
+ try std.testing.expectEqual(@as(?Direction, null), result.direction);
+ try std.testing.expectEqual(@as(f64, 50), result.horizontal_delta);
+}
+
+test "vertical-dominant movement remains scrolling" {
+ var gesture: Gesture = .{};
+ gesture.add(true, -100);
+ gesture.add(false, 90);
+ const result = gesture.stop(true).?;
+ try std.testing.expectEqual(@as(?Direction, null), result.direction);
+}
+
+test "vertical-only stop does not contaminate the next gesture" {
+ var gesture: Gesture = .{};
+ gesture.add(false, 140);
+ try std.testing.expect(gesture.stop(false) == null);
+ gesture.add(true, -100);
+ const result = gesture.stop(true).?;
+ try std.testing.expectEqual(Direction.back, result.direction.?);
+}