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