╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ main_wasm.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
93 // browser-focus the focused overlay control (text input or submit button) 94 // so the browser handles typing and Enter-to-submit natively. without this 95 // the submit button never gets focus and Enter falls through to the wasm. 96 switch (child.focus.kind) { 97 .text_input, .text_input_password => _focusInput(@intCast(gid)), 98 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) _focusInput(@intCast(gid)), 99 else => {}, 100 } 101 102 // keep the focused widget visible within its own scrollable element. 103 // only fire on an actual focus change so we don't fight the user's 104 // manual scrolling on unrelated ticks. 105 if (gid != last_scrolled_focus_id) { 106 last_scrolled_focus_id = gid; 107 _scrollToFocus(@intCast(gid)); 108 } 109 } 110 } 111 } 112 113 fn onKeyDown(key_code: u32) !void { 114 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 115 const key: inp.Key = switch (key_code) { 116 13 => .enter, 117 33 => .page_up, 118 34 => .page_down, 119 35 => .end, 120 36 => .home, 121 37 => .arrow_left, 122 38 => .arrow_up, 123 39 => .arrow_right, 124 40 => .arrow_down, 125 else => return, 126 }; 127 128 if (key == .enter) { 129 if (root_ptr.getFocus().grandchild_id) |gid| { 130 // follow a cross-page link 131 if (ui.crossPageLink(root_ptr.getFocus(), gid, session.data.current_page)) |route| { 132 const url = try route.urlAlloc(&page_arena); 133 navigate(url); 134 return; 135 } 136 } 137 } 138 139 try root_ptr.input(allocator, key, root_ptr.getFocus()); 140 } 141 142 fn onMouseClick(focus_id: usize) !void { 143 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 144 // follow a cross-page link 145 if (ui.crossPageLink(root_ptr.getFocus(), focus_id, session.data.current_page)) |route| { 146 const url = try route.urlAlloc(&page_arena); 147 navigate(url); 148 return; 149 } 150 root_ptr.getFocus().setFocus(focus_id); 151 } 152 153 fn setFocus(focus_id: usize) !void { 154 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 155 root_ptr.getFocus().setFocus(focus_id); 156 } 157 158 fn consoleLog(arg: []const u8) void { 159 _consoleLog(arg.ptr, @intCast(arg.len)); 160 } 161 162 fn setHtml(arg: []const u8) void { 163 _setHtml(arg.ptr, @intCast(arg.len)); 164 } 165 166 fn setOverlay(arg: []const u8) void { 167 _setOverlay(arg.ptr, @intCast(arg.len)); 168 } 169 170 fn navigate(arg: []const u8) void { 171 _navigate(arg.ptr, @intCast(arg.len)); 172 } 173 174 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 175 extern fn _setHtml(arg: [*]const u8, len: u32) void; 176 extern fn _setOverlay(arg: [*]const u8, len: u32) void; 177 extern fn _replaceState(arg: [*]const u8, len: u32) void; 178 extern fn _focusInput(focus_id: u32) void; 179 extern fn _navigate(arg: [*]const u8, len: u32) void; 180 extern fn _scrollToFocus(focus_id: u32) void; 181 182 /// js calls this first to get a wasm pointer it can write the page json into. 183 export fn _alloc(len: u32) ?[*]u8 { 184 const slice = allocator.alloc(u8, len) catch return null; 185 return slice.ptr; 186 } 187 188 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 189 const json = json_ptr[0..json_len]; 190 defer allocator.free(json); 191 init(json, min_height, max_width) catch |err| { 192 var buf: [256]u8 = undefined; 193 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 194 consoleLog(str); 195 }; 196 } 197 198 export fn _tick(min_height: u32, max_width: u32) bool { 199 tick(min_height, max_width) catch |err| { 200 var buf: [256]u8 = undefined; 201 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 202 consoleLog(str); 203 return false; 204 }; 205 return true; 206 } 207 208 export fn _onKeyDown(key_code: u32) void { 209 onKeyDown(key_code) catch |err| { 210 var buf: [256]u8 = undefined; 211 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 212 consoleLog(str); 213 }; 214 } 215 216 export fn _onMouseClick(focus_id: u32) void { 217 onMouseClick(focus_id) catch |err| { 218 var buf: [256]u8 = undefined; 219 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 220 consoleLog(str); 221 }; 222 } 223 224 export fn _setFocus(focus_id: u32) void { 225 setFocus(focus_id) catch |err| { 226 var buf: [256]u8 = undefined; 227 const str = std.fmt.bufPrint(&buf, "setFocus: {}", .{err}) catch unreachable; 228 consoleLog(str); 229 }; 230 } 231 232 // js calls this when the value of an overlay <input> changes; we replace 233 // the matching TextInput's content with the supplied utf-8 bytes. 234 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 235 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 236 var buf: [256]u8 = undefined; 237 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 238 consoleLog(str); 239 }; 240 } 241 242 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 243 const root_ptr = if (root) |*r| r else return error.NotStarted; 244 const root_focus = root_ptr.getFocus(); 245 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 246 switch (child.focus.kind) { 247 .text_input, .text_input_password => {}, 248 else => return error.NotATextInput, 249 } 250 const ti = session.text_inputs.get(focus_id) orelse return error.UnknownFocusId; 251 try ti.setContent(allocator, bytes); 252 // mirror the TTY behavior in Login.View.input: editing an input clears 253 // the stale failure flag so the "(invalid)" label doesn't linger after 254 // the user has typed a correction 255 session.data.login_failure = null; 256 } 257