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