╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("../../ui.zig"); 3 const inp = @import("../input.zig"); 4 const xit = @import("xit"); 5 const xitui = xit.xitui; 6 const wgt = xitui.widget; 7 const layout = xitui.layout; 8 const Key = xitui.input.Key; 9 const Grid = xitui.grid.Grid; 10 const Focus = xitui.focus.Focus; 11 12 const repos_tab_label = "repos"; 13 const forks_tab_label = "forks"; 14 const settings_tab_label = "settings"; 15 const new_repo_tab_label = "new repo"; 16 17 pub const AuthTab = @import("./../AuthTab.zig"); 18 19 name: []const u8, 20 title: ui.Title, 21 22 const Self = @This(); 23 24 pub fn init(arena: *std.heap.ArenaAllocator, name: []const u8) !Self { 25 return .{ 26 .name = name, 27 .title = try ui.Title.init(arena, name, .scanlines), 28 }; 29 } 30 31 pub const View = struct { 32 scroll: wgt.Scroll(ui.Widget), 33 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 34 tabs_id: usize, 35 gap_id: usize, 36 first_group_width: usize, 37 session: *ui.Session, 38 39 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 40 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .round_corners = true, .direction = .horiz }); 41 errdefer box.deinit(allocator); 42 43 var title_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 44 var title_box_owned = false; 45 errdefer if (!title_box_owned) title_box.deinit(allocator); 46 47 var tabs_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 48 var tabs_box_owned = false; 49 errdefer if (!tabs_box_owned) tabs_box.deinit(allocator); 50 51 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 52 errdefer tab_ids.deinit(allocator); 53 54 // the leading space plus the title 55 const first_group_width = 1 + try data.title.width(); 56 57 try ui.widget.addBackButton(allocator, &title_box, session); 58 59 { 60 var text = try wgt.Text.init(allocator, " "); 61 errdefer text.deinit(allocator); 62 try title_box.children.put(allocator, text.getFocus().id, .{ 63 .widget = .{ .text = text }, 64 .rect = null, 65 .min_size = .{ .width = 1, .height = null }, 66 }); 67 } 68 69 // title sits to the left of the tabs 70 { 71 var title_view = try ui.Title.View.init(allocator, &data.title); 72 errdefer title_view.deinit(allocator); 73 title_view.getFocus().mode = .all; 74 title_view.getFocus().kind = .{ .custom = "a:/" }; 75 try title_box.children.put(allocator, title_view.getFocus().id, .{ 76 .widget = .{ .title = title_view }, 77 .rect = null, 78 .min_size = null, 79 }); 80 } 81 82 // spacer 83 { 84 var text = try wgt.Text.init(allocator, " "); 85 errdefer text.deinit(allocator); 86 try tabs_box.children.put(allocator, text.getFocus().id, .{ 87 .widget = .{ .text = text }, 88 .rect = null, 89 .min_size = .{ .width = 1, .height = null }, 90 }); 91 } 92 93 // every tab link is user-scoped so selecting one stays on this page — 94 // switching its stack and updating the url — instead of navigating to 95 // the global settings or auth pages. the "ai:" prefix makes each an 96 // in-page anchor: crossPageLink ignores it so a wasm click just switches 97 // tabs (the page already holds every tab's content), while the href is 98 // still followed with js off. allocated in the page arena so the focus 99 // kinds that borrow them live as long as this page's widget tree. 100 const aa = session.page_arena.allocator(); 101 const current_page = session.data.current_page; 102 const current_tag = std.meta.activeTag(current_page); 103 const current_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try current_page.toUrl(session.page_arena)}); 104 const repos_link = if (current_tag == .user_repos) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}", .{data.name}); 105 const forks_link = if (current_tag == .user_forks) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/forks", .{data.name}); 106 const settings_link = if (current_tag == .user_settings) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/settings", .{data.name}); 107 const new_repo_link = if (current_tag == .user_new_repo) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/new-repo", .{data.name}); 108 const auth_link = if (current_tag == .user_auth) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/auth", .{data.name}); 109 110 // the tab matching the current page is focused initially; matching by 111 // link (rather than position) keeps this robust to tab changes. 112 const selected_link: []const u8 = switch (current_page) { 113 .user_settings => settings_link, 114 .user_new_repo => new_repo_link, 115 .user_auth => auth_link, 116 .user_forks => forks_link, 117 else => repos_link, 118 }; 119 var selected_tab: ?usize = null; 120 121 // repos tab 122 { 123 var text_box = try wgt.TextBox.init(allocator, repos_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 124 errdefer text_box.deinit(allocator); 125 text_box.getFocus().mode = .all; 126 text_box.getFocus().kind = .{ .custom = repos_link }; 127 try tab_ids.put(allocator, text_box.getFocus().id, {}); 128 if (std.mem.eql(u8, repos_link, selected_link)) selected_tab = text_box.getFocus().id; 129 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 130 .widget = .{ .text_box = text_box }, 131 .rect = null, 132 .min_size = .{ .width = repos_tab_label.len + 2, .height = null }, 133 }); 134 } 135 136 // forks tab 137 { 138 var text_box = try wgt.TextBox.init(allocator, forks_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 139 errdefer text_box.deinit(allocator); 140 text_box.getFocus().mode = .all; 141 text_box.getFocus().kind = .{ .custom = forks_link }; 142 try tab_ids.put(allocator, text_box.getFocus().id, {}); 143 if (std.mem.eql(u8, forks_link, selected_link)) selected_tab = text_box.getFocus().id; 144 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 145 .widget = .{ .text_box = text_box }, 146 .rect = null, 147 .min_size = .{ .width = forks_tab_label.len + 2, .height = null }, 148 }); 149 } 150 151 // spacer pushes settings, new repo and auth to the right 152 { 153 var spacer = try ui.widget.Spacer.init(allocator); 154 errdefer spacer.deinit(allocator); 155 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ 156 .widget = .{ .spacer = spacer }, 157 .rect = null, 158 .min_size = null, 159 .flex = .grow, 160 }); 161 } 162 163 // new repo tab. repos are created for the logged-in user. 164 if (session.data.user_id != null) { 165 var text_box = try wgt.TextBox.init(allocator, new_repo_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 166 errdefer text_box.deinit(allocator); 167 text_box.getFocus().mode = .all; 168 text_box.getFocus().kind = .{ .custom = new_repo_link }; 169 try tab_ids.put(allocator, text_box.getFocus().id, {}); 170 if (std.mem.eql(u8, new_repo_link, selected_link)) selected_tab = text_box.getFocus().id; 171 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 172 .widget = .{ .text_box = text_box }, 173 .rect = null, 174 .min_size = .{ .width = new_repo_tab_label.len + 2, .height = null }, 175 }); 176 } 177 178 // settings tab. settings are account preferences, so it needs a login. 179 if (session.data.user_id != null) { 180 var text_box = try wgt.TextBox.init(allocator, settings_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 181 errdefer text_box.deinit(allocator); 182 text_box.getFocus().mode = .all; 183 text_box.getFocus().kind = .{ .custom = settings_link }; 184 try tab_ids.put(allocator, text_box.getFocus().id, {}); 185 if (std.mem.eql(u8, settings_link, selected_link)) selected_tab = text_box.getFocus().id; 186 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 187 .widget = .{ .text_box = text_box }, 188 .rect = null, 189 .min_size = .{ .width = settings_tab_label.len + 2, .height = null }, 190 }); 191 } 192 193 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 194 // link; repoint its instance at this user's auth route so it stays local. 195 { 196 var auth_tab = try AuthTab.View.init(allocator, session); 197 errdefer auth_tab.deinit(allocator); 198 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 199 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 200 if (std.mem.eql(u8, auth_link, selected_link)) selected_tab = auth_tab.getFocus().id; 201 try tabs_box.children.put(allocator, auth_tab.getFocus().id, .{ 202 .widget = .{ .auth_tab = auth_tab }, 203 .rect = null, 204 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 205 }); 206 } 207 208 // quit tab 209 if (session.is_terminal) { 210 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 211 errdefer text_box.deinit(allocator); 212 text_box.getFocus().mode = .all; 213 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 214 try tab_ids.put(allocator, text_box.getFocus().id, {}); 215 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 216 .widget = .{ .text_box = text_box }, 217 .rect = null, 218 // the label is a single column; +2 for the border 219 .min_size = .{ .width = 1 + 2, .height = null }, 220 }); 221 } 222 223 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 224 const title_id = title_box.getFocus().id; 225 const tabs_id = tabs_box.getFocus().id; 226 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 227 title_box_owned = true; 228 // a blank row between the title and the tabs, shown only when they wrap 229 const gap_id = blk: { 230 var gap = try wgt.Text.init(allocator, " "); 231 errdefer gap.deinit(allocator); 232 const id = gap.getFocus().id; 233 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 234 break :blk id; 235 }; 236 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 237 tabs_box_owned = true; 238 box.getFocus().child_id = tabs_id; 239 240 return .{ 241 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 242 .tab_ids = tab_ids, 243 .tabs_id = tabs_id, 244 .gap_id = gap_id, 245 .first_group_width = first_group_width, 246 .session = session, 247 }; 248 } 249 250 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 251 self.scroll.deinit(allocator); 252 self.tab_ids.deinit(allocator); 253 } 254 255 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 256 self.clearGrid(); 257 const box = &self.scroll.child.box; 258 const back_visible = self.session.back == .available; 259 ui.widget.setBackButtonVisible(box, back_visible); 260 const tabs_child = self.tabsChild(); 261 const tabs_box = &tabs_child.widget.box; 262 263 // only the selected tab shows its border 264 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 265 var tabs_width: usize = 0; 266 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 267 const tb: ?*wgt.TextBox = switch (child.widget) { 268 .text_box => |*x| x, 269 .auth_tab => |*at| blk: { 270 // the label tracks login state per frame, so the width must too 271 child.min_size = .{ .width = at.minWidth(), .height = null }; 272 break :blk &at.text_box; 273 }, 274 else => null, 275 }; 276 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 277 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 278 } 279 280 // the outer box's hidden border occupies two columns. keep the title 281 // and tabs together if they fit; otherwise tab strip on its own row. 282 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 283 const content_width = if (viewport_width) |width| width -| 2 else null; 284 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 285 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 286 box.options.direction = if (wrap) .vert else .horiz; 287 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 288 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 289 290 var scroll_constraint = constraint; 291 scroll_constraint.min_size.width = viewport_width; 292 try self.scroll.build(allocator, scroll_constraint, root_focus); 293 } 294 295 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 296 _ = allocator; 297 const current_tab = self.currentTabIndex() orelse return; 298 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 299 const tab_id = self.tab_ids.keys()[new_tab]; 300 root_focus.setFocus(tab_id); 301 const tabs_child = self.tabsChild(); 302 const tabs_rect = tabs_child.rect orelse return; 303 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 304 var rect = tab.rect orelse return; 305 rect.x += tabs_rect.x; 306 rect.y += tabs_rect.y; 307 self.scroll.scrollToRect(rect); 308 } 309 } 310 311 pub fn clearGrid(self: *View) void { 312 self.scroll.clearGrid(); 313 } 314 315 pub fn getGrid(self: View) ?Grid { 316 return self.scroll.getGrid(); 317 } 318 319 pub fn getFocus(self: *View) *Focus { 320 return self.scroll.getFocus(); 321 } 322 323 pub fn getSelectedIndex(self: View) ?usize { 324 return self.currentTabIndex(); 325 } 326 327 fn currentTabIndex(self: View) ?usize { 328 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 329 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 330 return self.tab_ids.getIndex(child_id); 331 } 332 333 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 334 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 335 } 336 }; 337