╭ 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 try root_ptr.getFocus().setFocus(focus_id); 110 } 111 112 fn consoleLog(arg: []const u8) void { 113 _consoleLog(arg.ptr, @intCast(arg.len)); 114 } 115 116 fn setHtml(arg: []const u8) void { 117 _setHtml(arg.ptr, @intCast(arg.len)); 118 } 119 120 fn setOverlay(arg: []const u8) void { 121 _setOverlay(arg.ptr, @intCast(arg.len)); 122 } 123 124 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 125 extern fn _setHtml(arg: [*]const u8, len: u32) void; 126 extern fn _setOverlay(arg: [*]const u8, len: u32) void; 127 extern fn _pushState(arg: [*]const u8, len: u32) void; 128 extern fn _focusInput(focus_id: u32) void; 129 130 /// js calls this first to get a wasm pointer it can write the page json into. 131 export fn _alloc(len: u32) ?[*]u8 { 132 const slice = allocator.alloc(u8, len) catch return null; 133 return slice.ptr; 134 } 135 136 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 137 const json = json_ptr[0..json_len]; 138 defer allocator.free(json); 139 init(json, min_height, max_width) catch |err| { 140 var buf: [256]u8 = undefined; 141 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 142 consoleLog(str); 143 }; 144 } 145 146 export fn _tick(min_height: u32, max_width: u32) bool { 147 tick(min_height, max_width) catch |err| { 148 var buf: [256]u8 = undefined; 149 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 150 consoleLog(str); 151 return false; 152 }; 153 return true; 154 } 155 156 export fn _onKeyDown(key_code: u32) void { 157 onKeyDown(key_code) catch |err| { 158 var buf: [256]u8 = undefined; 159 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 160 consoleLog(str); 161 }; 162 } 163 164 export fn _onMouseClick(focus_id: u32) void { 165 onMouseClick(focus_id) catch |err| { 166 var buf: [256]u8 = undefined; 167 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 168 consoleLog(str); 169 }; 170 } 171 172 // js calls this when the value of an overlay <input> changes; we replace 173 // the matching TextInput's content with the supplied utf-8 bytes. 174 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 175 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 176 var buf: [256]u8 = undefined; 177 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 178 consoleLog(str); 179 }; 180 } 181 182 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 183 const root_ptr = if (root) |*r| r else return error.NotStarted; 184 const root_focus = root_ptr.getFocus(); 185 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 186 switch (child.focus.kind) { 187 .text_input, .text_input_password => {}, 188 else => return error.NotATextInput, 189 } 190 const ti: *wgt.TextInput(ui.Widget) = @fieldParentPtr("focus", child.focus); 191 try ti.setContent(allocator, bytes); 192 // mirror the TTY behavior in Login.View.input: editing an input clears 193 // the stale failure flag so the "(invalid)" label doesn't linger after 194 // the user has typed a correction 195 session.data.login_failure = null; 196 } 197