╭ 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 auth_tab: AuthTab, 14 15 const Self = @This(); 16 17 pub fn init() Self { 18 return .{ .auth_tab = AuthTab.init() }; 19 } 20 21 pub const View = struct { 22 box: wgt.Box(ui.Widget), 23 data: *const Self, 24 tab_ids: [3]usize, 25 26 const auth_tab_index: usize = 2; 27 // the auth slot must fit either "login" or "logout" 28 const auth_tab_min_width: usize = @as(usize, "logout".len) + 2; 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 = null, .rounded_corners = true, .direction = .horiz }); 32 errdefer box.deinit(allocator); 33 34 var tab_ids: [3]usize = undefined; 35 36 // fixed tabs: users, repos 37 const fixed_tab_names = [_][]const u8{ "users", "repos" }; 38 for (fixed_tab_names, 0..) |name, i| { 39 var text_box = try wgt.TextBox(ui.Widget).init(allocator, name, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 40 errdefer text_box.deinit(allocator); 41 text_box.getFocus().focusable = true; 42 tab_ids[i] = text_box.getFocus().id; 43 try box.children.put(allocator, text_box.getFocus().id, .{ 44 .widget = .{ .text_box = text_box }, 45 .rect = null, 46 .min_size = .{ .width = name.len + 2, .height = null }, 47 }); 48 } 49 50 // spacer pushes the auth tab to the right 51 { 52 var spacer = ui.Spacer.init(allocator); 53 errdefer spacer.deinit(allocator); 54 try box.children.put(allocator, spacer.getFocus().id, .{ 55 .widget = .{ .spacer = spacer }, 56 .rect = null, 57 .min_size = .{ .width = 1, .height = null }, 58 }); 59 } 60 61 // auth tab — knows how to swap its own label based on session 62 { 63 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 64 errdefer auth_tab.deinit(allocator); 65 tab_ids[auth_tab_index] = auth_tab.getFocus().id; 66 try box.children.put(allocator, auth_tab.getFocus().id, .{ 67 .widget = .{ .home_auth_tab = auth_tab }, 68 .rect = null, 69 .min_size = .{ .width = auth_tab_min_width, .height = null }, 70 }); 71 } 72 73 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 74 // when a previous login attempt just failed, land on the auth tab so 75 // the user sees the form (with its "(invalid)" label) instead of the 76 // default users list — otherwise the post-redirect page reads as if 77 // the login silently succeeded. 78 self.getFocus().child_id = if (session.data.login_failure != null) 79 tab_ids[auth_tab_index] 80 else 81 tab_ids[0]; 82 return self; 83 } 84 85 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 86 self.box.deinit(allocator); 87 } 88 89 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 90 self.clearGrid(); 91 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 92 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 93 .text_box => |*x| x, 94 .home_auth_tab => |*at| &at.text_box, 95 else => null, 96 }; 97 if (tb) |t| { 98 t.options.border_style = if (self.getFocus().child_id == id) 99 (if (root_focus.grandchild_id == id) .double else .single) 100 else 101 .hidden; 102 } 103 } 104 try self.box.build(allocator, constraint, root_focus); 105 } 106 107 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 108 _ = allocator; 109 const current_tab = self.currentTabIndex() orelse return; 110 var new_tab = current_tab; 111 switch (key) { 112 .arrow_left => new_tab -|= 1, 113 .arrow_right => if (new_tab + 1 < self.tab_ids.len) { 114 new_tab += 1; 115 }, 116 else => {}, 117 } 118 if (new_tab != current_tab) { 119 try root_focus.setFocus(self.tab_ids[new_tab]); 120 } 121 } 122 123 pub fn clearGrid(self: *View) void { 124 self.box.clearGrid(); 125 } 126 127 pub fn getGrid(self: View) ?Grid { 128 return self.box.getGrid(); 129 } 130 131 pub fn getFocus(self: *View) *Focus { 132 return self.box.getFocus(); 133 } 134 135 pub fn getSelectedIndex(self: View) ?usize { 136 return self.currentTabIndex(); 137 } 138 139 fn currentTabIndex(self: View) ?usize { 140 const child_id = self.box.focus.child_id orelse return null; 141 for (self.tab_ids, 0..) |id, i| { 142 if (id == child_id) return i; 143 } 144 return null; 145 } 146 }; 147