╭ Settings.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("../ui.zig"); 3 const inp = @import("./input.zig"); 4 const xit = @import("xit"); 5 const xitui = xit.xitui; 6 const wgt = xitui.widget; 7 const layout = xitui.layout; 8 const Key = xitui.input.Key; 9 const Grid = xitui.grid.Grid; 10 const Focus = xitui.focus.Focus; 11 12 const Self = @This(); 13 14 pub fn init() Self { 15 return .{}; 16 } 17 18 pub const View = struct { 19 center: ui.Center, 20 session: *ui.Session, 21 button_id: usize, 22 23 // the stack switches between two whole views; build() selects one based on 24 // whether a user is logged in. the logged-in view holds the settings 25 // controls (currently just the ANSI toggle, but it will grow). 26 const logged_in_index: usize = 0; 27 const logged_out_index: usize = 1; 28 29 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !View { 30 var stack = try wgt.Stack(ui.Widget).init(allocator); 31 errdefer stack.deinit(allocator); 32 33 var button_id: usize = undefined; 34 // the logged-in view: the controls shown to a signed-in user. 35 { 36 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 37 errdefer box.deinit(allocator); 38 39 // on the web each control posts to a page-scoped form 40 box.getFocus().kind = .{ .custom = "form:ansi" }; 41 42 { 43 var button = try wgt.TextBox(ui.Widget).init(allocator, ansiLabel(session), .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 44 errdefer button.deinit(allocator); 45 // the renderer distinguishes plain clickables from buttons that 46 // should POST to a server route by this kind. 47 button.getFocus().kind = .{ .custom = "submit" }; 48 button.getFocus().focusable = true; 49 button_id = button.getFocus().id; 50 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 51 } 52 53 box.getFocus().child_id = button_id; 54 try stack.children.put(allocator, box.getFocus().id, .{ .box = box }); 55 } 56 57 // the logged-out view: shown when no user is signed in. 58 { 59 const logged_out_message = 60 \\you must be logged in to see settings. 61 \\ 62 \\here's an emoticon from the early 2000s instead: 63 \\ 64 \\<('o'<) ^( '-' )^ (>'o')> 65 \\ 66 \\hope it helps. 67 ; 68 69 var message = try wgt.TextBox(ui.Widget).init(allocator, logged_out_message, .{ .border_style = null, .rounded_corners = false, .wrap_kind = .word }); 70 errdefer message.deinit(allocator); 71 try stack.children.put(allocator, message.getFocus().id, .{ .text_box = message }); 72 } 73 74 return .{ 75 .center = try ui.Center.init(allocator, .{ .stack = stack }), 76 .session = session, 77 .button_id = button_id, 78 }; 79 } 80 81 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 82 self.center.deinit(allocator); 83 } 84 85 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 86 // select the logged-in view when logged in, else the logged-out view. 87 // decided per-frame since the tty doesn't rebuild the page on login. 88 const logged_in = self.session.data.user_id != null; 89 const stack = &self.center.child.stack; 90 if (logged_in) { 91 const box = &stack.children.values()[logged_in_index].box; 92 const button = &box.children.values()[0].widget.text_box; 93 button.box.children.values()[0].widget.text.content = ansiLabel(self.session); 94 } 95 stack.getFocus().child_id = stack.children.keys()[if (logged_in) logged_in_index else logged_out_index]; 96 try self.center.build(allocator, constraint, root_focus); 97 } 98 99 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 100 _ = allocator; 101 if (self.session.data.user_id == null) return; // blank/disabled when logged out 102 switch (key) { 103 .enter => try self.toggle(), 104 .mouse => |mouse| { 105 if (inp.leftClickOn(root_focus, self.button_id, mouse)) { 106 try self.toggle(); 107 } 108 }, 109 else => {}, 110 } 111 } 112 113 fn toggle(self: *View) !void { 114 try self.session.push(.toggle_ansi); 115 } 116 117 pub fn clearGrid(self: *View) void { 118 self.center.clearGrid(); 119 } 120 121 pub fn getGrid(self: View) ?Grid { 122 return self.center.getGrid(); 123 } 124 125 pub fn getFocus(self: *View) *Focus { 126 return self.center.getFocus(); 127 } 128 129 pub fn getSelectedIndex(self: View) ?usize { 130 _ = self; 131 return 0; 132 } 133 }; 134 135 fn ansiLabel(session: *const ui.Session) []const u8 { 136 const label_on = "turn off ANSI art"; 137 const label_off = "turn on ANSI art"; 138 return if (session.data.enable_ansi) label_on else label_off; 139 } 140