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