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