╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
91 // link (rather than position) keeps this robust to tab changes. 92 const current_tag = std.meta.activeTag(session.data.current_page); 93 const about_link = try ui.inPageTabLink(session, .home_about, current_tag == .home_about); 94 const users_link = try ui.inPageTabLink(session, .{ .home_users = .{} }, current_tag == .home_users); 95 const new_repo_link = try ui.inPageTabLink(session, .home_new_repo, current_tag == .home_new_repo); 96 const auth_link = try ui.inPageTabLink(session, .home_auth, current_tag == .home_auth); 97 const current_link: []const u8 = switch (current_tag) { 98 .home_users => users_link, 99 .home_new_repo => new_repo_link, 100 .home_auth => auth_link, 101 else => about_link, 102 }; 103 var selected_tab: ?usize = null; 104 105 // about tab 106 { 107 var text_box = try wgt.TextBox.init(allocator, about_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 108 errdefer text_box.deinit(allocator); 109 text_box.getFocus().mode = .all; 110 text_box.getFocus().kind = .{ .custom = about_link }; 111 try tab_ids.put(allocator, text_box.getFocus().id, {}); 112 if (std.mem.eql(u8, about_link, current_link)) selected_tab = text_box.getFocus().id; 113 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 114 .widget = .{ .text_box = text_box }, 115 .rect = null, 116 .min_size = .{ .width = about_tab_label.len + 2, .height = null }, 117 }); 118 } 119 120 // users tab 121 { 122 var text_box = try wgt.TextBox.init(allocator, users_tab_label, .{ .border_style = .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 = users_link }; 126 try tab_ids.put(allocator, text_box.getFocus().id, {}); 127 if (std.mem.eql(u8, users_link, current_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 = users_tab_label.len + 2, .height = null }, 132 }); 133 } 134 135 // spacer pushes new repo and auth to the right 136 { 137 var spacer = try ui.widget.Spacer.init(allocator); 138 errdefer spacer.deinit(allocator); 139 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ 140 .widget = .{ .spacer = spacer }, 141 .rect = null, 142 .min_size = null, 143 .flex = .grow, 144 }); 145 } 146 147 // new repo tab. repos are created for the logged-in user. 148 if (session.data.user_id != null) { 149 var text_box = try wgt.TextBox.init(allocator, new_repo_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = new_repo_tab_bottom_label }); 150 errdefer text_box.deinit(allocator); 151 text_box.getFocus().mode = .all; 152 text_box.getFocus().kind = .{ .custom = new_repo_link }; 153 try tab_ids.put(allocator, text_box.getFocus().id, {}); 154 if (std.mem.eql(u8, new_repo_link, current_link)) selected_tab = text_box.getFocus().id; 155 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 156 .widget = .{ .text_box = text_box }, 157 .rect = null, 158 .min_size = .{ .width = @max(new_repo_tab_label.len, new_repo_tab_bottom_label.len) + 2, .height = null }, 159 }); 160 } 161 162 // auth tab (login / logout) 163 { 164 var auth_tab = try AuthTab.View.init(allocator, session); 165 errdefer auth_tab.deinit(allocator); 166 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 167 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 168 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 169 try tabs_box.children.put(allocator, auth_tab.getFocus().id, .{ 170 .widget = .{ .auth_tab = auth_tab }, 171 .rect = null, 172 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 173 }); 174 } 175 176 // quit tab 177 if (session.is_terminal) { 178 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 179 errdefer text_box.deinit(allocator); 180 text_box.getFocus().mode = .all; 181 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 182 try tab_ids.put(allocator, text_box.getFocus().id, {}); 183 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 184 .widget = .{ .text_box = text_box }, 185 .rect = null, 186 // the label is a single column; +2 for the border 187 .min_size = .{ .width = 1 + 2, .height = null }, 188 }); 189 } 190 191 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 192 const title_id = title_box.getFocus().id; 193 const tabs_id = tabs_box.getFocus().id; 194 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 195 title_box_owned = true; 196 // a blank row between the title and the tabs, shown only when they wrap 197 const gap_id = blk: { 198 var gap = try wgt.Text.init(allocator, " "); 199 errdefer gap.deinit(allocator); 200 const id = gap.getFocus().id; 201 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 202 break :blk id; 203 }; 204 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 205 tabs_box_owned = true; 206 box.getFocus().child_id = tabs_id; 207 208 return .{ 209 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 210 .tab_ids = tab_ids, 211 .tabs_id = tabs_id, 212 .gap_id = gap_id, 213 .first_group_width = first_group_width, 214 .session = session, 215 }; 216 } 217 218 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 219 self.scroll.deinit(allocator); 220 self.tab_ids.deinit(allocator); 221 } 222 223 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 224 self.clearGrid(); 225 const box = &self.scroll.child.box; 226 const back_visible = self.session.back == .available; 227 ui.widget.setBackButtonVisible(box, back_visible); 228 const tabs_child = self.tabsChild(); 229 const tabs_box = &tabs_child.widget.box; 230 231 // only the selected tab shows its border 232 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 233 var tabs_width: usize = 0; 234 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 235 const tb: ?*wgt.TextBox = switch (child.widget) { 236 .text_box => |*x| x, 237 .auth_tab => |*at| blk: { 238 // the label tracks login state per frame, so the width must too 239 child.min_size = .{ .width = at.minWidth(), .height = null }; 240 break :blk &at.text_box; 241 }, 242 else => null, 243 }; 244 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 245 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 246 } 247 248 // the outer box's hidden border occupies two columns. keep the title 249 // and tabs together if they fit; otherwise tab strip on its own row. 250 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 251 const content_width = if (viewport_width) |width| width -| 2 else null; 252 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 253 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 254 box.options.direction = if (wrap) .vert else .horiz; 255 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 256 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 257 258 var scroll_constraint = constraint; 259 scroll_constraint.min_size.width = viewport_width; 260 try self.scroll.build(allocator, scroll_constraint, root_focus); 261 } 262 263 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 264 _ = allocator; 265 const current_tab = self.currentTabIndex() orelse return; 266 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 267 const tab_id = self.tab_ids.keys()[new_tab]; 268 root_focus.setFocus(tab_id); 269 const tabs_child = self.tabsChild(); 270 const tabs_rect = tabs_child.rect orelse return; 271 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 272 var rect = tab.rect orelse return; 273 rect.x += tabs_rect.x; 274 rect.y += tabs_rect.y; 275 self.scroll.scrollToRect(rect); 276 } 277 } 278 279 pub fn clearGrid(self: *View) void { 280 self.scroll.clearGrid(); 281 } 282 283 pub fn getGrid(self: View) ?Grid { 284 return self.scroll.getGrid(); 285 } 286 287 pub fn getFocus(self: *View) *Focus { 288 return self.scroll.getFocus(); 289 } 290 291 pub fn getSelectedIndex(self: View) ?usize { 292 return self.currentTabIndex(); 293 } 294 295 fn currentTabIndex(self: View) ?usize { 296 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 297 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 298 return self.tab_ids.getIndex(child_id); 299 } 300 301 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 302 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 303 } 304 }; 305