╭ 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 // TODO: receive the Page as json from the host and parse it back into 19 // a Page so initRoot can render real data. for now we initialize with 20 // an empty page just so the wasm build compiles. 21 var page_maybe: ?pg.Page = null; 22 23 fn start() !void { 24 const page = page_maybe orelse pg.Page{ .user_repo = pg.UserRepoPage.empty() }; 25 26 var next_root = try ui.initRoot(allocator, &page); 27 errdefer next_root.deinit(); 28 29 if (root) |*old_root| old_root.deinit(); 30 root = next_root; 31 32 try updateHtml(); 33 } 34 35 fn tick() !void { 36 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 37 // TODO: make this fit the current size of the browser window 38 try root_ptr.build(.{ 39 .min_size = .{ .width = null, .height = null }, 40 .max_size = .{ .width = 80, .height = 24 }, 41 }, root_ptr.getFocus()); 42 try updateHtml(); 43 } 44 45 fn onKeyDown(key_code: u32) !void { 46 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 47 const key: inp.Key = switch (key_code) { 48 33 => .page_up, 49 34 => .page_down, 50 35 => .end, 51 36 => .home, 52 38 => .arrow_up, 53 40 => .arrow_down, 54 else => return, 55 }; 56 try root_ptr.input(key, root_ptr.getFocus()); 57 } 58 59 fn consoleLog(arg: []const u8) void { 60 _consoleLog(arg.ptr, @intCast(arg.len)); 61 } 62 63 fn setHtml(arg: []const u8) void { 64 _setHtml(arg.ptr, @intCast(arg.len)); 65 } 66 67 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 68 extern fn _setHtml(arg: [*]const u8, len: u32) void; 69 70 export fn _start() void { 71 start() catch |err| { 72 var buf: [256]u8 = undefined; 73 const str = std.fmt.bufPrint(&buf, "start: {}", .{err}) catch unreachable; 74 consoleLog(str); 75 }; 76 } 77 78 export fn _tick() bool { 79 tick() catch |err| { 80 var buf: [256]u8 = undefined; 81 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 82 consoleLog(str); 83 return false; 84 }; 85 return true; 86 } 87 88 export fn _onKeyDown(key_code: u32) void { 89 onKeyDown(key_code) catch |err| { 90 var buf: [256]u8 = undefined; 91 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 92 consoleLog(str); 93 }; 94 } 95