╭ 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 // 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 49 const header_index: usize = 0; 50 const stack_index: usize = 1; 51 52 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 53 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 54 errdefer box.deinit(allocator); 55 56 // build the header first so we can grab the users-tab focus id and 57 // hand it to login/logout 58 { 59 var header_view = try Header.View.init(allocator, &data.header, session); 60 errdefer header_view.deinit(allocator); 61 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .home_header = header_view }, .rect = null, .min_size = null }); 62 } 63 64 { 65 var stack = try wgt.Stack(ui.Widget).init(allocator); 66 errdefer stack.deinit(allocator); 67 68 { 69 var repos_view = try Repos.View.init(allocator, &data.repos, !session.is_terminal); 70 errdefer repos_view.deinit(allocator); 71 try stack.children.put(allocator, repos_view.getFocus().id, .{ .home_repos = repos_view }); 72 } 73 74 { 75 var users_view = try Users.View.init(allocator, &data.users, !session.is_terminal); 76 errdefer users_view.deinit(allocator); 77 try stack.children.put(allocator, users_view.getFocus().id, .{ .home_users = users_view }); 78 } 79 80 // the header has no settings tab without a login, so keep the 81 // stack's children 1:1 with the tabs by skipping the view too 82 if (session.data.user_id != null) { 83 var settings_view = try Settings.View.init(allocator, session); 84 errdefer settings_view.deinit(allocator); 85 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 86 } 87 88 { 89 var auth_view = try Auth.View.init(allocator, &data.auth, session); 90 errdefer auth_view.deinit(allocator); 91 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 92 } 93 94 if (session.is_terminal) { 95 var quit_view = try Quit.View.init(allocator, session); 96 errdefer quit_view.deinit(allocator); 97 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 98 } 99 100 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 101 } 102 103 var self = View{ 104 .box = box, 105 }; 106 self.getFocus().child_id = box.children.keys()[header_index]; 107 return self; 108 } 109 110 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 111 self.box.deinit(allocator); 112 } 113 114 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 115 self.clearGrid(); 116 const header = &self.box.children.values()[header_index].widget.home_header; 117 const stack = &self.box.children.values()[stack_index].widget.stack; 118 119 // each header tab maps 1:1 to a stack child by position 120 if (header.getSelectedIndex()) |index| 121 stack.getFocus().child_id = stack.children.keys()[index]; 122 try self.box.build(allocator, constraint, root_focus); 123 } 124 125 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 126 if (self.getFocus().child_id) |child_id| { 127 if (self.box.children.getIndex(child_id)) |current_index| { 128 const child = &self.box.children.values()[current_index].widget; 129 var index = current_index; 130 131 const direction = inp.vertDirection(key); 132 133 switch (direction) { 134 .up => { 135 switch (child.*) { 136 .home_header => { 137 try child.input(allocator, key, root_focus); 138 }, 139 .stack => { 140 if (child.stack.getSelected()) |selected_widget| { 141 const at_top = switch (selected_widget.*) { 142 .home_users => |*v| v.getSelectedIndex() == 0, 143 .home_repos => |*v| v.getSelectedIndex() == 0, 144 .home_settings => |*v| v.getSelectedIndex() == 0, 145 .home_auth => |*v| v.getSelectedIndex() == 0, 146 .quit => |*v| v.getSelectedIndex() == 0, 147 else => false, 148 }; 149 if (at_top) { 150 index = header_index; 151 } else { 152 try child.input(allocator, key, root_focus); 153 } 154 } 155 }, 156 else => {}, 157 } 158 }, 159 .down => { 160 switch (child.*) { 161 .home_header => { 162 index = stack_index; 163 }, 164 .stack => { 165 try child.input(allocator, key, root_focus); 166 }, 167 else => {}, 168 } 169 }, 170 .none => { 171 try child.input(allocator, key, root_focus); 172 }, 173 } 174 175 if (index != current_index) { 176 root_focus.setFocus(self.box.children.keys()[index]); 177 } 178 } 179 } 180 } 181 182 pub fn clearGrid(self: *View) void { 183 self.box.clearGrid(); 184 } 185 186 pub fn getGrid(self: View) ?Grid { 187 return self.box.getGrid(); 188 } 189 190 pub fn getFocus(self: *View) *Focus { 191 return self.box.getFocus(); 192 } 193 }; 194