╭ Home.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("../ui.zig"); 3 const xit = @import("xit"); 4 const xitui = xit.xitui; 5 const wgt = xitui.widget; 6 const layout = xitui.layout; 7 const Key = xitui.input.Key; 8 const Grid = xitui.grid.Grid; 9 const Focus = xitui.focus.Focus; 10 const inp = @import("./input.zig"); 11 const evt = @import("../event.zig"); 12 13 pub const Users = @import("./Home/Users.zig"); 14 pub const Repos = @import("./Home/Repos.zig"); 15 pub const Header = @import("./Home/Header.zig"); 16 pub const Settings = @import("./Settings.zig"); 17 pub const Auth = @import("./Auth.zig"); 18 pub const Quit = @import("./Quit.zig"); 19 20 header: Header, 21 users: Users, 22 repos: Repos, 23 settings: Settings, 24 auth: Auth, 25 quit: Quit, 26 27 const Self = @This(); 28 29 pub fn init( 30 arena: *std.heap.ArenaAllocator, 31 haxy_moment: evt.AdminDB.HashMap(.read_only), 32 user_id: ?[evt.event_id_size]u8, 33 // pagination window start for each list tab; the inactive tab gets 0. 34 users_start: usize, 35 repos_start: usize, 36 ) !Self { 37 return .{ 38 .header = try Header.init(arena), 39 .users = try Users.init(arena, haxy_moment, users_start), 40 .repos = try Repos.init(arena, haxy_moment, user_id, repos_start), 41 .settings = Settings.init(), 42 .auth = Auth.init(), 43 .quit = Quit.init(), 44 }; 45 } 46 47 pub const View = struct { 48 box: wgt.Box(ui.Widget), 49 50 const header_index: usize = 0; 51 const stack_index: usize = 1; 52 53 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 54 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 55 errdefer box.deinit(allocator); 56 57 // build the header first so we can grab the users-tab focus id and 58 // hand it to login/logout 59 { 60 var header_view = try Header.View.init(allocator, &data.header, session); 61 errdefer header_view.deinit(allocator); 62 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .home_header = header_view }, .rect = null, .min_size = null }); 63 } 64 65 { 66 var stack = try wgt.Stack(ui.Widget).init(allocator); 67 errdefer stack.deinit(allocator); 68 69 { 70 var repos_view = try Repos.View.init(allocator, &data.repos, !session.is_terminal); 71 errdefer repos_view.deinit(allocator); 72 try stack.children.put(allocator, repos_view.getFocus().id, .{ .home_repos = repos_view }); 73 } 74 75 { 76 var users_view = try Users.View.init(allocator, &data.users, !session.is_terminal); 77 errdefer users_view.deinit(allocator); 78 try stack.children.put(allocator, users_view.getFocus().id, .{ .home_users = users_view }); 79 } 80 81 // the header has no settings tab without a login, so keep the 82 // stack's children 1:1 with the tabs by skipping the view too 83 if (session.data.user_id != null) { 84 var settings_view = try Settings.View.init(allocator, session); 85 errdefer settings_view.deinit(allocator); 86 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 87 } 88 89 { 90 var auth_view = try Auth.View.init(allocator, &data.auth, session); 91 errdefer auth_view.deinit(allocator); 92 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 93 } 94 95 if (session.is_terminal) { 96 var quit_view = try Quit.View.init(allocator, session); 97 errdefer quit_view.deinit(allocator); 98 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 99 } 100 101 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 102 } 103 104 var self = View{ 105 .box = box, 106 }; 107 self.getFocus().child_id = box.children.keys()[header_index]; 108 return self; 109 } 110 111 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 112 self.box.deinit(allocator); 113 } 114 115 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 116 self.clearGrid(); 117 const header = &self.box.children.values()[header_index].widget.home_header; 118 const stack = &self.box.children.values()[stack_index].widget.stack; 119 120 // each header tab maps 1:1 to a stack child by position 121 if (header.getSelectedIndex()) |index| 122 stack.getFocus().child_id = stack.children.keys()[index]; 123 try self.box.build(allocator, constraint, root_focus); 124 } 125 126 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 127 if (self.getFocus().child_id) |child_id| { 128 if (self.box.children.getIndex(child_id)) |current_index| { 129 const child = &self.box.children.values()[current_index].widget; 130 var index = current_index; 131 132 const direction = inp.vertDirection(key); 133 134 switch (direction) { 135 .up => { 136 switch (child.*) { 137 .home_header => { 138 try child.input(allocator, key, root_focus); 139 }, 140 .stack => { 141 if (child.stack.getSelected()) |selected_widget| { 142 if (selected_widget.atTop(root_focus)) { 143 index = header_index; 144 } else { 145 try child.input(allocator, key, root_focus); 146 } 147 } 148 }, 149 else => {}, 150 } 151 }, 152 .down => { 153 switch (child.*) { 154 .home_header => { 155 index = stack_index; 156 }, 157 .stack => { 158 try child.input(allocator, key, root_focus); 159 }, 160 else => {}, 161 } 162 }, 163 .none => { 164 try child.input(allocator, key, root_focus); 165 }, 166 } 167 168 if (index != current_index) { 169 root_focus.setFocus(self.box.children.keys()[index]); 170 } 171 } 172 } 173 } 174 175 pub fn clearGrid(self: *View) void { 176 self.box.clearGrid(); 177 } 178 179 pub fn getGrid(self: View) ?Grid { 180 return self.box.getGrid(); 181 } 182 183 pub fn getFocus(self: *View) *Focus { 184 return self.box.getFocus(); 185 } 186 }; 187