╭ 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 var last_scrolled_focus_id: ?usize = null; // last focused widget we asked JS to scroll into view 17 18 fn init(json: []const u8, min_height: u32, max_width: u32) !void { 19 _ = page_arena.reset(.free_all); 20 // drop the last-pushed page so the next tick pushes the freshly-loaded 21 // 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 // 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 56 try root_ptr.build(allocator, .{ 57 // min_height lets the TUI fill the viewport when its content is 58 // short; max height stays null so taller content extends downward 59 // and the browser scrolls. 60 .min_size = .{ .width = null, .height = min_height }, 61 .max_size = .{ .width = max_width, .height = null }, 62 }, root_ptr.getFocus()); 63 64 // mirror the page Header settled on into the browser URL. _pushState 65 // is idempotent on the JS side (it bails when pathname already matches) 66 // so the first tick after _init is a no-op even though we always send 67 // something through. 68 const current_page = session.data.current_page; 69 const should_push = if (last_pushed_page_maybe) |lp| !lp.eql(current_page) else true; 70 if (should_push) { 71 last_pushed_page_maybe = current_page; 72 const url = try current_page.urlAlloc(&page_arena); 73 _pushState(url.ptr, @intCast(url.len)); 74 } 75 76 const html = try web.generateHtml(allocator, root_ptr); 77 defer allocator.free(html); 78 setHtml(html); 79 80 // emit the overlay (form + inputs + button) on every tick so the wasm 81 // layout drives positions, not just the server's initial render. JS 82 // diffs the result against the previous overlay; an unchanged overlay 83 // leaves the live <form> alone — crucial since wiping it mid-click 84 // would detach the submit button before the browser can dispatch the 85 // form submission. 86 const overlay = try web.generateOverlay(allocator, root_ptr); 87 defer allocator.free(overlay); 88 setOverlay(overlay); 89 90 const root_focus = root_ptr.getFocus(); 91 if (root_focus.grandchild_id) |gid| { 92 if (root_focus.children.get(gid)) |child| { 93 // browser-focus the focused overlay control (text input or submit button) 94 // so the browser handles typing and Enter-to-submit natively. without this 95 // the submit button never gets focus and Enter falls through to the wasm. 96 switch (child.focus.kind) { 97 .text_input, .text_input_password => _focusInput(@intCast(gid)), 98 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) _focusInput(@intCast(gid)), 99 else => {}, 100 } 101 102 // keep the focused widget within the browser viewport. only fire on 103 // an actual focus change so we don't fight the user's manual 104 // scrolling on unrelated ticks. 105 if (gid != last_scrolled_focus_id) { 106 last_scrolled_focus_id = gid; 107 const r = child.rect; 108 _scrollToRect(@intCast(r.x), @intCast(r.y), @intCast(r.size.width), @intCast(r.size.height)); 109 } 110 } 111 } 112 } 113 114 fn onKeyDown(key_code: u32) !void { 115 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 116 const key: inp.Key = switch (key_code) { 117 13 => .enter, 118 33 => .page_up, 119 34 => .page_down, 120 35 => .end, 121 36 => .home, 122 37 => .arrow_left, 123 38 => .arrow_up, 124 39 => .arrow_right, 125 40 => .arrow_down, 126 else => return, 127 }; 128 129 if (key == .enter) { 130 if (root_ptr.getFocus().grandchild_id) |gid| { 131 // follow a cross-page link 132 if (ui.crossPageLink(root_ptr.getFocus(), gid, session.data.current_page)) |route| { 133 const url = try route.urlAlloc(&page_arena); 134 navigate(url); 135 return; 136 } 137 } 138 } 139 140 try root_ptr.input(allocator, key, root_ptr.getFocus()); 141 } 142 143 fn onMouseClick(focus_id: usize) !void { 144 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 145 // follow a cross-page link 146 if (ui.crossPageLink(root_ptr.getFocus(), focus_id, session.data.current_page)) |route| { 147 const url = try route.urlAlloc(&page_arena); 148 navigate(url); 149 return; 150 } 151 try root_ptr.getFocus().setFocus(focus_id); 152 } 153 154 fn consoleLog(arg: []const u8) void { 155 _consoleLog(arg.ptr, @intCast(arg.len)); 156 } 157 158 fn setHtml(arg: []const u8) void { 159 _setHtml(arg.ptr, @intCast(arg.len)); 160 } 161 162 fn setOverlay(arg: []const u8) void { 163 _setOverlay(arg.ptr, @intCast(arg.len)); 164 } 165 166 fn navigate(arg: []const u8) void { 167 _navigate(arg.ptr, @intCast(arg.len)); 168 } 169 170 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 171 extern fn _setHtml(arg: [*]const u8, len: u32) void; 172 extern fn _setOverlay(arg: [*]const u8, len: u32) void; 173 extern fn _pushState(arg: [*]const u8, len: u32) void; 174 extern fn _focusInput(focus_id: u32) void; 175 extern fn _navigate(arg: [*]const u8, len: u32) void; 176 extern fn _scrollToRect(x: u32, y: u32, width: u32, height: u32) void; 177 178 /// js calls this first to get a wasm pointer it can write the page json into. 179 export fn _alloc(len: u32) ?[*]u8 { 180 const slice = allocator.alloc(u8, len) catch return null; 181 return slice.ptr; 182 } 183 184 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 185 const json = json_ptr[0..json_len]; 186 defer allocator.free(json); 187 init(json, min_height, max_width) catch |err| { 188 var buf: [256]u8 = undefined; 189 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 190 consoleLog(str); 191 }; 192 } 193 194 export fn _tick(min_height: u32, max_width: u32) bool { 195 tick(min_height, max_width) catch |err| { 196 var buf: [256]u8 = undefined; 197 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 198 consoleLog(str); 199 return false; 200 }; 201 return true; 202 } 203 204 export fn _onKeyDown(key_code: u32) void { 205 onKeyDown(key_code) catch |err| { 206 var buf: [256]u8 = undefined; 207 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 208 consoleLog(str); 209 }; 210 } 211 212 export fn _onMouseClick(focus_id: u32) void { 213 onMouseClick(focus_id) catch |err| { 214 var buf: [256]u8 = undefined; 215 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 216 consoleLog(str); 217 }; 218 } 219 220 // js calls this when the value of an overlay <input> changes; we replace 221 // the matching TextInput's content with the supplied utf-8 bytes. 222 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 223 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 224 var buf: [256]u8 = undefined; 225 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 226 consoleLog(str); 227 }; 228 } 229 230 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 231 const root_ptr = if (root) |*r| r else return error.NotStarted; 232 const root_focus = root_ptr.getFocus(); 233 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 234 switch (child.focus.kind) { 235 .text_input, .text_input_password => {}, 236 else => return error.NotATextInput, 237 } 238 const ti: *wgt.TextInput(ui.Widget) = @fieldParentPtr("focus", child.focus); 239 try ti.setContent(allocator, bytes); 240 // mirror the TTY behavior in Login.View.input: editing an input clears 241 // the stale failure flag so the "(invalid)" label doesn't linger after 242 // the user has typed a correction 243 session.data.login_failure = null; 244 } 245