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