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