╭ 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 page is allocated into this arena via parseFromSliceLeaky. it has 21 // to outlive `root`, because `root` holds slices into the parsed strings. 22 var page_arena: ?std.heap.ArenaAllocator = null; 23 var page: ?ui.Page = 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 page = try std.json.parseFromSliceLeaky(ui.Page, (page_arena orelse unreachable).allocator(), json, .{ 33 .allocate = .alloc_always, 34 }); 35 36 // restore session.user_id from the serialized page so the first render 37 // matches what the server rendered (otherwise wasm would render the 38 // page in a logged-out state on top of a logged-in server-rendered HTML). 39 session.user_id = switch (page orelse unreachable) { 40 .home => |h| h.user_id, 41 }; 42 43 var next_root = try ui.initRoot(allocator, &(page orelse unreachable), &session); 44 errdefer next_root.deinit(allocator); 45 46 if (root) |*old_root| old_root.deinit(allocator); 47 root = next_root; 48 49 try tick(min_height, max_width); 50 } 51 52 fn tick(min_height: u32, max_width: u32) !void { 53 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 54 try root_ptr.build(allocator, .{ 55 // min_height lets the TUI fill the viewport when its content is 56 // short; max height stays null so taller content extends downward 57 // and the browser scrolls. 58 .min_size = .{ .width = null, .height = min_height }, 59 .max_size = .{ .width = max_width, .height = null }, 60 }, root_ptr.getFocus()); 61 try updateHtml(); 62 } 63 64 fn onKeyDown(key_code: u32) !void { 65 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 66 const key: inp.Key = switch (key_code) { 67 13 => .enter, 68 33 => .page_up, 69 34 => .page_down, 70 35 => .end, 71 36 => .home, 72 37 => .arrow_left, 73 38 => .arrow_up, 74 39 => .arrow_right, 75 40 => .arrow_down, 76 else => return, 77 }; 78 try root_ptr.input(allocator, key, root_ptr.getFocus()); 79 } 80 81 fn onMouseClick(focus_id: usize) !void { 82 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 83 try root_ptr.getFocus().setFocus(focus_id); 84 } 85 86 fn consoleLog(arg: []const u8) void { 87 _consoleLog(arg.ptr, @intCast(arg.len)); 88 } 89 90 fn setHtml(arg: []const u8) void { 91 _setHtml(arg.ptr, @intCast(arg.len)); 92 } 93 94 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 95 extern fn _setHtml(arg: [*]const u8, len: u32) void; 96 97 /// js calls this first to get a wasm pointer it can write the page json into. 98 export fn _alloc(len: u32) ?[*]u8 { 99 const slice = allocator.alloc(u8, len) catch return null; 100 return slice.ptr; 101 } 102 103 export fn _start(json_ptr: [*]u8, json_len: u32, min_height: u32, max_width: u32) void { 104 const json = json_ptr[0..json_len]; 105 defer allocator.free(json); 106 start(json, min_height, max_width) catch |err| { 107 var buf: [256]u8 = undefined; 108 const str = std.fmt.bufPrint(&buf, "start: {}", .{err}) catch unreachable; 109 consoleLog(str); 110 }; 111 } 112 113 export fn _tick(min_height: u32, max_width: u32) bool { 114 tick(min_height, max_width) catch |err| { 115 var buf: [256]u8 = undefined; 116 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 117 consoleLog(str); 118 return false; 119 }; 120 return true; 121 } 122 123 export fn _onKeyDown(key_code: u32) void { 124 onKeyDown(key_code) catch |err| { 125 var buf: [256]u8 = undefined; 126 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 127 consoleLog(str); 128 }; 129 } 130 131 export fn _onMouseClick(focus_id: u32) void { 132 onMouseClick(focus_id) catch |err| { 133 var buf: [256]u8 = undefined; 134 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 135 consoleLog(str); 136 }; 137 } 138 139 // js calls this when the value of an overlay <input> changes; we replace 140 // the matching TextInput's content with the supplied utf-8 bytes. 141 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 142 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 143 var buf: [256]u8 = undefined; 144 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 145 consoleLog(str); 146 }; 147 } 148 149 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 150 const root_ptr = if (root) |*r| r else return error.NotStarted; 151 const root_focus = root_ptr.getFocus(); 152 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 153 switch (child.focus.kind) { 154 .text_input, .text_input_password => {}, 155 else => return error.NotATextInput, 156 } 157 const ti: *wgt.TextInput(ui.Widget) = @fieldParentPtr("focus", child.focus); 158 try ti.setContent(allocator, bytes); 159 } 160