╭ 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 inp = xitui.input; 8 const Grid = xitui.grid.Grid; 9 const Focus = xitui.focus.Focus; 10 const evt = @import("../event.zig"); 11 12 pub const Users = @import("./Home/Users.zig"); 13 pub const Repos = @import("./Home/Repos.zig"); 14 pub const Header = @import("./Home/Header.zig"); 15 pub const Settings = @import("./Settings.zig"); 16 pub const Auth = @import("./Auth.zig"); 17 pub const Quit = @import("./Quit.zig"); 18 19 header: Header, 20 users: Users, 21 repos: Repos, 22 settings: Settings, 23 auth: Auth, 24 quit: Quit, 25 26 const Self = @This(); 27 28 pub fn init( 29 arena: *std.heap.ArenaAllocator, 30 haxy_moment: evt.AdminDB.HashMap(.read_only), 31 // pagination window start for each list tab; the inactive tab gets 0. 32 users_after: usize, 33 repos_after: usize, 34 ) !Self { 35 return .{ 36 .header = try Header.init(arena), 37 .users = try Users.init(arena, haxy_moment, users_after), 38 .repos = try Repos.init(arena, haxy_moment, repos_after), 39 .settings = Settings.init(), 40 .auth = Auth.init(), 41 .quit = Quit.init(), 42 }; 43 } 44 45 pub const View = struct { 46 box: wgt.Box(ui.Widget), 47 data: *const Self, 48 session: *ui.Session, 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 var users_tab_id: usize = undefined; 60 { 61 var header_view = try Header.View.init(allocator, &data.header, session); 62 errdefer header_view.deinit(allocator); 63 users_tab_id = header_view.tab_ids.keys()[0]; 64 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .home_header = header_view }, .rect = null, .min_size = null }); 65 } 66 67 { 68 var stack = try wgt.Stack(ui.Widget).init(allocator); 69 errdefer stack.deinit(allocator); 70 71 { 72 var users_view = try Users.View.init(allocator, &data.users, !session.is_terminal); 73 errdefer users_view.deinit(allocator); 74 try stack.children.put(allocator, users_view.getFocus().id, .{ .home_users = users_view }); 75 } 76 77 { 78 var repos_view = try Repos.View.init(allocator, &data.repos, !session.is_terminal); 79 errdefer repos_view.deinit(allocator); 80 try stack.children.put(allocator, repos_view.getFocus().id, .{ .home_repos = repos_view }); 81 } 82 83 { 84 var settings_view = try Settings.View.init(allocator, &data.settings, 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, users_tab_id); 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, &data.quit, 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 .data = data, 107 .session = session, 108 }; 109 self.getFocus().child_id = box.children.keys()[header_index]; 110 return self; 111 } 112 113 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 114 self.box.deinit(allocator); 115 } 116 117 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 118 self.clearGrid(); 119 const header = &self.box.children.values()[header_index].widget.home_header; 120 const stack = &self.box.children.values()[stack_index].widget.stack; 121 122 // each header tab maps 1:1 to a stack child by position; mirror the 123 // selected child's page into the url, keyed by its kind 124 if (header.getSelectedIndex()) |index| { 125 stack.getFocus().child_id = stack.children.keys()[index]; 126 switch (std.meta.activeTag(stack.children.values()[index])) { 127 // mirror each list tab's built pagination window so the url stays 128 // consistent with what's shown (and so a build doesn't reset it). 129 .home_users => self.session.data.current_page = .{ .home_users = self.data.users.after }, 130 .home_repos => self.session.data.current_page = .{ .home_repos = self.data.repos.after }, 131 .home_settings => self.session.data.current_page = .home_settings, 132 .home_auth => self.session.data.current_page = .home_auth, 133 // the quit tab is tty-only and not a route, so leave current_page 134 // alone (nothing to mirror into the url). 135 else => {}, 136 } 137 } 138 try self.box.build(allocator, constraint, root_focus); 139 } 140 141 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 142 if (self.getFocus().child_id) |child_id| { 143 if (self.box.children.getIndex(child_id)) |current_index| { 144 const child = &self.box.children.values()[current_index].widget; 145 var index = current_index; 146 147 const Direction = enum { up, down, none }; 148 const direction: Direction = switch (key) { 149 .arrow_up => .up, 150 .arrow_down => .down, 151 .mouse => |mouse| if (mouse.action == .scroll) 152 (if (mouse.action.scroll == .up) .up else .down) 153 else 154 .none, 155 else => .none, 156 }; 157 158 switch (direction) { 159 .up => { 160 switch (child.*) { 161 .home_header => { 162 try child.input(allocator, key, root_focus); 163 }, 164 .stack => { 165 if (child.stack.getSelected()) |selected_widget| { 166 const at_top = switch (selected_widget.*) { 167 .home_users => |*v| v.getSelectedIndex() == 0, 168 .home_repos => |*v| v.getSelectedIndex() == 0, 169 .home_settings => |*v| v.getSelectedIndex() == 0, 170 .home_auth => |*v| v.getSelectedIndex() == 0, 171 .quit => |*v| v.getSelectedIndex() == 0, 172 else => false, 173 }; 174 if (at_top) { 175 index = header_index; 176 } else { 177 try child.input(allocator, key, root_focus); 178 } 179 } 180 }, 181 else => {}, 182 } 183 }, 184 .down => { 185 switch (child.*) { 186 .home_header => { 187 index = stack_index; 188 }, 189 .stack => { 190 try child.input(allocator, key, root_focus); 191 }, 192 else => {}, 193 } 194 }, 195 .none => { 196 try child.input(allocator, key, root_focus); 197 }, 198 } 199 200 if (index != current_index) { 201 try root_focus.setFocus(self.box.children.keys()[index]); 202 } 203 } 204 } 205 } 206 207 pub fn clearGrid(self: *View) void { 208 self.box.clearGrid(); 209 } 210 211 pub fn getGrid(self: View) ?Grid { 212 return self.box.getGrid(); 213 } 214 215 pub fn getFocus(self: *View) *Focus { 216 return self.box.getFocus(); 217 } 218 }; 219