╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
11 12 const about_tab_label = "about"; 13 const users_tab_label = "users"; 14 const new_tab_label = "new"; 15 const new_repo_tab_bottom_label = "repo"; 16 const new_user_tab_bottom_label = "user"; 17 18 // null hides the title 19 title: ?ui.Title, 20 21 const Self = @This(); 22 23 pub fn init(arena: *std.heap.ArenaAllocator, title: ?[]const u8) !Self { 24 return .{ 25 .title = if (title) |t| try ui.Title.init(arena, t, .scanlines) else null, 26 }; 27 } 28 29 pub const View = struct { 30 scroll: wgt.Scroll(ui.Widget), 31 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 32 tabs_id: usize, 33 gap_id: usize, 34 first_group_width: usize, 35 session: *ui.Session, 36 37 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 38 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = .hidden, .round_corners = true, .direction = .horiz }); 39 errdefer box.deinit(allocator); 40 41 var title_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 42 var title_box_owned = false; 43 errdefer if (!title_box_owned) title_box.deinit(allocator); 44 45 var tabs_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 46 var tabs_box_owned = false; 47 errdefer if (!tabs_box_owned) tabs_box.deinit(allocator); 48 49 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 50 errdefer tab_ids.deinit(allocator); 51 52 // the leading space plus the title 53 const first_group_width = if (data.title) |title| 1 + try title.width() else 0; 54 55 try ui.widget.addBackButton(allocator, &title_box, session); 56 57 // title sits to the left of the tabs 58 if (data.title) |*title| { 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 var title_view = try ui.Title.View.init(allocator, title); 70 errdefer title_view.deinit(allocator); 71 try title_box.children.put(allocator, title_view.getFocus().id, .{ 72 .widget = .{ .title = title_view }, 73 .rect = null, 74 .min_size = null, 75 }); 76 } 77 78 // spacer 79 { 80 var text = try wgt.Text.init(allocator, " "); 81 errdefer text.deinit(allocator); 82 try tabs_box.children.put(allocator, text.getFocus().id, .{ 83 .widget = .{ .text = text }, 84 .rect = null, 85 .min_size = .{ .width = 1, .height = null }, 86 }); 87 } 88 89 // the tab matching the current page is focused initially; matching by 90 // link (rather than position) keeps this robust to tab changes. 91 const current_tag = std.meta.activeTag(session.data.current_page); 92 const about_link = try ui.inPageTabLink(session, .home_about, current_tag == .home_about); 93 const users_link = try ui.inPageTabLink(session, .{ .home_users = .{} }, current_tag == .home_users); 94 const new_repo_link = try ui.inPageTabLink(session, .home_repo_new, current_tag == .home_repo_new); 95 const new_user_link = try ui.inPageTabLink(session, .home_user_new, current_tag == .home_user_new); 96 const user_link = try ui.inPageTabLink(session, .{ .home_user = .{} }, current_tag == .home_user); 97 const current_link: []const u8 = switch (current_tag) { 98 .home_users => users_link, 99 .home_repo_new => new_repo_link, 100 .home_user_new => new_user_link, 101 .home_user => user_link, 102 else => about_link, 103 }; 104 var selected_tab: ?usize = null; 105 106 // about tab 107 { 108 var text_box = try wgt.TextBox.init(allocator, about_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 109 errdefer text_box.deinit(allocator); 110 text_box.getFocus().mode = .all; 111 text_box.getFocus().kind = .{ .custom = about_link }; 112 try tab_ids.put(allocator, text_box.getFocus().id, {}); 113 if (std.mem.eql(u8, about_link, current_link)) selected_tab = text_box.getFocus().id; 114 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 115 .widget = .{ .text_box = text_box }, 116 .rect = null, 117 .min_size = .{ .width = about_tab_label.len + 2, .height = null }, 118 }); 119 } 120 121 // users tab 122 { 123 var text_box = try wgt.TextBox.init(allocator, users_tab_label, .{ .border = .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 = users_link }; 127 try tab_ids.put(allocator, text_box.getFocus().id, {}); 128 if (std.mem.eql(u8, users_link, current_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 = users_tab_label.len + 2, .height = null }, 133 }); 134 } 135 136 // spacer pushes new repo, new user, and user to the right 137 { 138 var spacer = try ui.widget.Spacer.init(allocator); 139 errdefer spacer.deinit(allocator); 140 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ 141 .widget = .{ .spacer = spacer }, 142 .rect = null, 143 .min_size = null, 144 .flex = .grow, 145 }); 146 } 147 148 // new repo tab with a login, new user tab without one 149 if (session.data.user_id != null) { 150 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 } }); 151 errdefer text_box.deinit(allocator); 152 text_box.getFocus().mode = .all; 153 text_box.getFocus().kind = .{ .custom = new_repo_link }; 154 try tab_ids.put(allocator, text_box.getFocus().id, {}); 155 if (std.mem.eql(u8, new_repo_link, current_link)) selected_tab = text_box.getFocus().id; 156 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 157 .widget = .{ .text_box = text_box }, 158 .rect = null, 159 .min_size = .{ .width = @max(new_tab_label.len, new_repo_tab_bottom_label.len) + 2, .height = null }, 160 }); 161 } else { 162 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 } }); 163 errdefer text_box.deinit(allocator); 164 text_box.getFocus().mode = .all; 165 text_box.getFocus().kind = .{ .custom = new_user_link }; 166 try tab_ids.put(allocator, text_box.getFocus().id, {}); 167 if (std.mem.eql(u8, new_user_link, current_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 = @max(new_tab_label.len, new_user_tab_bottom_label.len) + 2, .height = null }, 172 }); 173 } 174 175 // user tab 176 { 177 const label = ui.UserSettings.tabLabel(session); 178 const count_label = try ui.UserSettings.tabBottomLabel(session); 179 var text_box = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = count_label }); 180 errdefer text_box.deinit(allocator); 181 text_box.getFocus().mode = .all; 182 text_box.getFocus().kind = .{ .custom = user_link }; 183 try tab_ids.put(allocator, text_box.getFocus().id, {}); 184 if (std.mem.eql(u8, user_link, current_link)) selected_tab = text_box.getFocus().id; 185 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 186 .widget = .{ .text_box = text_box }, 187 .rect = null, 188 .min_size = .{ .width = @max(label.len, count_label.text.len) + 2, .height = null }, 189 }); 190 } 191 192 // quit tab 193 if (session.is_terminal) { 194 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 195 errdefer text_box.deinit(allocator); 196 text_box.getFocus().mode = .all; 197 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 198 try tab_ids.put(allocator, 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 // the label is a single column; +2 for the border 203 .min_size = .{ .width = 1 + 2, .height = null }, 204 }); 205 } 206 207 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 208 const title_id = title_box.getFocus().id; 209 const tabs_id = tabs_box.getFocus().id; 210 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 211 title_box_owned = true; 212 // a blank row between the title and the tabs, shown only when they wrap 213 const gap_id = blk: { 214 var gap = try wgt.Text.init(allocator, " "); 215 errdefer gap.deinit(allocator); 216 const id = gap.getFocus().id; 217 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 218 break :blk id; 219 }; 220 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 221 tabs_box_owned = true; 222 box.getFocus().child_id = tabs_id; 223 224 return .{ 225 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 226 .tab_ids = tab_ids, 227 .tabs_id = tabs_id, 228 .gap_id = gap_id, 229 .first_group_width = first_group_width, 230 .session = session, 231 }; 232 } 233 234 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 235 self.scroll.deinit(allocator); 236 self.tab_ids.deinit(allocator); 237 } 238 239 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 240 self.clearGrid(); 241 const box = &self.scroll.child.box; 242 const back_visible = self.session.back == .available; 243 ui.widget.setBackButtonVisible(box, back_visible); 244 const tabs_child = self.tabsChild(); 245 const tabs_box = &tabs_child.widget.box; 246 247 // only the selected tab shows its border 248 const selected_tab = tabs_box.getFocus().child_id; 249 var tabs_width: usize = 0; 250 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 251 const tb: ?*wgt.TextBox = switch (child.widget) { 252 .text_box => |*x| x, 253 else => null, 254 }; 255 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 256 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 257 } 258 259 // the outer box's hidden border occupies two columns. keep the title 260 // and tabs together if they fit; otherwise tab strip on its own row. 261 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 262 const content_width = if (viewport_width) |width| width -| 2 else null; 263 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 264 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 265 box.options.direction = if (wrap) .vert else .horiz; 266 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 267 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 268 269 var scroll_constraint = constraint; 270 scroll_constraint.min_size.width = viewport_width; 271 try self.scroll.build(allocator, scroll_constraint, root_focus); 272 } 273 274 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 275 _ = allocator; 276 const current_tab = self.currentTabIndex() orelse return; 277 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 278 const tab_id = self.tab_ids.keys()[new_tab]; 279 root_focus.setFocus(tab_id); 280 const tabs_child = self.tabsChild(); 281 const tabs_rect = tabs_child.rect orelse return; 282 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 283 var rect = tab.rect orelse return; 284 rect.x += tabs_rect.x; 285 rect.y += tabs_rect.y; 286 self.scroll.scrollToRect(rect); 287 } 288 } 289 290 pub fn clearGrid(self: *View) void { 291 self.scroll.clearGrid(); 292 } 293 294 pub fn getGrid(self: View) ?Grid { 295 return self.scroll.getGrid(); 296 } 297 298 pub fn getFocus(self: *View) *Focus { 299 return self.scroll.getFocus(); 300 } 301 302 pub fn getSelectedIndex(self: View) ?usize { 303 return self.currentTabIndex(); 304 } 305 306 fn currentTabIndex(self: View) ?usize { 307 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 308 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 309 return self.tab_ids.getIndex(child_id); 310 } 311 312 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 313 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 314 } 315 }; 316