╭ 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 pub const AuthTab = @import("../AuthTab.zig"); 14 15 name: []const u8, 16 owner_name: []const u8, 17 title: ui.Title, 18 id: []const u8, 19 oid: []const u8, 20 21 const Self = @This(); 22 23 pub fn init(arena: *std.heap.ArenaAllocator, name: []const u8, owner_name: []const u8, id: []const u8, oid: []const u8) !Self { 24 return .{ 25 .name = name, 26 .owner_name = owner_name, 27 .title = try ui.Title.init(arena, name), 28 .id = try arena.allocator().dupe(u8, id), 29 .oid = try arena.allocator().dupe(u8, oid), 30 }; 31 } 32 33 pub const View = struct { 34 scroll: wgt.Scroll(ui.Widget), 35 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 36 tabs_id: usize, 37 first_group_width: usize, 38 39 pub fn init(allocator: std.mem.Allocator, data: *const Self, commit_count: ?u64, session: *ui.Session) !View { 40 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 41 errdefer box.deinit(allocator); 42 43 var title_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 44 var title_box_owned = false; 45 errdefer if (!title_box_owned) title_box.deinit(allocator); 46 47 var tabs_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 48 var tabs_box_owned = false; 49 errdefer if (!tabs_box_owned) tabs_box.deinit(allocator); 50 51 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 52 errdefer tab_ids.deinit(allocator); 53 54 const aa = session.page_arena.allocator(); 55 const identity = try std.fmt.allocPrint(aa, "{s}/{s}", .{ data.owner_name, data.name }); 56 const commits_label = if (commit_count) |count| try std.fmt.allocPrint(aa, "commits ({d})", .{count}) else "commits"; 57 var first_group_width = try data.title.width(); 58 59 // the user's name links to their page. 60 { 61 var text_buf: [evt.User.name_max_len + 1]u8 = undefined; 62 const text = try std.fmt.bufPrint(&text_buf, "{s}/", .{data.owner_name}); 63 var owner = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .wrap_kind = .none }); 64 errdefer owner.deinit(allocator); 65 owner.getFocus().focusable = true; 66 owner.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "a:/user/{s}", .{data.owner_name}) }; 67 first_group_width += try xitui.width.displayWidth(text) + 2; 68 try title_box.children.put(allocator, owner.getFocus().id, .{ .widget = .{ .text_box = owner }, .rect = null, .min_size = null }); 69 } 70 71 // the title links to the target repository. 72 { 73 const route = ui.RoutablePage.repoFilesRoute(identity, null, "", "", 0) orelse return error.RouteTooLong; 74 var title = try ui.Title.View.init(allocator, &data.title); 75 errdefer title.deinit(allocator); 76 title.getFocus().focusable = true; 77 title.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}) }; 78 try title_box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .title = title }, .rect = null, .min_size = null }); 79 } 80 81 // separate the title from the tabs. 82 { 83 var spacer = try wgt.Text.init(allocator, " "); 84 errdefer spacer.deinit(allocator); 85 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .text = spacer }, .rect = null, .min_size = .{ .width = 1, .height = null } }); 86 } 87 88 const current_tag = std.meta.activeTag(session.data.current_page); 89 const routes = [_]ui.RoutablePage{ 90 ui.RoutablePage.forkPatchRoute(identity, data.id) orelse return error.RouteTooLong, 91 ui.RoutablePage.forkFilesRoute(identity, data.id, data.oid, "", 0) orelse return error.RouteTooLong, 92 ui.RoutablePage.forkCommitsRoute(identity, data.id, data.oid, 0, "") orelse return error.RouteTooLong, 93 }; 94 const tags = [_]std.meta.Tag(ui.RoutablePage){ .fork_patch, .fork_files, .fork_commits }; 95 const labels = [_][]const u8{ "patch", "files", commits_label }; 96 var selected_tab: ?usize = null; 97 98 for (routes, tags, labels) |route, tag, label| { 99 const selected = current_tag == tag; 100 var tab = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 101 errdefer tab.deinit(allocator); 102 tab.getFocus().focusable = true; 103 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 104 try tab_ids.put(allocator, tab.getFocus().id, {}); 105 if (selected) selected_tab = tab.getFocus().id; 106 try tabs_box.children.put(allocator, tab.getFocus().id, .{ 107 .widget = .{ .text_box = tab }, 108 .rect = null, 109 .min_size = .{ .width = label.len + 2, .height = null }, 110 }); 111 } 112 113 // spacer pushes settings + auth to the right. 114 { 115 var spacer = try ui.widget.Spacer.init(allocator); 116 errdefer spacer.deinit(allocator); 117 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null, .flex = .grow }); 118 } 119 120 const settings_route = ui.RoutablePage.forkSettingsRoute(identity, data.id) orelse return error.RouteTooLong; 121 const settings_link = try ui.inPageTabLink(session, settings_route, current_tag == .fork_settings); 122 const auth_route = ui.RoutablePage.forkAuthRoute(identity, data.id) orelse return error.RouteTooLong; 123 const auth_link = try ui.inPageTabLink(session, auth_route, current_tag == .fork_auth); 124 125 // settings are account preferences, so they require a login. 126 if (session.data.user_id != null) { 127 var settings = try wgt.TextBox.init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 128 errdefer settings.deinit(allocator); 129 settings.getFocus().focusable = true; 130 settings.getFocus().kind = .{ .custom = settings_link }; 131 try tab_ids.put(allocator, settings.getFocus().id, {}); 132 if (current_tag == .fork_settings) selected_tab = settings.getFocus().id; 133 try tabs_box.children.put(allocator, settings.getFocus().id, .{ .widget = .{ .text_box = settings }, .rect = null, .min_size = .{ .width = "settings".len + 2, .height = null } }); 134 } 135 136 // keep authentication within the fork page. 137 if (!session.data.is_local) { 138 var auth_tab = try AuthTab.View.init(allocator, session); 139 errdefer auth_tab.deinit(allocator); 140 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 141 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 142 if (current_tag == .fork_auth) selected_tab = auth_tab.getFocus().id; 143 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 } }); 144 } 145 146 if (session.is_terminal) { 147 var quit = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 148 errdefer quit.deinit(allocator); 149 quit.getFocus().focusable = true; 150 quit.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 151 try tab_ids.put(allocator, quit.getFocus().id, {}); 152 try tabs_box.children.put(allocator, quit.getFocus().id, .{ .widget = .{ .text_box = quit }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 153 } 154 155 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 156 const title_id = title_box.getFocus().id; 157 const tabs_id = tabs_box.getFocus().id; 158 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 159 title_box_owned = true; 160 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 161 tabs_box_owned = true; 162 box.getFocus().child_id = tabs_id; 163 164 return .{ 165 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 166 .tab_ids = tab_ids, 167 .tabs_id = tabs_id, 168 .first_group_width = first_group_width, 169 }; 170 } 171 172 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 173 self.scroll.deinit(allocator); 174 self.tab_ids.deinit(allocator); 175 } 176 177 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 178 self.clearGrid(); 179 const box = &self.scroll.child.box; 180 const tabs_child = self.tabsChild(); 181 const tabs_box = &tabs_child.widget.box; 182 183 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 184 var tabs_width: usize = 0; 185 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 186 const text_box: ?*wgt.TextBox = switch (child.widget) { 187 .text_box => |*value| value, 188 .auth_tab => |*auth_tab| blk: { 189 child.min_size = .{ .width = auth_tab.minWidth(), .height = null }; 190 break :blk &auth_tab.text_box; 191 }, 192 else => null, 193 }; 194 if (text_box) |tab| tab.options.border_style = if (selected_tab == id) .single else .hidden; 195 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 196 } 197 198 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 199 const content_width = if (viewport_width) |width| width -| 2 else null; 200 const wrap = if (content_width) |width| self.first_group_width + tabs_width > width else false; 201 box.options.direction = if (wrap) .vert else .horiz; 202 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 203 204 var scroll_constraint = constraint; 205 scroll_constraint.min_size.width = viewport_width; 206 try self.scroll.build(allocator, scroll_constraint, root_focus); 207 } 208 209 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 210 _ = allocator; 211 const current_tab = self.currentTabIndex() orelse return; 212 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 213 const tab_id = self.tab_ids.keys()[new_tab]; 214 root_focus.setFocus(tab_id); 215 const tabs_child = self.tabsChild(); 216 const tabs_rect = tabs_child.rect orelse return; 217 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 218 var rect = tab.rect orelse return; 219 rect.x += tabs_rect.x; 220 rect.y += tabs_rect.y; 221 self.scroll.scrollToRect(rect); 222 } 223 } 224 225 pub fn clearGrid(self: *View) void { 226 self.scroll.clearGrid(); 227 } 228 229 pub fn getGrid(self: View) ?Grid { 230 return self.scroll.getGrid(); 231 } 232 233 pub fn getFocus(self: *View) *Focus { 234 return self.scroll.getFocus(); 235 } 236 237 pub fn getSelectedIndex(self: View) ?usize { 238 return self.currentTabIndex(); 239 } 240 241 fn currentTabIndex(self: View) ?usize { 242 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 243 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 244 return self.tab_ids.getIndex(child_id); 245 } 246 247 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 248 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 249 } 250 }; 251