╭ 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 // `tag` is the url-encoded tag filter ("" = unfiltered). 19 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, count: usize, tag: []const u8) !View { 20 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .rounded_corners = true, .direction = .horiz }); 21 errdefer box.deinit(allocator); 22 23 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 24 errdefer tab_ids.deinit(allocator); 25 26 const aa = session.page_arena.allocator(); 27 28 // results tab, labeled with the listing's issue count 29 { 30 const label = try std.fmt.allocPrint(aa, "results ({d})", .{count}); 31 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 32 errdefer text_box.deinit(allocator); 33 text_box.getFocus().focusable = true; 34 try tab_ids.put(allocator, text_box.getFocus().id, {}); 35 try box.children.put(allocator, text_box.getFocus().id, .{ 36 .widget = .{ .text_box = text_box }, 37 .rect = null, 38 .min_size = .{ .width = label.len + 2, .height = null }, 39 }); 40 } 41 42 // tags tab, labeled with the active tag filter 43 { 44 const label = if (tag.len == 0) "tags" else blk: { 45 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, tag)); 46 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 47 }; 48 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 49 errdefer text_box.deinit(allocator); 50 text_box.getFocus().focusable = true; 51 try tab_ids.put(allocator, text_box.getFocus().id, {}); 52 try box.children.put(allocator, text_box.getFocus().id, .{ 53 .widget = .{ .text_box = text_box }, 54 .rect = null, 55 .min_size = .{ .width = label.len + 2, .height = null }, 56 }); 57 } 58 59 var self = View{ .box = box, .tab_ids = tab_ids }; 60 self.getFocus().child_id = self.tab_ids.keys()[0]; 61 return self; 62 } 63 64 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 65 self.box.deinit(allocator); 66 self.tab_ids.deinit(allocator); 67 } 68 69 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 70 self.clearGrid(); 71 // only the selected tab shows its border 72 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 73 switch (child.widget) { 74 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 75 else => {}, 76 } 77 } 78 try self.box.build(allocator, constraint, root_focus); 79 } 80 81 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 82 _ = allocator; 83 const current_tab = self.currentTabIndex() orelse return; 84 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 85 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 86 } 87 } 88 89 pub fn clearGrid(self: *View) void { 90 self.box.clearGrid(); 91 } 92 93 pub fn getGrid(self: View) ?Grid { 94 return self.box.getGrid(); 95 } 96 97 pub fn getFocus(self: *View) *Focus { 98 return self.box.getFocus(); 99 } 100 101 pub fn getSelectedIndex(self: View) ?usize { 102 return self.currentTabIndex(); 103 } 104 105 fn currentTabIndex(self: View) ?usize { 106 const child_id = self.box.focus.child_id orelse return null; 107 return self.tab_ids.getIndex(child_id); 108 } 109 }; 110