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