╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1908 var button = try wgt.TextBox.init(allocator, options.label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = options.bottom_label } }); 1909 errdefer button.deinit(allocator); 1910 button.getFocus().mode = .all; 1911 button.getFocus().kind = .{ .custom = options.action }; 1912 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 1913 if (index == 0) box.getFocus().child_id = button.getFocus().id; 1914 } 1915 1916 return Center.init(allocator, .{ .box = box }); 1917 } 1918 1919 // a form's submit button, then a spacer absorbing the leftover 1920 // min-height the box hands its last child, so the button keeps its 1921 // natural height 1922 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 1923 { 1924 var submit = try SubmitButton.initLabeled(allocator, label); 1925 errdefer submit.deinit(allocator); 1926 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1927 } 1928 { 1929 var spacer = try Spacer.init(allocator); 1930 errdefer spacer.deinit(allocator); 1931 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1932 } 1933 } 1934 1935 // the conflict resolve form. the "use this" links are navigations that 1936 // flip the url's theirs: pick, reloading the prefills; one submit 1937 // settles every conflict. the form: subtree makes the web renderer POST 1938 // to the resolve route. 1939 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 1940 const aa = session.page_arena.allocator(); 1941 // init only picks the resolve view once it has read the conflict 1942 const conflict = if (data.conflict) |*c| c else unreachable; 1943 1944 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 1945 errdefer box.deinit(allocator); 1946 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 1947 box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 1948 1949 if (conflict.title) |*fc| { 1950 try addLabel(allocator, &box, "title conflict:"); 1951 try addFieldConflict(allocator, &box, session, data, "title", fc); 1952 } 1953 if (conflict.labels) |*fc| { 1954 try addLabel(allocator, &box, "labels conflict:"); 1955 try addFieldConflict(allocator, &box, session, data, "labels", fc); 1956 } 1957 if (comptime @hasField(Data.Conflict, "status")) { 1958 if (conflict.status) |*fc| { 1959 try addLabel(allocator, &box, "status conflict:"); 1960 try addAtomicConflict(allocator, &box, session, data, "status", fc); 1961 } 1962 } 1963 if (comptime @hasField(Data.Conflict, "revision")) { 1964 if (conflict.revision) |*fc| { 1965 try addLabel(allocator, &box, "revision conflict:"); 1966 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 1967 } 1968 if (conflict.target_branch) |*fc| { 1969 try addLabel(allocator, &box, "target branch conflict:"); 1970 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 1971 } 1972 } 1973 1974 if (conflict.description) |*desc| { 1975 try addLabel(allocator, &box, "description conflict:"); 1976 var hunk_index: usize = 0; 1977 for (desc.chunks, 0..) |*chunk, chunk_index| { 1978 switch (chunk.*) { 1979 .same => |text| { 1980 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .word }); 1981 errdefer tb.deinit(allocator); 1982 tb.getFocus().mode = .all; 1983 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1984 }, 1985 // an auto-resolved chunk: only one side changed it, so its 1986 // version stands, shown distinctly with its author 1987 .auto => |auto| { 1988 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 1989 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 1990 const label = switch (author) { 1991 .user_name, .email => |name| try aa.print(" {s} by {s} ", .{ verb, name }), 1992 .unknown => try aa.print(" {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 1993 }; 1994 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 1995 errdefer tb.deinit(allocator); 1996 tb.getFocus().mode = .all; 1997 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1998 }, 1999 .conflict => |hunk| { 2000 // a blank row on each side sets the group apart from 2001 // the flowing shared and auto-resolved chunks 2002 if (chunk_index > 0) try addGap(allocator, &box); 2003 const name = try aa.print("d{d}", .{hunk_index}); 2004 hunk_index += 1; 2005 const picked_theirs = data.theirsPicked(name); 2006 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 2007 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 2008 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true }); 2009 errdefer resolution_input.deinit(allocator); 2010 resolution_input.getFocus().mode = .all; 2011 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 2012 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2013 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 2014 }, 2015 } 2016 } 2017 } 2018 2019 try addSubmitButtonLabeled(allocator, &box, "submit"); 2020 2021 box.getFocus().child_id = box.children.keys()[0]; 2022 return box; 2023 } 2024 2025 // one conflicted scalar field: both sides' version rows, then the 2026 // resolution input prefilled from the picked side. 2027 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2028 const aa = session.page_arena.allocator(); 2029 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2030 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2031 2032 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2033 errdefer resolution_input.deinit(allocator); 2034 resolution_input.getFocus().mode = .all; 2035 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2036 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2037 } 2038 2039 // atomic fields can only pick one complete side 2040 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2041 const aa = session.page_arena.allocator(); 2042 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2043 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2044 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2045 .border = .single, 2046 .round_corners = true, 2047 .wrap_kind = .word, 2048 .top_label = .{ .text = " resolution " }, 2049 }); 2050 errdefer selected.deinit(allocator); 2051 selected.getFocus().mode = .all; 2052 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2053 } 2054 2055 // a blank row setting a conflict group apart from its neighbors 2056 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2057 var text = try wgt.Text.init(allocator, " "); 2058 errdefer text.deinit(allocator); 2059 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2060 } 2061 2062 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2063 var label = try SectionLabel.init(allocator, content); 2064 errdefer label.deinit(allocator); 2065 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2066 } 2067 2068 // one side of a conflict: the "use this" link on the left, the version 2069 // itself as a labeled read-only box. 2070 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2071 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2072 errdefer row.deinit(allocator); 2073 2074 { 2075 const use_label = "use this"; 2076 var use = try wgt.TextBox.init(allocator, use_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 2077 errdefer use.deinit(allocator); 2078 use.getFocus().mode = .all; 2079 use.getFocus().kind = .{ .custom = link }; 2080 try row.children.put(allocator, use.getFocus().id, .{ 2081 .widget = .{ .text_box = use }, 2082 .rect = null, 2083 .min_size = .{ .width = use_label.len + 2, .height = null }, 2084 .max_size = .{ .width = use_label.len + 2, .height = null }, 2085 }); 2086 } 2087 2088 { 2089 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2090 errdefer tb.deinit(allocator); 2091 tb.getFocus().mode = .all; 2092 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2093 } 2094 2095 row.getFocus().child_id = row.children.keys()[0]; 2096 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2097 } 2098 2099 // the label of one side's version box, naming its author 2100 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2101 return switch (author) { 2102 .user_name, .email => |name| try aa.print(" edited by {s} ", .{name}), 2103 .unknown => if (ours) " current version " else " their version ", 2104 }; 2105 } 2106 2107 // the a: link that reloads the resolve page with `name` picked from ours 2108 // or theirs, reprefilling its resolution input 2109 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2110 const aa = session.page_arena.allocator(); 2111 var picks: std.ArrayList(u8) = .empty; 2112 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2113 while (it.next()) |pick| { 2114 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2115 if (picks.items.len > 0) try picks.append(aa, ','); 2116 try picks.appendSlice(aa, pick); 2117 } 2118 if (theirs) { 2119 if (picks.items.len > 0) try picks.append(aa, ','); 2120 try picks.appendSlice(aa, name); 2121 } 2122 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2123 return aa.print("a:{s}", .{try route.toUrl(session.page_arena)}); 2124 } 2125 2126 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2127 var row = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = bottom_label } }); 2128 errdefer row.deinit(allocator); 2129 row.getFocus().mode = .all; 2130 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2131 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2132 } 2133 2134 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2135 self.box.deinit(allocator); 2136 } 2137 2138 fn header(self: *This) *HeaderType { 2139 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2140 } 2141 2142 fn viewStack(self: *This) *wgt.Stack(Widget) { 2143 return &self.box.children.values()[stack_index].widget.stack; 2144 } 2145 2146 // `index`'s master-detail split inside the stack. 2147 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2148 return &self.viewStack().children.values()[index].box; 2149 } 2150 2151 // the labels view's flow inside the stack. 2152 fn labelsView(self: *This) *WordFlow { 2153 return &self.viewStack().children.values()[labels_view_index].word_flow; 2154 } 2155 2156 // the non-confirmation form inside the stack 2157 fn formBox(self: *This) ?*wgt.Box(Widget) { 2158 return switch (self.viewStack().children.values()[form_view_index]) { 2159 .box => |*box| box, 2160 // the resolve form sits inside a scroll on the terminal 2161 .scroll => |*scroll| &scroll.child.box, 2162 else => null, 2163 }; 2164 } 2165 2166 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2167 if (supports_conflicts and self.data.view == .resolve) return null; 2168 return switch (self.data.view) { 2169 .new_comment, .edit_comment => null, 2170 else => self.formBox(), 2171 }; 2172 } 2173 2174 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2175 for (form.children.values()) |*child| switch (child.widget) { 2176 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2177 else => {}, 2178 }; 2179 return error.MissingFormField; 2180 } 2181 2182 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2183 for (form.children.values()) |*child| { 2184 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2185 } 2186 return null; 2187 } 2188 2189 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2190 return switch (self.viewStack().children.values()[form_view_index]) { 2191 .center => |*center| switch (center.child.*) { 2192 .box => |*box| box, 2193 else => null, 2194 }, 2195 else => null, 2196 }; 2197 } 2198 2199 // the resolve form's scroll on the terminal (the web page scrolls itself) 2200 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2201 return switch (self.viewStack().children.values()[form_view_index]) { 2202 .scroll => |*scroll| scroll, 2203 else => null, 2204 }; 2205 } 2206 2207 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2208 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2209 } 2210 2211 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2212 return &self.listScroll(index).child.box; 2213 } 2214 2215 fn detail(self: *This, index: usize) *DetailType { 2216 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 2217 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 2218 } 2219 2220 fn window(self: *This, index: usize) *const Window { 2221 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2222 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2223 return self.data.window(splitStatus(index)); 2224 } 2225 2226 fn stackSelectedIndex(self: *This) ?usize { 2227 const stack = self.viewStack(); 2228 const cid = stack.getFocus().child_id orelse return null; 2229 return stack.children.getIndex(cid); 2230 } 2231 2232 // the stack's selected master-detail split (null when the labels view or the 2233 // new-thread form shows). 2234 fn selectedSplitIndex(self: *This) ?usize { 2235 const idx = self.stackSelectedIndex() orelse return null; 2236 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2237 } 2238 2239 fn detailActive(self: *This, index: usize) bool { 2240 const rb = self.resultsBox(index); 2241 const cid = rb.getFocus().child_id orelse return false; 2242 return rb.children.getIndex(cid) == detail_index; 2243 } 2244 2245 fn headerActive(self: *This) bool { 2246 const cid = self.box.getFocus().child_id orelse return false; 2247 return self.box.children.getIndex(cid) == header_index; 2248 } 2249 2250 fn labelsViewActive(self: *This) bool { 2251 if (self.headerActive()) return false; 2252 return self.stackSelectedIndex() == labels_view_index; 2253 } 2254 2255 fn formViewActive(self: *This) bool { 2256 if (self.headerActive()) return false; 2257 return self.stackSelectedIndex() == form_view_index; 2258 } 2259 2260 // the selected thread's index, or null when a window-navigation row is 2261 // selected (a leading "previous" row shifts the thread rows down by one). 2262 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2263 const lb = self.listBox(index); 2264 const cid = lb.getFocus().child_id orelse return null; 2265 const idx = lb.children.getIndex(cid) orelse return null; 2266 const win = self.window(index); 2267 const lead: usize = if (win.prev_id != null) 1 else 0; 2268 if (idx < lead or idx - lead >= win.items.len) return null; 2269 return idx - lead; 2270 } 2271 2272 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2273 self.clearGrid(); 2274 2275 // each header tab maps 1:1 to a stack child by position 2276 if (self.header().getSelectedIndex()) |index| { 2277 const stack = self.viewStack(); 2278 stack.getFocus().child_id = stack.children.keys()[index]; 2279 } 2280 2281 if (self.selectedSplitIndex()) |i| { 2282 // swap the detail pane to the selected thread when it changes. 2283 if (self.selectedThreadIndex(i)) |selected| { 2284 const entry = self.window(i).items[selected]; 2285 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2286 if (changed) { 2287 try self.detail(i).setEntry(allocator, entry); 2288 } 2289 } 2290 2291 // the selected list row shows a border (the focused TextBox 2292 // upgrades it to a double border itself); the rest stay borderless. 2293 const lb = self.listBox(i); 2294 for (lb.children.keys(), lb.children.values()) |id, *child| { 2295 switch (child.widget) { 2296 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2297 else => {}, 2298 } 2299 } 2300 2301 // cap the list at list_max_width only while the detail pane fits 2302 // beside it. the box drops the detail when the width can't hold 2303 // both minimums, so when it's that narrow we lift the cap and let 2304 // the list fill the whole width. 2305 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2306 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2307 2308 // stretch the detail pane across the rest of the width so it fills 2309 // the area rather than shrinking to its content; its scroll fills 2310 // the pane. 2311 if (constraint.max_size.width) |w| { 2312 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2313 } else { 2314 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2315 } 2316 } 2317 2318 // refresh the form inputs' entries in the session's focus-id -> input 2319 // map with this frame's addresses, so the web/wasm form handling can 2320 // find them by focus id 2321 if (self.threadForm()) |form| { 2322 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2323 (try formField(form, "title")).options.top_label.text = if (failure == .required_title) " title (required) " else " title "; 2324 if (comptime supports_drafts) { 2325 if (formWidget(form, .patch_source)) |source| { 2326 source.field().options.top_label.text = 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 "; 2327 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2328 try submit.setLabel(allocator, switch (source.selectedKind()) { 2329 .fork => "submit draft", 2330 .branch => "submit patch", 2331 }); 2332 } 2333 (try formField(form, "target_branch")).options.top_label.text = 2334 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 "; 2335 } 2336 } 2337 if (self.formBox()) |form| { 2338 const inputs_arena = self.session.arena.allocator(); 2339 for (form.children.values()) |*child| switch (child.widget) { 2340 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2341 else => {}, 2342 }; 2343 } 2344 2345 try self.box.build(allocator, constraint, root_focus); 2346 } 2347 2348 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2349 // wheel input moves focus like the arrows 2350 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2351 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2352 else 2353 raw_key; 2354 2355 if (self.headerActive()) { 2356 // down from the sub-header re-enters the stack if the selected 2357 // view has something to focus; enter in the search box submits 2358 // the query; other keys move within the sub-header. 2359 if (inp.vertDirection(key) == .down) { 2360 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2361 .word_flow => |*tf| tf.text_boxes.items.len > 0, 2362 // nothing focusable in the unauthorized view 2363 .unauthorized => false, 2364 else => true, 2365 } else false; 2366 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2367 } else if (key == .enter) { 2368 if (try self.header().submittedText(allocator)) |text| { 2369 defer allocator.free(text); 2370 try self.submitSearch(text); 2371 } 2372 } else { 2373 try self.header().input(allocator, key, root_focus); 2374 } 2375 return; 2376 } 2377 if (self.labelsViewActive()) { 2378 self.labelsViewInput(key, root_focus); 2379 return; 2380 } 2381 if (self.formViewActive()) { 2382 if (confirmation(self.data.view) != null) { 2383 try self.confirmationFormInput(allocator, key, root_focus); 2384 } else if (supports_conflicts and self.data.view == .resolve) { 2385 try self.resolveInput(allocator, key, root_focus); 2386 } else { 2387 try self.formInput(allocator, key, root_focus); 2388 } 2389 return; 2390 } 2391 const i = self.selectedSplitIndex() orelse return; 2392 if (self.detailActive(i)) { 2393 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2394 .none => {}, 2395 .list => self.focusList(i, root_focus), 2396 .header => self.focusHeader(root_focus), 2397 } 2398 } else { 2399 self.listInput(i, key, root_focus); 2400 } 2401 } 2402 2403 // navigate to the typed query's results, keeping the label filter and the 2404 // status list the page shows. an empty box clears a filtered page and 2405 // does nothing on any other one. 2406 fn submitSearch(self: *This, text: []const u8) !void { 2407 if (text.len == 0 and self.data.search == null) return; 2408 const selected = self.header().getSelectedIndex() orelse 0; 2409 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2410 const route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2411 try self.session.navigate(route.withSearch(text) orelse return); 2412 } 2413 2414 // arrow keys move the label selection; up from the top row crosses to the 2415 // header tabs. 2416 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2417 const tf = self.labelsView(); 2418 const cid = tf.focus.child_id orelse return; 2419 const cur = tf.indexOfFocusId(cid) orelse return; 2420 const count = tf.text_boxes.items.len; 2421 switch (key) { 2422 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2423 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2424 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2425 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2426 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2427 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2428 else => {}, 2429 } 2430 } 2431 2432 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2433 // up/down (and the scroll wheel) move the selection a row; page up/down 2434 // jump a fixed amount. right/Enter cross into the detail pane. up from 2435 // the top row crosses into the header tabs. 2436 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2437 const lb = self.listBox(index); 2438 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2439 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2440 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2441 return; 2442 } 2443 switch (key) { 2444 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2445 else => {}, 2446 } 2447 } 2448 2449 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2450 const form = self.formBox() orelse return; 2451 const cid = form.getFocus().child_id orelse return; 2452 const cur = form.children.getIndex(cid) orelse return; 2453 const child = &form.children.values()[cur]; 2454 2455 if (child.widget == .text_input) { 2456 const text_input = &child.widget.text_input; 2457 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2458 return child.widget.input(allocator, key, root_focus); 2459 } 2460 2461 const on_submit = child.widget == .submit_button; 2462 switch (key) { 2463 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2464 root_focus.setFocus(form.children.keys()[i]) 2465 else 2466 self.focusHeader(root_focus), 2467 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2468 .enter => if (on_submit) return self.submitForm(allocator), 2469 .mouse => |mouse| if (on_submit) { 2470 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2471 }, 2472 else => {}, 2473 } 2474 try child.widget.input(allocator, key, root_focus); 2475 } 2476 2477 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2478 const form = self.confirmationForm() orelse return; 2479 const child_id = form.getFocus().child_id orelse return; 2480 const index = form.children.getIndex(child_id) orelse return; 2481 switch (key) { 2482 .arrow_up, .back_tab => if (index > 1) 2483 root_focus.setFocus(form.children.keys()[index - 1]) 2484 else 2485 self.focusHeader(root_focus), 2486 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2487 .enter => try self.submitConfirmation(allocator, index), 2488 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2489 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2490 try self.submitConfirmation(allocator, button_index); 2491 break; 2492 } 2493 }, 2494 else => {}, 2495 } 2496 } 2497 2498 // the multiline hunk inputs keep enter and any up/down that has a row to 2499 // move to; enter on a "use this" link is a navigation the host follows 2500 // before this runs. 2501 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2502 const form = self.formBox() orelse return; 2503 const cid = form.getFocus().child_id orelse return; 2504 const cur = form.children.getIndex(cid) orelse return; 2505 const child = &form.children.values()[cur]; 2506 2507 if (child.widget == .text_input) { 2508 const ti = &child.widget.text_input; 2509 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2510 return child.widget.input(allocator, key, root_focus); 2511 } 2512 2513 const on_submit = child.widget == .submit_button; 2514 2515 switch (key) { 2516 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2517 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2518 .arrow_left, .arrow_right => switch (child.widget) { 2519 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2520 else => try child.widget.input(allocator, key, root_focus), 2521 }, 2522 .enter => if (on_submit) try self.submitResolution(allocator), 2523 .mouse => |mouse| if (on_submit) { 2524 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2525 }, 2526 else => try child.widget.input(allocator, key, root_focus), 2527 } 2528 } 2529 2530 // a multiline input keeps enter and any up/down that has a row to move to 2531 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2532 return switch (key) { 2533 .enter => true, 2534 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2535 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2536 else => false, 2537 }; 2538 } 2539 2540 // the neighboring form control, skipping spacers and blank rows 2541 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2542 var i = cur; 2543 while (true) { 2544 if (down) { 2545 i += 1; 2546 if (i >= form.children.count()) return null; 2547 } else { 2548 if (i == 0) return null; 2549 i -= 1; 2550 } 2551 const child = &form.children.values()[i]; 2552 if (child.hidden) continue; 2553 switch (child.widget) { 2554 .spacer, .text => continue, 2555 else => return i, 2556 } 2557 } 2558 } 2559 2560 // focus the resolve-form child at `i` (a version row focuses its selected 2561 // child) and keep it visible on the terminal 2562 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2563 const child = &form.children.values()[i]; 2564 switch (child.widget) { 2565 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2566 else => root_focus.setFocus(form.children.keys()[i]), 2567 } 2568 if (self.session.is_terminal) { 2569 if (self.resolveScroll()) |sc| { 2570 if (child.rect) |rect| sc.scrollToRect(rect); 2571 } 2572 } 2573 } 2574 2575 // re-emit the conflicted thread's event with the resolved content; any 2576 // event on the thread settles its conflict entry. this is the terminal 2577 // path; the web posts the form to the resolve route. 2578 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2579 if (comptime wasm) return; 2580 const io = self.session.io orelse return; 2581 const src = self.data.repo_source orelse return; 2582 const entry = self.data.selectedThread() orelse return; 2583 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2584 const form = self.formBox() orelse return; 2585 2586 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2587 var title: ?[]u8 = null; 2588 var labels: ?[]u8 = null; 2589 var hunks: std.ArrayList([]const u8) = .empty; 2590 defer { 2591 if (title) |t| allocator.free(t); 2592 if (labels) |t| allocator.free(t); 2593 for (hunks.items) |h| allocator.free(h); 2594 hunks.deinit(allocator); 2595 } 2596 for (form.children.values()) |*child| switch (child.widget) { 2597 .text_input => |*ti| { 2598 const text = try ti.text(allocator); 2599 errdefer allocator.free(text); 2600 if (std.mem.eql(u8, ti.options.name, "title")) { 2601 title = text; 2602 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2603 labels = text; 2604 } else { 2605 try hunks.append(allocator, text); 2606 } 2607 }, 2608 else => {}, 2609 }; 2610 2611 const id_bytes = try evt.parseEventId(entry.id); 2612 2613 switch (src.repo_kind) { 2614 inline else => |repo_kind| { 2615 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2616 defer any_repo.deinit(io, allocator); 2617 switch (any_repo) { 2618 inline else => |*repo| { 2619 var resolution: Event.Resolve = .{ 2620 .title = title, 2621 .labels = labels, 2622 .hunks = hunks.items, 2623 }; 2624 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2625 const change = Event.Update{ .resolve = resolution }; 2626 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2627 // leave the form up for correction 2628 error.InvalidFields => return, 2629 else => |e| return e, 2630 }; 2631 }, 2632 } 2633 }, 2634 } 2635 2636 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2637 try self.session.navigate(route); 2638 } 2639 2640 // commit a new or edited comment and navigate to its permalink. this is the 2641 // terminal path; the web posts the form to its comment route. 2642 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2643 if (comptime wasm) return; 2644 const io = self.session.io orelse return; 2645 const src = self.data.repo_source orelse return; 2646 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2647 const author = actor.author; 2648 const form = self.formBox() orelse return; 2649 const body_input = try formField(form, "body"); 2650 const body = try body_input.text(allocator); 2651 defer allocator.free(body); 2652 if (!evt.Comment.fieldsValid(body)) return; 2653 2654 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2655 @memcpy(&thread_id, self.data.selected_id); 2656 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2657 var parent: [evt.event_id_size * 2]u8 = undefined; 2658 @memcpy(&parent, self.data.comment_id); 2659 break :blk parent; 2660 }; 2661 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2662 const editing = self.data.view == .edit_comment; 2663 if (editing) { 2664 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2665 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 2666 } 2667 switch (src.repo_kind) { 2668 inline else => |repo_kind| { 2669 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2670 defer any_repo.deinit(io, allocator); 2671 switch (any_repo) { 2672 inline else => |*repo| if (editing) { 2673 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2674 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 2675 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2676 } else { 2677 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &parent_id, body, author); 2678 }, 2679 } 2680 }, 2681 } 2682 2683 body_input.clear(allocator); 2684 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2685 try self.session.navigate(route); 2686 } 2687 2688 // terminal form submissions; the web posts to the forms' routes instead 2689 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2690 switch (self.data.view) { 2691 .new_comment, .edit_comment => try self.submitComment(allocator), 2692 .edit => try self.submitEditedThread(allocator), 2693 else => try self.submitNewThread(allocator), 2694 } 2695 } 2696 2697 // commit the new thread to the repo's events branch and navigate to it. 2698 // this is the terminal path; the web posts the form to the thread route, 2699 // so the wasm side never runs (or compiles) the repo access below. 2700 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2701 if (comptime wasm) return; 2702 const io = self.session.io orelse return; 2703 const src = self.data.repo_source orelse return; 2704 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2705 2706 const form = self.threadForm() orelse return; 2707 const title_input = try formField(form, "title"); 2708 const labels_input = try formField(form, "labels"); 2709 const description_input = try formField(form, "description"); 2710 2711 const title = try title_input.text(allocator); 2712 defer allocator.free(title); 2713 const labels = try labels_input.text(allocator); 2714 defer allocator.free(labels); 2715 const description = try description_input.text(allocator); 2716 defer allocator.free(description); 2717 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2718 defer if (comptime supports_drafts) allocator.free(target_branch); 2719 2720 if (!Event.fieldsValid(title, labels)) { 2721 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2722 return; 2723 } 2724 2725 if (comptime supports_drafts) { 2726 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2727 const source_branch = switch (source.selectedKind()) { 2728 .fork => null, 2729 .branch => try source.field().text(allocator), 2730 }; 2731 defer if (source_branch) |branch| allocator.free(branch); 2732 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 2733 const failure = FeedbackFailure.fromError(err) orelse return err; 2734 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2735 return; 2736 }; 2737 title_input.clear(allocator); 2738 labels_input.clear(allocator); 2739 description_input.clear(allocator); 2740 (try formField(form, "target_branch")).clear(allocator); 2741 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2742 try self.session.navigate(route); 2743 return; 2744 } 2745 2746 var id_bytes: [evt.event_id_size]u8 = undefined; 2747 io.random(&id_bytes); 2748 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2749 2750 const event = evt.EventWithId{ 2751 .id = event_id_hex, 2752 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2753 .author = actor.author, 2754 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2755 .title = title, 2756 .description = description, 2757 .labels = labels, 2758 }), 2759 }; 2760 2761 switch (src.repo_kind) { 2762 inline else => |repo_kind| { 2763 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2764 defer any_repo.deinit(io, allocator); 2765 switch (any_repo) { 2766 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2767 } 2768 }, 2769 } 2770 2771 // wipe the form so a return visit starts fresh 2772 title_input.clear(allocator); 2773 labels_input.clear(allocator); 2774 description_input.clear(allocator); 2775 2776 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", &event_id_hex) orelse return; 2777 try self.session.navigate(route); 2778 } 2779 2780 // re-emit the selected thread's event with the form's content (status 2781 // preserved), then reload the thread's page. this is the terminal path; 2782 // the web posts the form to the edit route. 2783 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2784 if (comptime wasm) return; 2785 const io = self.session.io orelse return; 2786 const entry = self.data.selectedThread() orelse return; 2787 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2788 const author = actor.author; 2789 2790 const form = self.threadForm() orelse return; 2791 const title_input = try formField(form, "title"); 2792 const labels_input = try formField(form, "labels"); 2793 const description_input = try formField(form, "description"); 2794 2795 const title = try title_input.text(allocator); 2796 defer allocator.free(title); 2797 const labels = try labels_input.text(allocator); 2798 defer allocator.free(labels); 2799 const description = try description_input.text(allocator); 2800 defer allocator.free(description); 2801 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2802 defer if (comptime supports_drafts) allocator.free(target_branch); 2803 2804 if (!Event.fieldsValid(title, labels)) { 2805 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2806 return; 2807 } 2808 const src = self.data.repo_source orelse return; 2809 2810 if (comptime supports_drafts) { 2811 if (entryDraft(entry.*)) { 2812 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 2813 const failure = FeedbackFailure.fromError(err) orelse return err; 2814 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2815 return; 2816 }; 2817 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2818 try self.session.navigate(route); 2819 return; 2820 } 2821 } 2822 2823 const id_bytes = try evt.parseEventId(entry.id); 2824 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2825 2826 switch (src.repo_kind) { 2827 inline else => |repo_kind| { 2828 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2829 defer any_repo.deinit(io, allocator); 2830 switch (any_repo) { 2831 inline else => |*repo| if (supports_drafts) { 2832 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2833 .title = title, 2834 .labels = labels, 2835 .description = description, 2836 .target_branch = target_branch, 2837 } }, author) catch |err| { 2838 const failure = FeedbackFailure.fromError(err) orelse return err; 2839 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2840 return; 2841 }; 2842 } else if (has_status) 2843 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2844 .title = title, 2845 .labels = labels, 2846 .description = description, 2847 } }, author) 2848 else 2849 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 2850 } 2851 }, 2852 } 2853 2854 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2855 try self.session.navigate(route); 2856 } 2857 2858 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 2859 const aa = self.session.arena.allocator(); 2860 const source = if (supports_drafts) blk: { 2861 const form = self.threadForm() orelse break :blk null; 2862 break :blk formWidget(form, .patch_source); 2863 } else null; 2864 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2865 .failure = failure, 2866 .fields = if (comptime supports_drafts) .{ 2867 .title = try aa.dupe(u8, title), 2868 .labels = try aa.dupe(u8, labels), 2869 .description = try aa.dupe(u8, description), 2870 .target_branch = try aa.dupe(u8, target_branch), 2871 .source_branch = if (source) |value| try value.field().text(aa) else "", 2872 .source_kind = if (source) |value| value.selectedKind() else .fork, 2873 } else .{ 2874 .title = try aa.dupe(u8, title), 2875 .labels = try aa.dupe(u8, labels), 2876 .description = try aa.dupe(u8, description), 2877 }, 2878 }); 2879 } 2880 2881 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2882 switch (confirmation(self.data.view) orelse return) { 2883 .publish => if (comptime supports_drafts) { 2884 if (comptime wasm) return; 2885 const entry = self.data.selectedThread() orelse return; 2886 if (!entryDraft(entry.*)) return; 2887 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 2888 try self.data.publishDraft(self.session, allocator, author, entry.id); 2889 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", entry.id) orelse return; 2890 try self.session.navigate(route); 2891 } else unreachable, 2892 .merge => if (comptime supports_merge) { 2893 if (comptime wasm) return; 2894 const entry = self.data.selectedThread() orelse return; 2895 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2896 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2897 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2898 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2899 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2900 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2901 try self.session.navigate(route); 2902 return; 2903 }, 2904 else => return err, 2905 }; 2906 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2907 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2908 try self.session.navigate(route); 2909 } else unreachable, 2910 .remove => try self.submitRemove(allocator), 2911 } 2912 } 2913 2914 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2915 const comment = self.data.comment_id.len != 0; 2916 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2917 const id = evt.parseEventId(event_id) catch return; 2918 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2919 } 2920 2921 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2922 if (comptime wasm) return; 2923 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2924 const author = actor.author; 2925 2926 if (comptime supports_drafts) { 2927 const selected = self.data.selectedThread() orelse return; 2928 if (event_kind == kind and entryDraft(selected.*)) { 2929 try Data.removeDraft(self.session, allocator, author, &id); 2930 const route = draftsRoute(self.data.identity) orelse return; 2931 try self.session.navigate(route); 2932 return; 2933 } 2934 } 2935 2936 const io = self.session.io orelse return; 2937 const src = self.data.repo_source orelse return; 2938 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2939 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 2940 2941 switch (src.repo_kind) { 2942 inline else => |repo_kind| { 2943 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2944 defer any_repo.deinit(io, allocator); 2945 switch (any_repo) { 2946 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 2947 } 2948 }, 2949 } 2950 2951 const id_hex = std.fmt.bytesToHex(id, .lower); 2952 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2953 try self.session.navigate(route); 2954 } 2955 2956 // return to the list. 2957 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2958 root_focus.setFocus(self.listScroll(index).getFocus().id); 2959 } 2960 2961 // cross to the header tabs above the stack. 2962 fn focusHeader(self: *This, root_focus: *Focus) void { 2963 root_focus.setFocus(self.box.children.keys()[header_index]); 2964 } 2965 2966 pub fn clearGrid(self: *This) void { 2967 self.box.clearGrid(); 2968 } 2969 2970 pub fn getGrid(self: This) ?Grid { 2971 return self.box.getGrid(); 2972 } 2973 2974 pub fn getFocus(self: *This) *Focus { 2975 return self.box.getFocus(); 2976 } 2977 2978 // for the parent's "scroll up at the top jumps to the header" check: at the 2979 // top only while the header tabs hold focus, so up from the split 2980 // crosses into the tabs first. 2981 pub fn atTop(self: *This) bool { 2982 return self.headerActive(); 2983 } 2984 }; 2985 } 2986