╭ main_wasm.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("./ui.zig"); 3 const pg = @import("./page.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 ui.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: ?pg.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(pg.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(x: usize, y: usize) !void { 70 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 71 const root_focus = root_ptr.getFocus(); 72 var iter = root_focus.children.iterator(); 73 while (iter.next()) |entry| { 74 const child = entry.value_ptr.*; 75 if (!child.focus.focusable) continue; 76 const r = child.rect; 77 if (x >= r.x and y >= r.y and x < r.x + r.size.width and y < r.y + r.size.height) { 78 try root_focus.setFocus(entry.key_ptr.*); 79 break; 80 } 81 } 82 } 83 84 fn consoleLog(arg: []const u8) void { 85 _consoleLog(arg.ptr, @intCast(arg.len)); 86 } 87 88 fn setHtml(arg: []const u8) void { 89 _setHtml(arg.ptr, @intCast(arg.len)); 90 } 91 92 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 93 extern fn _setHtml(arg: [*]const u8, len: u32) void; 94 95 /// js calls this first to get a wasm pointer it can write the page json into. 96 export fn _alloc(len: u32) ?[*]u8 { 97 const slice = allocator.alloc(u8, len) catch return null; 98 return slice.ptr; 99 } 100 101 export fn _start(json_ptr: [*]u8, json_len: u32, max_width: u32) void { 102 const json = json_ptr[0..json_len]; 103 defer allocator.free(json); 104 start(json, max_width) catch |err| { 105 var buf: [256]u8 = undefined; 106 const str = std.fmt.bufPrint(&buf, "start: {}", .{err}) catch unreachable; 107 consoleLog(str); 108 }; 109 } 110 111 export fn _tick(max_width: u32) bool { 112 tick(max_width) catch |err| { 113 var buf: [256]u8 = undefined; 114 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 115 consoleLog(str); 116 return false; 117 }; 118 return true; 119 } 120 121 export fn _onKeyDown(key_code: u32) void { 122 onKeyDown(key_code) catch |err| { 123 var buf: [256]u8 = undefined; 124 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 125 consoleLog(str); 126 }; 127 } 128 129 export fn _onMouseClick(x: u32, y: u32) void { 130 onMouseClick(x, y) catch |err| { 131 var buf: [256]u8 = undefined; 132 const str = std.fmt.bufPrint(&buf, "onMouseClick: {}", .{err}) catch unreachable; 133 consoleLog(str); 134 }; 135 } 136