╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
8 const layout = xitui.layout; 9 const Key = xitui.input.Key; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 13 const repos_tab_label = "repos"; 14 const forks_tab_label = "forks"; 15 const new_tab_label = "new"; 16 const new_repo_tab_bottom_label = "repo"; 17 const new_user_tab_bottom_label = "user"; 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 = .hidden, .round_corners = true, .direction = .horiz }); 41 errdefer box.deinit(allocator); 42 43 var title_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = 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 = 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 and links to the user's default tab 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 = try ui.userLink(session.page_arena, data.name) }; 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 new repo or user 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 current_page = session.data.current_page; 101 const current_tag = std.meta.activeTag(current_page); 102 const name = ui.RoutablePage.Array(evt.User.name_max_len).from(data.name) orelse return error.RouteTooLong; 103 const repos_link = try ui.inPageTabLink(session, .{ .user_repos = .{ .name = name } }, current_tag == .user_repos); 104 const forks_link = try ui.inPageTabLink(session, .{ .user_forks = .{ .name = name } }, current_tag == .user_forks); 105 const new_repo_link = try ui.inPageTabLink(session, .{ .user_repo_new = name }, current_tag == .user_repo_new); 106 const new_user_link = try ui.inPageTabLink(session, .{ .user_user_new = name }, current_tag == .user_user_new); 107 const user_link = try ui.inPageTabLink(session, .{ .user_user = .{ .name = name } }, current_tag == .user_user); 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 selected_link: []const u8 = switch (current_page) { 112 .user_repo_new => new_repo_link, 113 .user_user_new => new_user_link, 114 .user_user => user_link, 115 .user_forks => forks_link, 116 else => repos_link, 117 }; 118 var selected_tab: ?usize = null; 119 120 // repos tab 121 { 122 var text_box = try wgt.TextBox.init(allocator, repos_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 123 errdefer text_box.deinit(allocator); 124 text_box.getFocus().mode = .all; 125 text_box.getFocus().kind = .{ .custom = repos_link }; 126 try tab_ids.put(allocator, text_box.getFocus().id, {}); 127 if (std.mem.eql(u8, repos_link, selected_link)) selected_tab = text_box.getFocus().id; 128 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 129 .widget = .{ .text_box = text_box }, 130 .rect = null, 131 .min_size = .{ .width = repos_tab_label.len + 2, .height = null }, 132 }); 133 } 134 135 // forks tab 136 { 137 var text_box = try wgt.TextBox.init(allocator, forks_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 138 errdefer text_box.deinit(allocator); 139 text_box.getFocus().mode = .all; 140 text_box.getFocus().kind = .{ .custom = forks_link }; 141 try tab_ids.put(allocator, text_box.getFocus().id, {}); 142 if (std.mem.eql(u8, forks_link, selected_link)) selected_tab = text_box.getFocus().id; 143 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 144 .widget = .{ .text_box = text_box }, 145 .rect = null, 146 .min_size = .{ .width = forks_tab_label.len + 2, .height = null }, 147 }); 148 } 149 150 // spacer pushes new repo, new user, and user to the right 151 { 152 var spacer = try ui.widget.Spacer.init(allocator); 153 errdefer spacer.deinit(allocator); 154 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ 155 .widget = .{ .spacer = spacer }, 156 .rect = null, 157 .min_size = null, 158 .flex = .grow, 159 }); 160 } 161 162 // new repo tab with a login, new user tab without one 163 if (session.data.user_id != null) { 164 var text_box = try wgt.TextBox.init(allocator, new_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = new_repo_tab_bottom_label } }); 165 errdefer text_box.deinit(allocator); 166 text_box.getFocus().mode = .all; 167 text_box.getFocus().kind = .{ .custom = new_repo_link }; 168 try tab_ids.put(allocator, text_box.getFocus().id, {}); 169 if (std.mem.eql(u8, new_repo_link, selected_link)) selected_tab = text_box.getFocus().id; 170 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 171 .widget = .{ .text_box = text_box }, 172 .rect = null, 173 .min_size = .{ .width = @max(new_tab_label.len, new_repo_tab_bottom_label.len) + 2, .height = null }, 174 }); 175 } else { 176 var text_box = try wgt.TextBox.init(allocator, new_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = new_user_tab_bottom_label } }); 177 errdefer text_box.deinit(allocator); 178 text_box.getFocus().mode = .all; 179 text_box.getFocus().kind = .{ .custom = new_user_link }; 180 try tab_ids.put(allocator, text_box.getFocus().id, {}); 181 if (std.mem.eql(u8, new_user_link, selected_link)) selected_tab = text_box.getFocus().id; 182 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 183 .widget = .{ .text_box = text_box }, 184 .rect = null, 185 .min_size = .{ .width = @max(new_tab_label.len, new_user_tab_bottom_label.len) + 2, .height = null }, 186 }); 187 } 188 189 // user tab 190 { 191 const label = ui.UserSettings.tabLabel(session); 192 const count_label = try ui.UserSettings.tabBottomLabel(session); 193 var text_box = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = count_label }); 194 errdefer text_box.deinit(allocator); 195 text_box.getFocus().mode = .all; 196 text_box.getFocus().kind = .{ .custom = user_link }; 197 try tab_ids.put(allocator, text_box.getFocus().id, {}); 198 if (std.mem.eql(u8, user_link, selected_link)) selected_tab = text_box.getFocus().id; 199 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 200 .widget = .{ .text_box = text_box }, 201 .rect = null, 202 .min_size = .{ .width = @max(label.len, count_label.text.len) + 2, .height = null }, 203 }); 204 } 205 206 // quit tab 207 if (session.is_terminal) { 208 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 209 errdefer text_box.deinit(allocator); 210 text_box.getFocus().mode = .all; 211 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 212 try tab_ids.put(allocator, text_box.getFocus().id, {}); 213 try tabs_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 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 222 const title_id = title_box.getFocus().id; 223 const tabs_id = tabs_box.getFocus().id; 224 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 225 title_box_owned = true; 226 // a blank row between the title and the tabs, shown only when they wrap 227 const gap_id = blk: { 228 var gap = try wgt.Text.init(allocator, " "); 229 errdefer gap.deinit(allocator); 230 const id = gap.getFocus().id; 231 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 232 break :blk id; 233 }; 234 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 235 tabs_box_owned = true; 236 box.getFocus().child_id = tabs_id; 237 238 return .{ 239 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 240 .tab_ids = tab_ids, 241 .tabs_id = tabs_id, 242 .gap_id = gap_id, 243 .first_group_width = first_group_width, 244 .session = session, 245 }; 246 } 247 248 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 249 self.scroll.deinit(allocator); 250 self.tab_ids.deinit(allocator); 251 } 252 253 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 254 self.clearGrid(); 255 const box = &self.scroll.child.box; 256 const back_visible = self.session.back == .available; 257 ui.widget.setBackButtonVisible(box, back_visible); 258 const tabs_child = self.tabsChild(); 259 const tabs_box = &tabs_child.widget.box; 260 261 // only the selected tab shows its border 262 const selected_tab = tabs_box.getFocus().child_id; 263 var tabs_width: usize = 0; 264 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 265 const tb: ?*wgt.TextBox = switch (child.widget) { 266 .text_box => |*x| x, 267 else => null, 268 }; 269 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 270 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 271 } 272 273 // the outer box's hidden border occupies two columns. keep the title 274 // and tabs together if they fit; otherwise tab strip on its own row. 275 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 276 const content_width = if (viewport_width) |width| width -| 2 else null; 277 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 278 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 279 box.options.direction = if (wrap) .vert else .horiz; 280 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 281 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 282 283 var scroll_constraint = constraint; 284 scroll_constraint.min_size.width = viewport_width; 285 try self.scroll.build(allocator, scroll_constraint, root_focus); 286 } 287 288 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 289 _ = allocator; 290 const current_tab = self.currentTabIndex() orelse return; 291 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 292 const tab_id = self.tab_ids.keys()[new_tab]; 293 root_focus.setFocus(tab_id); 294 const tabs_child = self.tabsChild(); 295 const tabs_rect = tabs_child.rect orelse return; 296 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 297 var rect = tab.rect orelse return; 298 rect.x += tabs_rect.x; 299 rect.y += tabs_rect.y; 300 self.scroll.scrollToRect(rect); 301 } 302 } 303 304 pub fn clearGrid(self: *View) void { 305 self.scroll.clearGrid(); 306 } 307 308 pub fn getGrid(self: View) ?Grid { 309 return self.scroll.getGrid(); 310 } 311 312 pub fn getFocus(self: *View) *Focus { 313 return self.scroll.getFocus(); 314 } 315 316 pub fn getSelectedIndex(self: View) ?usize { 317 return self.currentTabIndex(); 318 } 319 320 fn currentTabIndex(self: View) ?usize { 321 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 322 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 323 return self.tab_ids.getIndex(child_id); 324 } 325 326 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 327 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 328 } 329 }; 330