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