╭ AuthTab.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 Key = xitui.input.Key; 8 const Grid = xitui.grid.Grid; 9 const Focus = xitui.focus.Focus; 10 11 const login_tab_label = "login"; 12 const logout_tab_label = "logout"; 13 14 pub const View = struct { 15 text_box: wgt.TextBox, 16 session: *ui.Session, 17 // backs the bottom label, which borrows it 18 bottom_label_buf: [ui.clipped_bottom_label_max_len]u8, 19 20 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !View { 21 var text_box = try wgt.TextBox.init(allocator, login_tab_label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 22 errdefer text_box.deinit(allocator); 23 text_box.getFocus().mode = .all; 24 text_box.getFocus().kind = .{ .custom = "ai:/auth" }; 25 return .{ 26 .text_box = text_box, 27 .session = session, 28 .bottom_label_buf = undefined, 29 }; 30 } 31 32 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 33 self.text_box.deinit(allocator); 34 } 35 36 fn text(self: *const View) []const u8 { 37 return if (self.session.data.user_id == null) login_tab_label else logout_tab_label; 38 } 39 40 fn bottomLabel(self: *View) []const u8 { 41 if (self.session.data.user_id == null) return ""; 42 const name = self.session.data.user_name orelse return ""; 43 return ui.clippedBottomLabel(&self.bottom_label_buf, name) catch unreachable; 44 } 45 46 pub fn minWidth(self: *View) usize { 47 return @max(self.text().len, self.bottomLabel().len) + 2; 48 } 49 50 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 51 try self.text_box.setContent(allocator, self.text()); 52 self.text_box.options.bottom_label = self.bottomLabel(); 53 try self.text_box.build(allocator, constraint, root_focus); 54 } 55 56 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 57 try self.text_box.input(allocator, key, root_focus); 58 } 59 60 pub fn clearGrid(self: *View) void { 61 self.text_box.clearGrid(); 62 } 63 64 pub fn getGrid(self: View) ?Grid { 65 return self.text_box.getGrid(); 66 } 67 68 pub fn getFocus(self: *View) *Focus { 69 return self.text_box.getFocus(); 70 } 71 }; 72