╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1892 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1893 errdefer tb.deinit(allocator); 1894 tb.getFocus().mode = .all; 1895 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1896 } 1897 1898 row.getFocus().child_id = row.children.keys()[0]; 1899 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1900 } 1901 1902 // the label of one side's version box, naming its author 1903 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1904 return switch (author) { 1905 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1906 .unknown => if (ours) " current version " else " their version ", 1907 }; 1908 } 1909 1910 // the a: link that reloads the resolve page with `name` picked from ours 1911 // or theirs, reprefilling its resolution input 1912 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1913 const aa = session.page_arena.allocator(); 1914 var picks: std.ArrayList(u8) = .empty; 1915 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1916 while (it.next()) |pick| { 1917 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1918 if (picks.items.len > 0) try picks.append(aa, ','); 1919 try picks.appendSlice(aa, pick); 1920 } 1921 if (theirs) { 1922 if (picks.items.len > 0) try picks.append(aa, ','); 1923 try picks.appendSlice(aa, name); 1924 } 1925 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1926 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1927 } 1928 1929 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1930 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1931 errdefer row.deinit(allocator); 1932 row.getFocus().mode = .all; 1933 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1934 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1935 } 1936 1937 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 1938 self.box.deinit(allocator); 1939 } 1940 1941 fn header(self: *This) *HeaderType { 1942 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 1943 } 1944 1945 fn viewStack(self: *This) *wgt.Stack(Widget) { 1946 return &self.box.children.values()[stack_index].widget.stack; 1947 } 1948 1949 // `index`'s master-detail split inside the stack. 1950 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 1951 return &self.viewStack().children.values()[index].box; 1952 } 1953 1954 // the tags view's flow inside the stack. 1955 fn tagsView(self: *This) *TagFlow { 1956 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1957 } 1958 1959 // the non-confirmation form inside the stack 1960 fn formBox(self: *This) ?*wgt.Box(Widget) { 1961 return switch (self.viewStack().children.values()[form_view_index]) { 1962 .box => |*box| box, 1963 // the resolve form sits inside a scroll on the terminal 1964 .scroll => |*scroll| &scroll.child.box, 1965 else => null, 1966 }; 1967 } 1968 1969 fn threadForm(self: *This) ?*wgt.Box(Widget) { 1970 return switch (self.data.view) { 1971 .new, .edit => self.formBox(), 1972 else => null, 1973 }; 1974 } 1975 1976 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 1977 return switch (self.viewStack().children.values()[form_view_index]) { 1978 .center => |*center| switch (center.child.*) { 1979 .box => |*box| box, 1980 else => null, 1981 }, 1982 else => null, 1983 }; 1984 } 1985 1986 // the resolve form's scroll on the terminal (the web page scrolls itself) 1987 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 1988 return switch (self.viewStack().children.values()[form_view_index]) { 1989 .scroll => |*scroll| scroll, 1990 else => null, 1991 }; 1992 } 1993 1994 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 1995 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1996 } 1997 1998 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 1999 return &self.listScroll(index).child.box; 2000 } 2001 2002 fn detail(self: *This, index: usize) *DetailType { 2003 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 2004 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 2005 } 2006 2007 fn window(self: *This, index: usize) *const Window { 2008 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2009 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2010 return self.data.window(splitStatus(index)); 2011 } 2012 2013 fn stackSelectedIndex(self: *This) ?usize { 2014 const stack = self.viewStack(); 2015 const cid = stack.getFocus().child_id orelse return null; 2016 return stack.children.getIndex(cid); 2017 } 2018 2019 // the stack's selected master-detail split (null when the tags view or the 2020 // new-thread form shows). 2021 fn selectedSplitIndex(self: *This) ?usize { 2022 const idx = self.stackSelectedIndex() orelse return null; 2023 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2024 } 2025 2026 fn detailActive(self: *This, index: usize) bool { 2027 const rb = self.resultsBox(index); 2028 const cid = rb.getFocus().child_id orelse return false; 2029 return rb.children.getIndex(cid) == detail_index; 2030 } 2031 2032 fn headerActive(self: *This) bool { 2033 const cid = self.box.getFocus().child_id orelse return false; 2034 return self.box.children.getIndex(cid) == header_index; 2035 } 2036 2037 fn tagsViewActive(self: *This) bool { 2038 if (self.headerActive()) return false; 2039 return self.stackSelectedIndex() == tags_view_index; 2040 } 2041 2042 fn formViewActive(self: *This) bool { 2043 if (self.headerActive()) return false; 2044 return self.stackSelectedIndex() == form_view_index; 2045 } 2046 2047 // the selected thread's index, or null when a window-navigation row is 2048 // selected (a leading "previous" row shifts the thread rows down by one). 2049 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2050 const lb = self.listBox(index); 2051 const cid = lb.getFocus().child_id orelse return null; 2052 const idx = lb.children.getIndex(cid) orelse return null; 2053 const win = self.window(index); 2054 const lead: usize = if (win.prev_id != null) 1 else 0; 2055 if (idx < lead or idx - lead >= win.items.len) return null; 2056 return idx - lead; 2057 } 2058 2059 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2060 self.clearGrid(); 2061 2062 // each header tab maps 1:1 to a stack child by position 2063 if (self.header().getSelectedIndex()) |index| { 2064 const stack = self.viewStack(); 2065 stack.getFocus().child_id = stack.children.keys()[index]; 2066 } 2067 2068 if (self.selectedSplitIndex()) |i| { 2069 // swap the detail pane to the selected thread when it changes. 2070 if (self.selectedThreadIndex(i)) |selected| { 2071 const entry = self.window(i).items[selected]; 2072 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2073 if (changed) { 2074 try self.detail(i).setEntry(allocator, entry); 2075 } 2076 } 2077 2078 // the selected list row shows a border (the focused TextBox 2079 // upgrades it to a double border itself); the rest stay borderless. 2080 const lb = self.listBox(i); 2081 for (lb.children.keys(), lb.children.values()) |id, *child| { 2082 switch (child.widget) { 2083 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 2084 else => {}, 2085 } 2086 } 2087 2088 // cap the list at list_max_width only while the detail pane fits 2089 // beside it. the box drops the detail when the width can't hold 2090 // both minimums, so when it's that narrow we lift the cap and let 2091 // the list fill the whole width. 2092 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2093 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2094 2095 // stretch the detail pane across the rest of the width so it fills 2096 // the area rather than shrinking to its content; its scroll fills 2097 // the pane. 2098 if (constraint.max_size.width) |w| { 2099 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2100 } else { 2101 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2102 } 2103 } 2104 2105 // refresh the form inputs' entries in the session's focus-id -> input 2106 // map with this frame's addresses, so the web/wasm form handling can 2107 // find them by focus id 2108 if (self.threadForm()) |form| { 2109 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2110 form.children.values()[title_field_index].widget.text_input.options.label = if (failure == .required_title) " title (required) " else " title "; 2111 if (comptime supports_drafts) { 2112 const invalid_target = failure == .invalid_target_branch; 2113 form.children.values()[target_branch_field_index].widget.text_input.options.label = if (invalid_target) " target branch (not found) " else " target branch "; 2114 } 2115 } 2116 if (self.formBox()) |form| { 2117 const inputs_arena = self.session.arena.allocator(); 2118 for (form.children.values()) |*child| switch (child.widget) { 2119 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2120 else => {}, 2121 }; 2122 } 2123 2124 try self.box.build(allocator, constraint, root_focus); 2125 } 2126 2127 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2128 // wheel input moves focus like the arrows 2129 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2130 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2131 else 2132 raw_key; 2133 2134 if (self.headerActive()) { 2135 // down from the tabs re-enters the stack if the selected view has 2136 // something to focus; other keys move the tabs. 2137 if (inp.vertDirection(key) == .down) { 2138 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2139 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2140 // nothing focusable in the unauthorized view 2141 .unauthorized => false, 2142 else => true, 2143 } else false; 2144 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2145 } else { 2146 try self.header().input(allocator, key, root_focus); 2147 } 2148 return; 2149 } 2150 if (self.tagsViewActive()) { 2151 self.tagsViewInput(key, root_focus); 2152 return; 2153 } 2154 if (self.formViewActive()) { 2155 if (confirmation(self.data.view) != null) { 2156 try self.confirmationFormInput(allocator, key, root_focus); 2157 } else if (supports_conflicts and self.data.view == .resolve) { 2158 try self.resolveInput(allocator, key, root_focus); 2159 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 2160 try self.commentFormInput(allocator, key, root_focus); 2161 } else { 2162 try self.formInput(allocator, key, root_focus); 2163 } 2164 return; 2165 } 2166 const i = self.selectedSplitIndex() orelse return; 2167 if (self.detailActive(i)) { 2168 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2169 .none => {}, 2170 .list => self.focusList(i, root_focus), 2171 .header => self.focusHeader(root_focus), 2172 } 2173 } else { 2174 self.listInput(i, key, root_focus); 2175 } 2176 } 2177 2178 // arrow keys move the tag selection; up from the top row crosses to the 2179 // header tabs. 2180 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2181 const tf = self.tagsView(); 2182 const cid = tf.focus.child_id orelse return; 2183 const cur = tf.indexOfFocusId(cid) orelse return; 2184 const count = tf.text_boxes.items.len; 2185 switch (key) { 2186 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2187 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2188 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2189 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2190 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2191 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2192 else => {}, 2193 } 2194 } 2195 2196 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2197 // up/down (and the scroll wheel) move the selection a row; page up/down 2198 // jump a fixed amount. right/Enter cross into the detail pane. up from 2199 // the top row crosses into the header tabs. 2200 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2201 const lb = self.listBox(index); 2202 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2203 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2204 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2205 return; 2206 } 2207 switch (key) { 2208 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2209 else => {}, 2210 } 2211 } 2212 2213 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2214 const form = self.threadForm() orelse return; 2215 const cid = form.getFocus().child_id orelse return; 2216 const cur = form.children.getIndex(cid) orelse return; 2217 2218 if (cur == description_field_index) { 2219 const child = &form.children.values()[cur]; 2220 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2221 return child.widget.input(allocator, key, root_focus); 2222 } 2223 2224 switch (key) { 2225 .arrow_up, .back_tab => if (cur > 0) 2226 root_focus.setFocus(form.children.keys()[cur - 1]) 2227 else 2228 self.focusHeader(root_focus), 2229 .arrow_down, .tab => if (cur < submit_field_index) { 2230 root_focus.setFocus(form.children.keys()[cur + 1]); 2231 }, 2232 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2233 .mouse => |mouse| if (cur == submit_field_index) { 2234 const submit = &form.children.values()[cur].widget.submit_button; 2235 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2236 }, 2237 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2238 } 2239 } 2240 2241 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2242 const form = self.formBox() orelse return; 2243 const cid = form.getFocus().child_id orelse return; 2244 const cur = form.children.getIndex(cid) orelse return; 2245 const child = &form.children.values()[cur]; 2246 2247 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2248 return child.widget.input(allocator, key, root_focus); 2249 2250 switch (key) { 2251 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2252 root_focus.setFocus(form.children.keys()[cur - 1]) 2253 else 2254 self.focusHeader(root_focus), 2255 .arrow_down, .tab => if (cur < comment_submit_field_index) 2256 root_focus.setFocus(form.children.keys()[cur + 1]), 2257 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2258 .mouse => |mouse| if (cur == comment_submit_field_index) { 2259 const submit = &child.widget.submit_button; 2260 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2261 }, 2262 else => try child.widget.input(allocator, key, root_focus), 2263 } 2264 } 2265 2266 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2267 const form = self.confirmationForm() orelse return; 2268 const child_id = form.getFocus().child_id orelse return; 2269 const index = form.children.getIndex(child_id) orelse return; 2270 switch (key) { 2271 .arrow_up, .back_tab => if (index > 1) 2272 root_focus.setFocus(form.children.keys()[index - 1]) 2273 else 2274 self.focusHeader(root_focus), 2275 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2276 .enter => try self.submitConfirmation(allocator, index), 2277 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2278 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2279 try self.submitConfirmation(allocator, button_index); 2280 break; 2281 } 2282 }, 2283 else => {}, 2284 } 2285 } 2286 2287 // the multiline hunk inputs keep enter and any up/down that has a row to 2288 // move to; enter on a "use this" link is a navigation the host follows 2289 // before this runs. 2290 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2291 const form = self.formBox() orelse return; 2292 const cid = form.getFocus().child_id orelse return; 2293 const cur = form.children.getIndex(cid) orelse return; 2294 const child = &form.children.values()[cur]; 2295 2296 if (child.widget == .text_input) { 2297 const ti = &child.widget.text_input; 2298 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2299 return child.widget.input(allocator, key, root_focus); 2300 } 2301 2302 const on_submit = child.widget == .submit_button; 2303 2304 switch (key) { 2305 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2306 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2307 .arrow_left, .arrow_right => switch (child.widget) { 2308 .box => |*row| { 2309 const row_cid = row.getFocus().child_id orelse return; 2310 const row_index = row.children.getIndex(row_cid) orelse return; 2311 if (key == .arrow_left) { 2312 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2313 } else if (row_index + 1 < row.children.count()) { 2314 root_focus.setFocus(row.children.keys()[row_index + 1]); 2315 } 2316 }, 2317 else => try child.widget.input(allocator, key, root_focus), 2318 }, 2319 .enter => if (on_submit) try self.submitResolution(allocator), 2320 .mouse => |mouse| if (on_submit) { 2321 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2322 }, 2323 else => try child.widget.input(allocator, key, root_focus), 2324 } 2325 } 2326 2327 // a multiline input keeps enter and any up/down that has a row to move to 2328 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2329 return switch (key) { 2330 .enter => true, 2331 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2332 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2333 else => false, 2334 }; 2335 } 2336 2337 // the neighboring focusable resolve-form child, skipping the spacer and 2338 // the blank gap rows 2339 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2340 var i = cur; 2341 while (true) { 2342 if (down) { 2343 i += 1; 2344 if (i >= form.children.count()) return null; 2345 } else { 2346 if (i == 0) return null; 2347 i -= 1; 2348 } 2349 switch (form.children.values()[i].widget) { 2350 .spacer, .text => continue, 2351 else => return i, 2352 } 2353 } 2354 } 2355 2356 // focus the resolve-form child at `i` (a version row focuses its selected 2357 // child) and keep it visible on the terminal 2358 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2359 const child = &form.children.values()[i]; 2360 switch (child.widget) { 2361 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2362 else => root_focus.setFocus(form.children.keys()[i]), 2363 } 2364 if (self.session.is_terminal) { 2365 if (self.resolveScroll()) |sc| { 2366 if (child.rect) |rect| sc.scrollToRect(rect); 2367 } 2368 } 2369 } 2370 2371 // re-emit the conflicted thread's event with the resolved content; any 2372 // event on the thread settles its conflict entry. this is the terminal 2373 // path; the web posts the form to the resolve route. 2374 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2375 if (comptime wasm) return; 2376 const io = self.session.io orelse return; 2377 const src = self.data.repo_source orelse return; 2378 const entry = self.data.selectedThread() orelse return; 2379 const author = (try self.session.eventAuthor()) orelse return; 2380 const form = self.formBox() orelse return; 2381 2382 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2383 var title: ?[]u8 = null; 2384 var tags: ?[]u8 = null; 2385 var hunks: std.ArrayList([]const u8) = .empty; 2386 defer { 2387 if (title) |t| allocator.free(t); 2388 if (tags) |t| allocator.free(t); 2389 for (hunks.items) |h| allocator.free(h); 2390 hunks.deinit(allocator); 2391 } 2392 for (form.children.values()) |*child| switch (child.widget) { 2393 .text_input => |*ti| { 2394 const text = try ti.text(allocator); 2395 errdefer allocator.free(text); 2396 if (std.mem.eql(u8, ti.options.name, "title")) { 2397 title = text; 2398 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2399 tags = text; 2400 } else { 2401 try hunks.append(allocator, text); 2402 } 2403 }, 2404 else => {}, 2405 }; 2406 2407 const id_bytes = try evt.parseEventId(entry.id); 2408 2409 switch (src.repo_kind) { 2410 inline else => |repo_kind| { 2411 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2412 defer any_repo.deinit(io, allocator); 2413 switch (any_repo) { 2414 inline else => |*repo| { 2415 var resolution: Event.Resolve = .{ 2416 .title = title, 2417 .tags = tags, 2418 .hunks = hunks.items, 2419 }; 2420 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2421 const change = Event.Update{ .resolve = resolution }; 2422 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2423 // leave the form up for correction 2424 error.InvalidFields => return, 2425 else => |e| return e, 2426 }; 2427 }, 2428 } 2429 }, 2430 } 2431 2432 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2433 try self.session.navigate(route); 2434 } 2435 2436 // commit a new or edited comment and navigate to its permalink. this is the 2437 // terminal path; the web posts the form to its comment route. 2438 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2439 if (comptime wasm) return; 2440 const io = self.session.io orelse return; 2441 const src = self.data.repo_source orelse return; 2442 const author = (try self.session.eventAuthor()) orelse return; 2443 const form = self.formBox() orelse return; 2444 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2445 const body = try body_input.text(allocator); 2446 defer allocator.free(body); 2447 if (!evt.Comment.fieldsValid(body)) return; 2448 2449 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2450 @memcpy(&thread_id, self.data.selected_id); 2451 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2452 var parent: [evt.event_id_size * 2]u8 = undefined; 2453 @memcpy(&parent, self.data.comment_id); 2454 break :blk parent; 2455 }; 2456 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2457 const editing = self.data.view == .edit_comment; 2458 switch (src.repo_kind) { 2459 inline else => |repo_kind| { 2460 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2461 defer any_repo.deinit(io, allocator); 2462 switch (any_repo) { 2463 inline else => |*repo| if (editing) { 2464 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2465 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2466 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2467 } else { 2468 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2469 }, 2470 } 2471 }, 2472 } 2473 2474 body_input.clear(allocator); 2475 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2476 try self.session.navigate(route); 2477 } 2478 2479 // the terminal submit paths: the new form commits a new thread event, the 2480 // edit form re-emits the selected thread's event with the form's content. 2481 // the web posts the forms to their routes instead. 2482 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2483 if (self.data.view == .edit) { 2484 try self.submitEditedThread(allocator); 2485 } else { 2486 try self.submitNewThread(allocator); 2487 } 2488 } 2489 2490 // commit the new thread to the repo's events branch and navigate to it. 2491 // this is the terminal path; the web posts the form to the thread route, 2492 // so the wasm side never runs (or compiles) the repo access below. 2493 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2494 if (comptime wasm) return; 2495 const io = self.session.io orelse return; 2496 const src = self.data.repo_source orelse return; 2497 const author = (try self.session.eventAuthor()) orelse return; 2498 2499 const form = self.threadForm() orelse return; 2500 const title_input = &form.children.values()[title_field_index].widget.text_input; 2501 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2502 const description_input = &form.children.values()[description_field_index].widget.text_input; 2503 2504 const title = try title_input.text(allocator); 2505 defer allocator.free(title); 2506 const tags = try tags_input.text(allocator); 2507 defer allocator.free(tags); 2508 const description = try description_input.text(allocator); 2509 defer allocator.free(description); 2510 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2511 defer if (comptime supports_drafts) allocator.free(target_branch); 2512 2513 if (!Event.fieldsValid(title, tags)) { 2514 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2515 return; 2516 } 2517 2518 if (comptime supports_drafts) { 2519 const event_id_hex = Data.createDraft(self.data, self.session, allocator, title, tags, description, target_branch) catch |err| switch (err) { 2520 error.InvalidFields => { 2521 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2522 return; 2523 }, 2524 else => |other| return other, 2525 }; 2526 title_input.clear(allocator); 2527 tags_input.clear(allocator); 2528 description_input.clear(allocator); 2529 form.children.values()[target_branch_field_index].widget.text_input.clear(allocator); 2530 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2531 try self.session.navigate(route); 2532 return; 2533 } 2534 2535 var id_bytes: [evt.event_id_size]u8 = undefined; 2536 io.random(&id_bytes); 2537 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2538 2539 const event = evt.EventWithId{ 2540 .id = event_id_hex, 2541 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2542 .author = author, 2543 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2544 .title = title, 2545 .description = description, 2546 .tags = tags, 2547 }), 2548 }; 2549 2550 switch (src.repo_kind) { 2551 inline else => |repo_kind| { 2552 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2553 defer any_repo.deinit(io, allocator); 2554 switch (any_repo) { 2555 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2556 } 2557 }, 2558 } 2559 2560 // wipe the form so a return visit starts fresh 2561 title_input.clear(allocator); 2562 tags_input.clear(allocator); 2563 description_input.clear(allocator); 2564 2565 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2566 try self.session.navigate(route); 2567 } 2568 2569 // re-emit the selected thread's event with the form's content (status 2570 // preserved), then reload the thread's page. this is the terminal path; 2571 // the web posts the form to the edit route. 2572 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2573 if (comptime wasm) return; 2574 const io = self.session.io orelse return; 2575 const entry = self.data.selectedThread() orelse return; 2576 2577 const form = self.threadForm() orelse return; 2578 const title_input = &form.children.values()[title_field_index].widget.text_input; 2579 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2580 const description_input = &form.children.values()[description_field_index].widget.text_input; 2581 2582 const title = try title_input.text(allocator); 2583 defer allocator.free(title); 2584 const tags = try tags_input.text(allocator); 2585 defer allocator.free(tags); 2586 const description = try description_input.text(allocator); 2587 defer allocator.free(description); 2588 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2589 defer if (comptime supports_drafts) allocator.free(target_branch); 2590 2591 if (!Event.fieldsValid(title, tags)) { 2592 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2593 return; 2594 } 2595 const src = self.data.repo_source orelse return; 2596 2597 if (comptime supports_drafts) { 2598 if (entryDraft(entry.*)) { 2599 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| switch (err) { 2600 error.InvalidFields => { 2601 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2602 return; 2603 }, 2604 else => |other| return other, 2605 }; 2606 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2607 try self.session.navigate(route); 2608 return; 2609 } 2610 } 2611 2612 const id_bytes = try evt.parseEventId(entry.id); 2613 const author = (try self.session.eventAuthor()) orelse return; 2614 2615 switch (src.repo_kind) { 2616 inline else => |repo_kind| { 2617 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2618 defer any_repo.deinit(io, allocator); 2619 switch (any_repo) { 2620 inline else => |*repo| if (supports_drafts) { 2621 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2622 .title = title, 2623 .tags = tags, 2624 .description = description, 2625 .target_branch = target_branch, 2626 } }, author) catch |err| switch (err) { 2627 error.InvalidFields => { 2628 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2629 return; 2630 }, 2631 else => |other| return other, 2632 }; 2633 } else if (has_status) 2634 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2635 .title = title, 2636 .tags = tags, 2637 .description = description, 2638 } }, author) 2639 else 2640 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2641 } 2642 }, 2643 } 2644 2645 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2646 try self.session.navigate(route); 2647 } 2648 2649 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2650 const aa = self.session.arena.allocator(); 2651 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2652 .failure = failure, 2653 .fields = if (comptime supports_drafts) .{ 2654 .title = try aa.dupe(u8, title), 2655 .tags = try aa.dupe(u8, tags), 2656 .description = try aa.dupe(u8, description), 2657 .target_branch = try aa.dupe(u8, target_branch), 2658 } else .{ 2659 .title = try aa.dupe(u8, title), 2660 .tags = try aa.dupe(u8, tags), 2661 .description = try aa.dupe(u8, description), 2662 }, 2663 }); 2664 } 2665 2666 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2667 switch (confirmation(self.data.view) orelse return) { 2668 .publish => if (comptime supports_drafts) { 2669 if (comptime wasm) return; 2670 const entry = self.data.selectedThread() orelse return; 2671 if (!entryDraft(entry.*)) return; 2672 try self.data.publishDraft(self.session, allocator, entry.id); 2673 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2674 try self.session.navigate(route); 2675 } else unreachable, 2676 .merge => if (comptime supports_merge) { 2677 if (comptime wasm) return; 2678 const entry = self.data.selectedThread() orelse return; 2679 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2680 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2681 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2682 try self.session.navigate(route); 2683 } else unreachable, 2684 .remove => try self.submitRemove(allocator), 2685 } 2686 } 2687 2688 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2689 const comment = self.data.comment_id.len != 0; 2690 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2691 const id = evt.parseEventId(event_id) catch return; 2692 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2693 } 2694 2695 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2696 if (comptime wasm) return; 2697 2698 if (comptime supports_drafts) { 2699 const selected = self.data.selectedThread() orelse return; 2700 if (event_kind == kind and entryDraft(selected.*)) { 2701 try Data.removeDraft(self.session, allocator, &id); 2702 const route = draftsRoute(self.data.identity) orelse return; 2703 try self.session.navigate(route); 2704 return; 2705 } 2706 } 2707 2708 const io = self.session.io orelse return; 2709 const src = self.data.repo_source orelse return; 2710 const author = (try self.session.eventAuthor()) orelse return; 2711 2712 switch (src.repo_kind) { 2713 inline else => |repo_kind| { 2714 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2715 defer any_repo.deinit(io, allocator); 2716 switch (any_repo) { 2717 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2718 } 2719 }, 2720 } 2721 2722 const id_hex = std.fmt.bytesToHex(id, .lower); 2723 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2724 try self.session.navigate(route); 2725 } 2726 2727 // return to the list. 2728 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2729 root_focus.setFocus(self.listScroll(index).getFocus().id); 2730 } 2731 2732 // cross to the header tabs above the stack. 2733 fn focusHeader(self: *This, root_focus: *Focus) void { 2734 root_focus.setFocus(self.box.children.keys()[header_index]); 2735 } 2736 2737 pub fn clearGrid(self: *This) void { 2738 self.box.clearGrid(); 2739 } 2740 2741 pub fn getGrid(self: This) ?Grid { 2742 return self.box.getGrid(); 2743 } 2744 2745 pub fn getFocus(self: *This) *Focus { 2746 return self.box.getFocus(); 2747 } 2748 2749 // for the parent's "scroll up at the top jumps to the header" check: at the 2750 // top only while the header tabs hold focus, so up from the split 2751 // crosses into the tabs first. 2752 pub fn atTop(self: *This) bool { 2753 return self.headerActive(); 2754 } 2755 }; 2756 } 2757