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