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