╭ 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 = try wgt.Box(ui.Widget).init(allocator, .{ .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 = try wgt.Text(ui.Widget).init(allocator, " "); 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 commits_route = ui.RoutablePage.repoCommitsRoute(identity, "", 0) orelse return error.RouteTooLong; 94 const commits_link = try std.fmt.allocPrint(aa, "a:{s}", .{try commits_route.urlAlloc(session.page_arena)}); 95 const refs_link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}/refs", .{ data.owner_name, data.name }); 96 const settings_link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}/settings", .{ data.owner_name, data.name }); 97 const auth_link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}/auth", .{ data.owner_name, data.name }); 98 99 // the tab matching the current page is focused initially; matching by 100 // link (rather than position) keeps this robust to tab changes. 101 const current_link: []const u8 = switch (session.data.current_page) { 102 .repo_commits => commits_link, 103 .repo_refs => refs_link, 104 .repo_settings => settings_link, 105 .repo_auth => auth_link, 106 else => files_link, 107 }; 108 var selected_tab: ?usize = null; 109 110 // files tab 111 { 112 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "files", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 113 errdefer text_box.deinit(allocator); 114 text_box.getFocus().focusable = true; 115 text_box.getFocus().kind = .{ .custom = files_link }; 116 try tab_ids.put(allocator, text_box.getFocus().id, {}); 117 if (std.mem.eql(u8, files_link, current_link)) selected_tab = text_box.getFocus().id; 118 try box.children.put(allocator, text_box.getFocus().id, .{ 119 .widget = .{ .text_box = text_box }, 120 .rect = null, 121 .min_size = .{ .width = "files".len + 2, .height = null }, 122 }); 123 } 124 125 // commits tab 126 { 127 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "commits", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 128 errdefer text_box.deinit(allocator); 129 text_box.getFocus().focusable = true; 130 text_box.getFocus().kind = .{ .custom = commits_link }; 131 try tab_ids.put(allocator, text_box.getFocus().id, {}); 132 if (std.mem.eql(u8, commits_link, current_link)) selected_tab = text_box.getFocus().id; 133 try box.children.put(allocator, text_box.getFocus().id, .{ 134 .widget = .{ .text_box = text_box }, 135 .rect = null, 136 .min_size = .{ .width = "commits".len + 2, .height = null }, 137 }); 138 } 139 140 // refs tab 141 { 142 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "refs", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 143 errdefer text_box.deinit(allocator); 144 text_box.getFocus().focusable = true; 145 text_box.getFocus().kind = .{ .custom = refs_link }; 146 try tab_ids.put(allocator, text_box.getFocus().id, {}); 147 if (std.mem.eql(u8, refs_link, current_link)) selected_tab = text_box.getFocus().id; 148 try box.children.put(allocator, text_box.getFocus().id, .{ 149 .widget = .{ .text_box = text_box }, 150 .rect = null, 151 .min_size = .{ .width = "refs".len + 2, .height = null }, 152 }); 153 } 154 155 // spacer pushes settings + auth to the right 156 { 157 var spacer = try ui.Spacer.init(allocator); 158 errdefer spacer.deinit(allocator); 159 try box.children.put(allocator, spacer.getFocus().id, .{ 160 .widget = .{ .spacer = spacer }, 161 .rect = null, 162 .min_size = null, 163 }); 164 } 165 166 // settings tab 167 { 168 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 169 errdefer text_box.deinit(allocator); 170 text_box.getFocus().focusable = true; 171 text_box.getFocus().kind = .{ .custom = settings_link }; 172 try tab_ids.put(allocator, text_box.getFocus().id, {}); 173 if (std.mem.eql(u8, settings_link, current_link)) selected_tab = text_box.getFocus().id; 174 try box.children.put(allocator, text_box.getFocus().id, .{ 175 .widget = .{ .text_box = text_box }, 176 .rect = null, 177 .min_size = .{ .width = "settings".len + 2, .height = null }, 178 }); 179 } 180 181 // auth tab (login / logout). AuthTab defaults to the global a:/auth link; 182 // repoint its instance at this repo's auth route so it stays local too. 183 { 184 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 185 errdefer auth_tab.deinit(allocator); 186 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 187 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 188 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 189 try box.children.put(allocator, auth_tab.getFocus().id, .{ 190 .widget = .{ .auth_tab = auth_tab }, 191 .rect = null, 192 .min_size = .{ .width = AuthTab.min_width, .height = null }, 193 }); 194 } 195 196 // quit tab 197 if (session.is_terminal) { 198 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 199 errdefer text_box.deinit(allocator); 200 text_box.getFocus().focusable = true; 201 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 202 try tab_ids.put(allocator, text_box.getFocus().id, {}); 203 try box.children.put(allocator, text_box.getFocus().id, .{ 204 .widget = .{ .text_box = text_box }, 205 .rect = null, 206 // the label is a single column; +2 for the border 207 .min_size = .{ .width = 1 + 2, .height = null }, 208 }); 209 } 210 211 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 212 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 213 return self; 214 } 215 216 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 217 self.box.deinit(allocator); 218 self.tab_ids.deinit(allocator); 219 } 220 221 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 222 self.clearGrid(); 223 // only the selected tab shows its border 224 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 225 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 226 .text_box => |*x| x, 227 .auth_tab => |*at| &at.text_box, 228 else => null, 229 }; 230 if (tb) |t| { 231 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 232 } 233 } 234 try self.box.build(allocator, constraint, root_focus); 235 } 236 237 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 238 _ = allocator; 239 const current_tab = self.currentTabIndex() orelse return; 240 var new_tab = current_tab; 241 switch (key) { 242 .arrow_left => new_tab -|= 1, 243 .arrow_right => if (new_tab + 1 < self.tab_ids.count()) { 244 new_tab += 1; 245 }, 246 else => {}, 247 } 248 if (new_tab != current_tab) { 249 try root_focus.setFocus(self.tab_ids.keys()[new_tab]); 250 } 251 } 252 253 pub fn clearGrid(self: *View) void { 254 self.box.clearGrid(); 255 } 256 257 pub fn getGrid(self: View) ?Grid { 258 return self.box.getGrid(); 259 } 260 261 pub fn getFocus(self: *View) *Focus { 262 return self.box.getFocus(); 263 } 264 265 pub fn getSelectedIndex(self: View) ?usize { 266 return self.currentTabIndex(); 267 } 268 269 fn currentTabIndex(self: View) ?usize { 270 const child_id = self.box.focus.child_id orelse return null; 271 return self.tab_ids.getIndex(child_id); 272 } 273 }; 274