╭ 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); 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); 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 123 if (header.getSelectedIndex()) |index| { 124 stack.getFocus().child_id = stack.children.keys()[index]; 125 switch (index) { 126 // mirror each list tab's built pagination window so the url stays 127 // consistent with what's shown (and so a build doesn't reset it). 128 0 => self.session.data.current_page = .{ .home_users = self.data.users.after }, 129 1 => self.session.data.current_page = .{ .home_repos = self.data.repos.after }, 130 2 => self.session.data.current_page = .home_settings, 131 3 => self.session.data.current_page = .home_auth, 132 // the quit tab is tty-only and not a route, so leave current_page 133 // alone (nothing to mirror into the url). 134 else => {}, 135 } 136 } 137 try self.box.build(allocator, constraint, root_focus); 138 } 139 140 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 141 if (self.getFocus().child_id) |child_id| { 142 if (self.box.children.getIndex(child_id)) |current_index| { 143 const child = &self.box.children.values()[current_index].widget; 144 var index = current_index; 145 146 const Direction = enum { up, down, none }; 147 const direction: Direction = switch (key) { 148 .arrow_up => .up, 149 .arrow_down => .down, 150 .mouse => |mouse| if (mouse.action == .scroll) 151 (if (mouse.action.scroll == .up) .up else .down) 152 else 153 .none, 154 else => .none, 155 }; 156 157 switch (direction) { 158 .up => { 159 switch (child.*) { 160 .home_header => { 161 try child.input(allocator, key, root_focus); 162 }, 163 .stack => { 164 if (child.stack.getSelected()) |selected_widget| { 165 const at_top = switch (selected_widget.*) { 166 .home_users => |*v| v.getSelectedIndex() == 0, 167 .home_repos => |*v| v.getSelectedIndex() == 0, 168 .home_settings => |*v| v.getSelectedIndex() == 0, 169 .home_auth => |*v| v.getSelectedIndex() == 0, 170 .quit => |*v| v.getSelectedIndex() == 0, 171 else => false, 172 }; 173 if (at_top) { 174 index = header_index; 175 } else { 176 try child.input(allocator, key, root_focus); 177 } 178 } 179 }, 180 else => {}, 181 } 182 }, 183 .down => { 184 switch (child.*) { 185 .home_header => { 186 index = stack_index; 187 }, 188 .stack => { 189 try child.input(allocator, key, root_focus); 190 }, 191 else => {}, 192 } 193 }, 194 .none => { 195 try child.input(allocator, key, root_focus); 196 }, 197 } 198 199 if (index != current_index) { 200 try root_focus.setFocus(self.box.children.keys()[index]); 201 } 202 } 203 } 204 } 205 206 pub fn clearGrid(self: *View) void { 207 self.box.clearGrid(); 208 } 209 210 pub fn getGrid(self: View) ?Grid { 211 return self.box.getGrid(); 212 } 213 214 pub fn getFocus(self: *View) *Focus { 215 return self.box.getFocus(); 216 } 217 }; 218