╭ 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 Header = @import("./Home/Header.zig"); 16 pub const Quit = @import("./Quit.zig"); 17 18 header: Header, 19 about: About, 20 users: Users, 21 user_notifications: ui.UserNotifications, 22 quit: Quit, 23 24 const Self = @This(); 25 26 pub fn init( 27 arena: *std.heap.ArenaAllocator, 28 session: *ui.Session, 29 haxy_moment: evt.AdminDB.HashMap(.read_only), 30 // the users tab's window and search 31 users_route: ui.RoutablePage.HomeUsersRoute, 32 user_tab: ui.RoutablePage.UserTab, 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 .user_notifications = try ui.UserNotifications.init(arena, session, user_tab), 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 = 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 shows new repo with a login and new user without 79 // one, so keep the stack's children 1:1 with the tabs 80 if (session.data.user_id != null) { 81 var new_repo_view = try ui.NewRepo.View.init(allocator, session, .home_repo_new); 82 errdefer new_repo_view.deinit(allocator); 83 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 84 } else { 85 var new_user_view = try ui.NewUser.View.init(allocator, session, .home_user_new); 86 errdefer new_user_view.deinit(allocator); 87 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 88 } 89 90 { 91 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, .{ .home_user = .{} }); 92 errdefer user_view.deinit(allocator); 93 try stack.children.put(allocator, user_view.getFocus().id, user_view); 94 } 95 96 if (session.is_terminal) { 97 var quit_view = try Quit.View.init(allocator, session); 98 errdefer quit_view.deinit(allocator); 99 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 100 } 101 102 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 103 } 104 105 var self = View{ 106 .box = box, 107 }; 108 // search results open in the tab's own search box 109 self.getFocus().child_id = box.children.keys()[if (data.users.search != null) stack_index else header_index]; 110 return self; 111 } 112 113 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 114 self.box.deinit(allocator); 115 } 116 117 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 118 self.clearGrid(); 119 const header = &self.box.children.values()[header_index].widget.home_header; 120 const stack = &self.box.children.values()[stack_index].widget.stack; 121 122 // each header tab maps 1:1 to a stack child by position 123 if (header.getSelectedIndex()) |index| 124 stack.getFocus().child_id = stack.children.keys()[index]; 125 try self.box.build(allocator, constraint, root_focus); 126 } 127 128 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 129 const stack = &self.box.children.values()[stack_index].widget.stack; 130 if (self.getFocus().child_id) |child_id| { 131 if (self.box.children.getIndex(child_id)) |current_index| { 132 const child = &self.box.children.values()[current_index].widget; 133 var index = current_index; 134 135 const direction = inp.vertDirection(key); 136 137 switch (direction) { 138 .up => { 139 switch (child.*) { 140 .home_header => { 141 try child.input(allocator, key, root_focus); 142 }, 143 .stack => { 144 if (child.stack.getSelected()) |selected_widget| { 145 if (selected_widget.atTop(root_focus)) { 146 index = header_index; 147 } else { 148 try child.input(allocator, key, root_focus); 149 } 150 } 151 }, 152 else => {}, 153 } 154 }, 155 .down => { 156 switch (child.*) { 157 .home_header => { 158 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 159 .home_users => |*v| return v.focusHeader(root_focus), 160 .user_notifications => |*v| if (v.focusHeader(root_focus)) return, 161 else => {}, 162 }; 163 index = stack_index; 164 }, 165 .stack => { 166 try child.input(allocator, key, root_focus); 167 }, 168 else => {}, 169 } 170 }, 171 .none => { 172 try child.input(allocator, key, root_focus); 173 }, 174 } 175 176 if (index != current_index) { 177 root_focus.setFocus(self.box.children.keys()[index]); 178 } 179 } 180 } 181 } 182 183 pub fn clearGrid(self: *View) void { 184 self.box.clearGrid(); 185 } 186 187 pub fn getGrid(self: View) ?Grid { 188 return self.box.getGrid(); 189 } 190 191 pub fn getFocus(self: *View) *Focus { 192 return self.box.getFocus(); 193 } 194 }; 195