╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
49 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .round_corners = true, .direction = .horiz }); 50 errdefer box.deinit(allocator); 51 52 var title_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = 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_style = 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 std.fmt.allocPrint(aa, "{s}/{s}", .{ data.forker_name, data.name }); 65 const commits_label = if (commit_count) |count| try std.fmt.allocPrint(aa, 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_style = .hidden, .wrap_kind = .none }); 75 errdefer forker.deinit(allocator); 76 forker.getFocus().mode = .all; 77 forker.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "a:/user/{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 std.fmt.allocPrint(aa, "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, 0, "") 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 var selected_tab: ?usize = null; 109 110 for (routes, tags, labels) |route, tag, label| { 111 const selected = current_tag == tag; 112 var tab = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 113 errdefer tab.deinit(allocator); 114 tab.getFocus().mode = .all; 115 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 116 try tab_ids.put(allocator, tab.getFocus().id, {}); 117 if (selected) selected_tab = tab.getFocus().id; 118 try tabs_box.children.put(allocator, tab.getFocus().id, .{ 119 .widget = .{ .text_box = tab }, 120 .rect = null, 121 .min_size = .{ .width = label.len + 2, .height = null }, 122 }); 123 } 124 125 // spacer pushes new repo and auth to the right. 126 { 127 var spacer = try ui.widget.Spacer.init(allocator); 128 errdefer spacer.deinit(allocator); 129 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null, .flex = .grow }); 130 } 131 132 const new_repo_route = ui.RoutablePage.forkNewRepoRoute(fork_identity, data.id) orelse return error.RouteTooLong; 133 const new_repo_link = try ui.inPageTabLink(session, new_repo_route, current_tag == .fork_new_repo); 134 const auth_route = ui.RoutablePage.forkAuthRoute(fork_identity, data.id) orelse return error.RouteTooLong; 135 const auth_link = try ui.inPageTabLink(session, auth_route, current_tag == .fork_auth); 136 137 // repos are created for the logged-in user. 138 if (session.data.user_id != null) { 139 var new_repo = 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 }); 140 errdefer new_repo.deinit(allocator); 141 new_repo.getFocus().mode = .all; 142 new_repo.getFocus().kind = .{ .custom = new_repo_link }; 143 try tab_ids.put(allocator, new_repo.getFocus().id, {}); 144 if (current_tag == .fork_new_repo) selected_tab = new_repo.getFocus().id; 145 try tabs_box.children.put(allocator, new_repo.getFocus().id, .{ .widget = .{ .text_box = new_repo }, .rect = null, .min_size = .{ .width = @max(new_repo_tab_label.len, new_repo_tab_bottom_label.len) + 2, .height = null } }); 146 } 147 148 // keep authentication within the fork page. 149 if (session.data.host_kind == .server) { 150 var auth_tab = try AuthTab.View.init(allocator, session); 151 errdefer auth_tab.deinit(allocator); 152 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 153 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 154 if (current_tag == .fork_auth) selected_tab = auth_tab.getFocus().id; 155 try tabs_box.children.put(allocator, auth_tab.getFocus().id, .{ .widget = .{ .auth_tab = auth_tab }, .rect = null, .min_size = .{ .width = auth_tab.minWidth(), .height = null } }); 156 } 157 158 if (session.is_terminal) { 159 var quit = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 160 errdefer quit.deinit(allocator); 161 quit.getFocus().mode = .all; 162 quit.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 163 try tab_ids.put(allocator, quit.getFocus().id, {}); 164 try tabs_box.children.put(allocator, quit.getFocus().id, .{ .widget = .{ .text_box = quit }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 165 } 166 167 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 168 const title_id = title_box.getFocus().id; 169 const tabs_id = tabs_box.getFocus().id; 170 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 171 title_box_owned = true; 172 // a blank row between the title and the tabs, shown only when they wrap 173 const gap_id = blk: { 174 var gap = try wgt.Text.init(allocator, " "); 175 errdefer gap.deinit(allocator); 176 const id = gap.getFocus().id; 177 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 178 break :blk id; 179 }; 180 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 181 tabs_box_owned = true; 182 box.getFocus().child_id = tabs_id; 183 184 return .{ 185 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 186 .tab_ids = tab_ids, 187 .tabs_id = tabs_id, 188 .gap_id = gap_id, 189 .first_group_width = first_group_width, 190 .session = session, 191 }; 192 } 193 194 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 195 self.scroll.deinit(allocator); 196 self.tab_ids.deinit(allocator); 197 } 198 199 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 200 self.clearGrid(); 201 const box = &self.scroll.child.box; 202 const back_visible = self.session.back == .available; 203 ui.widget.setBackButtonVisible(box, back_visible); 204 const tabs_child = self.tabsChild(); 205 const tabs_box = &tabs_child.widget.box; 206 207 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 208 var tabs_width: usize = 0; 209 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 210 const text_box: ?*wgt.TextBox = switch (child.widget) { 211 .text_box => |*value| value, 212 .auth_tab => |*auth_tab| blk: { 213 child.min_size = .{ .width = auth_tab.minWidth(), .height = null }; 214 break :blk &auth_tab.text_box; 215 }, 216 else => null, 217 }; 218 if (text_box) |tab| ui.widget.markSelected(tab, selected_tab == id); 219 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 220 } 221 222 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 223 const content_width = if (viewport_width) |width| width -| 2 else null; 224 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 225 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 226 box.options.direction = if (wrap) .vert else .horiz; 227 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 228 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 229 230 var scroll_constraint = constraint; 231 scroll_constraint.min_size.width = viewport_width; 232 try self.scroll.build(allocator, scroll_constraint, root_focus); 233 } 234 235 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 236 _ = allocator; 237 const current_tab = self.currentTabIndex() orelse return; 238 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 239 const tab_id = self.tab_ids.keys()[new_tab]; 240 root_focus.setFocus(tab_id); 241 const tabs_child = self.tabsChild(); 242 const tabs_rect = tabs_child.rect orelse return; 243 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 244 var rect = tab.rect orelse return; 245 rect.x += tabs_rect.x; 246 rect.y += tabs_rect.y; 247 self.scroll.scrollToRect(rect); 248 } 249 } 250 251 pub fn clearGrid(self: *View) void { 252 self.scroll.clearGrid(); 253 } 254 255 pub fn getGrid(self: View) ?Grid { 256 return self.scroll.getGrid(); 257 } 258 259 pub fn getFocus(self: *View) *Focus { 260 return self.scroll.getFocus(); 261 } 262 263 pub fn getSelectedIndex(self: View) ?usize { 264 return self.currentTabIndex(); 265 } 266 267 fn currentTabIndex(self: View) ?usize { 268 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 269 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 270 return self.tab_ids.getIndex(child_id); 271 } 272 273 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 274 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 275 } 276 }; 277