Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: Touch constructor and TouchEvent touch lists

orezraey72db1b7dd5b7
diff --git a/src/browser/tests/event/touch.html b/src/browser/tests/event/touch.html
new file mode 100644
index 000000000..63a7930ab
--- /dev/null
+++ b/src/browser/tests/event/touch.html
@@ -0,0 +1,66 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+<body><div id=host></div></body>
+
+<script id=touch-constructor>
+{
+ const body = document.body;
+ const t = new Touch({ identifier: 7, target: body, clientX: 5, clientY: 6, pageX: 15, screenX: 25, radiusX: 2, force: 0.5, rotationAngle: 30 });
+ testing.expectEqual(7, t.identifier);
+ testing.expectEqual(body, t.target);
+ testing.expectEqual([5, 6, 15, 0, 25, 0], [t.clientX, t.clientY, t.pageX, t.pageY, t.screenX, t.screenY]);
+ testing.expectEqual([2, 0, 0.5, 30], [t.radiusX, t.radiusY, t.force, t.rotationAngle]);
+
+ // Every member but identifier and target defaults to 0.
+ const bare = new Touch({ identifier: 8, target: body });
+ testing.expectEqual([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [bare.clientX, bare.clientY, bare.pageX, bare.pageY,
+ bare.screenX, bare.screenY, bare.radiusX, bare.radiusY, bare.force, bare.rotationAngle]);
+
+ // A window is an EventTarget too.
+ testing.expectEqual(window, new Touch({ identifier: 1, target: window }).target);
+
+ // identifier and target are required, and target must be an EventTarget.
+ testing.expectError('TypeError', () => new Touch({ identifier: 1 }));
+ testing.expectError('TypeError', () => new Touch({ target: body }));
+ testing.expectError('TypeError', () => new Touch({ identifier: 1, target: {} }));
+ testing.expectError('TypeError', () => new Touch());
+
+ // The numbers are restricted: NaN and the infinities are TypeErrors.
+ testing.expectError('TypeError', () => new Touch({ identifier: 1, target: body, clientX: NaN }));
+ testing.expectError('TypeError', () => new Touch({ identifier: 1, target: body, force: Infinity }));
+ testing.expectError('TypeError', () => new Touch({ identifier: 1, target: body, pageY: -Infinity }));
+ // identifier is a long: it wraps.
+ testing.expectEqual(5, new Touch({ identifier: 2 ** 32 + 5, target: body }).identifier);
+ // The float members are single precision.
+ testing.expectEqual(0.30000001192092896, new Touch({ identifier: 1, target: body, force: 0.3 }).force);
+}
+</script>
+
+<script id=touch-event-init-lists>
+{
+ const body = document.body;
+ const a = new Touch({ identifier: 1, target: body });
+ const b = new Touch({ identifier: 2, target: body });
+ const ev = new TouchEvent('touchstart', { touches: [a, b], targetTouches: [a], changedTouches: [b], bubbles: true });
+ testing.expectEqual([2, 1, 1], [ev.touches.length, ev.targetTouches.length, ev.changedTouches.length]);
+ // The lists hold the touches given, and are the same list on every read.
+ testing.expectEqual(true, ev.touches[0] === a && ev.touches.item(1) === b);
+ testing.expectEqual(true, ev.targetTouches[0] === a && ev.changedTouches[0] === b);
+ testing.expectEqual(true, ev.touches === ev.touches);
+ testing.expectEqual(undefined, ev.touches[2]);
+ testing.expectEqual(null, ev.touches.item(2));
+
+ // Any iterable of Touch; anything else in it is a TypeError.
+ testing.expectEqual(1, new TouchEvent('x', { touches: new Set([a]) }).touches.length);
+ testing.expectError('TypeError', () => new TouchEvent('x', { touches: [{}] }));
+
+ // Without them, the lists are empty.
+ testing.expectEqual([0, 0, 0], ((e) => [e.touches.length, e.targetTouches.length, e.changedTouches.length])(new TouchEvent('x')));
+
+ // A dispatched event carries them to its listeners.
+ let seen;
+ body.addEventListener('touchstart', (e) => { seen = e.touches.length; }, { once: true });
+ body.dispatchEvent(ev);
+ testing.expectEqual(2, seen);
+}
+</script>
diff --git a/src/browser/webapi/event/Touch.zig b/src/browser/webapi/event/Touch.zig
index 77fafb432..23fd0dcb8 100644
--- a/src/browser/webapi/event/Touch.zig
+++ b/src/browser/webapi/event/Touch.zig
@@ -16,27 +16,28 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
+const std = @import("std");
const js = @import("../../js/js.zig");
-const Page = @import("../../Page.zig");
+const Frame = @import("../../Frame.zig");
const EventTarget = @import("../EventTarget.zig");
-const TouchEvent = @import("TouchEvent.zig");
// https://w3c.github.io/touch-events/#idl-def-touch
//
-// Stored by value inside its TouchEvent, so its address is stable for the
-// event's lifetime.
+// Allocated by the frame's factory, so it lives as long as the frame: a Touch
+// can be shared by any number of TouchLists, of trusted events and of events
+// the page builds, and none of them owns it.
const Touch = @This();
-_event: *TouchEvent,
-// EventTarget, not Element, per spec, and because dispatch retargets it
-// across shadow boundaries the same way it retargets Event.target (see
-// TouchEvent.touchTargetPtr / EventManager's AdjustedTargets).
+// EventTarget, not Element, per spec, and because dispatch retargets a trusted
+// touch's target across shadow boundaries the same way it retargets
+// Event.target (see TouchEvent.touchTargetPtr / EventManager's AdjustedTargets).
_target: ?*EventTarget,
_point: Point,
+_page_x: f64,
+_page_y: f64,
+_screen_x: f64,
+_screen_y: f64,
-/// One contact as the client describes it. The names and defaults are CDP's
-/// Input.TouchPoint, so the protocol handler parses straight into it; the
-/// defaults are Chrome's for a point that leaves them out.
pub const Point = struct {
x: f64,
y: f64,
@@ -47,12 +48,60 @@ pub const Point = struct {
force: f64 = 1,
};
-pub fn acquireRef(self: *Touch) void {
- self._event.asEvent().acquireRef();
+// https://w3c.github.io/touch-events/#dom-touchinit
+const Init = struct {
+ clientX: f64 = 0,
+ clientY: f64 = 0,
+ force: f32 = 0,
+ identifier: i32,
+ pageX: f64 = 0,
+ pageY: f64 = 0,
+ radiusX: f32 = 0,
+ radiusY: f32 = 0,
+ rotationAngle: f32 = 0,
+ screenX: f64 = 0,
+ screenY: f64 = 0,
+ target: *EventTarget,
+};
+
+fn init(opts: Init, frame: *Frame) !*Touch {
+ // All restricted: NaN or an infinity is a TypeError.
+ const numbers = .{ opts.clientX, opts.clientY, opts.force, opts.pageX, opts.pageY, opts.radiusX, opts.radiusY, opts.rotationAngle, opts.screenX, opts.screenY };
+ inline for (numbers) |n| {
+ if (!std.math.isFinite(n)) {
+ return frame.js.typeError("TouchInit member is not a finite number");
+ }
+ }
+ return frame._factory.create(Touch{
+ ._target = opts.target,
+ ._point = .{
+ .x = opts.clientX,
+ .y = opts.clientY,
+ .id = opts.identifier,
+ .radiusX = opts.radiusX,
+ .radiusY = opts.radiusY,
+ .rotationAngle = opts.rotationAngle,
+ .force = opts.force,
+ },
+ ._page_x = opts.pageX,
+ ._page_y = opts.pageY,
+ ._screen_x = opts.screenX,
+ ._screen_y = opts.screenY,
+ });
}
-pub fn releaseRef(self: *Touch, page: *Page) void {
- self._event.asEvent().releaseRef(page);
+/// A touch from input. This engine has no layout, so its page and screen
+/// coordinates alias the client ones, the same simplification
+/// MouseEvent.compatCoordinate makes.
+pub fn initTrusted(target: *EventTarget, point: Point, frame: *Frame) !*Touch {
+ return frame._factory.create(Touch{
+ ._target = target,
+ ._point = point,
+ ._page_x = point.x,
+ ._page_y = point.y,
+ ._screen_x = point.x,
+ ._screen_y = point.y,
+ });
}
fn getIdentifier(self: *const Touch) i32 {
@@ -71,6 +120,22 @@ fn getClientY(self: *const Touch) f64 {
return self._point.y;
}
+fn getPageX(self: *const Touch) f64 {
+ return self._page_x;
+}
+
+fn getPageY(self: *const Touch) f64 {
+ return self._page_y;
+}
+
+fn getScreenX(self: *const Touch) f64 {
+ return self._screen_x;
+}
+
+fn getScreenY(self: *const Touch) f64 {
+ return self._screen_y;
+}
+
fn getRadiusX(self: *const Touch) f64 {
return self._point.radiusX;
}
@@ -95,16 +160,15 @@ pub const JsApi = struct {
pub var class_id: bridge.ClassId = undefined;
};
+ pub const constructor = bridge.constructor(Touch.init, .{});
pub const identifier = bridge.accessor(Touch.getIdentifier, null, .{});
pub const target = bridge.accessor(Touch.getTarget, null, .{});
pub const clientX = bridge.accessor(Touch.getClientX, null, .{});
pub const clientY = bridge.accessor(Touch.getClientY, null, .{});
- // This engine has no layout, so page and screen coordinates alias the
- // client ones, the same simplification MouseEvent.compatCoordinate makes.
- pub const pageX = bridge.accessor(Touch.getClientX, null, .{});
- pub const pageY = bridge.accessor(Touch.getClientY, null, .{});
- pub const screenX = bridge.accessor(Touch.getClientX, null, .{});
- pub const screenY = bridge.accessor(Touch.getClientY, null, .{});
+ pub const pageX = bridge.accessor(Touch.getPageX, null, .{});
+ pub const pageY = bridge.accessor(Touch.getPageY, null, .{});
+ pub const screenX = bridge.accessor(Touch.getScreenX, null, .{});
+ pub const screenY = bridge.accessor(Touch.getScreenY, null, .{});
pub const radiusX = bridge.accessor(Touch.getRadiusX, null, .{});
pub const radiusY = bridge.accessor(Touch.getRadiusY, null, .{});
pub const rotationAngle = bridge.accessor(Touch.getRotationAngle, null, .{});
diff --git a/src/browser/webapi/event/TouchEvent.zig b/src/browser/webapi/event/TouchEvent.zig
index 4c1f292a8..ba90f850f 100644
--- a/src/browser/webapi/event/TouchEvent.zig
+++ b/src/browser/webapi/event/TouchEvent.zig
@@ -16,6 +16,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
+const std = @import("std");
const lp = @import("lightpanda");
const js = @import("../../js/js.zig");
@@ -40,16 +41,23 @@ _alt_key: bool = false,
_meta_key: bool = false,
_ctrl_key: bool = false,
_shift_key: bool = false,
-_touch: ?Touch = null,
+// The touch input dispatched this event for, whose target dispatch retargets.
+_trusted_touch: ?*Touch = null,
+_touches: []const *Touch = &.{},
+_target_touches: []const *Touch = &.{},
+_changed_touches: []const *Touch = &.{},
_touches_list: ?*TouchList = null,
_target_touches_list: ?*TouchList = null,
_changed_list: ?*TouchList = null,
const TouchEventOptions = struct {
altKey: bool = false,
+ changedTouches: []const *Touch = &.{},
ctrlKey: bool = false,
metaKey: bool = false,
shiftKey: bool = false,
+ targetTouches: []const *Touch = &.{},
+ touches: []const *Touch = &.{},
};
pub const Options = Event.inheritOptions(
@@ -65,9 +73,19 @@ pub fn initTrusted(typ: []const u8, _opts: ?Options, frame: *Frame) !*TouchEvent
return initWithTrusted(typ, _opts, true, frame);
}
+/// An event for touch input: its one touch is the changed one, and also the
+/// active one unless it's being lifted (touchend, touchcancel).
pub fn initTrustedWithTouch(typ: []const u8, _opts: ?Options, target: *Element, point: Touch.Point, frame: *Frame) !*TouchEvent {
const event = try initWithTrusted(typ, _opts, true, frame);
- event._touch = .{ ._event = event, ._target = target.asEventTarget(), ._point = point };
+ const touch = try Touch.initTrusted(target.asEventTarget(), point, frame);
+ const lifted = std.mem.eql(u8, typ, "touchend") or std.mem.eql(u8, typ, "touchcancel");
+ const one = try event.asEvent()._arena.allocator().dupe(*Touch, &.{touch});
+ event._trusted_touch = touch;
+ event._changed_touches = one;
+ if (!lifted) {
+ event._touches = one;
+ event._target_touches = one;
+ }
return event;
}
@@ -86,6 +104,9 @@ fn initWithTrusted(typ: []const u8, _opts: ?Options, trusted: bool, frame: *Fram
._meta_key = opts.metaKey,
._ctrl_key = opts.ctrlKey,
._shift_key = opts.shiftKey,
+ ._touches = try arena.allocator().dupe(*Touch, opts.touches),
+ ._target_touches = try arena.allocator().dupe(*Touch, opts.targetTouches),
+ ._changed_touches = try arena.allocator().dupe(*Touch, opts.changedTouches),
},
);
@@ -97,41 +118,37 @@ pub fn asEvent(self: *TouchEvent) *Event {
return self._proto.asEvent();
}
+/// Only a trusted touch's target is retargeted: one the page put in an event
+/// it built keeps the target it was given.
pub fn touchTargetPtr(self: *TouchEvent) ?*?*EventTarget {
- if (self._touch) |*t| return &t._target;
+ if (self._trusted_touch) |t| return &t._target;
return null;
}
/// Cached, so repeated reads don't grow the event's arena. touches and
-/// targetTouches hold the same set here but keep separate lists: they are
+/// targetTouches can hold the same set but keep separate lists: they are
/// distinct objects in real browsers ([SameObject] only ties identity to
/// repeated reads of one attribute).
-fn touchList(self: *TouchEvent, active_only: bool, cache: *?*TouchList) !*TouchList {
+fn touchList(self: *TouchEvent, touches: []const *Touch, cache: *?*TouchList) !*TouchList {
if (cache.*) |list| {
return list;
}
-
- const event = self.asEvent();
- // touchend and touchcancel report the lifted contact in changedTouches only.
- const lifted = event._type_string.eql(comptime .wrap("touchend")) or event._type_string.eql(comptime .wrap("touchcancel"));
- const touch: ?*Touch = if (self._touch) |*t| (if (active_only and lifted) null else t) else null;
-
- const list = try event._arena.create(TouchList);
- list.* = .{ ._event = self, ._touch = touch };
+ const list = try self.asEvent()._arena.create(TouchList);
+ list.* = .{ ._event = self, ._touches = touches };
cache.* = list;
return list;
}
fn getTouches(self: *TouchEvent) !*TouchList {
- return self.touchList(true, &self._touches_list);
+ return self.touchList(self._touches, &self._touches_list);
}
fn getTargetTouches(self: *TouchEvent) !*TouchList {
- return self.touchList(true, &self._target_touches_list);
+ return self.touchList(self._target_touches, &self._target_touches_list);
}
fn getChangedTouches(self: *TouchEvent) !*TouchList {
- return self.touchList(false, &self._changed_list);
+ return self.touchList(self._changed_touches, &self._changed_list);
}
pub fn getAltKey(self: *const TouchEvent) bool {
@@ -185,3 +202,7 @@ test "WebApi: TouchEvent caches its touch lists" {
try testing.expect(try event.getChangedTouches() == try event.getChangedTouches());
try testing.expect(try event.getTouches() != try event.getTargetTouches());
}
+
+test "WebApi: TouchEvent" {
+ try testing.htmlRunner("event/touch.html", .{});
+}
diff --git a/src/browser/webapi/event/TouchList.zig b/src/browser/webapi/event/TouchList.zig
index 5f69a5420..459cc44d6 100644
--- a/src/browser/webapi/event/TouchList.zig
+++ b/src/browser/webapi/event/TouchList.zig
@@ -22,13 +22,13 @@ const Frame = @import("../../Frame.zig");
const Touch = @import("Touch.zig");
const TouchEvent = @import("TouchEvent.zig");
-// https://w3c.github.io/touch-events/#idl-def-touchlist
-//
-// Single-touch: at most one Touch.
+// One of a TouchEvent's touches, targetTouches or changedTouches. It's in the
+// event's arena and keeps the event alive; the touches live as long as their
+// frame (see Touch).
const TouchList = @This();
_event: *TouchEvent,
-_touch: ?*Touch,
+_touches: []const *Touch,
pub fn acquireRef(self: *TouchList) void {
self._event.asEvent().acquireRef();
@@ -39,14 +39,14 @@ pub fn releaseRef(self: *TouchList, page: *Page) void {
}
fn length(self: *const TouchList) u32 {
- return if (self._touch != null) 1 else 0;
+ return @intCast(self._touches.len);
}
pub fn item(self: *TouchList, index: usize) ?*Touch {
- if (index != 0) {
+ if (index >= self._touches.len) {
return null;
}
- return self._touch;
+ return self._touches[index];
}
pub const JsApi = struct {