╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
73 74 // the title links to the target repository. 75 { 76 const route = ui.RoutablePage.repoFilesRoute(identity, null, "", "", 0) orelse return error.RouteTooLong; 77 var title = try ui.Title.View.init(allocator, &data.title); 78 errdefer title.deinit(allocator); 79 title.getFocus().mode = .all; 80 title.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}) }; 81 try title_box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .title = title }, .rect = null, .min_size = null }); 82 } 83 84 // separate the title from the tabs. 85 { 86 var spacer = try wgt.Text.init(allocator, " "); 87 errdefer spacer.deinit(allocator); 88 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .text = spacer }, .rect = null, .min_size = .{ .width = 1, .height = null } }); 89 } 90 91 const current_tag = std.meta.activeTag(session.data.current_page); 92 const routes = [_]ui.RoutablePage{ 93 ui.RoutablePage.forkPatchRoute(identity, data.id) orelse return error.RouteTooLong, 94 ui.RoutablePage.forkFilesRoute(identity, data.id, data.oid, "", 0) orelse return error.RouteTooLong, 95 ui.RoutablePage.forkCommitsRoute(identity, data.id, data.oid, 0, "") orelse return error.RouteTooLong, 96 }; 97 const tags = [_]std.meta.Tag(ui.RoutablePage){ .fork_patch, .fork_files, .fork_commits }; 98 const labels = [_][]const u8{ "patch", "files", commits_label }; 99 var selected_tab: ?usize = null; 100 101 for (routes, tags, labels) |route, tag, label| { 102 const selected = current_tag == tag; 103 var tab = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 104 errdefer tab.deinit(allocator); 105 tab.getFocus().mode = .all; 106 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 107 try tab_ids.put(allocator, tab.getFocus().id, {}); 108 if (selected) selected_tab = tab.getFocus().id; 109 try tabs_box.children.put(allocator, tab.getFocus().id, .{ 110 .widget = .{ .text_box = tab }, 111 .rect = null, 112 .min_size = .{ .width = label.len + 2, .height = null }, 113 }); 114 } 115 116 // spacer pushes settings + auth to the right. 117 { 118 var spacer = try ui.widget.Spacer.init(allocator); 119 errdefer spacer.deinit(allocator); 120 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null, .flex = .grow }); 121 } 122 123 const settings_route = ui.RoutablePage.forkSettingsRoute(identity, data.id) orelse return error.RouteTooLong; 124 const settings_link = try ui.inPageTabLink(session, settings_route, current_tag == .fork_settings); 125 const auth_route = ui.RoutablePage.forkAuthRoute(identity, data.id) orelse return error.RouteTooLong; 126 const auth_link = try ui.inPageTabLink(session, auth_route, current_tag == .fork_auth); 127 128 // settings are account preferences, so they require a login. 129 if (session.data.user_id != null) { 130 var settings = try wgt.TextBox.init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 131 errdefer settings.deinit(allocator); 132 settings.getFocus().mode = .all; 133 settings.getFocus().kind = .{ .custom = settings_link }; 134 try tab_ids.put(allocator, settings.getFocus().id, {}); 135 if (current_tag == .fork_settings) selected_tab = settings.getFocus().id; 136 try tabs_box.children.put(allocator, settings.getFocus().id, .{ .widget = .{ .text_box = settings }, .rect = null, .min_size = .{ .width = "settings".len + 2, .height = null } }); 137 } 138 139 // keep authentication within the fork page. 140 if (!session.data.is_local) { 141 var auth_tab = try AuthTab.View.init(allocator, session); 142 errdefer auth_tab.deinit(allocator); 143 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 144 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 145 if (current_tag == .fork_auth) selected_tab = auth_tab.getFocus().id; 146 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 } }); 147 } 148 149 if (session.is_terminal) { 150 var quit = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 151 errdefer quit.deinit(allocator); 152 quit.getFocus().mode = .all; 153 quit.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 154 try tab_ids.put(allocator, quit.getFocus().id, {}); 155 try tabs_box.children.put(allocator, quit.getFocus().id, .{ .widget = .{ .text_box = quit }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 156 } 157 158 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 159 const title_id = title_box.getFocus().id; 160 const tabs_id = tabs_box.getFocus().id; 161 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 162 title_box_owned = true; 163 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 164 tabs_box_owned = true; 165 box.getFocus().child_id = tabs_id; 166 167 return .{ 168 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 169 .tab_ids = tab_ids, 170 .tabs_id = tabs_id, 171 .first_group_width = first_group_width, 172 .session = session, 173 }; 174 } 175 176 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 177 self.scroll.deinit(allocator); 178 self.tab_ids.deinit(allocator); 179 } 180 181 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 182 self.clearGrid(); 183 const box = &self.scroll.child.box; 184 const back_visible = self.session.back == .available; 185 ui.widget.setBackButtonVisible(box, back_visible); 186 const tabs_child = self.tabsChild(); 187 const tabs_box = &tabs_child.widget.box; 188 189 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 190 var tabs_width: usize = 0; 191 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 192 const text_box: ?*wgt.TextBox = switch (child.widget) { 193 .text_box => |*value| value, 194 .auth_tab => |*auth_tab| blk: { 195 child.min_size = .{ .width = auth_tab.minWidth(), .height = null }; 196 break :blk &auth_tab.text_box; 197 }, 198 else => null, 199 }; 200 if (text_box) |tab| tab.options.border_style = if (selected_tab == id) .single else .hidden; 201 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 202 } 203 204 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 205 const content_width = if (viewport_width) |width| width -| 2 else null; 206 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 207 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 208 box.options.direction = if (wrap) .vert else .horiz; 209 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 210 211 var scroll_constraint = constraint; 212 scroll_constraint.min_size.width = viewport_width; 213 try self.scroll.build(allocator, scroll_constraint, root_focus); 214 } 215 216 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 217 _ = allocator; 218 const current_tab = self.currentTabIndex() orelse return; 219 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 220 const tab_id = self.tab_ids.keys()[new_tab]; 221 root_focus.setFocus(tab_id); 222 const tabs_child = self.tabsChild(); 223 const tabs_rect = tabs_child.rect orelse return; 224 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 225 var rect = tab.rect orelse return; 226 rect.x += tabs_rect.x; 227 rect.y += tabs_rect.y; 228 self.scroll.scrollToRect(rect); 229 } 230 } 231 232 pub fn clearGrid(self: *View) void { 233 self.scroll.clearGrid(); 234 } 235 236 pub fn getGrid(self: View) ?Grid { 237 return self.scroll.getGrid(); 238 } 239 240 pub fn getFocus(self: *View) *Focus { 241 return self.scroll.getFocus(); 242 } 243 244 pub fn getSelectedIndex(self: View) ?usize { 245 return self.currentTabIndex(); 246 } 247 248 fn currentTabIndex(self: View) ?usize { 249 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 250 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 251 return self.tab_ids.getIndex(child_id); 252 } 253 254 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 255 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 256 } 257 }; 258