|
| 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 | +} |
0 commit comments