╭ SubHeader.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 // tabs switching between the issues page's views. only "results" for now. 13 14 pub const View = struct { 15 box: wgt.Box(ui.Widget), 16 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 17 18 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, count: usize) !View { 19 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 20 errdefer box.deinit(allocator); 21 22 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 23 errdefer tab_ids.deinit(allocator); 24 25 // results tab, labeled with the listing's issue count 26 { 27 const label = try std.fmt.allocPrint(session.page_arena.allocator(), "results ({d})", .{count}); 28 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 29 errdefer text_box.deinit(allocator); 30 text_box.getFocus().focusable = true; 31 try tab_ids.put(allocator, text_box.getFocus().id, {}); 32 try box.children.put(allocator, text_box.getFocus().id, .{ 33 .widget = .{ .text_box = text_box }, 34 .rect = null, 35 .min_size = .{ .width = label.len + 2, .height = null }, 36 }); 37 } 38 39 var self = View{ .box = box, .tab_ids = tab_ids }; 40 self.getFocus().child_id = self.tab_ids.keys()[0]; 41 return self; 42 } 43 44 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 45 self.box.deinit(allocator); 46 self.tab_ids.deinit(allocator); 47 } 48 49 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 50 self.clearGrid(); 51 // only the selected tab shows its border 52 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 53 switch (child.widget) { 54 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 55 else => {}, 56 } 57 } 58 try self.box.build(allocator, constraint, root_focus); 59 } 60 61 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 62 _ = allocator; 63 const current_tab = self.currentTabIndex() orelse return; 64 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 65 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 66 } 67 } 68 69 pub fn clearGrid(self: *View) void { 70 self.box.clearGrid(); 71 } 72 73 pub fn getGrid(self: View) ?Grid { 74 return self.box.getGrid(); 75 } 76 77 pub fn getFocus(self: *View) *Focus { 78 return self.box.getFocus(); 79 } 80 81 pub fn getSelectedIndex(self: View) ?usize { 82 return self.currentTabIndex(); 83 } 84 85 fn currentTabIndex(self: View) ?usize { 86 const child_id = self.box.focus.child_id orelse return null; 87 return self.tab_ids.getIndex(child_id); 88 } 89 }; 90