╭ main_wasm.zig ────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("./ui.zig"); 3 const xitui = @import("xit").xitui; 4 const inp = xitui.input; 5 6 const allocator = std.heap.wasm_allocator; 7 8 var root: ?ui.Widget = null; 9 10 fn updateHtml() !void { 11 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 12 const html = try ui.generateHtml(allocator, root_ptr); 13 defer allocator.free(html); 14 setHtml(html); 15 } 16 17 fn start() !void { 18 var next_root = try ui.initRoot(allocator); 19 errdefer next_root.deinit(); 20 21 if (root) |*old_root| old_root.deinit(); 22 root = next_root; 23 24 try updateHtml(); 25 } 26 27 fn tick() !void { 28 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 29 // TODO: make this fit the current size of the browser window 30 try root_ptr.build(.{ 31 .min_size = .{ .width = null, .height = null }, 32 .max_size = .{ .width = 80, .height = 24 }, 33 }, root_ptr.getFocus()); 34 try updateHtml(); 35 } 36 37 fn onKeyDown(key_code: u32) !void { 38 const root_ptr = if (root) |*root_value| root_value else return error.NotStarted; 39 const key: inp.Key = switch (key_code) { 40 33 => .page_up, 41 34 => .page_down, 42 35 => .end, 43 36 => .home, 44 38 => .arrow_up, 45 40 => .arrow_down, 46 else => return, 47 }; 48 try root_ptr.input(key, root_ptr.getFocus()); 49 } 50 51 fn consoleLog(arg: []const u8) void { 52 _consoleLog(arg.ptr, @intCast(arg.len)); 53 } 54 55 fn setHtml(arg: []const u8) void { 56 _setHtml(arg.ptr, @intCast(arg.len)); 57 } 58 59 extern fn _consoleLog(arg: [*]const u8, len: u32) void; 60 extern fn _setHtml(arg: [*]const u8, len: u32) void; 61 62 export fn _start() void { 63 start() catch |err| { 64 var buf: [256]u8 = undefined; 65 const str = std.fmt.bufPrint(&buf, "start: {}", .{err}) catch unreachable; 66 consoleLog(str); 67 }; 68 } 69 70 export fn _tick() bool { 71 tick() catch |err| { 72 var buf: [256]u8 = undefined; 73 const str = std.fmt.bufPrint(&buf, "tick: {}", .{err}) catch unreachable; 74 consoleLog(str); 75 return false; 76 }; 77 return true; 78 } 79 80 export fn _onKeyDown(key_code: u32) void { 81 onKeyDown(key_code) catch |err| { 82 var buf: [256]u8 = undefined; 83 const str = std.fmt.bufPrint(&buf, "onKeyDown: {}", .{err}) catch unreachable; 84 consoleLog(str); 85 }; 86 } 87