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