╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("../../ui.zig"); 3 const xit = @import("xit"); 4 const xitui = xit.xitui; 5 const wgt = xitui.widget; 6 const layout = xitui.layout; 7 const inp = xitui.input; 8 const Grid = xitui.grid.Grid; 9 const Focus = xitui.focus.Focus; 10 11 pub const AuthTab = @import("./../AuthTab.zig"); 12 13 name: []const u8, 14 owner_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, owner_name: []const u8) !Self { 21 return .{ 22 .name = name, 23 .owner_name = owner_name, 24 .title = try ui.Title.init(arena, name), 25 .auth_tab = AuthTab.init(), 26 }; 27 } 28 29 pub const View = struct { 30 box: wgt.Box(ui.Widget), 31 data: *const Self, 32 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 33 34 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 35 var box = wgt.Box(ui.Widget).init(.{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 36 errdefer box.deinit(allocator); 37 38 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 39 errdefer tab_ids.deinit(allocator); 40 41 const aa = session.page_arena.allocator(); 42 43 // the user's name 44 { 45 const text = try std.fmt.allocPrint(aa, "{s}/", .{data.owner_name}); 46 const link = try std.fmt.allocPrint(aa, "a:/user/{s}", .{data.owner_name}); 47 48 var text_box = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .wrap_kind = .none }); 49 errdefer text_box.deinit(allocator); 50 text_box.getFocus().focusable = true; 51 text_box.getFocus().kind = .{ .custom = link }; 52 try box.children.put(allocator, text_box.getFocus().id, .{ 53 .widget = .{ .text_box = text_box }, 54 .rect = null, 55 .min_size = null, 56 .shrink = true, 57 }); 58 } 59 60 // title sits to the left of the tabs 61 { 62 var title_view = try ui.Title.View.init(allocator, &data.title); 63 errdefer title_view.deinit(allocator); 64 title_view.getFocus().focusable = true; 65 title_view.getFocus().kind = .{ .custom = "a:/" }; 66 // shrink the title when there is not enough space 67 try box.children.put(allocator, title_view.getFocus().id, .{ 68 .widget = .{ .title = title_view }, 69 .rect = null, 70 .min_size = null, 71 .shrink = true, 72 }); 73 } 74 75 // spacer 76 { 77 var text = wgt.Text(ui.Widget).init(" "); 78 errdefer text.deinit(allocator); 79 try box.children.put(allocator, text.getFocus().id, .{ 80 .widget = .{ .text = text }, 81 .rect = null, 82 .min_size = .{ .width = 1, .height = null }, 83 }); 84 } 85 86 // every tab link is repo-scoped so selecting one stays on this page — 87 // switching its stack and updating the url — instead of navigating to 88 // the global settings or auth pages 89 const files_link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}", .{ data.owner_name, data.name }); 90 const settings_link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}/settings", .{ data.owner_name, data.name }); 91 const auth_link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}/auth", .{ data.owner_name, data.name }); 92 93 // files tab 94 { 95 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "files", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 96 errdefer text_box.deinit(allocator); 97 text_box.getFocus().focusable = true; 98 text_box.getFocus().kind = .{ .custom = files_link }; 99 try tab_ids.put(allocator, text_box.getFocus().id, {}); 100 try box.children.put(allocator, text_box.getFocus().id, .{ 101 .widget = .{ .text_box = text_box }, 102 .rect = null, 103 .min_size = .{ .width = "files".len + 2, .height = null }, 104 }); 105 } 106 107 // spacer pushes settings + auth to the right 108 { 109 var spacer = ui.Spacer.init(); 110 errdefer spacer.deinit(allocator); 111 try box.children.put(allocator, spacer.getFocus().id, .{ 112 .widget = .{ .spacer = spacer }, 113 .rect = null, 114 .min_size = null, 115 }); 116 } 117 118 // settings tab 119 { 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 try box.children.put(allocator, text_box.getFocus().id, .{ 126 .widget = .{ .text_box = text_box }, 127 .rect = null, 128 .min_size = .{ .width = "settings".len + 2, .height = null }, 129 }); 130 } 131 132 // auth tab (login / logout). AuthTab defaults to the global a:/auth link; 133 // repoint its instance at this repo's auth route so it stays local too. 134 { 135 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, 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 try box.children.put(allocator, auth_tab.getFocus().id, .{ 140 .widget = .{ .auth_tab = auth_tab }, 141 .rect = null, 142 .min_size = .{ .width = AuthTab.min_width, .height = null }, 143 }); 144 } 145 146 // quit tab 147 if (session.is_terminal) { 148 var text_box = try wgt.TextBox(ui.Widget).init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 149 errdefer text_box.deinit(allocator); 150 text_box.getFocus().focusable = true; 151 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 152 try tab_ids.put(allocator, text_box.getFocus().id, {}); 153 try box.children.put(allocator, text_box.getFocus().id, .{ 154 .widget = .{ .text_box = text_box }, 155 .rect = null, 156 // the label is a single column; +2 for the border 157 .min_size = .{ .width = 1 + 2, .height = null }, 158 }); 159 } 160 161 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 162 // open on the tab named by the current route 163 self.getFocus().child_id = self.tab_ids.keys()[ 164 switch (session.data.current_page) { 165 .repo_settings => 1, 166 .repo_auth => 2, 167 else => 0, 168 } 169 ]; 170 return self; 171 } 172 173 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 174 self.box.deinit(allocator); 175 self.tab_ids.deinit(allocator); 176 } 177 178 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 179 self.clearGrid(); 180 // only the selected tab shows its border 181 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 182 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 183 .text_box => |*x| x, 184 .auth_tab => |*at| &at.text_box, 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 try self.box.build(allocator, constraint, root_focus); 192 } 193 194 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 195 _ = allocator; 196 const current_tab = self.currentTabIndex() orelse return; 197 var new_tab = current_tab; 198 switch (key) { 199 .arrow_left => new_tab -|= 1, 200 .arrow_right => if (new_tab + 1 < self.tab_ids.count()) { 201 new_tab += 1; 202 }, 203 else => {}, 204 } 205 if (new_tab != current_tab) { 206 try root_focus.setFocus(self.tab_ids.keys()[new_tab]); 207 } 208 } 209 210 pub fn clearGrid(self: *View) void { 211 self.box.clearGrid(); 212 } 213 214 pub fn getGrid(self: View) ?Grid { 215 return self.box.getGrid(); 216 } 217 218 pub fn getFocus(self: *View) *Focus { 219 return self.box.getFocus(); 220 } 221 222 pub fn getSelectedIndex(self: View) ?usize { 223 return self.currentTabIndex(); 224 } 225 226 fn currentTabIndex(self: View) ?usize { 227 const child_id = self.box.focus.child_id orelse return null; 228 return self.tab_ids.getIndex(child_id); 229 } 230 }; 231