╭ 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 Key = xitui.input.Key; 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 var last_scrolled_focus_id: ?usize = null; // last focused widget we asked JS to scroll into view 17 18 fn init(json: []const u8, min_height: u32, max_width: u32) !void { 19 _ = page_arena.reset(.free_all); 20 // drop the last-pushed page so the next tick pushes the freshly-loaded 21 // page's url. 22 last_pushed_page_maybe = null; 23 24 // alloc_always so the parsed strings live entirely inside the arena 25 // and don't borrow from the caller's json buffer. 26 snapshot = try std.json.parseFromSliceLeaky(ui.Snapshot, page_arena.allocator(), json, .{ 27 .allocate = .alloc_always, 28 }); 29 30 // adopt the server's session view wholesale — one assignment instead of 31 // hand-restoring each field by reaching into the page's data tree. 32 session = .{ 33 .data = snapshot.session, 34 .arena = &page_arena, 35 // one render per page load (init resets the arena), so page-scoped and 36 // session-scoped allocations share the same arena here. 37 .page_arena = &page_arena, 38 }; 39 40 var next_root = try ui.initRoot(allocator, &snapshot.page, &session); 41 errdefer next_root.deinit(allocator); 42 43 if (root) |*old_root| old_root.deinit(allocator); 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 // a widget asked to move to another page, so hand it to the host instead of 53 // rendering this one again. 54 if (session.next_page) |route| { 55 session.next_page = null; 56 const url = try route.toUrl(&page_arena); 57 _navigate(url.ptr, @intCast(url.len)); 58 return; 59 } 60 // bind the UI to the browser viewport (cols x rows) like the terminal: 61 // min == max height fills it exactly, and each Scroll clips to its 62 // viewport while handing its full content to a native-scrollable element. 63 // a full-height page instead grows to its content and the browser scrolls 64 // the whole page. 65 const max_height: ?u32 = if (session.data.current_page.fullHeight()) null else min_height; 66 try root_ptr.build(allocator, .{ 67 .min_size = .{ .width = null, .height = min_height }, 68 .max_size = .{ .width = max_width, .height = max_height }, 69 }, root_ptr.getFocus()); 70 71 // push input-driven in-page route changes into the browser url without 72 // adding history entries. real cross-page navigations go through _navigate. 73 const current_page = session.data.current_page; 74 // the not-found page keeps the url that named the missing page 75 const should_push = current_page != .not_found and if (last_pushed_page_maybe) |lp| !lp.eql(current_page) else true; 76 if (should_push) { 77 last_pushed_page_maybe = current_page; 78 const url = try current_page.toUrl(&page_arena); 79 _replaceState(url.ptr, @intCast(url.len)); 80 } 81 82 const html = try web.generateHtml(allocator, root_ptr, &session); 83 defer allocator.free(html); 84 _setHtml(html.ptr, @intCast(html.len)); 85 86 const root_focus = root_ptr.getFocus(); 87 if (root_focus.grandchild_id) |gid| { 88 if (root_focus.children.get(gid)) |child| { 89 // browser-focus the focused native control (text input, submit button 90 // or file picker) so the browser handles typing, Enter-to-submit and 91 // Enter-to-open-the-picker natively. without this the control never 92 // gets focus and Enter falls through to the wasm. 93 switch (child.focus.kind) { 94 .text_input, .text_input_password, .text_area => _focusInput(@intCast(gid)), 95 .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)), 96 else => {}, 97 } 98 99 // keep the focused widget visible within its own scrollable element. 100 // only fire on an actual focus change so we don't fight the user's 101 // manual scrolling on unrelated ticks. 102 if (gid != last_scrolled_focus_id) { 103 last_scrolled_focus_id = gid; 104 _scrollToFocus(@intCast(gid)); 105 } 106 } 107 } 108 } 109 110 fn onKeyDown(key_code: u32) !void { 111 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 112 const key: Key = switch (key_code) { 113 9 => .tab, 114 13 => .enter, 115 33 => .page_up, 116 34 => .page_down, 117 35 => .end, 118 36 => .home, 119 37 => .arrow_left, 120 38 => .arrow_up, 121 39 => .arrow_right, 122 40 => .arrow_down, 123 else => return, 124 }; 125 const root_focus = root_ptr.getFocus(); 126 const focused_before = root_focus.grandchild_id; 127 128 if (key == .enter) { 129 if (root_focus.grandchild_id) |gid| { 130 // follow a cross-page link 131 if (ui.crossPageLink(root_focus, gid, session.data)) |route| { 132 const url = try route.toUrl(&page_arena); 133 _navigate(url.ptr, @intCast(url.len)); 134 return; 135 } 136 // follow a link to bytes the server serves directly 137 if (ui.rawLink(root_focus, gid)) |url| { 138 _navigate(url.ptr, @intCast(url.len)); 139 return; 140 } 141 } 142 } 143 144 try root_ptr.input(allocator, key, root_focus); 145 const focused_after = root_focus.grandchild_id; 146 if (key != .enter and focused_before == focused_after) return; 147 const focus_id = focused_after orelse return; 148 if (ui.inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 149 } 150 151 fn onMouseClick(focus_id: usize) !void { 152 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 153 // follow a cross-page link 154 if (ui.crossPageLink(root_ptr.getFocus(), focus_id, session.data)) |route| { 155 const url = try route.toUrl(&page_arena); 156 _navigate(url.ptr, @intCast(url.len)); 157 return; 158 } 159 // the grid suppresses the anchor's own navigation, so follow it here 160 if (ui.rawLink(root_ptr.getFocus(), focus_id)) |url| { 161 _navigate(url.ptr, @intCast(url.len)); 162 return; 163 } 164 try setFocus(focus_id); 165 } 166 167 fn setFocus(focus_id: usize) !void { 168 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 169 const root_focus = root_ptr.getFocus(); 170 root_focus.setFocus(focus_id); 171 if (ui.inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 172 } 173 174 fn consoleLog(arg: []const u8) void { 175 _consoleLog(arg.ptr, @intCast(arg.len)); 176 } 177 178 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 179 extern fn _setHtml(arg: [*]const u8, len: u32) void; 180 extern fn _replaceState(arg: [*]const u8, len: u32) void; 181 extern fn _focusInput(focus_id: u32) void; 182 extern fn _navigate(arg: [*]const u8, len: u32) void; 183 extern fn _scrollToFocus(focus_id: u32) void; 184 /// js calls this first to get a wasm pointer it can write the page json into. 185 export fn _alloc(len: u32) ?[*]u8 { 186 const slice = allocator.alloc(u8, len) catch return null; 187 return slice.ptr; 188 } 189 190 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 191 const json = json_ptr[0..json_len]; 192 defer allocator.free(json); 193 init(json, min_height, max_width) catch |err| { 194 var buf: [256]u8 = undefined; 195 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 196 consoleLog(str); 197 }; 198 } 199 200 export fn _tick(min_height: u32, max_width: u32) bool { 201 tick(min_height, max_width) catch |err| { 202 var buf: [256]u8 = undefined; 203 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 204 consoleLog(str); 205 return false; 206 }; 207 return true; 208 } 209 210 export fn _onKeyDown(key_code: u32) void { 211 onKeyDown(key_code) catch |err| { 212 var buf: [256]u8 = undefined; 213 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 214 consoleLog(str); 215 }; 216 } 217 218 export fn _onMouseClick(focus_id: u32) void { 219 onMouseClick(focus_id) catch |err| { 220 var buf: [256]u8 = undefined; 221 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 222 consoleLog(str); 223 }; 224 } 225 226 export fn _setFocus(focus_id: u32) void { 227 setFocus(focus_id) catch |err| { 228 var buf: [256]u8 = undefined; 229 const str = std.fmt.bufPrint(&buf, "setFocus: {}", .{err}) catch unreachable; 230 consoleLog(str); 231 }; 232 } 233 234 // js calls this when the value of a native <input> changes; we replace 235 // the matching TextInput's content with the supplied utf-8 bytes. 236 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 237 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 238 var buf: [256]u8 = undefined; 239 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 240 consoleLog(str); 241 }; 242 } 243 244 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 245 const root_ptr = if (root) |*r| r else return error.NotStarted; 246 const root_focus = root_ptr.getFocus(); 247 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 248 switch (child.focus.kind) { 249 .text_input, .text_input_password, .text_area => {}, 250 else => return error.NotATextInput, 251 } 252 const ti = session.text_inputs.get(focus_id) orelse return error.UnknownFocusId; 253 try ti.setContent(allocator, bytes); 254 } 255