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