╭ 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 16 pub const AuthTab = @import("./../AuthTab.zig"); 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_style = .hidden, .round_corners = true, .direction = .horiz }); 39 errdefer box.deinit(allocator); 40 41 var title_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = 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_style = 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 settings_link = try ui.inPageTabLink(session, .home_settings, current_tag == .home_settings); 95 const auth_link = try ui.inPageTabLink(session, .home_auth, current_tag == .home_auth); 96 const current_link: []const u8 = switch (current_tag) { 97 .home_users => users_link, 98 .home_settings => settings_link, 99 .home_auth => auth_link, 100 else => about_link, 101 }; 102 var selected_tab: ?usize = null; 103 104 // about tab 105 { 106 var text_box = try wgt.TextBox.init(allocator, about_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 107 errdefer text_box.deinit(allocator); 108 text_box.getFocus().mode = .all; 109 text_box.getFocus().kind = .{ .custom = about_link }; 110 try tab_ids.put(allocator, text_box.getFocus().id, {}); 111 if (std.mem.eql(u8, about_link, current_link)) selected_tab = text_box.getFocus().id; 112 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 113 .widget = .{ .text_box = text_box }, 114 .rect = null, 115 .min_size = .{ .width = about_tab_label.len + 2, .height = null }, 116 }); 117 } 118 119 // users tab 120 { 121 var text_box = try wgt.TextBox.init(allocator, users_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 122 errdefer text_box.deinit(allocator); 123 text_box.getFocus().mode = .all; 124 text_box.getFocus().kind = .{ .custom = users_link }; 125 try tab_ids.put(allocator, text_box.getFocus().id, {}); 126 if (std.mem.eql(u8, users_link, current_link)) selected_tab = text_box.getFocus().id; 127 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 128 .widget = .{ .text_box = text_box }, 129 .rect = null, 130 .min_size = .{ .width = users_tab_label.len + 2, .height = null }, 131 }); 132 } 133 134 // spacer pushes settings + auth to the right 135 { 136 var spacer = try ui.widget.Spacer.init(allocator); 137 errdefer spacer.deinit(allocator); 138 try tabs_box.children.put(allocator, spacer.getFocus().id, .{ 139 .widget = .{ .spacer = spacer }, 140 .rect = null, 141 .min_size = null, 142 .flex = .grow, 143 }); 144 } 145 146 // settings tab. settings are account preferences, so it needs a login. 147 if (session.data.user_id != null) { 148 var text_box = try wgt.TextBox.init(allocator, settings_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 149 errdefer text_box.deinit(allocator); 150 text_box.getFocus().mode = .all; 151 text_box.getFocus().kind = .{ .custom = settings_link }; 152 try tab_ids.put(allocator, text_box.getFocus().id, {}); 153 if (std.mem.eql(u8, settings_link, current_link)) selected_tab = text_box.getFocus().id; 154 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 155 .widget = .{ .text_box = text_box }, 156 .rect = null, 157 .min_size = .{ .width = settings_tab_label.len + 2, .height = null }, 158 }); 159 } 160 161 // auth tab (login / logout) 162 { 163 var auth_tab = try AuthTab.View.init(allocator, session); 164 errdefer auth_tab.deinit(allocator); 165 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 166 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 167 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 168 try tabs_box.children.put(allocator, auth_tab.getFocus().id, .{ 169 .widget = .{ .auth_tab = auth_tab }, 170 .rect = null, 171 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 172 }); 173 } 174 175 // quit tab 176 if (session.is_terminal) { 177 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 178 errdefer text_box.deinit(allocator); 179 text_box.getFocus().mode = .all; 180 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 181 try tab_ids.put(allocator, text_box.getFocus().id, {}); 182 try tabs_box.children.put(allocator, text_box.getFocus().id, .{ 183 .widget = .{ .text_box = text_box }, 184 .rect = null, 185 // the label is a single column; +2 for the border 186 .min_size = .{ .width = 1 + 2, .height = null }, 187 }); 188 } 189 190 tabs_box.getFocus().child_id = selected_tab orelse tab_ids.keys()[0]; 191 const title_id = title_box.getFocus().id; 192 const tabs_id = tabs_box.getFocus().id; 193 try box.children.put(allocator, title_id, .{ .widget = .{ .box = title_box }, .rect = null, .min_size = null }); 194 title_box_owned = true; 195 // a blank row between the title and the tabs, shown only when they wrap 196 const gap_id = blk: { 197 var gap = try wgt.Text.init(allocator, " "); 198 errdefer gap.deinit(allocator); 199 const id = gap.getFocus().id; 200 try box.children.put(allocator, id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null, .hidden = true }); 201 break :blk id; 202 }; 203 try box.children.put(allocator, tabs_id, .{ .widget = .{ .box = tabs_box }, .rect = null, .min_size = null }); 204 tabs_box_owned = true; 205 box.getFocus().child_id = tabs_id; 206 207 return .{ 208 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 209 .tab_ids = tab_ids, 210 .tabs_id = tabs_id, 211 .gap_id = gap_id, 212 .first_group_width = first_group_width, 213 .session = session, 214 }; 215 } 216 217 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 218 self.scroll.deinit(allocator); 219 self.tab_ids.deinit(allocator); 220 } 221 222 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 223 self.clearGrid(); 224 const box = &self.scroll.child.box; 225 const back_visible = self.session.back == .available; 226 ui.widget.setBackButtonVisible(box, back_visible); 227 const tabs_child = self.tabsChild(); 228 const tabs_box = &tabs_child.widget.box; 229 230 // only the selected tab shows its border 231 const selected_tab = if (box.getFocus().child_id == self.tabs_id) tabs_box.getFocus().child_id else null; 232 var tabs_width: usize = 0; 233 for (tabs_box.children.keys(), tabs_box.children.values()) |id, *child| { 234 const tb: ?*wgt.TextBox = switch (child.widget) { 235 .text_box => |*x| x, 236 .auth_tab => |*at| blk: { 237 // the label tracks login state per frame, so the width must too 238 child.min_size = .{ .width = at.minWidth(), .height = null }; 239 break :blk &at.text_box; 240 }, 241 else => null, 242 }; 243 if (tb) |t| ui.widget.markSelected(t, selected_tab == id); 244 tabs_width += if (child.min_size) |min_size| min_size.width orelse 0 else 0; 245 } 246 247 // the outer box's hidden border occupies two columns. keep the title 248 // and tabs together if they fit; otherwise tab strip on its own row. 249 const viewport_width = constraint.max_size.width orelse constraint.min_size.width; 250 const content_width = if (viewport_width) |width| width -| 2 else null; 251 const back_width: usize = if (back_visible) ui.widget.back_button_width else 0; 252 const wrap = if (content_width) |width| self.first_group_width + back_width + tabs_width > width else false; 253 box.options.direction = if (wrap) .vert else .horiz; 254 tabs_child.min_size = if (wrap) .{ .width = content_width, .height = null } else null; 255 (box.children.getPtr(self.gap_id) orelse unreachable).hidden = !wrap; 256 257 var scroll_constraint = constraint; 258 scroll_constraint.min_size.width = viewport_width; 259 try self.scroll.build(allocator, scroll_constraint, root_focus); 260 } 261 262 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 263 _ = allocator; 264 const current_tab = self.currentTabIndex() orelse return; 265 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 266 const tab_id = self.tab_ids.keys()[new_tab]; 267 root_focus.setFocus(tab_id); 268 const tabs_child = self.tabsChild(); 269 const tabs_rect = tabs_child.rect orelse return; 270 const tab = tabs_child.widget.box.children.get(tab_id) orelse return; 271 var rect = tab.rect orelse return; 272 rect.x += tabs_rect.x; 273 rect.y += tabs_rect.y; 274 self.scroll.scrollToRect(rect); 275 } 276 } 277 278 pub fn clearGrid(self: *View) void { 279 self.scroll.clearGrid(); 280 } 281 282 pub fn getGrid(self: View) ?Grid { 283 return self.scroll.getGrid(); 284 } 285 286 pub fn getFocus(self: *View) *Focus { 287 return self.scroll.getFocus(); 288 } 289 290 pub fn getSelectedIndex(self: View) ?usize { 291 return self.currentTabIndex(); 292 } 293 294 fn currentTabIndex(self: View) ?usize { 295 if (self.scroll.child.box.focus.child_id != self.tabs_id) return null; 296 const child_id = self.tabsChild().widget.box.focus.child_id orelse return null; 297 return self.tab_ids.getIndex(child_id); 298 } 299 300 fn tabsChild(self: *const View) *wgt.Box(ui.Widget).Child { 301 return self.scroll.child.box.children.getPtr(self.tabs_id) orelse unreachable; 302 } 303 }; 304