╭ 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, &session); 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, &session); 75 defer allocator.free(overlay); 76 setOverlay(overlay); 77 } 78 79 fn onKeyDown(key_code: u32) !void { 80 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 81 const key: inp.Key = switch (key_code) { 82 13 => .enter, 83 33 => .page_up, 84 34 => .page_down, 85 35 => .end, 86 36 => .home, 87 37 => .arrow_left, 88 38 => .arrow_up, 89 39 => .arrow_right, 90 40 => .arrow_down, 91 else => return, 92 }; 93 try root_ptr.input(allocator, key, root_ptr.getFocus()); 94 } 95 96 fn onMouseClick(focus_id: usize) !void { 97 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 98 try root_ptr.getFocus().setFocus(focus_id); 99 } 100 101 fn consoleLog(arg: []const u8) void { 102 _consoleLog(arg.ptr, @intCast(arg.len)); 103 } 104 105 fn setHtml(arg: []const u8) void { 106 _setHtml(arg.ptr, @intCast(arg.len)); 107 } 108 109 fn setOverlay(arg: []const u8) void { 110 _setOverlay(arg.ptr, @intCast(arg.len)); 111 } 112 113 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 114 extern fn _setHtml(arg: [*]const u8, len: u32) void; 115 extern fn _setOverlay(arg: [*]const u8, len: u32) void; 116 extern fn _pushState(arg: [*]const u8, len: u32) void; 117 118 /// js calls this first to get a wasm pointer it can write the page json into. 119 export fn _alloc(len: u32) ?[*]u8 { 120 const slice = allocator.alloc(u8, len) catch return null; 121 return slice.ptr; 122 } 123 124 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 125 const json = json_ptr[0..json_len]; 126 defer allocator.free(json); 127 init(json, min_height, max_width) catch |err| { 128 var buf: [256]u8 = undefined; 129 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 130 consoleLog(str); 131 }; 132 } 133 134 export fn _tick(min_height: u32, max_width: u32) bool { 135 tick(min_height, max_width) catch |err| { 136 var buf: [256]u8 = undefined; 137 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 138 consoleLog(str); 139 return false; 140 }; 141 return true; 142 } 143 144 export fn _onKeyDown(key_code: u32) void { 145 onKeyDown(key_code) catch |err| { 146 var buf: [256]u8 = undefined; 147 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 148 consoleLog(str); 149 }; 150 } 151 152 export fn _onMouseClick(focus_id: u32) void { 153 onMouseClick(focus_id) catch |err| { 154 var buf: [256]u8 = undefined; 155 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 156 consoleLog(str); 157 }; 158 } 159 160 // js calls this when the value of an overlay <input> changes; we replace 161 // the matching TextInput's content with the supplied utf-8 bytes. 162 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 163 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 164 var buf: [256]u8 = undefined; 165 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 166 consoleLog(str); 167 }; 168 } 169 170 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 171 const root_ptr = if (root) |*r| r else return error.NotStarted; 172 const root_focus = root_ptr.getFocus(); 173 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 174 switch (child.focus.kind) { 175 .text_input, .text_input_password => {}, 176 else => return error.NotATextInput, 177 } 178 const ti: *wgt.TextInput(ui.Widget) = @fieldParentPtr("focus", child.focus); 179 try ti.setContent(allocator, bytes); 180 // mirror the TTY behavior in Login.View.input: editing an input clears 181 // the stale failure flag so the "(invalid)" label doesn't linger after 182 // the user has typed a correction 183 session.data.login_failure = null; 184 } 185