╭ 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 36 fn start(json: []const u8, min_height: u32, max_width: u32) !void { 37 if (page_arena) |*a| a.deinit(); 38 page_arena = std.heap.ArenaAllocator.init(allocator); 39 40 // alloc_always so the parsed strings live entirely inside the arena 41 // and don't borrow from the caller's json buffer. 42 snapshot = try std.json.parseFromSliceLeaky(ui.Snapshot, (page_arena orelse unreachable).allocator(), json, .{ 43 .allocate = .alloc_always, 44 }); 45 46 // adopt the server's session view wholesale — one assignment instead of 47 // hand-restoring each field by reaching into the page's data tree. 48 session.data = (snapshot orelse unreachable).session; 49 50 var next_root = try ui.initRoot(allocator, &(snapshot orelse unreachable).page, &session); 51 errdefer next_root.deinit(allocator); 52 53 if (root) |*old_root| old_root.deinit(allocator); 54 root = next_root; 55 56 try tick(min_height, max_width); 57 } 58 59 fn tick(min_height: u32, max_width: u32) !void { 60 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 61 try root_ptr.build(allocator, .{ 62 // min_height lets the TUI fill the viewport when its content is 63 // short; max height stays null so taller content extends downward 64 // and the browser scrolls. 65 .min_size = .{ .width = null, .height = min_height }, 66 .max_size = .{ .width = max_width, .height = null }, 67 }, root_ptr.getFocus()); 68 try updateHtml(); 69 } 70 71 fn onKeyDown(key_code: u32) !void { 72 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 73 const key: inp.Key = switch (key_code) { 74 13 => .enter, 75 33 => .page_up, 76 34 => .page_down, 77 35 => .end, 78 36 => .home, 79 37 => .arrow_left, 80 38 => .arrow_up, 81 39 => .arrow_right, 82 40 => .arrow_down, 83 else => return, 84 }; 85 try root_ptr.input(allocator, key, root_ptr.getFocus()); 86 } 87 88 fn onMouseClick(focus_id: usize) !void { 89 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 90 try root_ptr.getFocus().setFocus(focus_id); 91 } 92 93 fn consoleLog(arg: []const u8) void { 94 _consoleLog(arg.ptr, @intCast(arg.len)); 95 } 96 97 fn setHtml(arg: []const u8) void { 98 _setHtml(arg.ptr, @intCast(arg.len)); 99 } 100 101 fn setOverlay(arg: []const u8) void { 102 _setOverlay(arg.ptr, @intCast(arg.len)); 103 } 104 105 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 106 extern fn _setHtml(arg: [*]const u8, len: u32) void; 107 extern fn _setOverlay(arg: [*]const u8, len: u32) void; 108 109 /// js calls this first to get a wasm pointer it can write the page json into. 110 export fn _alloc(len: u32) ?[*]u8 { 111 const slice = allocator.alloc(u8, len) catch return null; 112 return slice.ptr; 113 } 114 115 export fn _start(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 116 const json = json_ptr[0..json_len]; 117 defer allocator.free(json); 118 start(json, min_height, max_width) catch |err| { 119 var buf: [256]u8 = undefined; 120 const str = std.fmt.bufPrint(&buf, "start: {}", .{err}) catch unreachable; 121 consoleLog(str); 122 }; 123 } 124 125 export fn _tick(min_height: u32, max_width: u32) bool { 126 tick(min_height, max_width) catch |err| { 127 var buf: [256]u8 = undefined; 128 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 129 consoleLog(str); 130 return false; 131 }; 132 return true; 133 } 134 135 export fn _onKeyDown(key_code: u32) void { 136 onKeyDown(key_code) catch |err| { 137 var buf: [256]u8 = undefined; 138 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 139 consoleLog(str); 140 }; 141 } 142 143 export fn _onMouseClick(focus_id: u32) void { 144 onMouseClick(focus_id) catch |err| { 145 var buf: [256]u8 = undefined; 146 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 147 consoleLog(str); 148 }; 149 } 150 151 // js calls this when the value of an overlay <input> changes; we replace 152 // the matching TextInput's content with the supplied utf-8 bytes. 153 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 154 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 155 var buf: [256]u8 = undefined; 156 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 157 consoleLog(str); 158 }; 159 } 160 161 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 162 const root_ptr = if (root) |*r| r else return error.NotStarted; 163 const root_focus = root_ptr.getFocus(); 164 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 165 switch (child.focus.kind) { 166 .text_input, .text_input_password => {}, 167 else => return error.NotATextInput, 168 } 169 const ti: *wgt.TextInput(ui.Widget) = @fieldParentPtr("focus", child.focus); 170 try ti.setContent(allocator, bytes); 171 // mirror the TTY behavior in Login.View.input: editing an input clears 172 // the stale failure flag so the "(invalid)" label doesn't linger after 173 // the user has typed a correction 174 session.data.login_failure = null; 175 } 176