@@ -5,6 +5,8 @@ const webui = @import("webui");
55var python_server_proc : std.process.Child = undefined ;
66var python_running : bool = false ;
77
8+ var home_url : [:0 ]u8 = undefined ;
9+
810pub fn main () ! void {
911 // Create new window
1012 var nwin = webui .newWindow ();
@@ -42,8 +44,8 @@ pub fn main() !void {
4244
4345 // Show a new window served by our custom web server (spawned above):
4446 var buf : [64 ]u8 = undefined ;
45- const url : [: 0 ] u8 = try std .fmt .bufPrintZ (& buf , "http://localhost:{d}/index.html" , .{backend_port });
46- _ = nwin .show (url );
47+ home_url = try std .fmt .bufPrintZ (& buf , "http://localhost:{d}/index.html" , .{backend_port });
48+ _ = nwin .show (home_url );
4749
4850 // Wait until all windows get closed
4951 webui .wait ();
@@ -107,24 +109,29 @@ fn events(e: webui.Event) void {
107109 std .debug .print ("Click. \n " , .{});
108110 },
109111 .EVENT_NAVIGATION = > {
110- const allocator = std .heap .c_allocator ;
112+ // Because we used `bind(MyWindow, "", events);`
113+ // WebUI will block all `href` link clicks and sent here instead.
114+ // We can then control the behaviour of links as needed.
115+ //
116+ // In this SPA example, the client has its own router and
117+ // does the navigation for all pages in page.js (2nd, 3rd, 4th).
118+ //
119+ // However, for the 1st page, we have to handle navigation here:
111120
112121 // get the url string
113122 const url = e .getString ();
114123
115- // we use this to get widnow
116- var win = e .getWindow ();
124+ if (std .mem .eql (u8 , url , home_url )) {
125+ // we use this to get widnow
126+ var win = e .getWindow ();
117127
118- // we generate the new url!
119- const new_url = allocator .dupeZ (u8 , url ) catch unreachable ;
120- defer allocator .free (new_url );
128+ std .debug .print ("WebUI.js is Starting navigation to: {s}\n " , .{url });
121129
122- std .debug .print ("Starting navigation to: {s}\n " , .{url });
123-
124- // Because we used `bind(MyWindow, "", events);`
125- // WebUI will block all `href` link clicks and sent here instead.
126- // We can then control the behaviour of links as needed.
127- win .navigate (new_url );
130+ win .navigate (url );
131+ } else {
132+ std .debug .print ("Client JS is navigating to: {s}\n " , .{url });
133+ // nothing to do, client is navigating
134+ }
128135 },
129136 else = > {
130137 std .debug .print ("Other event {}. \n " , .{e .event_type });
0 commit comments