╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ main_wasm.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
30 // adopt the server's session view wholesale — one assignment instead of 31 // hand-restoring each field by reaching into the page's data tree. 32 session = .{ 33 .data = snapshot.session, 34 .arena = &page_arena, 35 // one render per page load (init resets the arena), so page-scoped and 36 // session-scoped allocations share the same arena here. 37 .page_arena = &page_arena, 38 }; 39 40 var next_root = try ui.initRoot(allocator, &snapshot.page, &session); 41 errdefer next_root.deinit(allocator); 42 43 if (root) |*old_root| old_root.deinit(allocator); 44 root = next_root; 45 46 try tick(min_height, max_width); 47 } 48 49 fn tick(min_height: u32, max_width: u32) !void { 50 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 51 52 // apply actions queued during input. the wasm path has no repo, so this is 53 // in-memory only; logged-in web persistence goes through the /ansi POST. 54 session.applyPending(); 55 // a widget asked to move to another page, so hand it to the host instead of 56 // rendering this one again. 57 if (session.next_page) |route| { 58 session.next_page = null; 59 const url = try route.toUrl(&page_arena); 60 _navigate(url.ptr, @intCast(url.len)); 61 return; 62 } 63 // bind the UI to the browser viewport (cols x rows) like the terminal: 64 // min == max height fills it exactly, and each Scroll clips to its 65 // viewport while handing its full content to a native-scrollable element. 66 // a full-height page instead grows to its content and the browser scrolls 67 // the whole page. 68 const max_height: ?u32 = if (session.data.current_page.fullHeight()) null else min_height; 69 try root_ptr.build(allocator, .{ 70 .min_size = .{ .width = null, .height = min_height }, 71 .max_size = .{ .width = max_width, .height = max_height }, 72 }, root_ptr.getFocus()); 73 74 // push input-driven in-page route changes into the browser url without 75 // adding history entries. real cross-page navigations go through _navigate. 76 const current_page = session.data.current_page; 77 const should_push = if (last_pushed_page_maybe) |lp| !lp.eql(current_page) else true; 78 if (should_push) { 79 last_pushed_page_maybe = current_page; 80 const url = try current_page.toUrl(&page_arena); 81 _replaceState(url.ptr, @intCast(url.len)); 82 } 83 84 const html = try web.generateHtml(allocator, root_ptr, &session); 85 defer allocator.free(html); 86 _setHtml(html.ptr, @intCast(html.len)); 87 88 const root_focus = root_ptr.getFocus(); 89 if (root_focus.grandchild_id) |gid| { 90 if (root_focus.children.get(gid)) |child| { 91 // browser-focus the focused native control (text input, submit button 92 // or file picker) so the browser handles typing, Enter-to-submit and 93 // Enter-to-open-the-picker natively. without this the control never 94 // gets focus and Enter falls through to the wasm. 95 switch (child.focus.kind) { 96 .text_input, .text_input_password, .text_area => _focusInput(@intCast(gid)), 97 .custom => |custom| if (std.mem.eql(u8, custom, "submit") or std.mem.startsWith(u8, custom, ui.submit_action_prefix) or std.mem.startsWith(u8, custom, ui.file_input_prefix)) _focusInput(@intCast(gid)), 98 else => {}, 99 } 100 101 // keep the focused widget visible within its own scrollable element. 102 // only fire on an actual focus change so we don't fight the user's 103 // manual scrolling on unrelated ticks. 104 if (gid != last_scrolled_focus_id) { 105 last_scrolled_focus_id = gid; 106 _scrollToFocus(@intCast(gid)); 107 } 108 } 109 } 110 } 111 112 fn onKeyDown(key_code: u32) !void { 113 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 114 const key: Key = switch (key_code) { 115 9 => .tab, 116 13 => .enter, 117 33 => .page_up, 118 34 => .page_down, 119 35 => .end, 120 36 => .home, 121 37 => .arrow_left, 122 38 => .arrow_up, 123 39 => .arrow_right, 124 40 => .arrow_down, 125 else => return, 126 }; 127 const root_focus = root_ptr.getFocus(); 128 const focused_before = root_focus.grandchild_id; 129 130 if (key == .enter) { 131 if (root_focus.grandchild_id) |gid| { 132 // follow a cross-page link 133 if (ui.crossPageLink(root_focus, gid, session.data)) |route| { 134 const url = try route.toUrl(&page_arena); 135 _navigate(url.ptr, @intCast(url.len)); 136 return; 137 } 138 // follow a link to bytes the server serves directly 139 if (ui.rawLink(root_focus, gid)) |url| { 140 _navigate(url.ptr, @intCast(url.len)); 141 return; 142 } 143 } 144 } 145 146 try root_ptr.input(allocator, key, root_focus); 147 const focused_after = root_focus.grandchild_id; 148 if (key != .enter and focused_before == focused_after) return; 149 const focus_id = focused_after orelse return; 150 if (ui.inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 151 } 152 153 fn onMouseClick(focus_id: usize) !void { 154 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 155 // follow a cross-page link 156 if (ui.crossPageLink(root_ptr.getFocus(), focus_id, session.data)) |route| { 157 const url = try route.toUrl(&page_arena); 158 _navigate(url.ptr, @intCast(url.len)); 159 return; 160 } 161 // the grid suppresses the anchor's own navigation, so follow it here 162 if (ui.rawLink(root_ptr.getFocus(), focus_id)) |url| { 163 _navigate(url.ptr, @intCast(url.len)); 164 return; 165 } 166 try setFocus(focus_id); 167 } 168 169 fn setFocus(focus_id: usize) !void { 170 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 171 const root_focus = root_ptr.getFocus(); 172 root_focus.setFocus(focus_id); 173 if (ui.inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 174 } 175 176 fn consoleLog(arg: []const u8) void { 177 _consoleLog(arg.ptr, @intCast(arg.len)); 178 } 179 180 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 181 extern fn _setHtml(arg: [*]const u8, len: u32) void; 182 extern fn _replaceState(arg: [*]const u8, len: u32) void; 183 extern fn _focusInput(focus_id: u32) void; 184 extern fn _navigate(arg: [*]const u8, len: u32) void; 185 extern fn _scrollToFocus(focus_id: u32) void; 186 /// js calls this first to get a wasm pointer it can write the page json into. 187 export fn _alloc(len: u32) ?[*]u8 { 188 const slice = allocator.alloc(u8, len) catch return null; 189 return slice.ptr; 190 } 191 192 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 193 const json = json_ptr[0..json_len]; 194 defer allocator.free(json); 195 init(json, min_height, max_width) catch |err| { 196 var buf: [256]u8 = undefined; 197 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 198 consoleLog(str); 199 }; 200 } 201 202 export fn _tick(min_height: u32, max_width: u32) bool { 203 tick(min_height, max_width) catch |err| { 204 var buf: [256]u8 = undefined; 205 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 206 consoleLog(str); 207 return false; 208 }; 209 return true; 210 } 211 212 export fn _onKeyDown(key_code: u32) void { 213 onKeyDown(key_code) catch |err| { 214 var buf: [256]u8 = undefined; 215 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 216 consoleLog(str); 217 }; 218 } 219 220 export fn _onMouseClick(focus_id: u32) void { 221 onMouseClick(focus_id) catch |err| { 222 var buf: [256]u8 = undefined; 223 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 224 consoleLog(str); 225 }; 226 } 227 228 export fn _setFocus(focus_id: u32) void { 229 setFocus(focus_id) catch |err| { 230 var buf: [256]u8 = undefined; 231 const str = std.fmt.bufPrint(&buf, "setFocus: {}", .{err}) catch unreachable; 232 consoleLog(str); 233 }; 234 } 235 236 // js calls this when the value of a native <input> changes; we replace 237 // the matching TextInput's content with the supplied utf-8 bytes. 238 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 239 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 240 var buf: [256]u8 = undefined; 241 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 242 consoleLog(str); 243 }; 244 } 245 246 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 247 const root_ptr = if (root) |*r| r else return error.NotStarted; 248 const root_focus = root_ptr.getFocus(); 249 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 250 switch (child.focus.kind) { 251 .text_input, .text_input_password, .text_area => {}, 252 else => return error.NotATextInput, 253 } 254 const ti = session.text_inputs.get(focus_id) orelse return error.UnknownFocusId; 255 try ti.setContent(allocator, bytes); 256 } 257