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