Skip to content

Commit 9081a81

Browse files
Merge pull request #1207 from lightpanda-io/pagetransitionevent
add PageTransitionEvent
2 parents 0dfd5ce + 2bbbb46 commit 9081a81

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
2+
//
3+
// Francis Bouvier <francis@lightpanda.io>
4+
// Pierre Tachoire <pierre@lightpanda.io>
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU Affero General Public License as
8+
// published by the Free Software Foundation, either version 3 of the
9+
// License, or (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU Affero General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Affero General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
const log = @import("../../log.zig");
20+
const Window = @import("../html/window.zig").Window;
21+
22+
const parser = @import("../netsurf.zig");
23+
const Event = @import("../events/event.zig").Event;
24+
25+
// https://developer.mozilla.org/en-US/docs/Web/API/PageTransitionEvent
26+
const PageTransitionEvent = @This();
27+
28+
pub const prototype = *Event;
29+
pub const union_make_copy = true;
30+
31+
pub const EventInit = struct {
32+
persisted: ?bool,
33+
};
34+
35+
proto: parser.Event,
36+
persisted: bool,
37+
38+
pub fn constructor(event_type: []const u8, opts: EventInit) !PageTransitionEvent {
39+
const event = try parser.eventCreate();
40+
defer parser.eventDestroy(event);
41+
42+
try parser.eventInit(event, event_type, .{});
43+
parser.eventSetInternalType(event, .page_transition_event);
44+
45+
return .{
46+
.proto = event.*,
47+
.persisted = opts.persisted orelse false,
48+
};
49+
}
50+
51+
const PageTransitionKind = enum { show, hide };
52+
53+
pub fn dispatch(window: *Window, kind: PageTransitionKind, persisted: bool) void {
54+
const evt_type = switch (kind) {
55+
.show => "pageshow",
56+
.hide => "pagehide",
57+
};
58+
59+
log.debug(.script_event, "dispatch event", .{
60+
.type = evt_type,
61+
.source = "navigation",
62+
});
63+
64+
var evt = PageTransitionEvent.constructor(evt_type, .{ .persisted = persisted }) catch |err| {
65+
log.err(.app, "event constructor error", .{
66+
.err = err,
67+
.type = evt_type,
68+
.source = "navigation",
69+
});
70+
71+
return;
72+
};
73+
74+
_ = parser.eventTargetDispatchEvent(
75+
@as(*parser.EventTarget, @ptrCast(window)),
76+
&evt.proto,
77+
) catch |err| {
78+
log.err(.app, "dispatch event error", .{
79+
.err = err,
80+
.type = evt_type,
81+
.source = "navigation",
82+
});
83+
};
84+
}

src/browser/events/event.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const MessageEvent = @import("../dom/MessageChannel.zig").MessageEvent;
4040
const PopStateEvent = @import("../html/History.zig").PopStateEvent;
4141
const CompositionEvent = @import("composition_event.zig").CompositionEvent;
4242
const NavigationCurrentEntryChangeEvent = @import("../navigation/root.zig").NavigationCurrentEntryChangeEvent;
43+
const PageTransitionEvent = @import("../events/PageTransitionEvent.zig");
4344

4445
// Event interfaces
4546
pub const Interfaces = .{
@@ -53,6 +54,7 @@ pub const Interfaces = .{
5354
PopStateEvent,
5455
CompositionEvent,
5556
NavigationCurrentEntryChangeEvent,
57+
PageTransitionEvent,
5658
};
5759

5860
pub const Union = generate.Union(Interfaces);
@@ -85,6 +87,7 @@ pub const Event = struct {
8587
.navigation_current_entry_change_event => .{
8688
.NavigationCurrentEntryChangeEvent = @as(*NavigationCurrentEntryChangeEvent, @ptrCast(evt)).*,
8789
},
90+
.page_transition_event => .{ .PageTransitionEvent = @as(*PageTransitionEvent, @ptrCast(evt)).* },
8891
};
8992
}
9093

src/browser/netsurf.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ pub const EventType = enum(u8) {
561561
pop_state = 9,
562562
composition_event = 10,
563563
navigation_current_entry_change_event = 11,
564+
page_transition_event = 12,
564565
};
565566

566567
pub const MutationEvent = c.dom_mutation_event;

src/browser/page.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const HTMLDocument = @import("html/document.zig").HTMLDocument;
3737

3838
const NavigationKind = @import("navigation/root.zig").NavigationKind;
3939
const NavigationCurrentEntryChangeEvent = @import("navigation/root.zig").NavigationCurrentEntryChangeEvent;
40+
const PageTransitionEvent = @import("events/PageTransitionEvent.zig");
4041

4142
const js = @import("js/js.zig");
4243
const URL = @import("../url.zig").URL;
@@ -677,6 +678,8 @@ pub const Page = struct {
677678
parser.toEventTarget(Window, &self.window),
678679
loadevt,
679680
);
681+
682+
PageTransitionEvent.dispatch(&self.window, .show, false);
680683
}
681684

682685
fn pageHeaderDoneCallback(transfer: *Http.Transfer) !void {

0 commit comments

Comments
 (0)