2 Commits

Author SHA1 Message Date
Muki Kiboigo
cb141c35b7 create proper navigate event 2025-11-11 21:01:27 -08:00
Muki Kiboigo
01c2f2c6ea wip navigate event 2025-11-11 20:20:39 -08:00
3 changed files with 136 additions and 0 deletions

View File

@@ -40,6 +40,7 @@ const MessageEvent = @import("../dom/MessageChannel.zig").MessageEvent;
const PopStateEvent = @import("../html/History.zig").PopStateEvent; const PopStateEvent = @import("../html/History.zig").PopStateEvent;
const CompositionEvent = @import("composition_event.zig").CompositionEvent; const CompositionEvent = @import("composition_event.zig").CompositionEvent;
const NavigationCurrentEntryChangeEvent = @import("../navigation/root.zig").NavigationCurrentEntryChangeEvent; const NavigationCurrentEntryChangeEvent = @import("../navigation/root.zig").NavigationCurrentEntryChangeEvent;
const NavigateEvent = @import("../navigation/root.zig").NavigateEvent;
// Event interfaces // Event interfaces
pub const Interfaces = .{ pub const Interfaces = .{
@@ -53,6 +54,7 @@ pub const Interfaces = .{
PopStateEvent, PopStateEvent,
CompositionEvent, CompositionEvent,
NavigationCurrentEntryChangeEvent, NavigationCurrentEntryChangeEvent,
NavigateEvent,
}; };
pub const Union = generate.Union(Interfaces); pub const Union = generate.Union(Interfaces);
@@ -85,6 +87,7 @@ pub const Event = struct {
.navigation_current_entry_change_event => .{ .navigation_current_entry_change_event => .{
.NavigationCurrentEntryChangeEvent = @as(*NavigationCurrentEntryChangeEvent, @ptrCast(evt)).*, .NavigationCurrentEntryChangeEvent = @as(*NavigationCurrentEntryChangeEvent, @ptrCast(evt)).*,
}, },
.navigate_event => .{ .NavigateEvent = @as(*NavigateEvent, @ptrCast(evt)).* },
}; };
} }

View File

@@ -208,6 +208,138 @@ pub const NavigationCurrentEntryChangeEvent = struct {
} }
}; };
pub const NavigateEvent = struct {
pub const prototype = *Event;
pub const union_make_copy = true;
pub const EventInit = struct {
canIntercept: ?bool = false,
// todo: destination
downloadRequest: ?[]const u8 = null,
// todo: formData
hashChange: ?bool = false,
hasUAVisualTransition: ?bool = false,
// info: ?js.Value,
navigationType: ?NavigationType = .push,
// todo: signal
// todo: sourceElement
userInitiated: ?bool = false,
};
proto: parser.Event,
can_intercept: bool,
download_request: ?[]const u8,
// todo: desintation
hash_change: bool,
has_ua_visual_transition: bool,
info: []const u8,
navigation_type: NavigationType,
// todo: signal
// todo: sourceElement
user_initiated: bool,
pub fn constructor(event_type: []const u8, opts: EventInit) !NavigateEvent {
const event = try parser.eventCreate();
defer parser.eventDestroy(event);
try parser.eventInit(event, event_type, .{});
parser.eventSetInternalType(event, .navigate_event);
return .{
.proto = event.*,
.can_intercept = opts.canIntercept orelse false,
.download_request = opts.downloadRequest orelse null,
.hash_change = opts.hashChange orelse false,
.has_ua_visual_transition = opts.hasUAVisualTransition orelse false,
.info = undefined,
.navigation_type = opts.navigationType orelse .push,
// .info = if (opts.info) |info| try info.toString(page.arena) else null,
.user_initiated = opts.userInitiated orelse false,
};
}
pub const InterceptOptions = struct {
// runs after currentEntry is updated
handler: ?js.Function,
// runs before currentEntry is updated
precommitHandler: ?js.Function,
focusReset: ?enum { @"after-transition", manual },
scroll: ?enum { @"after-transition", manual },
};
// https://developer.mozilla.org/en-US/docs/Web/API/NavigateEvent/intercept
pub fn intercept(
self: *NavigateEvent,
opts: ?InterceptOptions,
_: *Page,
) !void {
if (!self.can_intercept) {
return error.Security;
}
if (self.proto.in_dispatch) {
return error.InvalidState;
}
// try parser.eventPreventDefault(&self.proto);
if (opts) |options| {
if (options.precommitHandler) |handler| {
_ = try handler.call(void, .{});
}
}
// update current entry here.
if (opts) |options| {
if (options.handler) |handler| {
const result = try handler.call(js.Value, .{});
if (result.value.isPromise()) {
// must be stored and resolved based on nav outcome.
}
}
// todo: handle focusReset and scroll
}
}
pub fn scroll(self: *NavigateEvent) !void {
_ = self;
}
pub fn dispatch(navigation: *Navigation, from: *NavigationHistoryEntry, typ: ?NavigationType) void {
log.debug(.script_event, "dispatch event", .{
.type = "navigate",
.source = "navigation",
});
var evt = NavigateEvent.constructor(
"navigate",
.{ .from = from, .navigationType = typ },
) catch |err| {
log.err(.app, "event constructor error", .{
.err = err,
.type = "navigate",
.source = "navigation",
});
return;
};
_ = parser.eventTargetDispatchEvent(
@as(*parser.EventTarget, @ptrCast(navigation)),
&evt.proto,
) catch |err| {
log.err(.app, "dispatch event error", .{
.err = err,
.type = "navigate",
.source = "navigation",
});
};
}
};
const testing = @import("../../testing.zig"); const testing = @import("../../testing.zig");
test "Browser: Navigation" { test "Browser: Navigation" {
try testing.htmlRunner("html/navigation/navigation.html"); try testing.htmlRunner("html/navigation/navigation.html");

View File

@@ -561,6 +561,7 @@ pub const EventType = enum(u8) {
pop_state = 9, pop_state = 9,
composition_event = 10, composition_event = 10,
navigation_current_entry_change_event = 11, navigation_current_entry_change_event = 11,
navigate_event = 12,
}; };
pub const MutationEvent = c.dom_mutation_event; pub const MutationEvent = c.dom_mutation_event;