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