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