╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ main_wasm.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
87 _setHtml(html.ptr, @intCast(html.len)); 88 89 const root_focus = root_ptr.getFocus(); 90 if (root_focus.grandchild_id) |gid| { 91 if (root_focus.children.get(gid)) |child| { 92 // browser-focus the focused native control (text input, submit button 93 // or file picker) so the browser handles typing, Enter-to-submit and 94 // Enter-to-open-the-picker natively. without this the control never 95 // gets focus and Enter falls through to the wasm. 96 switch (child.focus.kind) { 97 .text_input, .text_input_password, .text_area => _focusInput(@intCast(gid)), 98 .custom => |custom| if (std.mem.eql(u8, custom, "submit") or std.mem.startsWith(u8, custom, ui.submit_action_prefix) or std.mem.startsWith(u8, custom, ui.file_input_prefix)) _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: Key = switch (key_code) { 116 9 => .tab, 117 13 => .enter, 118 33 => .page_up, 119 34 => .page_down, 120 35 => .end, 121 36 => .home, 122 37 => .arrow_left, 123 38 => .arrow_up, 124 39 => .arrow_right, 125 40 => .arrow_down, 126 else => return, 127 }; 128 const root_focus = root_ptr.getFocus(); 129 const focused_before = root_focus.grandchild_id; 130 131 if (key == .enter) { 132 if (root_focus.grandchild_id) |gid| { 133 // follow a cross-page link 134 if (ui.crossPageLink(root_focus, gid, session.data)) |route| { 135 const url = try route.toUrl(&page_arena); 136 _navigate(url.ptr, @intCast(url.len)); 137 return; 138 } 139 // follow a link to bytes the server serves directly 140 if (ui.rawLink(root_focus, gid)) |url| { 141 _navigate(url.ptr, @intCast(url.len)); 142 return; 143 } 144 } 145 } 146 147 try root_ptr.input(allocator, key, root_focus); 148 const focused_after = root_focus.grandchild_id; 149 if (key != .enter and focused_before == focused_after) return; 150 const focus_id = focused_after orelse return; 151 if (ui.inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 152 } 153 154 fn onMouseClick(focus_id: usize) !void { 155 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 156 // follow a cross-page link 157 if (ui.crossPageLink(root_ptr.getFocus(), focus_id, session.data)) |route| { 158 const url = try route.toUrl(&page_arena); 159 _navigate(url.ptr, @intCast(url.len)); 160 return; 161 } 162 // the grid suppresses the anchor's own navigation, so follow it here 163 if (ui.rawLink(root_ptr.getFocus(), focus_id)) |url| { 164 _navigate(url.ptr, @intCast(url.len)); 165 return; 166 } 167 try setFocus(focus_id); 168 } 169 170 fn setFocus(focus_id: usize) !void { 171 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 172 const root_focus = root_ptr.getFocus(); 173 root_focus.setFocus(focus_id); 174 if (ui.inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 175 } 176 177 fn consoleLog(arg: []const u8) void { 178 _consoleLog(arg.ptr, @intCast(arg.len)); 179 } 180 181 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 182 extern fn _setHtml(arg: [*]const u8, len: u32) void; 183 extern fn _replaceState(arg: [*]const u8, len: u32) void; 184 extern fn _focusInput(focus_id: u32) void; 185 extern fn _navigate(arg: [*]const u8, len: u32) void; 186 extern fn _scrollToFocus(focus_id: u32) void; 187 /// js calls this first to get a wasm pointer it can write the page json into. 188 export fn _alloc(len: u32) ?[*]u8 { 189 const slice = allocator.alloc(u8, len) catch return null; 190 return slice.ptr; 191 } 192 193 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 194 const json = json_ptr[0..json_len]; 195 defer allocator.free(json); 196 init(json, min_height, max_width) catch |err| { 197 var buf: [256]u8 = undefined; 198 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 199 consoleLog(str); 200 }; 201 } 202 203 export fn _tick(min_height: u32, max_width: u32) bool { 204 tick(min_height, max_width) catch |err| { 205 var buf: [256]u8 = undefined; 206 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 207 consoleLog(str); 208 return false; 209 }; 210 return true; 211 } 212 213 export fn _onKeyDown(key_code: u32) void { 214 onKeyDown(key_code) catch |err| { 215 var buf: [256]u8 = undefined; 216 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 217 consoleLog(str); 218 }; 219 } 220 221 export fn _onMouseClick(focus_id: u32) void { 222 onMouseClick(focus_id) catch |err| { 223 var buf: [256]u8 = undefined; 224 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 225 consoleLog(str); 226 }; 227 } 228 229 export fn _setFocus(focus_id: u32) void { 230 setFocus(focus_id) catch |err| { 231 var buf: [256]u8 = undefined; 232 const str = std.fmt.bufPrint(&buf, "setFocus: {}", .{err}) catch unreachable; 233 consoleLog(str); 234 }; 235 } 236 237 // js calls this when the value of a native <input> changes; we replace 238 // the matching TextInput's content with the supplied utf-8 bytes. 239 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 240 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 241 var buf: [256]u8 = undefined; 242 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 243 consoleLog(str); 244 }; 245 } 246 247 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 248 const root_ptr = if (root) |*r| r else return error.NotStarted; 249 const root_focus = root_ptr.getFocus(); 250 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 251 switch (child.focus.kind) { 252 .text_input, .text_input_password, .text_area => {}, 253 else => return error.NotATextInput, 254 } 255 const ti = session.text_inputs.get(focus_id) orelse return error.UnknownFocusId; 256 try ti.setContent(allocator, bytes); 257 } 258