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