╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ main_wasm.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
90 fn onKeyDown(key_code: u32) !void { 91 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 92 const key: inp.Key = switch (key_code) { 93 13 => .enter, 94 33 => .page_up, 95 34 => .page_down, 96 35 => .end, 97 36 => .home, 98 37 => .arrow_left, 99 38 => .arrow_up, 100 39 => .arrow_right, 101 40 => .arrow_down, 102 else => return, 103 }; 104 try root_ptr.input(allocator, key, root_ptr.getFocus()); 105 } 106 107 fn onMouseClick(focus_id: usize) !void { 108 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 109 try root_ptr.getFocus().setFocus(focus_id); 110 } 111 112 fn consoleLog(arg: []const u8) void { 113 _consoleLog(arg.ptr, @intCast(arg.len)); 114 } 115 116 fn setHtml(arg: []const u8) void { 117 _setHtml(arg.ptr, @intCast(arg.len)); 118 } 119 120 fn setOverlay(arg: []const u8) void { 121 _setOverlay(arg.ptr, @intCast(arg.len)); 122 } 123 124 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 125 extern fn _setHtml(arg: [*]const u8, len: u32) void; 126 extern fn _setOverlay(arg: [*]const u8, len: u32) void; 127 extern fn _pushState(arg: [*]const u8, len: u32) void; 128 extern fn _focusInput(focus_id: u32) void; 129 130 /// js calls this first to get a wasm pointer it can write the page json into. 131 export fn _alloc(len: u32) ?[*]u8 { 132 const slice = allocator.alloc(u8, len) catch return null; 133 return slice.ptr; 134 } 135 136 export fn _init(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 137 const json = json_ptr[0..json_len]; 138 defer allocator.free(json); 139 init(json, min_height, max_width) catch |err| { 140 var buf: [256]u8 = undefined; 141 const str = std.fmt.bufPrint(&buf, "init: {}", .{err}) catch unreachable; 142 consoleLog(str); 143 }; 144 } 145 146 export fn _tick(min_height: u32, max_width: u32) bool { 147 tick(min_height, max_width) catch |err| { 148 var buf: [256]u8 = undefined; 149 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 150 consoleLog(str); 151 return false; 152 }; 153 return true; 154 } 155 156 export fn _onKeyDown(key_code: u32) void { 157 onKeyDown(key_code) catch |err| { 158 var buf: [256]u8 = undefined; 159 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 160 consoleLog(str); 161 }; 162 } 163 164 export fn _onMouseClick(focus_id: u32) void { 165 onMouseClick(focus_id) catch |err| { 166 var buf: [256]u8 = undefined; 167 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 168 consoleLog(str); 169 }; 170 } 171 172 // js calls this when the value of an overlay <input> changes; we replace 173 // the matching TextInput's content with the supplied utf-8 bytes. 174 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 175 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 176 var buf: [256]u8 = undefined; 177 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 178 consoleLog(str); 179 }; 180 } 181 182 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 183 const root_ptr = if (root) |*r| r else return error.NotStarted; 184 const root_focus = root_ptr.getFocus(); 185 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 186 switch (child.focus.kind) { 187 .text_input, .text_input_password => {}, 188 else => return error.NotATextInput, 189 } 190 const ti: *wgt.TextInput(ui.Widget) = @fieldParentPtr("focus", child.focus); 191 try ti.setContent(allocator, bytes); 192 // mirror the TTY behavior in Login.View.input: editing an input clears 193 // the stale failure flag so the "(invalid)" label doesn't linger after 194 // the user has typed a correction 195 session.data.login_failure = null; 196 } 197