╭ Header.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 11 pub const AuthTab = @import("./../AuthTab.zig"); 12 13 name: []const u8, 14 owner_name: []const u8, 15 title: ui.Title, 16 auth_tab: AuthTab, 17 18 const Self = @This(); 19 20 pub fn init(arena: *std.heap.ArenaAllocator, name: []const u8, owner_name: []const u8) !Self { 21 return .{ 22 .name = name, 23 .owner_name = owner_name, 24 .title = try ui.Title.init(arena, name), 25 .auth_tab = AuthTab.init(), 26 }; 27 } 28 29 pub const View = struct { 30 box: wgt.Box(ui.Widget), 31 data: *const Self, 32 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 33 34 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 35 var box = wgt.Box(ui.Widget).init(.{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 36 errdefer box.deinit(allocator); 37 38 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 39 errdefer tab_ids.deinit(allocator); 40 41 const aa = session.page_arena.allocator(); 42 43 // the user's name 44 { 45 const text = try std.fmt.allocPrint(aa, "{s}/", .{data.owner_name}); 46 const link = try std.fmt.allocPrint(aa, "a:/user/{s}", .{data.owner_name}); 47 48 var text_box = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .wrap_kind = .none }); 49 errdefer text_box.deinit(allocator); 50 text_box.getFocus().focusable = true; 51 text_box.getFocus().kind = .{ .custom = link }; 52 try box.children.put(allocator, text_box.getFocus().id, .{ 53 .widget = .{ .text_box = text_box }, 54 .rect = null, 55 .min_size = null, 56 .shrink = true, 57 }); 58 } 59 60 // title sits to the left of the tabs 61 { 62 var title_view = try ui.Title.View.init(allocator, &data.title); 63 errdefer title_view.deinit(allocator); 64 title_view.getFocus().focusable = true; 65 title_view.getFocus().kind = .{ .custom = "a:/" }; 66 // shrink the title when there is not enough space 67 try box.children.put(allocator, title_view.getFocus().id, .{ 68 .widget = .{ .title = title_view }, 69 .rect = null, 70 .min_size = null, 71 .shrink = true, 72 }); 73 } 74 75 // spacer 76 { 77 var text = wgt.Text(ui.Widget).init(" "); 78 errdefer text.deinit(allocator); 79 try box.children.put(allocator, text.getFocus().id, .{ 80 .widget = .{ .text = text }, 81 .rect = null, 82 .min_size = .{ .width = 1, .height = null }, 83 }); 84 } 85 86 // every tab link is repo-scoped so selecting one stays on this page — 87 // switching its stack and updating the url — instead of navigating to 88 // the global settings or auth pages. the files tab routes through the 89 // shared helper so the /repo/.../files url format lives in one place. 90 const identity = try std.fmt.allocPrint(aa, "{s}/{s}", .{ data.owner_name, data.name }); 91 const files_route = ui.RoutablePage.repoFilesRoute(identity, "") orelse return error.RouteTooLong; 92 const files_link = try std.fmt.allocPrint(aa, "a:{s}", .{try files_route.urlAlloc(session.page_arena)}); 93 const settings_link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}/settings", .{ data.owner_name, data.name }); 94 const auth_link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}/auth", .{ data.owner_name, data.name }); 95 96 // files tab 97 { 98 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "files", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 99 errdefer text_box.deinit(allocator); 100 text_box.getFocus().focusable = true; 101 text_box.getFocus().kind = .{ .custom = files_link }; 102 try tab_ids.put(allocator, text_box.getFocus().id, {}); 103 try box.children.put(allocator, text_box.getFocus().id, .{ 104 .widget = .{ .text_box = text_box }, 105 .rect = null, 106 .min_size = .{ .width = "files".len + 2, .height = null }, 107 }); 108 } 109 110 // spacer pushes settings + auth to the right 111 { 112 var spacer = ui.Spacer.init(); 113 errdefer spacer.deinit(allocator); 114 try box.children.put(allocator, spacer.getFocus().id, .{ 115 .widget = .{ .spacer = spacer }, 116 .rect = null, 117 .min_size = null, 118 }); 119 } 120 121 // settings tab 122 { 123 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 124 errdefer text_box.deinit(allocator); 125 text_box.getFocus().focusable = true; 126 text_box.getFocus().kind = .{ .custom = settings_link }; 127 try tab_ids.put(allocator, text_box.getFocus().id, {}); 128 try box.children.put(allocator, text_box.getFocus().id, .{ 129 .widget = .{ .text_box = text_box }, 130 .rect = null, 131 .min_size = .{ .width = "settings".len + 2, .height = null }, 132 }); 133 } 134 135 // auth tab (login / logout). AuthTab defaults to the global a:/auth link; 136 // repoint its instance at this repo's auth route so it stays local too. 137 { 138 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 139 errdefer auth_tab.deinit(allocator); 140 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 141 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 142 try box.children.put(allocator, auth_tab.getFocus().id, .{ 143 .widget = .{ .auth_tab = auth_tab }, 144 .rect = null, 145 .min_size = .{ .width = AuthTab.min_width, .height = null }, 146 }); 147 } 148 149 // quit tab 150 if (session.is_terminal) { 151 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 152 errdefer text_box.deinit(allocator); 153 text_box.getFocus().focusable = true; 154 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 155 try tab_ids.put(allocator, text_box.getFocus().id, {}); 156 try box.children.put(allocator, text_box.getFocus().id, .{ 157 .widget = .{ .text_box = text_box }, 158 .rect = null, 159 // the label is a single column; +2 for the border 160 .min_size = .{ .width = 1 + 2, .height = null }, 161 }); 162 } 163 164 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 165 // open on the tab named by the current route 166 self.getFocus().child_id = self.tab_ids.keys()[ 167 switch (session.data.current_page) { 168 .repo_settings => 1, 169 .repo_auth => 2, 170 else => 0, 171 } 172 ]; 173 return self; 174 } 175 176 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 177 self.box.deinit(allocator); 178 self.tab_ids.deinit(allocator); 179 } 180 181 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 182 self.clearGrid(); 183 // only the selected tab shows its border 184 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 185 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 186 .text_box => |*x| x, 187 .auth_tab => |*at| &at.text_box, 188 else => null, 189 }; 190 if (tb) |t| { 191 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 192 } 193 } 194 try self.box.build(allocator, constraint, root_focus); 195 } 196 197 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 198 _ = allocator; 199 const current_tab = self.currentTabIndex() orelse return; 200 var new_tab = current_tab; 201 switch (key) { 202 .arrow_left => new_tab -|= 1, 203 .arrow_right => if (new_tab + 1 < self.tab_ids.count()) { 204 new_tab += 1; 205 }, 206 else => {}, 207 } 208 if (new_tab != current_tab) { 209 try root_focus.setFocus(self.tab_ids.keys()[new_tab]); 210 } 211 } 212 213 pub fn clearGrid(self: *View) void { 214 self.box.clearGrid(); 215 } 216 217 pub fn getGrid(self: View) ?Grid { 218 return self.box.getGrid(); 219 } 220 221 pub fn getFocus(self: *View) *Focus { 222 return self.box.getFocus(); 223 } 224 225 pub fn getSelectedIndex(self: View) ?usize { 226 return self.currentTabIndex(); 227 } 228 229 fn currentTabIndex(self: View) ?usize { 230 const child_id = self.box.focus.child_id orelse return null; 231 return self.tab_ids.getIndex(child_id); 232 } 233 }; 234