╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
835 if (delta < 0 and at_top) return self.focusHeader(root_focus); 836 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 837 return; 838 } 839 switch (key) { 840 .enter, .arrow_right => try self.focusDetail(index, root_focus), 841 else => {}, 842 } 843 } 844 845 fn detailInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 846 if (self.tagsFocused(index)) { 847 try self.tagsInput(index, key, root_focus); 848 } else { 849 try self.descriptionInput(index, key, root_focus); 850 } 851 } 852 853 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 854 const sc = self.detailScroll(index); 855 switch (key) { 856 .arrow_left => return self.focusList(index, root_focus), 857 // once the scroll can't move further, cross into the tags (or the 858 // header tabs when the issue has none). 859 .arrow_up => { 860 const before = sc.y; 861 sc.y -= 1; 862 sc.clampToContent(); 863 if (sc.y == before) { 864 if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else self.focusHeader(root_focus); 865 } 866 return; 867 }, 868 .arrow_down => sc.y += 1, 869 .page_up => sc.y -= 10, 870 .page_down => sc.y += 10, 871 .home => sc.y = 0, 872 .end => sc.y = std.math.maxInt(isize), 873 .mouse => |mouse| switch (mouse.action) { 874 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 875 else => {}, 876 }, 877 else => return, 878 } 879 sc.clampToContent(); 880 } 881 882 // up/down (and tab/shift+tab) move between the form's fields; up from the 883 // title crosses into the header tabs. the multiline description keeps 884 // enter and any up/down that has a row to move to. 885 fn newViewInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 886 const form = self.newForm(); 887 const cid = form.getFocus().child_id orelse return; 888 const cur = form.children.getIndex(cid) orelse return; 889 890 if (cur == description_field_index) { 891 const child = &form.children.values()[cur]; 892 const description = &child.widget.text_input; 893 switch (key) { 894 .enter => return child.widget.input(allocator, key, root_focus), 895 .arrow_up => if (!try description.cursorOnFirstRow(allocator)) 896 return child.widget.input(allocator, key, root_focus), 897 .arrow_down => if (!try description.cursorOnLastRow(allocator)) 898 return child.widget.input(allocator, key, root_focus), 899 else => {}, 900 } 901 } 902 903 switch (key) { 904 .arrow_up, .back_tab => if (cur > 0) 905 root_focus.setFocus(form.children.keys()[cur - 1]) 906 else 907 self.focusHeader(root_focus), 908 .arrow_down, .tab => if (cur < submit_field_index) { 909 root_focus.setFocus(form.children.keys()[cur + 1]); 910 }, 911 .enter => if (cur == submit_field_index) try self.submitNewIssue(allocator), 912 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 913 try self.submitNewIssue(allocator); 914 }, 915 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 916 } 917 } 918 919 // commit the new issue to the repo's events branch and navigate to it. 920 // this is the terminal path; the web posts the form to the issue route, 921 // so the wasm side never runs (or compiles) the repo access below. 922 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 923 if (comptime wasm) return; 924 const io = self.session.io orelse return; 925 const src = self.data.repo_source orelse return; 926 927 const form = self.newForm(); 928 const title_input = &form.children.values()[title_field_index].widget.text_input; 929 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 930 const description_input = &form.children.values()[description_field_index].widget.text_input; 931 932 const title = try title_input.text(allocator); 933 defer allocator.free(title); 934 const tags = try tags_input.text(allocator); 935 defer allocator.free(tags); 936 const description = try description_input.text(allocator); 937 defer allocator.free(description); 938 939 // a title is required, and a too-long tag would fail consumption 940 // after the event is already committed 941 if (title.len == 0) return; 942 var tag_iter = evt.Issue.tagIterator(tags); 943 while (tag_iter.next()) |tag| { 944 if (tag.len > evt.Issue.tag_max_len) return; 945 } 946 947 var id_bytes: [evt.event_id_size]u8 = undefined; 948 io.random(&id_bytes); 949 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 950 951 const event = evt.EventWithId{ 952 .id = event_id_hex, 953 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 954 .event = .{ .issue = .{ 955 .title = title, 956 .description = description, 957 .tags = tags, 958 } }, 959 }; 960 961 switch (src.repo_kind) { 962 inline else => |repo_kind| { 963 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 964 defer any_repo.deinit(io, allocator); 965 switch (any_repo) { 966 inline else => |*repo| try evt.commitAndConsume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}, self.session.local != null), 967 } 968 }, 969 } 970 971 // wipe the form so a return visit starts fresh 972 title_input.clear(allocator); 973 tags_input.clear(allocator); 974 description_input.clear(allocator); 975 976 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 977 try self.session.navigate(route); 978 } 979 980 // arrow keys move the tag selection; at the flow's edges focus crosses to 981 // the neighboring widgets. 982 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 983 const tf = self.tagFlow(index) orelse return; 984 const cid = tf.focus.child_id orelse return; 985 const cur = tf.indexOfFocusId(cid) orelse return; 986 const count = tf.text_boxes.items.len; 987 const sc = self.detailScroll(index); 988 switch (key) { 989 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 990 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 991 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusHeader(root_focus), 992 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 993 .home => self.focusTag(index, tf, root_focus, 0), 994 .end => self.focusTag(index, tf, root_focus, count - 1), 995 .mouse => |mouse| switch (mouse.action) { 996 .scroll => |dir| { 997 sc.y += if (dir == .up) @as(isize, -1) else 1; 998 sc.clampToContent(); 999 }, 1000 else => {}, 1001 }, 1002 else => {}, 1003 } 1004 } 1005 1006 const tags_child_index: usize = 0; 1007 1008 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1009 const inner = self.detailInner(index); 1010 if (inner.children.count() == 0) return null; 1011 return switch (inner.children.values()[tags_child_index].widget) { 1012 .tag_flow => |*tf| tf, 1013 else => null, 1014 }; 1015 } 1016 1017 fn tagsFocused(self: *View, index: usize) bool { 1018 const inner = self.detailInner(index); 1019 const cid = inner.getFocus().child_id orelse return false; 1020 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1021 } 1022 1023 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1024 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1025 // keep the tag visible on the terminal: its rect offset by the flow's 1026 // position in the pane. 1027 if (self.session.is_terminal and item < tf.rects.items.len) { 1028 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1029 var rect = tf.rects.items[item]; 1030 rect.x += flow_rect.x; 1031 rect.y += flow_rect.y; 1032 self.detailScroll(index).scrollToRect(rect); 1033 } 1034 } 1035 } 1036 1037 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1038 const tf = self.tagFlow(index) orelse return; 1039 if (tf.text_boxes.items.len == 0) return; 1040 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1041 const item = tf.indexOfFocusId(cid) orelse 0; 1042 self.focusTag(index, tf, root_focus, item); 1043 } 1044 1045 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1046 if (self.description_id[index]) |id| root_focus.setFocus(id); 1047 } 1048 1049 // enter the detail pane. an empty pane (no issues) can't be entered. 1050 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1051 if (self.detailInner(index).children.count() == 0) return; 1052 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1053 } 1054 1055 // return to the list. 1056 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1057 root_focus.setFocus(self.listScroll(index).getFocus().id); 1058 } 1059 1060 // cross to the header tabs above the stack. 1061 fn focusHeader(self: *View, root_focus: *Focus) void { 1062 root_focus.setFocus(self.box.children.keys()[header_index]); 1063 } 1064 1065 pub fn clearGrid(self: *View) void { 1066 self.box.clearGrid(); 1067 } 1068 1069 pub fn getGrid(self: View) ?Grid { 1070 return self.box.getGrid(); 1071 } 1072 1073 pub fn getFocus(self: *View) *Focus { 1074 return self.box.getFocus(); 1075 } 1076 1077 // for the parent's "scroll up at the top jumps to the header" check: at the 1078 // top only while the header tabs hold focus, so up from the split 1079 // crosses into the tabs first. 1080 pub fn getSelectedIndex(self: *View) ?usize { 1081 return if (self.headerActive()) 0 else 1; 1082 } 1083 }; 1084 1085 // the "a:" navigation link for `status`'s issues page filtered to the 1086 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1087 // a rooted url ignores `status` (it derives its view from the issue's own); 1088 // with an empty `id` this is the bare list link. 1089 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1090 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1091 const url = try route.toUrl(page_arena); 1092 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1093 } 1094 1095 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1096 // href is only followed with js off. 1097 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1098 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1099 const url = try route.toUrl(page_arena); 1100 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1101 } 1102 1103 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1104 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1105 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1106 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1107 const url = try route.toUrl(page_arena); 1108 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1109 } 1110 1111 // tabs switching between the issues page's views. 1112 pub const Header = struct { 1113 box: wgt.Box(ui.Widget), 1114 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1115 1116 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1117 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1118 errdefer box.deinit(allocator); 1119 1120 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1121 errdefer tab_ids.deinit(allocator); 1122 1123 const aa = session.page_arena.allocator(); 1124 1125 // a list tab per status, labeled with its listing's issue count 1126 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1127 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1128 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1129 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1130 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1131 errdefer text_box.deinit(allocator); 1132 text_box.getFocus().focusable = true; 1133 text_box.getFocus().kind = .{ .custom = link }; 1134 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1135 try box.children.put(allocator, text_box.getFocus().id, .{ 1136 .widget = .{ .text_box = text_box }, 1137 .rect = null, 1138 .min_size = .{ .width = label.len + 2, .height = null }, 1139 }); 1140 } 1141 1142 // tags tab, labeled with the active tag filter 1143 { 1144 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1145 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1146 const label = if (data.tag.len == 0) "tags" else blk: { 1147 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1148 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1149 }; 1150 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1151 errdefer text_box.deinit(allocator); 1152 text_box.getFocus().focusable = true; 1153 text_box.getFocus().kind = .{ .custom = tags_link }; 1154 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1155 try box.children.put(allocator, text_box.getFocus().id, .{ 1156 .widget = .{ .text_box = text_box }, 1157 .rect = null, 1158 .min_size = .{ .width = label.len + 2, .height = null }, 1159 }); 1160 } 1161 1162 // new-issue tab 1163 { 1164 const new_route = ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1165 const new_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try new_route.toUrl(session.page_arena)}); 1166 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "new", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1167 errdefer text_box.deinit(allocator); 1168 text_box.getFocus().focusable = true; 1169 text_box.getFocus().kind = .{ .custom = new_link }; 1170 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1171 try box.children.put(allocator, text_box.getFocus().id, .{ 1172 .widget = .{ .text_box = text_box }, 1173 .rect = null, 1174 .min_size = .{ .width = "new".len + 2, .height = null }, 1175 }); 1176 } 1177 1178 var self = Header{ .box = box, .tab_ids = tab_ids }; 1179 // the tab matching the page's view is selected initially. 1180 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1181 return self; 1182 } 1183 1184 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1185 self.box.deinit(allocator); 1186 self.tab_ids.deinit(allocator); 1187 } 1188 1189 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1190 self.clearGrid(); 1191 // only the selected tab shows its border 1192 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1193 switch (child.widget) { 1194 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1195 else => {}, 1196 } 1197 } 1198 try self.box.build(allocator, constraint, root_focus); 1199 } 1200 1201 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1202 _ = allocator; 1203 const current_tab = self.currentTabIndex() orelse return; 1204 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1205 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1206 } 1207 } 1208 1209 pub fn clearGrid(self: *Header) void { 1210 self.box.clearGrid(); 1211 } 1212 1213 pub fn getGrid(self: Header) ?Grid { 1214 return self.box.getGrid(); 1215 } 1216 1217 pub fn getFocus(self: *Header) *Focus { 1218 return self.box.getFocus(); 1219 } 1220 1221 pub fn getSelectedIndex(self: Header) ?usize { 1222 return self.currentTabIndex(); 1223 } 1224 1225 fn currentTabIndex(self: Header) ?usize { 1226 const child_id = self.box.focus.child_id orelse return null; 1227 return self.tab_ids.getIndex(child_id); 1228 } 1229 }; 1230