╭ 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 xitui = @import("xit").xitui; 5 const inp = xitui.input; 6 7 const allocator = std.heap.wasm_allocator; 8 9 var root: ?ui.Widget = null; 10 11 fn updateHtml() !void { 12 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 13 const html = try web.generateHtml(allocator, root_ptr); 14 defer allocator.free(html); 15 setHtml(html); 16 } 17 18 // the page is allocated into this arena via parseFromSliceLeaky. it has 19 // to outlive `root`, because `root` holds slices into the parsed strings. 20 var page_arena: ?std.heap.ArenaAllocator = null; 21 var page: ?ui.Page = null; 22 23 fn start(json: []const u8, max_width: u32) !void { 24 if (page_arena) |*a| a.deinit(); 25 page_arena = std.heap.ArenaAllocator.init(allocator); 26 27 // alloc_always so the parsed strings live entirely inside the arena 28 // and don't borrow from the caller's json buffer. 29 page = try std.json.parseFromSliceLeaky(ui.Page, (page_arena orelse unreachable).allocator(), json, .{ 30 .allocate = .alloc_always, 31 }); 32 33 var next_root = try ui.initRoot(allocator, &(page orelse unreachable)); 34 errdefer next_root.deinit(); 35 36 if (root) |*old_root| old_root.deinit(); 37 root = next_root; 38 39 try tick(max_width); 40 } 41 42 fn tick(max_width: u32) !void { 43 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 44 try root_ptr.build(.{ 45 .min_size = .{ .width = null, .height = null }, 46 // height is null so the TUI grows to fit all its content; the browser 47 // page handles vertical scrolling. 48 .max_size = .{ .width = max_width, .height = null }, 49 }, root_ptr.getFocus()); 50 try updateHtml(); 51 } 52 53 fn onKeyDown(key_code: u32) !void { 54 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 55 const key: inp.Key = switch (key_code) { 56 33 => .page_up, 57 34 => .page_down, 58 35 => .end, 59 36 => .home, 60 37 => .arrow_left, 61 38 => .arrow_up, 62 39 => .arrow_right, 63 40 => .arrow_down, 64 else => return, 65 }; 66 try root_ptr.input(key, root_ptr.getFocus()); 67 } 68 69 fn onMouseClick(focus_id: usize) !void { 70 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 71 try root_ptr.getFocus().setFocus(focus_id); 72 } 73 74 fn consoleLog(arg: []const u8) void { 75 _consoleLog(arg.ptr, @intCast(arg.len)); 76 } 77 78 fn setHtml(arg: []const u8) void { 79 _setHtml(arg.ptr, @intCast(arg.len)); 80 } 81 82 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 83 extern fn _setHtml(arg: [*]const u8, len: u32) void; 84 85 /// js calls this first to get a wasm pointer it can write the page json into. 86 export fn _alloc(len: u32) ?[*]u8 { 87 const slice = allocator.alloc(u8, len) catch return null; 88 return slice.ptr; 89 } 90 91 export fn _start(json_ptr: [*]u8, json_len: u32, max_width: u32) void { 92 const json = json_ptr[0..json_len]; 93 defer allocator.free(json); 94 start(json, max_width) catch |err| { 95 var buf: [256]u8 = undefined; 96 const str = std.fmt.bufPrint(&buf, "start: {}", .{err}) catch unreachable; 97 consoleLog(str); 98 }; 99 } 100 101 export fn _tick(max_width: u32) bool { 102 tick(max_width) catch |err| { 103 var buf: [256]u8 = undefined; 104 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 105 consoleLog(str); 106 return false; 107 }; 108 return true; 109 } 110 111 export fn _onKeyDown(key_code: u32) void { 112 onKeyDown(key_code) catch |err| { 113 var buf: [256]u8 = undefined; 114 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 115 consoleLog(str); 116 }; 117 } 118 119 export fn _onMouseClick(focus_id: u32) void { 120 onMouseClick(focus_id) catch |err| { 121 var buf: [256]u8 = undefined; 122 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 123 consoleLog(str); 124 }; 125 } 126