╭ 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 title: ui.Title, 15 auth_tab: AuthTab, 16 17 const Self = @This(); 18 19 pub fn init(arena: *std.heap.ArenaAllocator) !Self { 20 return .{ 21 .title = try ui.Title.init(arena, "haxy"), 22 .auth_tab = AuthTab.init(), 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 // shrink the title when there is not enough space 42 try box.children.put(allocator, title_view.getFocus().id, .{ 43 .widget = .{ .title = title_view }, 44 .rect = null, 45 .min_size = null, 46 .flex = .shrink, 47 }); 48 } 49 50 // spacer 51 { 52 var text = try wgt.Text(ui.Widget).init(allocator, " "); 53 errdefer text.deinit(allocator); 54 try box.children.put(allocator, text.getFocus().id, .{ 55 .widget = .{ .text = text }, 56 .rect = null, 57 .min_size = .{ .width = 1, .height = null }, 58 }); 59 } 60 61 // the tab matching the current page is focused initially; matching by 62 // link (rather than position) keeps this robust to tab changes. 63 const current_tag = std.meta.activeTag(session.data.current_page); 64 const repos_link = try ui.inPageTabLink(session, .{ .home_repos = 0 }, current_tag == .home_repos); 65 const users_link = try ui.inPageTabLink(session, .{ .home_users = 0 }, current_tag == .home_users); 66 const settings_link = try ui.inPageTabLink(session, .home_settings, current_tag == .home_settings); 67 const auth_link = try ui.inPageTabLink(session, .home_auth, current_tag == .home_auth); 68 const current_link: []const u8 = switch (current_tag) { 69 .home_users => users_link, 70 .home_settings => settings_link, 71 .home_auth => auth_link, 72 else => repos_link, 73 }; 74 var selected_tab: ?usize = null; 75 76 // repos tab 77 { 78 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "repos", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 79 errdefer text_box.deinit(allocator); 80 text_box.getFocus().focusable = true; 81 text_box.getFocus().kind = .{ .custom = repos_link }; 82 try tab_ids.put(allocator, text_box.getFocus().id, {}); 83 if (std.mem.eql(u8, repos_link, current_link)) selected_tab = text_box.getFocus().id; 84 try box.children.put(allocator, text_box.getFocus().id, .{ 85 .widget = .{ .text_box = text_box }, 86 .rect = null, 87 .min_size = .{ .width = "repos".len + 2, .height = null }, 88 }); 89 } 90 91 // users tab 92 { 93 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "users", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 94 errdefer text_box.deinit(allocator); 95 text_box.getFocus().focusable = true; 96 text_box.getFocus().kind = .{ .custom = users_link }; 97 try tab_ids.put(allocator, text_box.getFocus().id, {}); 98 if (std.mem.eql(u8, users_link, current_link)) selected_tab = text_box.getFocus().id; 99 try box.children.put(allocator, text_box.getFocus().id, .{ 100 .widget = .{ .text_box = text_box }, 101 .rect = null, 102 .min_size = .{ .width = "users".len + 2, .height = null }, 103 }); 104 } 105 106 // spacer pushes settings + auth to the right 107 { 108 var spacer = try ui.widget.Spacer.init(allocator); 109 errdefer spacer.deinit(allocator); 110 try box.children.put(allocator, spacer.getFocus().id, .{ 111 .widget = .{ .spacer = spacer }, 112 .rect = null, 113 .min_size = null, 114 .flex = .grow, 115 }); 116 } 117 118 // settings tab. settings are account preferences, so it needs a login. 119 if (session.data.user_id != null) { 120 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "settings", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 121 errdefer text_box.deinit(allocator); 122 text_box.getFocus().focusable = true; 123 text_box.getFocus().kind = .{ .custom = settings_link }; 124 try tab_ids.put(allocator, text_box.getFocus().id, {}); 125 if (std.mem.eql(u8, settings_link, current_link)) selected_tab = text_box.getFocus().id; 126 try box.children.put(allocator, text_box.getFocus().id, .{ 127 .widget = .{ .text_box = text_box }, 128 .rect = null, 129 .min_size = .{ .width = "settings".len + 2, .height = null }, 130 }); 131 } 132 133 // auth tab (login / logout) 134 { 135 var auth_tab = try AuthTab.View.init(allocator, session); 136 errdefer auth_tab.deinit(allocator); 137 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 138 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 139 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 140 try box.children.put(allocator, auth_tab.getFocus().id, .{ 141 .widget = .{ .auth_tab = auth_tab }, 142 .rect = null, 143 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 144 }); 145 } 146 147 // quit tab 148 if (session.is_terminal) { 149 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 150 errdefer text_box.deinit(allocator); 151 text_box.getFocus().focusable = true; 152 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 153 try tab_ids.put(allocator, text_box.getFocus().id, {}); 154 try box.children.put(allocator, text_box.getFocus().id, .{ 155 .widget = .{ .text_box = text_box }, 156 .rect = null, 157 // the label is a single column; +2 for the border 158 .min_size = .{ .width = 1 + 2, .height = null }, 159 }); 160 } 161 162 var self = View{ 163 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 164 .tab_ids = tab_ids, 165 }; 166 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 167 return self; 168 } 169 170 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 171 self.scroll.deinit(allocator); 172 self.tab_ids.deinit(allocator); 173 } 174 175 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 176 self.clearGrid(); 177 const box = &self.scroll.child.box; 178 for (box.children.keys(), box.children.values()) |id, *child| { 179 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 180 .text_box => |*x| x, 181 .auth_tab => |*at| blk: { 182 // the label tracks login state per frame, so the width must too 183 child.min_size = .{ .width = at.minWidth(), .height = null }; 184 break :blk &at.text_box; 185 }, 186 else => null, 187 }; 188 if (tb) |t| { 189 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 190 } 191 } 192 var scroll_constraint = constraint; 193 scroll_constraint.min_size.width = constraint.max_size.width orelse constraint.min_size.width; 194 try self.scroll.build(allocator, scroll_constraint, root_focus); 195 } 196 197 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 198 _ = allocator; 199 const current_tab = self.currentTabIndex() orelse return; 200 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 201 const tab_id = self.tab_ids.keys()[new_tab]; 202 root_focus.setFocus(tab_id); 203 const tab = self.scroll.child.box.children.get(tab_id) orelse return; 204 if (tab.rect) |rect| self.scroll.scrollToRect(rect); 205 } 206 } 207 208 pub fn clearGrid(self: *View) void { 209 self.scroll.clearGrid(); 210 } 211 212 pub fn getGrid(self: View) ?Grid { 213 return self.scroll.getGrid(); 214 } 215 216 pub fn getFocus(self: *View) *Focus { 217 return self.scroll.getFocus(); 218 } 219 220 pub fn getSelectedIndex(self: View) ?usize { 221 return self.currentTabIndex(); 222 } 223 224 fn currentTabIndex(self: View) ?usize { 225 const child_id = self.scroll.child.box.focus.child_id orelse return null; 226 return self.tab_ids.getIndex(child_id); 227 } 228 }; 229