╭ 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, 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(max_width); 50 } 51 52 fn tick(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_size = .{ .width = null, .height = null }, 56 // height is null so the TUI grows to fit all its content; the browser 57 // page handles vertical scrolling. 58 .max_size = .{ .width = max_width, .height = null }, 59 }, root_ptr.getFocus()); 60 try updateHtml(); 61 } 62 63 fn onKeyDown(key_code: u32) !void { 64 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 65 const key: inp.Key = switch (key_code) { 66 13 => .enter, 67 33 => .page_up, 68 34 => .page_down, 69 35 => .end, 70 36 => .home, 71 37 => .arrow_left, 72 38 => .arrow_up, 73 39 => .arrow_right, 74 40 => .arrow_down, 75 else => return, 76 }; 77 try root_ptr.input(allocator, key, root_ptr.getFocus()); 78 } 79 80 fn onMouseClick(focus_id: usize) !void { 81 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 82 try root_ptr.getFocus().setFocus(focus_id); 83 } 84 85 fn consoleLog(arg: []const u8) void { 86 _consoleLog(arg.ptr, @intCast(arg.len)); 87 } 88 89 fn setHtml(arg: []const u8) void { 90 _setHtml(arg.ptr, @intCast(arg.len)); 91 } 92 93 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 94 extern fn _setHtml(arg: [*]const u8, len: u32) void; 95 96 /// js calls this first to get a wasm pointer it can write the page json into. 97 export fn _alloc(len: u32) ?[*]u8 { 98 const slice = allocator.alloc(u8, len) catch return null; 99 return slice.ptr; 100 } 101 102 export fn _start(json_ptr: [*]u8, json_len: u32, max_width: u32) void { 103 const json = json_ptr[0..json_len]; 104 defer allocator.free(json); 105 start(json, max_width) catch |err| { 106 var buf: [256]u8 = undefined; 107 const str = std.fmt.bufPrint(&buf, "start: {}", .{err}) catch unreachable; 108 consoleLog(str); 109 }; 110 } 111 112 export fn _tick(max_width: u32) bool { 113 tick(max_width) catch |err| { 114 var buf: [256]u8 = undefined; 115 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 116 consoleLog(str); 117 return false; 118 }; 119 return true; 120 } 121 122 export fn _onKeyDown(key_code: u32) void { 123 onKeyDown(key_code) catch |err| { 124 var buf: [256]u8 = undefined; 125 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 126 consoleLog(str); 127 }; 128 } 129 130 export fn _onMouseClick(focus_id: u32) void { 131 onMouseClick(focus_id) catch |err| { 132 var buf: [256]u8 = undefined; 133 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 134 consoleLog(str); 135 }; 136 } 137 138 // js calls this when the value of an overlay <input> changes; we replace 139 // the matching TextInput's content with the supplied utf-8 bytes. 140 export fn _setTextInputValue(focus_id: u32, value_ptr: [*]const u8, value_len: u32) void { 141 setTextInputValue(focus_id, value_ptr[0..value_len]) catch |err| { 142 var buf: [256]u8 = undefined; 143 const str = std.fmt.bufPrint(&buf, "setTextInputValue: {}", .{err}) catch unreachable; 144 consoleLog(str); 145 }; 146 } 147 148 fn setTextInputValue(focus_id: u32, bytes: []const u8) !void { 149 const root_ptr = if (root) |*r| r else return error.NotStarted; 150 const root_focus = root_ptr.getFocus(); 151 const child = root_focus.children.get(focus_id) orelse return error.UnknownFocusId; 152 switch (child.focus.kind) { 153 .text_input, .text_input_password => {}, 154 else => return error.NotATextInput, 155 } 156 const ti: *wgt.TextInput(ui.Widget) = @fieldParentPtr("focus", child.focus); 157 try ti.setContent(allocator, bytes); 158 } 159