proper UIEvent

This commit is contained in:
Muki Kiboigo
2025-12-10 06:48:22 -08:00
parent e1b129f43b
commit f2a204989b
4 changed files with 69 additions and 3 deletions

View File

@@ -565,6 +565,7 @@ pub const JsApis = flattenTypes(&.{
@import("../webapi/event/ErrorEvent.zig"),
@import("../webapi/event/MessageEvent.zig"),
@import("../webapi/event/ProgressEvent.zig"),
@import("../webapi/event/UIEvent.zig"),
@import("../webapi/MessageChannel.zig"),
@import("../webapi/MessagePort.zig"),
@import("../webapi/media/MediaError.zig"),

View File

@@ -0,0 +1,49 @@
<!DOCTYPE html>
<script src="../testing.js"></script>
<script id=uiEventConstructor>
const evt = new UIEvent('click', {
detail: 5,
bubbles: true,
cancelable: true
});
testing.expectEqual('click', evt.type);
testing.expectEqual(5, evt.detail);
testing.expectEqual(true, evt.bubbles);
testing.expectEqual(true, evt.cancelable);
testing.expectEqual(window, evt.view);
</script>
<script id=uiEventWithView>
const evt2 = new UIEvent('mousedown', {
detail: 2,
view: window
});
testing.expectEqual('mousedown', evt2.type);
testing.expectEqual(2, evt2.detail);
testing.expectEqual(window, evt2.view);
</script>
<script id=uiEventDefaults>
const evt3 = new UIEvent('focus');
testing.expectEqual('focus', evt3.type);
testing.expectEqual(0, evt3.detail);
testing.expectEqual(false, evt3.bubbles);
testing.expectEqual(false, evt3.cancelable);
testing.expectEqual(window, evt3.view);
</script>
<script id=uiEventInheritance>
const evt4 = new UIEvent('blur', { detail: 1 });
testing.expectEqual('blur', evt4.type);
testing.expectEqual(Event.NONE, evt4.eventPhase);
testing.expectEqual(false, evt4.defaultPrevented);
testing.expectEqual(1, evt4.detail);
</script>
<script id=uiEventDetailTypes>
const evt5 = new UIEvent('custom', { detail: 0 });
const evt6 = new UIEvent('custom', { detail: 100 });
testing.expectEqual(0, evt5.detail);
testing.expectEqual(100, evt6.detail);
</script>

View File

@@ -56,6 +56,7 @@ pub const Type = union(enum) {
message_event: *@import("event/MessageEvent.zig"),
progress_event: *@import("event/ProgressEvent.zig"),
composition_event: *@import("event/CompositionEvent.zig"),
ui_event: *@import("event/UIEvent.zig"),
};
const Options = struct {

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
@@ -30,16 +30,24 @@ _view: *Window,
pub const EventOptions = struct {
detail: u32 = 0,
view: ?*Window = null,
// From Event.
bubbles: bool = false,
cancelable: bool = false,
};
pub fn init(typ: []const u8, _options: ?EventOptions, page: *Page) !*UIEvent {
const options = _options orelse EventOptions{};
return page._factory.event(typ, UIEvent{
const event = try page._factory.event(typ, UIEvent{
._proto = undefined,
._detail = options.detail,
._view = options.view,
._view = options.view orelse page.window,
});
event._proto._bubbles = options.bubbles;
event._proto._cancelable = options.cancelable;
return event;
}
pub fn asEvent(self: *UIEvent) *Event {
@@ -56,6 +64,8 @@ pub fn getView(self: *UIEvent) *Window {
return self._view;
}
// deprecated `initUIEvent()` not implemented
pub const JsApi = struct {
pub const bridge = js.Bridge(UIEvent);
@@ -69,3 +79,8 @@ pub const JsApi = struct {
pub const detail = bridge.accessor(UIEvent.getDetail, null, .{});
pub const view = bridge.accessor(UIEvent.getView, null, .{});
};
const testing = @import("../../../testing.zig");
test "WebApi: UIEvent" {
try testing.htmlRunner("event/ui.html", .{});
}