╭ 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 session: *ui.Session, 26 button_id: usize, 27 28 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !View { 29 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 30 errdefer box.deinit(allocator); 31 32 { 33 var prompt = try wgt.Text(ui.Widget).init(allocator, "are you sure?"); 34 errdefer prompt.deinit(allocator); 35 try box.children.put(allocator, prompt.getFocus().id, .{ 36 .widget = .{ .text = prompt }, 37 .rect = null, 38 .min_size = null, 39 }); 40 } 41 42 var button_id: usize = undefined; 43 { 44 var button = try wgt.TextBox(ui.Widget).init(allocator, "quit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 45 errdefer button.deinit(allocator); 46 button.getFocus().focusable = true; 47 button_id = button.getFocus().id; 48 try box.children.put(allocator, button.getFocus().id, .{ 49 .widget = .{ .text_box = button }, 50 .rect = null, 51 .min_size = null, 52 }); 53 } 54 55 box.getFocus().child_id = button_id; 56 57 return .{ 58 .center = try ui.Center.init(allocator, .{ .box = box }), 59 .session = session, 60 .button_id = button_id, 61 }; 62 } 63 64 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 65 self.center.deinit(allocator); 66 } 67 68 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 69 try self.center.build(allocator, constraint, root_focus); 70 } 71 72 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 73 _ = allocator; 74 switch (key) { 75 .enter => { 76 self.session.quit_requested = true; 77 return; 78 }, 79 .mouse => |mouse| { 80 if (mouse.action == .press and mouse.action.press == .left) { 81 if (root_focus.children.get(self.button_id)) |entry| { 82 const r = entry.rect; 83 if (mouse.x >= r.x and mouse.y >= r.y and 84 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 85 { 86 self.session.quit_requested = true; 87 return; 88 } 89 } 90 } 91 }, 92 else => {}, 93 } 94 } 95 96 pub fn clearGrid(self: *View) void { 97 self.center.clearGrid(); 98 } 99 100 pub fn getGrid(self: View) ?Grid { 101 return self.center.getGrid(); 102 } 103 104 pub fn getFocus(self: *View) *Focus { 105 return self.center.getFocus(); 106 } 107 108 pub fn getSelectedIndex(self: View) ?usize { 109 _ = self; 110 return 0; 111 } 112 }; 113