╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Logout.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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