╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("../../ui.zig"); 3 const inp = @import("../input.zig"); 4 const xit = @import("xit"); 5 const xitui = xit.xitui; 6 const wgt = xitui.widget; 7 const layout = xitui.layout; 8 const Key = xitui.input.Key; 9 const Grid = xitui.grid.Grid; 10 const Focus = xitui.focus.Focus; 11 12 const about_tab_label = "about"; 13 const users_tab_label = "users"; 14 const new_tab_label = "new"; 15 const new_repo_tab_bottom_label = "repo"; 16 const new_user_tab_bottom_label = "user"; 17 18 // null hides the title 19 title: ?ui.Title, 20 21 const Self = @This(); 22 23 pub fn init(arena: *std.heap.ArenaAllocator, title: ?[]const u8) !Self { 24 return .{ 25 .title = if (title) |t| try ui.Title.init(arena, t, .scanlines) else null, 26 }; 27 } 28 29 pub const View = struct { 30 scroll: wgt.Scroll(ui.Widget), 31 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 32 tabs_id: usize, 33 gap_id: usize, 34 first_group_width: usize, 35 session: *ui.Session, 36 37 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 38 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = .hidden, .round_corners = true, .direction = .horiz }); 39 errdefer box.deinit(allocator); 40 41 var title_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 42 var title_box_owned = false; 43 errdefer if (!title_box_owned) title_box.deinit(allocator); 44 45 var tabs_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 46 var tabs_box_owned = false; 47 errdefer if (!tabs_box_owned) tabs_box.deinit(allocator); 48 49 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 50 errdefer tab_ids.deinit(allocator); 51 52 // the leading space plus the title 53 const first_group_width = if (data.title) |title| 1 + try title.width() else 0; 54 55 try ui.widget.addBackButton(allocator, &title_box, session); 56 57 // title sits to the left of the tabs 58 if (data.title) |*title| { 59 { 60 var text = try wgt.Text.init(allocator, " "); 61 errdefer text.deinit(allocator); 62 try title_box.children.put(allocator, text.getFocus().id, .{ 63 .widget = .{ .text = text }, 64 .rect = null, 65 .min_size = .{ .width = 1, .height = null }, 66 }); 67 } 68 69 var title_view = try ui.Title.View.init(allocator, title); 70 errdefer title_view.deinit(allocator); 71 try title_box.children.put(allocator, title_view.getFocus().id, .{ 72 .widget = .{ .title = title_view }, 73 .rect = null, 74 .min_size = null, 75 }); 76 } 77 78 // spacer 79 { 80 var text = try wgt.Text.init(allocator, " "); 81 errdefer text.deinit(allocator); 82 try tabs_box.children.put(allocator, text.getFocus().id, .{ 83 .widget = .{ .text = text }, 84 .rect = null, 85 .min_size = .{ .width = 1, .height = null }, 86 }); 87 } 88 89 // the tab matching the current page is focused initially; matching by 90 // link (rather than position) keeps this robust to tab changes. 91 const current_tag = std.meta.activeTag(session.data.current_page); 92 const about_link = try ui.inPageTabLink(session, .home_about, current_tag == .home_about); 93 const users_link = try ui.inPageTabLink(session, .{ .home_users = .{} }, current_tag == .home_users); 94 const new_repo_link = try ui.inPageTabLink(session, .home_repo_new, current_tag == .home_repo_new); 95 const new_user_link = try ui.inPageTabLink(session, .home_user_new, current_tag == .home_user_new); 96 const user_link = try ui.inPageTabLink(session, .home_user, current_tag == .home_user); 97 const current_link: []const u8 = switch (current_tag) { 98 .home_users => users_link, 99 .home_repo_new => new_repo_link, 100 .home_user_new => new_user_link, 101 .home_user => user_link, 102 else => about_link, 103 }; 104 var selected_tab: ?usize = null; 105 106 // about tab 107 { 108 var text_box = try wgt.TextBox.init(allocator, about_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 109 errdefer text_box.deinit(allocator); 110 text_box.getFocus().mode = .all; 111 text_box.getFocus().kind = .{ .custom = about_link }; 112 try tab_ids.put(allocator, text_box.getFocus().id, {}); 113 if (std.mem.eql(u8, about_link, current_link)) selected_tab = text_box.getFocus().id; 114 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 115 .widget = .{ .text_box = text_box }, 116 .rect = null, 117 .min_size = .{ .width = about_tab_label.len + 2, .height = null }, 118 }); 119 } 120 121 // users tab 122 { 123 var text_box = try wgt.TextBox.init(allocator, users_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 124 errdefer text_box.deinit(allocator); 125 text_box.getFocus().mode = .all; 126 text_box.getFocus().kind = .{ .custom = users_link }; 127 try tab_ids.put(allocator, text_box.getFocus().id, {}); 128 if (std.mem.eql(u8, users_link, current_link)) selected_tab = text_box.getFocus().id; 129 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 130 .widget = .{ .text_box = text_box }, 131 .rect = null, 132 .min_size = .{ .width = users_tab_label.len + 2, .height = null }, 133 }); 134 } 135 136 // spacer pushes new repo, new user, and user to the right 137 { 138 var spacer = try ui.widget.Spacer.init(allocator); 139 errdefer spacer.deinit(allocator); 140 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ 141 .widget = .{ .spacer = spacer }, 142 .rect = null, 143 .min_size = null, 144 .flex = .grow, 145 }); 146 } 147 148 // new repo tab with a login, new user tab without one 149 if (session.data.user_id != null) { 150 var text_box = try wgt.TextBox.init(allocator, new_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = new_repo_tab_bottom_label } }); 151 errdefer text_box.deinit(allocator); 152 text_box.getFocus().mode = .all; 153 text_box.getFocus().kind = .{ .custom = new_repo_link }; 154 try tab_ids.put(allocator, text_box.getFocus().id, {}); 155 if (std.mem.eql(u8, new_repo_link, current_link)) selected_tab = text_box.getFocus().id; 156 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 157 .widget = .{ .text_box = text_box }, 158 .rect = null, 159 .min_size = .{ .width = @max(new_tab_label.len, new_repo_tab_bottom_label.len) + 2, .height = null }, 160 }); 161 } else { 162 var text_box = try wgt.TextBox.init(allocator, new_tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = new_user_tab_bottom_label } }); 163 errdefer text_box.deinit(allocator); 164 text_box.getFocus().mode = .all; 165 text_box.getFocus().kind = .{ .custom = new_user_link }; 166 try tab_ids.put(allocator, text_box.getFocus().id, {}); 167 if (std.mem.eql(u8, new_user_link, current_link)) selected_tab = text_box.getFocus().id; 168 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 169 .widget = .{ .text_box = text_box }, 170 .rect = null, 171 .min_size = .{ .width = @max(new_tab_label.len, new_user_tab_bottom_label.len) + 2, .height = null }, 172 }); 173 } 174 175 // user tab 176 { 177 const label = ui.UserSettings.tabLabel(session); 178 var text_box = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 179 errdefer text_box.deinit(allocator); 180 text_box.getFocus().mode = .all; 181 text_box.getFocus().kind = .{ .custom = user_link }; 182 try tab_ids.put(allocator, text_box.getFocus().id, {}); 183 if (std.mem.eql(u8, user_link, current_link)) selected_tab = text_box.getFocus().id; 184 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 185 .widget = .{ .text_box = text_box }, 186 .rect = null, 187 .min_size = .{ .width = label.len + 2, .height = null }, 188 }); 189 } 190 191 // quit tab 192 if (session.is_terminal) { 193 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 194 errdefer text_box.deinit(allocator); 195 text_box.getFocus().mode = .all; 196 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 197 try tab_ids.put(allocator, text_box.getFocus().id, {}); 198 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 199 .widget = .{ .text_box = text_box }, 200 .rect = null, 201 // the label is a single column; +2 for the border 202 .min_size = .{ .width = 1 + 2, .height = null }, 203 }); 204 } 205 206 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 207 const title_id = title_box.getFocus().id; 208 const tabs_id = tabs_box.getFocus().id; 209 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 210 title_box_owned = true; 211 // a blank row between the title and the tabs, shown only when they wrap 212 const gap_id = blk: { 213 var gap = try wgt.Text.init(allocator, " "); 214 errdefer gap.deinit(allocator); 215 const id = gap.getFocus().id; 216 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 217 break :blk id; 218 }; 219 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 220 tabs_box_owned = true; 221 box.getFocus().child_id = tabs_id; 222 223 return .{ 224 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 225 .tab_ids = tab_ids, 226 .tabs_id = tabs_id, 227 .gap_id = gap_id, 228 .first_group_width = first_group_width, 229 .session = session, 230 }; 231 } 232 233 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 234 self.scroll.deinit(allocator); 235 self.tab_ids.deinit(allocator); 236 } 237 238 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 239 self.clearGrid(); 240 const box = &self.scroll.child.box; 241 const back_visible = self.session.back == .available; 242 ui.widget.setBackButtonVisible(box, back_visible); 243 const tabs_child = self.tabsChild(); 244 const tabs_box = &tabs_child.widget.box; 245 246 // only the selected tab shows its border 247 const selected_tab = tabs_box.getFocus().child_id; 248 var tabs_width: usize = 0; 249 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 250 const tb: ?*wgt.TextBox = switch (child.widget) { 251 .text_box => |*x| x, 252 else => null, 253 }; 254 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 255 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 256 } 257 258 // the outer box's hidden border occupies two columns. keep the title 259 // and tabs together if they fit; otherwise tab strip on its own row. 260 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 261 const content_width = if (viewport_width) |width| width -| 2 else null; 262 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 263 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 264 box.options.direction = if (wrap) .vert else .horiz; 265 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 266 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 267 268 var scroll_constraint = constraint; 269 scroll_constraint.min_size.width = viewport_width; 270 try self.scroll.build(allocator, scroll_constraint, root_focus); 271 } 272 273 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 274 _ = allocator; 275 const current_tab = self.currentTabIndex() orelse return; 276 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 277 const tab_id = self.tab_ids.keys()[new_tab]; 278 root_focus.setFocus(tab_id); 279 const tabs_child = self.tabsChild(); 280 const tabs_rect = tabs_child.rect orelse return; 281 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 282 var rect = tab.rect orelse return; 283 rect.x += tabs_rect.x; 284 rect.y += tabs_rect.y; 285 self.scroll.scrollToRect(rect); 286 } 287 } 288 289 pub fn clearGrid(self: *View) void { 290 self.scroll.clearGrid(); 291 } 292 293 pub fn getGrid(self: View) ?Grid { 294 return self.scroll.getGrid(); 295 } 296 297 pub fn getFocus(self: *View) *Focus { 298 return self.scroll.getFocus(); 299 } 300 301 pub fn getSelectedIndex(self: View) ?usize { 302 return self.currentTabIndex(); 303 } 304 305 fn currentTabIndex(self: View) ?usize { 306 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 307 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 308 return self.tab_ids.getIndex(child_id); 309 } 310 311 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 312 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 313 } 314 }; 315