╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
493 var tags = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = !wasm }); 494 errdefer tags.deinit(allocator); 495 tags.getFocus().focusable = true; 496 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = null }); 497 } 498 499 { 500 var description = try wgt.TextInput(ui.Widget).init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = !wasm, .multiline = true, .visible_height = 5 }); 501 errdefer description.deinit(allocator); 502 description.getFocus().focusable = true; 503 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 504 } 505 506 { 507 var button = try wgt.TextBox(ui.Widget).init(allocator, "submit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 508 errdefer button.deinit(allocator); 509 button.getFocus().focusable = true; 510 // the renderer distinguishes plain clickables from buttons that 511 // should POST to a server route by this kind. 512 button.getFocus().kind = .{ .custom = "submit" }; 513 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 514 } 515 516 // absorbs the leftover min-height the box hands its last child, so 517 // the button keeps its natural height 518 { 519 var spacer = try ui.Spacer.init(allocator); 520 errdefer spacer.deinit(allocator); 521 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 522 } 523 524 box.getFocus().child_id = box.children.keys()[title_field_index]; 525 return box; 526 } 527 528 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 529 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 530 errdefer row.deinit(allocator); 531 row.getFocus().focusable = true; 532 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 533 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 534 } 535 536 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 537 self.box.deinit(allocator); 538 } 539 540 fn header(self: *View) *Header { 541 return &self.box.children.values()[header_index].widget.repo_issues_header; 542 } 543 544 fn viewStack(self: *View) *wgt.Stack(ui.Widget) { 545 return &self.box.children.values()[stack_index].widget.stack; 546 } 547 548 // `index`'s master-detail split inside the stack. 549 fn resultsBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 550 return &self.viewStack().children.values()[index].box; 551 } 552 553 // the tags view's flow inside the stack. 554 fn tagsView(self: *View) *ui.TagFlow { 555 return &self.viewStack().children.values()[tags_view_index].tag_flow; 556 } 557 558 // the new-issue form inside the stack. 559 fn newForm(self: *View) *wgt.Box(ui.Widget) { 560 return &self.viewStack().children.values()[new_view_index].box; 561 } 562 563 fn listScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 564 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 565 } 566 567 fn listBox(self: *View, index: usize) *wgt.Box(ui.Widget) { 568 return &self.listScroll(index).child.box; 569 } 570 571 fn detailOuter(self: *View, index: usize) *wgt.Box(ui.Widget) { 572 return &self.resultsBox(index).children.values()[detail_index].widget.box; 573 } 574 575 fn detailScroll(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 576 return &self.detailOuter(index).children.values()[0].widget.scroll; 577 } 578 579 fn detailInner(self: *View, index: usize) *wgt.Box(ui.Widget) { 580 return &self.detailScroll(index).child.box; 581 } 582 583 fn window(self: *View, index: usize) *const Window { 584 return self.data.window(splitStatus(index)); 585 } 586 587 fn stackSelectedIndex(self: *View) ?usize { 588 const stack = self.viewStack(); 589 const cid = stack.getFocus().child_id orelse return null; 590 return stack.children.getIndex(cid); 591 } 592 593 // the stack's selected master-detail split (null when the tags view or the 594 // new-issue form shows). 595 fn selectedSplitIndex(self: *View) ?usize { 596 const idx = self.stackSelectedIndex() orelse return null; 597 return if (idx < split_count) idx else null; 598 } 599 600 fn detailActive(self: *View, index: usize) bool { 601 const rb = self.resultsBox(index); 602 const cid = rb.getFocus().child_id orelse return false; 603 return rb.children.getIndex(cid) == detail_index; 604 } 605 606 fn headerActive(self: *View) bool { 607 const cid = self.box.getFocus().child_id orelse return false; 608 return self.box.children.getIndex(cid) == header_index; 609 } 610 611 fn tagsViewActive(self: *View) bool { 612 if (self.headerActive()) return false; 613 return self.stackSelectedIndex() == tags_view_index; 614 } 615 616 fn newViewActive(self: *View) bool { 617 if (self.headerActive()) return false; 618 return self.stackSelectedIndex() == new_view_index; 619 } 620 621 // the selected issue's index, or null when a window-navigation row is 622 // selected (a leading "previous" row shifts the issue rows down by one). 623 fn selectedIssueIndex(self: *View, index: usize) ?usize { 624 const lb = self.listBox(index); 625 const cid = lb.getFocus().child_id orelse return null; 626 const idx = lb.children.getIndex(cid) orelse return null; 627 const win = self.window(index); 628 const lead: usize = if (win.prev_id != null) 1 else 0; 629 if (idx < lead or idx - lead >= win.issues.len) return null; 630 return idx - lead; 631 } 632 633 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 634 self.clearGrid(); 635 636 // each header tab maps 1:1 to a stack child by position. mirror the 637 // selection into the url: the view the page's window is rooted at 638 // keeps its rooted url, the others get their list route (keeping the 639 // tag filter). 640 if (self.header().getSelectedIndex()) |index| { 641 const stack = self.viewStack(); 642 stack.getFocus().child_id = stack.children.keys()[index]; 643 self.session.data.current_page = (if (index == viewIndex(self.data.view) and self.data.selected_id.len != 0) 644 ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, self.data.tag, self.data.selected_id) 645 else if (index == tags_view_index) 646 ui.RoutablePage.repoIssuesTagsRoute(self.data.identity, self.data.tag) 647 else if (index == new_view_index) 648 ui.RoutablePage.repoIssuesNewRoute(self.data.identity) 649 else 650 ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(index), self.data.tag, "")) orelse self.session.data.current_page; 651 } 652 653 if (self.selectedSplitIndex()) |i| { 654 // swap the detail pane to the selected issue when it changes. 655 try self.refreshDetail(allocator, i); 656 657 // mirror the focused issue into the url, but only while focus is 658 // inside the split. an issue's url is the same whether or not the 659 // list is filtered, so the mirror drops the tag. 660 if (root_focus.grandchild_id) |g| { 661 if (self.resultsBox(i).getFocus().children.contains(g)) { 662 if (self.selectedIssueIndex(i)) |sel| { 663 if (ui.RoutablePage.repoIssuesRoute(self.data.identity, splitStatus(i), "", self.window(i).issues[sel].id)) |route| 664 self.session.data.current_page = route; 665 } 666 } 667 } 668 669 // the selected list row shows a border (the focused TextBox 670 // upgrades it to a double border itself); the rest stay borderless. 671 const lb = self.listBox(i); 672 for (lb.children.keys(), lb.children.values()) |id, *child| { 673 switch (child.widget) { 674 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 675 else => {}, 676 } 677 } 678 679 // the pane's selected child shows the selection border: the 680 // description directly, the tags via the flow's selected item. 681 const inner = self.detailInner(i); 682 for (inner.children.keys(), inner.children.values()) |id, *child| { 683 switch (child.widget) { 684 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 685 else => {}, 686 } 687 } 688 if (self.tagFlow(i)) |tf| tf.selected = self.tagsFocused(i); 689 690 // cap the list at list_max_width only while the detail pane fits 691 // beside it. the box drops the detail when the width can't hold 692 // both minimums, so when it's that narrow we lift the cap and let 693 // the list fill the whole width. 694 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 695 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 696 697 // stretch the detail pane across the rest of the width so it fills 698 // the area rather than shrinking to its content; its scroll fills 699 // the pane. 700 if (constraint.max_size.width) |w| { 701 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 702 } else { 703 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 704 } 705 } 706 707 self.tagsView().selected = self.tagsViewActive(); 708 709 // refresh the form inputs' entries in the session's focus-id -> input 710 // map with this frame's addresses, so the web/wasm form handling can 711 // find them by focus id 712 const form = self.newForm(); 713 const inputs_arena = self.session.arena.allocator(); 714 for (form.children.values()) |*child| switch (child.widget) { 715 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 716 else => {}, 717 }; 718 719 try self.box.build(allocator, constraint, root_focus); 720 } 721 722 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 723 const sel = self.selectedIssueIndex(index) orelse return; 724 if (self.detailed_index[index]) |d| if (d == sel) return; 725 try self.populateDetail(allocator, index, sel); 726 self.detailed_index[index] = sel; 727 } 728 729 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 730 const entry = self.window(index).issues[sel]; 731 const inner = self.detailInner(index); 732 733 for (inner.children.values()) |*child| child.widget.deinit(allocator); 734 inner.children.clearAndFree(allocator); 735 inner.getFocus().child_id = null; 736 737 // the issue's tags, each linking to this status's list filtered to 738 // that tag. 739 { 740 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 741 defer items.deinit(allocator); 742 var tag_iter = evt.Issue.tagIterator(entry.issue.tags); 743 while (tag_iter.next()) |tag| { 744 if (tag.len == 0) continue; 745 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, splitStatus(index), tag) }); 746 } 747 if (items.items.len > 0) { 748 var tf = try ui.TagFlow.init(allocator); 749 errdefer tf.deinit(allocator); 750 try tf.setItems(allocator, items.items); 751 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 752 } 753 } 754 755 // the description as a focusable word-wrapped text box. its hidden 756 // border reserves the space the border occupies when focused, so 757 // focusing doesn't shift layout. 758 self.description_id[index] = blk: { 759 const description = if (entry.issue.description.len == 0) "(no description)" else entry.issue.description; 760 var tb = try wgt.TextBox(ui.Widget).init(allocator, description, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 761 errdefer tb.deinit(allocator); 762 tb.getFocus().focusable = true; 763 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 764 break :blk tb.getFocus().id; 765 }; 766 767 // select the description by default 768 inner.getFocus().child_id = self.description_id[index]; 769 770 // reset the scroll to the top for the newly-shown issue: directly on the 771 // terminal (the wasm offset), and via a version bump on the web (so the 772 // renderer's scroll id changes and JS drops the preserved position). 773 const sc = self.detailScroll(index); 774 sc.x = 0; 775 sc.y = 0; 776 sc.getFocus().version +%= 1; 777 } 778 779 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 780 if (self.headerActive()) { 781 // down from the tabs re-enters the stack if the selected view has 782 // something to focus; other keys move the tabs. 783 if (inp.vertDirection(key) == .down) { 784 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 785 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 786 else => true, 787 } else false; 788 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 789 } else { 790 try self.header().input(allocator, key, root_focus); 791 } 792 return; 793 } 794 if (self.tagsViewActive()) { 795 self.tagsViewInput(key, root_focus); 796 return; 797 } 798 if (self.newViewActive()) { 799 try self.newViewInput(allocator, key, root_focus); 800 return; 801 } 802 const i = self.selectedSplitIndex() orelse return; 803 if (self.detailActive(i)) { 804 try self.detailInput(i, key, root_focus); 805 } else { 806 try self.listInput(i, key, root_focus); 807 } 808 } 809 810 // arrow keys move the tag selection; up from the top row crosses to the 811 // header tabs. 812 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 813 const tf = self.tagsView(); 814 const cid = tf.focus.child_id orelse return; 815 const cur = tf.indexOfFocusId(cid) orelse return; 816 const count = tf.text_boxes.items.len; 817 switch (key) { 818 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 819 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 820 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 821 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 822 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 823 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 824 else => {}, 825 } 826 } 827 828 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 829 // up/down (and the scroll wheel) move the selection a row; page up/down 830 // jump a fixed amount. right/Enter cross into the detail pane. up from 831 // the top row crosses into the header tabs. 832 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 833 const lb = self.listBox(index); 834 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 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