╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Settings.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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, data: *const Self, 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 = switch (session.data.current_page) { 41 .user_repos => |u| try std.fmt.allocPrint(session.page_arena.allocator(), "form:/user/{s}/ansi", .{u.name.slice()}), 42 .user_settings, .user_auth => |name| try std.fmt.allocPrint(session.page_arena.allocator(), "form:/user/{s}/ansi", .{name.slice()}), 43 else => "form:/ansi", 44 } }; 45 46 { 47 var button = try wgt.TextBox(ui.Widget).init(allocator, ansiLabel(session), .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 48 errdefer button.deinit(allocator); 49 // the renderer distinguishes plain clickables from buttons that 50 // should POST to a server route by this kind. 51 button.getFocus().kind = .{ .custom = "submit" }; 52 button.getFocus().focusable = true; 53 button_id = button.getFocus().id; 54 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 55 } 56 57 box.getFocus().child_id = button_id; 58 try stack.children.put(allocator, box.getFocus().id, .{ .box = box }); 59 } 60 61 // the logged-out view: shown when no user is signed in. 62 { 63 const logged_out_message = 64 \\you must be logged in to see settings. 65 \\ 66 \\here's an emoticon from the early 2000s instead: 67 \\ 68 \\<('o'<) ^( '-' )^ (>'o')> 69 \\ 70 \\hope it helps. 71 ; 72 73 var message = try wgt.TextBox(ui.Widget).init(allocator, logged_out_message, .{ .border_style = null, .rounded_corners = false, .wrap_kind = .word }); 74 errdefer message.deinit(allocator); 75 try stack.children.put(allocator, message.getFocus().id, .{ .text_box = message }); 76 } 77 78 return .{ 79 .center = try ui.Center.init(allocator, .{ .stack = stack }, .both), 80 .data = data, 81 .session = session, 82 .button_id = button_id, 83 }; 84 } 85 86 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 87 self.center.deinit(allocator); 88 } 89 90 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 91 // select the logged-in view when logged in, else the logged-out view. 92 // decided per-frame since the tty doesn't rebuild the page on login. 93 const logged_in = self.session.data.user_id != null; 94 const stack = &self.center.child.stack; 95 if (logged_in) { 96 const box = &stack.children.values()[logged_in_index].box; 97 const button = &box.children.values()[0].widget.text_box; 98 button.box.children.values()[0].widget.text.content = ansiLabel(self.session); 99 } 100 stack.getFocus().child_id = stack.children.keys()[if (logged_in) logged_in_index else logged_out_index]; 101 try self.center.build(allocator, constraint, root_focus); 102 } 103 104 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 105 _ = allocator; 106 if (self.session.data.user_id == null) return; // blank/disabled when logged out 107 switch (key) { 108 .enter => try self.toggle(), 109 .mouse => |mouse| { 110 if (mouse.action == .press and mouse.action.press == .left) { 111 if (root_focus.children.get(self.button_id)) |entry| { 112 const r = entry.rect; 113 if (mouse.x >= r.x and mouse.y >= r.y and 114 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 115 { 116 try self.toggle(); 117 } 118 } 119 } 120 }, 121 else => {}, 122 } 123 } 124 125 fn toggle(self: *View) !void { 126 try self.session.push(.toggle_ansi); 127 } 128 129 pub fn clearGrid(self: *View) void { 130 self.center.clearGrid(); 131 } 132 133 pub fn getGrid(self: View) ?Grid { 134 return self.center.getGrid(); 135 } 136 137 pub fn getFocus(self: *View) *Focus { 138 return self.center.getFocus(); 139 } 140 141 pub fn getSelectedIndex(self: View) ?usize { 142 _ = self; 143 return 0; 144 } 145 }; 146 147 fn ansiLabel(session: *const ui.Session) []const u8 { 148 const label_on = "turn off ANSI art"; 149 const label_off = "turn on ANSI art"; 150 return if (session.data.enable_ansi) label_on else label_off; 151 } 152