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