╭ 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 title: ui.Title, 14 auth_tab: AuthTab, 15 16 const Self = @This(); 17 18 pub fn init() Self { 19 return .{ 20 .title = ui.Title.init("haxy"), 21 .auth_tab = AuthTab.init(), 22 }; 23 } 24 25 pub const View = struct { 26 box: wgt.Box(ui.Widget), 27 data: *const Self, 28 tab_ids: [3]usize, 29 30 const auth_tab_index: usize = 2; 31 // the auth slot must fit either "login" or "logout" 32 const auth_tab_min_width: usize = @as(usize, "logout".len) + 2; 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 = null, .rounded_corners = true, .direction = .horiz }); 36 errdefer box.deinit(allocator); 37 38 var tab_ids: [3]usize = undefined; 39 40 // title sits to the left of the tabs 41 { 42 var title_view = try ui.Title.View.init(allocator, &data.title); 43 errdefer title_view.deinit(allocator); 44 try box.children.put(allocator, title_view.getFocus().id, .{ 45 .widget = .{ .title = title_view }, 46 .rect = null, 47 .min_size = .{ .width = data.title.width, .height = null }, 48 }); 49 } 50 51 // gap between title and the users tab 52 { 53 var text = wgt.Text(ui.Widget).init(" "); 54 errdefer text.deinit(allocator); 55 try box.children.put(allocator, text.getFocus().id, .{ 56 .widget = .{ .text = text }, 57 .rect = null, 58 .min_size = .{ .width = 1, .height = null }, 59 }); 60 } 61 62 // fixed tabs: users, repos 63 for ( 64 [_][]const u8{ "users", "repos" }, 65 [_][]const u8{ "a:/users", "a:/repos" }, 66 0.., 67 ) |name, focus_name, i| { 68 var text_box = try wgt.TextBox(ui.Widget).init(allocator, name, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 69 errdefer text_box.deinit(allocator); 70 text_box.getFocus().focusable = true; 71 text_box.getFocus().kind = .{ .custom = focus_name }; 72 tab_ids[i] = text_box.getFocus().id; 73 try box.children.put(allocator, text_box.getFocus().id, .{ 74 .widget = .{ .text_box = text_box }, 75 .rect = null, 76 .min_size = .{ .width = name.len + 2, .height = null }, 77 }); 78 } 79 80 // spacer pushes the auth tab to the right 81 { 82 var spacer = ui.Spacer.init(); 83 errdefer spacer.deinit(allocator); 84 try box.children.put(allocator, spacer.getFocus().id, .{ 85 .widget = .{ .spacer = spacer }, 86 .rect = null, 87 .min_size = .{ .width = 1, .height = null }, 88 }); 89 } 90 91 // auth tab — knows how to swap its own label based on session 92 { 93 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 94 errdefer auth_tab.deinit(allocator); 95 tab_ids[auth_tab_index] = auth_tab.getFocus().id; 96 try box.children.put(allocator, auth_tab.getFocus().id, .{ 97 .widget = .{ .home_auth_tab = auth_tab }, 98 .rect = null, 99 .min_size = .{ .width = auth_tab_min_width, .height = null }, 100 }); 101 } 102 103 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 104 // initial selected tab follows session.data.current_page — the web 105 // layer decides this from the request URL, the TTY uses whatever 106 // the session arrives with — so we don't bake a default here. 107 self.getFocus().child_id = tab_ids[ 108 switch (session.data.current_page) { 109 .home_users => 0, 110 .home_repos => 1, 111 .home_auth => 2, 112 } 113 ]; 114 return self; 115 } 116 117 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 118 self.box.deinit(allocator); 119 } 120 121 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 122 self.clearGrid(); 123 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 124 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 125 .text_box => |*x| x, 126 .home_auth_tab => |*at| &at.text_box, 127 else => null, 128 }; 129 if (tb) |t| { 130 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 131 } 132 } 133 try self.box.build(allocator, constraint, root_focus); 134 } 135 136 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 137 _ = allocator; 138 const current_tab = self.currentTabIndex() orelse return; 139 var new_tab = current_tab; 140 switch (key) { 141 .arrow_left => new_tab -|= 1, 142 .arrow_right => if (new_tab + 1 < self.tab_ids.len) { 143 new_tab += 1; 144 }, 145 else => {}, 146 } 147 if (new_tab != current_tab) { 148 try root_focus.setFocus(self.tab_ids[new_tab]); 149 } 150 } 151 152 pub fn clearGrid(self: *View) void { 153 self.box.clearGrid(); 154 } 155 156 pub fn getGrid(self: View) ?Grid { 157 return self.box.getGrid(); 158 } 159 160 pub fn getFocus(self: *View) *Focus { 161 return self.box.getFocus(); 162 } 163 164 pub fn getSelectedIndex(self: View) ?usize { 165 return self.currentTabIndex(); 166 } 167 168 fn currentTabIndex(self: View) ?usize { 169 const child_id = self.box.focus.child_id orelse return null; 170 for (self.tab_ids, 0..) |id, i| { 171 if (id == child_id) return i; 172 } 173 return null; 174 } 175 }; 176