╭ 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 ) !Self { 32 return .{ 33 .header = try Header.init(arena), 34 .users = try Users.init(arena, haxy_moment), 35 .repos = try Repos.init(arena, haxy_moment), 36 .settings = Settings.init(), 37 .auth = Auth.init(), 38 .quit = Quit.init(), 39 }; 40 } 41 42 pub const View = struct { 43 box: wgt.Box(ui.Widget), 44 data: *const Self, 45 session: *ui.Session, 46 47 const header_index: usize = 0; 48 const stack_index: usize = 1; 49 50 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 51 var box = wgt.Box(ui.Widget).init(.{ .border_style = null, .rounded_corners = true, .direction = .vert }); 52 errdefer box.deinit(allocator); 53 54 // build the header first so we can grab the users-tab focus id and 55 // hand it to login/logout 56 var users_tab_id: usize = undefined; 57 { 58 var header_view = try Header.View.init(allocator, &data.header, session); 59 errdefer header_view.deinit(allocator); 60 users_tab_id = header_view.tab_ids.keys()[0]; 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 = wgt.Stack(ui.Widget).init(); 66 errdefer stack.deinit(allocator); 67 68 { 69 var users_view = try Users.View.init(allocator, &data.users); 70 errdefer users_view.deinit(allocator); 71 try stack.children.put(allocator, users_view.getFocus().id, .{ .home_users = users_view }); 72 } 73 74 { 75 var repos_view = try Repos.View.init(allocator, &data.repos); 76 errdefer repos_view.deinit(allocator); 77 try stack.children.put(allocator, repos_view.getFocus().id, .{ .home_repos = repos_view }); 78 } 79 80 { 81 var settings_view = try Settings.View.init(allocator, &data.settings, session); 82 errdefer settings_view.deinit(allocator); 83 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 84 } 85 86 { 87 var auth_view = try Auth.View.init(allocator, &data.auth, session, users_tab_id); 88 errdefer auth_view.deinit(allocator); 89 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 90 } 91 92 if (session.is_terminal) { 93 var quit_view = try Quit.View.init(allocator, &data.quit, session); 94 errdefer quit_view.deinit(allocator); 95 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 96 } 97 98 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 99 } 100 101 var self = View{ 102 .box = box, 103 .data = data, 104 .session = session, 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 switch (index) { 123 0 => self.session.data.current_page = .home_users, 124 1 => self.session.data.current_page = .home_repos, 125 2 => self.session.data.current_page = .home_settings, 126 3 => self.session.data.current_page = .home_auth, 127 // the quit tab is tty-only and not a route, so leave current_page 128 // alone (nothing to mirror into the url). 129 else => {}, 130 } 131 } 132 try self.box.build(allocator, constraint, root_focus); 133 } 134 135 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 136 if (self.getFocus().child_id) |child_id| { 137 if (self.box.children.getIndex(child_id)) |current_index| { 138 const child = &self.box.children.values()[current_index].widget; 139 var index = current_index; 140 141 const Direction = enum { up, down, none }; 142 const direction: Direction = switch (key) { 143 .arrow_up => .up, 144 .arrow_down => .down, 145 .mouse => |mouse| if (mouse.action == .scroll) 146 (if (mouse.action.scroll == .up) .up else .down) 147 else 148 .none, 149 else => .none, 150 }; 151 152 switch (direction) { 153 .up => { 154 switch (child.*) { 155 .home_header => { 156 try child.input(allocator, key, root_focus); 157 }, 158 .stack => { 159 if (child.stack.getSelected()) |selected_widget| { 160 const at_top = switch (selected_widget.*) { 161 .home_users => |*v| v.getSelectedIndex() == 0, 162 .home_repos => |*v| v.getSelectedIndex() == 0, 163 .home_settings => |*v| v.getSelectedIndex() == 0, 164 .home_auth => |*v| v.getSelectedIndex() == 0, 165 .quit => |*v| v.getSelectedIndex() == 0, 166 else => false, 167 }; 168 if (at_top) { 169 index = header_index; 170 } else { 171 try child.input(allocator, key, root_focus); 172 } 173 } 174 }, 175 else => {}, 176 } 177 }, 178 .down => { 179 switch (child.*) { 180 .home_header => { 181 index = stack_index; 182 }, 183 .stack => { 184 try child.input(allocator, key, root_focus); 185 }, 186 else => {}, 187 } 188 }, 189 .none => { 190 try child.input(allocator, key, root_focus); 191 }, 192 } 193 194 if (index != current_index) { 195 try root_focus.setFocus(self.box.children.keys()[index]); 196 } 197 } 198 } 199 } 200 201 pub fn clearGrid(self: *View) void { 202 self.box.clearGrid(); 203 } 204 205 pub fn getGrid(self: View) ?Grid { 206 return self.box.getGrid(); 207 } 208 209 pub fn getFocus(self: *View) *Focus { 210 return self.box.getFocus(); 211 } 212 }; 213