╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
948 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 949 if (focus_child.focus.mode != .all) continue; 950 const rect: layout.IRect = .{ 951 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 952 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 953 .size = focus_child.rect.size, 954 }; 955 if (!self.rectVisible(rect)) continue; 956 chosen = id; 957 if (!last) break; 958 } 959 if (chosen) |id| { 960 root_focus.setFocus(id); 961 return true; 962 } 963 return false; 964 } 965 966 fn focusAdjacentVisible(self: *This, root_focus: *Focus, down: bool) bool { 967 const inner_box = self.inner(); 968 var next = self.focusedChild(root_focus) orelse return false; 969 while (true) { 970 if (down) { 971 next += 1; 972 if (next >= inner_box.children.count()) return false; 973 } else { 974 if (next == 0) return false; 975 next -= 1; 976 } 977 if (self.focusVisibleChild(next, root_focus, !down)) { 978 self.focusChild(next, !down, root_focus); 979 return true; 980 } 981 } 982 } 983 984 fn focusedChild(self: *This, root_focus: *Focus) ?usize { 985 const inner_box = self.inner(); 986 var id = root_focus.grandchild_id orelse return null; 987 while (inner_box.getFocus().children.get(id)) |child| { 988 if (child.parent_id == inner_box.getFocus().id) return inner_box.children.getIndex(id); 989 id = child.parent_id; 990 } 991 return null; 992 } 993 994 fn childFocusable(self: *This, child_index: usize) bool { 995 const focus = self.inner().children.values()[child_index].widget.getFocus(); 996 if (focus.mode == .all) return true; 997 for (focus.children.values()) |item| { 998 if (item.focus.mode == .all) return true; 999 } 1000 return false; 1001 } 1002 1003 fn focusChild(self: *This, child_index: usize, last: bool, root_focus: *Focus) void { 1004 const inner_box = self.inner(); 1005 const child = &inner_box.children.values()[child_index]; 1006 var target = inner_box.children.keys()[child_index]; 1007 var rect = child.rect orelse return; 1008 switch (child.widget) { 1009 .repo_comment => |*comment| { 1010 if (comment.rowRect(last)) |row_rect| { 1011 rect.x += row_rect.x; 1012 rect.y += row_rect.y; 1013 rect.size = row_rect.size; 1014 } 1015 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1016 }, 1017 .box => |*box| if (box.children.count() > 0) { 1018 target = if (child_index == tool_row_index) 1019 box.getFocus().child_id orelse return 1020 else 1021 box.children.keys()[if (last) box.children.count() - 1 else 0]; 1022 root_focus.setFocus(target); 1023 }, 1024 else => root_focus.setFocus(target), 1025 } 1026 const detail_scroll = &self.scroll; 1027 if (detail_scroll.grid) |viewport| { 1028 const viewport_height = viewport.size.height - detail_scroll.bar_h; 1029 if (rect.size.height > viewport_height) { 1030 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1031 detail_scroll.y = std.math.clamp(detail_scroll.y, rect.y, bottom_aligned); 1032 detail_scroll.clampToContent(); 1033 return; 1034 } 1035 } 1036 detail_scroll.scrollToRect(rect); 1037 } 1038 1039 fn moveVertical(self: *This, root_focus: *Focus, down: bool) bool { 1040 const inner_box = self.inner(); 1041 const current = self.focusedChild(root_focus) orelse return false; 1042 if (inner_box.children.values()[current].widget == .repo_comment) { 1043 const comment = &inner_box.children.values()[current].widget.repo_comment; 1044 if (down and !comment.bodyFocused()) { 1045 self.focusChild(current, true, root_focus); 1046 return true; 1047 } 1048 if (!down and comment.bodyFocused()) { 1049 self.focusChild(current, false, root_focus); 1050 return true; 1051 } 1052 } 1053 1054 var next = current; 1055 while (true) { 1056 if (down) { 1057 next += 1; 1058 if (next >= inner_box.children.count()) return false; 1059 } else { 1060 if (next == 0) return false; 1061 next -= 1; 1062 } 1063 if (!self.childFocusable(next)) continue; 1064 self.focusChild(next, !down, root_focus); 1065 return true; 1066 } 1067 } 1068 1069 fn moveHorizontal(self: *This, root_focus: *Focus, right: bool) bool { 1070 const inner_box = self.inner(); 1071 const current = self.focusedChild(root_focus) orelse return false; 1072 const child = &inner_box.children.values()[current]; 1073 if (child.widget == .repo_comment) { 1074 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1075 if (moved) if (child.rect) |rect| self.scroll.scrollToRect(rect); 1076 return moved; 1077 } 1078 const row = switch (child.widget) { 1079 .box => |*box| box, 1080 else => return false, 1081 }; 1082 const selected = row.getFocus().child_id orelse return false; 1083 const selected_index = row.children.getIndex(selected) orelse return false; 1084 const target = if (right) selected_index + 1 else selected_index -| 1; 1085 if (target == selected_index or target >= row.children.count()) return false; 1086 root_focus.setFocus(row.children.keys()[target]); 1087 if (child.rect) |rect| self.scroll.scrollToRect(rect); 1088 return true; 1089 } 1090 1091 fn tagsInput(self: *This, child_index: usize, tags: *TagFlow, key: Key, root_focus: *Focus) void { 1092 const child_id = tags.focus.child_id orelse return; 1093 const cur = tags.indexOfFocusId(child_id) orelse return; 1094 const count = tags.text_boxes.items.len; 1095 switch (key) { 1096 .arrow_left => if (cur > 0) self.focusTag(child_index, tags, root_focus, cur - 1) else { 1097 self.exit = .list; 1098 }, 1099 .arrow_right => if (cur + 1 < count) self.focusTag(child_index, tags, root_focus, cur + 1), 1100 .arrow_up => if (tags.rowStep(cur, false)) |index| { 1101 self.focusTag(child_index, tags, root_focus, index); 1102 } else { 1103 _ = self.moveVertical(root_focus, false); 1104 }, 1105 .arrow_down => if (tags.rowStep(cur, true)) |index| { 1106 self.focusTag(child_index, tags, root_focus, index); 1107 } else { 1108 _ = self.moveVertical(root_focus, true); 1109 }, 1110 .home => self.focusTag(child_index, tags, root_focus, 0), 1111 .end => self.focusTag(child_index, tags, root_focus, count - 1), 1112 else => {}, 1113 } 1114 } 1115 1116 fn focusTag(self: *This, child_index: usize, tags: *TagFlow, root_focus: *Focus, item: usize) void { 1117 root_focus.setFocus(tags.text_boxes.items[item].getFocus().id); 1118 if (self.session.is_terminal and item < tags.rects.items.len) { 1119 if (self.inner().children.values()[child_index].rect) |flow_rect| { 1120 var rect = tags.rects.items[item]; 1121 rect.x += flow_rect.x; 1122 rect.y += flow_rect.y; 1123 self.scroll.scrollToRect(rect); 1124 } 1125 } 1126 } 1127 1128 fn toolRowFocused(self: *This, root_focus: *Focus) bool { 1129 return self.focusedChild(root_focus) == tool_row_index; 1130 } 1131 1132 fn focusToolRow(self: *This, root_focus: *Focus) void { 1133 const child_id = self.toolRow().getFocus().child_id orelse { 1134 self.exit = .header; 1135 return; 1136 }; 1137 root_focus.setFocus(child_id); 1138 if (self.inner().children.values()[tool_row_index].rect) |rect| self.scroll.scrollToRect(rect); 1139 } 1140 1141 pub fn focusFirst(self: *This, root_focus: ?*Focus) bool { 1142 const inner_box = self.inner(); 1143 for (tool_row_index + 1..inner_box.children.count()) |child_index| { 1144 if (!self.childFocusable(child_index)) continue; 1145 if (root_focus) |focus| 1146 self.focusChild(child_index, false, focus) 1147 else 1148 inner_box.getFocus().child_id = inner_box.children.keys()[child_index]; 1149 return true; 1150 } 1151 return false; 1152 } 1153 1154 pub fn atTop(self: *This, root_focus: *Focus) bool { 1155 const focused = self.focusedChild(root_focus) orelse return false; 1156 for (0..self.inner().children.count()) |child_index| { 1157 if (!self.childFocusable(child_index)) continue; 1158 return focused == child_index; 1159 } 1160 return false; 1161 } 1162 1163 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 1164 if (comptime wasm) return; 1165 1166 if (comptime supports_drafts) { 1167 const entry = self.entry orelse return; 1168 if (event_kind == kind and entryDraft(entry)) { 1169 try Data.removeDraft(self.session, allocator, &id); 1170 const route = draftsRoute(self.data.identity) orelse return; 1171 try self.session.navigate(route); 1172 return; 1173 } 1174 } 1175 1176 const io = self.session.io orelse return; 1177 const source = self.data.repo_source orelse return; 1178 const author = (try self.session.eventAuthor()) orelse return; 1179 1180 switch (source.repo_kind) { 1181 inline else => |repo_kind| { 1182 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1183 defer any_repo.deinit(io, allocator); 1184 switch (any_repo) { 1185 inline else => |*repo| try evt.remove(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 1186 } 1187 }, 1188 } 1189 1190 const id_hex = std.fmt.bytesToHex(id, .lower); 1191 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 1192 try self.session.navigate(route); 1193 } 1194 1195 fn toggleStatus(self: *This, allocator: std.mem.Allocator) !void { 1196 if (comptime (!has_status or wasm)) return; 1197 const entry = self.entry orelse return; 1198 const io = self.session.io orelse return; 1199 const source = self.data.repo_source orelse return; 1200 const author = (try self.session.eventAuthor()) orelse return; 1201 const status = (statusChange(entryStatus(entry)) orelse return).status; 1202 const id = try evt.parseEventId(entry.id); 1203 1204 switch (source.repo_kind) { 1205 inline else => |repo_kind| { 1206 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1207 defer any_repo.deinit(io, allocator); 1208 switch (any_repo) { 1209 inline else => |*repo| try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, .{ .status = status }, author), 1210 } 1211 }, 1212 } 1213 1214 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 1215 try self.session.navigate(route); 1216 } 1217 }; 1218 } 1219 1220 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 1221 const Self = Data; 1222 const HeaderType = Data.Header; 1223 const Window = Data.Window; 1224 const Entry = Data.Entry; 1225 const Event = Data.Event; 1226 const feedback_tag: std.meta.Tag(ui.Session.FormFeedback) = switch (kind) { 1227 .issue => .issue, 1228 .patch => .patch, 1229 .discuss => .discussion, 1230 else => unreachable, 1231 }; 1232 const FeedbackFailure = if (kind == .patch) ui.Session.FormFeedback.PatchFailure else ui.Session.FormFeedback.ThreadFailure; 1233 const Status = Data.Status; 1234 const has_status = @hasField(Event, "status"); 1235 const supports_conflicts = Event.merge_policy == .field_conflicts; 1236 const supports_drafts = @hasField(Self, "drafts"); 1237 const supports_merge = @hasDecl(Data, "mergeRoute"); 1238 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 1239 const ViewKind = Data.ViewKind; 1240 const thread_name = Data.thread_name; 1241 const DetailType = Data.Detail; 1242 1243 return struct { 1244 const This = @This(); 1245 // a vertical box: the header tabs on top, then a stack holding a 1246 // master-detail split (thread list + description pane) per status list, 1247 // plus the tags view. 1248 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 1249 data: *const Self, 1250 session: *ui.Session, 1251 1252 const header_index: usize = 0; 1253 const stack_index: usize = 1; 1254 // indices within the stack, 1:1 with the header tabs. 1255 const status_count = @typeInfo(Status).@"enum".fields.len; 1256 const tags_view_index: usize = status_count; 1257 // the new-thread form, or the edit, comment, or resolve form when the page was 1258 // loaded at one of their urls. 1259 const form_view_index: usize = tags_view_index + 1; 1260 // the conflicts split; the tab and stack child exist only when the repo 1261 // has conflicted threads. 1262 const drafts_view_index: usize = form_view_index + 1; 1263 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 1264 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 1265 // indices within a split (the horizontal box inside the stack). 1266 const list_index: usize = 0; 1267 const detail_index: usize = 1; 1268 const list_max_width: usize = 35; 1269 const detail_min_width: usize = 40; 1270 1271 pub fn viewIndex(view: ViewKind) usize { 1272 const name = @tagName(view); 1273 if (std.mem.eql(u8, name, "tags")) return tags_view_index; 1274 if (std.mem.eql(u8, name, "new") or std.mem.eql(u8, name, "edit") or std.mem.eql(u8, name, "publish") or std.mem.eql(u8, name, "merge") or std.mem.eql(u8, name, "new_comment") or std.mem.eql(u8, name, "edit_comment") or std.mem.eql(u8, name, "remove") or std.mem.eql(u8, name, "resolve")) return form_view_index; 1275 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 1276 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 1277 if (std.mem.eql(u8, name, "description")) unreachable; 1278 inline for (@typeInfo(Status).@"enum".fields, 0..) |field, index| { 1279 if (std.mem.eql(u8, name, field.name)) return index; 1280 } 1281 unreachable; 1282 } 1283 1284 const Confirmation = enum { remove, publish, merge }; 1285 1286 fn confirmation(view: ViewKind) ?Confirmation { 1287 if (view == .remove) return .remove; 1288 if (supports_drafts and std.mem.eql(u8, @tagName(view), "publish")) return .publish; 1289 if (supports_merge and std.mem.eql(u8, @tagName(view), "merge")) return .merge; 1290 return null; 1291 } 1292 1293 // the status a status split lists; the conflicts split has none (its 1294 // threads carry their own). 1295 fn splitStatus(index: usize) Status { 1296 return @enumFromInt(index); 1297 } 1298 1299 fn entryConflicted(entry: Entry) bool { 1300 return if (supports_conflicts) entry.conflicted else false; 1301 } 1302 1303 fn entryStatus(entry: Entry) Status { 1304 if (comptime has_status) return Data.statusKind(entry.record.event); 1305 return @enumFromInt(0); 1306 } 1307 1308 fn entryDraft(entry: Entry) bool { 1309 return if (supports_drafts) entry.draft else false; 1310 } 1311 1312 fn listRoute(identity: []const u8, status: Status, tag: []const u8, selected: []const u8) ?ui.RoutablePage { 1313 return Data.listRoute(identity, status, tag, selected); 1314 } 1315 1316 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 1317 return if (supports_drafts) Data.draftsRoute(identity) else null; 1318 } 1319 1320 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1321 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 1322 } 1323 1324 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1325 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 1326 } 1327 1328 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1329 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 1330 } 1331 1332 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 1333 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 1334 } 1335 1336 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8, id: []const u8) ![]const u8 { 1337 const route = listRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1338 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1339 } 1340 1341 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 1342 const selected = std.mem.eql(u8, id, data.selected_id); 1343 const route = (if (selected and data.description_page) 1344 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 1345 else if (selected) 1346 if (data.comment_page) |page| 1347 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 1348 else 1349 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 1350 else 1351 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 1352 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 1353 } 1354 1355 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 1356 if (supports_conflicts and status_maybe == null and !drafts) { 1357 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 1358 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1359 } 1360 if (drafts and id.len == 0) { 1361 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 1362 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1363 } 1364 return listLink(page_arena, data.identity, status_maybe orelse @enumFromInt(0), data.tag, id); 1365 } 1366 1367 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8) ![]const u8 { 1368 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1369 return listLink(page_arena, identity, status, encoded, ""); 1370 } 1371 1372 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 1373 var outer = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1374 errdefer outer.deinit(allocator); 1375 1376 // the tabs at the top. 1377 { 1378 var hdr = try Data.initHeader(allocator, session, data); 1379 errdefer hdr.deinit(allocator); 1380 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 1381 } 1382 1383 // the stack enters `outer` before its children enter it, so an error 1384 // frees each child exactly once. 1385 { 1386 var stack = try wgt.Stack(Widget).init(allocator); 1387 errdefer stack.deinit(allocator); 1388 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1389 } 1390 const stack = &outer.children.values()[stack_index].widget.stack; 1391 1392 inline for (@typeInfo(Status).@"enum".fields) |field| { 1393 var split = try initSplit(allocator, session, data, @enumFromInt(field.value), false); 1394 errdefer split.deinit(allocator); 1395 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1396 } 1397 1398 // the tags view 1399 { 1400 var tf = try TagFlow.init(allocator); 1401 errdefer tf.deinit(allocator); 1402 var items: std.ArrayList(TagFlow.Item) = .empty; 1403 defer items.deinit(allocator); 1404 // when filtered, the first item clears the filter 1405 if (data.tag.len != 0) 1406 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @enumFromInt(0), "", "") }); 1407 for (data.tags) |tag| 1408 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, @enumFromInt(0), tag) }); 1409 try tf.setItems(allocator, items.items); 1410 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 1411 } 1412 1413 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1414 // its place, prefilled with the selected thread's content. a 1415 // logged-out session can't create events, so the unauthorized view 1416 // stands in. 1417 if (session.data.host_kind == .local or session.data.user_id != null) { 1418 if (confirmation(data.view)) |confirmation_kind| { 1419 const aa = session.page_arena.allocator(); 1420 const route = switch (confirmation_kind) { 1421 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1422 .publish => publishRoute(data.identity, data.selected_id), 1423 .merge => mergeRoute(data.identity, data.selected_id), 1424 } orelse return error.RouteTooLong; 1425 const page_url = try route.toUrl(session.page_arena); 1426 const action = try std.fmt.allocPrint(aa, "form:{s}", .{page_url}); 1427 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1428 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1429 var buttons: [2]ConfirmationButton = undefined; 1430 buttons[0] = .{ .label = switch (confirmation_kind) { 1431 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1432 .publish => "publish patch", 1433 .merge => "merge patch", 1434 } }; 1435 var button_count: usize = 1; 1436 if (confirmation_kind == .merge) { 1437 if (comptime supports_merge) { 1438 const availability = if (data.selectedThread()) |entry| 1439 if (Data.canMerge(entry.*, session)) entry.mergeability else pch.Mergeability{} 1440 else 1441 pch.Mergeability{}; 1442 buttons[0].bottom_label = mergeBottomLabel(availability.source); 1443 if (availability.source != .clean) buttons[0].action = ""; 1444 buttons[1] = .{ 1445 .label = "squash and merge patch", 1446 .bottom_label = mergeBottomLabel(availability.squash), 1447 .action = if (availability.squash == .clean) try std.fmt.allocPrint(aa, "{s}{s}/squash", .{ ui.submit_action_prefix, page_url }) else "", 1448 }; 1449 button_count += 1; 1450 } else unreachable; 1451 } 1452 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1453 errdefer center.deinit(allocator); 1454 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1455 } else if (data.view == .new_comment or data.view == .edit_comment) { 1456 const editing = data.view == .edit_comment; 1457 const route = if (editing) 1458 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1459 else 1460 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1461 const page_url = try route.toUrl(session.page_arena); 1462 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 1463 const page = data.comment_page; 1464 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1465 const parent_route = if (editing) 1466 if (top_level) 1467 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1468 else if (page) |p| 1469 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1470 else 1471 null 1472 else if (page) |p| 1473 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1474 else 1475 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 1476 const parent_author: ui.Author = if (editing) 1477 if (top_level) 1478 if (data.selectedThread()) |entry| entry.author else .unknown 1479 else if (page) |p| 1480 p.selected.parent_author orelse .unknown 1481 else 1482 .unknown 1483 else if (page) |p| 1484 p.selected.author 1485 else if (data.selectedThread()) |entry| 1486 entry.author 1487 else 1488 .unknown; 1489 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 1490 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 1491 errdefer form.deinit(allocator); 1492 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1493 } else if (supports_conflicts and data.view == .resolve) { 1494 // on the web the page grows to the form's height and the 1495 // browser scrolls it; the terminal scrolls the form itself 1496 var form_widget: Widget = blk: { 1497 var form = try initResolveForm(allocator, session, data); 1498 errdefer form.deinit(allocator); 1499 break :blk if (session.is_terminal) 1500 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 1501 else 1502 .{ .box = form }; 1503 }; 1504 errdefer form_widget.deinit(allocator); 1505 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 1506 } else { 1507 const aa = session.page_arena.allocator(); 1508 const route = if (data.view == .edit) 1509 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 1510 else 1511 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 1512 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 1513 const record = if (data.view == .edit) 1514 (if (data.selectedThread()) |entry| &entry.record else null) 1515 else 1516 null; 1517 var form = try initThreadForm(allocator, session, data, action, record); 1518 errdefer form.deinit(allocator); 1519 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1520 } 1521 } else { 1522 var message = try ui.Unauthorized.View.init(allocator); 1523 errdefer message.deinit(allocator); 1524 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 1525 } 1526 1527 if (supports_drafts) { 1528 var split = try initSplit(allocator, session, data, null, true); 1529 errdefer split.deinit(allocator); 1530 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1531 } 1532 1533 // the conflicts split; its tab exists only when the repo has 1534 // conflicts, so skip the child too to keep the stack 1:1 with the 1535 // tabs. 1536 if (supports_conflicts and data.conflicts.count > 0) { 1537 var split = try initSplit(allocator, session, data, null, false); 1538 errdefer split.deinit(allocator); 1539 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1540 } 1541 1542 // the stack starts on the page's view. 1543 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 1544 1545 // focus entering the view lands on the tabs first. 1546 outer.getFocus().child_id = outer.children.keys()[header_index]; 1547 1548 return .{ 1549 .box = outer, 1550 .data = data, 1551 .session = session, 1552 }; 1553 } 1554 1555 // the master-detail split showing `status`'s window, or the conflicts 1556 // window when null. 1557 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 1558 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1559 errdefer box.deinit(allocator); 1560 1561 const win = if (status_maybe) |status| data.window(status) else if (drafts and supports_drafts) &data.drafts else if (supports_conflicts) &data.conflicts else unreachable; 1562 1563 // the thread list (one focusable row per title), plus a "next" link that 1564 // reloads the page rooted at the following thread. 1565 { 1566 var list_scroll = blk: { 1567 var list_box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 1568 errdefer list_box.deinit(allocator); 1569 if (win.prev_id) |prev| 1570 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 1571 for (win.items) |entry| { 1572 const label: []const u8 = if (entry.comments.count > 0) 1573 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 1574 else 1575 ""; 1576 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 1577 } 1578 if (win.next_id) |next| 1579 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 1580 // select the window's first thread (past a leading "previous" 1581 // row) so its description shows on load. 1582 if (win.items.len > 0) 1583 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 1584 else if (list_box.children.count() > 0) 1585 list_box.getFocus().child_id = list_box.children.keys()[0]; 1586 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 1587 }; 1588 errdefer list_scroll.deinit(allocator); 1589 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 1590 } 1591 1592 // frame the detail pane inside the master-detail split 1593 { 1594 var frame = try wgt.Box(Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 1595 errdefer frame.deinit(allocator); 1596 frame.getFocus().mode = .mouse; 1597 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 1598 errdefer detail_view.deinit(allocator); 1599 frame.getFocus().child_id = detail_view.getFocus().id; 1600 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 1601 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 1602 } 1603 1604 box.getFocus().child_id = box.children.keys()[list_index]; 1605 return box; 1606 } 1607 1608 // a thread form, plus the target branch for patches, prefilled from 1609 // `record` when given. its form: subtree makes the web renderer POST 1610 // the inputs to `action`'s route. 1611 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 1612 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1613 errdefer box.deinit(allocator); 1614 box.getFocus().kind = .{ .custom = action }; 1615 1616 const saved_form = session.formFeedback(feedback_tag); 1617 const saved_fields = if (saved_form) |saved| saved.fields else null; 1618 1619 { 1620 var title = try wgt.TextInput.init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1621 errdefer title.deinit(allocator); 1622 title.getFocus().mode = .all; 1623 if (saved_fields) |saved| 1624 try title.setContent(allocator, saved.title) 1625 else if (record) |r| 1626 try title.setContent(allocator, r.event.title); 1627 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1628 box.getFocus().child_id = title.getFocus().id; 1629 } 1630 1631 { 1632 var tags = try wgt.TextInput.init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1633 errdefer tags.deinit(allocator); 1634 tags.getFocus().mode = .all; 1635 if (saved_fields) |saved| 1636 try tags.setContent(allocator, saved.tags) 1637 else if (record) |r| 1638 try tags.setContent(allocator, r.event.tags); 1639 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1640 } 1641 1642 { 1643 var description = try wgt.TextInput.init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 1644 errdefer description.deinit(allocator); 1645 description.getFocus().mode = .all; 1646 if (saved_fields) |saved| 1647 try description.setContent(allocator, saved.description) 1648 else if (record) |r| 1649 try description.setContent(allocator, r.event.description); 1650 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 1651 } 1652 1653 if (supports_drafts and record == null) { 1654 var source = try Data.Source.init(allocator, session, if (saved_fields) |saved| saved.source_branch else "", if (saved_fields) |saved| saved.source_kind else .fork); 1655 errdefer source.deinit(allocator); 1656 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .patch_source = source }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1657 } 1658 1659 if (supports_drafts) { 1660 var target = try wgt.TextInput.init(allocator, .{ .label = " target branch ", .name = "target_branch", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1661 errdefer target.deinit(allocator); 1662 target.getFocus().mode = .all; 1663 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 1664 try target.setContent(allocator, content); 1665 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1666 } 1667 1668 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 1669 1670 return box; 1671 } 1672 1673 fn initCommentForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, author: ui.Author, parent_route: ui.RoutablePage, initial_body: ?[]const u8) !wgt.Box(Widget) { 1674 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1675 errdefer box.deinit(allocator); 1676 box.getFocus().kind = .{ .custom = action }; 1677 1678 { 1679 var parent = try ui.authorBox(allocator, session.page_arena, author); 1680 errdefer parent.deinit(allocator); 1681 parent.options.label = " replying to "; 1682 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 1683 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1684 box.getFocus().child_id = parent.getFocus().id; 1685 } 1686 1687 { 1688 var body = try wgt.TextInput.init(allocator, .{ 1689 .label = " comment ", 1690 .name = "body", 1691 .visible_width = null, 1692 .rounded_corners = true, 1693 .render_content = session.is_terminal, 1694 .multiline = true, 1695 .visible_height = 5, 1696 .scroll = .{ .fill = true }, 1697 }); 1698 errdefer body.deinit(allocator); 1699 body.getFocus().mode = .all; 1700 if (initial_body) |text| try body.setContent(allocator, text); 1701 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 1702 } 1703 1704 try addSubmitButtonLabeled(allocator, &box, "submit"); 1705 return box; 1706 } 1707 1708 const ConfirmationButton = struct { 1709 label: []const u8, 1710 bottom_label: []const u8 = "", 1711 action: []const u8 = "submit", 1712 }; 1713 1714 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 1715 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 1716 errdefer box.deinit(allocator); 1717 box.getFocus().kind = .{ .custom = action }; 1718 1719 var prompt = try wgt.Text.init(allocator, "are you sure?"); 1720 errdefer prompt.deinit(allocator); 1721 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 1722 1723 for (buttons, 0..) |options, index| { 1724 var button = try wgt.TextBox.init(allocator, options.label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .bottom_label = options.bottom_label }); 1725 errdefer button.deinit(allocator); 1726 button.getFocus().mode = .all; 1727 button.getFocus().kind = .{ .custom = options.action }; 1728 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 1729 if (index == 0) box.getFocus().child_id = button.getFocus().id; 1730 } 1731 1732 return Center.init(allocator, .{ .box = box }); 1733 } 1734 1735 // a form's submit button, then a spacer absorbing the leftover 1736 // min-height the box hands its last child, so the button keeps its 1737 // natural height 1738 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 1739 { 1740 var submit = try SubmitButton.initLabeled(allocator, label); 1741 errdefer submit.deinit(allocator); 1742 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1743 } 1744 { 1745 var spacer = try Spacer.init(allocator); 1746 errdefer spacer.deinit(allocator); 1747 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1748 } 1749 } 1750 1751 // the conflict resolve form. the "use this" links are navigations that 1752 // flip the url's theirs: pick, reloading the prefills; one submit 1753 // settles every conflict. the form: subtree makes the web renderer POST 1754 // to the resolve route. 1755 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 1756 const aa = session.page_arena.allocator(); 1757 // init only picks the resolve view once it has read the conflict 1758 const conflict = if (data.conflict) |*c| c else unreachable; 1759 1760 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1761 errdefer box.deinit(allocator); 1762 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 1763 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 1764 1765 if (conflict.title) |*fc| { 1766 try addLabel(allocator, &box, "title conflict:"); 1767 try addFieldConflict(allocator, &box, session, data, "title", fc); 1768 } 1769 if (conflict.tags) |*fc| { 1770 try addLabel(allocator, &box, "tags conflict:"); 1771 try addFieldConflict(allocator, &box, session, data, "tags", fc); 1772 } 1773 if (comptime @hasField(Data.Conflict, "status")) { 1774 if (conflict.status) |*fc| { 1775 try addLabel(allocator, &box, "status conflict:"); 1776 try addAtomicConflict(allocator, &box, session, data, "status", fc); 1777 } 1778 } 1779 if (comptime @hasField(Data.Conflict, "revision")) { 1780 if (conflict.revision) |*fc| { 1781 try addLabel(allocator, &box, "revision conflict:"); 1782 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 1783 } 1784 if (conflict.target_branch) |*fc| { 1785 try addLabel(allocator, &box, "target branch conflict:"); 1786 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 1787 } 1788 } 1789 1790 if (conflict.description) |*desc| { 1791 try addLabel(allocator, &box, "description conflict:"); 1792 var hunk_index: usize = 0; 1793 for (desc.chunks, 0..) |*chunk, chunk_index| { 1794 switch (chunk.*) { 1795 .same => |text| { 1796 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 1797 errdefer tb.deinit(allocator); 1798 tb.getFocus().mode = .all; 1799 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1800 }, 1801 // an auto-resolved chunk: only one side changed it, so its 1802 // version stands, shown distinctly with its author 1803 .auto => |auto| { 1804 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 1805 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 1806 const label = switch (author) { 1807 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 1808 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 1809 }; 1810 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1811 errdefer tb.deinit(allocator); 1812 tb.getFocus().mode = .all; 1813 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1814 }, 1815 .conflict => |hunk| { 1816 // a blank row on each side sets the group apart from 1817 // the flowing shared and auto-resolved chunks 1818 if (chunk_index > 0) try addGap(allocator, &box); 1819 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 1820 hunk_index += 1; 1821 const picked_theirs = data.theirsPicked(name); 1822 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 1823 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 1824 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 1825 errdefer resolution_input.deinit(allocator); 1826 resolution_input.getFocus().mode = .all; 1827 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 1828 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1829 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 1830 }, 1831 } 1832 } 1833 } 1834 1835 try addSubmitButtonLabeled(allocator, &box, "submit"); 1836 1837 box.getFocus().child_id = box.children.keys()[0]; 1838 return box; 1839 } 1840 1841 // one conflicted scalar field: both sides' version rows, then the 1842 // resolution input prefilled from the picked side. 1843 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1844 const aa = session.page_arena.allocator(); 1845 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1846 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1847 1848 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1849 errdefer resolution_input.deinit(allocator); 1850 resolution_input.getFocus().mode = .all; 1851 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 1852 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1853 } 1854 1855 // atomic fields can only pick one complete side 1856 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1857 const aa = session.page_arena.allocator(); 1858 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1859 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1860 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 1861 .border_style = .single, 1862 .rounded_corners = true, 1863 .wrap_kind = .word, 1864 .label = " resolution ", 1865 }); 1866 errdefer selected.deinit(allocator); 1867 selected.getFocus().mode = .all; 1868 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 1869 } 1870 1871 // a blank row setting a conflict group apart from its neighbors 1872 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 1873 var text = try wgt.Text.init(allocator, " "); 1874 errdefer text.deinit(allocator); 1875 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1876 } 1877 1878 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 1879 var label = try SectionLabel.init(allocator, content); 1880 errdefer label.deinit(allocator); 1881 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1882 } 1883 1884 // one side of a conflict: the "use this" link on the left, the version 1885 // itself as a labeled read-only box. 1886 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1887 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1888 errdefer row.deinit(allocator); 1889 1890 { 1891 const use_label = "use this"; 1892 var use = try wgt.TextBox.init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1893 errdefer use.deinit(allocator); 1894 use.getFocus().mode = .all; 1895 use.getFocus().kind = .{ .custom = link }; 1896 try row.children.put(allocator, use.getFocus().id, .{ 1897 .widget = .{ .text_box = use }, 1898 .rect = null, 1899 .min_size = .{ .width = use_label.len + 2, .height = null }, 1900 .max_size = .{ .width = use_label.len + 2, .height = null }, 1901 }); 1902 } 1903 1904 { 1905 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 }); 1906 errdefer tb.deinit(allocator); 1907 tb.getFocus().mode = .all; 1908 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1909 } 1910 1911 row.getFocus().child_id = row.children.keys()[0]; 1912 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1913 } 1914 1915 // the label of one side's version box, naming its author 1916 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1917 return switch (author) { 1918 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1919 .unknown => if (ours) " current version " else " their version ", 1920 }; 1921 } 1922 1923 // the a: link that reloads the resolve page with `name` picked from ours 1924 // or theirs, reprefilling its resolution input 1925 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1926 const aa = session.page_arena.allocator(); 1927 var picks: std.ArrayList(u8) = .empty; 1928 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1929 while (it.next()) |pick| { 1930 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1931 if (picks.items.len > 0) try picks.append(aa, ','); 1932 try picks.appendSlice(aa, pick); 1933 } 1934 if (theirs) { 1935 if (picks.items.len > 0) try picks.append(aa, ','); 1936 try picks.appendSlice(aa, name); 1937 } 1938 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1939 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1940 } 1941 1942 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1943 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1944 errdefer row.deinit(allocator); 1945 row.getFocus().mode = .all; 1946 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1947 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1948 } 1949 1950 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 1951 self.box.deinit(allocator); 1952 } 1953 1954 fn header(self: *This) *HeaderType { 1955 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 1956 } 1957 1958 fn viewStack(self: *This) *wgt.Stack(Widget) { 1959 return &self.box.children.values()[stack_index].widget.stack; 1960 } 1961 1962 // `index`'s master-detail split inside the stack. 1963 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 1964 return &self.viewStack().children.values()[index].box; 1965 } 1966 1967 // the tags view's flow inside the stack. 1968 fn tagsView(self: *This) *TagFlow { 1969 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1970 } 1971 1972 // the non-confirmation form inside the stack 1973 fn formBox(self: *This) ?*wgt.Box(Widget) { 1974 return switch (self.viewStack().children.values()[form_view_index]) { 1975 .box => |*box| box, 1976 // the resolve form sits inside a scroll on the terminal 1977 .scroll => |*scroll| &scroll.child.box, 1978 else => null, 1979 }; 1980 } 1981 1982 fn threadForm(self: *This) ?*wgt.Box(Widget) { 1983 if (supports_conflicts and self.data.view == .resolve) return null; 1984 return switch (self.data.view) { 1985 .new_comment, .edit_comment => null, 1986 else => self.formBox(), 1987 }; 1988 } 1989 1990 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 1991 for (form.children.values()) |*child| switch (child.widget) { 1992 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 1993 else => {}, 1994 }; 1995 return error.MissingFormField; 1996 } 1997 1998 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 1999 for (form.children.values()) |*child| { 2000 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2001 } 2002 return null; 2003 } 2004 2005 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2006 return switch (self.viewStack().children.values()[form_view_index]) { 2007 .center => |*center| switch (center.child.*) { 2008 .box => |*box| box, 2009 else => null, 2010 }, 2011 else => null, 2012 }; 2013 } 2014 2015 // the resolve form's scroll on the terminal (the web page scrolls itself) 2016 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2017 return switch (self.viewStack().children.values()[form_view_index]) { 2018 .scroll => |*scroll| scroll, 2019 else => null, 2020 }; 2021 } 2022 2023 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2024 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2025 } 2026 2027 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2028 return &self.listScroll(index).child.box; 2029 } 2030 2031 fn detail(self: *This, index: usize) *DetailType { 2032 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 2033 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 2034 } 2035 2036 fn window(self: *This, index: usize) *const Window { 2037 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2038 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2039 return self.data.window(splitStatus(index)); 2040 } 2041 2042 fn stackSelectedIndex(self: *This) ?usize { 2043 const stack = self.viewStack(); 2044 const cid = stack.getFocus().child_id orelse return null; 2045 return stack.children.getIndex(cid); 2046 } 2047 2048 // the stack's selected master-detail split (null when the tags view or the 2049 // new-thread form shows). 2050 fn selectedSplitIndex(self: *This) ?usize { 2051 const idx = self.stackSelectedIndex() orelse return null; 2052 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2053 } 2054 2055 fn detailActive(self: *This, index: usize) bool { 2056 const rb = self.resultsBox(index); 2057 const cid = rb.getFocus().child_id orelse return false; 2058 return rb.children.getIndex(cid) == detail_index; 2059 } 2060 2061 fn headerActive(self: *This) bool { 2062 const cid = self.box.getFocus().child_id orelse return false; 2063 return self.box.children.getIndex(cid) == header_index; 2064 } 2065 2066 fn tagsViewActive(self: *This) bool { 2067 if (self.headerActive()) return false; 2068 return self.stackSelectedIndex() == tags_view_index; 2069 } 2070 2071 fn formViewActive(self: *This) bool { 2072 if (self.headerActive()) return false; 2073 return self.stackSelectedIndex() == form_view_index; 2074 } 2075 2076 // the selected thread's index, or null when a window-navigation row is 2077 // selected (a leading "previous" row shifts the thread rows down by one). 2078 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2079 const lb = self.listBox(index); 2080 const cid = lb.getFocus().child_id orelse return null; 2081 const idx = lb.children.getIndex(cid) orelse return null; 2082 const win = self.window(index); 2083 const lead: usize = if (win.prev_id != null) 1 else 0; 2084 if (idx < lead or idx - lead >= win.items.len) return null; 2085 return idx - lead; 2086 } 2087 2088 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2089 self.clearGrid(); 2090 2091 // each header tab maps 1:1 to a stack child by position 2092 if (self.header().getSelectedIndex()) |index| { 2093 const stack = self.viewStack(); 2094 stack.getFocus().child_id = stack.children.keys()[index]; 2095 } 2096 2097 if (self.selectedSplitIndex()) |i| { 2098 // swap the detail pane to the selected thread when it changes. 2099 if (self.selectedThreadIndex(i)) |selected| { 2100 const entry = self.window(i).items[selected]; 2101 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2102 if (changed) { 2103 try self.detail(i).setEntry(allocator, entry); 2104 } 2105 } 2106 2107 // the selected list row shows a border (the focused TextBox 2108 // upgrades it to a double border itself); the rest stay borderless. 2109 const lb = self.listBox(i); 2110 for (lb.children.keys(), lb.children.values()) |id, *child| { 2111 switch (child.widget) { 2112 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 2113 else => {}, 2114 } 2115 } 2116 2117 // cap the list at list_max_width only while the detail pane fits 2118 // beside it. the box drops the detail when the width can't hold 2119 // both minimums, so when it's that narrow we lift the cap and let 2120 // the list fill the whole width. 2121 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2122 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2123 2124 // stretch the detail pane across the rest of the width so it fills 2125 // the area rather than shrinking to its content; its scroll fills 2126 // the pane. 2127 if (constraint.max_size.width) |w| { 2128 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2129 } else { 2130 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2131 } 2132 } 2133 2134 // refresh the form inputs' entries in the session's focus-id -> input 2135 // map with this frame's addresses, so the web/wasm form handling can 2136 // find them by focus id 2137 if (self.threadForm()) |form| { 2138 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2139 (try formField(form, "title")).options.label = if (failure == .required_title) " title (required) " else " title "; 2140 if (comptime supports_drafts) { 2141 if (formWidget(form, .patch_source)) |source| { 2142 source.field().options.label = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2143 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2144 try submit.setLabel(allocator, switch (source.selected) { 2145 .fork => "submit draft", 2146 .branch => "submit patch", 2147 }); 2148 } 2149 (try formField(form, "target_branch")).options.label = 2150 if (failure == .invalid_target_branch) " target branch (invalid) " else if (self.data.view == .edit and failure == .same_branch) " target branch (same as source) " else if (self.data.view == .edit and failure == .unrelated_branches) " target branch (unrelated to source) " else if (self.data.view == .edit and failure == .invalid_source_branch) " target branch (source branch not found) " else " target branch "; 2151 } 2152 } 2153 if (self.formBox()) |form| { 2154 const inputs_arena = self.session.arena.allocator(); 2155 for (form.children.values()) |*child| switch (child.widget) { 2156 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2157 else => {}, 2158 }; 2159 } 2160 2161 try self.box.build(allocator, constraint, root_focus); 2162 } 2163 2164 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2165 // wheel input moves focus like the arrows 2166 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2167 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2168 else 2169 raw_key; 2170 2171 if (self.headerActive()) { 2172 // down from the tabs re-enters the stack if the selected view has 2173 // something to focus; other keys move the tabs. 2174 if (inp.vertDirection(key) == .down) { 2175 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2176 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2177 // nothing focusable in the unauthorized view 2178 .unauthorized => false, 2179 else => true, 2180 } else false; 2181 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2182 } else { 2183 try self.header().input(allocator, key, root_focus); 2184 } 2185 return; 2186 } 2187 if (self.tagsViewActive()) { 2188 self.tagsViewInput(key, root_focus); 2189 return; 2190 } 2191 if (self.formViewActive()) { 2192 if (confirmation(self.data.view) != null) { 2193 try self.confirmationFormInput(allocator, key, root_focus); 2194 } else if (supports_conflicts and self.data.view == .resolve) { 2195 try self.resolveInput(allocator, key, root_focus); 2196 } else { 2197 try self.formInput(allocator, key, root_focus); 2198 } 2199 return; 2200 } 2201 const i = self.selectedSplitIndex() orelse return; 2202 if (self.detailActive(i)) { 2203 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2204 .none => {}, 2205 .list => self.focusList(i, root_focus), 2206 .header => self.focusHeader(root_focus), 2207 } 2208 } else { 2209 self.listInput(i, key, root_focus); 2210 } 2211 } 2212 2213 // arrow keys move the tag selection; up from the top row crosses to the 2214 // header tabs. 2215 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2216 const tf = self.tagsView(); 2217 const cid = tf.focus.child_id orelse return; 2218 const cur = tf.indexOfFocusId(cid) orelse return; 2219 const count = tf.text_boxes.items.len; 2220 switch (key) { 2221 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2222 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2223 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2224 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2225 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2226 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2227 else => {}, 2228 } 2229 } 2230 2231 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2232 // up/down (and the scroll wheel) move the selection a row; page up/down 2233 // jump a fixed amount. right/Enter cross into the detail pane. up from 2234 // the top row crosses into the header tabs. 2235 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2236 const lb = self.listBox(index); 2237 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2238 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2239 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2240 return; 2241 } 2242 switch (key) { 2243 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2244 else => {}, 2245 } 2246 } 2247 2248 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2249 const form = self.formBox() orelse return; 2250 const cid = form.getFocus().child_id orelse return; 2251 const cur = form.children.getIndex(cid) orelse return; 2252 const child = &form.children.values()[cur]; 2253 2254 if (child.widget == .text_input) { 2255 const text_input = &child.widget.text_input; 2256 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2257 return child.widget.input(allocator, key, root_focus); 2258 } 2259 2260 const on_submit = child.widget == .submit_button; 2261 switch (key) { 2262 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2263 root_focus.setFocus(form.children.keys()[i]) 2264 else 2265 self.focusHeader(root_focus), 2266 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2267 .enter => if (on_submit) return self.submitForm(allocator), 2268 .mouse => |mouse| if (on_submit) { 2269 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2270 }, 2271 else => {}, 2272 } 2273 try child.widget.input(allocator, key, root_focus); 2274 } 2275 2276 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2277 const form = self.confirmationForm() orelse return; 2278 const child_id = form.getFocus().child_id orelse return; 2279 const index = form.children.getIndex(child_id) orelse return; 2280 switch (key) { 2281 .arrow_up, .back_tab => if (index > 1) 2282 root_focus.setFocus(form.children.keys()[index - 1]) 2283 else 2284 self.focusHeader(root_focus), 2285 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2286 .enter => try self.submitConfirmation(allocator, index), 2287 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2288 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2289 try self.submitConfirmation(allocator, button_index); 2290 break; 2291 } 2292 }, 2293 else => {}, 2294 } 2295 } 2296 2297 // the multiline hunk inputs keep enter and any up/down that has a row to 2298 // move to; enter on a "use this" link is a navigation the host follows 2299 // before this runs. 2300 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2301 const form = self.formBox() orelse return; 2302 const cid = form.getFocus().child_id orelse return; 2303 const cur = form.children.getIndex(cid) orelse return; 2304 const child = &form.children.values()[cur]; 2305 2306 if (child.widget == .text_input) { 2307 const ti = &child.widget.text_input; 2308 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2309 return child.widget.input(allocator, key, root_focus); 2310 } 2311 2312 const on_submit = child.widget == .submit_button; 2313 2314 switch (key) { 2315 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2316 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2317 .arrow_left, .arrow_right => switch (child.widget) { 2318 .box => |*row| { 2319 const row_cid = row.getFocus().child_id orelse return; 2320 const row_index = row.children.getIndex(row_cid) orelse return; 2321 if (key == .arrow_left) { 2322 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2323 } else if (row_index + 1 < row.children.count()) { 2324 root_focus.setFocus(row.children.keys()[row_index + 1]); 2325 } 2326 }, 2327 else => try child.widget.input(allocator, key, root_focus), 2328 }, 2329 .enter => if (on_submit) try self.submitResolution(allocator), 2330 .mouse => |mouse| if (on_submit) { 2331 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2332 }, 2333 else => try child.widget.input(allocator, key, root_focus), 2334 } 2335 } 2336 2337 // a multiline input keeps enter and any up/down that has a row to move to 2338 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2339 return switch (key) { 2340 .enter => true, 2341 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2342 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2343 else => false, 2344 }; 2345 } 2346 2347 // the neighboring form control, skipping spacers and blank rows 2348 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2349 var i = cur; 2350 while (true) { 2351 if (down) { 2352 i += 1; 2353 if (i >= form.children.count()) return null; 2354 } else { 2355 if (i == 0) return null; 2356 i -= 1; 2357 } 2358 const child = &form.children.values()[i]; 2359 if (child.hidden) continue; 2360 switch (child.widget) { 2361 .spacer, .text => continue, 2362 else => return i, 2363 } 2364 } 2365 } 2366 2367 // focus the resolve-form child at `i` (a version row focuses its selected 2368 // child) and keep it visible on the terminal 2369 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2370 const child = &form.children.values()[i]; 2371 switch (child.widget) { 2372 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2373 else => root_focus.setFocus(form.children.keys()[i]), 2374 } 2375 if (self.session.is_terminal) { 2376 if (self.resolveScroll()) |sc| { 2377 if (child.rect) |rect| sc.scrollToRect(rect); 2378 } 2379 } 2380 } 2381 2382 // re-emit the conflicted thread's event with the resolved content; any 2383 // event on the thread settles its conflict entry. this is the terminal 2384 // path; the web posts the form to the resolve route. 2385 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2386 if (comptime wasm) return; 2387 const io = self.session.io orelse return; 2388 const src = self.data.repo_source orelse return; 2389 const entry = self.data.selectedThread() orelse return; 2390 const author = (try self.session.eventAuthor()) orelse return; 2391 const form = self.formBox() orelse return; 2392 2393 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2394 var title: ?[]u8 = null; 2395 var tags: ?[]u8 = null; 2396 var hunks: std.ArrayList([]const u8) = .empty; 2397 defer { 2398 if (title) |t| allocator.free(t); 2399 if (tags) |t| allocator.free(t); 2400 for (hunks.items) |h| allocator.free(h); 2401 hunks.deinit(allocator); 2402 } 2403 for (form.children.values()) |*child| switch (child.widget) { 2404 .text_input => |*ti| { 2405 const text = try ti.text(allocator); 2406 errdefer allocator.free(text); 2407 if (std.mem.eql(u8, ti.options.name, "title")) { 2408 title = text; 2409 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2410 tags = text; 2411 } else { 2412 try hunks.append(allocator, text); 2413 } 2414 }, 2415 else => {}, 2416 }; 2417 2418 const id_bytes = try evt.parseEventId(entry.id); 2419 2420 switch (src.repo_kind) { 2421 inline else => |repo_kind| { 2422 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2423 defer any_repo.deinit(io, allocator); 2424 switch (any_repo) { 2425 inline else => |*repo| { 2426 var resolution: Event.Resolve = .{ 2427 .title = title, 2428 .tags = tags, 2429 .hunks = hunks.items, 2430 }; 2431 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2432 const change = Event.Update{ .resolve = resolution }; 2433 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2434 // leave the form up for correction 2435 error.InvalidFields => return, 2436 else => |e| return e, 2437 }; 2438 }, 2439 } 2440 }, 2441 } 2442 2443 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2444 try self.session.navigate(route); 2445 } 2446 2447 // commit a new or edited comment and navigate to its permalink. this is the 2448 // terminal path; the web posts the form to its comment route. 2449 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2450 if (comptime wasm) return; 2451 const io = self.session.io orelse return; 2452 const src = self.data.repo_source orelse return; 2453 const author = (try self.session.eventAuthor()) orelse return; 2454 const form = self.formBox() orelse return; 2455 const body_input = try formField(form, "body"); 2456 const body = try body_input.text(allocator); 2457 defer allocator.free(body); 2458 if (!evt.Comment.fieldsValid(body)) return; 2459 2460 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2461 @memcpy(&thread_id, self.data.selected_id); 2462 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2463 var parent: [evt.event_id_size * 2]u8 = undefined; 2464 @memcpy(&parent, self.data.comment_id); 2465 break :blk parent; 2466 }; 2467 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2468 const editing = self.data.view == .edit_comment; 2469 switch (src.repo_kind) { 2470 inline else => |repo_kind| { 2471 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2472 defer any_repo.deinit(io, allocator); 2473 switch (any_repo) { 2474 inline else => |*repo| if (editing) { 2475 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2476 try evt.Comment.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2477 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2478 } else { 2479 event_id_hex = try evt.Comment.create(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2480 }, 2481 } 2482 }, 2483 } 2484 2485 body_input.clear(allocator); 2486 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2487 try self.session.navigate(route); 2488 } 2489 2490 // terminal form submissions; the web posts to the forms' routes instead 2491 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2492 switch (self.data.view) { 2493 .new_comment, .edit_comment => try self.submitComment(allocator), 2494 .edit => try self.submitEditedThread(allocator), 2495 else => try self.submitNewThread(allocator), 2496 } 2497 } 2498 2499 // commit the new thread to the repo's events branch and navigate to it. 2500 // this is the terminal path; the web posts the form to the thread route, 2501 // so the wasm side never runs (or compiles) the repo access below. 2502 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2503 if (comptime wasm) return; 2504 const io = self.session.io orelse return; 2505 const src = self.data.repo_source orelse return; 2506 const author = (try self.session.eventAuthor()) orelse return; 2507 2508 const form = self.threadForm() orelse return; 2509 const title_input = try formField(form, "title"); 2510 const tags_input = try formField(form, "tags"); 2511 const description_input = try formField(form, "description"); 2512 2513 const title = try title_input.text(allocator); 2514 defer allocator.free(title); 2515 const tags = try tags_input.text(allocator); 2516 defer allocator.free(tags); 2517 const description = try description_input.text(allocator); 2518 defer allocator.free(description); 2519 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2520 defer if (comptime supports_drafts) allocator.free(target_branch); 2521 2522 if (!Event.fieldsValid(title, tags)) { 2523 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2524 return; 2525 } 2526 2527 if (comptime supports_drafts) { 2528 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2529 const source_branch = switch (source.selected) { 2530 .fork => null, 2531 .branch => try source.field().text(allocator), 2532 }; 2533 defer if (source_branch) |branch| allocator.free(branch); 2534 const event_id_hex = Data.create(self.data, self.session, allocator, title, tags, description, target_branch, source_branch) catch |err| { 2535 const failure = FeedbackFailure.fromError(err) orelse return err; 2536 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2537 return; 2538 }; 2539 title_input.clear(allocator); 2540 tags_input.clear(allocator); 2541 description_input.clear(allocator); 2542 (try formField(form, "target_branch")).clear(allocator); 2543 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2544 try self.session.navigate(route); 2545 return; 2546 } 2547 2548 var id_bytes: [evt.event_id_size]u8 = undefined; 2549 io.random(&id_bytes); 2550 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2551 2552 const event = evt.EventWithId{ 2553 .id = event_id_hex, 2554 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2555 .author = author, 2556 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2557 .title = title, 2558 .description = description, 2559 .tags = tags, 2560 }), 2561 }; 2562 2563 switch (src.repo_kind) { 2564 inline else => |repo_kind| { 2565 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2566 defer any_repo.deinit(io, allocator); 2567 switch (any_repo) { 2568 inline else => |*repo| try evt.consume(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2569 } 2570 }, 2571 } 2572 2573 // wipe the form so a return visit starts fresh 2574 title_input.clear(allocator); 2575 tags_input.clear(allocator); 2576 description_input.clear(allocator); 2577 2578 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2579 try self.session.navigate(route); 2580 } 2581 2582 // re-emit the selected thread's event with the form's content (status 2583 // preserved), then reload the thread's page. this is the terminal path; 2584 // the web posts the form to the edit route. 2585 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2586 if (comptime wasm) return; 2587 const io = self.session.io orelse return; 2588 const entry = self.data.selectedThread() orelse return; 2589 2590 const form = self.threadForm() orelse return; 2591 const title_input = try formField(form, "title"); 2592 const tags_input = try formField(form, "tags"); 2593 const description_input = try formField(form, "description"); 2594 2595 const title = try title_input.text(allocator); 2596 defer allocator.free(title); 2597 const tags = try tags_input.text(allocator); 2598 defer allocator.free(tags); 2599 const description = try description_input.text(allocator); 2600 defer allocator.free(description); 2601 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2602 defer if (comptime supports_drafts) allocator.free(target_branch); 2603 2604 if (!Event.fieldsValid(title, tags)) { 2605 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2606 return; 2607 } 2608 const src = self.data.repo_source orelse return; 2609 2610 if (comptime supports_drafts) { 2611 if (entryDraft(entry.*)) { 2612 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| { 2613 const failure = FeedbackFailure.fromError(err) orelse return err; 2614 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2615 return; 2616 }; 2617 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2618 try self.session.navigate(route); 2619 return; 2620 } 2621 } 2622 2623 const id_bytes = try evt.parseEventId(entry.id); 2624 const author = (try self.session.eventAuthor()) orelse return; 2625 2626 switch (src.repo_kind) { 2627 inline else => |repo_kind| { 2628 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2629 defer any_repo.deinit(io, allocator); 2630 switch (any_repo) { 2631 inline else => |*repo| if (supports_drafts) { 2632 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2633 .title = title, 2634 .tags = tags, 2635 .description = description, 2636 .target_branch = target_branch, 2637 } }, author) catch |err| { 2638 const failure = FeedbackFailure.fromError(err) orelse return err; 2639 try self.rememberThreadFeedback(failure, title, tags, description, target_branch); 2640 return; 2641 }; 2642 } else if (has_status) 2643 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2644 .title = title, 2645 .tags = tags, 2646 .description = description, 2647 } }, author) 2648 else 2649 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2650 } 2651 }, 2652 } 2653 2654 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2655 try self.session.navigate(route); 2656 } 2657 2658 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2659 const aa = self.session.arena.allocator(); 2660 const source = if (supports_drafts) blk: { 2661 const form = self.threadForm() orelse break :blk null; 2662 break :blk formWidget(form, .patch_source); 2663 } else null; 2664 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2665 .failure = failure, 2666 .fields = if (comptime supports_drafts) .{ 2667 .title = try aa.dupe(u8, title), 2668 .tags = try aa.dupe(u8, tags), 2669 .description = try aa.dupe(u8, description), 2670 .target_branch = try aa.dupe(u8, target_branch), 2671 .source_branch = if (source) |value| try value.field().text(aa) else "", 2672 .source_kind = if (source) |value| value.selected else .fork, 2673 } else .{ 2674 .title = try aa.dupe(u8, title), 2675 .tags = try aa.dupe(u8, tags), 2676 .description = try aa.dupe(u8, description), 2677 }, 2678 }); 2679 } 2680 2681 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2682 switch (confirmation(self.data.view) orelse return) { 2683 .publish => if (comptime supports_drafts) { 2684 if (comptime wasm) return; 2685 const entry = self.data.selectedThread() orelse return; 2686 if (!entryDraft(entry.*)) return; 2687 try self.data.publishDraft(self.session, allocator, entry.id); 2688 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2689 try self.session.navigate(route); 2690 } else unreachable, 2691 .merge => if (comptime supports_merge) { 2692 if (comptime wasm) return; 2693 const entry = self.data.selectedThread() orelse return; 2694 const revision: pch.MergeRevision = if (button_index > 1) .squash else .source; 2695 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2696 self.data.mergePatch(self.session, allocator, entry.id, revision) catch |err| switch (err) { 2697 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2698 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2699 try self.session.navigate(route); 2700 return; 2701 }, 2702 else => return err, 2703 }; 2704 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2705 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2706 try self.session.navigate(route); 2707 } else unreachable, 2708 .remove => try self.submitRemove(allocator), 2709 } 2710 } 2711 2712 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2713 const comment = self.data.comment_id.len != 0; 2714 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2715 const id = evt.parseEventId(event_id) catch return; 2716 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2717 } 2718 2719 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2720 if (comptime wasm) return; 2721 2722 if (comptime supports_drafts) { 2723 const selected = self.data.selectedThread() orelse return; 2724 if (event_kind == kind and entryDraft(selected.*)) { 2725 try Data.removeDraft(self.session, allocator, &id); 2726 const route = draftsRoute(self.data.identity) orelse return; 2727 try self.session.navigate(route); 2728 return; 2729 } 2730 } 2731 2732 const io = self.session.io orelse return; 2733 const src = self.data.repo_source orelse return; 2734 const author = (try self.session.eventAuthor()) orelse return; 2735 2736 switch (src.repo_kind) { 2737 inline else => |repo_kind| { 2738 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2739 defer any_repo.deinit(io, allocator); 2740 switch (any_repo) { 2741 inline else => |*repo| try evt.remove(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2742 } 2743 }, 2744 } 2745 2746 const id_hex = std.fmt.bytesToHex(id, .lower); 2747 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2748 try self.session.navigate(route); 2749 } 2750 2751 // return to the list. 2752 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2753 root_focus.setFocus(self.listScroll(index).getFocus().id); 2754 } 2755 2756 // cross to the header tabs above the stack. 2757 fn focusHeader(self: *This, root_focus: *Focus) void { 2758 root_focus.setFocus(self.box.children.keys()[header_index]); 2759 } 2760 2761 pub fn clearGrid(self: *This) void { 2762 self.box.clearGrid(); 2763 } 2764 2765 pub fn getGrid(self: This) ?Grid { 2766 return self.box.getGrid(); 2767 } 2768 2769 pub fn getFocus(self: *This) *Focus { 2770 return self.box.getFocus(); 2771 } 2772 2773 // for the parent's "scroll up at the top jumps to the header" check: at the 2774 // top only while the header tabs hold focus, so up from the split 2775 // crosses into the tabs first. 2776 pub fn atTop(self: *This) bool { 2777 return self.headerActive(); 2778 } 2779 }; 2780 } 2781