╭ 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. 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, identity: []const u8, 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 const results_route = ui.RoutablePage.repoIssuesRoute(identity, tag, "") orelse return error.RouteTooLong; 29 const results_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try results_route.urlAlloc(session.page_arena)}); 30 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(identity) orelse return error.RouteTooLong; 31 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.urlAlloc(session.page_arena)}); 32 33 // results tab, labeled with the listing's issue count 34 { 35 const label = try std.fmt.allocPrint(aa, "results ({d})", .{count}); 36 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 37 errdefer text_box.deinit(allocator); 38 text_box.getFocus().focusable = true; 39 text_box.getFocus().kind = .{ .custom = results_link }; 40 try tab_ids.put(allocator, text_box.getFocus().id, {}); 41 try box.children.put(allocator, text_box.getFocus().id, .{ 42 .widget = .{ .text_box = text_box }, 43 .rect = null, 44 .min_size = .{ .width = label.len + 2, .height = null }, 45 }); 46 } 47 48 // tags tab, labeled with the active tag filter 49 { 50 const label = if (tag.len == 0) "tags" else blk: { 51 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, tag)); 52 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 53 }; 54 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 55 errdefer text_box.deinit(allocator); 56 text_box.getFocus().focusable = true; 57 text_box.getFocus().kind = .{ .custom = tags_link }; 58 try tab_ids.put(allocator, text_box.getFocus().id, {}); 59 try box.children.put(allocator, text_box.getFocus().id, .{ 60 .widget = .{ .text_box = text_box }, 61 .rect = null, 62 .min_size = .{ .width = label.len + 2, .height = null }, 63 }); 64 } 65 66 var self = View{ .box = box, .tab_ids = tab_ids }; 67 // the tab matching the incoming route's view is selected initially. 68 const selected_index: usize = switch (session.data.current_page) { 69 .repo_issues => |i| switch (i.view) { 70 .results => 0, 71 .tags => 1, 72 }, 73 else => 0, 74 }; 75 self.getFocus().child_id = self.tab_ids.keys()[selected_index]; 76 return self; 77 } 78 79 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 80 self.box.deinit(allocator); 81 self.tab_ids.deinit(allocator); 82 } 83 84 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 85 self.clearGrid(); 86 // only the selected tab shows its border 87 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 88 switch (child.widget) { 89 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 90 else => {}, 91 } 92 } 93 try self.box.build(allocator, constraint, root_focus); 94 } 95 96 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 97 _ = allocator; 98 const current_tab = self.currentTabIndex() orelse return; 99 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 100 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 101 } 102 } 103 104 pub fn clearGrid(self: *View) void { 105 self.box.clearGrid(); 106 } 107 108 pub fn getGrid(self: View) ?Grid { 109 return self.box.getGrid(); 110 } 111 112 pub fn getFocus(self: *View) *Focus { 113 return self.box.getFocus(); 114 } 115 116 pub fn getSelectedIndex(self: View) ?usize { 117 return self.currentTabIndex(); 118 } 119 120 fn currentTabIndex(self: View) ?usize { 121 const child_id = self.box.focus.child_id orelse return null; 122 return self.tab_ids.getIndex(child_id); 123 } 124 }; 125