╭ Logout.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 fn init() Self { 14 return .{}; 15 } 16 17 pub const View = struct { 18 center: ui.Center, 19 data: *const Self, 20 session: *ui.Session, 21 button_id: usize, 22 // focus id of the header's "users" tab; on logout we jump there so the 23 // user isn't stranded on a button that's about to be hidden. 24 users_tab_id: usize, 25 26 const prompt_index: usize = 0; 27 const button_index: usize = 1; 28 29 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session, users_tab_id: usize) !View { 30 var box = wgt.Box(ui.Widget).init(.{ .border_style = null, .rounded_corners = true, .direction = .vert }); 31 errdefer box.deinit(allocator); 32 // marks this subtree as an HTML form scope for the web overlay 33 box.getFocus().kind = .{ .custom = switch (session.data.current_page) { 34 .user, .user_settings, .user_auth => |name| try std.fmt.allocPrint(session.page_arena.allocator(), "form:/user/{s}/logout", .{name.slice()}), 35 else => "form:/logout", 36 } }; 37 38 { 39 var prompt = wgt.Text(ui.Widget).init("are you sure?"); 40 errdefer prompt.deinit(allocator); 41 try box.children.put(allocator, prompt.getFocus().id, .{ 42 .widget = .{ .text = prompt }, 43 .rect = null, 44 .min_size = null, 45 }); 46 } 47 48 var button_id: usize = undefined; 49 { 50 var button = try wgt.TextBox(ui.Widget).init(allocator, "logout", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 51 errdefer button.deinit(allocator); 52 button.getFocus().focusable = true; 53 // the renderer distinguishes plain clickables from buttons that 54 // should POST to a server route by this kind. 55 button.getFocus().kind = .{ .custom = "submit" }; 56 button_id = button.getFocus().id; 57 try box.children.put(allocator, button.getFocus().id, .{ 58 .widget = .{ .text_box = button }, 59 .rect = null, 60 .min_size = null, 61 }); 62 } 63 64 box.getFocus().child_id = button_id; 65 66 return .{ 67 .center = try ui.Center.init(allocator, .{ .box = box }, .both), 68 .data = data, 69 .session = session, 70 .button_id = button_id, 71 .users_tab_id = users_tab_id, 72 }; 73 } 74 75 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 76 self.center.deinit(allocator); 77 } 78 79 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 80 try self.center.build(allocator, constraint, root_focus); 81 } 82 83 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 84 _ = allocator; 85 switch (key) { 86 .enter => { 87 try self.logout(root_focus); 88 return; 89 }, 90 .mouse => |mouse| { 91 if (mouse.action == .press and mouse.action.press == .left) { 92 if (root_focus.children.get(self.button_id)) |entry| { 93 const r = entry.rect; 94 if (mouse.x >= r.x and mouse.y >= r.y and 95 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 96 { 97 try self.logout(root_focus); 98 return; 99 } 100 } 101 } 102 }, 103 else => {}, 104 } 105 } 106 107 fn logout(self: *View, root_focus: *Focus) !void { 108 self.session.data.user_id = null; 109 // jump focus back to the users tab — the logout button is about to 110 // be hidden by the tab-label swap. 111 try root_focus.setFocus(self.users_tab_id); 112 } 113 114 pub fn clearGrid(self: *View) void { 115 self.center.clearGrid(); 116 } 117 118 pub fn getGrid(self: View) ?Grid { 119 return self.center.getGrid(); 120 } 121 122 pub fn getFocus(self: *View) *Focus { 123 return self.center.getFocus(); 124 } 125 126 pub fn getSelectedIndex(self: View) ?usize { 127 _ = self; 128 return 0; 129 } 130 }; 131