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