╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1899 self.focusVisible(index, root_focus, to_end); 1900 } else { 1901 self.focusDetailEdge(index, root_focus, to_end); 1902 } 1903 } 1904 1905 fn focusDetailEdge(self: *View, index: usize, root_focus: *Focus, last: bool) void { 1906 const focus = self.detailInner(index).getFocus(); 1907 var chosen: ?usize = null; 1908 for (focus.children.keys(), focus.children.values()) |id, child| { 1909 if (!child.focus.focusable) continue; 1910 chosen = id; 1911 if (!last) break; 1912 } 1913 if (chosen) |id| root_focus.setFocus(id); 1914 } 1915 1916 fn detailRectVisible(self: *View, index: usize, rect: layout.IRect) bool { 1917 const sc = self.detailScroll(index); 1918 const viewport = sc.grid orelse return false; 1919 const top = sc.y; 1920 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1921 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1922 } 1923 1924 // focus the top- or bottom-most focusable widget intersecting the viewport. 1925 fn focusVisible(self: *View, index: usize, root_focus: *Focus, last: bool) void { 1926 const inner = self.detailInner(index); 1927 var offset: usize = 0; 1928 while (offset < inner.children.count()) : (offset += 1) { 1929 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1930 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1931 } 1932 } 1933 1934 fn focusVisibleChild(self: *View, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1935 const child = &self.detailInner(index).children.values()[child_index]; 1936 const child_rect = child.rect orelse return false; 1937 var chosen: ?usize = null; 1938 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1939 const id = self.detailInner(index).children.keys()[child_index]; 1940 chosen = id; 1941 if (!last) { 1942 root_focus.setFocus(id); 1943 return true; 1944 } 1945 } 1946 1947 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1948 if (!focus_child.focus.focusable) continue; 1949 const rect: layout.IRect = .{ 1950 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1951 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1952 .size = focus_child.rect.size, 1953 }; 1954 if (!self.detailRectVisible(index, rect)) continue; 1955 chosen = id; 1956 if (!last) break; 1957 } 1958 if (chosen) |id| { 1959 root_focus.setFocus(id); 1960 return true; 1961 } 1962 return false; 1963 } 1964 1965 fn focusAdjacentVisible(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 1966 const inner = self.detailInner(index); 1967 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1968 while (true) { 1969 if (down) { 1970 next += 1; 1971 if (next >= inner.children.count()) return false; 1972 } else { 1973 if (next == 0) return false; 1974 next -= 1; 1975 } 1976 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1977 self.focusDetailChild(index, next, !down, root_focus); 1978 return true; 1979 } 1980 } 1981 } 1982 1983 fn focusedDetailChild(self: *View, index: usize, root_focus: *Focus) ?usize { 1984 const inner = self.detailInner(index); 1985 var id = root_focus.grandchild_id orelse return null; 1986 while (inner.getFocus().children.get(id)) |child| { 1987 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1988 id = child.parent_id; 1989 } 1990 return null; 1991 } 1992 1993 fn detailChildFocusable(self: *View, index: usize, child_index: usize) bool { 1994 const child = &self.detailInner(index).children.values()[child_index].widget; 1995 const focus = child.getFocus(); 1996 if (focus.focusable) return true; 1997 for (focus.children.values()) |item| { 1998 if (item.focus.focusable) return true; 1999 } 2000 return false; 2001 } 2002 2003 fn focusDetailChild(self: *View, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 2004 const inner = self.detailInner(index); 2005 const child = &inner.children.values()[child_index]; 2006 var target = inner.children.keys()[child_index]; 2007 var rect = child.rect orelse return; 2008 switch (child.widget) { 2009 .repo_comment => |*comment| { 2010 if (comment.rowRect(last)) |row_rect| { 2011 rect.x += row_rect.x; 2012 rect.y += row_rect.y; 2013 rect.size = row_rect.size; 2014 } 2015 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 2016 }, 2017 .box => |*box| if (box.children.count() > 0) { 2018 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 2019 root_focus.setFocus(target); 2020 }, 2021 else => root_focus.setFocus(target), 2022 } 2023 const sc = self.detailScroll(index); 2024 if (sc.grid) |viewport| { 2025 const viewport_height = viewport.size.height - sc.bar_h; 2026 if (rect.size.height > viewport_height) { 2027 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 2028 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 2029 sc.clampToContent(); 2030 return; 2031 } 2032 } 2033 sc.scrollToRect(rect); 2034 } 2035 2036 fn moveDetailVertical(self: *View, index: usize, root_focus: *Focus, down: bool) bool { 2037 const inner = self.detailInner(index); 2038 const current = self.focusedDetailChild(index, root_focus) orelse return false; 2039 if (inner.children.values()[current].widget == .repo_comment) { 2040 const comment = &inner.children.values()[current].widget.repo_comment; 2041 if (down and !comment.bodyFocused()) { 2042 self.focusDetailChild(index, current, true, root_focus); 2043 return true; 2044 } 2045 if (!down and comment.bodyFocused()) { 2046 self.focusDetailChild(index, current, false, root_focus); 2047 return true; 2048 } 2049 } 2050 2051 var next = current; 2052 while (true) { 2053 if (down) { 2054 next += 1; 2055 if (next >= inner.children.count()) return false; 2056 } else { 2057 if (next == 0) return false; 2058 next -= 1; 2059 } 2060 if (!self.detailChildFocusable(index, next)) continue; 2061 self.focusDetailChild(index, next, !down, root_focus); 2062 return true; 2063 } 2064 } 2065 2066 fn moveCommentHorizontal(self: *View, index: usize, root_focus: *Focus, right: bool) bool { 2067 const inner = self.detailInner(index); 2068 const current = self.focusedDetailChild(index, root_focus) orelse return false; 2069 const child = &inner.children.values()[current]; 2070 if (child.widget == .repo_comment) { 2071 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 2072 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 2073 return moved; 2074 } 2075 const row = switch (child.widget) { 2076 .box => |*box| box, 2077 else => return false, 2078 }; 2079 const selected = row.getFocus().child_id orelse return false; 2080 const selected_index = row.children.getIndex(selected) orelse return false; 2081 const target = if (right) selected_index + 1 else selected_index -| 1; 2082 if (target == selected_index or target >= row.children.count()) return false; 2083 root_focus.setFocus(row.children.keys()[target]); 2084 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 2085 return true; 2086 } 2087 2088 // up/down (and tab/shift+tab) move between a form's fields; up from the 2089 // title crosses into the header tabs. the multiline description keeps 2090 // enter and any up/down that has a row to move to. 2091 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2092 const form = self.issueForm() orelse return; 2093 const cid = form.getFocus().child_id orelse return; 2094 const cur = form.children.getIndex(cid) orelse return; 2095 2096 if (cur == description_field_index) { 2097 const child = &form.children.values()[cur]; 2098 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2099 return child.widget.input(allocator, key, root_focus); 2100 } 2101 2102 switch (key) { 2103 .arrow_up, .back_tab => if (cur > 0) 2104 root_focus.setFocus(form.children.keys()[cur - 1]) 2105 else 2106 self.focusHeader(root_focus), 2107 .arrow_down, .tab => if (cur < submit_field_index) { 2108 root_focus.setFocus(form.children.keys()[cur + 1]); 2109 }, 2110 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2111 .mouse => |mouse| if (cur == submit_field_index) { 2112 const submit = &form.children.values()[cur].widget.submit_button; 2113 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2114 }, 2115 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2116 } 2117 } 2118 2119 fn commentFormInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2120 const form = self.issueForm() orelse return; 2121 const cid = form.getFocus().child_id orelse return; 2122 const cur = form.children.getIndex(cid) orelse return; 2123 const child = &form.children.values()[cur]; 2124 2125 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2126 return child.widget.input(allocator, key, root_focus); 2127 2128 switch (key) { 2129 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2130 root_focus.setFocus(form.children.keys()[cur - 1]) 2131 else 2132 self.focusHeader(root_focus), 2133 .arrow_down, .tab => if (cur < comment_submit_field_index) 2134 root_focus.setFocus(form.children.keys()[cur + 1]), 2135 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2136 .mouse => |mouse| if (cur == comment_submit_field_index) { 2137 const submit = &child.widget.submit_button; 2138 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2139 }, 2140 else => try child.widget.input(allocator, key, root_focus), 2141 } 2142 } 2143 2144 // the multiline hunk inputs keep enter and any up/down that has a row to 2145 // move to; enter on a "use this" link is a navigation the host follows 2146 // before this runs. 2147 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2148 const form = self.issueForm() orelse return; 2149 const cid = form.getFocus().child_id orelse return; 2150 const cur = form.children.getIndex(cid) orelse return; 2151 const child = &form.children.values()[cur]; 2152 2153 if (child.widget == .text_input) { 2154 const ti = &child.widget.text_input; 2155 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2156 return child.widget.input(allocator, key, root_focus); 2157 } 2158 2159 const on_submit = child.widget == .submit_button; 2160 2161 switch (key) { 2162 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2163 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2164 .arrow_left, .arrow_right => switch (child.widget) { 2165 .box => |*row| { 2166 const row_cid = row.getFocus().child_id orelse return; 2167 const row_index = row.children.getIndex(row_cid) orelse return; 2168 if (key == .arrow_left) { 2169 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2170 } else if (row_index + 1 < row.children.count()) { 2171 root_focus.setFocus(row.children.keys()[row_index + 1]); 2172 } 2173 }, 2174 else => try child.widget.input(allocator, key, root_focus), 2175 }, 2176 .enter => if (on_submit) try self.submitResolution(allocator), 2177 .mouse => |mouse| if (on_submit) { 2178 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2179 }, 2180 else => try child.widget.input(allocator, key, root_focus), 2181 } 2182 } 2183 2184 // a multiline input keeps enter and any up/down that has a row to move to 2185 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 2186 return switch (key) { 2187 .enter => true, 2188 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2189 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2190 else => false, 2191 }; 2192 } 2193 2194 // the neighboring focusable resolve-form child, skipping the spacer and 2195 // the blank gap rows 2196 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 2197 var i = cur; 2198 while (true) { 2199 if (down) { 2200 i += 1; 2201 if (i >= form.children.count()) return null; 2202 } else { 2203 if (i == 0) return null; 2204 i -= 1; 2205 } 2206 switch (form.children.values()[i].widget) { 2207 .spacer, .text => continue, 2208 else => return i, 2209 } 2210 } 2211 } 2212 2213 // focus the resolve-form child at `i` (a version row focuses its selected 2214 // child) and keep it visible on the terminal 2215 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 2216 const child = &form.children.values()[i]; 2217 switch (child.widget) { 2218 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2219 else => root_focus.setFocus(form.children.keys()[i]), 2220 } 2221 if (self.session.is_terminal) { 2222 if (self.resolveScroll()) |sc| { 2223 if (child.rect) |rect| sc.scrollToRect(rect); 2224 } 2225 } 2226 } 2227 2228 // re-emit the conflicted issue's event with the resolved content; any 2229 // event on the issue settles its conflict entry. this is the terminal 2230 // path; the web posts the form to the resolve route. 2231 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 2232 if (comptime wasm) return; 2233 const io = self.session.io orelse return; 2234 const src = self.data.repo_source orelse return; 2235 const entry = self.data.selectedIssue() orelse return; 2236 const author = (try self.session.eventAuthor()) orelse return; 2237 const form = self.issueForm() orelse return; 2238 2239 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2240 var title: ?[]u8 = null; 2241 var tags: ?[]u8 = null; 2242 var hunks: std.ArrayList([]const u8) = .empty; 2243 defer { 2244 if (title) |t| allocator.free(t); 2245 if (tags) |t| allocator.free(t); 2246 for (hunks.items) |h| allocator.free(h); 2247 hunks.deinit(allocator); 2248 } 2249 for (form.children.values()) |*child| switch (child.widget) { 2250 .text_input => |*ti| { 2251 const text = try ti.text(allocator); 2252 errdefer allocator.free(text); 2253 if (std.mem.eql(u8, ti.options.name, "title")) { 2254 title = text; 2255 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2256 tags = text; 2257 } else { 2258 try hunks.append(allocator, text); 2259 } 2260 }, 2261 else => {}, 2262 }; 2263 2264 var id_bytes: [evt.event_id_size]u8 = undefined; 2265 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2266 2267 switch (src.repo_kind) { 2268 inline else => |repo_kind| { 2269 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2270 defer any_repo.deinit(io, allocator); 2271 switch (any_repo) { 2272 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 2273 .title = title, 2274 .tags = tags, 2275 .hunks = hunks.items, 2276 } }, author) catch |err| switch (err) { 2277 // leave the form up for correction 2278 error.InvalidFields => return, 2279 else => |e| return e, 2280 }, 2281 } 2282 }, 2283 } 2284 2285 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2286 try self.session.navigate(route); 2287 } 2288 2289 // commit a new or edited comment and navigate to its permalink. this is the 2290 // terminal path; the web posts the form to its comment route. 2291 fn submitComment(self: *View, allocator: std.mem.Allocator) !void { 2292 if (comptime wasm) return; 2293 const io = self.session.io orelse return; 2294 const src = self.data.repo_source orelse return; 2295 const author = (try self.session.eventAuthor()) orelse return; 2296 const form = self.issueForm() orelse return; 2297 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2298 const body = try body_input.text(allocator); 2299 defer allocator.free(body); 2300 if (!evt.Comment.fieldsValid(body)) return; 2301 2302 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2303 @memcpy(&thread_id, self.data.selected_id); 2304 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2305 var parent: [evt.event_id_size * 2]u8 = undefined; 2306 @memcpy(&parent, self.data.comment_id); 2307 break :blk parent; 2308 }; 2309 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2310 const editing = self.data.view == .edit_comment; 2311 switch (src.repo_kind) { 2312 inline else => |repo_kind| { 2313 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2314 defer any_repo.deinit(io, allocator); 2315 switch (any_repo) { 2316 inline else => |*repo| if (editing) { 2317 var comment_id: [evt.event_id_size]u8 = undefined; 2318 _ = std.fmt.hexToBytes(&comment_id, self.data.comment_id) catch return; 2319 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2320 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2321 } else { 2322 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2323 }, 2324 } 2325 }, 2326 } 2327 2328 body_input.clear(allocator); 2329 const route = ui.RoutablePage.repoCommentsRoute(self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2330 try self.session.navigate(route); 2331 } 2332 2333 // the terminal submit paths: the new form commits a new-issue event, the 2334 // edit form re-emits the selected issue's event with the form's content. 2335 // the web posts the forms to their routes instead. 2336 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 2337 if (self.data.view == .edit) { 2338 try self.submitEditedIssue(allocator); 2339 } else { 2340 try self.submitNewIssue(allocator); 2341 } 2342 } 2343 2344 // commit the new issue to the repo's events branch and navigate to it. 2345 // this is the terminal path; the web posts the form to the issue route, 2346 // so the wasm side never runs (or compiles) the repo access below. 2347 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 2348 if (comptime wasm) return; 2349 const io = self.session.io orelse return; 2350 const src = self.data.repo_source orelse return; 2351 const author = (try self.session.eventAuthor()) orelse return; 2352 2353 const form = self.issueForm() orelse return; 2354 const title_input = &form.children.values()[title_field_index].widget.text_input; 2355 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2356 const description_input = &form.children.values()[description_field_index].widget.text_input; 2357 2358 const title = try title_input.text(allocator); 2359 defer allocator.free(title); 2360 const tags = try tags_input.text(allocator); 2361 defer allocator.free(tags); 2362 const description = try description_input.text(allocator); 2363 defer allocator.free(description); 2364 2365 if (!evt.Issue.fieldsValid(title, tags)) return; 2366 2367 var id_bytes: [evt.event_id_size]u8 = undefined; 2368 io.random(&id_bytes); 2369 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2370 2371 const event = evt.EventWithId{ 2372 .id = event_id_hex, 2373 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2374 .author = author, 2375 .event = .{ .issue = .{ 2376 .title = title, 2377 .description = description, 2378 .tags = tags, 2379 } }, 2380 }; 2381 2382 switch (src.repo_kind) { 2383 inline else => |repo_kind| { 2384 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2385 defer any_repo.deinit(io, allocator); 2386 switch (any_repo) { 2387 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2388 } 2389 }, 2390 } 2391 2392 // wipe the form so a return visit starts fresh 2393 title_input.clear(allocator); 2394 tags_input.clear(allocator); 2395 description_input.clear(allocator); 2396 2397 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 2398 try self.session.navigate(route); 2399 } 2400 2401 // re-emit the selected issue's event with the form's content (status 2402 // preserved), then reload the issue's page. this is the terminal path; 2403 // the web posts the form to the edit route. 2404 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 2405 if (comptime wasm) return; 2406 const io = self.session.io orelse return; 2407 const src = self.data.repo_source orelse return; 2408 const entry = self.data.selectedIssue() orelse return; 2409 const author = (try self.session.eventAuthor()) orelse return; 2410 2411 const form = self.issueForm() orelse return; 2412 const title_input = &form.children.values()[title_field_index].widget.text_input; 2413 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2414 const description_input = &form.children.values()[description_field_index].widget.text_input; 2415 2416 const title = try title_input.text(allocator); 2417 defer allocator.free(title); 2418 const tags = try tags_input.text(allocator); 2419 defer allocator.free(tags); 2420 const description = try description_input.text(allocator); 2421 defer allocator.free(description); 2422 2423 if (!evt.Issue.fieldsValid(title, tags)) return; 2424 2425 var id_bytes: [evt.event_id_size]u8 = undefined; 2426 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2427 2428 switch (src.repo_kind) { 2429 inline else => |repo_kind| { 2430 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2431 defer any_repo.deinit(io, allocator); 2432 switch (any_repo) { 2433 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2434 .title = title, 2435 .tags = tags, 2436 .description = description, 2437 } }, author), 2438 } 2439 }, 2440 } 2441 2442 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 2443 try self.session.navigate(route); 2444 } 2445 2446 fn removeIssue(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2447 const sel = self.detailed_index[index] orelse return; 2448 const entry = self.window(index).issues[sel]; 2449 2450 var id_bytes: [evt.event_id_size]u8 = undefined; 2451 _ = std.fmt.hexToBytes(&id_bytes, entry.id) catch return; 2452 try self.removeEvent(allocator, .issue, id_bytes); 2453 } 2454 2455 fn removeComment(self: *View, allocator: std.mem.Allocator, comment: *Comment.Item) !void { 2456 try self.removeEvent(allocator, .comment, comment.id); 2457 } 2458 2459 fn removeEvent(self: *View, allocator: std.mem.Allocator, kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2460 if (comptime wasm) return; 2461 const io = self.session.io orelse return; 2462 const src = self.data.repo_source orelse return; 2463 const author = (try self.session.eventAuthor()) orelse return; 2464 2465 switch (src.repo_kind) { 2466 inline else => |repo_kind| { 2467 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2468 defer any_repo.deinit(io, allocator); 2469 switch (any_repo) { 2470 inline else => |*repo| try evt.remove(repo_kind, repo.self_repo_opts, io, allocator, repo, &id, kind, author), 2471 } 2472 }, 2473 } 2474 2475 const id_hex = std.fmt.bytesToHex(id, .lower); 2476 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, kind, &id_hex) orelse return; 2477 try self.session.navigate(route); 2478 } 2479 2480 // flip the shown issue's status by re-emitting its event, then reload the 2481 // page rooted at the issue so the view reflects the change. this is the 2482 // terminal path; the web posts the button's form to the status route. 2483 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 2484 if (comptime wasm) return; 2485 const io = self.session.io orelse return; 2486 const src = self.data.repo_source orelse return; 2487 const sel = self.detailed_index[index] orelse return; 2488 const entry = self.window(index).issues[sel]; 2489 const author = (try self.session.eventAuthor()) orelse return; 2490 2491 const status: evt.Issue.Status = switch (entry.issue.event.status) { 2492 .open => .closed, 2493 .closed => .open, 2494 }; 2495 2496 var id_bytes: [evt.event_id_size]u8 = undefined; 2497 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 2498 2499 switch (src.repo_kind) { 2500 inline else => |repo_kind| { 2501 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2502 defer any_repo.deinit(io, allocator); 2503 switch (any_repo) { 2504 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2505 } 2506 }, 2507 } 2508 2509 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 2510 try self.session.navigate(route); 2511 } 2512 2513 // arrow keys move the tag selection; at the flow's edges focus crosses to 2514 // the neighboring widgets. 2515 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 2516 const tf = self.tagFlow(index) orelse return; 2517 const cid = tf.focus.child_id orelse return; 2518 const cur = tf.indexOfFocusId(cid) orelse return; 2519 const count = tf.text_boxes.items.len; 2520 switch (key) { 2521 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 2522 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 2523 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2524 self.focusTag(index, tf, root_focus, i); 2525 } else { 2526 _ = self.moveDetailVertical(index, root_focus, false); 2527 }, 2528 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2529 self.focusTag(index, tf, root_focus, i); 2530 } else { 2531 _ = self.moveDetailVertical(index, root_focus, true); 2532 }, 2533 .home => self.focusTag(index, tf, root_focus, 0), 2534 .end => self.focusTag(index, tf, root_focus, count - 1), 2535 else => {}, 2536 } 2537 } 2538 2539 const title_child_index: usize = 0; 2540 const tags_child_index: usize = 2; 2541 2542 // the tool row's first child after the spacer that pushes it right 2543 const first_in_row_index: usize = 1; 2544 2545 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 2546 const inner = self.detailInner(index); 2547 if (inner.children.count() <= tags_child_index) return null; 2548 return switch (inner.children.values()[tags_child_index].widget) { 2549 .tag_flow => |*tf| tf, 2550 else => null, 2551 }; 2552 } 2553 2554 fn tagsFocused(self: *View, index: usize) bool { 2555 const inner = self.detailInner(index); 2556 const cid = inner.getFocus().child_id orelse return false; 2557 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 2558 } 2559 2560 fn toolRowFocused(self: *View, index: usize) bool { 2561 const outer = self.detailOuter(index); 2562 const cid = outer.getFocus().child_id orelse return false; 2563 return outer.children.getIndex(cid) == tool_row_index; 2564 } 2565 2566 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2567 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2568 // keep the tag visible on the terminal: its rect offset by the flow's 2569 // position in the pane. 2570 if (self.session.is_terminal and item < tf.rects.items.len) { 2571 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2572 var rect = tf.rects.items[item]; 2573 rect.x += flow_rect.x; 2574 rect.y += flow_rect.y; 2575 self.detailScroll(index).scrollToRect(rect); 2576 } 2577 } 2578 } 2579 2580 fn titleFocused(self: *View, index: usize) bool { 2581 const inner = self.detailInner(index); 2582 const cid = inner.getFocus().child_id orelse return false; 2583 return inner.children.getIndex(cid) == title_child_index; 2584 } 2585 2586 fn authorFocused(self: *View, index: usize) bool { 2587 const inner = self.detailInner(index); 2588 const cid = inner.getFocus().child_id orelse return false; 2589 return cid == self.author_id[index]; 2590 } 2591 2592 fn descriptionFocused(self: *View, index: usize) bool { 2593 const cid = self.detailInner(index).getFocus().child_id orelse return false; 2594 return cid == self.description_id[index]; 2595 } 2596 2597 // return to the tool row's last-focused button (the header when the 2598 // description page shows no row). 2599 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2600 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2601 root_focus.setFocus(cid); 2602 } 2603 2604 // enter the detail pane. an empty pane (no issues) can't be entered. 2605 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2606 if (self.detailInner(index).children.count() == 0) return; 2607 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2608 } 2609 2610 // return to the list. 2611 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2612 root_focus.setFocus(self.listScroll(index).getFocus().id); 2613 } 2614 2615 // cross to the header tabs above the stack. 2616 fn focusHeader(self: *View, root_focus: *Focus) void { 2617 root_focus.setFocus(self.box.children.keys()[header_index]); 2618 } 2619 2620 pub fn clearGrid(self: *View) void { 2621 self.box.clearGrid(); 2622 } 2623 2624 pub fn getGrid(self: View) ?Grid { 2625 return self.box.getGrid(); 2626 } 2627 2628 pub fn getFocus(self: *View) *Focus { 2629 return self.box.getFocus(); 2630 } 2631 2632 // for the parent's "scroll up at the top jumps to the header" check: at the 2633 // top only while the header tabs hold focus, so up from the split 2634 // crosses into the tabs first. 2635 pub fn getSelectedIndex(self: *View) ?usize { 2636 return if (self.headerActive()) 0 else 1; 2637 } 2638 }; 2639 2640 // the "a:" navigation link for `status`'s issues page filtered to the 2641 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2642 // a rooted url ignores `status` (it derives its view from the issue's own); 2643 // with an empty `id` this is the bare list link. 2644 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2645 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2646 const url = try route.toUrl(page_arena); 2647 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2648 } 2649 2650 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2651 // href is only followed with js off. 2652 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2653 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2654 const url = try route.toUrl(page_arena); 2655 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2656 } 2657 2658 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2659 // or the conflicts window when null, rooted at issue `id`. 2660 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2661 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2662 return conflictsLink(page_arena, data.identity, id); 2663 } 2664 2665 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2666 // first window). 2667 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2668 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2669 const url = try route.toUrl(page_arena); 2670 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2671 } 2672 2673 // the "a:" link to the whole-description page of issue `id` within `identity`. 2674 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2675 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2676 const url = try route.toUrl(page_arena); 2677 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2678 } 2679 2680 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2681 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2682 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2683 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2684 const url = try route.toUrl(page_arena); 2685 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2686 } 2687 2688 // tabs switching between the issues page's views. 2689 pub const Header = struct { 2690 box: wgt.Box(ui.Widget), 2691 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2692 2693 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2694 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2695 errdefer box.deinit(allocator); 2696 2697 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2698 errdefer tab_ids.deinit(allocator); 2699 2700 const aa = session.page_arena.allocator(); 2701 2702 // a list tab per status, labeled with its listing's issue count 2703 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2704 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2705 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2706 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2707 try addTab(allocator, &box, &tab_ids, label, link); 2708 } 2709 2710 // tags tab, labeled with the active tag filter 2711 { 2712 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2713 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2714 const label = if (data.tag.len == 0) "tags" else blk: { 2715 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2716 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2717 }; 2718 try addTab(allocator, &box, &tab_ids, label, tags_link); 2719 } 2720 2721 // new-issue tab; an edit or resolve url shows its tab in this place 2722 { 2723 const route = switch (data.view) { 2724 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2725 .new_comment => ui.RoutablePage.repoCommentNewRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2726 .edit_comment => ui.RoutablePage.repoCommentEditRoute(data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 2727 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2728 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2729 }; 2730 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2731 const label: []const u8 = switch (data.view) { 2732 .edit => "edit", 2733 .new_comment => "reply", 2734 .edit_comment => "edit", 2735 .resolve => "resolve", 2736 else => "new", 2737 }; 2738 try addTab(allocator, &box, &tab_ids, label, link); 2739 } 2740 2741 // conflicts tab, labeled with the conflict count 2742 if (data.conflicts.count > 0) { 2743 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2744 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2745 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2746 try addTab(allocator, &box, &tab_ids, label, link); 2747 } 2748 2749 var self = Header{ .box = box, .tab_ids = tab_ids }; 2750 // the tab matching the page's view is selected initially. 2751 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2752 return self; 2753 } 2754 2755 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2756 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2757 errdefer text_box.deinit(allocator); 2758 text_box.getFocus().focusable = true; 2759 text_box.getFocus().kind = .{ .custom = link }; 2760 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2761 try box.children.put(allocator, text_box.getFocus().id, .{ 2762 .widget = .{ .text_box = text_box }, 2763 .rect = null, 2764 .min_size = .{ .width = label.len + 2, .height = null }, 2765 }); 2766 } 2767 2768 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2769 self.box.deinit(allocator); 2770 self.tab_ids.deinit(allocator); 2771 } 2772 2773 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2774 self.clearGrid(); 2775 // only the selected tab shows its border 2776 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2777 switch (child.widget) { 2778 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2779 else => {}, 2780 } 2781 } 2782 try self.box.build(allocator, constraint, root_focus); 2783 } 2784 2785 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2786 _ = allocator; 2787 const current_tab = self.currentTabIndex() orelse return; 2788 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2789 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2790 } 2791 } 2792 2793 pub fn clearGrid(self: *Header) void { 2794 self.box.clearGrid(); 2795 } 2796 2797 pub fn getGrid(self: Header) ?Grid { 2798 return self.box.getGrid(); 2799 } 2800 2801 pub fn getFocus(self: *Header) *Focus { 2802 return self.box.getFocus(); 2803 } 2804 2805 pub fn getSelectedIndex(self: Header) ?usize { 2806 return self.currentTabIndex(); 2807 } 2808 2809 fn currentTabIndex(self: Header) ?usize { 2810 const child_id = self.box.focus.child_id orelse return null; 2811 return self.tab_ids.getIndex(child_id); 2812 } 2813 }; 2814