╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1782 const title_input = &form.children.values()[title_field_index].widget.text_input; 1783 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1784 const description_input = &form.children.values()[description_field_index].widget.text_input; 1785 1786 const title = try title_input.text(allocator); 1787 defer allocator.free(title); 1788 const tags = try tags_input.text(allocator); 1789 defer allocator.free(tags); 1790 const description = try description_input.text(allocator); 1791 defer allocator.free(description); 1792 1793 if (!evt.Issue.fieldsValid(title, tags)) return; 1794 1795 var id_bytes: [evt.event_id_size]u8 = undefined; 1796 io.random(&id_bytes); 1797 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1798 1799 const event = evt.EventWithId{ 1800 .id = event_id_hex, 1801 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1802 .author_email = author_email, 1803 .event = .{ .issue = .{ 1804 .title = title, 1805 .description = description, 1806 .tags = tags, 1807 } }, 1808 }; 1809 1810 switch (src.repo_kind) { 1811 inline else => |repo_kind| { 1812 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1813 defer any_repo.deinit(io, allocator); 1814 switch (any_repo) { 1815 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1816 } 1817 }, 1818 } 1819 1820 // wipe the form so a return visit starts fresh 1821 title_input.clear(allocator); 1822 tags_input.clear(allocator); 1823 description_input.clear(allocator); 1824 1825 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1826 try self.session.navigate(route); 1827 } 1828 1829 // re-emit the selected issue's event with the form's content (status 1830 // preserved), then reload the issue's page. this is the terminal path; 1831 // the web posts the form to the edit route. 1832 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 1833 if (comptime wasm) return; 1834 const io = self.session.io orelse return; 1835 const src = self.data.repo_source orelse return; 1836 const entry = self.data.selectedIssue() orelse return; 1837 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1838 1839 const form = self.issueForm() orelse return; 1840 const title_input = &form.children.values()[title_field_index].widget.text_input; 1841 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1842 const description_input = &form.children.values()[description_field_index].widget.text_input; 1843 1844 const title = try title_input.text(allocator); 1845 defer allocator.free(title); 1846 const tags = try tags_input.text(allocator); 1847 defer allocator.free(tags); 1848 const description = try description_input.text(allocator); 1849 defer allocator.free(description); 1850 1851 if (!evt.Issue.fieldsValid(title, tags)) return; 1852 1853 var id_bytes: [evt.event_id_size]u8 = undefined; 1854 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1855 1856 switch (src.repo_kind) { 1857 inline else => |repo_kind| { 1858 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1859 defer any_repo.deinit(io, allocator); 1860 switch (any_repo) { 1861 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 1862 .title = title, 1863 .tags = tags, 1864 .description = description, 1865 } }, author_email), 1866 } 1867 }, 1868 } 1869 1870 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1871 try self.session.navigate(route); 1872 } 1873 1874 // flip the shown issue's status by re-emitting its event, then reload the 1875 // page rooted at the issue so the view reflects the change. this is the 1876 // terminal path; the web posts the button's form to the status route. 1877 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1878 if (comptime wasm) return; 1879 const io = self.session.io orelse return; 1880 const src = self.data.repo_source orelse return; 1881 const sel = self.detailed_index[index] orelse return; 1882 const entry = self.window(index).issues[sel]; 1883 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1884 1885 const status: evt.Issue.Status = switch (entry.issue.event.status) { 1886 .open => .closed, 1887 .closed => .open, 1888 }; 1889 1890 var id_bytes: [evt.event_id_size]u8 = undefined; 1891 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1892 1893 switch (src.repo_kind) { 1894 inline else => |repo_kind| { 1895 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1896 defer any_repo.deinit(io, allocator); 1897 switch (any_repo) { 1898 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 1899 } 1900 }, 1901 } 1902 1903 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 1904 try self.session.navigate(route); 1905 } 1906 1907 // arrow keys move the tag selection; at the flow's edges focus crosses to 1908 // the neighboring widgets. 1909 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1910 const tf = self.tagFlow(index) orelse return; 1911 const cid = tf.focus.child_id orelse return; 1912 const cur = tf.indexOfFocusId(cid) orelse return; 1913 const count = tf.text_boxes.items.len; 1914 switch (key) { 1915 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1916 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1917 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 1918 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1919 .home => self.focusTag(index, tf, root_focus, 0), 1920 .end => self.focusTag(index, tf, root_focus, count - 1), 1921 else => {}, 1922 } 1923 } 1924 1925 const button_in_row_index: usize = 1; 1926 const title_child_index: usize = 0; 1927 const tags_child_index: usize = 2; 1928 1929 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1930 const inner = self.detailInner(index); 1931 if (inner.children.count() <= tags_child_index) return null; 1932 return switch (inner.children.values()[tags_child_index].widget) { 1933 .tag_flow => |*tf| tf, 1934 else => null, 1935 }; 1936 } 1937 1938 fn tagsFocused(self: *View, index: usize) bool { 1939 const inner = self.detailInner(index); 1940 const cid = inner.getFocus().child_id orelse return false; 1941 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1942 } 1943 1944 // the open/close button inside the detail frame's tool row, identified 1945 // by its posting kind; a logged-out session's or conflicted issue's row 1946 // has none. 1947 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1948 const row = self.toolRow(index); 1949 if (row.children.count() <= button_in_row_index) return null; 1950 switch (row.children.values()[button_in_row_index].widget) { 1951 .text_box => |*tb| switch (tb.box.focus.kind) { 1952 .custom => |custom| if (std.mem.eql(u8, custom, "submit")) return tb, 1953 else => {}, 1954 }, 1955 else => {}, 1956 } 1957 return null; 1958 } 1959 1960 fn toolRowFocused(self: *View, index: usize) bool { 1961 const outer = self.detailOuter(index); 1962 const cid = outer.getFocus().child_id orelse return false; 1963 return outer.children.getIndex(cid) == tool_row_index; 1964 } 1965 1966 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1967 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1968 // keep the tag visible on the terminal: its rect offset by the flow's 1969 // position in the pane. 1970 if (self.session.is_terminal and item < tf.rects.items.len) { 1971 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1972 var rect = tf.rects.items[item]; 1973 rect.x += flow_rect.x; 1974 rect.y += flow_rect.y; 1975 self.detailScroll(index).scrollToRect(rect); 1976 } 1977 } 1978 } 1979 1980 fn titleFocused(self: *View, index: usize) bool { 1981 const inner = self.detailInner(index); 1982 const cid = inner.getFocus().child_id orelse return false; 1983 return inner.children.getIndex(cid) == title_child_index; 1984 } 1985 1986 fn authorPresent(self: *View, index: usize) bool { 1987 return self.author_id[index] != null; 1988 } 1989 1990 fn authorFocused(self: *View, index: usize) bool { 1991 const inner = self.detailInner(index); 1992 const cid = inner.getFocus().child_id orelse return false; 1993 return cid == self.author_id[index]; 1994 } 1995 1996 // the author sits right under the title at the top of the pane, so 1997 // focusing it scrolls there. 1998 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 1999 const id = self.author_id[index] orelse return; 2000 root_focus.setFocus(id); 2001 self.detailScroll(index).y = 0; 2002 } 2003 2004 // the title sits at the top of the pane, so focusing it scrolls there. 2005 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2006 const inner = self.detailInner(index); 2007 if (inner.children.count() == 0) return; 2008 root_focus.setFocus(inner.children.keys()[title_child_index]); 2009 self.detailScroll(index).y = 0; 2010 } 2011 2012 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2013 const tf = self.tagFlow(index) orelse return; 2014 if (tf.text_boxes.items.len == 0) return; 2015 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2016 const item = tf.indexOfFocusId(cid) orelse 0; 2017 self.focusTag(index, tf, root_focus, item); 2018 } 2019 2020 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2021 if (self.description_id[index]) |id| root_focus.setFocus(id); 2022 } 2023 2024 // return to the tool row's last-focused button (the header when the 2025 // description page shows no row). 2026 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2027 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2028 root_focus.setFocus(cid); 2029 } 2030 2031 // enter the detail pane. an empty pane (no issues) can't be entered. 2032 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2033 if (self.detailInner(index).children.count() == 0) return; 2034 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2035 } 2036 2037 // return to the list. 2038 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2039 root_focus.setFocus(self.listScroll(index).getFocus().id); 2040 } 2041 2042 // cross to the header tabs above the stack. 2043 fn focusHeader(self: *View, root_focus: *Focus) void { 2044 root_focus.setFocus(self.box.children.keys()[header_index]); 2045 } 2046 2047 pub fn clearGrid(self: *View) void { 2048 self.box.clearGrid(); 2049 } 2050 2051 pub fn getGrid(self: View) ?Grid { 2052 return self.box.getGrid(); 2053 } 2054 2055 pub fn getFocus(self: *View) *Focus { 2056 return self.box.getFocus(); 2057 } 2058 2059 // for the parent's "scroll up at the top jumps to the header" check: at the 2060 // top only while the header tabs hold focus, so up from the split 2061 // crosses into the tabs first. 2062 pub fn getSelectedIndex(self: *View) ?usize { 2063 return if (self.headerActive()) 0 else 1; 2064 } 2065 }; 2066 2067 // the "a:" navigation link for `status`'s issues page filtered to the 2068 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2069 // a rooted url ignores `status` (it derives its view from the issue's own); 2070 // with an empty `id` this is the bare list link. 2071 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2072 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2073 const url = try route.toUrl(page_arena); 2074 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2075 } 2076 2077 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2078 // href is only followed with js off. 2079 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2080 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2081 const url = try route.toUrl(page_arena); 2082 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2083 } 2084 2085 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2086 // or the conflicts window when null, rooted at issue `id`. 2087 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2088 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2089 return conflictsLink(page_arena, data.identity, id); 2090 } 2091 2092 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2093 // first window). 2094 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2095 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2096 const url = try route.toUrl(page_arena); 2097 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2098 } 2099 2100 // the "a:" link to the whole-description page of issue `id` within `identity`. 2101 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2102 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2103 const url = try route.toUrl(page_arena); 2104 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2105 } 2106 2107 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2108 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2109 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2110 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2111 const url = try route.toUrl(page_arena); 2112 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2113 } 2114 2115 // tabs switching between the issues page's views. 2116 pub const Header = struct { 2117 box: wgt.Box(ui.Widget), 2118 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2119 2120 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2121 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2122 errdefer box.deinit(allocator); 2123 2124 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2125 errdefer tab_ids.deinit(allocator); 2126 2127 const aa = session.page_arena.allocator(); 2128 2129 // a list tab per status, labeled with its listing's issue count 2130 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2131 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2132 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2133 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2134 try addTab(allocator, &box, &tab_ids, label, link); 2135 } 2136 2137 // tags tab, labeled with the active tag filter 2138 { 2139 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2140 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2141 const label = if (data.tag.len == 0) "tags" else blk: { 2142 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2143 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2144 }; 2145 try addTab(allocator, &box, &tab_ids, label, tags_link); 2146 } 2147 2148 // new-issue tab; an edit or resolve url shows its tab in this place 2149 { 2150 const route = switch (data.view) { 2151 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2152 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2153 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2154 }; 2155 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2156 const label: []const u8 = switch (data.view) { 2157 .edit => "edit", 2158 .resolve => "resolve", 2159 else => "new", 2160 }; 2161 try addTab(allocator, &box, &tab_ids, label, link); 2162 } 2163 2164 // conflicts tab, labeled with the conflict count 2165 if (data.conflicts.count > 0) { 2166 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2167 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2168 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2169 try addTab(allocator, &box, &tab_ids, label, link); 2170 } 2171 2172 var self = Header{ .box = box, .tab_ids = tab_ids }; 2173 // the tab matching the page's view is selected initially. 2174 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2175 return self; 2176 } 2177 2178 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2179 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2180 errdefer text_box.deinit(allocator); 2181 text_box.getFocus().focusable = true; 2182 text_box.getFocus().kind = .{ .custom = link }; 2183 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2184 try box.children.put(allocator, text_box.getFocus().id, .{ 2185 .widget = .{ .text_box = text_box }, 2186 .rect = null, 2187 .min_size = .{ .width = label.len + 2, .height = null }, 2188 }); 2189 } 2190 2191 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2192 self.box.deinit(allocator); 2193 self.tab_ids.deinit(allocator); 2194 } 2195 2196 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2197 self.clearGrid(); 2198 // only the selected tab shows its border 2199 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2200 switch (child.widget) { 2201 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2202 else => {}, 2203 } 2204 } 2205 try self.box.build(allocator, constraint, root_focus); 2206 } 2207 2208 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2209 _ = allocator; 2210 const current_tab = self.currentTabIndex() orelse return; 2211 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2212 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2213 } 2214 } 2215 2216 pub fn clearGrid(self: *Header) void { 2217 self.box.clearGrid(); 2218 } 2219 2220 pub fn getGrid(self: Header) ?Grid { 2221 return self.box.getGrid(); 2222 } 2223 2224 pub fn getFocus(self: *Header) *Focus { 2225 return self.box.getFocus(); 2226 } 2227 2228 pub fn getSelectedIndex(self: Header) ?usize { 2229 return self.currentTabIndex(); 2230 } 2231 2232 fn currentTabIndex(self: Header) ?usize { 2233 const child_id = self.box.focus.child_id orelse return null; 2234 return self.tab_ids.getIndex(child_id); 2235 } 2236 }; 2237