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