╭ 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 inp = xitui.input; 7 const wgt = xitui.widget; 8 9 const allocator = std.heap.wasm_allocator; 10 11 var root: ?ui.Widget = null; 12 13 fn updateHtml() !void { 14 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 15 const html = try web.generateHtml(allocator, root_ptr, &session); 16 defer allocator.free(html); 17 setHtml(html); 18 } 19 20 // the snapshot is allocated into this arena via parseFromSliceLeaky. it 21 // has to outlive `root`, because `root` holds slices into the parsed strings. 22 var page_arena: ?std.heap.ArenaAllocator = null; 23 var snapshot: ?ui.Snapshot = null; 24 var session: ui.Session = .{}; 25 26 fn start(json: []const u8, min_height: u32, max_width: u32) !void { 27 if (page_arena) |*a| a.deinit(); 28 page_arena = std.heap.ArenaAllocator.init(allocator); 29 30 // alloc_always so the parsed strings live entirely inside the arena 31 // and don't borrow from the caller's json buffer. 32 snapshot = try std.json.parseFromSliceLeaky(ui.Snapshot, (page_arena orelse unreachable).allocator(), json, .{ 33 .allocate = .alloc_always, 34 }); 35 36 // adopt the server's session view wholesale — one assignment instead of 37 // hand-restoring each field by reaching into the page's data tree. 38 session.data = (snapshot orelse unreachable).session; 39 40 var next_root = try ui.initRoot(allocator, &(snapshot orelse unreachable).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 try root_ptr.build(allocator, .{ 52 // min_height lets the TUI fill the viewport when its content is 53 // short; max height stays null so taller content extends downward 54 // and the browser scrolls. 55 .min_size = .{ .width = null, .height = min_height }, 56 .max_size = .{ .width = max_width, .height = null }, 57 }, root_ptr.getFocus()); 58 try updateHtml(); 59 } 60 61 fn onKeyDown(key_code: u32) !void { 62 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 63 const key: inp.Key = switch (key_code) { 64 13 => .enter, 65 33 => .page_up, 66 34 => .page_down, 67 35 => .end, 68 36 => .home, 69 37 => .arrow_left, 70 38 => .arrow_up, 71 39 => .arrow_right, 72 40 => .arrow_down, 73 else => return, 74 }; 75 try root_ptr.input(allocator, key, root_ptr.getFocus()); 76 } 77 78 fn onMouseClick(focus_id: usize) !void { 79 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 80 try root_ptr.getFocus().setFocus(focus_id); 81 } 82 83 fn consoleLog(arg: []const u8) void { 84 _consoleLog(arg.ptr, @intCast(arg.len)); 85 } 86 87 fn setHtml(arg: []const u8) void { 88 _setHtml(arg.ptr, @intCast(arg.len)); 89 } 90 91 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 92 extern fn _setHtml(arg: [*]const u8, len: u32) void; 93 94 /// js calls this first to get a wasm pointer it can write the page json into. 95 export fn _alloc(len: u32) ?[*]u8 { 96 const slice = allocator.alloc(u8, len) catch return null; 97 return slice.ptr; 98 } 99 100 export fn _start(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 101 const json = json_ptr[0..json_len]; 102 defer allocator.free(json); 103 start(json, min_height, max_width) catch |err| { 104 var buf: [256]u8 = undefined; 105 const str = std.fmt.bufPrint(&buf, "start: {}", .{err}) catch unreachable; 106 consoleLog(str); 107 }; 108 } 109 110 export fn _tick(min_height: u32, max_width: u32) bool { 111 tick(min_height, max_width) catch |err| { 112 var buf: [256]u8 = undefined; 113 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 114 consoleLog(str); 115 return false; 116 }; 117 return true; 118 } 119 120 export fn _onKeyDown(key_code: u32) void { 121 onKeyDown(key_code) catch |err| { 122 var buf: [256]u8 = undefined; 123 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 124 consoleLog(str); 125 }; 126 } 127 128 export fn _onMouseClick(focus_id: u32) void { 129 onMouseClick(focus_id) catch |err| { 130 var buf: [256]u8 = undefined; 131 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 132 consoleLog(str); 133 }; 134 } 135 136 // js calls this when the value of an overlay <input> changes; we replace 137 // the matching TextInput's content with the supplied utf-8 bytes. 138 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 139 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 140 var buf: [256]u8 = undefined; 141 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 142 consoleLog(str); 143 }; 144 } 145 146 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 147 const root_ptr = if (root) |*r| r else return error.NotStarted; 148 const root_focus = root_ptr.getFocus(); 149 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 150 switch (child.focus.kind) { 151 .text_input, .text_input_password => {}, 152 else => return error.NotATextInput, 153 } 154 const ti: *wgt.TextInput(ui.Widget) = @fieldParentPtr("focus", child.focus); 155 try ti.setContent(allocator, bytes); 156 // mirror the TTY behavior in Login.View.input: editing an input clears 157 // the stale failure flag so the "(invalid)" label doesn't linger after 158 // the user has typed a correction 159 session.data.login_failure = null; 160 } 161