╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ main_wasm.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
22 23 // alloc_always so the parsed strings live entirely inside the arena 24 // and don't borrow from the caller's json buffer. 25 snapshot = try std.json.parseFromSliceLeaky(ui.Snapshot, page_arena.allocator(), json, .{ 26 .allocate = .alloc_always, 27 }); 28 29 // adopt the server's session view wholesale — one assignment instead of 30 // hand-restoring each field by reaching into the page's data tree. 31 session = .{ 32 .data = snapshot.session, 33 .arena = &page_arena, 34 // one render per page load (init resets the arena), so page-scoped and 35 // session-scoped allocations share the same arena here. 36 .page_arena = &page_arena, 37 }; 38 39 var next_root = try ui.initRoot(allocator, &snapshot.page, &session); 40 errdefer next_root.deinit(allocator); 41 42 if (root) |*old_root| old_root.deinit(allocator); 43 root = next_root; 44 45 try tick(min_height, max_width); 46 } 47 48 fn tick(min_height: u32, max_width: u32) !void { 49 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 50 51 // apply actions queued during input. the wasm path has no repo, so this is 52 // in-memory only; logged-in web persistence goes through the /ansi POST. 53 session.applyPending(); 54 55 try root_ptr.build(allocator, .{ 56 // min_height lets the TUI fill the viewport when its content is 57 // short; max height stays null so taller content extends downward 58 // and the browser scrolls. 59 .min_size = .{ .width = null, .height = min_height }, 60 .max_size = .{ .width = max_width, .height = null }, 61 }, root_ptr.getFocus()); 62 63 // mirror the page Header settled on into the browser URL. _pushState 64 // is idempotent on the JS side (it bails when pathname already matches) 65 // so the first tick after _init is a no-op even though we always send 66 // something through. 67 const current_page = session.data.current_page; 68 const should_push = if (last_pushed_page_maybe) |lp| !lp.eql(current_page) else true; 69 if (should_push) { 70 last_pushed_page_maybe = current_page; 71 const url = try current_page.urlAlloc(&page_arena); 72 _pushState(url.ptr, @intCast(url.len)); 73 } 74 75 const html = try web.generateHtml(allocator, root_ptr); 76 defer allocator.free(html); 77 setHtml(html); 78 79 // emit the overlay (form + inputs + button) on every tick so the wasm 80 // layout drives positions, not just the server's initial render. JS 81 // diffs the result against the previous overlay; an unchanged overlay 82 // leaves the live <form> alone — crucial since wiping it mid-click 83 // would detach the submit button before the browser can dispatch the 84 // form submission. 85 const overlay = try web.generateOverlay(allocator, root_ptr); 86 defer allocator.free(overlay); 87 setOverlay(overlay); 88 89 // browser-focus the focused overlay control (text input or submit button) 90 // so the browser handles typing and Enter-to-submit natively. without this 91 // the submit button never gets focus and Enter falls through to the wasm. 92 const root_focus = root_ptr.getFocus(); 93 if (root_focus.grandchild_id) |gid| { 94 if (root_focus.children.get(gid)) |child| { 95 switch (child.focus.kind) { 96 .text_input, .text_input_password => _focusInput(@intCast(gid)), 97 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) _focusInput(@intCast(gid)), 98 else => {}, 99 } 100 } 101 } 102 } 103 104 fn onKeyDown(key_code: u32) !void { 105 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 106 const key: inp.Key = switch (key_code) { 107 13 => .enter, 108 33 => .page_up, 109 34 => .page_down, 110 35 => .end, 111 36 => .home, 112 37 => .arrow_left, 113 38 => .arrow_up, 114 39 => .arrow_right, 115 40 => .arrow_down, 116 else => return, 117 }; 118 119 if (key == .enter) { 120 if (root_ptr.getFocus().grandchild_id) |gid| { 121 // follow a cross-page link 122 if (ui.crossPageLink(root_ptr.getFocus(), gid, session.data.current_page)) |route| { 123 const url = try route.urlAlloc(&page_arena); 124 navigate(url); 125 return; 126 } 127 } 128 } 129 130 try root_ptr.input(allocator, key, root_ptr.getFocus()); 131 } 132 133 fn onMouseClick(focus_id: usize) !void { 134 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 135 // follow a cross-page link 136 if (ui.crossPageLink(root_ptr.getFocus(), focus_id, session.data.current_page)) |route| { 137 const url = try route.urlAlloc(&page_arena); 138 navigate(url); 139 return; 140 } 141 try root_ptr.getFocus().setFocus(focus_id); 142 } 143 144 fn consoleLog(arg: []const u8) void { 145 _consoleLog(arg.ptr, @intCast(arg.len)); 146 } 147 148 fn setHtml(arg: []const u8) void { 149 _setHtml(arg.ptr, @intCast(arg.len)); 150 } 151 152 fn setOverlay(arg: []const u8) void { 153 _setOverlay(arg.ptr, @intCast(arg.len)); 154 } 155 156 fn navigate(arg: []const u8) void { 157 _navigate(arg.ptr, @intCast(arg.len)); 158 } 159 160 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 161 extern fn _setHtml(arg: [*]const u8, len: u32) void; 162 extern fn _setOverlay(arg: [*]const u8, len: u32) void; 163 extern fn _pushState(arg: [*]const u8, len: u32) void; 164 extern fn _focusInput(focus_id: u32) void; 165 extern fn _navigate(arg: [*]const u8, len: u32) void; 166 167 /// js calls this first to get a wasm pointer it can write the page json into. 168 export fn _alloc(len: u32) ?[*]u8 { 169 const slice = allocator.alloc(u8, len) catch return null; 170 return slice.ptr; 171 } 172 173 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 174 const json = json_ptr[0..json_len]; 175 defer allocator.free(json); 176 init(json, min_height, max_width) catch |err| { 177 var buf: [256]u8 = undefined; 178 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 179 consoleLog(str); 180 }; 181 } 182 183 export fn _tick(min_height: u32, max_width: u32) bool { 184 tick(min_height, max_width) catch |err| { 185 var buf: [256]u8 = undefined; 186 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 187 consoleLog(str); 188 return false; 189 }; 190 return true; 191 } 192 193 export fn _onKeyDown(key_code: u32) void { 194 onKeyDown(key_code) catch |err| { 195 var buf: [256]u8 = undefined; 196 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 197 consoleLog(str); 198 }; 199 } 200 201 export fn _onMouseClick(focus_id: u32) void { 202 onMouseClick(focus_id) catch |err| { 203 var buf: [256]u8 = undefined; 204 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 205 consoleLog(str); 206 }; 207 } 208 209 // js calls this when the value of an overlay <input> changes; we replace 210 // the matching TextInput's content with the supplied utf-8 bytes. 211 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 212 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 213 var buf: [256]u8 = undefined; 214 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 215 consoleLog(str); 216 }; 217 } 218 219 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 220 const root_ptr = if (root) |*r| r else return error.NotStarted; 221 const root_focus = root_ptr.getFocus(); 222 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 223 switch (child.focus.kind) { 224 .text_input, .text_input_password => {}, 225 else => return error.NotATextInput, 226 } 227 const ti: *wgt.TextInput(ui.Widget) = @fieldParentPtr("focus", child.focus); 228 try ti.setContent(allocator, bytes); 229 // mirror the TTY behavior in Login.View.input: editing an input clears 230 // the stale failure flag so the "(invalid)" label doesn't linger after 231 // the user has typed a correction 232 session.data.login_failure = null; 233 } 234