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