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