╭ 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 104 self.getFocus().child_id = tab_ids[ 105 switch (session.data.current_page) { 106 .home_users => 0, 107 .home_repos => 1, 108 .home_settings => 2, 109 .home_auth => 3, 110 else => 0, 111 } 112 ]; 113 return self; 114 } 115 116 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 117 self.box.deinit(allocator); 118 } 119 120 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 121 self.clearGrid(); 122 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 123 const tb: ?*wgt.TextBox(ui.Widget) = switch (child.widget) { 124 .text_box => |*x| x, 125 .auth_tab => |*at| &at.text_box, 126 else => null, 127 }; 128 if (tb) |t| { 129 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 130 } 131 } 132 try self.box.build(allocator, constraint, root_focus); 133 } 134 135 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 136 _ = allocator; 137 const current_tab = self.currentTabIndex() orelse return; 138 var new_tab = current_tab; 139 switch (key) { 140 .arrow_left => new_tab -|= 1, 141 .arrow_right => if (new_tab + 1 < self.tab_ids.len) { 142 new_tab += 1; 143 }, 144 else => {}, 145 } 146 if (new_tab != current_tab) { 147 try root_focus.setFocus(self.tab_ids[new_tab]); 148 } 149 } 150 151 pub fn clearGrid(self: *View) void { 152 self.box.clearGrid(); 153 } 154 155 pub fn getGrid(self: View) ?Grid { 156 return self.box.getGrid(); 157 } 158 159 pub fn getFocus(self: *View) *Focus { 160 return self.box.getFocus(); 161 } 162 163 pub fn getSelectedIndex(self: View) ?usize { 164 return self.currentTabIndex(); 165 } 166 167 fn currentTabIndex(self: View) ?usize { 168 const child_id = self.box.focus.child_id orelse return null; 169 for (self.tab_ids, 0..) |id, i| { 170 if (id == child_id) return i; 171 } 172 return null; 173 } 174 }; 175