╭ 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(arena: *std.heap.ArenaAllocator) !Self { 19 return .{ 20 .title = try ui.Title.init(arena, "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: [4]usize, 29 30 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 31 var box = wgt.Box(ui.Widget).init(.{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 32 errdefer box.deinit(allocator); 33 34 var tab_ids: [4]usize = undefined; 35 36 // title sits to the left of the tabs 37 { 38 var title_view = try ui.Title.View.init(allocator, &data.title); 39 errdefer title_view.deinit(allocator); 40 try box.children.put(allocator, title_view.getFocus().id, .{ 41 .widget = .{ .title = title_view }, 42 .rect = null, 43 .min_size = .{ .width = data.title.width, .height = null }, 44 }); 45 } 46 47 // spacer 48 { 49 var text = wgt.Text(ui.Widget).init(" "); 50 errdefer text.deinit(allocator); 51 try box.children.put(allocator, text.getFocus().id, .{ 52 .widget = .{ .text = text }, 53 .rect = null, 54 .min_size = .{ .width = 1, .height = null }, 55 }); 56 } 57 58 // tabs 59 var tab_index: usize = 0; 60 for ( 61 [_][]const u8{ "users", "repos", "", "settings", "" }, 62 [_][]const u8{ "a:/users", "a:/repos", "", "a:/settings", "a:/auth" }, 63 ) |name, focus_name| { 64 // spacer 65 if (focus_name.len == 0) { 66 var spacer = ui.Spacer.init(); 67 errdefer spacer.deinit(allocator); 68 try box.children.put(allocator, spacer.getFocus().id, .{ 69 .widget = .{ .spacer = spacer }, 70 .rect = null, 71 .min_size = .{ .width = 1, .height = null }, 72 }); 73 } 74 // auth 75 else if (std.mem.eql(u8, "a:/auth", focus_name)) { 76 var auth_tab = try AuthTab.View.init(allocator, &data.auth_tab, session); 77 errdefer auth_tab.deinit(allocator); 78 tab_ids[tab_index] = auth_tab.getFocus().id; 79 tab_index += 1; 80 try box.children.put(allocator, auth_tab.getFocus().id, .{ 81 .widget = .{ .auth_tab = auth_tab }, 82 .rect = null, 83 .min_size = .{ .width = AuthTab.min_width, .height = null }, 84 }); 85 } 86 // other tabs 87 else { 88 var text_box = try wgt.TextBox(ui.Widget).init(allocator, name, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 89 errdefer text_box.deinit(allocator); 90 text_box.getFocus().focusable = true; 91 text_box.getFocus().kind = .{ .custom = focus_name }; 92 tab_ids[tab_index] = text_box.getFocus().id; 93 tab_index += 1; 94 try box.children.put(allocator, text_box.getFocus().id, .{ 95 .widget = .{ .text_box = text_box }, 96 .rect = null, 97 .min_size = .{ .width = name.len + 2, .height = null }, 98 }); 99 } 100 } 101 102 var self = View{ .box = box, .data = data, .tab_ids = tab_ids }; 103 // initial selected tab follows session.data.current_page — the web 104 // layer decides this from the request URL, the TTY uses whatever 105 // the session arrives with — so we don't bake a default here. 106 self.getFocus().child_id = tab_ids[ 107 switch (session.data.current_page) { 108 .home_users => 0, 109 .home_repos => 1, 110 .settings => 2, 111 .auth => 3, 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 .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