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