╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ main_wasm.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
168 fn setFocus(focus_id: usize) !void { 169 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 170 const root_focus = root_ptr.getFocus(); 171 root_focus.setFocus(focus_id); 172 if (ui.inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 173 } 174 175 fn consoleLog(arg: []const u8) void { 176 _consoleLog(arg.ptr, @intCast(arg.len)); 177 } 178 179 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 180 extern fn _setHtml(arg: [*]const u8, len: u32) void; 181 extern fn _replaceState(arg: [*]const u8, len: u32) void; 182 extern fn _focusInput(focus_id: u32) void; 183 extern fn _navigate(arg: [*]const u8, len: u32) void; 184 extern fn _scrollToFocus(focus_id: u32) void; 185 /// js calls this first to get a wasm pointer it can write the page json into. 186 export fn _alloc(len: u32) ?[*]u8 { 187 const slice = allocator.alloc(u8, len) catch return null; 188 return slice.ptr; 189 } 190 191 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 192 const json = json_ptr[0..json_len]; 193 defer allocator.free(json); 194 init(json, min_height, max_width) catch |err| { 195 var buf: [256]u8 = undefined; 196 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 197 consoleLog(str); 198 }; 199 } 200 201 export fn _tick(min_height: u32, max_width: u32) bool { 202 tick(min_height, max_width) catch |err| { 203 var buf: [256]u8 = undefined; 204 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 205 consoleLog(str); 206 return false; 207 }; 208 return true; 209 } 210 211 export fn _onKeyDown(key_code: u32) void { 212 onKeyDown(key_code) catch |err| { 213 var buf: [256]u8 = undefined; 214 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 215 consoleLog(str); 216 }; 217 } 218 219 export fn _onMouseClick(focus_id: u32) void { 220 onMouseClick(focus_id) catch |err| { 221 var buf: [256]u8 = undefined; 222 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 223 consoleLog(str); 224 }; 225 } 226 227 export fn _setFocus(focus_id: u32) void { 228 setFocus(focus_id) catch |err| { 229 var buf: [256]u8 = undefined; 230 const str = std.fmt.bufPrint(&buf, "setFocus: {}", .{err}) catch unreachable; 231 consoleLog(str); 232 }; 233 } 234 235 // js calls this when the value of a native <input> changes; we replace 236 // the matching TextInput's content with the supplied utf-8 bytes. 237 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 238 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 239 var buf: [256]u8 = undefined; 240 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 241 consoleLog(str); 242 }; 243 } 244 245 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 246 const root_ptr = if (root) |*r| r else return error.NotStarted; 247 const root_focus = root_ptr.getFocus(); 248 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 249 switch (child.focus.kind) { 250 .text_input, .text_input_password, .text_area => {}, 251 else => return error.NotATextInput, 252 } 253 const ti = session.text_inputs.get(focus_id) orelse return error.UnknownFocusId; 254 try ti.setContent(allocator, bytes); 255 } 256