╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
68 69 try ui.widget.addBackButton(allocator, &title_box, session); 70 71 // the forker's name links to their page. 72 { 73 var text_buf: [evt.User.name_max_len + 1]u8 = undefined; 74 const text = try std.fmt.bufPrint(&text_buf, "{s}:", .{data.forker_name}); 75 var forker = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .wrap_kind = .none }); 76 errdefer forker.deinit(allocator); 77 forker.getFocus().mode = .all; 78 forker.getFocus().kind = .{ .custom = try aa.print("a:/{s}", .{data.forker_name}) }; 79 first_group_width += try xitui.width.displayWidth(text) + 2; 80 try title_box.children.put(allocator, forker.getFocus().id, .{ .widget = .{ .text_box = forker }, .rect = null, .min_size = null }); 81 } 82 83 // the title links to the fork's default tab. 84 { 85 const route = ui.RoutablePage.forkPatchRoute(fork_identity, data.id) orelse return error.RouteTooLong; 86 var title = try ui.Title.View.init(allocator, &data.title); 87 errdefer title.deinit(allocator); 88 title.getFocus().mode = .all; 89 title.getFocus().kind = .{ .custom = try aa.print("a:{s}", .{try route.toUrl(session.page_arena)}) }; 90 try title_box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .title = title }, .rect = null, .min_size = null }); 91 } 92 93 // separate the title from the tabs. 94 { 95 var spacer = try wgt.Text.init(allocator, " "); 96 errdefer spacer.deinit(allocator); 97 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .text = spacer }, .rect = null, .min_size = .{ .width = 1, .height = null } }); 98 } 99 100 const current_tag = std.meta.activeTag(session.data.current_page); 101 const routes = [_]ui.RoutablePage{ 102 ui.RoutablePage.forkPatchRoute(fork_identity, data.id) orelse return error.RouteTooLong, 103 ui.RoutablePage.forkDiffRoute(fork_identity, data.id, "", "", 0, "") orelse return error.RouteTooLong, 104 ui.RoutablePage.forkFilesRoute(fork_identity, data.id, data.oid, "", 0) orelse return error.RouteTooLong, 105 ui.RoutablePage.forkCommitsRoute(fork_identity, data.id, data.oid) orelse return error.RouteTooLong, 106 ui.RoutablePage.forkDeleteRoute(fork_identity, data.id) orelse return error.RouteTooLong, 107 }; 108 const tags = [_]std.meta.Tag(ui.RoutablePage){ .fork_patch, .fork_diff, .fork_files, .fork_commits, .fork_delete }; 109 const labels = [_][]const u8{ patch_tab_label, diff_tab_label, files_tab_label, commits_tab_label, delete_tab_label }; 110 // a single commit's diff names the commit under its tab, and commits 111 // shows its count there 112 const bottom_labels = [_][]const u8{ "", switch (session.data.current_page) { 113 .fork_diff => |*d| if (d.oid.len == 0) "" else try ui.clippedBottomLabel(try aa.alloc(u8, ui.clipped_bottom_label_max_len), d.oid.slice()), 114 else => "", 115 }, "", if (commit_count) |count| try aa.print("({d})", .{count}) else "", "" }; 116 var selected_tab: ?usize = null; 117 118 // the delete tab is last, so leaving it out keeps the stack's order 119 const tab_count = if (deletable) routes.len else routes.len - 1; 120 for (routes[0..tab_count], tags[0..tab_count], labels[0..tab_count], bottom_labels[0..tab_count]) |route, tag, label, bottom_label| { 121 const selected = current_tag == tag; 122 var tab = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = bottom_label } }); 123 errdefer tab.deinit(allocator); 124 tab.getFocus().mode = .all; 125 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 126 try tab_ids.put(allocator, tab.getFocus().id, {}); 127 if (selected) selected_tab = tab.getFocus().id; 128 try tabs_box.children.put(allocator, tab.getFocus().id, .{ 129 .widget = .{ .text_box = tab }, 130 .rect = null, 131 .min_size = .{ .width = label.len + 2, .height = null }, 132 }); 133 } 134 135 // spacer pushes new repo, new user, and user 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, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null, .flex = .grow }); 140 } 141 142 const new_repo_route = ui.RoutablePage.forkRepoNewRoute(fork_identity, data.id) orelse return error.RouteTooLong; 143 const new_repo_link = try ui.inPageTabLink(session, new_repo_route, current_tag == .fork_repo_new); 144 const new_user_route = ui.RoutablePage.forkUserNewRoute(fork_identity, data.id) orelse return error.RouteTooLong; 145 const new_user_link = try ui.inPageTabLink(session, new_user_route, current_tag == .fork_user_new); 146 const user_route = ui.RoutablePage.forkUserRoute(fork_identity, data.id) orelse return error.RouteTooLong; 147 const user_link = try ui.inPageTabLink(session, user_route, current_tag == .fork_user); 148 149 // new repo tab with a login, new user tab without one outside local mode 150 if (session.data.user_id != null) { 151 var new_repo = try wgt.TextBox.init(allocator, new_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = new_repo_tab_bottom_label } }); 152 errdefer new_repo.deinit(allocator); 153 new_repo.getFocus().mode = .all; 154 new_repo.getFocus().kind = .{ .custom = new_repo_link }; 155 try tab_ids.put(allocator, new_repo.getFocus().id, {}); 156 if (current_tag == .fork_repo_new) selected_tab = new_repo.getFocus().id; 157 try tabs_box.children.put(allocator, new_repo.getFocus().id, .{ .widget = .{ .text_box = new_repo }, .rect = null, .min_size = .{ .width = @max(new_tab_label.len, new_repo_tab_bottom_label.len) + 2, .height = null } }); 158 } else if (session.data.host_kind == .server) { 159 var new_user = try wgt.TextBox.init(allocator, new_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = new_user_tab_bottom_label } }); 160 errdefer new_user.deinit(allocator); 161 new_user.getFocus().mode = .all; 162 new_user.getFocus().kind = .{ .custom = new_user_link }; 163 try tab_ids.put(allocator, new_user.getFocus().id, {}); 164 if (current_tag == .fork_user_new) selected_tab = new_user.getFocus().id; 165 try tabs_box.children.put(allocator, new_user.getFocus().id, .{ .widget = .{ .text_box = new_user }, .rect = null, .min_size = .{ .width = @max(new_tab_label.len, new_user_tab_bottom_label.len) + 2, .height = null } }); 166 } 167 168 if (session.data.host_kind == .server) { 169 const label = ui.UserSettings.tabLabel(session); 170 const count_label = try ui.UserSettings.tabBottomLabel(session); 171 var user_tab = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = count_label }); 172 errdefer user_tab.deinit(allocator); 173 user_tab.getFocus().mode = .all; 174 user_tab.getFocus().kind = .{ .custom = user_link }; 175 try tab_ids.put(allocator, user_tab.getFocus().id, {}); 176 if (current_tag == .fork_user) selected_tab = user_tab.getFocus().id; 177 try tabs_box.children.put(allocator, user_tab.getFocus().id, .{ .widget = .{ .text_box = user_tab }, .rect = null, .min_size = .{ .width = @max(label.len, count_label.text.len) + 2, .height = null } }); 178 } 179 180 if (session.is_terminal) { 181 var quit = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 182 errdefer quit.deinit(allocator); 183 quit.getFocus().mode = .all; 184 quit.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 185 try tab_ids.put(allocator, quit.getFocus().id, {}); 186 try tabs_box.children.put(allocator, quit.getFocus().id, .{ .widget = .{ .text_box = quit }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 187 } 188 189 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 190 const title_id = title_box.getFocus().id; 191 const tabs_id = tabs_box.getFocus().id; 192 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 193 title_box_owned = true; 194 // a blank row between the title and the tabs, shown only when they wrap 195 const gap_id = blk: { 196 var gap = try wgt.Text.init(allocator, " "); 197 errdefer gap.deinit(allocator); 198 const id = gap.getFocus().id; 199 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 200 break :blk id; 201 }; 202 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 203 tabs_box_owned = true; 204 box.getFocus().child_id = tabs_id; 205 206 return .{ 207 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 208 .tab_ids = tab_ids, 209 .tabs_id = tabs_id, 210 .gap_id = gap_id, 211 .first_group_width = first_group_width, 212 .session = session, 213 }; 214 } 215 216 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 217 self.scroll.deinit(allocator); 218 self.tab_ids.deinit(allocator); 219 } 220 221 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 222 self.clearGrid(); 223 const box = &self.scroll.child.box; 224 const back_visible = self.session.back == .available; 225 ui.widget.setBackButtonVisible(box, back_visible); 226 const tabs_child = self.tabsChild(); 227 const tabs_box = &tabs_child.widget.box; 228 229 const selected_tab = tabs_box.getFocus().child_id; 230 var tabs_width: usize = 0; 231 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 232 const text_box: ?*wgt.TextBox = switch (child.widget) { 233 .text_box => |*value| value, 234 else => null, 235 }; 236 if (text_box) |tab| ui.widget.markSelected(tab, selected_tab == id); 237 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 238 } 239 240 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 241 const content_width = if (viewport_width) |width| width -| 2 else null; 242 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 243 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 244 box.options.direction = if (wrap) .vert else .horiz; 245 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 246 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 247 248 var scroll_constraint = constraint; 249 scroll_constraint.min_size.width = viewport_width; 250 try self.scroll.build(allocator, scroll_constraint, root_focus); 251 } 252 253 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 254 _ = allocator; 255 const current_tab = self.currentTabIndex() orelse return; 256 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 257 const tab_id = self.tab_ids.keys()[new_tab]; 258 root_focus.setFocus(tab_id); 259 const tabs_child = self.tabsChild(); 260 const tabs_rect = tabs_child.rect orelse return; 261 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 262 var rect = tab.rect orelse return; 263 rect.x += tabs_rect.x; 264 rect.y += tabs_rect.y; 265 self.scroll.scrollToRect(rect); 266 } 267 } 268 269 pub fn clearGrid(self: *View) void { 270 self.scroll.clearGrid(); 271 } 272 273 pub fn getGrid(self: View) ?Grid { 274 return self.scroll.getGrid(); 275 } 276 277 pub fn getFocus(self: *View) *Focus { 278 return self.scroll.getFocus(); 279 } 280 281 pub fn getSelectedIndex(self: View) ?usize { 282 return self.currentTabIndex(); 283 } 284 285 fn currentTabIndex(self: View) ?usize { 286 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 287 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 288 return self.tab_ids.getIndex(child_id); 289 } 290 291 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 292 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 293 } 294 }; 295