╭ 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 pub const AuthTab = @import("./../AuthTab.zig"); 13 14 name: []const u8, 15 title: ui.Title, 16 17 const Self = @This(); 18 19 pub fn init(arena: *std.heap.ArenaAllocator, name: []const u8) !Self { 20 return .{ 21 .name = name, 22 .title = try ui.Title.init(arena, name), 23 }; 24 } 25 26 pub const View = struct { 27 scroll: wgt.Scroll(ui.Widget), 28 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 29 30 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 31 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 32 errdefer box.deinit(allocator); 33 34 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 35 errdefer tab_ids.deinit(allocator); 36 37 // title sits to the left of the tabs 38 { 39 var title_view = try ui.Title.View.init(allocator, &data.title); 40 errdefer title_view.deinit(allocator); 41 title_view.getFocus().focusable = true; 42 title_view.getFocus().kind = .{ .custom = "a:/" }; 43 // shrink the title when there is not enough space 44 try box.children.put(allocator, title_view.getFocus().id, .{ 45 .widget = .{ .title = title_view }, 46 .rect = null, 47 .min_size = null, 48 .flex = .shrink, 49 }); 50 } 51 52 // spacer 53 { 54 var text = try wgt.Text.init(allocator, " "); 55 errdefer text.deinit(allocator); 56 try box.children.put(allocator, text.getFocus().id, .{ 57 .widget = .{ .text = text }, 58 .rect = null, 59 .min_size = .{ .width = 1, .height = null }, 60 }); 61 } 62 63 // every tab link is user-scoped so selecting one stays on this page — 64 // switching its stack and updating the url — instead of navigating to 65 // the global settings or auth pages. the "ai:" prefix makes each an 66 // in-page anchor: crossPageLink ignores it so a wasm click just switches 67 // tabs (the page already holds every tab's content), while the href is 68 // still followed with js off. allocated in the page arena so the focus 69 // kinds that borrow them live as long as this page's widget tree. 70 const aa = session.page_arena.allocator(); 71 const current_page = session.data.current_page; 72 const current_tag = std.meta.activeTag(current_page); 73 const current_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try current_page.toUrl(session.page_arena)}); 74 const repos_link = if (current_tag == .user_repos) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}", .{data.name}); 75 const forks_link = if (current_tag == .user_forks) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/forks", .{data.name}); 76 const settings_link = if (current_tag == .user_settings) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/settings", .{data.name}); 77 const auth_link = if (current_tag == .user_auth) current_link else try std.fmt.allocPrint(aa, "ai:/user/{s}/auth", .{data.name}); 78 79 // the tab matching the current page is focused initially; matching by 80 // link (rather than position) keeps this robust to tab changes. 81 const selected_link: []const u8 = switch (current_page) { 82 .user_settings => settings_link, 83 .user_auth => auth_link, 84 .user_forks => forks_link, 85 else => repos_link, 86 }; 87 var selected_tab: ?usize = null; 88 89 // repos tab 90 { 91 var text_box = try wgt.TextBox.init(allocator, "repos", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 92 errdefer text_box.deinit(allocator); 93 text_box.getFocus().focusable = true; 94 text_box.getFocus().kind = .{ .custom = repos_link }; 95 try tab_ids.put(allocator, text_box.getFocus().id, {}); 96 if (std.mem.eql(u8, repos_link, selected_link)) selected_tab = text_box.getFocus().id; 97 try box.children.put(allocator, text_box.getFocus().id, .{ 98 .widget = .{ .text_box = text_box }, 99 .rect = null, 100 .min_size = .{ .width = "repos".len + 2, .height = null }, 101 }); 102 } 103 104 // forks tab 105 { 106 var text_box = try wgt.TextBox.init(allocator, "forks", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 107 errdefer text_box.deinit(allocator); 108 text_box.getFocus().focusable = true; 109 text_box.getFocus().kind = .{ .custom = forks_link }; 110 try tab_ids.put(allocator, text_box.getFocus().id, {}); 111 if (std.mem.eql(u8, forks_link, selected_link)) selected_tab = text_box.getFocus().id; 112 try box.children.put(allocator, text_box.getFocus().id, .{ 113 .widget = .{ .text_box = text_box }, 114 .rect = null, 115 .min_size = .{ .width = "forks".len + 2, .height = null }, 116 }); 117 } 118 119 // spacer pushes settings + auth to the right 120 { 121 var spacer = try ui.widget.Spacer.init(allocator); 122 errdefer spacer.deinit(allocator); 123 try box.children.put(allocator, spacer.getFocus().id, .{ 124 .widget = .{ .spacer = spacer }, 125 .rect = null, 126 .min_size = null, 127 .flex = .grow, 128 }); 129 } 130 131 // settings tab. settings are account preferences, so it needs a login. 132 if (session.data.user_id != null) { 133 var text_box = try wgt.TextBox.init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 134 errdefer text_box.deinit(allocator); 135 text_box.getFocus().focusable = true; 136 text_box.getFocus().kind = .{ .custom = settings_link }; 137 try tab_ids.put(allocator, text_box.getFocus().id, {}); 138 if (std.mem.eql(u8, settings_link, selected_link)) selected_tab = text_box.getFocus().id; 139 try box.children.put(allocator, text_box.getFocus().id, .{ 140 .widget = .{ .text_box = text_box }, 141 .rect = null, 142 .min_size = .{ .width = "settings".len + 2, .height = null }, 143 }); 144 } 145 146 // auth tab (login / logout). AuthTab defaults to the global ai:/auth 147 // link; repoint its instance at this user's auth route so it stays local. 148 { 149 var auth_tab = try AuthTab.View.init(allocator, session); 150 errdefer auth_tab.deinit(allocator); 151 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 152 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 153 if (std.mem.eql(u8, auth_link, selected_link)) selected_tab = auth_tab.getFocus().id; 154 try box.children.put(allocator, auth_tab.getFocus().id, .{ 155 .widget = .{ .auth_tab = auth_tab }, 156 .rect = null, 157 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 158 }); 159 } 160 161 // quit tab 162 if (session.is_terminal) { 163 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 164 errdefer text_box.deinit(allocator); 165 text_box.getFocus().focusable = true; 166 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 167 try tab_ids.put(allocator, text_box.getFocus().id, {}); 168 try box.children.put(allocator, text_box.getFocus().id, .{ 169 .widget = .{ .text_box = text_box }, 170 .rect = null, 171 // the label is a single column; +2 for the border 172 .min_size = .{ .width = 1 + 2, .height = null }, 173 }); 174 } 175 176 var self = View{ 177 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 178 .tab_ids = tab_ids, 179 }; 180 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 181 return self; 182 } 183 184 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 185 self.scroll.deinit(allocator); 186 self.tab_ids.deinit(allocator); 187 } 188 189 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 190 self.clearGrid(); 191 // only the selected tab shows its border 192 const box = &self.scroll.child.box; 193 for (box.children.keys(), box.children.values()) |id, *child| { 194 const tb: ?*wgt.TextBox = switch (child.widget) { 195 .text_box => |*x| x, 196 .auth_tab => |*at| blk: { 197 // the label tracks login state per frame, so the width must too 198 child.min_size = .{ .width = at.minWidth(), .height = null }; 199 break :blk &at.text_box; 200 }, 201 else => null, 202 }; 203 if (tb) |t| { 204 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 205 } 206 } 207 var scroll_constraint = constraint; 208 scroll_constraint.min_size.width = constraint.max_size.width orelse constraint.min_size.width; 209 try self.scroll.build(allocator, scroll_constraint, root_focus); 210 } 211 212 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 213 _ = allocator; 214 const current_tab = self.currentTabIndex() orelse return; 215 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 216 const tab_id = self.tab_ids.keys()[new_tab]; 217 root_focus.setFocus(tab_id); 218 const tab = self.scroll.child.box.children.get(tab_id) orelse return; 219 if (tab.rect) |rect| self.scroll.scrollToRect(rect); 220 } 221 } 222 223 pub fn clearGrid(self: *View) void { 224 self.scroll.clearGrid(); 225 } 226 227 pub fn getGrid(self: View) ?Grid { 228 return self.scroll.getGrid(); 229 } 230 231 pub fn getFocus(self: *View) *Focus { 232 return self.scroll.getFocus(); 233 } 234 235 pub fn getSelectedIndex(self: View) ?usize { 236 return self.currentTabIndex(); 237 } 238 239 fn currentTabIndex(self: View) ?usize { 240 const child_id = self.scroll.child.box.focus.child_id orelse return null; 241 return self.tab_ids.getIndex(child_id); 242 } 243 }; 244