╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ main_wasm.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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