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