╭ 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 Key = xitui.input.Key; 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 // bind the UI to the browser viewport (cols x rows) like the terminal: 56 // min == max height fills it exactly, and each Scroll clips to its 57 // viewport while handing its full content to a native-scrollable element. 58 // a full-height page instead grows to its content and the browser scrolls 59 // the whole page, which the overlay's absolute positions track. 60 const max_height: ?u32 = if (session.data.current_page.fullHeight()) null else min_height; 61 try root_ptr.build(allocator, .{ 62 .min_size = .{ .width = null, .height = min_height }, 63 .max_size = .{ .width = max_width, .height = max_height }, 64 }, root_ptr.getFocus()); 65 66 // mirror the page Header settled on into the browser URL. these are 67 // passive same-page changes, like moving down the commits list, so we 68 // replace the URL in place rather than pushing a history entry. real 69 // cross-page navigations go through _navigate. 70 const current_page = session.data.current_page; 71 const should_push = if (last_pushed_page_maybe) |lp| !lp.eql(current_page) else true; 72 if (should_push) { 73 last_pushed_page_maybe = current_page; 74 const url = try current_page.toUrl(&page_arena); 75 _replaceState(url.ptr, @intCast(url.len)); 76 } 77 78 const html = try web.generateHtml(allocator, root_ptr); 79 defer allocator.free(html); 80 _setHtml(html.ptr, @intCast(html.len)); 81 82 // emit the overlay (form + inputs + button) on every tick so the wasm 83 // layout drives positions, not just the server's initial render. JS 84 // diffs the result against the previous overlay; an unchanged overlay 85 // leaves the live <form> alone — crucial since wiping it mid-click 86 // would detach the submit button before the browser can dispatch the 87 // form submission. 88 const overlay = try web.generateOverlay(allocator, root_ptr, &session); 89 defer allocator.free(overlay); 90 _setOverlay(overlay.ptr, @intCast(overlay.len)); 91 92 const root_focus = root_ptr.getFocus(); 93 if (root_focus.grandchild_id) |gid| { 94 if (root_focus.children.get(gid)) |child| { 95 // browser-focus the focused overlay control (text input, submit button 96 // or file picker) so the browser handles typing, Enter-to-submit and 97 // Enter-to-open-the-picker natively. without this the control never 98 // gets focus and Enter falls through to the wasm. 99 switch (child.focus.kind) { 100 .text_input, .text_input_password, .text_area => _focusInput(@intCast(gid)), 101 .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)), 102 else => {}, 103 } 104 105 // keep the focused widget visible within its own scrollable element. 106 // only fire on an actual focus change so we don't fight the user's 107 // manual scrolling on unrelated ticks. 108 if (gid != last_scrolled_focus_id) { 109 last_scrolled_focus_id = gid; 110 _scrollToFocus(@intCast(gid)); 111 } 112 } 113 } 114 } 115 116 fn onKeyDown(key_code: u32) !void { 117 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 118 const key: Key = switch (key_code) { 119 13 => .enter, 120 33 => .page_up, 121 34 => .page_down, 122 35 => .end, 123 36 => .home, 124 37 => .arrow_left, 125 38 => .arrow_up, 126 39 => .arrow_right, 127 40 => .arrow_down, 128 else => return, 129 }; 130 131 if (key == .enter) { 132 if (root_ptr.getFocus().grandchild_id) |gid| { 133 // follow a cross-page link 134 if (ui.crossPageLink(root_ptr.getFocus(), gid, session.data)) |route| { 135 const url = try route.toUrl(&page_arena); 136 _navigate(url.ptr, @intCast(url.len)); 137 return; 138 } 139 // follow a link to bytes the server serves directly 140 if (ui.rawLink(root_ptr.getFocus(), gid)) |url| { 141 _navigate(url.ptr, @intCast(url.len)); 142 return; 143 } 144 } 145 } 146 147 try root_ptr.input(allocator, key, root_ptr.getFocus()); 148 } 149 150 fn onMouseClick(focus_id: usize) !void { 151 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 152 // follow a cross-page link 153 if (ui.crossPageLink(root_ptr.getFocus(), focus_id, session.data)) |route| { 154 const url = try route.toUrl(&page_arena); 155 _navigate(url.ptr, @intCast(url.len)); 156 return; 157 } 158 // the grid suppresses the anchor's own navigation, so follow it here 159 if (ui.rawLink(root_ptr.getFocus(), focus_id)) |url| { 160 _navigate(url.ptr, @intCast(url.len)); 161 return; 162 } 163 root_ptr.getFocus().setFocus(focus_id); 164 } 165 166 fn setFocus(focus_id: usize) !void { 167 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 168 root_ptr.getFocus().setFocus(focus_id); 169 } 170 171 fn consoleLog(arg: []const u8) void { 172 _consoleLog(arg.ptr, @intCast(arg.len)); 173 } 174 175 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 176 extern fn _setHtml(arg: [*]const u8, len: u32) void; 177 extern fn _setOverlay(arg: [*]const u8, len: u32) void; 178 extern fn _replaceState(arg: [*]const u8, len: u32) void; 179 extern fn _focusInput(focus_id: u32) void; 180 extern fn _navigate(arg: [*]const u8, len: u32) void; 181 extern fn _scrollToFocus(focus_id: u32) void; 182 /// js calls this first to get a wasm pointer it can write the page json into. 183 export fn _alloc(len: u32) ?[*]u8 { 184 const slice = allocator.alloc(u8, len) catch return null; 185 return slice.ptr; 186 } 187 188 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 189 const json = json_ptr[0..json_len]; 190 defer allocator.free(json); 191 init(json, min_height, max_width) catch |err| { 192 var buf: [256]u8 = undefined; 193 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 194 consoleLog(str); 195 }; 196 } 197 198 export fn _tick(min_height: u32, max_width: u32) bool { 199 tick(min_height, max_width) catch |err| { 200 var buf: [256]u8 = undefined; 201 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 202 consoleLog(str); 203 return false; 204 }; 205 return true; 206 } 207 208 export fn _onKeyDown(key_code: u32) void { 209 onKeyDown(key_code) catch |err| { 210 var buf: [256]u8 = undefined; 211 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 212 consoleLog(str); 213 }; 214 } 215 216 export fn _onMouseClick(focus_id: u32) void { 217 onMouseClick(focus_id) catch |err| { 218 var buf: [256]u8 = undefined; 219 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 220 consoleLog(str); 221 }; 222 } 223 224 export fn _setFocus(focus_id: u32) void { 225 setFocus(focus_id) catch |err| { 226 var buf: [256]u8 = undefined; 227 const str = std.fmt.bufPrint(&buf, "setFocus: {}", .{err}) catch unreachable; 228 consoleLog(str); 229 }; 230 } 231 232 // js calls this when the value of an overlay <input> changes; we replace 233 // the matching TextInput's content with the supplied utf-8 bytes. 234 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 235 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 236 var buf: [256]u8 = undefined; 237 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 238 consoleLog(str); 239 }; 240 } 241 242 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 243 const root_ptr = if (root) |*r| r else return error.NotStarted; 244 const root_focus = root_ptr.getFocus(); 245 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 246 switch (child.focus.kind) { 247 .text_input, .text_input_password, .text_area => {}, 248 else => return error.NotATextInput, 249 } 250 const ti = session.text_inputs.get(focus_id) orelse return error.UnknownFocusId; 251 try ti.setContent(allocator, bytes); 252 // mirror the TTY behavior in Login.View.input: editing an input clears 253 // the stale failure flag so the "(invalid)" label doesn't linger after 254 // the user has typed a correction 255 session.data.login_failure = null; 256 } 257