╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Home.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
13 pub const About = @import("./Home/About.zig"); 14 pub const Users = @import("./Home/Users.zig"); 15 pub const Header = @import("./Home/Header.zig"); 16 pub const Auth = @import("./Auth.zig"); 17 pub const Quit = @import("./Quit.zig"); 18 19 header: Header, 20 about: About, 21 users: Users, 22 auth: Auth, 23 quit: Quit, 24 25 const Self = @This(); 26 27 pub fn init( 28 arena: *std.heap.ArenaAllocator, 29 session: *ui.Session, 30 haxy_moment: evt.AdminDB.HashMap(.read_only), 31 // the users tab's window and search 32 users_route: ui.RoutablePage.HomeUsersRoute, 33 ) !Self { 34 const about = try About.init(arena, session); 35 return .{ 36 .header = try Header.init(arena, about.title), 37 .about = about, 38 .users = try Users.init(arena, haxy_moment, users_route), 39 .auth = Auth.init(), 40 .quit = Quit.init(), 41 }; 42 } 43 44 pub const View = struct { 45 box: wgt.Box(ui.Widget), 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 = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .round_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 { 57 var header_view = try Header.View.init(allocator, &data.header, session); 58 errdefer header_view.deinit(allocator); 59 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .home_header = header_view }, .rect = null, .min_size = null }); 60 } 61 62 { 63 var stack = try wgt.Stack(ui.Widget).init(allocator); 64 errdefer stack.deinit(allocator); 65 66 { 67 var about_view = try About.View.init(allocator, &data.about, session); 68 errdefer about_view.deinit(allocator); 69 try stack.children.put(allocator, about_view.getFocus().id, .{ .home_about = about_view }); 70 } 71 72 { 73 var users_view = try Users.View.init(allocator, &data.users, session); 74 errdefer users_view.deinit(allocator); 75 try stack.children.put(allocator, users_view.getFocus().id, .{ .home_users = users_view }); 76 } 77 78 // the header has no new repo tab without a login, so keep the 79 // stack's children 1:1 with the tabs by skipping that view too 80 if (session.data.user_id != null) { 81 var new_repo_view = try ui.NewRepo.View.init(allocator, session, .home_new_repo); 82 errdefer new_repo_view.deinit(allocator); 83 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 84 } 85 86 { 87 var auth_view = try Auth.View.init(allocator, &data.auth, session); 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, 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 }; 104 // search results open in the tab's own search box 105 self.getFocus().child_id = box.children.keys()[if (data.users.search != null) stack_index else header_index]; 106 return self; 107 } 108 109 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 110 self.box.deinit(allocator); 111 } 112 113 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 114 self.clearGrid(); 115 const header = &self.box.children.values()[header_index].widget.home_header; 116 const stack = &self.box.children.values()[stack_index].widget.stack; 117 118 // each header tab maps 1:1 to a stack child by position 119 if (header.getSelectedIndex()) |index| 120 stack.getFocus().child_id = stack.children.keys()[index]; 121 try self.box.build(allocator, constraint, root_focus); 122 } 123 124 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 125 const stack = &self.box.children.values()[stack_index].widget.stack; 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 if (selected_widget.atTop(root_focus)) { 142 index = header_index; 143 } else { 144 try child.input(allocator, key, root_focus); 145 } 146 } 147 }, 148 else => {}, 149 } 150 }, 151 .down => { 152 switch (child.*) { 153 .home_header => { 154 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 155 .home_users => |*v| return v.focusHeader(root_focus), 156 else => {}, 157 }; 158 index = stack_index; 159 }, 160 .stack => { 161 try child.input(allocator, key, root_focus); 162 }, 163 else => {}, 164 } 165 }, 166 .none => { 167 try child.input(allocator, key, root_focus); 168 }, 169 } 170 171 if (index != current_index) { 172 root_focus.setFocus(self.box.children.keys()[index]); 173 } 174 } 175 } 176 } 177 178 pub fn clearGrid(self: *View) void { 179 self.box.clearGrid(); 180 } 181 182 pub fn getGrid(self: View) ?Grid { 183 return self.box.getGrid(); 184 } 185 186 pub fn getFocus(self: *View) *Focus { 187 return self.box.getFocus(); 188 } 189 }; 190