╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ main_wasm.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
131 fn onMouseClick(focus_id: usize) !void { 132 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 133 // follow a cross-page link 134 if (ui.crossPageLink(root_ptr.getFocus(), focus_id, session.data.current_page)) |route| { 135 const url = try route.urlAlloc(&page_arena); 136 navigate(url); 137 return; 138 } 139 try root_ptr.getFocus().setFocus(focus_id); 140 } 141 142 fn consoleLog(arg: []const u8) void { 143 _consoleLog(arg.ptr, @intCast(arg.len)); 144 } 145 146 fn setHtml(arg: []const u8) void { 147 _setHtml(arg.ptr, @intCast(arg.len)); 148 } 149 150 fn setOverlay(arg: []const u8) void { 151 _setOverlay(arg.ptr, @intCast(arg.len)); 152 } 153 154 fn navigate(arg: []const u8) void { 155 _navigate(arg.ptr, @intCast(arg.len)); 156 } 157 158 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 159 extern fn _setHtml(arg: [*]const u8, len: u32) void; 160 extern fn _setOverlay(arg: [*]const u8, len: u32) void; 161 extern fn _pushState(arg: [*]const u8, len: u32) void; 162 extern fn _focusInput(focus_id: u32) void; 163 extern fn _navigate(arg: [*]const u8, len: u32) void; 164 165 /// js calls this first to get a wasm pointer it can write the page json into. 166 export fn _alloc(len: u32) ?[*]u8 { 167 const slice = allocator.alloc(u8, len) catch return null; 168 return slice.ptr; 169 } 170 171 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 172 const json = json_ptr[0..json_len]; 173 defer allocator.free(json); 174 init(json, min_height, max_width) catch |err| { 175 var buf: [256]u8 = undefined; 176 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 177 consoleLog(str); 178 }; 179 } 180 181 export fn _tick(min_height: u32, max_width: u32) bool { 182 tick(min_height, max_width) catch |err| { 183 var buf: [256]u8 = undefined; 184 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 185 consoleLog(str); 186 return false; 187 }; 188 return true; 189 } 190 191 export fn _onKeyDown(key_code: u32) void { 192 onKeyDown(key_code) catch |err| { 193 var buf: [256]u8 = undefined; 194 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 195 consoleLog(str); 196 }; 197 } 198 199 export fn _onMouseClick(focus_id: u32) void { 200 onMouseClick(focus_id) catch |err| { 201 var buf: [256]u8 = undefined; 202 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 203 consoleLog(str); 204 }; 205 } 206 207 // js calls this when the value of an overlay <input> changes; we replace 208 // the matching TextInput's content with the supplied utf-8 bytes. 209 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 210 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 211 var buf: [256]u8 = undefined; 212 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 213 consoleLog(str); 214 }; 215 } 216 217 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 218 const root_ptr = if (root) |*r| r else return error.NotStarted; 219 const root_focus = root_ptr.getFocus(); 220 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 221 switch (child.focus.kind) { 222 .text_input, .text_input_password => {}, 223 else => return error.NotATextInput, 224 } 225 const ti: *wgt.TextInput(ui.Widget) = @fieldParentPtr("focus", child.focus); 226 try ti.setContent(allocator, bytes); 227 // mirror the TTY behavior in Login.View.input: editing an input clears 228 // the stale failure flag so the "(invalid)" label doesn't linger after 229 // the user has typed a correction 230 session.data.login_failure = null; 231 } 232