@@ -29,10 +29,11 @@ const EventTarget = @import("../EventTarget.zig");
2929const Navigation = @This ();
3030
3131const NavigationKind = @import ("root.zig" ).NavigationKind ;
32- const NavigationHistoryEntry = @import ("NavigationHistoryEntry .zig" );
32+ const NavigationActivation = @import ("NavigationActivation .zig" );
3333const NavigationTransition = @import ("root.zig" ).NavigationTransition ;
3434const NavigationState = @import ("root.zig" ).NavigationState ;
3535
36+ const NavigationHistoryEntry = @import ("NavigationHistoryEntry.zig" );
3637const NavigationCurrentEntryChangeEvent = @import ("../event/NavigationCurrentEntryChangeEvent.zig" );
3738const NavigationEventTarget = @import ("NavigationEventTarget.zig" );
3839
@@ -44,6 +45,7 @@ _index: usize = 0,
4445// Need to be stable pointers, because Events can reference entries.
4546_entries : std .ArrayList (* NavigationHistoryEntry ) = .empty ,
4647_next_entry_id : usize = 0 ,
48+ _activation : ? NavigationActivation = null ,
4749
4850pub fn init (arena : std.mem.Allocator ) Navigation {
4951 return Navigation { ._arena = arena };
@@ -63,6 +65,10 @@ pub fn onNewPage(self: *Navigation, page: *Page) !void {
6365 );
6466}
6567
68+ pub fn getActivation (self : * const Navigation ) ? NavigationActivation {
69+ return self ._activation ;
70+ }
71+
6672pub fn getCanGoBack (self : * const Navigation ) bool {
6773 return self ._index > 0 ;
6874}
@@ -71,12 +77,18 @@ pub fn getCanGoForward(self: *const Navigation) bool {
7177 return self ._entries .items .len > self ._index + 1 ;
7278}
7379
80+ pub fn getCurrentEntryOrNull (self : * Navigation ) ? * NavigationHistoryEntry {
81+ if (self ._entries .items .len > self ._index ) {
82+ return self ._entries .items [self ._index ];
83+ } else return null ;
84+ }
85+
7486pub fn getCurrentEntry (self : * Navigation ) * NavigationHistoryEntry {
7587 // This should never fail. An entry should always be created before
7688 // we run the scripts on the page we are loading.
7789 std .debug .assert (self ._entries .items .len > 0 );
7890
79- return self ._entries . items [ self . _index ] ;
91+ return self .getCurrentEntryOrNull () .? ;
8092}
8193
8294pub fn getTransition (_ : * const Navigation ) ? NavigationTransition {
@@ -117,8 +129,8 @@ pub fn forward(self: *Navigation, page: *Page) !NavigationReturn {
117129
118130pub fn updateEntries (self : * Navigation , url : [:0 ]const u8 , kind : NavigationKind , page : * Page , dispatch : bool ) ! void {
119131 switch (kind ) {
120- .replace = > {
121- _ = try self .replaceEntry (url , .{ .source = .navigation , .value = null }, page , dispatch );
132+ .replace = > | state | {
133+ _ = try self .replaceEntry (url , .{ .source = .navigation , .value = state }, page , dispatch );
122134 },
123135 .push = > | state | {
124136 _ = try self .pushEntry (url , .{ .source = .navigation , .value = state }, page , dispatch );
@@ -131,14 +143,23 @@ pub fn updateEntries(self: *Navigation, url: [:0]const u8, kind: NavigationKind,
131143}
132144
133145// This is for after true navigation processing, where we need to ensure that our entries are up to date.
134- // This is only really safe to run in the `pageDoneCallback` where we can guarantee that the URL and NavigationKind are correct.
135- pub fn processNavigation (self : * Navigation , page : * Page ) ! void {
146+ //
147+ // This is only really safe to run in the `pageDoneCallback`
148+ // where we can guarantee that the URL and NavigationKind are correct.
149+ pub fn commitNavigation (self : * Navigation , page : * Page ) ! void {
136150 const url = page .url ;
137151
138152 const kind : NavigationKind = self ._current_navigation_kind orelse .{ .push = null };
139153 defer self ._current_navigation_kind = null ;
140154
155+ const from_entry = self .getCurrentEntryOrNull ();
141156 try self .updateEntries (url , kind , page , false );
157+
158+ self ._activation = NavigationActivation {
159+ ._from = from_entry ,
160+ ._entry = self .getCurrentEntry (),
161+ ._type = kind .toNavigationType (),
162+ };
142163}
143164
144165/// Pushes an entry into the Navigation stack WITHOUT actually navigating to it.
@@ -401,6 +422,7 @@ pub const JsApi = struct {
401422 pub var class_id : bridge.ClassId = undefined ;
402423 };
403424
425+ pub const activation = bridge .accessor (Navigation .getActivation , null , .{});
404426 pub const canGoBack = bridge .accessor (Navigation .getCanGoBack , null , .{});
405427 pub const canGoForward = bridge .accessor (Navigation .getCanGoForward , null , .{});
406428 pub const currentEntry = bridge .accessor (Navigation .getCurrentEntry , null , .{});
0 commit comments