╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
772 // on an edit page, register the issue's content as the inputs' 773 // page-constant initial values, which the web overlay renders 774 // into them. 775 if (self.data.view == .edit) { 776 if (self.data.selectedIssue()) |entry| { 777 const fields = form.children.values(); 778 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.issue.title); 779 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.issue.tags); 780 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.issue.description); 781 } 782 } 783 784 try self.box.build(allocator, constraint, root_focus); 785 } 786 787 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 788 const sel = self.selectedIssueIndex(index) orelse return; 789 if (self.detailed_index[index]) |d| if (d == sel) return; 790 try self.populateDetail(allocator, index, sel); 791 self.detailed_index[index] = sel; 792 } 793 794 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 795 const entry = self.window(index).issues[sel]; 796 const inner = self.detailInner(index); 797 // the /description page replaces its issue's detail with a back link 798 // and the whole description; other issues keep their normal detail. 799 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 800 801 for (inner.children.values()) |*child| child.widget.deinit(allocator); 802 inner.children.clearAndFree(allocator); 803 inner.getFocus().child_id = null; 804 805 // the tool row: the open/close and edit buttons; the description page 806 // shows none. 807 { 808 const row = self.toolRow(index); 809 for (row.children.values()) |*child| child.widget.deinit(allocator); 810 row.children.clearAndFree(allocator); 811 row.getFocus().child_id = null; 812 row.getFocus().kind = .container; 813 } 814 if (!description_page) { 815 const action: []const u8 = switch (entry.issue.status) { 816 .open => "close", 817 .closed => "open", 818 }; 819 const row = self.toolRow(index); 820 const pa = self.session.page_arena.allocator(); 821 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 822 try std.fmt.allocPrint(pa, "form:/issues/{s}/{s}", .{ entry.id, action }) 823 else 824 try std.fmt.allocPrint(pa, "form:/repo/{s}/issues/{s}/{s}", .{ self.data.identity, entry.id, action }) }; 825 826 { 827 var spacer = try ui.Spacer.init(allocator); 828 errdefer spacer.deinit(allocator); 829 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 830 } 831 832 { 833 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 834 errdefer button.deinit(allocator); 835 button.getFocus().focusable = true; 836 // the renderer distinguishes plain clickables from buttons that 837 // should POST to a server route by this kind. 838 button.getFocus().kind = .{ .custom = "submit" }; 839 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 840 } 841 842 // the edit button links to the issue's edit page. 843 { 844 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 845 errdefer button.deinit(allocator); 846 button.getFocus().focusable = true; 847 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 848 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 849 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 850 } 851 852 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 853 } 854 855 if (description_page) { 856 // the back link, in the title slot so the pane's input handling 857 // applies to it unchanged. 858 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 859 errdefer tb.deinit(allocator); 860 tb.getFocus().focusable = true; 861 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.status, "", entry.id) }; 862 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 863 } else { 864 // the issue's title as a focusable word-wrapped text box. 865 { 866 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 867 errdefer tb.deinit(allocator); 868 tb.getFocus().focusable = true; 869 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 870 } 871 872 // the issue's tags, each linking to this status's list filtered to 873 // that tag. 874 { 875 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 876 defer items.deinit(allocator); 877 var tag_iter = evt.Issue.tagIterator(entry.issue.tags); 878 while (tag_iter.next()) |tag| { 879 if (tag.len == 0) continue; 880 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, splitStatus(index), tag) }); 881 } 882 if (items.items.len > 0) { 883 var tf = try ui.TagFlow.init(allocator); 884 errdefer tf.deinit(allocator); 885 try tf.setItems(allocator, items.items); 886 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 887 } 888 } 889 } 890 891 // the description as a focusable word-wrapped text box. past the limit 892 // it's cut at the last whole line and links to the /description page, 893 // which shows the whole thing. 894 self.description_id[index] = blk: { 895 const whole = entry.issue.description; 896 const cut_short = !description_page and whole.len > max_description_size; 897 const shown = if (cut_short) 898 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 899 else if (whole.len == 0) 900 "(no description)" 901 else 902 whole; 903 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 904 .border_style = .single, 905 .rounded_corners = true, 906 .wrap_kind = .word, 907 .label = " description ", 908 .bottom_label = if (cut_short) " click or press enter to see more " else "", 909 }); 910 errdefer tb.deinit(allocator); 911 tb.getFocus().focusable = true; 912 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 913 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 914 break :blk tb.getFocus().id; 915 }; 916 917 // select the title by default 918 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 919 920 // reset the scroll to the top for the newly-shown issue: directly on the 921 // terminal (the wasm offset), and via a version bump on the web (so the 922 // renderer's scroll id changes and JS drops the preserved position). 923 const sc = self.detailScroll(index); 924 sc.x = 0; 925 sc.y = 0; 926 sc.getFocus().version +%= 1; 927 } 928 929 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 930 if (self.headerActive()) { 931 // down from the tabs re-enters the stack if the selected view has 932 // something to focus; other keys move the tabs. 933 if (inp.vertDirection(key) == .down) { 934 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 935 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 936 else => true, 937 } else false; 938 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 939 } else { 940 try self.header().input(allocator, key, root_focus); 941 } 942 return; 943 } 944 if (self.tagsViewActive()) { 945 self.tagsViewInput(key, root_focus); 946 return; 947 } 948 if (self.formViewActive()) { 949 try self.formInput(allocator, key, root_focus); 950 return; 951 } 952 const i = self.selectedSplitIndex() orelse return; 953 if (self.detailActive(i)) { 954 try self.detailInput(allocator, i, key, root_focus); 955 } else { 956 try self.listInput(i, key, root_focus); 957 } 958 } 959 960 // arrow keys move the tag selection; up from the top row crosses to the 961 // header tabs. 962 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 963 const tf = self.tagsView(); 964 const cid = tf.focus.child_id orelse return; 965 const cur = tf.indexOfFocusId(cid) orelse return; 966 const count = tf.text_boxes.items.len; 967 switch (key) { 968 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 969 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 970 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 971 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 972 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 973 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 974 else => {}, 975 } 976 } 977 978 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 979 // up/down (and the scroll wheel) move the selection a row; page up/down 980 // jump a fixed amount. right/Enter cross into the detail pane. up from 981 // the top row crosses into the header tabs. 982 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 983 const lb = self.listBox(index); 984 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 985 if (delta < 0 and at_top) return self.focusHeader(root_focus); 986 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 987 return; 988 } 989 switch (key) { 990 .enter, .arrow_right => try self.focusDetail(index, root_focus), 991 else => {}, 992 } 993 } 994 995 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 996 if (self.toolRowFocused(index)) { 997 try self.toolRowInput(allocator, index, key, root_focus); 998 } else if (self.titleFocused(index)) { 999 try self.titleInput(index, key, root_focus); 1000 } else if (self.tagsFocused(index)) { 1001 try self.tagsInput(index, key, root_focus); 1002 } else { 1003 try self.descriptionInput(index, key, root_focus); 1004 } 1005 } 1006 1007 // the tool row: enter or a click on the open/close button flips the 1008 // issue's status; the edit button is an a: link the host follows. 1009 // arrows cross between the buttons and to the neighboring widgets. 1010 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1011 const row = self.toolRow(index); 1012 const on_edit = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) == edit_in_row_index else false; 1013 switch (key) { 1014 .arrow_left => if (on_edit) root_focus.setFocus(row.children.keys()[button_in_row_index]) else try self.focusList(index, root_focus), 1015 .arrow_right => if (!on_edit) root_focus.setFocus(row.children.keys()[edit_in_row_index]), 1016 .arrow_up => self.focusHeader(root_focus), 1017 .arrow_down => self.focusTitle(index, root_focus), 1018 .enter => if (!on_edit) try self.toggleIssueStatus(allocator, index), 1019 .mouse => |mouse| switch (mouse.action) { 1020 .scroll => |dir| { 1021 const sc = self.detailScroll(index); 1022 sc.y += if (dir == .up) @as(isize, -1) else 1; 1023 sc.clampToContent(); 1024 }, 1025 else => if (self.statusButton(index)) |button| { 1026 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1027 }, 1028 }, 1029 else => {}, 1030 } 1031 } 1032 1033 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1034 const sc = self.detailScroll(index); 1035 switch (key) { 1036 .arrow_left => try self.focusList(index, root_focus), 1037 .arrow_up => self.focusToolRow(index, root_focus), 1038 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1039 .mouse => |mouse| switch (mouse.action) { 1040 .scroll => |dir| { 1041 sc.y += if (dir == .up) @as(isize, -1) else 1; 1042 sc.clampToContent(); 1043 }, 1044 else => {}, 1045 }, 1046 else => {}, 1047 } 1048 } 1049 1050 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1051 const sc = self.detailScroll(index); 1052 switch (key) { 1053 .arrow_left => return self.focusList(index, root_focus), 1054 // once the scroll can't move further, cross into the tags (or the 1055 // open/close button when the issue has none). 1056 .arrow_up => { 1057 const before = sc.y; 1058 sc.y -= 1; 1059 sc.clampToContent(); 1060 if (sc.y == before) { 1061 if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else self.focusTitle(index, root_focus); 1062 } 1063 return; 1064 }, 1065 .arrow_down => sc.y += 1, 1066 .page_up => sc.y -= 10, 1067 .page_down => sc.y += 10, 1068 .home => sc.y = 0, 1069 .end => sc.y = std.math.maxInt(isize), 1070 .mouse => |mouse| switch (mouse.action) { 1071 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 1072 else => {}, 1073 }, 1074 else => return, 1075 } 1076 sc.clampToContent(); 1077 } 1078 1079 // up/down (and tab/shift+tab) move between a form's fields; up from the 1080 // title crosses into the header tabs. the multiline description keeps 1081 // enter and any up/down that has a row to move to. 1082 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1083 const form = self.issueForm(); 1084 const cid = form.getFocus().child_id orelse return; 1085 const cur = form.children.getIndex(cid) orelse return; 1086 1087 if (cur == description_field_index) { 1088 const child = &form.children.values()[cur]; 1089 const description = &child.widget.text_input; 1090 switch (key) { 1091 .enter => return child.widget.input(allocator, key, root_focus), 1092 .arrow_up => if (!try description.cursorOnFirstRow(allocator)) 1093 return child.widget.input(allocator, key, root_focus), 1094 .arrow_down => if (!try description.cursorOnLastRow(allocator)) 1095 return child.widget.input(allocator, key, root_focus), 1096 else => {}, 1097 } 1098 } 1099 1100 switch (key) { 1101 .arrow_up, .back_tab => if (cur > 0) 1102 root_focus.setFocus(form.children.keys()[cur - 1]) 1103 else 1104 self.focusHeader(root_focus), 1105 .arrow_down, .tab => if (cur < submit_field_index) { 1106 root_focus.setFocus(form.children.keys()[cur + 1]); 1107 }, 1108 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1109 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 1110 try self.submitForm(allocator); 1111 }, 1112 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1113 } 1114 } 1115 1116 // the terminal submit paths: the new form commits a new-issue event, the 1117 // edit form re-emits the selected issue's event with the form's content. 1118 // the web posts the forms to their routes instead. 1119 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 1120 if (self.data.view == .edit) { 1121 try self.submitEditedIssue(allocator); 1122 } else { 1123 try self.submitNewIssue(allocator); 1124 } 1125 } 1126 1127 // commit the new issue to the repo's events branch and navigate to it. 1128 // this is the terminal path; the web posts the form to the issue route, 1129 // so the wasm side never runs (or compiles) the repo access below. 1130 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 1131 if (comptime wasm) return; 1132 const io = self.session.io orelse return; 1133 const src = self.data.repo_source orelse return; 1134 1135 const form = self.issueForm(); 1136 const title_input = &form.children.values()[title_field_index].widget.text_input; 1137 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1138 const description_input = &form.children.values()[description_field_index].widget.text_input; 1139 1140 const title = try title_input.text(allocator); 1141 defer allocator.free(title); 1142 const tags = try tags_input.text(allocator); 1143 defer allocator.free(tags); 1144 const description = try description_input.text(allocator); 1145 defer allocator.free(description); 1146 1147 if (!evt.Issue.fieldsValid(title, tags)) return; 1148 1149 var id_bytes: [evt.event_id_size]u8 = undefined; 1150 io.random(&id_bytes); 1151 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1152 1153 const event = evt.EventWithId{ 1154 .id = event_id_hex, 1155 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1156 .event = .{ .issue = .{ 1157 .title = title, 1158 .description = description, 1159 .tags = tags, 1160 } }, 1161 }; 1162 1163 switch (src.repo_kind) { 1164 inline else => |repo_kind| { 1165 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1166 defer any_repo.deinit(io, allocator); 1167 switch (any_repo) { 1168 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1169 } 1170 }, 1171 } 1172 1173 // wipe the form so a return visit starts fresh 1174 title_input.clear(allocator); 1175 tags_input.clear(allocator); 1176 description_input.clear(allocator); 1177 1178 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1179 try self.session.navigate(route); 1180 } 1181 1182 // re-emit the selected issue's event with the form's content (status 1183 // preserved), then reload the issue's page. this is the terminal path; 1184 // the web posts the form to the edit route. 1185 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 1186 if (comptime wasm) return; 1187 const io = self.session.io orelse return; 1188 const src = self.data.repo_source orelse return; 1189 const entry = self.data.selectedIssue() orelse return; 1190 1191 const form = self.issueForm(); 1192 const title_input = &form.children.values()[title_field_index].widget.text_input; 1193 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1194 const description_input = &form.children.values()[description_field_index].widget.text_input; 1195 1196 const title = try title_input.text(allocator); 1197 defer allocator.free(title); 1198 const tags = try tags_input.text(allocator); 1199 defer allocator.free(tags); 1200 const description = try description_input.text(allocator); 1201 defer allocator.free(description); 1202 1203 if (!evt.Issue.fieldsValid(title, tags)) return; 1204 1205 var id_bytes: [evt.event_id_size]u8 = undefined; 1206 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1207 1208 switch (src.repo_kind) { 1209 inline else => |repo_kind| { 1210 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1211 defer any_repo.deinit(io, allocator); 1212 switch (any_repo) { 1213 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 1214 .title = title, 1215 .tags = tags, 1216 .description = description, 1217 } }), 1218 } 1219 }, 1220 } 1221 1222 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.status, "", entry.id) orelse return; 1223 try self.session.navigate(route); 1224 } 1225 1226 // flip the shown issue's status by re-emitting its event, then reload the 1227 // page rooted at the issue so the view reflects the change. this is the 1228 // terminal path; the web posts the button's form to the status route. 1229 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1230 if (comptime wasm) return; 1231 const io = self.session.io orelse return; 1232 const src = self.data.repo_source orelse return; 1233 const sel = self.detailed_index[index] orelse return; 1234 const entry = self.window(index).issues[sel]; 1235 1236 const status: evt.Issue.Status = switch (entry.issue.status) { 1237 .open => .closed, 1238 .closed => .open, 1239 }; 1240 1241 var id_bytes: [evt.event_id_size]u8 = undefined; 1242 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1243 1244 switch (src.repo_kind) { 1245 inline else => |repo_kind| { 1246 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1247 defer any_repo.deinit(io, allocator); 1248 switch (any_repo) { 1249 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }), 1250 } 1251 }, 1252 } 1253 1254 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 1255 try self.session.navigate(route); 1256 } 1257 1258 // arrow keys move the tag selection; at the flow's edges focus crosses to 1259 // the neighboring widgets. 1260 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1261 const tf = self.tagFlow(index) orelse return; 1262 const cid = tf.focus.child_id orelse return; 1263 const cur = tf.indexOfFocusId(cid) orelse return; 1264 const count = tf.text_boxes.items.len; 1265 const sc = self.detailScroll(index); 1266 switch (key) { 1267 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1268 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1269 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusTitle(index, root_focus), 1270 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1271 .home => self.focusTag(index, tf, root_focus, 0), 1272 .end => self.focusTag(index, tf, root_focus, count - 1), 1273 .mouse => |mouse| switch (mouse.action) { 1274 .scroll => |dir| { 1275 sc.y += if (dir == .up) @as(isize, -1) else 1; 1276 sc.clampToContent(); 1277 }, 1278 else => {}, 1279 }, 1280 else => {}, 1281 } 1282 } 1283 1284 const button_in_row_index: usize = 1; 1285 const edit_in_row_index: usize = 2; 1286 const title_child_index: usize = 0; 1287 const tags_child_index: usize = 1; 1288 1289 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1290 const inner = self.detailInner(index); 1291 if (inner.children.count() <= tags_child_index) return null; 1292 return switch (inner.children.values()[tags_child_index].widget) { 1293 .tag_flow => |*tf| tf, 1294 else => null, 1295 }; 1296 } 1297 1298 fn tagsFocused(self: *View, index: usize) bool { 1299 const inner = self.detailInner(index); 1300 const cid = inner.getFocus().child_id orelse return false; 1301 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1302 } 1303 1304 // the open/close button inside the detail frame's tool row. 1305 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1306 const row = self.toolRow(index); 1307 if (row.children.count() == 0) return null; 1308 return &row.children.values()[button_in_row_index].widget.text_box; 1309 } 1310 1311 fn toolRowFocused(self: *View, index: usize) bool { 1312 const outer = self.detailOuter(index); 1313 const cid = outer.getFocus().child_id orelse return false; 1314 return outer.children.getIndex(cid) == tool_row_index; 1315 } 1316 1317 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1318 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1319 // keep the tag visible on the terminal: its rect offset by the flow's 1320 // position in the pane. 1321 if (self.session.is_terminal and item < tf.rects.items.len) { 1322 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1323 var rect = tf.rects.items[item]; 1324 rect.x += flow_rect.x; 1325 rect.y += flow_rect.y; 1326 self.detailScroll(index).scrollToRect(rect); 1327 } 1328 } 1329 } 1330 1331 fn titleFocused(self: *View, index: usize) bool { 1332 const inner = self.detailInner(index); 1333 const cid = inner.getFocus().child_id orelse return false; 1334 return inner.children.getIndex(cid) == title_child_index; 1335 } 1336 1337 // the title sits at the top of the pane, so focusing it scrolls there. 1338 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 1339 const inner = self.detailInner(index); 1340 if (inner.children.count() == 0) return; 1341 root_focus.setFocus(inner.children.keys()[title_child_index]); 1342 self.detailScroll(index).y = 0; 1343 } 1344 1345 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1346 const tf = self.tagFlow(index) orelse return; 1347 if (tf.text_boxes.items.len == 0) return; 1348 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1349 const item = tf.indexOfFocusId(cid) orelse 0; 1350 self.focusTag(index, tf, root_focus, item); 1351 } 1352 1353 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1354 if (self.description_id[index]) |id| root_focus.setFocus(id); 1355 } 1356 1357 // return to the tool row's last-focused button (the header when the 1358 // description page shows no row). 1359 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 1360 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 1361 root_focus.setFocus(cid); 1362 } 1363 1364 // enter the detail pane. an empty pane (no issues) can't be entered. 1365 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1366 if (self.detailInner(index).children.count() == 0) return; 1367 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1368 } 1369 1370 // return to the list. 1371 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1372 root_focus.setFocus(self.listScroll(index).getFocus().id); 1373 } 1374 1375 // cross to the header tabs above the stack. 1376 fn focusHeader(self: *View, root_focus: *Focus) void { 1377 root_focus.setFocus(self.box.children.keys()[header_index]); 1378 } 1379 1380 pub fn clearGrid(self: *View) void { 1381 self.box.clearGrid(); 1382 } 1383 1384 pub fn getGrid(self: View) ?Grid { 1385 return self.box.getGrid(); 1386 } 1387 1388 pub fn getFocus(self: *View) *Focus { 1389 return self.box.getFocus(); 1390 } 1391 1392 // for the parent's "scroll up at the top jumps to the header" check: at the 1393 // top only while the header tabs hold focus, so up from the split 1394 // crosses into the tabs first. 1395 pub fn getSelectedIndex(self: *View) ?usize { 1396 return if (self.headerActive()) 0 else 1; 1397 } 1398 }; 1399 1400 // the "a:" navigation link for `status`'s issues page filtered to the 1401 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1402 // a rooted url ignores `status` (it derives its view from the issue's own); 1403 // with an empty `id` this is the bare list link. 1404 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1405 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1406 const url = try route.toUrl(page_arena); 1407 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1408 } 1409 1410 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1411 // href is only followed with js off. 1412 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1413 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1414 const url = try route.toUrl(page_arena); 1415 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1416 } 1417 1418 // the "a:" link to the whole-description page of issue `id` within `identity`. 1419 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1420 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 1421 const url = try route.toUrl(page_arena); 1422 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1423 } 1424 1425 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1426 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1427 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1428 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1429 const url = try route.toUrl(page_arena); 1430 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1431 } 1432 1433 // tabs switching between the issues page's views. 1434 pub const Header = struct { 1435 box: wgt.Box(ui.Widget), 1436 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1437 1438 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1439 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1440 errdefer box.deinit(allocator); 1441 1442 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1443 errdefer tab_ids.deinit(allocator); 1444 1445 const aa = session.page_arena.allocator(); 1446 1447 // a list tab per status, labeled with its listing's issue count 1448 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1449 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1450 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1451 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1452 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1453 errdefer text_box.deinit(allocator); 1454 text_box.getFocus().focusable = true; 1455 text_box.getFocus().kind = .{ .custom = link }; 1456 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1457 try box.children.put(allocator, text_box.getFocus().id, .{ 1458 .widget = .{ .text_box = text_box }, 1459 .rect = null, 1460 .min_size = .{ .width = label.len + 2, .height = null }, 1461 }); 1462 } 1463 1464 // tags tab, labeled with the active tag filter 1465 { 1466 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1467 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1468 const label = if (data.tag.len == 0) "tags" else blk: { 1469 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1470 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1471 }; 1472 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1473 errdefer text_box.deinit(allocator); 1474 text_box.getFocus().focusable = true; 1475 text_box.getFocus().kind = .{ .custom = tags_link }; 1476 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1477 try box.children.put(allocator, text_box.getFocus().id, .{ 1478 .widget = .{ .text_box = text_box }, 1479 .rect = null, 1480 .min_size = .{ .width = label.len + 2, .height = null }, 1481 }); 1482 } 1483 1484 // new-issue tab; an edit url shows the edit tab in its place 1485 { 1486 const route = if (data.view == .edit) 1487 ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong 1488 else 1489 ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1490 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1491 const label: []const u8 = if (data.view == .edit) "edit" else "new"; 1492 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1493 errdefer text_box.deinit(allocator); 1494 text_box.getFocus().focusable = true; 1495 text_box.getFocus().kind = .{ .custom = link }; 1496 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1497 try box.children.put(allocator, text_box.getFocus().id, .{ 1498 .widget = .{ .text_box = text_box }, 1499 .rect = null, 1500 .min_size = .{ .width = label.len + 2, .height = null }, 1501 }); 1502 } 1503 1504 var self = Header{ .box = box, .tab_ids = tab_ids }; 1505 // the tab matching the page's view is selected initially. 1506 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1507 return self; 1508 } 1509 1510 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1511 self.box.deinit(allocator); 1512 self.tab_ids.deinit(allocator); 1513 } 1514 1515 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1516 self.clearGrid(); 1517 // only the selected tab shows its border 1518 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1519 switch (child.widget) { 1520 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1521 else => {}, 1522 } 1523 } 1524 try self.box.build(allocator, constraint, root_focus); 1525 } 1526 1527 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1528 _ = allocator; 1529 const current_tab = self.currentTabIndex() orelse return; 1530 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1531 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1532 } 1533 } 1534 1535 pub fn clearGrid(self: *Header) void { 1536 self.box.clearGrid(); 1537 } 1538 1539 pub fn getGrid(self: Header) ?Grid { 1540 return self.box.getGrid(); 1541 } 1542 1543 pub fn getFocus(self: *Header) *Focus { 1544 return self.box.getFocus(); 1545 } 1546 1547 pub fn getSelectedIndex(self: Header) ?usize { 1548 return self.currentTabIndex(); 1549 } 1550 1551 fn currentTabIndex(self: Header) ?usize { 1552 const child_id = self.box.focus.child_id orelse return null; 1553 return self.tab_ids.getIndex(child_id); 1554 } 1555 }; 1556