╭ 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.Center, 20 data: *const Self, 21 session: *ui.Session, 22 button_id: usize, 23 // focus id of the header's "users" tab; on logout we jump there so the 24 // user isn't stranded on a button that's about to be hidden. 25 users_tab_id: usize, 26 27 const prompt_index: usize = 0; 28 const button_index: usize = 1; 29 30 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session, users_tab_id: usize) !View { 31 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 32 errdefer box.deinit(allocator); 33 // marks this subtree as an HTML form scope for the web overlay 34 box.getFocus().kind = .{ .custom = switch (session.data.current_page) { 35 .user_repos => |u| try std.fmt.allocPrint(session.page_arena.allocator(), "form:/user/{s}/logout", .{u.name.slice()}), 36 .user_settings, .user_auth => |name| try std.fmt.allocPrint(session.page_arena.allocator(), "form:/user/{s}/logout", .{name.slice()}), 37 else => "form:/logout", 38 } }; 39 40 { 41 var prompt = try wgt.Text(ui.Widget).init(allocator, "are you sure?"); 42 errdefer prompt.deinit(allocator); 43 try box.children.put(allocator, prompt.getFocus().id, .{ 44 .widget = .{ .text = prompt }, 45 .rect = null, 46 .min_size = null, 47 }); 48 } 49 50 var button_id: usize = undefined; 51 { 52 var button = try wgt.TextBox(ui.Widget).init(allocator, "logout", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 53 errdefer button.deinit(allocator); 54 button.getFocus().focusable = true; 55 // the renderer distinguishes plain clickables from buttons that 56 // should POST to a server route by this kind. 57 button.getFocus().kind = .{ .custom = "submit" }; 58 button_id = button.getFocus().id; 59 try box.children.put(allocator, button.getFocus().id, .{ 60 .widget = .{ .text_box = button }, 61 .rect = null, 62 .min_size = null, 63 }); 64 } 65 66 box.getFocus().child_id = button_id; 67 68 return .{ 69 .center = try ui.Center.init(allocator, .{ .box = box }), 70 .data = data, 71 .session = session, 72 .button_id = button_id, 73 .users_tab_id = users_tab_id, 74 }; 75 } 76 77 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 78 self.center.deinit(allocator); 79 } 80 81 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 82 try self.center.build(allocator, constraint, root_focus); 83 } 84 85 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 86 _ = allocator; 87 switch (key) { 88 .enter => { 89 try self.logout(root_focus); 90 return; 91 }, 92 .mouse => |mouse| { 93 if (inp.leftClickOn(root_focus, self.button_id, mouse)) { 94 try self.logout(root_focus); 95 return; 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 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