╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Home.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 // pagination window start for each list tab; the inactive tab gets 0. 33 users_start: usize, 34 repos_start: usize, 35 ) !Self { 36 return .{ 37 .header = try Header.init(arena), 38 .users = try Users.init(arena, haxy_moment, users_start), 39 .repos = try Repos.init(arena, haxy_moment, repos_start), 40 .settings = Settings.init(), 41 .auth = Auth.init(), 42 .quit = Quit.init(), 43 }; 44 } 45 46 pub const View = struct { 47 box: wgt.Box(ui.Widget), 48 data: *const Self, 49 session: *ui.Session, 50 51 const header_index: usize = 0; 52 const stack_index: usize = 1; 53 54 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 55 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 56 errdefer box.deinit(allocator); 57 58 // build the header first so we can grab the users-tab focus id and 59 // hand it to login/logout 60 var users_tab_id: usize = undefined; 61 { 62 var header_view = try Header.View.init(allocator, &data.header, session); 63 errdefer header_view.deinit(allocator); 64 users_tab_id = header_view.tab_ids.keys()[0]; 65 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .home_header = header_view }, .rect = null, .min_size = null }); 66 } 67 68 { 69 var stack = try wgt.Stack(ui.Widget).init(allocator); 70 errdefer stack.deinit(allocator); 71 72 { 73 var users_view = try Users.View.init(allocator, &data.users, !session.is_terminal); 74 errdefer users_view.deinit(allocator); 75 try stack.children.put(allocator, users_view.getFocus().id, .{ .home_users = users_view }); 76 } 77 78 { 79 var repos_view = try Repos.View.init(allocator, &data.repos, !session.is_terminal); 80 errdefer repos_view.deinit(allocator); 81 try stack.children.put(allocator, repos_view.getFocus().id, .{ .home_repos = repos_view }); 82 } 83 84 { 85 var settings_view = try Settings.View.init(allocator, session); 86 errdefer settings_view.deinit(allocator); 87 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 88 } 89 90 { 91 var auth_view = try Auth.View.init(allocator, &data.auth, session, users_tab_id); 92 errdefer auth_view.deinit(allocator); 93 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 94 } 95 96 if (session.is_terminal) { 97 var quit_view = try Quit.View.init(allocator, session); 98 errdefer quit_view.deinit(allocator); 99 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 100 } 101 102 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 103 } 104 105 var self = View{ 106 .box = box, 107 .data = data, 108 .session = session, 109 }; 110 self.getFocus().child_id = box.children.keys()[header_index]; 111 return self; 112 } 113 114 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 115 self.box.deinit(allocator); 116 } 117 118 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 119 self.clearGrid(); 120 const header = &self.box.children.values()[header_index].widget.home_header; 121 const stack = &self.box.children.values()[stack_index].widget.stack; 122 123 // each header tab maps 1:1 to a stack child by position; mirror the 124 // selected child's page into the url, keyed by its kind 125 if (header.getSelectedIndex()) |index| { 126 stack.getFocus().child_id = stack.children.keys()[index]; 127 switch (std.meta.activeTag(stack.children.values()[index])) { 128 // mirror each list tab's built pagination window so the url stays 129 // consistent with what's shown (and so a build doesn't reset it). 130 .home_users => self.session.data.current_page = .{ .home_users = self.data.users.start }, 131 .home_repos => self.session.data.current_page = .{ .home_repos = self.data.repos.start }, 132 .home_settings => self.session.data.current_page = .home_settings, 133 .home_auth => self.session.data.current_page = .home_auth, 134 // the quit tab is tty-only and not a route, so leave current_page 135 // alone (nothing to mirror into the url). 136 else => {}, 137 } 138 } 139 try self.box.build(allocator, constraint, root_focus); 140 } 141 142 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 143 if (self.getFocus().child_id) |child_id| { 144 if (self.box.children.getIndex(child_id)) |current_index| { 145 const child = &self.box.children.values()[current_index].widget; 146 var index = current_index; 147 148 const direction = inp.vertDirection(key); 149 150 switch (direction) { 151 .up => { 152 switch (child.*) { 153 .home_header => { 154 try child.input(allocator, key, root_focus); 155 }, 156 .stack => { 157 if (child.stack.getSelected()) |selected_widget| { 158 const at_top = switch (selected_widget.*) { 159 .home_users => |*v| v.getSelectedIndex() == 0, 160 .home_repos => |*v| v.getSelectedIndex() == 0, 161 .home_settings => |*v| v.getSelectedIndex() == 0, 162 .home_auth => |*v| v.getSelectedIndex() == 0, 163 .quit => |*v| v.getSelectedIndex() == 0, 164 else => false, 165 }; 166 if (at_top) { 167 index = header_index; 168 } else { 169 try child.input(allocator, key, root_focus); 170 } 171 } 172 }, 173 else => {}, 174 } 175 }, 176 .down => { 177 switch (child.*) { 178 .home_header => { 179 index = stack_index; 180 }, 181 .stack => { 182 try child.input(allocator, key, root_focus); 183 }, 184 else => {}, 185 } 186 }, 187 .none => { 188 try child.input(allocator, key, root_focus); 189 }, 190 } 191 192 if (index != current_index) { 193 root_focus.setFocus(self.box.children.keys()[index]); 194 } 195 } 196 } 197 } 198 199 pub fn clearGrid(self: *View) void { 200 self.box.clearGrid(); 201 } 202 203 pub fn getGrid(self: View) ?Grid { 204 return self.box.getGrid(); 205 } 206 207 pub fn getFocus(self: *View) *Focus { 208 return self.box.getFocus(); 209 } 210 }; 211