╭ 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 inp = xitui.input; 8 const Grid = xitui.grid.Grid; 9 const Focus = xitui.focus.Focus; 10 11 const Self = @This(); 12 13 pub const min_width: usize = "logout".len + 2; 14 15 pub fn init() Self { 16 return .{}; 17 } 18 19 pub const View = struct { 20 text_box: wgt.TextBox(ui.Widget), 21 session: *ui.Session, 22 23 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !View { 24 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "login", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 25 errdefer text_box.deinit(allocator); 26 text_box.getFocus().focusable = true; 27 text_box.getFocus().kind = .{ .custom = "ai:/auth" }; 28 return .{ 29 .text_box = text_box, 30 .session = session, 31 }; 32 } 33 34 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 35 self.text_box.deinit(allocator); 36 } 37 38 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 39 // update the displayed label based on session state. the inner Text 40 // widget's content is just a slice, so we can repoint it without 41 // re-allocating. 42 if (self.text_box.box.children.values().len > 0) { 43 const text_widget = &self.text_box.box.children.values()[0].widget.text; 44 text_widget.content = if (self.session.data.user_id != null) "logout" else "login"; 45 } 46 try self.text_box.build(allocator, constraint, root_focus); 47 } 48 49 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 50 try self.text_box.input(allocator, key, root_focus); 51 } 52 53 pub fn clearGrid(self: *View) void { 54 self.text_box.clearGrid(); 55 } 56 57 pub fn getGrid(self: View) ?Grid { 58 return self.text_box.getGrid(); 59 } 60 61 pub fn getFocus(self: *View) *Focus { 62 return self.text_box.getFocus(); 63 } 64 }; 65