Skip to content

Lightpanda: the headless browser designed for AI and automation

refactor(input): model the touch contact as a phase, inherit touch-action in its own style group

Adrià Arrufatb04a5ff1019d
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zig
index 31c04b8d9..10bbe2e4b 100644
--- a/src/browser/StyleManager.zig
+++ b/src/browser/StyleManager.zig
@@ -55,6 +55,7 @@ arena: *lp.Arena,
visibility: Group(Visibility) = .{},
geometry: Group(Geometry) = .{},
+touch_action: Group(TouchAction) = .{},
// Keyed by property name, pruning to what is (hopefully) one or few rules
custom_rules: std.StringHashMapUnmanaged(CustomProperty) = .empty,
@@ -682,12 +683,13 @@ pub fn overscrollContainAxes(self: *StyleManager, el: *Element) Element.ScrollAx
return .{ .x = p.overscroll_x_contains, .y = p.overscroll_y_contains };
}
-/// The axes along which `el`'s own computed touch-action forbids a touch from
-/// panning. No ancestor walk.
+/// The axes along which the effective touch-action of `el`, its own
+/// intersected with its ancestors', forbids a touch from panning.
pub fn touchPanBlockedAxes(self: *StyleManager, el: *Element) Element.ScrollAxes {
+ self.assertOwns(el);
self.rebuildIfDirty() catch return .{};
- const p = self.geometryProps(el);
- return .{ .x = p.touch_pan_x_blocked, .y = p.touch_pan_y_blocked };
+ const p = self.touch_action.inheritedProps(self.arena.allocator(), el, self.frame);
+ return .{ .x = p.pan_x_blocked, .y = p.pan_y_blocked };
}
/// Own computed width or height in px, from inline style or a sheet rule.
@@ -1354,14 +1356,12 @@ const Visibility = struct {
/// An element's box: its size and how it scrolls.
const Geometry = struct {
const Declared = struct {
- const names = [_][]const u8{ "overflow-x", "overflow-y", "overscroll-behavior-x", "overscroll-behavior-y", "touch-action", "width", "height" };
+ const names = [_][]const u8{ "overflow-x", "overflow-y", "overscroll-behavior-x", "overscroll-behavior-y", "width", "height" };
overflow_x_scrolls: ?bool = null,
overflow_y_scrolls: ?bool = null,
overscroll_x_contains: ?bool = null,
overscroll_y_contains: ?bool = null,
- touch_pan_x_blocked: ?bool = null,
- touch_pan_y_blocked: ?bool = null,
width: ?Length = null,
height: ?Length = null,
@@ -1374,10 +1374,6 @@ const Geometry = struct {
self.overscroll_x_contains = overscrollContains(value);
} else if (std.ascii.eqlIgnoreCase(name, "overscroll-behavior-y")) {
self.overscroll_y_contains = overscrollContains(value);
- } else if (std.ascii.eqlIgnoreCase(name, "touch-action")) {
- const pans = touchActionPans(value) orelse return;
- self.touch_pan_x_blocked = !pans.x;
- self.touch_pan_y_blocked = !pans.y;
} else if (std.ascii.eqlIgnoreCase(name, "width")) {
self.width = Length.parse(value);
} else if (std.ascii.eqlIgnoreCase(name, "height")) {
@@ -1398,10 +1394,37 @@ const Geometry = struct {
return std.ascii.eqlIgnoreCase(value, "contain") or
std.ascii.eqlIgnoreCase(value, "none");
}
+ };
+
+ const Computed = packed struct(u72) {
+ overflow_x_scrolls: bool = false,
+ overflow_y_scrolls: bool = false,
+ overscroll_x_contains: bool = false,
+ overscroll_y_contains: bool = false,
+ width: Length = .{},
+ height: Length = .{},
+ };
+};
+
+/// The axes a touch may not pan along: touch-action, intersected with the
+/// ancestors' (an ancestor's `none` stops the pan too).
+const TouchAction = struct {
+ const Declared = struct {
+ const names = [_][]const u8{"touch-action"};
+
+ pan_x_blocked: ?bool = null,
+ pan_y_blocked: ?bool = null,
+
+ fn apply(self: *Declared, name: []const u8, value: []const u8) void {
+ if (!std.ascii.eqlIgnoreCase(name, "touch-action")) return;
+ const pans = parse(value) orelse return;
+ self.pan_x_blocked = !pans.x;
+ self.pan_y_blocked = !pans.y;
+ }
// The axes a touch may pan along; null for an invalid value. The
// directional keywords count for their whole axis.
- fn touchActionPans(value: []const u8) ?Element.ScrollAxes {
+ fn parse(value: []const u8) ?Element.ScrollAxes {
if (std.ascii.eqlIgnoreCase(value, "auto") or std.ascii.eqlIgnoreCase(value, "manipulation")) {
return .{ .x = true, .y = true };
}
@@ -1409,7 +1432,7 @@ const Geometry = struct {
return .{};
}
var pans: Element.ScrollAxes = .{};
- var it = std.mem.tokenizeAny(u8, value, " \t\n");
+ var it = std.mem.tokenizeAny(u8, value, &std.ascii.whitespace);
while (it.next()) |token| {
if (std.ascii.eqlIgnoreCase(token, "pan-x") or std.ascii.eqlIgnoreCase(token, "pan-left") or std.ascii.eqlIgnoreCase(token, "pan-right")) {
pans.x = true;
@@ -1423,25 +1446,32 @@ const Geometry = struct {
}
};
- const Computed = packed struct(u74) {
- overflow_x_scrolls: bool = false,
- overflow_y_scrolls: bool = false,
- overscroll_x_contains: bool = false,
- overscroll_y_contains: bool = false,
- touch_pan_x_blocked: bool = false,
- touch_pan_y_blocked: bool = false,
- width: Length = .{},
- height: Length = .{},
+ pub const Cascaded = struct {
+ pan_x_blocked: bool = false,
+ pan_y_blocked: bool = false,
+ };
+
+ const Computed = packed struct(u2) {
+ pan_x_blocked: bool = false,
+ pan_y_blocked: bool = false,
+
+ fn resolve(own: Cascaded, parent: Computed) Computed {
+ return .{
+ .pan_x_blocked = own.pan_x_blocked or parent.pan_x_blocked,
+ .pan_y_blocked = own.pan_y_blocked or parent.pan_y_blocked,
+ };
+ }
};
};
/// Every group's share of one declaration block, so a sheet's block is folded
/// once. Field names match the StyleManager's group fields.
const Declarations = struct {
- const names = Visibility.Declared.names ++ Geometry.Declared.names;
+ const names = Visibility.Declared.names ++ Geometry.Declared.names ++ TouchAction.Declared.names;
visibility: Visibility.Declared = .{},
geometry: Geometry.Declared = .{},
+ touch_action: TouchAction.Declared = .{},
fn apply(self: *Declarations, name: []const u8, value: []const u8) void {
inline for (group_fields) |field| {
@@ -2156,6 +2186,9 @@ test "StyleManager: memo: reuse and invalidation" {
try b.setStyle("touch-action: manipulation", frame);
try testing.expectEqual(Element.ScrollAxes{}, sm.touchPanBlockedAxes(b));
try testing.expectEqual(Element.ScrollAxes{}, sm.touchPanBlockedAxes(p));
+ // An ancestor's touch-action narrows its descendants'.
+ try p.setStyle("touch-action: pan-x", frame);
+ try testing.expectEqual(Element.ScrollAxes{ .x = false, .y = true }, sm.touchPanBlockedAxes(b));
// A stylesheet change resets the memo
sm.sheetModified();
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index 137a71b92..368ce2b28 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -615,14 +615,24 @@ pub const TouchContact = struct {
pointer_id: i32,
tap_count: u32 = 1,
suppress_mouse: bool = false,
- suppress_click: bool = false,
start_prevented: bool = false,
- // Moved past the slop, so touchmove is no longer suppressed.
- dragging: bool = false,
- // The drag became a scroll: the touch events left are not cancelable.
- scrolling: bool = false,
- // A scroll the touch-action allows: the pointer was cancelled.
- panning: bool = false,
+ phase: Phase = .slop,
+
+ const Phase = enum {
+ // Still a tap: only the pointer moves.
+ slop,
+ // Moved past the slop, so touchmove fires and nothing activates.
+ drag,
+ // The page didn't cancel the drag: the touch events left aren't
+ // cancelable.
+ scroll,
+ // A scroll the touch-action allows: the pointer was cancelled.
+ pan,
+ };
+
+ fn dispatch(self: TouchContact, modifiers: Modifiers) TouchDispatch {
+ return .{ .modifiers = modifiers, .scrolling = self.phase == .scroll or self.phase == .pan };
+ }
/// A released contact reports a 1x1 point; a cancelled one keeps its
/// geometry.
@@ -674,8 +684,8 @@ pub const LastTap = struct {
const TouchDispatch = struct {
modifiers: Modifiers = .{},
- // Event.cancelable: false for touchcancel, and once a drag scrolls.
- cancelable: bool = true,
+ // A scrolling contact's touch events aren't cancelable.
+ scrolling: bool = false,
};
/// Fires the touch event on `target` (no hit-test); returns whether
@@ -690,9 +700,9 @@ fn dispatchTouchEvent(frame: *Frame, target: *Element, typ: TouchType, point: To
.shiftKey = opts.modifiers.shift,
}, target, point, frame);
- // A cancelable touch event follows the same passive-listener-dependent
- // rule as wheel (see EventManager).
- if (opts.cancelable) {
+ // touchcancel is never cancelable per spec; the others follow the same
+ // passive-listener-dependent rule as wheel (see EventManager).
+ if (typ != .touchcancel and !opts.scrolling) {
event.asEvent()._cancelable_unless_passive = true;
}
@@ -732,7 +742,6 @@ pub fn touchStart(frame: *Frame, target: ?*Element, point: Touch.Point, modifier
dispatchBoundaryEvents(frame, null, resolved, TouchBoundary{ .g = g });
contact.suppress_mouse = try emitPointer(frame, resolved, "pointerdown", g, 0);
contact.start_prevented = try dispatchTouchEvent(frame, resolved, .touchstart, point, .{ .modifiers = modifiers });
- contact.suppress_click = contact.start_prevented;
page.input_touch_contact = contact;
}
@@ -759,25 +768,24 @@ pub fn touchMove(frame: *Frame, over: ?*Element, point: Touch.Point, modifiers:
const dx = point.x - contact.start.x;
const dy = point.y - contact.start.y;
const moved_off = if (over) |el| el != contact.target else false;
- const leaves_slop = !contact.dragging and (moved_off or dx * dx + dy * dy > tap_slop_px * tap_slop_px);
+ const leaves_slop = contact.phase == .slop and (moved_off or dx * dx + dy * dy > tap_slop_px * tap_slop_px);
if (leaves_slop) {
- contact.dragging = true;
- contact.suppress_click = true;
+ contact.phase = .drag;
}
var g = contact.gesture(point, .down, modifiers);
g.button = -1;
- if (!contact.panning) {
+ if (contact.phase != .pan) {
_ = try emitPointer(frame, contact.target, "pointermove", g, 0);
}
- if (!contact.dragging) {
+ if (contact.phase == .slop) {
page.input_touch_contact = contact;
return;
}
- const prevented = try dispatchTouchEvent(frame, contact.target, .touchmove, point, .{ .modifiers = modifiers, .cancelable = !contact.scrolling });
+ const prevented = try dispatchTouchEvent(frame, contact.target, .touchmove, point, contact.dispatch(modifiers));
if (leaves_slop and !prevented and !contact.start_prevented) {
- contact.scrolling = true;
- if (touchActionPans(frame, contact.target, dx, dy)) {
- contact.panning = true;
+ contact.phase = .scroll;
+ if (allowsPan(frame, contact.target, dx, dy)) {
+ contact.phase = .pan;
const cg = contact.gesture(point, .cancel, modifiers);
_ = try emitPointer(frame, contact.target, "pointercancel", cg, 0);
dispatchBoundaryEvents(frame, contact.target, null, TouchBoundary{ .g = cg });
@@ -786,18 +794,12 @@ pub fn touchMove(frame: *Frame, over: ?*Element, point: Touch.Point, modifiers:
page.input_touch_contact = contact;
}
-/// Whether the effective touch-action of `target`, which intersects its
-/// ancestors', allows a pan in the drag's dominant direction. A drag with no
-/// dominant direction, as WebDriver's, needs either axis.
-fn touchActionPans(frame: *Frame, target: *Element, dx: f64, dy: f64) bool {
+/// Whether `target`'s effective touch-action allows a pan in the drag's
+/// dominant direction. A drag with no dominant direction, as WebDriver's,
+/// needs either axis.
+fn allowsPan(frame: *Frame, target: *Element, dx: f64, dy: f64) bool {
const owner = target.ownerFrame(frame) orelse frame;
- var blocked: Element.ScrollAxes = .{};
- var current: ?*Element = target;
- while (current) |el| : (current = el.parentElement()) {
- const own = owner._style_manager.touchPanBlockedAxes(el);
- blocked.x = blocked.x or own.x;
- blocked.y = blocked.y or own.y;
- }
+ const blocked = owner._style_manager.touchPanBlockedAxes(target);
if (@abs(dx) > @abs(dy)) return !blocked.x;
if (@abs(dy) > @abs(dx)) return !blocked.y;
return !blocked.x or !blocked.y;
@@ -824,12 +826,12 @@ pub fn touchEnd(frame: *Frame, point: ?Touch.Point, modifiers: Modifiers, compat
const lift = point orelse contact.point;
const g = contact.gesture(lift, .up, modifiers);
- if (!contact.panning) {
+ if (contact.phase != .pan) {
_ = try emitPointer(frame, contact.target, "pointerup", g, 0);
dispatchBoundaryEvents(frame, contact.target, null, TouchBoundary{ .g = g });
}
- const prevented = try dispatchTouchEvent(frame, contact.target, .touchend, lift, .{ .modifiers = modifiers, .cancelable = !contact.scrolling });
- if (contact.suppress_click or prevented) return;
+ const prevented = try dispatchTouchEvent(frame, contact.target, .touchend, lift, contact.dispatch(modifiers));
+ if (contact.start_prevented or contact.phase != .slop or prevented) return;
frame.page.input_last_tap = .{
.count = contact.tap_count,
.x = contact.start.x,
@@ -862,12 +864,12 @@ pub fn touchEnd(frame: *Frame, point: ?Touch.Point, modifiers: Modifiers, compat
pub fn touchCancel(frame: *Frame, modifiers: Modifiers) !void {
const contact = frame.page.input_touch_contact orelse return;
frame.page.input_touch_contact = null;
- if (!contact.panning) {
+ if (contact.phase != .pan) {
const g = contact.gesture(contact.point, .cancel, modifiers);
_ = try emitPointer(frame, contact.target, "pointercancel", g, 0);
dispatchBoundaryEvents(frame, contact.target, null, TouchBoundary{ .g = g });
}
- _ = try dispatchTouchEvent(frame, contact.target, .touchcancel, contact.point, .{ .modifiers = modifiers, .cancelable = false });
+ _ = try dispatchTouchEvent(frame, contact.target, .touchcancel, contact.point, .{ .modifiers = modifiers });
}
fn touchCompatTarget(frame: *Frame, point: Touch.Point, pinned: ?*Element) !?*Element {
diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zig
index f8e5562c0..7fd6e9274 100644
--- a/src/server/cdp/domains/input.zig
+++ b/src/server/cdp/domains/input.zig
@@ -1514,18 +1514,18 @@ test "cdp.input: dispatchTouchEvent preserves each event's modifier keys" {
// The move leaves the slop, or no touchmove would fire.
const steps = .{
- .{ "touchStart", 1 },
- .{ "touchMove", 2 },
- .{ "touchEnd", 4 },
- .{ "touchStart", 15 },
- .{ "touchCancel", 8 },
+ .{ "touchStart", 1, 0 },
+ .{ "touchMove", 2, 20 },
+ .{ "touchEnd", 4, 0 },
+ .{ "touchStart", 15, 0 },
+ .{ "touchCancel", 8, 0 },
};
inline for (steps, 1..) |step, id| {
const is_lift = comptime std.mem.eql(u8, step[0], "touchEnd") or std.mem.eql(u8, step[0], "touchCancel");
try ctx.processMessage(.{
.id = id,
.method = "Input.dispatchTouchEvent",
- .params = .{ .type = step[0], .modifiers = step[1], .touchPoints = if (is_lift) &.{} else &.{.{ .x = 0, .y = if (comptime std.mem.eql(u8, step[0], "touchMove")) 20 else 0 }} },
+ .params = .{ .type = step[0], .modifiers = step[1], .touchPoints = if (is_lift) &.{} else &.{.{ .x = 0, .y = step[2] }} },
});
}
// Omitted modifiers reset to false, even after a modified gesture.