╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
950 } 951 952 if (supports_drafts and !description_page and comment_page == null) 953 try self.data.appendDetails(allocator, inner_box, self.session, entry); 954 955 if (comment_page) |comment_view| { 956 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 957 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 958 var back = try Comment.linkBox(allocator, self.session, back_label, ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 959 errdefer back.deinit(allocator); 960 try inner_box.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 961 962 try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, self.data.viewer != null, comment_view.selected); 963 try Comment.appendCount(allocator, inner_box, comment_view.replies.count, "reply", "replies"); 964 for (comment_view.replies.comments) |comment| try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, self.data.viewer != null, comment); 965 try Comment.appendWindowNav(allocator, inner_box, self.session, self.data.identity, kind, &comment_view.selected.comment.event.thread_id, &comment_view.selected.id, comment_view.replies); 966 967 var spacer = try Spacer.init(allocator); 968 errdefer spacer.deinit(allocator); 969 try inner_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 970 971 inner_box.getFocus().child_id = inner_box.children.keys()[tool_row_index + 1]; 972 self.resetScroll(); 973 return; 974 } 975 976 if (description_page) { 977 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 978 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 979 var text_box = try wgt.TextBox.init(allocator, back_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 980 errdefer text_box.deinit(allocator); 981 text_box.getFocus().mode = .all; 982 text_box.getFocus().kind = .{ .custom = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), "", entry.id, self.data.search orelse "", "") }; 983 try inner_box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 984 self.title_id = text_box.getFocus().id; 985 } else { 986 var title = try wgt.TextBox.init(allocator, entry.record.event.title, .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = " title " } }); 987 errdefer title.deinit(allocator); 988 title.getFocus().mode = .all; 989 try inner_box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_box = title }, .rect = null, .min_size = null }); 990 self.title_id = title.getFocus().id; 991 992 { 993 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 994 errdefer row.deinit(allocator); 995 var author = try ui.authorBox(allocator, self.session.page_arena, entry.author); 996 errdefer author.deinit(allocator); 997 try row.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 998 const timestamp = try ui.timestamp.format(self.session.page_arena.allocator(), std.math.cast(i64, entry.record.created_timestamp) orelse -1, try self.session.nowSeconds()); 999 var time = try wgt.TextBox.init(allocator, timestamp.relative, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = timestamp.exact } }); 1000 errdefer time.deinit(allocator); 1001 time.getFocus().mode = .all; 1002 try row.children.put(allocator, time.getFocus().id, .{ .widget = .{ .text_box = time }, .rect = null, .min_size = null }); 1003 row.getFocus().child_id = author.getFocus().id; 1004 self.author_id = row.getFocus().id; 1005 try inner_box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1006 } 1007 1008 var items: std.ArrayList(WordFlow.Item) = .empty; 1009 defer items.deinit(allocator); 1010 var label_iter = Event.labelIterator(entry.record.event.labels); 1011 while (label_iter.next()) |label| { 1012 if (label.len == 0) continue; 1013 try items.append(allocator, .{ .text = label, .link = try labelLink(self.session.page_arena, self.data.identity, entryStatus(entry), label, self.data.search orelse "", assigneeFilter(self.data)) }); 1014 } 1015 if (items.items.len > 0) { 1016 var labels = try WordFlow.init(allocator); 1017 errdefer labels.deinit(allocator); 1018 try labels.setItems(allocator, items.items); 1019 try inner_box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .word_flow = labels }, .rect = null, .min_size = null }); 1020 } 1021 } 1022 1023 if (kind == .issue) { 1024 const pa = self.session.page_arena.allocator(); 1025 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1026 const thread_url = try parent_route.toUrl(self.session.page_arena); 1027 if (entry.can_modify) try appendAssigneeRow(allocator, inner_box, self.session, thread_url); 1028 // every viewer gets each assignee as a link to the issues assigned to it 1029 const links = try pa.alloc([]const u8, entry.assignees.len); 1030 for (links, entry.assignees) |*link, assignee| link.* = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), self.data.label, "", self.data.search orelse "", try ui.urlEncodeRef(pa, assignee.display())); 1031 try appendAssignedRows(allocator, inner_box, self.session, thread_url, entry.assignees, links, entry.can_modify); 1032 } 1033 1034 const whole = entry.record.event.description; 1035 const preview_end = if (description_page) null else ui.detailPreviewEnd(whole); 1036 const shown = if (preview_end) |end| 1037 std.mem.trimEnd(u8, whole[0..end], " \t\r\n") 1038 else if (whole.len == 0) 1039 "(no description)" 1040 else 1041 whole; 1042 var description = try Markdown.Frame.init(allocator, shown, " description ", if (preview_end != null) " click or press enter to see more " else "", self.session.page_arena); 1043 errdefer description.deinit(allocator); 1044 if (preview_end != null) { 1045 const route = ui.RoutablePage.repoThreadDescriptionRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 1046 description.body().kind = .{ .custom = try self.session.page_arena.allocator().print("a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1047 } 1048 try inner_box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .markdown_frame = description }, .rect = null, .min_size = null }); 1049 self.description_id = description.getFocus().id; 1050 1051 if (!supports_drafts or !entryDraft(entry)) { 1052 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1053 const parent_url = try parent_route.toUrl(self.session.page_arena); 1054 try Attachment.appendRows(allocator, inner_box, self.session, self.data.identity, parent_url, entry.attachments); 1055 1056 if (!description_page) { 1057 if (self.data.viewer != null) { 1058 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoThreadCommentNewRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1059 errdefer reply.deinit(allocator); 1060 try inner_box.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1061 } 1062 1063 try Comment.appendCount(allocator, inner_box, entry.comments.count, "comment", "comments"); 1064 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, self.data.viewer != null, comment); 1065 try Comment.appendWindowNav(allocator, inner_box, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1066 } 1067 } 1068 1069 if (!description_page) { 1070 var spacer = try Spacer.init(allocator); 1071 errdefer spacer.deinit(allocator); 1072 try inner_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1073 } 1074 1075 _ = self.focusFirst(null); 1076 self.resetScroll(); 1077 } 1078 1079 fn resetScroll(self: *This) void { 1080 const detail_scroll = &self.scroll; 1081 detail_scroll.x = 0; 1082 detail_scroll.y = 0; 1083 detail_scroll.getFocus().version +%= 1; 1084 } 1085 1086 pub fn inputWithExit(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !Exit { 1087 self.exit = .none; 1088 const key: Key = switch (raw_key) { 1089 .scroll_up => .arrow_up, 1090 .scroll_down => .arrow_down, 1091 else => raw_key, 1092 }; 1093 1094 // a focused assignee input keeps its editing keys 1095 if (key == .home or key == .end) if (self.focusedAssigneeInput(root_focus)) |text_input| { 1096 try text_input.input(allocator, key, root_focus); 1097 return self.exit; 1098 }; 1099 1100 switch (key) { 1101 .page_up => self.pageDetail(root_focus, -10), 1102 .page_down => self.pageDetail(root_focus, 10), 1103 .home => self.jump(root_focus, false), 1104 .end => self.jump(root_focus, true), 1105 // tab steps between the links of a focused description or 1106 // comment; the web leaves it to the browser 1107 .tab, .back_tab => if (self.session.is_terminal) if (self.focusedChild(root_focus)) |index| if (self.focusedFrame(index)) |frame| { 1108 try frame.frame.stepLink(allocator, root_focus, &self.scroll, frame.x, frame.y, key == .tab); 1109 }, 1110 else => { 1111 const entry = self.entry orelse return self.exit; 1112 if (self.data.comment_page != null and std.mem.eql(u8, entry.id, self.data.selected_id)) { 1113 self.commentInput(key, root_focus); 1114 } else if (self.toolRowFocused(root_focus)) { 1115 try self.toolRowInput(allocator, key, root_focus); 1116 } else { 1117 try self.childInput(allocator, key, root_focus); 1118 } 1119 }, 1120 } 1121 return self.exit; 1122 } 1123 1124 // the assignee row's input, when it holds focus 1125 fn focusedAssigneeInput(self: *This, root_focus: *Focus) ?*wgt.TextInput { 1126 const child_index = self.focusedChild(root_focus) orelse return null; 1127 const child = &self.inner().children.values()[child_index].widget; 1128 if (child.* != .box or !isAssigneeRow(&child.box)) return null; 1129 const text_input = &child.box.children.values()[0].widget.text_input; 1130 return if (root_focus.grandchild_id == text_input.getFocus().id) text_input else null; 1131 } 1132 1133 fn childInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1134 const child_index = self.focusedChild(root_focus) orelse return; 1135 const inner_box = self.inner(); 1136 const child_id = inner_box.children.keys()[child_index]; 1137 const child = &inner_box.children.values()[child_index].widget; 1138 1139 if (child_id == self.title_id) { 1140 switch (key) { 1141 .arrow_left => self.exit = .list, 1142 .arrow_up => if (!self.moveVertical(root_focus, false)) { 1143 self.exit = .header; 1144 }, 1145 .arrow_down => _ = self.moveVertical(root_focus, true), 1146 else => {}, 1147 } 1148 return; 1149 } 1150 if (child_id == self.author_id) { 1151 switch (key) { 1152 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 1153 self.exit = .list; 1154 }, 1155 .arrow_right => _ = self.moveHorizontal(root_focus, true), 1156 .arrow_up => _ = self.moveVertical(root_focus, false), 1157 .arrow_down => _ = self.moveVertical(root_focus, true), 1158 else => {}, 1159 } 1160 return; 1161 } 1162 if (child_id == self.description_id) { 1163 self.descriptionInput(key, root_focus); 1164 return; 1165 } 1166 1167 switch (child.*) { 1168 .word_flow => |*labels| self.labelsInput(child_index, labels, key, root_focus), 1169 .copyable_text => |*copyable| switch (key) { 1170 .arrow_up => if (!self.moveVertical(root_focus, false)) { 1171 self.exit = .header; 1172 }, 1173 .arrow_down => _ = self.moveVertical(root_focus, true), 1174 .arrow_left => if (copyable.selected == 0) { 1175 self.exit = .list; 1176 } else try copyable.input(allocator, key, root_focus), 1177 else => try copyable.input(allocator, key, root_focus), 1178 }, 1179 .box => |*row| if (isAssigneeRow(row)) { 1180 try self.assigneeInput(allocator, row, key, root_focus); 1181 } else if (Attachment.removeId(row)) |attachment_id| { 1182 if (self.removableRowInput(row, key, root_focus)) try self.removeEvent(allocator, .attach, attachment_id); 1183 } else if (unassignEmail(row)) |encoded| { 1184 if (self.removableRowInput(row, key, root_focus)) try self.unassign(allocator, std.Uri.percentDecodeInPlace(try self.session.page_arena.allocator().dupe(u8, encoded))); 1185 } else self.commentInput(key, root_focus), 1186 else => self.commentInput(key, root_focus), 1187 } 1188 } 1189 1190 fn toolRowInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1191 const row = self.toolRow(); 1192 const child_id = row.getFocus().child_id orelse return; 1193 const cur = row.children.getIndex(child_id) orelse return; 1194 const on_primary = switch (row.children.values()[cur].widget.getFocus().kind) { 1195 .custom => |action| std.mem.eql(u8, action, "submit"), 1196 else => false, 1197 }; 1198 switch (key) { 1199 .arrow_left => { 1200 var previous = cur; 1201 while (previous > 0) { 1202 previous -= 1; 1203 if (row.children.values()[previous].widget.getFocus().mode != .all) continue; 1204 root_focus.setFocus(row.children.keys()[previous]); 1205 return; 1206 } 1207 self.exit = .list; 1208 }, 1209 .arrow_right => { 1210 var next = cur + 1; 1211 while (next < row.children.count()) : (next += 1) { 1212 if (row.children.values()[next].widget.getFocus().mode != .all) continue; 1213 root_focus.setFocus(row.children.keys()[next]); 1214 return; 1215 } 1216 }, 1217 .arrow_up => self.exit = .header, 1218 .arrow_down => _ = self.moveVertical(root_focus, true), 1219 .enter => if (on_primary) try self.primaryAction(allocator), 1220 .mouse => |mouse| if (on_primary and inp.leftClickOn(root_focus, child_id, mouse)) try self.primaryAction(allocator), 1221 else => {}, 1222 } 1223 } 1224 1225 fn primaryAction(self: *This, allocator: std.mem.Allocator) !void { 1226 try self.toggleStatus(allocator); 1227 } 1228 1229 fn descriptionInput(self: *This, key: Key, root_focus: *Focus) void { 1230 switch (key) { 1231 .arrow_left => self.exit = .list, 1232 .arrow_up => _ = self.moveVertical(root_focus, false), 1233 .arrow_down => _ = self.moveVertical(root_focus, true), 1234 else => {}, 1235 } 1236 } 1237 1238 fn commentInput(self: *This, key: Key, root_focus: *Focus) void { 1239 switch (key) { 1240 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 1241 self.exit = .list; 1242 }, 1243 .arrow_right => _ = self.moveHorizontal(root_focus, true), 1244 .arrow_up => _ = self.moveVertical(root_focus, false), 1245 .arrow_down => _ = self.moveVertical(root_focus, true), 1246 else => {}, 1247 } 1248 } 1249 1250 fn pageDetail(self: *This, root_focus: *Focus, delta: isize) void { 1251 if (!self.session.is_terminal) return; 1252 const detail_scroll = &self.scroll; 1253 detail_scroll.y += delta; 1254 detail_scroll.clampToContent(); 1255 self.focusVisible(root_focus, delta > 0); 1256 } 1257 1258 fn jump(self: *This, root_focus: *Focus, to_end: bool) void { 1259 if (self.session.is_terminal) { 1260 const detail_scroll = &self.scroll; 1261 detail_scroll.y = if (to_end) std.math.maxInt(isize) else 0; 1262 detail_scroll.clampToContent(); 1263 self.focusVisible(root_focus, to_end); 1264 } else { 1265 const focus = self.inner().getFocus(); 1266 var chosen: ?usize = null; 1267 for (focus.children.keys(), focus.children.values()) |id, child| { 1268 if (child.focus.mode != .all) continue; 1269 chosen = id; 1270 if (!to_end) break; 1271 } 1272 if (chosen) |id| root_focus.setFocus(id); 1273 } 1274 } 1275 1276 fn rectVisible(self: *This, rect: layout.IRect) bool { 1277 const detail_scroll = &self.scroll; 1278 const viewport = detail_scroll.grid orelse return false; 1279 const top = detail_scroll.y; 1280 const bottom = top + @as(isize, @intCast(viewport.size.height - detail_scroll.bar_h)); 1281 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1282 } 1283 1284 fn focusVisible(self: *This, root_focus: *Focus, last: bool) void { 1285 const inner_box = self.inner(); 1286 var offset: usize = 0; 1287 while (offset < inner_box.children.count()) : (offset += 1) { 1288 const child_index = if (last) inner_box.children.count() - 1 - offset else offset; 1289 if (self.focusVisibleChild(child_index, root_focus, last)) return; 1290 } 1291 } 1292 1293 fn focusVisibleChild(self: *This, child_index: usize, root_focus: *Focus, last: bool) bool { 1294 const inner_box = self.inner(); 1295 const child = &inner_box.children.values()[child_index]; 1296 const child_rect = child.rect orelse return false; 1297 var chosen: ?usize = null; 1298 if (child.widget.getFocus().mode == .all and self.rectVisible(child_rect)) { 1299 const id = inner_box.children.keys()[child_index]; 1300 chosen = id; 1301 if (!last) { 1302 root_focus.setFocus(id); 1303 return true; 1304 } 1305 } 1306 1307 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1308 if (focus_child.focus.mode != .all) continue; 1309 const rect: layout.IRect = .{ 1310 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1311 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1312 .size = focus_child.rect.size, 1313 }; 1314 if (!self.rectVisible(rect)) continue; 1315 chosen = id; 1316 if (!last) break; 1317 } 1318 if (chosen) |id| { 1319 root_focus.setFocus(id); 1320 return true; 1321 } 1322 return false; 1323 } 1324 1325 // the markdown frame focused in child `child_index`, placed in the scroll's 1326 // content 1327 fn focusedFrame(self: *This, child_index: usize) ?struct { frame: *Markdown.Frame, x: isize, y: isize } { 1328 const child = &self.inner().children.values()[child_index]; 1329 const rect = child.rect orelse return null; 1330 switch (child.widget) { 1331 .markdown_frame => |*frame| return .{ .frame = frame, .x = rect.x, .y = rect.y }, 1332 .repo_comment => |*comment| { 1333 if (!comment.bodyFocused()) return null; 1334 const row = comment.rowRect(true) orelse return null; 1335 return .{ .frame = comment.body(), .x = rect.x + row.x, .y = rect.y + row.y }; 1336 }, 1337 else => return null, 1338 } 1339 } 1340 1341 fn focusedChild(self: *This, root_focus: *Focus) ?usize { 1342 const inner_box = self.inner(); 1343 var id = root_focus.grandchild_id orelse return null; 1344 while (inner_box.getFocus().children.get(id)) |child| { 1345 if (child.parent_id == inner_box.getFocus().id) return inner_box.children.getIndex(id); 1346 id = child.parent_id; 1347 } 1348 return null; 1349 } 1350 1351 fn childFocusable(self: *This, child_index: usize) bool { 1352 const focus = self.inner().children.values()[child_index].widget.getFocus(); 1353 if (focus.mode == .all) return true; 1354 for (focus.children.values()) |item| { 1355 if (item.focus.mode == .all) return true; 1356 } 1357 return false; 1358 } 1359 1360 // focus child `child_index`, its last part when `last`, entered from above 1361 // when `down` 1362 fn focusChild(self: *This, child_index: usize, last: bool, down: bool, root_focus: *Focus) void { 1363 const inner_box = self.inner(); 1364 const child = &inner_box.children.values()[child_index]; 1365 var target = inner_box.children.keys()[child_index]; 1366 var rect = child.rect orelse return; 1367 switch (child.widget) { 1368 .repo_comment => |*comment| { 1369 if (comment.rowRect(last)) |row_rect| { 1370 rect.x += row_rect.x; 1371 rect.y += row_rect.y; 1372 rect.size = row_rect.size; 1373 } 1374 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1375 }, 1376 // a row enters on its first cell from either direction; the tool 1377 // row keeps the cell it was on 1378 .box => |*box| if (box.children.count() > 0) { 1379 target = if (child_index == tool_row_index) 1380 box.getFocus().child_id orelse return 1381 else 1382 box.children.keys()[0]; 1383 root_focus.setFocus(target); 1384 }, 1385 else => root_focus.setFocus(target), 1386 } 1387 widget.scrollToEnteredRow(&self.scroll, rect, down); 1388 } 1389 1390 fn moveVertical(self: *This, root_focus: *Focus, down: bool) bool { 1391 const inner_box = self.inner(); 1392 const current = self.focusedChild(root_focus) orelse return false; 1393 // a tall description or comment body is scrolled through before focus 1394 // leaves it 1395 if (self.focusedRect(current)) |rect| if (widget.scrollThroughRow(&self.scroll, rect, if (down) 1 else -1)) return true; 1396 if (inner_box.children.values()[current].widget == .repo_comment) { 1397 const comment = &inner_box.children.values()[current].widget.repo_comment; 1398 if (down and !comment.bodyFocused()) { 1399 self.focusChild(current, true, true, root_focus); 1400 return true; 1401 } 1402 if (!down and comment.bodyFocused()) { 1403 self.focusChild(current, false, false, root_focus); 1404 return true; 1405 } 1406 } 1407 1408 var next = current; 1409 while (true) { 1410 if (down) { 1411 next += 1; 1412 if (next >= inner_box.children.count()) return false; 1413 } else { 1414 if (next == 0) return false; 1415 next -= 1; 1416 } 1417 if (!self.childFocusable(next)) continue; 1418 self.focusChild(next, !down, down, root_focus); 1419 return true; 1420 } 1421 } 1422 1423 // the focused part of child `child_index`, in the scroll's content: a 1424 // comment's metadata or body row, else the whole child 1425 fn focusedRect(self: *This, child_index: usize) ?layout.IRect { 1426 const child = &self.inner().children.values()[child_index]; 1427 var rect = child.rect orelse return null; 1428 if (child.widget == .repo_comment) { 1429 const comment = &child.widget.repo_comment; 1430 const row = comment.rowRect(comment.bodyFocused()) orelse return null; 1431 rect.x += row.x; 1432 rect.y += row.y; 1433 rect.size = row.size; 1434 } 1435 return rect; 1436 } 1437 1438 fn moveHorizontal(self: *This, root_focus: *Focus, right: bool) bool { 1439 const inner_box = self.inner(); 1440 const current = self.focusedChild(root_focus) orelse return false; 1441 const child = &inner_box.children.values()[current]; 1442 if (child.widget == .repo_comment) { 1443 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1444 if (moved) if (child.rect) |rect| self.scroll.scrollToRect(rect); 1445 return moved; 1446 } 1447 const row = switch (child.widget) { 1448 .box => |*box| box, 1449 else => return false, 1450 }; 1451 if (!widget.moveInRow(row, root_focus, right)) return false; 1452 if (child.rect) |rect| self.scroll.scrollToRect(rect); 1453 return true; 1454 } 1455 1456 // the input takes typing and assigns on enter 1457 fn assigneeInput(self: *This, allocator: std.mem.Allocator, row: *wgt.Box(Widget), key: Key, root_focus: *Focus) !void { 1458 const text_input = &row.children.values()[0].widget.text_input; 1459 switch (key) { 1460 .arrow_up => _ = self.moveVertical(root_focus, false), 1461 .arrow_down => _ = self.moveVertical(root_focus, true), 1462 .arrow_left => if (text_input.cursor == 0) { 1463 self.exit = .list; 1464 } else try text_input.input(allocator, key, root_focus), 1465 .enter => { 1466 const text = try text_input.text(allocator); 1467 defer allocator.free(text); 1468 try self.assign(allocator, text); 1469 }, 1470 else => try text_input.input(allocator, key, root_focus), 1471 } 1472 } 1473 1474 // arrows move along a row that ends in a ✕. returns whether enter or a 1475 // click hit the ✕. 1476 fn removableRowInput(self: *This, row: *wgt.Box(Widget), key: Key, root_focus: *Focus) bool { 1477 const remove_id = row.children.keys()[row.children.count() - 1]; 1478 switch (key) { 1479 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 1480 self.exit = .list; 1481 }, 1482 .arrow_right => _ = self.moveHorizontal(root_focus, true), 1483 .arrow_up => _ = self.moveVertical(root_focus, false), 1484 .arrow_down => _ = self.moveVertical(root_focus, true), 1485 .enter => return row.getFocus().child_id == remove_id, 1486 .mouse => |mouse| return inp.leftClickOn(root_focus, remove_id, mouse), 1487 else => {}, 1488 } 1489 return false; 1490 } 1491 1492 fn labelsInput(self: *This, child_index: usize, labels: *WordFlow, key: Key, root_focus: *Focus) void { 1493 const child_id = labels.focus.child_id orelse return; 1494 const cur = labels.indexOfFocusId(child_id) orelse return; 1495 const count = labels.text_boxes.items.len; 1496 switch (key) { 1497 .arrow_left => if (cur > 0) self.focusLabel(child_index, labels, root_focus, cur - 1) else { 1498 self.exit = .list; 1499 }, 1500 .arrow_right => if (cur + 1 < count) self.focusLabel(child_index, labels, root_focus, cur + 1), 1501 .arrow_up => if (labels.rowStep(cur, false)) |index| { 1502 self.focusLabel(child_index, labels, root_focus, index); 1503 } else { 1504 _ = self.moveVertical(root_focus, false); 1505 }, 1506 .arrow_down => if (labels.rowStep(cur, true)) |index| { 1507 self.focusLabel(child_index, labels, root_focus, index); 1508 } else { 1509 _ = self.moveVertical(root_focus, true); 1510 }, 1511 .home => self.focusLabel(child_index, labels, root_focus, 0), 1512 .end => self.focusLabel(child_index, labels, root_focus, count - 1), 1513 else => {}, 1514 } 1515 } 1516 1517 fn focusLabel(self: *This, child_index: usize, labels: *WordFlow, root_focus: *Focus, item: usize) void { 1518 root_focus.setFocus(labels.text_boxes.items[item].getFocus().id); 1519 if (self.session.is_terminal and item < labels.rects.items.len) { 1520 if (self.inner().children.values()[child_index].rect) |flow_rect| { 1521 var rect = labels.rects.items[item]; 1522 rect.x += flow_rect.x; 1523 rect.y += flow_rect.y; 1524 self.scroll.scrollToRect(rect); 1525 } 1526 } 1527 } 1528 1529 fn toolRowFocused(self: *This, root_focus: *Focus) bool { 1530 return self.focusedChild(root_focus) == tool_row_index; 1531 } 1532 1533 pub fn focusFirst(self: *This, root_focus: ?*Focus) bool { 1534 const inner_box = self.inner(); 1535 for (inner_box.children.values(), 0..) |*child, child_index| { 1536 switch (child.widget) { 1537 .box => |*box| { 1538 for (box.children.keys(), box.children.values()) |id, *item| { 1539 if (item.widget.getFocus().mode != .all) continue; 1540 box.getFocus().child_id = id; 1541 break; 1542 } else continue; 1543 }, 1544 else => if (!self.childFocusable(child_index)) continue, 1545 } 1546 if (root_focus) |focus| 1547 self.focusChild(child_index, false, true, focus) 1548 else 1549 inner_box.getFocus().child_id = inner_box.children.keys()[child_index]; 1550 return true; 1551 } 1552 return false; 1553 } 1554 1555 pub fn atTop(self: *This, root_focus: *Focus) bool { 1556 const focused = self.focusedChild(root_focus) orelse return false; 1557 for (0..self.inner().children.count()) |child_index| { 1558 if (!self.childFocusable(child_index)) continue; 1559 return focused == child_index; 1560 } 1561 return false; 1562 } 1563 1564 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 1565 if (comptime wasm) return; 1566 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1567 const author = actor.author; 1568 1569 if (comptime supports_drafts) { 1570 const entry = self.entry orelse return; 1571 if (event_kind == kind and entryDraft(entry)) { 1572 try Data.removeDraft(self.session, allocator, author, &id); 1573 const route = draftsRoute(self.data.identity) orelse return; 1574 try self.session.navigate(route); 1575 return; 1576 } 1577 } 1578 1579 const io = self.session.io orelse return; 1580 const source = self.data.repo_source orelse return; 1581 if (!try source.canModify(io, allocator, actor, event_kind, &id)) return; 1582 const thread_id = evt.parseEventId((self.entry orelse return).id) catch return; 1583 1584 switch (source.repo_kind) { 1585 inline else => |repo_kind| { 1586 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1587 defer any_repo.deinit(io, allocator); 1588 switch (any_repo) { 1589 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 1590 } 1591 }, 1592 } 1593 1594 const id_hex = std.fmt.bytesToHex(id, .lower); 1595 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 1596 try self.session.navigate(route); 1597 } 1598 1599 // assign the typed email, or the email of the user the text names 1600 fn assign(self: *This, allocator: std.mem.Allocator, text: []const u8) !void { 1601 if (comptime wasm) return; 1602 const entry = self.entry orelse return; 1603 const io = self.session.io orelse return; 1604 const source = self.data.repo_source orelse return; 1605 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1606 const thread_id = try evt.parseEventId(entry.id); 1607 if (!try source.canModify(io, allocator, actor, kind, &thread_id)) return; 1608 1609 var arena = std.heap.ArenaAllocator.init(allocator); 1610 defer arena.deinit(); 1611 const failure: ?ui.Session.FormFeedback.AssignmentFailure = blk: { 1612 const moment = if (self.session.data.host_kind == .local) null else self.session.haxy_moment; 1613 const email = switch (try ui.assigneeEmail(moment, &arena, text)) { 1614 .email => |email| email, 1615 .failure => |value| break :blk value, 1616 }; 1617 const thread_id_hex = std.fmt.bytesToHex(thread_id, .lower); 1618 switch (source.repo_kind) { 1619 inline else => |repo_kind| { 1620 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1621 defer any_repo.deinit(io, allocator); 1622 switch (any_repo) { 1623 inline else => |*repo| _ = evt.Assignment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id_hex, email, actor.author) catch |err| switch (err) { 1624 error.AlreadyAssigned => break :blk .already_assigned, 1625 error.InvalidFields => break :blk .not_an_email, 1626 else => |e| return e, 1627 }, 1628 } 1629 }, 1630 } 1631 break :blk null; 1632 }; 1633 if (failure) |value| { 1634 self.session.data.form_feedback = .{ .assignment = .{ .failure = value, .text = try self.session.arena.allocator().dupe(u8, text) } }; 1635 return; 1636 } 1637 try self.session.navigate(ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1638 } 1639 1640 fn unassign(self: *This, allocator: std.mem.Allocator, email: []const u8) !void { 1641 if (comptime wasm) return; 1642 const entry = self.entry orelse return; 1643 const io = self.session.io orelse return; 1644 const source = self.data.repo_source orelse return; 1645 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1646 const thread_id = try evt.parseEventId(entry.id); 1647 if (!try source.canModify(io, allocator, actor, kind, &thread_id)) return; 1648 1649 switch (source.repo_kind) { 1650 inline else => |repo_kind| { 1651 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1652 defer any_repo.deinit(io, allocator); 1653 switch (any_repo) { 1654 inline else => |*repo| evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, .assign, &evt.Assignment.idOf(&thread_id, email), actor.author) catch |err| switch (err) { 1655 // a stale button; the refresh below shows the row as it is 1656 error.EventNotFound => {}, 1657 else => |e| return e, 1658 }, 1659 } 1660 }, 1661 } 1662 try self.session.navigate(ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1663 } 1664 1665 fn toggleStatus(self: *This, allocator: std.mem.Allocator) !void { 1666 if (comptime (!has_status or wasm)) return; 1667 const entry = self.entry orelse return; 1668 const io = self.session.io orelse return; 1669 const source = self.data.repo_source orelse return; 1670 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 1671 const author = actor.author; 1672 const status = (statusChange(entryStatus(entry)) orelse return).status; 1673 const id = try evt.parseEventId(entry.id); 1674 if (!try source.canModify(io, allocator, actor, kind, &id)) return; 1675 1676 switch (source.repo_kind) { 1677 inline else => |repo_kind| { 1678 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1679 defer any_repo.deinit(io, allocator); 1680 switch (any_repo) { 1681 inline else => |*repo| try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id, .{ .status = status }, author), 1682 } 1683 }, 1684 } 1685 1686 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 1687 try self.session.navigate(route); 1688 } 1689 }; 1690 } 1691 1692 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 1693 const Self = Data; 1694 const HeaderType = Data.Header; 1695 const Window = Data.Window; 1696 const Entry = Data.Entry; 1697 const Event = Data.Event; 1698 const feedback_tag: std.meta.Tag(ui.Session.FormFeedback) = switch (kind) { 1699 .issue => .issue, 1700 .patch => .patch, 1701 .discuss => .discussion, 1702 else => unreachable, 1703 }; 1704 const FeedbackFailure = if (kind == .patch) ui.Session.FormFeedback.PatchFailure else ui.Session.FormFeedback.ThreadFailure; 1705 const Status = Data.Status; 1706 const has_status = @hasField(Event, "status"); 1707 const supports_conflicts = Event.merge_policy == .field_conflicts; 1708 const supports_drafts = @hasField(Self, "drafts"); 1709 const supports_merge = @hasDecl(Data, "mergeRoute"); 1710 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 1711 const ViewKind = Data.ViewKind; 1712 const thread_name = Data.thread_name; 1713 const DetailType = Data.Detail; 1714 1715 return struct { 1716 const This = @This(); 1717 // a vertical box: the header tabs on top, then a stack holding a 1718 // master-detail split (thread list + description pane) per status list, 1719 // plus the labels view. 1720 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 1721 data: *const Self, 1722 session: *ui.Session, 1723 1724 const header_index: usize = 0; 1725 const stack_index: usize = 1; 1726 // indices within the stack, 1:1 with the header tabs. 1727 const status_count = @typeInfo(Status).@"enum".field_names.len; 1728 const labels_view_index: usize = status_count; 1729 // the new-thread form, or the edit, comment, or resolve form when the page was 1730 // loaded at one of their urls. 1731 const form_view_index: usize = labels_view_index + 1; 1732 // the conflicts split; the tab and stack child exist only when the repo 1733 // has conflicted threads. 1734 const drafts_view_index: usize = form_view_index + 1; 1735 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 1736 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 1737 // indices within a split (the horizontal box inside the stack). 1738 const list_index: usize = 0; 1739 const detail_index: usize = 1; 1740 const list_max_width: usize = 35; 1741 const detail_min_width: usize = 40; 1742 1743 pub fn viewIndex(view: ViewKind) usize { 1744 const name = @tagName(view); 1745 if (std.mem.eql(u8, name, "labels")) return labels_view_index; 1746 if (std.mem.eql(u8, name, "new") or std.mem.eql(u8, name, "edit") or std.mem.eql(u8, name, "publish") or std.mem.eql(u8, name, "merge") or std.mem.eql(u8, name, "new_comment") or std.mem.eql(u8, name, "edit_comment") or std.mem.eql(u8, name, "remove") or std.mem.eql(u8, name, "resolve")) return form_view_index; 1747 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 1748 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 1749 if (std.mem.eql(u8, name, "description")) unreachable; 1750 inline for (@typeInfo(Status).@"enum".field_names, 0..) |status_name, index| { 1751 if (std.mem.eql(u8, name, status_name)) return index; 1752 } 1753 unreachable; 1754 } 1755 1756 const Confirmation = enum { remove, publish, merge }; 1757 1758 fn confirmation(view: ViewKind) ?Confirmation { 1759 if (view == .remove) return .remove; 1760 if (supports_drafts and std.mem.eql(u8, @tagName(view), "publish")) return .publish; 1761 if (supports_merge and std.mem.eql(u8, @tagName(view), "merge")) return .merge; 1762 return null; 1763 } 1764 1765 // the status a status split lists; the conflicts split has none (its 1766 // threads carry their own). 1767 fn splitStatus(index: usize) Status { 1768 return @fromBackingInt(@intCast(index)); 1769 } 1770 1771 fn entryConflicted(entry: Entry) bool { 1772 return if (supports_conflicts) entry.conflicted else false; 1773 } 1774 1775 fn entryStatus(entry: Entry) Status { 1776 if (comptime has_status) return Data.statusKind(entry.record.event); 1777 return @fromBackingInt(@intCast(0)); 1778 } 1779 1780 fn entryDraft(entry: Entry) bool { 1781 return if (supports_drafts) entry.draft else false; 1782 } 1783 1784 fn listRoute(identity: []const u8, status: Status, label: []const u8, selected: []const u8) ?ui.RoutablePage { 1785 return Data.listRoute(identity, status, label, selected); 1786 } 1787 1788 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 1789 return if (supports_drafts) Data.draftsRoute(identity) else null; 1790 } 1791 1792 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1793 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 1794 } 1795 1796 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1797 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 1798 } 1799 1800 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1801 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 1802 } 1803 1804 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 1805 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 1806 } 1807 1808 // a list link keeps the filters the page carries: the label, and the 1809 // search query and url-encoded assignee when set 1810 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, id: []const u8, search: []const u8, assignee: []const u8) ![]const u8 { 1811 var route = listRoute(identity, status, label, id) orelse return error.RouteTooLong; 1812 if (search.len != 0) route = route.withSearch(search) orelse return error.RouteTooLong; 1813 if (assignee.len != 0) route = route.withEncodedAssignee(assignee) orelse return error.RouteTooLong; 1814 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1815 } 1816 1817 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 1818 const selected = std.mem.eql(u8, id, data.selected_id); 1819 const route = (if (selected and data.description_page) 1820 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 1821 else if (selected) 1822 if (data.comment_page) |page| 1823 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 1824 else 1825 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 1826 else 1827 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 1828 return page_arena.allocator().print("ai:{s}", .{try route.toUrl(page_arena)}); 1829 } 1830 1831 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 1832 if (supports_conflicts and status_maybe == null and !drafts) { 1833 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 1834 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1835 } 1836 if (drafts and id.len == 0) { 1837 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 1838 return page_arena.allocator().print("a:{s}", .{try route.toUrl(page_arena)}); 1839 } 1840 return listLink(page_arena, data.identity, status_maybe orelse @fromBackingInt(@intCast(0)), data.label, id, data.search orelse "", assigneeFilter(data)); 1841 } 1842 1843 fn labelLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, label: []const u8, search: []const u8, assignee: []const u8) ![]const u8 { 1844 const encoded = try ui.urlEncodeRef(page_arena.allocator(), label); 1845 return listLink(page_arena, identity, status, encoded, "", search, assignee); 1846 } 1847 1848 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 1849 var outer = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 1850 errdefer outer.deinit(allocator); 1851 1852 // the tabs at the top. 1853 { 1854 var hdr = try Data.initHeader(allocator, session, data); 1855 errdefer hdr.deinit(allocator); 1856 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 1857 } 1858 1859 // the stack enters `outer` before its children enter it, so an error 1860 // frees each child exactly once. 1861 { 1862 var stack = try wgt.Stack(Widget).init(allocator); 1863 errdefer stack.deinit(allocator); 1864 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1865 } 1866 const stack = &outer.children.values()[stack_index].widget.stack; 1867 1868 inline for (@typeInfo(Status).@"enum".field_values) |value| { 1869 var split = try initSplit(allocator, session, data, @fromBackingInt(@intCast(value)), false); 1870 errdefer split.deinit(allocator); 1871 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1872 } 1873 1874 // the labels view 1875 { 1876 var tf = try WordFlow.init(allocator); 1877 errdefer tf.deinit(allocator); 1878 var items: std.ArrayList(WordFlow.Item) = .empty; 1879 defer items.deinit(allocator); 1880 // when filtered, the first item clears the filter 1881 if (data.label.len != 0) 1882 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @fromBackingInt(@intCast(0)), "", "", data.search orelse "", assigneeFilter(data)) }); 1883 for (data.labels) |label| 1884 try items.append(allocator, .{ .text = label, .link = try labelLink(session.page_arena, data.identity, @fromBackingInt(@intCast(0)), label, data.search orelse "", assigneeFilter(data)) }); 1885 try tf.setItems(allocator, items.items); 1886 try stack.children.put(allocator, tf.getFocus().id, .{ .word_flow = tf }); 1887 } 1888 1889 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1890 // its place, prefilled with the selected thread's content. a viewer 1891 // who can't write in this tab gets the unauthorized view instead, 1892 // while local mode always writes. 1893 if (session.data.host_kind == .local or data.viewer != null) { 1894 if (confirmation(data.view)) |confirmation_kind| { 1895 const aa = session.page_arena.allocator(); 1896 const route = switch (confirmation_kind) { 1897 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1898 .publish => publishRoute(data.identity, data.selected_id), 1899 .merge => mergeRoute(data.identity, data.selected_id), 1900 } orelse return error.RouteTooLong; 1901 const page_url = try route.toUrl(session.page_arena); 1902 const action = try aa.print("form:{s}", .{page_url}); 1903 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1904 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1905 var buttons: [2]ConfirmationButton = undefined; 1906 buttons[0] = .{ .label = switch (confirmation_kind) { 1907 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1908 .publish => "publish patch", 1909 .merge => "merge patch", 1910 } }; 1911 var button_count: usize = 1; 1912 if (confirmation_kind == .merge) { 1913 if (comptime supports_merge) { 1914 const availability = if (data.selectedThread()) |entry| 1915 if (Data.canMerge(entry.*, session)) entry.mergeability else pch.Mergeability{} 1916 else 1917 pch.Mergeability{}; 1918 buttons[0].bottom_label = mergeBottomLabel(availability.source); 1919 if (availability.source != .clean) buttons[0].action = ""; 1920 buttons[1] = .{ 1921 .label = "squash and merge patch", 1922 .bottom_label = mergeBottomLabel(availability.squash), 1923 .action = if (availability.squash == .clean) try aa.print("{s}{s}/squash", .{ ui.submit_action_prefix, page_url }) else "", 1924 }; 1925 button_count += 1; 1926 } else unreachable; 1927 } 1928 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1929 errdefer center.deinit(allocator); 1930 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1931 } else if (data.view == .new_comment or data.view == .edit_comment) { 1932 const editing = data.view == .edit_comment; 1933 const route = if (editing) 1934 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1935 else 1936 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1937 const page_url = try route.toUrl(session.page_arena); 1938 const action = try session.page_arena.allocator().print("form:{s}", .{page_url}); 1939 const page = data.comment_page; 1940 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1941 const parent_route = if (editing) 1942 if (top_level) 1943 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1944 else if (page) |p| 1945 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1946 else 1947 null 1948 else if (page) |p| 1949 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1950 else 1951 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 1952 const parent_author: ui.Author = if (editing) 1953 if (top_level) 1954 if (data.selectedThread()) |entry| entry.author else .unknown 1955 else if (page) |p| 1956 p.selected.parent_author orelse .unknown 1957 else 1958 .unknown 1959 else if (page) |p| 1960 p.selected.author 1961 else if (data.selectedThread()) |entry| 1962 entry.author 1963 else 1964 .unknown; 1965 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 1966 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 1967 errdefer form.deinit(allocator); 1968 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1969 } else if (supports_conflicts and data.view == .resolve) { 1970 // on the web the page grows to the form's height and the 1971 // browser scrolls it; the terminal scrolls the form itself 1972 var form_widget: Widget = blk: { 1973 var form = try initResolveForm(allocator, session, data); 1974 errdefer form.deinit(allocator); 1975 break :blk if (session.is_terminal) 1976 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 1977 else 1978 .{ .box = form }; 1979 }; 1980 errdefer form_widget.deinit(allocator); 1981 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 1982 } else { 1983 const aa = session.page_arena.allocator(); 1984 const route = if (data.view == .edit) 1985 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 1986 else 1987 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 1988 const action = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}); 1989 const record = if (data.view == .edit) 1990 (if (data.selectedThread()) |entry| &entry.record else null) 1991 else 1992 null; 1993 var form = try initThreadForm(allocator, session, data, action, record); 1994 errdefer form.deinit(allocator); 1995 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1996 } 1997 } else { 1998 var message = try ui.Unauthorized.View.init(allocator); 1999 errdefer message.deinit(allocator); 2000 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 2001 } 2002 2003 if (supports_drafts) { 2004 var split = try initSplit(allocator, session, data, null, true); 2005 errdefer split.deinit(allocator); 2006 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 2007 } 2008 2009 // the conflicts split; its tab exists only when the repo has 2010 // conflicts, so skip the child too to keep the stack 1:1 with the 2011 // tabs. 2012 if (supports_conflicts and data.conflicts.count > 0) { 2013 var split = try initSplit(allocator, session, data, null, false); 2014 errdefer split.deinit(allocator); 2015 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 2016 } 2017 2018 // the stack starts on the page's view. 2019 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 2020 2021 // focus entering the view lands on the tabs first. 2022 outer.getFocus().child_id = outer.children.keys()[header_index]; 2023 2024 return .{ 2025 .box = outer, 2026 .data = data, 2027 .session = session, 2028 }; 2029 } 2030 2031 // the master-detail split showing `status`'s window, or the conflicts 2032 // window when null. 2033 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 2034 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2035 errdefer box.deinit(allocator); 2036 2037 const win = if (status_maybe) |status| data.window(status) else if (drafts and supports_drafts) &data.drafts else if (supports_conflicts) &data.conflicts else unreachable; 2038 2039 // the thread list (one focusable row per title), plus a "next" link that 2040 // reloads the page rooted at the following thread. 2041 { 2042 var list_scroll = blk: { 2043 var list_box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 2044 errdefer list_box.deinit(allocator); 2045 if (win.prev_id) |prev| 2046 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 2047 for (win.items) |entry| { 2048 const label: []const u8 = if (entry.comments.count > 0) 2049 try session.page_arena.allocator().print(" {d} comments ", .{entry.comments.count}) 2050 else 2051 ""; 2052 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 2053 } 2054 if (win.next_id) |next| 2055 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 2056 // select the window's first thread (past a leading "previous" 2057 // row) so its description shows on load. 2058 if (win.items.len > 0) 2059 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 2060 else if (list_box.children.count() > 0) 2061 list_box.getFocus().child_id = list_box.children.keys()[0]; 2062 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 2063 }; 2064 errdefer list_scroll.deinit(allocator); 2065 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 2066 } 2067 2068 // frame the detail pane inside the master-detail split, below the 2069 // way back to the list, shown only while the list is hidden 2070 { 2071 var frame = try wgt.Box(Widget).init(allocator, .{ .border = .hidden, .direction = .vert }); 2072 errdefer frame.deinit(allocator); 2073 frame.getFocus().mode = .mouse; 2074 try widget.addListBack(allocator, &frame, "← back to " ++ thread_name ++ " list"); 2075 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 2076 errdefer detail_view.deinit(allocator); 2077 frame.getFocus().child_id = detail_view.getFocus().id; 2078 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 2079 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 2080 } 2081 2082 // the page opens on the list, unless the url names a thread 2083 box.getFocus().child_id = box.children.keys()[if (data.selected_id.len > 0) detail_index else list_index]; 2084 return box; 2085 } 2086 2087 // a thread form, plus the target branch for patches, prefilled from 2088 // `record` when given. its form: subtree makes the web renderer POST 2089 // the inputs to `action`'s route. 2090 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 2091 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2092 errdefer box.deinit(allocator); 2093 box.getFocus().kind = .{ .custom = action }; 2094 2095 const saved_form = session.formFeedback(feedback_tag); 2096 const saved_fields = if (saved_form) |saved| saved.fields else null; 2097 2098 { 2099 var title = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " title " }, .name = "title", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2100 errdefer title.deinit(allocator); 2101 title.getFocus().mode = .all; 2102 if (saved_fields) |saved| 2103 try title.setContent(allocator, saved.title) 2104 else if (record) |r| 2105 try title.setContent(allocator, r.event.title); 2106 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2107 box.getFocus().child_id = title.getFocus().id; 2108 } 2109 2110 { 2111 var labels = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " labels (separate with spaces) " }, .name = "labels", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2112 errdefer labels.deinit(allocator); 2113 labels.getFocus().mode = .all; 2114 if (saved_fields) |saved| 2115 try labels.setContent(allocator, saved.labels) 2116 else if (record) |r| 2117 try labels.setContent(allocator, r.event.labels); 2118 try box.children.put(allocator, labels.getFocus().id, .{ .widget = .{ .text_input = labels }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2119 } 2120 2121 { 2122 var description = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " description (markdown supported) " }, .name = "description", .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 2123 errdefer description.deinit(allocator); 2124 description.getFocus().mode = .all; 2125 if (saved_fields) |saved| 2126 try description.setContent(allocator, saved.description) 2127 else if (record) |r| 2128 try description.setContent(allocator, r.event.description); 2129 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 2130 } 2131 2132 if (supports_drafts and record == null) { 2133 var source = try Data.Source.init(allocator, session, if (saved_fields) |saved| saved.source_branch else "", if (saved_fields) |saved| saved.source_kind else .fork); 2134 errdefer source.deinit(allocator); 2135 try box.children.put(allocator, source.getFocus().id, .{ .widget = .{ .patch_source = source }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2136 } 2137 2138 if (supports_drafts) { 2139 var target = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " target branch " }, .name = "target_branch", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2140 errdefer target.deinit(allocator); 2141 target.getFocus().mode = .all; 2142 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 2143 try target.setContent(allocator, content); 2144 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2145 } 2146 2147 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 2148 2149 return box; 2150 } 2151 2152 fn initCommentForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, author: ui.Author, parent_route: ui.RoutablePage, initial_body: ?[]const u8) !wgt.Box(Widget) { 2153 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2154 errdefer box.deinit(allocator); 2155 box.getFocus().kind = .{ .custom = action }; 2156 2157 { 2158 var parent = try ui.authorBox(allocator, session.page_arena, author); 2159 errdefer parent.deinit(allocator); 2160 parent.options.top_label.text = " replying to "; 2161 parent.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 2162 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2163 box.getFocus().child_id = parent.getFocus().id; 2164 } 2165 2166 { 2167 var body = try wgt.TextInput.init(allocator, .{ 2168 .top_label = .{ .text = " comment (markdown supported) " }, 2169 .name = "body", 2170 .visible_width = null, 2171 .round_corners = true, 2172 .render_content = session.is_terminal, 2173 .multiline = true, 2174 .visible_height = 5, 2175 .scroll = .{ .fill = true }, 2176 }); 2177 errdefer body.deinit(allocator); 2178 body.getFocus().mode = .all; 2179 if (initial_body) |text| try body.setContent(allocator, text); 2180 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 2181 } 2182 2183 try addSubmitButtonLabeled(allocator, &box, "submit"); 2184 return box; 2185 } 2186 2187 const ConfirmationButton = struct { 2188 label: []const u8, 2189 bottom_label: []const u8 = "", 2190 action: []const u8 = "submit", 2191 }; 2192 2193 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 2194 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 2195 errdefer box.deinit(allocator); 2196 box.getFocus().kind = .{ .custom = action }; 2197 2198 var prompt = try wgt.Text.init(allocator, "are you sure?"); 2199 errdefer prompt.deinit(allocator); 2200 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 2201 2202 for (buttons, 0..) |options, index| { 2203 var button = try wgt.TextBox.init(allocator, options.label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = options.bottom_label } }); 2204 errdefer button.deinit(allocator); 2205 button.getFocus().mode = .all; 2206 button.getFocus().kind = .{ .custom = options.action }; 2207 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 2208 if (index == 0) box.getFocus().child_id = button.getFocus().id; 2209 } 2210 2211 return Center.init(allocator, .{ .box = box }); 2212 } 2213 2214 // a form's submit button, then a spacer absorbing the leftover 2215 // min-height the box hands its last child, so the button keeps its 2216 // natural height 2217 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 2218 { 2219 var submit = try SubmitButton.initLabeled(allocator, label); 2220 errdefer submit.deinit(allocator); 2221 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 2222 } 2223 { 2224 var spacer = try Spacer.init(allocator); 2225 errdefer spacer.deinit(allocator); 2226 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 2227 } 2228 } 2229 2230 // the conflict resolve form. the "use this" links are navigations that 2231 // flip the url's theirs: pick, reloading the prefills; one submit 2232 // settles every conflict. the form: subtree makes the web renderer POST 2233 // to the resolve route. 2234 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 2235 const aa = session.page_arena.allocator(); 2236 // init only picks the resolve view once it has read the conflict 2237 const conflict = if (data.conflict) |*c| c else unreachable; 2238 2239 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 2240 errdefer box.deinit(allocator); 2241 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 2242 box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{try route.toUrl(session.page_arena)}) }; 2243 2244 if (conflict.title) |*fc| { 2245 try addLabel(allocator, &box, "title conflict:"); 2246 try addFieldConflict(allocator, &box, session, data, "title", fc); 2247 } 2248 if (conflict.labels) |*fc| { 2249 try addLabel(allocator, &box, "labels conflict:"); 2250 try addFieldConflict(allocator, &box, session, data, "labels", fc); 2251 } 2252 if (comptime @hasField(Data.Conflict, "status")) { 2253 if (conflict.status) |*fc| { 2254 try addLabel(allocator, &box, "status conflict:"); 2255 try addAtomicConflict(allocator, &box, session, data, "status", fc); 2256 } 2257 } 2258 if (comptime @hasField(Data.Conflict, "revision")) { 2259 if (conflict.revision) |*fc| { 2260 try addLabel(allocator, &box, "revision conflict:"); 2261 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 2262 } 2263 if (conflict.target_branch) |*fc| { 2264 try addLabel(allocator, &box, "target branch conflict:"); 2265 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 2266 } 2267 } 2268 2269 if (conflict.description) |*desc| { 2270 try addLabel(allocator, &box, "description conflict:"); 2271 var hunk_index: usize = 0; 2272 for (desc.chunks, 0..) |*chunk, chunk_index| { 2273 switch (chunk.*) { 2274 .same => |text| { 2275 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .word }); 2276 errdefer tb.deinit(allocator); 2277 tb.getFocus().mode = .all; 2278 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2279 }, 2280 // an auto-resolved chunk: only one side changed it, so its 2281 // version stands, shown distinctly with its author 2282 .auto => |auto| { 2283 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 2284 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 2285 const label = switch (author) { 2286 .user_name, .email => |name| try aa.print(" {s} by {s} ", .{ verb, name }), 2287 .unknown => try aa.print(" {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 2288 }; 2289 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2290 errdefer tb.deinit(allocator); 2291 tb.getFocus().mode = .all; 2292 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2293 }, 2294 .conflict => |hunk| { 2295 // a blank row on each side sets the group apart from 2296 // the flowing shared and auto-resolved chunks 2297 if (chunk_index > 0) try addGap(allocator, &box); 2298 const name = try aa.print("d{d}", .{hunk_index}); 2299 hunk_index += 1; 2300 const picked_theirs = data.theirsPicked(name); 2301 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 2302 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 2303 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal, .multiline = true }); 2304 errdefer resolution_input.deinit(allocator); 2305 resolution_input.getFocus().mode = .all; 2306 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 2307 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2308 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 2309 }, 2310 } 2311 } 2312 } 2313 2314 try addSubmitButtonLabeled(allocator, &box, "submit"); 2315 2316 box.getFocus().child_id = box.children.keys()[0]; 2317 return box; 2318 } 2319 2320 // one conflicted scalar field: both sides' version rows, then the 2321 // resolution input prefilled from the picked side. 2322 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2323 const aa = session.page_arena.allocator(); 2324 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2325 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2326 2327 var resolution_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " resolution " }, .name = name, .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 2328 errdefer resolution_input.deinit(allocator); 2329 resolution_input.getFocus().mode = .all; 2330 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 2331 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 2332 } 2333 2334 // atomic fields can only pick one complete side 2335 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 2336 const aa = session.page_arena.allocator(); 2337 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 2338 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 2339 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 2340 .border = .single, 2341 .round_corners = true, 2342 .wrap_kind = .word, 2343 .top_label = .{ .text = " resolution " }, 2344 }); 2345 errdefer selected.deinit(allocator); 2346 selected.getFocus().mode = .all; 2347 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 2348 } 2349 2350 // a blank row setting a conflict group apart from its neighbors 2351 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 2352 var text = try wgt.Text.init(allocator, " "); 2353 errdefer text.deinit(allocator); 2354 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 2355 } 2356 2357 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 2358 var label = try SectionLabel.init(allocator, content); 2359 errdefer label.deinit(allocator); 2360 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 2361 } 2362 2363 // one side of a conflict: the "use this" link on the left, the version 2364 // itself as a labeled read-only box. 2365 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 2366 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2367 errdefer row.deinit(allocator); 2368 2369 { 2370 const use_label = "use this"; 2371 var use = try wgt.TextBox.init(allocator, use_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 2372 errdefer use.deinit(allocator); 2373 use.getFocus().mode = .all; 2374 use.getFocus().kind = .{ .custom = link }; 2375 try row.children.put(allocator, use.getFocus().id, .{ 2376 .widget = .{ .text_box = use }, 2377 .rect = null, 2378 .min_size = .{ .width = use_label.len + 2, .height = null }, 2379 .max_size = .{ .width = use_label.len + 2, .height = null }, 2380 }); 2381 } 2382 2383 { 2384 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = label } }); 2385 errdefer tb.deinit(allocator); 2386 tb.getFocus().mode = .all; 2387 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2388 } 2389 2390 row.getFocus().child_id = row.children.keys()[0]; 2391 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 2392 } 2393 2394 // the label of one side's version box, naming its author 2395 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 2396 return switch (author) { 2397 .user_name, .email => |name| try aa.print(" edited by {s} ", .{name}), 2398 .unknown => if (ours) " current version " else " their version ", 2399 }; 2400 } 2401 2402 // the a: link that reloads the resolve page with `name` picked from ours 2403 // or theirs, reprefilling its resolution input 2404 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 2405 const aa = session.page_arena.allocator(); 2406 var picks: std.ArrayList(u8) = .empty; 2407 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 2408 while (it.next()) |pick| { 2409 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 2410 if (picks.items.len > 0) try picks.append(aa, ','); 2411 try picks.appendSlice(aa, pick); 2412 } 2413 if (theirs) { 2414 if (picks.items.len > 0) try picks.append(aa, ','); 2415 try picks.appendSlice(aa, name); 2416 } 2417 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 2418 return aa.print("a:{s}", .{try route.toUrl(session.page_arena)}); 2419 } 2420 2421 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 2422 var row = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = bottom_label } }); 2423 errdefer row.deinit(allocator); 2424 row.getFocus().mode = .all; 2425 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 2426 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 2427 } 2428 2429 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 2430 self.box.deinit(allocator); 2431 } 2432 2433 fn header(self: *This) *HeaderType { 2434 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 2435 } 2436 2437 fn viewStack(self: *This) *wgt.Stack(Widget) { 2438 return &self.box.children.values()[stack_index].widget.stack; 2439 } 2440 2441 // `index`'s master-detail split inside the stack. 2442 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 2443 return &self.viewStack().children.values()[index].box; 2444 } 2445 2446 // the labels view's flow inside the stack. 2447 fn labelsView(self: *This) *WordFlow { 2448 return &self.viewStack().children.values()[labels_view_index].word_flow; 2449 } 2450 2451 // the non-confirmation form inside the stack 2452 fn formBox(self: *This) ?*wgt.Box(Widget) { 2453 return switch (self.viewStack().children.values()[form_view_index]) { 2454 .box => |*box| box, 2455 // the resolve form sits inside a scroll on the terminal 2456 .scroll => |*scroll| &scroll.child.box, 2457 else => null, 2458 }; 2459 } 2460 2461 fn threadForm(self: *This) ?*wgt.Box(Widget) { 2462 if (supports_conflicts and self.data.view == .resolve) return null; 2463 return switch (self.data.view) { 2464 .new_comment, .edit_comment => null, 2465 else => self.formBox(), 2466 }; 2467 } 2468 2469 fn formField(form: *wgt.Box(Widget), name: []const u8) !*wgt.TextInput { 2470 for (form.children.values()) |*child| switch (child.widget) { 2471 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 2472 else => {}, 2473 }; 2474 return error.MissingFormField; 2475 } 2476 2477 fn formWidget(form: *wgt.Box(Widget), comptime widget_kind: std.meta.Tag(Widget)) ?*@FieldType(Widget, @tagName(widget_kind)) { 2478 for (form.children.values()) |*child| { 2479 if (child.widget == widget_kind) return &@field(child.widget, @tagName(widget_kind)); 2480 } 2481 return null; 2482 } 2483 2484 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 2485 return switch (self.viewStack().children.values()[form_view_index]) { 2486 .center => |*center| switch (center.child.*) { 2487 .box => |*box| box, 2488 else => null, 2489 }, 2490 else => null, 2491 }; 2492 } 2493 2494 // the resolve form's scroll on the terminal (the web page scrolls itself) 2495 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 2496 return switch (self.viewStack().children.values()[form_view_index]) { 2497 .scroll => |*scroll| scroll, 2498 else => null, 2499 }; 2500 } 2501 2502 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 2503 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 2504 } 2505 2506 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 2507 return &self.listScroll(index).child.box; 2508 } 2509 2510 fn detailFrame(self: *This, index: usize) *wgt.Box(Widget) { 2511 return &self.resultsBox(index).children.values()[detail_index].widget.box; 2512 } 2513 2514 fn detail(self: *This, index: usize) *DetailType { 2515 return &@field(self.detailFrame(index).children.values()[widget.list_pane_index].widget, Data.detail_widget_name); 2516 } 2517 2518 fn window(self: *This, index: usize) *const Window { 2519 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2520 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2521 return self.data.window(splitStatus(index)); 2522 } 2523 2524 fn stackSelectedIndex(self: *This) ?usize { 2525 const stack = self.viewStack(); 2526 const cid = stack.getFocus().child_id orelse return null; 2527 return stack.children.getIndex(cid); 2528 } 2529 2530 // the stack's selected master-detail split (null when the labels view or the 2531 // new-thread form shows). 2532 fn selectedSplitIndex(self: *This) ?usize { 2533 const idx = self.stackSelectedIndex() orelse return null; 2534 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2535 } 2536 2537 fn detailActive(self: *This, index: usize) bool { 2538 const rb = self.resultsBox(index); 2539 const cid = rb.getFocus().child_id orelse return false; 2540 return rb.children.getIndex(cid) == detail_index; 2541 } 2542 2543 fn headerActive(self: *This) bool { 2544 const cid = self.box.getFocus().child_id orelse return false; 2545 return self.box.children.getIndex(cid) == header_index; 2546 } 2547 2548 fn labelsViewActive(self: *This) bool { 2549 if (self.headerActive()) return false; 2550 return self.stackSelectedIndex() == labels_view_index; 2551 } 2552 2553 fn formViewActive(self: *This) bool { 2554 if (self.headerActive()) return false; 2555 return self.stackSelectedIndex() == form_view_index; 2556 } 2557 2558 // the selected thread's index, or null when a window-navigation row is 2559 // selected (a leading "previous" row shifts the thread rows down by one). 2560 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2561 const lb = self.listBox(index); 2562 const cid = lb.getFocus().child_id orelse return null; 2563 const idx = lb.children.getIndex(cid) orelse return null; 2564 const win = self.window(index); 2565 const lead: usize = if (win.prev_id != null) 1 else 0; 2566 if (idx < lead or idx - lead >= win.items.len) return null; 2567 return idx - lead; 2568 } 2569 2570 // swap the detail pane to the selected thread when it changes 2571 fn refreshDetail(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2572 const selected = self.selectedThreadIndex(index) orelse return; 2573 const entry = self.window(index).items[selected]; 2574 const changed = if (self.detail(index).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2575 if (!changed) return; 2576 try self.detail(index).setEntry(allocator, entry); 2577 widget.setListBackLink(self.detailFrame(index), try rowLink(self.session.page_arena, self.data, entry.id)); 2578 } 2579 2580 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2581 self.clearGrid(); 2582 2583 // each header tab maps 1:1 to a stack child by position 2584 if (self.header().getSelectedIndex()) |index| { 2585 const stack = self.viewStack(); 2586 stack.getFocus().child_id = stack.children.keys()[index]; 2587 } 2588 2589 if (self.selectedSplitIndex()) |i| { 2590 // whether the detail pane fits beside the list, which decides 2591 // the list cap below and whether the detail carries a way back 2592 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2593 try self.refreshDetail(allocator, i); 2594 widget.showListBack(self.detailFrame(i), !both_panes_fit); 2595 2596 // the selected list row shows a border (the focused TextBox 2597 // upgrades it to a double border itself); the rest stay borderless. 2598 const lb = self.listBox(i); 2599 for (lb.children.keys(), lb.children.values()) |id, *child| { 2600 switch (child.widget) { 2601 .text_box => |*tb| widget.markSelected(tb, lb.getFocus().child_id == id), 2602 else => {}, 2603 } 2604 } 2605 2606 // cap the list at list_max_width only while the detail pane fits 2607 // beside it. the box drops the detail when the width can't hold 2608 // both minimums, so when it's that narrow we lift the cap and let 2609 // the list fill the whole width. 2610 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2611 2612 // stretch the detail pane across the rest of the width so it fills 2613 // the area rather than shrinking to its content; its scroll fills 2614 // the pane. 2615 if (constraint.max_size.width) |w| { 2616 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2617 } else { 2618 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2619 } 2620 } 2621 2622 // refresh the form inputs' entries in the session's focus-id -> input 2623 // map with this frame's addresses, so the web/wasm form handling can 2624 // find them by focus id 2625 if (self.threadForm()) |form| { 2626 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2627 (try formField(form, "title")).options.top_label.text = if (failure == .required_title) " title (required) " else " title "; 2628 if (comptime supports_drafts) { 2629 if (formWidget(form, .patch_source)) |source| { 2630 source.field().options.top_label.text = if (failure == .invalid_source_branch) " source branch (not found) " else if (failure == .same_branch) " source branch (same as target) " else if (failure == .unrelated_branches) " source branch (unrelated) " else " source branch "; 2631 const submit = formWidget(form, .submit_button) orelse return error.MissingFormField; 2632 try submit.setLabel(allocator, switch (source.selectedKind()) { 2633 .fork => "submit draft", 2634 .branch => "submit patch", 2635 }); 2636 } 2637 (try formField(form, "target_branch")).options.top_label.text = 2638 if (failure == .invalid_target_branch) " target branch (invalid) " else if (self.data.view == .edit and failure == .same_branch) " target branch (same as source) " else if (self.data.view == .edit and failure == .unrelated_branches) " target branch (unrelated to source) " else if (self.data.view == .edit and failure == .invalid_source_branch) " target branch (source branch not found) " else " target branch "; 2639 } 2640 } 2641 if (self.formBox()) |form| { 2642 const inputs_arena = self.session.arena.allocator(); 2643 for (form.children.values()) |*child| switch (child.widget) { 2644 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2645 else => {}, 2646 }; 2647 } 2648 2649 try self.box.build(allocator, constraint, root_focus); 2650 } 2651 2652 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2653 // wheel input moves focus like the arrows 2654 const key: Key = switch (raw_key) { 2655 .scroll_up => .arrow_up, 2656 .scroll_down => .arrow_down, 2657 else => raw_key, 2658 }; 2659 2660 if (self.headerActive()) { 2661 // down from the sub-header re-enters the stack if the selected 2662 // view has something to focus; enter in the search box submits 2663 // the query; other keys move within the sub-header. 2664 if (inp.vertDirection(key) == .down) { 2665 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2666 .word_flow => |*tf| tf.text_boxes.items.len > 0, 2667 // nothing focusable in the unauthorized view 2668 .unauthorized => false, 2669 else => true, 2670 } else false; 2671 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2672 } else if (key == .enter) { 2673 if (try self.header().submittedText(allocator)) |submission| { 2674 defer allocator.free(submission.text); 2675 switch (submission.kind) { 2676 .search => try self.submitSearch(submission.text), 2677 .assignee => try self.submitAssignee(submission.text), 2678 } 2679 } 2680 } else { 2681 try self.header().input(allocator, key, root_focus); 2682 } 2683 return; 2684 } 2685 if (self.labelsViewActive()) { 2686 self.labelsViewInput(key, root_focus); 2687 return; 2688 } 2689 if (self.formViewActive()) { 2690 if (confirmation(self.data.view) != null) { 2691 try self.confirmationFormInput(allocator, key, root_focus); 2692 } else if (supports_conflicts and self.data.view == .resolve) { 2693 try self.resolveInput(allocator, key, root_focus); 2694 } else { 2695 try self.formInput(allocator, key, root_focus); 2696 } 2697 return; 2698 } 2699 const i = self.selectedSplitIndex() orelse return; 2700 if (self.detailActive(i)) { 2701 // the way back to the list, by enter or a click 2702 if (widget.listBackId(self.detailFrame(i))) |id| { 2703 if (inp.activated(root_focus, id, key)) return self.focusList(i, root_focus); 2704 if (root_focus.grandchild_id == id) return switch (key) { 2705 .arrow_left => self.focusList(i, root_focus), 2706 .arrow_up => self.focusHeader(root_focus), 2707 .arrow_down => _ = self.detail(i).focusFirst(root_focus), 2708 else => {}, 2709 }; 2710 } 2711 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2712 .none => {}, 2713 .list => self.focusList(i, root_focus), 2714 .header => if (widget.listBackId(self.detailFrame(i))) |id| root_focus.setFocus(id) else self.focusHeader(root_focus), 2715 } 2716 } else { 2717 try self.listInput(allocator, i, key, root_focus); 2718 } 2719 } 2720 2721 // navigate to the typed query's results, keeping the label filter and the 2722 // status list the page shows. an empty box clears a filtered page and 2723 // does nothing on any other one. 2724 fn submitSearch(self: *This, text: []const u8) !void { 2725 if (text.len == 0 and self.data.search == null) return; 2726 const selected = self.header().getSelectedIndex() orelse 0; 2727 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2728 var route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2729 const assignee = assigneeFilter(self.data); 2730 if (assignee.len != 0) route = route.withEncodedAssignee(assignee) orelse return; 2731 try self.session.navigate(route.withSearch(text) orelse return); 2732 } 2733 2734 // navigate to the issues assigned to the typed name or email, keeping 2735 // the label, the search and the status list. an empty box clears the 2736 // filter and does nothing on an unfiltered page. 2737 fn submitAssignee(self: *This, typed: []const u8) !void { 2738 const text = std.mem.trim(u8, typed, " \t"); 2739 if (text.len == 0 and assigneeFilter(self.data).len == 0) return; 2740 const selected = self.header().getSelectedIndex() orelse 0; 2741 const status: Status = if (selected < status_count) splitStatus(selected) else @fromBackingInt(@intCast(0)); 2742 var route = listRoute(self.data.identity, status, self.data.label, "") orelse return; 2743 if (self.data.search) |search| route = route.withSearch(search) orelse return; 2744 try self.session.navigate(route.withEncodedAssignee(try ui.urlEncodeRef(self.session.page_arena.allocator(), text)) orelse return); 2745 } 2746 2747 // arrow keys move the label selection; up from the top row crosses to the 2748 // header tabs. 2749 fn labelsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2750 const tf = self.labelsView(); 2751 const cid = tf.focus.child_id orelse return; 2752 const cur = tf.indexOfFocusId(cid) orelse return; 2753 const count = tf.text_boxes.items.len; 2754 switch (key) { 2755 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2756 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2757 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2758 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2759 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2760 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2761 else => {}, 2762 } 2763 } 2764 2765 fn listInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 2766 // up/down (and the scroll wheel) move the selection a row; page up/down 2767 // jump a fixed amount. right/Enter cross into the detail pane. up from 2768 // the top row crosses into the header tabs. 2769 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2770 const lb = self.listBox(index); 2771 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2772 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2773 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2774 return; 2775 } 2776 switch (key) { 2777 .enter, .arrow_right => self.focusDetail(index, root_focus), 2778 // when the window is too narrow to lay out the detail pane beside 2779 // the list, a click on a row opens it like enter. the row was just 2780 // selected, so the detail is swapped to it ahead of the build. 2781 .mouse => |mouse| if (self.resultsBox(index).children.values()[detail_index].rect == null and widget.clickOnSelectedRow(self.listBox(index), root_focus, mouse)) { 2782 try self.refreshDetail(allocator, index); 2783 self.focusDetail(index, root_focus); 2784 }, 2785 else => {}, 2786 } 2787 } 2788 2789 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2790 const form = self.formBox() orelse return; 2791 const cid = form.getFocus().child_id orelse return; 2792 const cur = form.children.getIndex(cid) orelse return; 2793 const child = &form.children.values()[cur]; 2794 2795 if (child.widget == .text_input) { 2796 const text_input = &child.widget.text_input; 2797 if (text_input.options.multiline and try multilineKeepsKey(text_input, allocator, key)) 2798 return child.widget.input(allocator, key, root_focus); 2799 } 2800 2801 const on_submit = child.widget == .submit_button; 2802 switch (key) { 2803 .arrow_up, .back_tab => return if (formStep(form, cur, false)) |i| 2804 root_focus.setFocus(form.children.keys()[i]) 2805 else 2806 self.focusHeader(root_focus), 2807 .arrow_down, .tab => return if (formStep(form, cur, true)) |i| root_focus.setFocus(form.children.keys()[i]), 2808 .enter => if (on_submit) return self.submitForm(allocator), 2809 .mouse => |mouse| if (on_submit) { 2810 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 2811 }, 2812 else => {}, 2813 } 2814 try child.widget.input(allocator, key, root_focus); 2815 } 2816 2817 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2818 const form = self.confirmationForm() orelse return; 2819 const child_id = form.getFocus().child_id orelse return; 2820 const index = form.children.getIndex(child_id) orelse return; 2821 switch (key) { 2822 .arrow_up, .back_tab => if (index > 1) 2823 root_focus.setFocus(form.children.keys()[index - 1]) 2824 else 2825 self.focusHeader(root_focus), 2826 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2827 .enter => try self.submitConfirmation(allocator, index), 2828 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2829 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2830 try self.submitConfirmation(allocator, button_index); 2831 break; 2832 } 2833 }, 2834 else => {}, 2835 } 2836 } 2837 2838 // the multiline hunk inputs keep enter and any up/down that has a row to 2839 // move to; enter on a "use this" link is a navigation the host follows 2840 // before this runs. 2841 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2842 const form = self.formBox() orelse return; 2843 const cid = form.getFocus().child_id orelse return; 2844 const cur = form.children.getIndex(cid) orelse return; 2845 const child = &form.children.values()[cur]; 2846 2847 if (child.widget == .text_input) { 2848 const ti = &child.widget.text_input; 2849 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2850 return child.widget.input(allocator, key, root_focus); 2851 } 2852 2853 const on_submit = child.widget == .submit_button; 2854 2855 switch (key) { 2856 .arrow_up, .back_tab => if (formStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2857 .arrow_down, .tab => if (formStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2858 .arrow_left, .arrow_right => switch (child.widget) { 2859 .box => |*row| _ = widget.moveInRow(row, root_focus, key == .arrow_right), 2860 else => try child.widget.input(allocator, key, root_focus), 2861 }, 2862 .enter => if (on_submit) try self.submitResolution(allocator), 2863 .mouse => |mouse| if (on_submit) { 2864 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2865 }, 2866 else => try child.widget.input(allocator, key, root_focus), 2867 } 2868 } 2869 2870 // a multiline input keeps enter and any up/down that has a row to move to 2871 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2872 return switch (key) { 2873 .enter => true, 2874 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2875 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2876 else => false, 2877 }; 2878 } 2879 2880 // the neighboring form control, skipping spacers and blank rows 2881 fn formStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2882 var i = cur; 2883 while (true) { 2884 if (down) { 2885 i += 1; 2886 if (i >= form.children.count()) return null; 2887 } else { 2888 if (i == 0) return null; 2889 i -= 1; 2890 } 2891 const child = &form.children.values()[i]; 2892 if (child.hidden) continue; 2893 switch (child.widget) { 2894 .spacer, .text => continue, 2895 else => return i, 2896 } 2897 } 2898 } 2899 2900 // focus the resolve-form child at `i` (a version row focuses its selected 2901 // child) and keep it visible on the terminal 2902 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2903 const child = &form.children.values()[i]; 2904 switch (child.widget) { 2905 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2906 else => root_focus.setFocus(form.children.keys()[i]), 2907 } 2908 if (self.session.is_terminal) { 2909 if (self.resolveScroll()) |sc| { 2910 if (child.rect) |rect| sc.scrollToRect(rect); 2911 } 2912 } 2913 } 2914 2915 // re-emit the conflicted thread's event with the resolved content; any 2916 // event on the thread settles its conflict entry. this is the terminal 2917 // path; the web posts the form to the resolve route. 2918 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2919 if (comptime wasm) return; 2920 const io = self.session.io orelse return; 2921 const src = self.data.repo_source orelse return; 2922 const entry = self.data.selectedThread() orelse return; 2923 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 2924 const form = self.formBox() orelse return; 2925 2926 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2927 var title: ?[]u8 = null; 2928 var labels: ?[]u8 = null; 2929 var hunks: std.ArrayList([]const u8) = .empty; 2930 defer { 2931 if (title) |t| allocator.free(t); 2932 if (labels) |t| allocator.free(t); 2933 for (hunks.items) |h| allocator.free(h); 2934 hunks.deinit(allocator); 2935 } 2936 for (form.children.values()) |*child| switch (child.widget) { 2937 .text_input => |*ti| { 2938 const text = try ti.text(allocator); 2939 errdefer allocator.free(text); 2940 if (std.mem.eql(u8, ti.options.name, "title")) { 2941 title = text; 2942 } else if (std.mem.eql(u8, ti.options.name, "labels")) { 2943 labels = text; 2944 } else { 2945 try hunks.append(allocator, text); 2946 } 2947 }, 2948 else => {}, 2949 }; 2950 2951 const id_bytes = try evt.parseEventId(entry.id); 2952 2953 switch (src.repo_kind) { 2954 inline else => |repo_kind| { 2955 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2956 defer any_repo.deinit(io, allocator); 2957 switch (any_repo) { 2958 inline else => |*repo| { 2959 var resolution: Event.Resolve = .{ 2960 .title = title, 2961 .labels = labels, 2962 .hunks = hunks.items, 2963 }; 2964 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2965 const change = Event.Update{ .resolve = resolution }; 2966 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2967 // leave the form up for correction 2968 error.InvalidFields => return, 2969 else => |e| return e, 2970 }; 2971 }, 2972 } 2973 }, 2974 } 2975 2976 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2977 try self.session.navigate(route); 2978 } 2979 2980 // commit a new or edited comment and navigate to its permalink. this is the 2981 // terminal path; the web posts the form to its comment route. 2982 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2983 if (comptime wasm) return; 2984 const io = self.session.io orelse return; 2985 const src = self.data.repo_source orelse return; 2986 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2987 const author = actor.author; 2988 const form = self.formBox() orelse return; 2989 const body_input = try formField(form, "body"); 2990 const body = try body_input.text(allocator); 2991 defer allocator.free(body); 2992 if (!evt.Comment.fieldsValid(body)) return; 2993 2994 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2995 @memcpy(&thread_id, self.data.selected_id); 2996 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2997 var parent: [evt.event_id_size * 2]u8 = undefined; 2998 @memcpy(&parent, self.data.comment_id); 2999 break :blk parent; 3000 }; 3001 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3002 const editing = self.data.view == .edit_comment; 3003 if (editing) { 3004 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3005 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3006 } 3007 switch (src.repo_kind) { 3008 inline else => |repo_kind| { 3009 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3010 defer any_repo.deinit(io, allocator); 3011 switch (any_repo) { 3012 inline else => |*repo| if (editing) { 3013 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3014 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3015 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3016 } else { 3017 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &parent_id, body, author); 3018 }, 3019 } 3020 }, 3021 } 3022 3023 body_input.clear(allocator); 3024 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3025 try self.session.navigate(route); 3026 } 3027 3028 // terminal form submissions; the web posts to the forms' routes instead 3029 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3030 switch (self.data.view) { 3031 .new_comment, .edit_comment => try self.submitComment(allocator), 3032 .edit => try self.submitEditedThread(allocator), 3033 else => try self.submitNewThread(allocator), 3034 } 3035 } 3036 3037 // commit the new thread to the repo's events branch and navigate to it. 3038 // this is the terminal path; the web posts the form to the thread route, 3039 // so the wasm side never runs (or compiles) the repo access below. 3040 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3041 if (comptime wasm) return; 3042 const io = self.session.io orelse return; 3043 const src = self.data.repo_source orelse return; 3044 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3045 3046 const form = self.threadForm() orelse return; 3047 const title_input = try formField(form, "title"); 3048 const labels_input = try formField(form, "labels"); 3049 const description_input = try formField(form, "description"); 3050 3051 const title = try title_input.text(allocator); 3052 defer allocator.free(title); 3053 const labels = try labels_input.text(allocator); 3054 defer allocator.free(labels); 3055 const description = try description_input.text(allocator); 3056 defer allocator.free(description); 3057 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3058 defer if (comptime supports_drafts) allocator.free(target_branch); 3059 3060 if (!Event.fieldsValid(title, labels)) { 3061 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3062 return; 3063 } 3064 3065 if (comptime supports_drafts) { 3066 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3067 const source_branch = switch (source.selectedKind()) { 3068 .fork => null, 3069 .branch => try source.field().text(allocator), 3070 }; 3071 defer if (source_branch) |branch| allocator.free(branch); 3072 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3073 const failure = FeedbackFailure.fromError(err) orelse return err; 3074 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3075 return; 3076 }; 3077 title_input.clear(allocator); 3078 labels_input.clear(allocator); 3079 description_input.clear(allocator); 3080 (try formField(form, "target_branch")).clear(allocator); 3081 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3082 try self.session.navigate(route); 3083 return; 3084 } 3085 3086 var id_bytes: [evt.event_id_size]u8 = undefined; 3087 io.random(&id_bytes); 3088 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3089 3090 const event = evt.EventWithId{ 3091 .id = event_id_hex, 3092 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3093 .author = actor.author, 3094 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3095 .title = title, 3096 .description = description, 3097 .labels = labels, 3098 }), 3099 }; 3100 3101 switch (src.repo_kind) { 3102 inline else => |repo_kind| { 3103 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3104 defer any_repo.deinit(io, allocator); 3105 switch (any_repo) { 3106 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3107 } 3108 }, 3109 } 3110 3111 // wipe the form so a return visit starts fresh 3112 title_input.clear(allocator); 3113 labels_input.clear(allocator); 3114 description_input.clear(allocator); 3115 3116 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", &event_id_hex) orelse return; 3117 try self.session.navigate(route); 3118 } 3119 3120 // re-emit the selected thread's event with the form's content (status 3121 // preserved), then reload the thread's page. this is the terminal path; 3122 // the web posts the form to the edit route. 3123 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3124 if (comptime wasm) return; 3125 const io = self.session.io orelse return; 3126 const entry = self.data.selectedThread() orelse return; 3127 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3128 const author = actor.author; 3129 3130 const form = self.threadForm() orelse return; 3131 const title_input = try formField(form, "title"); 3132 const labels_input = try formField(form, "labels"); 3133 const description_input = try formField(form, "description"); 3134 3135 const title = try title_input.text(allocator); 3136 defer allocator.free(title); 3137 const labels = try labels_input.text(allocator); 3138 defer allocator.free(labels); 3139 const description = try description_input.text(allocator); 3140 defer allocator.free(description); 3141 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3142 defer if (comptime supports_drafts) allocator.free(target_branch); 3143 3144 if (!Event.fieldsValid(title, labels)) { 3145 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3146 return; 3147 } 3148 const src = self.data.repo_source orelse return; 3149 3150 if (comptime supports_drafts) { 3151 if (entryDraft(entry.*)) { 3152 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3153 const failure = FeedbackFailure.fromError(err) orelse return err; 3154 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3155 return; 3156 }; 3157 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3158 try self.session.navigate(route); 3159 return; 3160 } 3161 } 3162 3163 const id_bytes = try evt.parseEventId(entry.id); 3164 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3165 3166 switch (src.repo_kind) { 3167 inline else => |repo_kind| { 3168 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3169 defer any_repo.deinit(io, allocator); 3170 switch (any_repo) { 3171 inline else => |*repo| if (supports_drafts) { 3172 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3173 .title = title, 3174 .labels = labels, 3175 .description = description, 3176 .target_branch = target_branch, 3177 } }, author) catch |err| { 3178 const failure = FeedbackFailure.fromError(err) orelse return err; 3179 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3180 return; 3181 }; 3182 } else if (has_status) 3183 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3184 .title = title, 3185 .labels = labels, 3186 .description = description, 3187 } }, author) 3188 else 3189 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3190 } 3191 }, 3192 } 3193 3194 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3195 try self.session.navigate(route); 3196 } 3197 3198 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3199 const aa = self.session.arena.allocator(); 3200 const source = if (supports_drafts) blk: { 3201 const form = self.threadForm() orelse break :blk null; 3202 break :blk formWidget(form, .patch_source); 3203 } else null; 3204 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3205 .failure = failure, 3206 .fields = if (comptime supports_drafts) .{ 3207 .title = try aa.dupe(u8, title), 3208 .labels = try aa.dupe(u8, labels), 3209 .description = try aa.dupe(u8, description), 3210 .target_branch = try aa.dupe(u8, target_branch), 3211 .source_branch = if (source) |value| try value.field().text(aa) else "", 3212 .source_kind = if (source) |value| value.selectedKind() else .fork, 3213 } else .{ 3214 .title = try aa.dupe(u8, title), 3215 .labels = try aa.dupe(u8, labels), 3216 .description = try aa.dupe(u8, description), 3217 }, 3218 }); 3219 } 3220 3221 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3222 switch (confirmation(self.data.view) orelse return) { 3223 .publish => if (comptime supports_drafts) { 3224 if (comptime wasm) return; 3225 const entry = self.data.selectedThread() orelse return; 3226 if (!entryDraft(entry.*)) return; 3227 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3228 try self.data.publishDraft(self.session, allocator, author, entry.id); 3229 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", entry.id) orelse return; 3230 try self.session.navigate(route); 3231 } else unreachable, 3232 .merge => if (comptime supports_merge) { 3233 if (comptime wasm) return; 3234 const entry = self.data.selectedThread() orelse return; 3235 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3236 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3237 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3238 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3239 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3240 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3241 try self.session.navigate(route); 3242 return; 3243 }, 3244 else => return err, 3245 }; 3246 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3247 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 3248 try self.session.navigate(route); 3249 } else unreachable, 3250 .remove => try self.submitRemove(allocator), 3251 } 3252 } 3253 3254 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3255 const comment = self.data.comment_id.len != 0; 3256 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3257 const id = evt.parseEventId(event_id) catch return; 3258 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3259 } 3260 3261 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3262 if (comptime wasm) return; 3263 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3264 const author = actor.author; 3265 3266 if (comptime supports_drafts) { 3267 const selected = self.data.selectedThread() orelse return; 3268 if (event_kind == kind and entryDraft(selected.*)) { 3269 try Data.removeDraft(self.session, allocator, author, &id); 3270 const route = draftsRoute(self.data.identity) orelse return; 3271 try self.session.navigate(route); 3272 return; 3273 } 3274 } 3275 3276 const io = self.session.io orelse return; 3277 const src = self.data.repo_source orelse return; 3278 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3279 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3280 3281 switch (src.repo_kind) { 3282 inline else => |repo_kind| { 3283 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3284 defer any_repo.deinit(io, allocator); 3285 switch (any_repo) { 3286 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 3287 } 3288 }, 3289 } 3290 3291 const id_hex = std.fmt.bytesToHex(id, .lower); 3292 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3293 try self.session.navigate(route); 3294 } 3295 3296 // enter the detail pane, landing on the way back to the list when it shows 3297 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3298 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3299 _ = self.detail(index).focusFirst(root_focus); 3300 } 3301 3302 // return to the list. 3303 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3304 root_focus.setFocus(self.listScroll(index).getFocus().id); 3305 } 3306 3307 // cross to the header tabs above the stack. 3308 fn focusHeader(self: *This, root_focus: *Focus) void { 3309 root_focus.setFocus(self.box.children.keys()[header_index]); 3310 } 3311 3312 pub fn clearGrid(self: *This) void { 3313 self.box.clearGrid(); 3314 } 3315 3316 pub fn getGrid(self: This) ?Grid { 3317 return self.box.getGrid(); 3318 } 3319 3320 pub fn getFocus(self: *This) *Focus { 3321 return self.box.getFocus(); 3322 } 3323 3324 // for the parent's "scroll up at the top jumps to the header" check: at the 3325 // top only while the header tabs hold focus, so up from the split 3326 // crosses into the tabs first. 3327 pub fn atTop(self: *This) bool { 3328 return self.headerActive(); 3329 } 3330 }; 3331 } 3332