╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Home.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 Header = @import("./Home/Header.zig"); 16 pub const Quit = @import("./Quit.zig"); 17 18 header: Header, 19 about: About, 20 users: Users, 21 quit: Quit, 22 23 const Self = @This(); 24 25 pub fn init( 26 arena: *std.heap.ArenaAllocator, 27 session: *ui.Session, 28 haxy_moment: evt.AdminDB.HashMap(.read_only), 29 // the users tab's window and search 30 users_route: ui.RoutablePage.HomeUsersRoute, 31 ) !Self { 32 const about = try About.init(arena, session); 33 return .{ 34 .header = try Header.init(arena, about.title), 35 .about = about, 36 .users = try Users.init(arena, haxy_moment, users_route), 37 .quit = Quit.init(), 38 }; 39 } 40 41 pub const View = struct { 42 box: wgt.Box(ui.Widget), 43 44 const header_index: usize = 0; 45 const stack_index: usize = 1; 46 47 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 48 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 49 errdefer box.deinit(allocator); 50 51 // build the header first so we can grab the users-tab focus id and 52 // hand it to login/logout 53 { 54 var header_view = try Header.View.init(allocator, &data.header, session); 55 errdefer header_view.deinit(allocator); 56 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .home_header = header_view }, .rect = null, .min_size = null }); 57 } 58 59 { 60 var stack = try wgt.Stack(ui.Widget).init(allocator); 61 errdefer stack.deinit(allocator); 62 63 { 64 var about_view = try About.View.init(allocator, &data.about, session); 65 errdefer about_view.deinit(allocator); 66 try stack.children.put(allocator, about_view.getFocus().id, .{ .home_about = about_view }); 67 } 68 69 { 70 var users_view = try Users.View.init(allocator, &data.users, session); 71 errdefer users_view.deinit(allocator); 72 try stack.children.put(allocator, users_view.getFocus().id, .{ .home_users = users_view }); 73 } 74 75 // the header shows new repo with a login and new user without 76 // one, so keep the stack's children 1:1 with the tabs 77 if (session.data.user_id != null) { 78 var new_repo_view = try ui.NewRepo.View.init(allocator, session, .home_repo_new); 79 errdefer new_repo_view.deinit(allocator); 80 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 81 } else { 82 var new_user_view = try ui.NewUser.View.init(allocator, session, .home_user_new); 83 errdefer new_user_view.deinit(allocator); 84 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 85 } 86 87 { 88 var user_view = try ui.UserSettings.initView(allocator, session); 89 errdefer user_view.deinit(allocator); 90 try stack.children.put(allocator, user_view.getFocus().id, user_view); 91 } 92 93 if (session.is_terminal) { 94 var quit_view = try Quit.View.init(allocator, session); 95 errdefer quit_view.deinit(allocator); 96 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 97 } 98 99 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 100 } 101 102 var self = View{ 103 .box = box, 104 }; 105 // search results open in the tab's own search box 106 self.getFocus().child_id = box.children.keys()[if (data.users.search != null) stack_index else 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 const stack = &self.box.children.values()[stack_index].widget.stack; 127 if (self.getFocus().child_id) |child_id| { 128 if (self.box.children.getIndex(child_id)) |current_index| { 129 const child = &self.box.children.values()[current_index].widget; 130 var index = current_index; 131 132 const direction = inp.vertDirection(key); 133 134 switch (direction) { 135 .up => { 136 switch (child.*) { 137 .home_header => { 138 try child.input(allocator, key, root_focus); 139 }, 140 .stack => { 141 if (child.stack.getSelected()) |selected_widget| { 142 if (selected_widget.atTop(root_focus)) { 143 index = header_index; 144 } else { 145 try child.input(allocator, key, root_focus); 146 } 147 } 148 }, 149 else => {}, 150 } 151 }, 152 .down => { 153 switch (child.*) { 154 .home_header => { 155 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 156 .home_users => |*v| return v.focusHeader(root_focus), 157 else => {}, 158 }; 159 index = stack_index; 160 }, 161 .stack => { 162 try child.input(allocator, key, root_focus); 163 }, 164 else => {}, 165 } 166 }, 167 .none => { 168 try child.input(allocator, key, root_focus); 169 }, 170 } 171 172 if (index != current_index) { 173 root_focus.setFocus(self.box.children.keys()[index]); 174 } 175 } 176 } 177 } 178 179 pub fn clearGrid(self: *View) void { 180 self.box.clearGrid(); 181 } 182 183 pub fn getGrid(self: View) ?Grid { 184 return self.box.getGrid(); 185 } 186 187 pub fn getFocus(self: *View) *Focus { 188 return self.box.getFocus(); 189 } 190 }; 191