╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ main_wasm.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
44 root = next_root; 45 46 try tick(min_height, max_width); 47 } 48 49 fn tick(min_height: u32, max_width: u32) !void { 50 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 51 52 // apply actions queued during input. the wasm path has no repo, so this is 53 // in-memory only; logged-in web persistence goes through the /ansi POST. 54 session.applyPending(); 55 // bind the UI to the browser viewport (cols x rows) like the terminal: 56 // min == max height fills it exactly, and each Scroll clips to its 57 // viewport while handing its full content to a native-scrollable element. 58 // a full-height page instead grows to its content and the browser scrolls 59 // the whole page. 60 const max_height: ?u32 = if (session.data.current_page.fullHeight()) null else min_height; 61 try root_ptr.build(allocator, .{ 62 .min_size = .{ .width = null, .height = min_height }, 63 .max_size = .{ .width = max_width, .height = max_height }, 64 }, root_ptr.getFocus()); 65 66 // push input-driven in-page route changes into the browser url without 67 // adding history entries. real cross-page navigations go through _navigate. 68 const current_page = session.data.current_page; 69 const should_push = if (last_pushed_page_maybe) |lp| !lp.eql(current_page) else true; 70 if (should_push) { 71 last_pushed_page_maybe = current_page; 72 const url = try current_page.toUrl(&page_arena); 73 _replaceState(url.ptr, @intCast(url.len)); 74 } 75 76 const html = try web.generateHtml(allocator, root_ptr, &session); 77 defer allocator.free(html); 78 _setHtml(html.ptr, @intCast(html.len)); 79 80 const root_focus = root_ptr.getFocus(); 81 if (root_focus.grandchild_id) |gid| { 82 if (root_focus.children.get(gid)) |child| { 83 // browser-focus the focused native control (text input, submit button 84 // or file picker) so the browser handles typing, Enter-to-submit and 85 // Enter-to-open-the-picker natively. without this the control never 86 // gets focus and Enter falls through to the wasm. 87 switch (child.focus.kind) { 88 .text_input, .text_input_password, .text_area => _focusInput(@intCast(gid)), 89 .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)), 90 else => {}, 91 } 92 93 // keep the focused widget visible within its own scrollable element. 94 // only fire on an actual focus change so we don't fight the user's 95 // manual scrolling on unrelated ticks. 96 if (gid != last_scrolled_focus_id) { 97 last_scrolled_focus_id = gid; 98 _scrollToFocus(@intCast(gid)); 99 } 100 } 101 } 102 } 103 104 fn onKeyDown(key_code: u32) !void { 105 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 106 const key: Key = switch (key_code) { 107 13 => .enter, 108 33 => .page_up, 109 34 => .page_down, 110 35 => .end, 111 36 => .home, 112 37 => .arrow_left, 113 38 => .arrow_up, 114 39 => .arrow_right, 115 40 => .arrow_down, 116 else => return, 117 }; 118 const root_focus = root_ptr.getFocus(); 119 const focused_before = root_focus.grandchild_id; 120 121 if (key == .enter) { 122 if (root_focus.grandchild_id) |gid| { 123 // follow a cross-page link 124 if (ui.crossPageLink(root_focus, gid, session.data)) |route| { 125 const url = try route.toUrl(&page_arena); 126 _navigate(url.ptr, @intCast(url.len)); 127 return; 128 } 129 // follow a link to bytes the server serves directly 130 if (ui.rawLink(root_focus, gid)) |url| { 131 _navigate(url.ptr, @intCast(url.len)); 132 return; 133 } 134 } 135 } 136 137 try root_ptr.input(allocator, key, root_focus); 138 const focused_after = root_focus.grandchild_id; 139 if (key != .enter and focused_before == focused_after) return; 140 const focus_id = focused_after orelse return; 141 if (ui.inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 142 } 143 144 fn onMouseClick(focus_id: usize) !void { 145 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 146 // follow a cross-page link 147 if (ui.crossPageLink(root_ptr.getFocus(), focus_id, session.data)) |route| { 148 const url = try route.toUrl(&page_arena); 149 _navigate(url.ptr, @intCast(url.len)); 150 return; 151 } 152 // the grid suppresses the anchor's own navigation, so follow it here 153 if (ui.rawLink(root_ptr.getFocus(), focus_id)) |url| { 154 _navigate(url.ptr, @intCast(url.len)); 155 return; 156 } 157 try setFocus(focus_id); 158 } 159 160 fn setFocus(focus_id: usize) !void { 161 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 162 const root_focus = root_ptr.getFocus(); 163 root_focus.setFocus(focus_id); 164 if (ui.inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 165 } 166 167 fn consoleLog(arg: []const u8) void { 168 _consoleLog(arg.ptr, @intCast(arg.len)); 169 } 170 171 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 172 extern fn _setHtml(arg: [*]const u8, len: u32) void; 173 extern fn _replaceState(arg: [*]const u8, len: u32) void; 174 extern fn _focusInput(focus_id: u32) void; 175 extern fn _navigate(arg: [*]const u8, len: u32) void; 176 extern fn _scrollToFocus(focus_id: u32) void; 177 /// js calls this first to get a wasm pointer it can write the page json into. 178 export fn _alloc(len: u32) ?[*]u8 { 179 const slice = allocator.alloc(u8, len) catch return null; 180 return slice.ptr; 181 } 182 183 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 184 const json = json_ptr[0..json_len]; 185 defer allocator.free(json); 186 init(json, min_height, max_width) catch |err| { 187 var buf: [256]u8 = undefined; 188 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 189 consoleLog(str); 190 }; 191 } 192 193 export fn _tick(min_height: u32, max_width: u32) bool { 194 tick(min_height, max_width) catch |err| { 195 var buf: [256]u8 = undefined; 196 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 197 consoleLog(str); 198 return false; 199 }; 200 return true; 201 } 202 203 export fn _onKeyDown(key_code: u32) void { 204 onKeyDown(key_code) catch |err| { 205 var buf: [256]u8 = undefined; 206 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 207 consoleLog(str); 208 }; 209 } 210 211 export fn _onMouseClick(focus_id: u32) void { 212 onMouseClick(focus_id) catch |err| { 213 var buf: [256]u8 = undefined; 214 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 215 consoleLog(str); 216 }; 217 } 218 219 export fn _setFocus(focus_id: u32) void { 220 setFocus(focus_id) catch |err| { 221 var buf: [256]u8 = undefined; 222 const str = std.fmt.bufPrint(&buf, "setFocus: {}", .{err}) catch unreachable; 223 consoleLog(str); 224 }; 225 } 226 227 // js calls this when the value of a native <input> changes; we replace 228 // the matching TextInput's content with the supplied utf-8 bytes. 229 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 230 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 231 var buf: [256]u8 = undefined; 232 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 233 consoleLog(str); 234 }; 235 } 236 237 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 238 const root_ptr = if (root) |*r| r else return error.NotStarted; 239 const root_focus = root_ptr.getFocus(); 240 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 241 switch (child.focus.kind) { 242 .text_input, .text_input_password, .text_area => {}, 243 else => return error.NotATextInput, 244 } 245 const ti = session.text_inputs.get(focus_id) orelse return error.UnknownFocusId; 246 try ti.setContent(allocator, bytes); 247 } 248