╭ Logout.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.widget.Center, 20 data: *const Self, 21 session: *ui.Session, 22 logout_id: usize, 23 24 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 25 // the form box makes the web renderer post the button to /logout 26 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 27 errdefer box.deinit(allocator); 28 box.getFocus().kind = .{ .custom = "form:logout" }; 29 30 var button = try wgt.TextBox.init(allocator, "logout", .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 31 errdefer button.deinit(allocator); 32 button.getFocus().mode = .all; 33 // the renderer distinguishes plain clickables from buttons that 34 // should POST to a server route by this kind. 35 button.getFocus().kind = .{ .custom = "submit" }; 36 const logout_id = button.getFocus().id; 37 try box.children.put(allocator, logout_id, .{ 38 .widget = .{ .text_box = button }, 39 .rect = null, 40 .min_size = null, 41 }); 42 box.getFocus().child_id = logout_id; 43 44 return .{ 45 .center = try ui.widget.Center.init(allocator, .{ .box = box }), 46 .data = data, 47 .session = session, 48 .logout_id = logout_id, 49 }; 50 } 51 52 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 53 self.center.deinit(allocator); 54 } 55 56 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 57 try self.center.build(allocator, constraint, root_focus); 58 } 59 60 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 61 _ = allocator; 62 switch (key) { 63 .enter => try self.logOut(), 64 .mouse => |mouse| if (inp.leftClickOn(root_focus, self.logout_id, mouse)) try self.logOut(), 65 else => {}, 66 } 67 } 68 69 fn logOut(self: *View) !void { 70 self.session.logOut(); 71 // leave the auth tab for the page it belongs to, where the web's 72 // /logout redirect also lands 73 try self.session.navigate(self.session.data.current_page.pageRoot()); 74 } 75 76 pub fn clearGrid(self: *View) void { 77 self.center.clearGrid(); 78 } 79 80 pub fn getGrid(self: View) ?Grid { 81 return self.center.getGrid(); 82 } 83 84 pub fn getFocus(self: *View) *Focus { 85 return self.center.getFocus(); 86 } 87 88 pub fn atTop(self: View) bool { 89 _ = self; 90 return true; 91 } 92 }; 93