╭ Quit.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 // focus kind marking the header's quit tab 14 pub const tab_kind = "quit"; 15 16 // label shown on the header's quit tab (a single-column box-drawing cross) 17 pub const tab_label = "╳"; 18 19 pub fn init() Self { 20 return .{}; 21 } 22 23 pub const View = struct { 24 center: ui.Center, 25 data: *const Self, 26 session: *ui.Session, 27 button_id: usize, 28 29 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 30 var box = wgt.Box(ui.Widget).init(.{ .border_style = null, .rounded_corners = true, .direction = .vert }); 31 errdefer box.deinit(allocator); 32 33 { 34 var prompt = wgt.Text(ui.Widget).init("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, "quit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 46 errdefer button.deinit(allocator); 47 button.getFocus().focusable = true; 48 button_id = button.getFocus().id; 49 try box.children.put(allocator, button.getFocus().id, .{ 50 .widget = .{ .text_box = button }, 51 .rect = null, 52 .min_size = null, 53 }); 54 } 55 56 box.getFocus().child_id = button_id; 57 58 return .{ 59 .center = try ui.Center.init(allocator, .{ .box = box }, .both), 60 .data = data, 61 .session = session, 62 .button_id = button_id, 63 }; 64 } 65 66 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 67 self.center.deinit(allocator); 68 } 69 70 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 71 try self.center.build(allocator, constraint, root_focus); 72 } 73 74 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 75 _ = allocator; 76 switch (key) { 77 .enter => { 78 self.session.quit_requested = true; 79 return; 80 }, 81 .mouse => |mouse| { 82 if (mouse.action == .press and mouse.action.press == .left) { 83 if (root_focus.children.get(self.button_id)) |entry| { 84 const r = entry.rect; 85 if (mouse.x >= r.x and mouse.y >= r.y and 86 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 87 { 88 self.session.quit_requested = true; 89 return; 90 } 91 } 92 } 93 }, 94 else => {}, 95 } 96 } 97 98 pub fn clearGrid(self: *View) void { 99 self.center.clearGrid(); 100 } 101 102 pub fn getGrid(self: View) ?Grid { 103 return self.center.getGrid(); 104 } 105 106 pub fn getFocus(self: *View) *Focus { 107 return self.center.getFocus(); 108 } 109 110 pub fn getSelectedIndex(self: View) ?usize { 111 _ = self; 112 return 0; 113 } 114 }; 115