╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1867 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1868 } 1869 const stack = &outer.children.values()[stack_index].widget.stack; 1870 1871 inline for (@typeInfo(Status).@"enum".field_values) |value| { 1872 var split = try initSplit(allocator, session, data, @fromBackingInt(@intCast(value)), false); 1873 errdefer split.deinit(allocator); 1874 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1875 } 1876 1877 // the labels view 1878 { 1879 var tf = try WordFlow.init(allocator); 1880 errdefer tf.deinit(allocator); 1881 var items: std.ArrayList(WordFlow.Item) = .empty; 1882 defer items.deinit(allocator); 1883 // when filtered, the first item clears the filter 1884 if (data.label.len != 0) 1885 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @fromBackingInt(@intCast(0)), "", "", data.search orelse "", assigneeFilter(data)) }); 1886 for (data.labels) |label| 1887 try items.append(allocator, .{ .text = label, .link = try labelLink(session.page_arena, data.identity, @fromBackingInt(@intCast(0)), label, data.search orelse "", assigneeFilter(data)) }); 1888 try tf.setItems(allocator, items.items); 1889 try stack.children.put(allocator, tf.getFocus().id, .{ .word_flow = tf }); 1890 } 1891 1892 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1893 // its place, prefilled with the selected thread's content. a viewer 1894 // who can't write in this tab gets the unauthorized view instead, 1895 // while local mode always writes. 1896 if (session.data.host_kind == .local or data.viewer != null) { 1897 if (confirmation(data.view)) |confirmation_kind| { 1898 const aa = session.page_arena.allocator(); 1899 const route = switch (confirmation_kind) { 1900 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1901 .publish => publishRoute(data.identity, data.selected_id), 1902 .merge => mergeRoute(data.identity, data.selected_id), 1903 } orelse return error.RouteTooLong; 1904 const page_url = try route.toUrl(session.page_arena); 1905 const action = try aa.print("form:{s}", .{page_url}); 1906 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1907 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1908 var buttons: [2]ConfirmationButton = undefined; 1909 buttons[0] = .{ .label = switch (confirmation_kind) { 1910 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1911 .publish => "publish patch", 1912 .merge => "merge patch", 1913 } }; 1914 var button_count: usize = 1; 1915 if (confirmation_kind == .merge) { 1916 if (comptime supports_merge) { 1917 const availability = if (data.selectedThread()) |entry| 1918 if (Data.canMerge(entry.*, session)) entry.mergeability else pch.Mergeability{} 1919 else 1920 pch.Mergeability{}; 1921 buttons[0].bottom_label = mergeBottomLabel(availability.source); 1922 if (availability.source != .clean) buttons[0].action = ""; 1923 buttons[1] = .{ 1924 .label = "squash and merge patch", 1925 .bottom_label = mergeBottomLabel(availability.squash), 1926 .action = if (availability.squash == .clean) try aa.print("{s}{s}/squash", .{ ui.submit_action_prefix, page_url }) else "", 1927 }; 1928 button_count += 1; 1929 } else unreachable; 1930 } 1931 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1932 errdefer center.deinit(allocator); 1933 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1934 } else if (data.view == .new_comment or data.view == .edit_comment) { 1935 const editing = data.view == .edit_comment; 1936 const route = if (editing) 1937 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1938 else 1939 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1940 const page_url = try route.toUrl(session.page_arena); 1941 const action = try session.page_arena.allocator().print("form:{s}", .{page_url}); 1942 const page = data.comment_page; 1943 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1944 const parent_route = if (editing) 1945 if (top_level) 1946 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1947 else if (page) |p| 1948 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1949 else 1950 null 1951 else if (page) |p| 1952 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1953 else 1954 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 1955 const parent_author: ui.Author = if (editing) 1956 if (top_level) 1957 if (data.selectedThread()) |entry| entry.author else .unknown 1958 else if (page) |p| 1959 p.selected.parent_author orelse .unknown 1960 else 1961 .unknown 1962 else if (page) |p| 1963 p.selected.author 1964 else if (data.selectedThread()) |entry| 1965 entry.author 1966 else 1967 .unknown; 1968 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 1969 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 1970 errdefer form.deinit(allocator); 1971 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1972 } else if (supports_conflicts and data.view == .resolve) { 1973 // on the web the page grows to the form's height and the 1974 // browser scrolls it; the terminal scrolls the form itself 1975 var form_widget: Widget = blk: { 1976 var form = try initResolveForm(allocator, session, data); 1977 errdefer form.deinit(allocator); 1978 break :blk if (session.is_terminal) 1979 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 1980 else 1981 .{ .box = form }; 1982 }; 1983 errdefer form_widget.deinit(allocator); 1984 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 1985 } else { 1986 const aa = session.page_arena.allocator(); 1987 const route = if (data.view == .edit) 1988 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 1989 else 1990 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 1991 const action = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}); 1992 const record = if (data.view == .edit) 1993 (if (data.selectedThread()) |entry| &entry.record else null) 1994 else 1995 null; 1996 var form = try initThreadForm(allocator, session, data, action, record); 1997 errdefer form.deinit(allocator); 1998 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1999 } 2000 } else { 2001 var message = try ui.Unauthorized.View.init(allocator); 2002 errdefer message.deinit(allocator); 2003 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 2004 } 2005 2006 if (supports_drafts) { 2007 var split = try initSplit(allocator, session, data, null, true); 2008 errdefer split.deinit(allocator); 2009 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 2010 } 2011 2012 // the conflicts split; its tab exists only when the repo has 2013 // conflicts, so skip the child too to keep the stack 1:1 with the 2014 // tabs. 2015 if (supports_conflicts and data.conflicts.count > 0) { 2016 var split = try initSplit(allocator, session, data, null, false); 2017 errdefer split.deinit(allocator); 2018 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 2019 } 2020 2021 // the stack starts on the page's view. 2022 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 2023 2024 // focus entering the view lands on the tabs first. 2025 outer.getFocus().child_id = outer.children.keys()[header_index]; 2026 2027 return .{ 2028 .box = outer, 2029 .data = data, 2030 .session = session, 2031 }; 2032 } 2033 2034 // the master-detail split showing `status`'s window, or the conflicts 2035 // window when null. 2036 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 2037 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2038 errdefer box.deinit(allocator); 2039 2040 const win = if (status_maybe) |status| data.window(status) else if (drafts and supports_drafts) &data.drafts else if (supports_conflicts) &data.conflicts else unreachable; 2041 2042 // the thread list (one focusable row per title), plus a "next" link that 2043 // reloads the page rooted at the following thread. 2044 { 2045 var list_scroll = blk: { 2046 var list_box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 2047 errdefer list_box.deinit(allocator); 2048 if (win.prev_id) |prev| 2049 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 2050 for (win.items) |entry| { 2051 const label: []const u8 = if (entry.comments.count > 0) 2052 try session.page_arena.allocator().print(" {d} comments ", .{entry.comments.count}) 2053 else 2054 ""; 2055 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 2056 } 2057 if (win.next_id) |next| 2058 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 2059 // select the window's first thread (past a leading "previous" 2060 // row) so its description shows on load. 2061 if (win.items.len > 0) 2062 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 2063 else if (list_box.children.count() > 0) 2064 list_box.getFocus().child_id = list_box.children.keys()[0]; 2065 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 2066 }; 2067 errdefer list_scroll.deinit(allocator); 2068 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 2069 } 2070 2071 // frame the detail pane inside the master-detail split, below the 2072 // way back to the list, shown only while the list is hidden 2073 { 2074 var frame = try wgt.Box(Widget).init(allocator, .{ .border = .hidden, .direction = .vert }); 2075 errdefer frame.deinit(allocator); 2076 frame.getFocus().mode = .mouse; 2077 { 2078 const label = try session.page_arena.allocator().print("← back to {s} list", .{thread_name}); 2079 var back = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 2080 errdefer back.deinit(allocator); 2081 back.getFocus().mode = .all; 2082 try frame.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null, .hidden = true }); 2083 } 2084 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 2085 errdefer detail_view.deinit(allocator); 2086 frame.getFocus().child_id = detail_view.getFocus().id; 2087 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 2088 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 2089 } 2090 2091 // the page opens on the list, unless the url names a thread 2092 box.getFocus().child_id = box.children.keys()[if (data.selected_id.len > 0) detail_index else list_index]; 2093 return box; 2094 } 2095 2096 // a thread form, plus the target branch for patches, prefilled from 2097 // `record` when given. its form: subtree makes the web renderer POST 2098 // the inputs to `action`'s route. 2099 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 2100 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2101 errdefer box.deinit(allocator); 2102 box.getFocus().kind = .{ .custom = action }; 2103 2104 const saved_form = session.formFeedback(feedback_tag); 2105 const saved_fields = if (saved_form) |saved| saved.fields else null; 2106 2107 { 2108 var title = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " title " }, .name = "title", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2109 errdefer title.deinit(allocator); 2110 title.getFocus().mode = .all; 2111 if (saved_fields) |saved| 2112 try title.setContent(allocator, saved.title) 2113 else if (record) |r| 2114 try title.setContent(allocator, r.event.title); 2115 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2116 box.getFocus().child_id = title.getFocus().id; 2117 } 2118 2119 { 2120 var labels = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " labels (separate with spaces) " }, .name = "labels", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2121 errdefer labels.deinit(allocator); 2122 labels.getFocus().mode = .all; 2123 if (saved_fields) |saved| 2124 try labels.setContent(allocator, saved.labels) 2125 else if (record) |r| 2126 try labels.setContent(allocator, r.event.labels); 2127 try box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .text_input = labels }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2128 } 2129 2130 { 2131 var description = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " description (markdown supported) " }, .name = "description", .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 2132 errdefer description.deinit(allocator); 2133 description.getFocus().mode = .all; 2134 if (saved_fields) |saved| 2135 try description.setContent(allocator, saved.description) 2136 else if (record) |r| 2137 try description.setContent(allocator, r.event.description); 2138 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 2139 } 2140 2141 if (supports_drafts and record == null) { 2142 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); 2143 errdefer source.deinit(allocator); 2144 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .patch_source = source }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2145 } 2146 2147 if (supports_drafts) { 2148 var target = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " target branch " }, .name = "target_branch", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2149 errdefer target.deinit(allocator); 2150 target.getFocus().mode = .all; 2151 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 2152 try target.setContent(allocator, content); 2153 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2154 } 2155 2156 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 2157 2158 return box; 2159 } 2160 2161 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) { 2162 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2163 errdefer box.deinit(allocator); 2164 box.getFocus().kind = .{ .custom = action }; 2165 2166 { 2167 var parent = try ui.authorBox(allocator, session.page_arena, author); 2168 errdefer parent.deinit(allocator); 2169 parent.options.top_label.text = " replying to "; 2170 parent.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 2171 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2172 box.getFocus().child_id = parent.getFocus().id; 2173 } 2174 2175 { 2176 var body = try wgt.TextInput.init(allocator, .{ 2177 .top_label = .{ .text = " comment (markdown supported) " }, 2178 .name = "body", 2179 .visible_width = null, 2180 .round_corners = true, 2181 .render_content = session.is_terminal, 2182 .multiline = true, 2183 .visible_height = 5, 2184 .scroll = .{ .fill = true }, 2185 }); 2186 errdefer body.deinit(allocator); 2187 body.getFocus().mode = .all; 2188 if (initial_body) |text| try body.setContent(allocator, text); 2189 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 2190 } 2191 2192 try addSubmitButtonLabeled(allocator, &box, "submit"); 2193 return box; 2194 } 2195 2196 const ConfirmationButton = struct { 2197 label: []const u8, 2198 bottom_label: []const u8 = "", 2199 action: []const u8 = "submit", 2200 }; 2201 2202 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 2203 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 2204 errdefer box.deinit(allocator); 2205 box.getFocus().kind = .{ .custom = action }; 2206 2207 var prompt = try wgt.Text.init(allocator, "are you sure?"); 2208 errdefer prompt.deinit(allocator); 2209 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 2210 2211 for (buttons, 0..) |options, index| { 2212 var button = try wgt.TextBox.init(allocator, options.label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = options.bottom_label } }); 2213 errdefer button.deinit(allocator); 2214 button.getFocus().mode = .all; 2215 button.getFocus().kind = .{ .custom = options.action }; 2216 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 2217 if (index == 0) box.getFocus().child_id = button.getFocus().id; 2218 } 2219 2220 return Center.init(allocator, .{ .box = box }); 2221 } 2222 2223 // a form's submit button, then a spacer absorbing the leftover 2224 // min-height the box hands its last child, so the button keeps its 2225 // natural height 2226 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 2227 { 2228 var submit = try SubmitButton.initLabeled(allocator, label); 2229 errdefer submit.deinit(allocator); 2230 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2231 } 2232 { 2233 var spacer = try Spacer.init(allocator); 2234 errdefer spacer.deinit(allocator); 2235 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 2236 } 2237 } 2238 2239 // the conflict resolve form. the "use this" links are navigations that 2240 // flip the url's theirs: pick, reloading the prefills; one submit 2241 // settles every conflict. the form: subtree makes the web renderer POST 2242 // to the resolve route. 2243 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 2244 const aa = session.page_arena.allocator(); 2245 // init only picks the resolve view once it has read the conflict 2246 const conflict = if (data.conflict) |*c| c else unreachable; 2247 2248 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2249 errdefer box.deinit(allocator); 2250 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 2251 box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 2252 2253 if (conflict.title) |*fc| { 2254 try addLabel(allocator, &box, "title conflict:"); 2255 try addFieldConflict(allocator, &box, session, data, "title", fc); 2256 } 2257 if (conflict.labels) |*fc| { 2258 try addLabel(allocator, &box, "labels conflict:"); 2259 try addFieldConflict(allocator, &box, session, data, "labels", fc); 2260 } 2261 if (comptime @hasField(Data.Conflict, "status")) { 2262 if (conflict.status) |*fc| { 2263 try addLabel(allocator, &box, "status conflict:"); 2264 try addAtomicConflict(allocator, &box, session, data, "status", fc); 2265 } 2266 } 2267 if (comptime @hasField(Data.Conflict, "revision")) { 2268 if (conflict.revision) |*fc| { 2269 try addLabel(allocator, &box, "revision conflict:"); 2270 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 2271 } 2272 if (conflict.target_branch) |*fc| { 2273 try addLabel(allocator, &box, "target branch conflict:"); 2274 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 2275 } 2276 } 2277 2278 if (conflict.description) |*desc| { 2279 try addLabel(allocator, &box, "description conflict:"); 2280 var hunk_index: usize = 0; 2281 for (desc.chunks, 0..) |*chunk, chunk_index| { 2282 switch (chunk.*) { 2283 .same => |text| { 2284 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .word }); 2285 errdefer tb.deinit(allocator); 2286 tb.getFocus().mode = .all; 2287 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2288 }, 2289 // an auto-resolved chunk: only one side changed it, so its 2290 // version stands, shown distinctly with its author 2291 .auto => |auto| { 2292 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 2293 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 2294 const label = switch (author) { 2295 .user_name, .email => |name| try aa.print(" {s} by {s} ", .{ verb, name }), 2296 .unknown => try aa.print(" {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 2297 }; 2298 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2299 errdefer tb.deinit(allocator); 2300 tb.getFocus().mode = .all; 2301 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2302 }, 2303 .conflict => |hunk| { 2304 // a blank row on each side sets the group apart from 2305 // the flowing shared and auto-resolved chunks 2306 if (chunk_index > 0) try addGap(allocator, &box); 2307 const name = try aa.print("d{d}", .{hunk_index}); 2308 hunk_index += 1; 2309 const picked_theirs = data.theirsPicked(name); 2310 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 2311 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 2312 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true }); 2313 errdefer resolution_input.deinit(allocator); 2314 resolution_input.getFocus().mode = .all; 2315 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 2316 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2317 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 2318 }, 2319 } 2320 } 2321 } 2322 2323 try addSubmitButtonLabeled(allocator, &box, "submit"); 2324 2325 box.getFocus().child_id = box.children.keys()[0]; 2326 return box; 2327 } 2328 2329 // one conflicted scalar field: both sides' version rows, then the 2330 // resolution input prefilled from the picked side. 2331 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2332 const aa = session.page_arena.allocator(); 2333 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2334 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2335 2336 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2337 errdefer resolution_input.deinit(allocator); 2338 resolution_input.getFocus().mode = .all; 2339 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2340 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2341 } 2342 2343 // atomic fields can only pick one complete side 2344 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2345 const aa = session.page_arena.allocator(); 2346 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2347 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2348 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2349 .border = .single, 2350 .round_corners = true, 2351 .wrap_kind = .word, 2352 .top_label = .{ .text = " resolution " }, 2353 }); 2354 errdefer selected.deinit(allocator); 2355 selected.getFocus().mode = .all; 2356 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2357 } 2358 2359 // a blank row setting a conflict group apart from its neighbors 2360 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2361 var text = try wgt.Text.init(allocator, " "); 2362 errdefer text.deinit(allocator); 2363 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2364 } 2365 2366 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2367 var label = try SectionLabel.init(allocator, content); 2368 errdefer label.deinit(allocator); 2369 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2370 } 2371 2372 // one side of a conflict: the "use this" link on the left, the version 2373 // itself as a labeled read-only box. 2374 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2375 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2376 errdefer row.deinit(allocator); 2377 2378 { 2379 const use_label = "use this"; 2380 var use = try wgt.TextBox.init(allocator, use_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 2381 errdefer use.deinit(allocator); 2382 use.getFocus().mode = .all; 2383 use.getFocus().kind = .{ .custom = link }; 2384 try row.children.put(allocator, use.getFocus().id, .{ 2385 .widget = .{ .text_box = use }, 2386 .rect = null, 2387 .min_size = .{ .width = use_label.len + 2, .height = null }, 2388 .max_size = .{ .width = use_label.len + 2, .height = null }, 2389 }); 2390 } 2391 2392 { 2393 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2394 errdefer tb.deinit(allocator); 2395 tb.getFocus().mode = .all; 2396 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2397 } 2398 2399 row.getFocus().child_id = row.children.keys()[0]; 2400 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2401 } 2402 2403 // the label of one side's version box, naming its author 2404 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2405 return switch (author) { 2406 .user_name, .email => |name| try aa.print(" edited by {s} ", .{name}), 2407 .unknown => if (ours) " current version " else " their version ", 2408 }; 2409 } 2410 2411 // the a: link that reloads the resolve page with `name` picked from ours 2412 // or theirs, reprefilling its resolution input 2413 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2414 const aa = session.page_arena.allocator(); 2415 var picks: std.ArrayList(u8) = .empty; 2416 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2417 while (it.next()) |pick| { 2418 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2419 if (picks.items.len > 0) try picks.append(aa, ','); 2420 try picks.appendSlice(aa, pick); 2421 } 2422 if (theirs) { 2423 if (picks.items.len > 0) try picks.append(aa, ','); 2424 try picks.appendSlice(aa, name); 2425 } 2426 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2427 return aa.print("a:{s}", .{try route.toUrl(session.page_arena)}); 2428 } 2429 2430 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2431 var row = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = bottom_label } }); 2432 errdefer row.deinit(allocator); 2433 row.getFocus().mode = .all; 2434 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2435 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2436 } 2437 2438 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2439 self.box.deinit(allocator); 2440 } 2441 2442 fn header(self: *This) *HeaderType { 2443 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2444 } 2445 2446 fn viewStack(self: *This) *wgt.Stack(Widget) { 2447 return &self.box.children.values()[stack_index].widget.stack; 2448 } 2449 2450 // `index`'s master-detail split inside the stack. 2451 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2452 return &self.viewStack().children.values()[index].box; 2453 } 2454 2455 // the labels view's flow inside the stack. 2456 fn labelsView(self: *This) *WordFlow { 2457 return &self.viewStack().children.values()[labels_view_index].word_flow; 2458 } 2459 2460 // the non-confirmation form inside the stack 2461 fn formBox(self: *This) ?*wgt.Box(Widget) { 2462 return switch (self.viewStack().children.values()[form_view_index]) { 2463 .box => |*box| box, 2464 // the resolve form sits inside a scroll on the terminal 2465 .scroll => |*scroll| &scroll.child.box, 2466 else => null, 2467 }; 2468 } 2469 2470 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2471 if (supports_conflicts and self.data.view == .resolve) return null; 2472 return switch (self.data.view) { 2473 .new_comment, .edit_comment => null, 2474 else => self.formBox(), 2475 }; 2476 } 2477 2478 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2479 for (form.children.values()) |*child| switch (child.widget) { 2480 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2481 else => {}, 2482 }; 2483 return error.MissingFormField; 2484 } 2485 2486 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2487 for (form.children.values()) |*child| { 2488 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2489 } 2490 return null; 2491 } 2492 2493 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2494 return switch (self.viewStack().children.values()[form_view_index]) { 2495 .center => |*center| switch (center.child.*) { 2496 .box => |*box| box, 2497 else => null, 2498 }, 2499 else => null, 2500 }; 2501 } 2502 2503 // the resolve form's scroll on the terminal (the web page scrolls itself) 2504 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2505 return switch (self.viewStack().children.values()[form_view_index]) { 2506 .scroll => |*scroll| scroll, 2507 else => null, 2508 }; 2509 } 2510 2511 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2512 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2513 } 2514 2515 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2516 return &self.listScroll(index).child.box; 2517 } 2518 2519 fn detailFrame(self: *This, index: usize) *wgt.Box(Widget) { 2520 return &self.resultsBox(index).children.values()[detail_index].widget.box; 2521 } 2522 2523 fn detail(self: *This, index: usize) *DetailType { 2524 return &@field(self.detailFrame(index).children.values()[detail_view_index].widget, Data.detail_widget_name); 2525 } 2526 2527 // the detail's way back to a list that isn't shown beside it 2528 fn backId(self: *This, index: usize) ?usize { 2529 const frame = self.detailFrame(index); 2530 return if (frame.children.values()[detail_back_index].hidden) null else frame.children.keys()[detail_back_index]; 2531 } 2532 2533 fn window(self: *This, index: usize) *const Window { 2534 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2535 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2536 return self.data.window(splitStatus(index)); 2537 } 2538 2539 fn stackSelectedIndex(self: *This) ?usize { 2540 const stack = self.viewStack(); 2541 const cid = stack.getFocus().child_id orelse return null; 2542 return stack.children.getIndex(cid); 2543 } 2544 2545 // the stack's selected master-detail split (null when the labels view or the 2546 // new-thread form shows). 2547 fn selectedSplitIndex(self: *This) ?usize { 2548 const idx = self.stackSelectedIndex() orelse return null; 2549 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2550 } 2551 2552 fn detailActive(self: *This, index: usize) bool { 2553 const rb = self.resultsBox(index); 2554 const cid = rb.getFocus().child_id orelse return false; 2555 return rb.children.getIndex(cid) == detail_index; 2556 } 2557 2558 fn headerActive(self: *This) bool { 2559 const cid = self.box.getFocus().child_id orelse return false; 2560 return self.box.children.getIndex(cid) == header_index; 2561 } 2562 2563 fn labelsViewActive(self: *This) bool { 2564 if (self.headerActive()) return false; 2565 return self.stackSelectedIndex() == labels_view_index; 2566 } 2567 2568 fn formViewActive(self: *This) bool { 2569 if (self.headerActive()) return false; 2570 return self.stackSelectedIndex() == form_view_index; 2571 } 2572 2573 // the selected thread's index, or null when a window-navigation row is 2574 // selected (a leading "previous" row shifts the thread rows down by one). 2575 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2576 const lb = self.listBox(index); 2577 const cid = lb.getFocus().child_id orelse return null; 2578 const idx = lb.children.getIndex(cid) orelse return null; 2579 const win = self.window(index); 2580 const lead: usize = if (win.prev_id != null) 1 else 0; 2581 if (idx < lead or idx - lead >= win.items.len) return null; 2582 return idx - lead; 2583 } 2584 2585 // swap the detail pane to the selected thread when it changes 2586 fn refreshDetail(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2587 const selected = self.selectedThreadIndex(index) orelse return; 2588 const entry = self.window(index).items[selected]; 2589 const changed = if (self.detail(index).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2590 if (!changed) return; 2591 try self.detail(index).setEntry(allocator, entry); 2592 // the way back links to the shown thread's own row, so both hosts 2593 // hand its press and enter to input instead of navigating 2594 const back = &self.detailFrame(index).children.values()[detail_back_index].widget.text_box; 2595 back.getFocus().kind = .{ .custom = try rowLink(self.session.page_arena, self.data, entry.id) }; 2596 } 2597 2598 // show the way back to the list only while the list is hidden 2599 fn showBack(self: *This, index: usize, shown: bool) void { 2600 const frame = self.detailFrame(index); 2601 frame.children.values()[detail_back_index].hidden = !shown; 2602 if (!shown and frame.getFocus().child_id == frame.children.keys()[detail_back_index]) 2603 frame.getFocus().child_id = frame.children.keys()[detail_view_index]; 2604 } 2605 2606 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2607 self.clearGrid(); 2608 2609 // each header tab maps 1:1 to a stack child by position 2610 if (self.header().getSelectedIndex()) |index| { 2611 const stack = self.viewStack(); 2612 stack.getFocus().child_id = stack.children.keys()[index]; 2613 } 2614 2615 if (self.selectedSplitIndex()) |i| { 2616 // whether the detail pane fits beside the list, which decides 2617 // the list cap below and whether the detail carries a way back 2618 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2619 try self.refreshDetail(allocator, i); 2620 self.showBack(i, !both_panes_fit); 2621 2622 // the selected list row shows a border (the focused TextBox 2623 // upgrades it to a double border itself); the rest stay borderless. 2624 const lb = self.listBox(i); 2625 for (lb.children.keys(), lb.children.values()) |id, *child| { 2626 switch (child.widget) { 2627 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2628 else => {}, 2629 } 2630 } 2631 2632 // cap the list at list_max_width only while the detail pane fits 2633 // beside it. the box drops the detail when the width can't hold 2634 // both minimums, so when it's that narrow we lift the cap and let 2635 // the list fill the whole width. 2636 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2637 2638 // stretch the detail pane across the rest of the width so it fills 2639 // the area rather than shrinking to its content; its scroll fills 2640 // the pane. 2641 if (constraint.max_size.width) |w| { 2642 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2643 } else { 2644 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2645 } 2646 } 2647 2648 // refresh the form inputs' entries in the session's focus-id -> input 2649 // map with this frame's addresses, so the web/wasm form handling can 2650 // find them by focus id 2651 if (self.threadForm()) |form| { 2652 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2653 (try formField(form, "title")).options.top_label.text = if (failure == .required_title) " title (required) " else " title "; 2654 if (comptime supports_drafts) { 2655 if (formWidget(form, .patch_source)) |source| { 2656 source.field().options.top_label.text = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2657 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2658 try submit.setLabel(allocator, switch (source.selectedKind()) { 2659 .fork => "submit draft", 2660 .branch => "submit patch", 2661 }); 2662 } 2663 (try formField(form, "target_branch")).options.top_label.text = 2664 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 "; 2665 } 2666 } 2667 if (self.formBox()) |form| { 2668 const inputs_arena = self.session.arena.allocator(); 2669 for (form.children.values()) |*child| switch (child.widget) { 2670 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2671 else => {}, 2672 }; 2673 } 2674 2675 try self.box.build(allocator, constraint, root_focus); 2676 } 2677 2678 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2679 // wheel input moves focus like the arrows 2680 const key: Key = switch (raw_key) { 2681 .scroll_up => .arrow_up, 2682 .scroll_down => .arrow_down, 2683 else => raw_key, 2684 }; 2685 2686 if (self.headerActive()) { 2687 // down from the sub-header re-enters the stack if the selected 2688 // view has something to focus; enter in the search box submits 2689 // the query; other keys move within the sub-header. 2690 if (inp.vertDirection(key) == .down) { 2691 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2692 .word_flow => |*tf| tf.text_boxes.items.len > 0, 2693 // nothing focusable in the unauthorized view 2694 .unauthorized => false, 2695 else => true, 2696 } else false; 2697 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2698 } else if (key == .enter) { 2699 if (try self.header().submittedText(allocator)) |submission| { 2700 defer allocator.free(submission.text); 2701 switch (submission.kind) { 2702 .search => try self.submitSearch(submission.text), 2703 .assignee => try self.submitAssignee(submission.text), 2704 } 2705 } 2706 } else { 2707 try self.header().input(allocator, key, root_focus); 2708 } 2709 return; 2710 } 2711 if (self.labelsViewActive()) { 2712 self.labelsViewInput(key, root_focus); 2713 return; 2714 } 2715 if (self.formViewActive()) { 2716 if (confirmation(self.data.view) != null) { 2717 try self.confirmationFormInput(allocator, key, root_focus); 2718 } else if (supports_conflicts and self.data.view == .resolve) { 2719 try self.resolveInput(allocator, key, root_focus); 2720 } else { 2721 try self.formInput(allocator, key, root_focus); 2722 } 2723 return; 2724 } 2725 const i = self.selectedSplitIndex() orelse return; 2726 if (self.detailActive(i)) { 2727 // the way back to the list, by enter or a click 2728 if (self.backId(i)) |id| { 2729 if (inp.activated(root_focus, id, key)) return self.focusList(i, root_focus); 2730 if (root_focus.grandchild_id == id) return switch (key) { 2731 .arrow_left => self.focusList(i, root_focus), 2732 .arrow_up => self.focusHeader(root_focus), 2733 .arrow_down => _ = self.detail(i).focusFirst(root_focus), 2734 else => {}, 2735 }; 2736 } 2737 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2738 .none => {}, 2739 .list => self.focusList(i, root_focus), 2740 .header => if (self.backId(i)) |id| root_focus.setFocus(id) else self.focusHeader(root_focus), 2741 } 2742 } else { 2743 try self.listInput(allocator, i, key, root_focus); 2744 } 2745 } 2746 2747 // navigate to the typed query's results, keeping the label filter and the 2748 // status list the page shows. an empty box clears a filtered page and 2749 // does nothing on any other one. 2750 fn submitSearch(self: *This, text: []const u8) !void { 2751 if (text.len == 0 and self.data.search == null) return; 2752 const selected = self.header().getSelectedIndex() orelse 0; 2753 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2754 var route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2755 const assignee = assigneeFilter(self.data); 2756 if (assignee.len != 0) route = route.withEncodedAssignee(assignee) orelse return; 2757 try self.session.navigate(route.withSearch(text) orelse return); 2758 } 2759 2760 // navigate to the issues assigned to the typed name or email, keeping 2761 // the label, the search and the status list. an empty box clears the 2762 // filter and does nothing on an unfiltered page. 2763 fn submitAssignee(self: *This, typed: []const u8) !void { 2764 const text = std.mem.trim(u8, typed, " \t"); 2765 if (text.len == 0 and assigneeFilter(self.data).len == 0) return; 2766 const selected = self.header().getSelectedIndex() orelse 0; 2767 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2768 var route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2769 if (self.data.search) |search| route = route.withSearch(search) orelse return; 2770 try self.session.navigate(route.withEncodedAssignee(try ui.urlEncodeRef(self.session.page_arena.allocator(), text)) orelse return); 2771 } 2772 2773 // arrow keys move the label selection; up from the top row crosses to the 2774 // header tabs. 2775 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2776 const tf = self.labelsView(); 2777 const cid = tf.focus.child_id orelse return; 2778 const cur = tf.indexOfFocusId(cid) orelse return; 2779 const count = tf.text_boxes.items.len; 2780 switch (key) { 2781 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2782 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2783 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2784 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2785 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2786 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2787 else => {}, 2788 } 2789 } 2790 2791 fn listInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 2792 // up/down (and the scroll wheel) move the selection a row; page up/down 2793 // jump a fixed amount. right/Enter cross into the detail pane. up from 2794 // the top row crosses into the header tabs. 2795 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2796 const lb = self.listBox(index); 2797 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2798 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2799 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2800 return; 2801 } 2802 switch (key) { 2803 .enter, .arrow_right => self.focusDetail(index, root_focus), 2804 // when the window is too narrow to lay out the detail pane beside 2805 // the list, a click on a row opens it like enter. the row was just 2806 // selected, so the detail is swapped to it ahead of the build. 2807 .mouse => |mouse| if (self.resultsBox(index).children.values()[detail_index].rect == null and widget.clickOnSelectedRow(self.listBox(index), root_focus, mouse)) { 2808 try self.refreshDetail(allocator, index); 2809 self.focusDetail(index, root_focus); 2810 }, 2811 else => {}, 2812 } 2813 } 2814 2815 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2816 const form = self.formBox() orelse return; 2817 const cid = form.getFocus().child_id orelse return; 2818 const cur = form.children.getIndex(cid) orelse return; 2819 const child = &form.children.values()[cur]; 2820 2821 if (child.widget == .text_input) { 2822 const text_input = &child.widget.text_input; 2823 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2824 return child.widget.input(allocator, key, root_focus); 2825 } 2826 2827 const on_submit = child.widget == .submit_button; 2828 switch (key) { 2829 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2830 root_focus.setFocus(form.children.keys()[i]) 2831 else 2832 self.focusHeader(root_focus), 2833 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2834 .enter => if (on_submit) return self.submitForm(allocator), 2835 .mouse => |mouse| if (on_submit) { 2836 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2837 }, 2838 else => {}, 2839 } 2840 try child.widget.input(allocator, key, root_focus); 2841 } 2842 2843 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2844 const form = self.confirmationForm() orelse return; 2845 const child_id = form.getFocus().child_id orelse return; 2846 const index = form.children.getIndex(child_id) orelse return; 2847 switch (key) { 2848 .arrow_up, .back_tab => if (index > 1) 2849 root_focus.setFocus(form.children.keys()[index - 1]) 2850 else 2851 self.focusHeader(root_focus), 2852 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2853 .enter => try self.submitConfirmation(allocator, index), 2854 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2855 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2856 try self.submitConfirmation(allocator, button_index); 2857 break; 2858 } 2859 }, 2860 else => {}, 2861 } 2862 } 2863 2864 // the multiline hunk inputs keep enter and any up/down that has a row to 2865 // move to; enter on a "use this" link is a navigation the host follows 2866 // before this runs. 2867 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2868 const form = self.formBox() orelse return; 2869 const cid = form.getFocus().child_id orelse return; 2870 const cur = form.children.getIndex(cid) orelse return; 2871 const child = &form.children.values()[cur]; 2872 2873 if (child.widget == .text_input) { 2874 const ti = &child.widget.text_input; 2875 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2876 return child.widget.input(allocator, key, root_focus); 2877 } 2878 2879 const on_submit = child.widget == .submit_button; 2880 2881 switch (key) { 2882 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2883 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2884 .arrow_left, .arrow_right => switch (child.widget) { 2885 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2886 else => try child.widget.input(allocator, key, root_focus), 2887 }, 2888 .enter => if (on_submit) try self.submitResolution(allocator), 2889 .mouse => |mouse| if (on_submit) { 2890 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2891 }, 2892 else => try child.widget.input(allocator, key, root_focus), 2893 } 2894 } 2895 2896 // a multiline input keeps enter and any up/down that has a row to move to 2897 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2898 return switch (key) { 2899 .enter => true, 2900 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2901 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2902 else => false, 2903 }; 2904 } 2905 2906 // the neighboring form control, skipping spacers and blank rows 2907 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2908 var i = cur; 2909 while (true) { 2910 if (down) { 2911 i += 1; 2912 if (i >= form.children.count()) return null; 2913 } else { 2914 if (i == 0) return null; 2915 i -= 1; 2916 } 2917 const child = &form.children.values()[i]; 2918 if (child.hidden) continue; 2919 switch (child.widget) { 2920 .spacer, .text => continue, 2921 else => return i, 2922 } 2923 } 2924 } 2925 2926 // focus the resolve-form child at `i` (a version row focuses its selected 2927 // child) and keep it visible on the terminal 2928 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2929 const child = &form.children.values()[i]; 2930 switch (child.widget) { 2931 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2932 else => root_focus.setFocus(form.children.keys()[i]), 2933 } 2934 if (self.session.is_terminal) { 2935 if (self.resolveScroll()) |sc| { 2936 if (child.rect) |rect| sc.scrollToRect(rect); 2937 } 2938 } 2939 } 2940 2941 // re-emit the conflicted thread's event with the resolved content; any 2942 // event on the thread settles its conflict entry. this is the terminal 2943 // path; the web posts the form to the resolve route. 2944 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2945 if (comptime wasm) return; 2946 const io = self.session.io orelse return; 2947 const src = self.data.repo_source orelse return; 2948 const entry = self.data.selectedThread() orelse return; 2949 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2950 const form = self.formBox() orelse return; 2951 2952 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2953 var title: ?[]u8 = null; 2954 var labels: ?[]u8 = null; 2955 var hunks: std.ArrayList([]const u8) = .empty; 2956 defer { 2957 if (title) |t| allocator.free(t); 2958 if (labels) |t| allocator.free(t); 2959 for (hunks.items) |h| allocator.free(h); 2960 hunks.deinit(allocator); 2961 } 2962 for (form.children.values()) |*child| switch (child.widget) { 2963 .text_input => |*ti| { 2964 const text = try ti.text(allocator); 2965 errdefer allocator.free(text); 2966 if (std.mem.eql(u8, ti.options.name, "title")) { 2967 title = text; 2968 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2969 labels = text; 2970 } else { 2971 try hunks.append(allocator, text); 2972 } 2973 }, 2974 else => {}, 2975 }; 2976 2977 const id_bytes = try evt.parseEventId(entry.id); 2978 2979 switch (src.repo_kind) { 2980 inline else => |repo_kind| { 2981 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2982 defer any_repo.deinit(io, allocator); 2983 switch (any_repo) { 2984 inline else => |*repo| { 2985 var resolution: Event.Resolve = .{ 2986 .title = title, 2987 .labels = labels, 2988 .hunks = hunks.items, 2989 }; 2990 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2991 const change = Event.Update{ .resolve = resolution }; 2992 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2993 // leave the form up for correction 2994 error.InvalidFields => return, 2995 else => |e| return e, 2996 }; 2997 }, 2998 } 2999 }, 3000 } 3001 3002 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3003 try self.session.navigate(route); 3004 } 3005 3006 // commit a new or edited comment and navigate to its permalink. this is the 3007 // terminal path; the web posts the form to its comment route. 3008 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 3009 if (comptime wasm) return; 3010 const io = self.session.io orelse return; 3011 const src = self.data.repo_source orelse return; 3012 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3013 const author = actor.author; 3014 const form = self.formBox() orelse return; 3015 const body_input = try formField(form, "body"); 3016 const body = try body_input.text(allocator); 3017 defer allocator.free(body); 3018 if (!evt.Comment.fieldsValid(body)) return; 3019 3020 var thread_id: [evt.event_id_size * 2]u8 = undefined; 3021 @memcpy(&thread_id, self.data.selected_id); 3022 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 3023 var parent: [evt.event_id_size * 2]u8 = undefined; 3024 @memcpy(&parent, self.data.comment_id); 3025 break :blk parent; 3026 }; 3027 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3028 const editing = self.data.view == .edit_comment; 3029 if (editing) { 3030 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3031 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3032 } 3033 switch (src.repo_kind) { 3034 inline else => |repo_kind| { 3035 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3036 defer any_repo.deinit(io, allocator); 3037 switch (any_repo) { 3038 inline else => |*repo| if (editing) { 3039 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3040 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3041 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3042 } else { 3043 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &parent_id, body, author); 3044 }, 3045 } 3046 }, 3047 } 3048 3049 body_input.clear(allocator); 3050 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3051 try self.session.navigate(route); 3052 } 3053 3054 // terminal form submissions; the web posts to the forms' routes instead 3055 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3056 switch (self.data.view) { 3057 .new_comment, .edit_comment => try self.submitComment(allocator), 3058 .edit => try self.submitEditedThread(allocator), 3059 else => try self.submitNewThread(allocator), 3060 } 3061 } 3062 3063 // commit the new thread to the repo's events branch and navigate to it. 3064 // this is the terminal path; the web posts the form to the thread route, 3065 // so the wasm side never runs (or compiles) the repo access below. 3066 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3067 if (comptime wasm) return; 3068 const io = self.session.io orelse return; 3069 const src = self.data.repo_source orelse return; 3070 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3071 3072 const form = self.threadForm() orelse return; 3073 const title_input = try formField(form, "title"); 3074 const labels_input = try formField(form, "labels"); 3075 const description_input = try formField(form, "description"); 3076 3077 const title = try title_input.text(allocator); 3078 defer allocator.free(title); 3079 const labels = try labels_input.text(allocator); 3080 defer allocator.free(labels); 3081 const description = try description_input.text(allocator); 3082 defer allocator.free(description); 3083 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3084 defer if (comptime supports_drafts) allocator.free(target_branch); 3085 3086 if (!Event.fieldsValid(title, labels)) { 3087 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3088 return; 3089 } 3090 3091 if (comptime supports_drafts) { 3092 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3093 const source_branch = switch (source.selectedKind()) { 3094 .fork => null, 3095 .branch => try source.field().text(allocator), 3096 }; 3097 defer if (source_branch) |branch| allocator.free(branch); 3098 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3099 const failure = FeedbackFailure.fromError(err) orelse return err; 3100 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3101 return; 3102 }; 3103 title_input.clear(allocator); 3104 labels_input.clear(allocator); 3105 description_input.clear(allocator); 3106 (try formField(form, "target_branch")).clear(allocator); 3107 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3108 try self.session.navigate(route); 3109 return; 3110 } 3111 3112 var id_bytes: [evt.event_id_size]u8 = undefined; 3113 io.random(&id_bytes); 3114 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3115 3116 const event = evt.EventWithId{ 3117 .id = event_id_hex, 3118 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3119 .author = actor.author, 3120 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3121 .title = title, 3122 .description = description, 3123 .labels = labels, 3124 }), 3125 }; 3126 3127 switch (src.repo_kind) { 3128 inline else => |repo_kind| { 3129 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3130 defer any_repo.deinit(io, allocator); 3131 switch (any_repo) { 3132 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3133 } 3134 }, 3135 } 3136 3137 // wipe the form so a return visit starts fresh 3138 title_input.clear(allocator); 3139 labels_input.clear(allocator); 3140 description_input.clear(allocator); 3141 3142 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", &event_id_hex) orelse return; 3143 try self.session.navigate(route); 3144 } 3145 3146 // re-emit the selected thread's event with the form's content (status 3147 // preserved), then reload the thread's page. this is the terminal path; 3148 // the web posts the form to the edit route. 3149 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3150 if (comptime wasm) return; 3151 const io = self.session.io orelse return; 3152 const entry = self.data.selectedThread() orelse return; 3153 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3154 const author = actor.author; 3155 3156 const form = self.threadForm() orelse return; 3157 const title_input = try formField(form, "title"); 3158 const labels_input = try formField(form, "labels"); 3159 const description_input = try formField(form, "description"); 3160 3161 const title = try title_input.text(allocator); 3162 defer allocator.free(title); 3163 const labels = try labels_input.text(allocator); 3164 defer allocator.free(labels); 3165 const description = try description_input.text(allocator); 3166 defer allocator.free(description); 3167 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3168 defer if (comptime supports_drafts) allocator.free(target_branch); 3169 3170 if (!Event.fieldsValid(title, labels)) { 3171 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3172 return; 3173 } 3174 const src = self.data.repo_source orelse return; 3175 3176 if (comptime supports_drafts) { 3177 if (entryDraft(entry.*)) { 3178 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3179 const failure = FeedbackFailure.fromError(err) orelse return err; 3180 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3181 return; 3182 }; 3183 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3184 try self.session.navigate(route); 3185 return; 3186 } 3187 } 3188 3189 const id_bytes = try evt.parseEventId(entry.id); 3190 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3191 3192 switch (src.repo_kind) { 3193 inline else => |repo_kind| { 3194 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3195 defer any_repo.deinit(io, allocator); 3196 switch (any_repo) { 3197 inline else => |*repo| if (supports_drafts) { 3198 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3199 .title = title, 3200 .labels = labels, 3201 .description = description, 3202 .target_branch = target_branch, 3203 } }, author) catch |err| { 3204 const failure = FeedbackFailure.fromError(err) orelse return err; 3205 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3206 return; 3207 }; 3208 } else if (has_status) 3209 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3210 .title = title, 3211 .labels = labels, 3212 .description = description, 3213 } }, author) 3214 else 3215 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3216 } 3217 }, 3218 } 3219 3220 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3221 try self.session.navigate(route); 3222 } 3223 3224 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3225 const aa = self.session.arena.allocator(); 3226 const source = if (supports_drafts) blk: { 3227 const form = self.threadForm() orelse break :blk null; 3228 break :blk formWidget(form, .patch_source); 3229 } else null; 3230 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3231 .failure = failure, 3232 .fields = if (comptime supports_drafts) .{ 3233 .title = try aa.dupe(u8, title), 3234 .labels = try aa.dupe(u8, labels), 3235 .description = try aa.dupe(u8, description), 3236 .target_branch = try aa.dupe(u8, target_branch), 3237 .source_branch = if (source) |value| try value.field().text(aa) else "", 3238 .source_kind = if (source) |value| value.selectedKind() else .fork, 3239 } else .{ 3240 .title = try aa.dupe(u8, title), 3241 .labels = try aa.dupe(u8, labels), 3242 .description = try aa.dupe(u8, description), 3243 }, 3244 }); 3245 } 3246 3247 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3248 switch (confirmation(self.data.view) orelse return) { 3249 .publish => if (comptime supports_drafts) { 3250 if (comptime wasm) return; 3251 const entry = self.data.selectedThread() orelse return; 3252 if (!entryDraft(entry.*)) return; 3253 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3254 try self.data.publishDraft(self.session, allocator, author, entry.id); 3255 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", entry.id) orelse return; 3256 try self.session.navigate(route); 3257 } else unreachable, 3258 .merge => if (comptime supports_merge) { 3259 if (comptime wasm) return; 3260 const entry = self.data.selectedThread() orelse return; 3261 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3262 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3263 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3264 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3265 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3266 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3267 try self.session.navigate(route); 3268 return; 3269 }, 3270 else => return err, 3271 }; 3272 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3273 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 3274 try self.session.navigate(route); 3275 } else unreachable, 3276 .remove => try self.submitRemove(allocator), 3277 } 3278 } 3279 3280 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3281 const comment = self.data.comment_id.len != 0; 3282 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3283 const id = evt.parseEventId(event_id) catch return; 3284 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3285 } 3286 3287 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3288 if (comptime wasm) return; 3289 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3290 const author = actor.author; 3291 3292 if (comptime supports_drafts) { 3293 const selected = self.data.selectedThread() orelse return; 3294 if (event_kind == kind and entryDraft(selected.*)) { 3295 try Data.removeDraft(self.session, allocator, author, &id); 3296 const route = draftsRoute(self.data.identity) orelse return; 3297 try self.session.navigate(route); 3298 return; 3299 } 3300 } 3301 3302 const io = self.session.io orelse return; 3303 const src = self.data.repo_source orelse return; 3304 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3305 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3306 3307 switch (src.repo_kind) { 3308 inline else => |repo_kind| { 3309 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3310 defer any_repo.deinit(io, allocator); 3311 switch (any_repo) { 3312 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 3313 } 3314 }, 3315 } 3316 3317 const id_hex = std.fmt.bytesToHex(id, .lower); 3318 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3319 try self.session.navigate(route); 3320 } 3321 3322 // enter the detail pane, landing on the way back to the list when it 3323 // has one. selecting the frame's child (then focusing the frame) also 3324 // handles the too-narrow case where the pane isn't laid out yet. 3325 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3326 if (self.backId(index) == null) { 3327 _ = self.detail(index).focusFirst(root_focus); 3328 return; 3329 } 3330 const frame = self.detailFrame(index); 3331 frame.getFocus().child_id = frame.children.keys()[detail_back_index]; 3332 root_focus.setFocus(frame.getFocus().id); 3333 } 3334 3335 // return to the list. 3336 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3337 root_focus.setFocus(self.listScroll(index).getFocus().id); 3338 } 3339 3340 // cross to the header tabs above the stack. 3341 fn focusHeader(self: *This, root_focus: *Focus) void { 3342 root_focus.setFocus(self.box.children.keys()[header_index]); 3343 } 3344 3345 pub fn clearGrid(self: *This) void { 3346 self.box.clearGrid(); 3347 } 3348 3349 pub fn getGrid(self: This) ?Grid { 3350 return self.box.getGrid(); 3351 } 3352 3353 pub fn getFocus(self: *This) *Focus { 3354 return self.box.getFocus(); 3355 } 3356 3357 // for the parent's "scroll up at the top jumps to the header" check: at the 3358 // top only while the header tabs hold focus, so up from the split 3359 // crosses into the tabs first. 3360 pub fn atTop(self: *This) bool { 3361 return self.headerActive(); 3362 } 3363 }; 3364 } 3365