╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
313 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 314 switch (child.widget) { 315 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 316 else => {}, 317 } 318 } 319 try self.box.build(allocator, constraint, root_focus); 320 } 321 322 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 323 _ = allocator; 324 const current_tab = self.currentTabIndex() orelse return; 325 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 326 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 327 } 328 } 329 330 pub fn clearGrid(self: *Header) void { 331 self.box.clearGrid(); 332 } 333 334 pub fn getGrid(self: Header) ?Grid { 335 return self.box.getGrid(); 336 } 337 338 pub fn getFocus(self: *Header) *Focus { 339 return self.box.getFocus(); 340 } 341 342 pub fn getSelectedIndex(self: Header) ?usize { 343 const index = self.currentTabIndex() orelse return null; 344 return self.tab_ids.values()[index]; 345 } 346 347 fn currentTabIndex(self: Header) ?usize { 348 const child_id = self.box.focus.child_id orelse return null; 349 return self.tab_ids.getIndex(child_id); 350 } 351 }; 352 353 // the selected thread's scrollable contents, shared by repo and fork views 354 pub fn Detail(comptime kind: evt.EventKind, comptime Data: type) type { 355 const Entry = Data.Entry; 356 const Event = Data.Event; 357 const Status = Data.Status; 358 const has_status = @hasField(Event, "status"); 359 const supports_conflicts = Event.merge_policy == .field_conflicts; 360 const supports_drafts = @hasField(Data, "drafts"); 361 const supports_forks = @hasField(Entry, "fork_exists"); 362 const supports_merge = @hasDecl(Data, "mergeRoute"); 363 return struct { 364 const This = @This(); 365 const thread_name = Data.thread_name; 366 367 pub const Exit = enum { none, list, header }; 368 pub const Options = struct { actions: bool = true }; 369 370 scroll: wgt.Scroll(Widget), 371 data: *const Data, 372 session: *ui.Session, 373 entry: ?Entry, 374 title_id: ?usize, 375 description_id: ?usize, 376 author_id: ?usize, 377 actions: bool, 378 exit: Exit, 379 380 const tool_row_index: usize = 0; 381 382 pub fn init(allocator: std.mem.Allocator, data: *const Data, session: *ui.Session, entry: ?Entry, options: Options) !This { 383 var content = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 384 errdefer content.deinit(allocator); 385 var detail_scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = content }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 386 errdefer detail_scroll.deinit(allocator); 387 388 var self: This = .{ 389 .scroll = detail_scroll, 390 .data = data, 391 .session = session, 392 .entry = null, 393 .title_id = null, 394 .description_id = null, 395 .author_id = null, 396 .actions = options.actions, 397 .exit = .none, 398 }; 399 if (entry) |value| try self.setEntry(allocator, value); 400 return self; 401 } 402 403 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 404 self.scroll.deinit(allocator); 405 } 406 407 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 408 self.clearGrid(); 409 try self.scroll.build(allocator, constraint, root_focus); 410 } 411 412 pub fn input(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 413 _ = try self.inputWithExit(allocator, key, root_focus); 414 } 415 416 pub fn clearGrid(self: *This) void { 417 self.scroll.clearGrid(); 418 } 419 420 pub fn getGrid(self: This) ?Grid { 421 return self.scroll.getGrid(); 422 } 423 424 pub fn getFocus(self: *This) *Focus { 425 return self.scroll.getFocus(); 426 } 427 428 fn inner(self: *This) *wgt.Box(Widget) { 429 return &self.scroll.child.box; 430 } 431 432 fn toolRow(self: *This) *wgt.Box(Widget) { 433 return &self.inner().children.values()[tool_row_index].widget.box; 434 } 435 436 fn entryConflicted(entry: Entry) bool { 437 return if (supports_conflicts) entry.conflicted else false; 438 } 439 440 fn entryStatus(entry: Entry) Status { 441 if (comptime has_status) return Data.statusKind(entry.record.event); 442 return @enumFromInt(0); 443 } 444 445 fn entryDraft(entry: Entry) bool { 446 return if (supports_drafts) entry.draft else false; 447 } 448 449 fn entryHasFork(entry: Entry) bool { 450 return if (supports_forks) entry.fork_exists else false; 451 } 452 453 const StatusChange = struct { 454 action: []const u8, 455 status: Status, 456 }; 457 458 fn statusChange(status: Status) ?StatusChange { 459 const name = @tagName(status); 460 if (std.mem.eql(u8, name, "open")) return .{ 461 .action = "close", 462 .status = std.meta.stringToEnum(Status, "closed") orelse return null, 463 }; 464 if (std.mem.eql(u8, name, "closed")) return .{ 465 .action = "open", 466 .status = std.meta.stringToEnum(Status, "open") orelse return null, 467 }; 468 return null; 469 } 470 471 fn listRoute(identity: []const u8, status: Status, tag: []const u8, selected: []const u8) ?ui.RoutablePage { 472 return Data.listRoute(identity, status, tag, selected); 473 } 474 475 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 476 return if (supports_drafts) Data.draftsRoute(identity) else null; 477 } 478 479 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 480 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 481 } 482 483 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 484 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 485 } 486 487 fn forkRoute(identity: []const u8, id: []const u8) ?ui.RoutablePage { 488 return if (supports_forks) Data.forkRoute(identity, id) else null; 489 } 490 491 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 492 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 493 } 494 495 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8, id: []const u8) ![]const u8 { 496 const route = listRoute(identity, status, tag, id) orelse return error.RouteTooLong; 497 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 498 } 499 500 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8) ![]const u8 { 501 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 502 return listLink(page_arena, identity, status, encoded, ""); 503 } 504 505 fn addToolButton(allocator: std.mem.Allocator, row: *wgt.Box(Widget), label: []const u8, action: []const u8) !void { 506 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 507 errdefer button.deinit(allocator); 508 button.getFocus().mode = .all; 509 button.getFocus().kind = .{ .custom = action }; 510 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = try xitui.width.displayWidth(label) + 2, .height = null } }); 511 } 512 513 pub fn setEntry(self: *This, allocator: std.mem.Allocator, entry: Entry) !void { 514 const inner_box = self.inner(); 515 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 516 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 517 518 for (inner_box.children.values()) |*child| child.widget.deinit(allocator); 519 inner_box.children.clearAndFree(allocator); 520 inner_box.getFocus().child_id = null; 521 self.entry = entry; 522 self.title_id = null; 523 self.author_id = null; 524 525 { 526 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 527 errdefer row.deinit(allocator); 528 try inner_box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 529 } 530 if (self.actions and !description_page and comment_page == null) { 531 const row = self.toolRow(); 532 const pa = self.session.page_arena.allocator(); 533 534 if (entryHasFork(entry) and self.session.data.current_page.parent() != .fork) { 535 const route = forkRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 536 try addToolButton(allocator, row, "view fork", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 537 const diff_route = ui.RoutablePage.forkDiffRoute(self.data.identity, entry.id, 0, "") orelse return error.RouteTooLong; 538 try addToolButton(allocator, row, "view diff", try std.fmt.allocPrint(pa, "a:{s}", .{try diff_route.toUrl(self.session.page_arena)})); 539 } 540 541 { 542 var spacer = try Spacer.init(allocator); 543 errdefer spacer.deinit(allocator); 544 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 545 } 546 547 if (comptime supports_merge) { 548 if (Data.canMerge(entry, self.session)) { 549 const route = mergeRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 550 try addToolButton(allocator, row, "merge", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 551 } 552 } 553 554 if (supports_drafts and entryDraft(entry)) { 555 const route = publishRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 556 try addToolButton(allocator, row, "publish", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 557 if (self.session.data.user_id != null) { 558 const edit_route = ui.RoutablePage.repoThreadEditRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 559 try addToolButton(allocator, row, "edit", try std.fmt.allocPrint(pa, "a:{s}", .{try edit_route.toUrl(self.session.page_arena)})); 560 const remove_route = ui.RoutablePage.repoThreadRemoveRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong; 561 try addToolButton(allocator, row, "✕", try std.fmt.allocPrint(pa, "a:{s}", .{try remove_route.toUrl(self.session.page_arena)})); 562 } 563 } else { 564 if (!self.session.is_terminal and !entryConflicted(entry) and (self.session.data.is_local or self.session.data.user_id != null)) { 565 const label = "add attachment"; 566 const action = if (self.data.identity.len == 0) 567 try std.fmt.allocPrint(pa, "{s}/{s}:{s}/attach", .{ ui.file_input_prefix, @tagName(kind), entry.id }) 568 else 569 try std.fmt.allocPrint(pa, "{s}/repo/{s}/{s}:{s}/attach", .{ ui.file_input_prefix, self.data.identity, @tagName(kind), entry.id }); 570 try addToolButton(allocator, row, label, action); 571 } 572 573 if (supports_conflicts and entryConflicted(entry)) { 574 const route = resolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 575 try addToolButton(allocator, row, "resolve conflict", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 576 } else { 577 if (has_status and (self.session.data.is_local or self.session.data.user_id != null)) { 578 if (statusChange(entryStatus(entry))) |change| { 579 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 580 row.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "form:{s}/{s}", .{ try route.toUrl(self.session.page_arena), change.action }) }; 581 try addToolButton(allocator, row, change.action, "submit"); 582 } 583 } 584 585 const route = ui.RoutablePage.repoThreadEditRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 586 try addToolButton(allocator, row, "edit", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 587 } 588 589 if (self.session.data.is_local or self.session.data.user_id != null) { 590 const route = ui.RoutablePage.repoThreadRemoveRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong; 591 try addToolButton(allocator, row, "✕", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 592 } 593 } 594 595 for (row.children.keys(), row.children.values()) |id, *child| { 596 if (child.widget.getFocus().mode != .all) continue; 597 row.getFocus().child_id = id; 598 break; 599 } 600 } 601 602 if (supports_drafts and !description_page and comment_page == null) 603 try self.data.appendDetails(allocator, inner_box, self.session, entry); 604 605 if (comment_page) |comment_view| { 606 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 607 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 608 var back = try Comment.linkBox(allocator, self.session, back_label, ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 609 errdefer back.deinit(allocator); 610 try inner_box.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 611 612 try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, comment_view.selected); 613 try Comment.appendCount(allocator, inner_box, comment_view.replies.count, "reply", "replies"); 614 for (comment_view.replies.comments) |comment| try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, comment); 615 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); 616 617 var spacer = try Spacer.init(allocator); 618 errdefer spacer.deinit(allocator); 619 try inner_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 620 621 inner_box.getFocus().child_id = inner_box.children.keys()[tool_row_index + 1]; 622 self.resetScroll(); 623 return; 624 } 625 626 if (description_page) { 627 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 628 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 629 var text_box = try wgt.TextBox.init(allocator, back_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 630 errdefer text_box.deinit(allocator); 631 text_box.getFocus().mode = .all; 632 text_box.getFocus().kind = .{ .custom = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), "", entry.id) }; 633 try inner_box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 634 self.title_id = text_box.getFocus().id; 635 } else { 636 var title = try wgt.TextBox.init(allocator, entry.record.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 637 errdefer title.deinit(allocator); 638 title.getFocus().mode = .all; 639 try inner_box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_box = title }, .rect = null, .min_size = null }); 640 self.title_id = title.getFocus().id; 641 642 var author = try ui.authorBox(allocator, self.session.page_arena, entry.author); 643 errdefer author.deinit(allocator); 644 try inner_box.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 645 self.author_id = author.getFocus().id; 646 647 var items: std.ArrayList(TagFlow.Item) = .empty; 648 defer items.deinit(allocator); 649 var tag_iter = Event.tagIterator(entry.record.event.tags); 650 while (tag_iter.next()) |tag| { 651 if (tag.len == 0) continue; 652 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entryStatus(entry), tag) }); 653 } 654 if (items.items.len > 0) { 655 var tags = try TagFlow.init(allocator); 656 errdefer tags.deinit(allocator); 657 try tags.setItems(allocator, items.items); 658 try inner_box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .tag_flow = tags }, .rect = null, .min_size = null }); 659 } 660 } 661 662 const whole = entry.record.event.description; 663 const preview_end = if (description_page) null else ui.detailPreviewEnd(whole); 664 const shown = if (preview_end) |end| 665 std.mem.trimEnd(u8, whole[0..end], " \t\r\n") 666 else if (whole.len == 0) 667 "(no description)" 668 else 669 whole; 670 var description = try wgt.TextBox.init(allocator, shown, .{ 671 .border_style = .single, 672 .rounded_corners = true, 673 .wrap_kind = .word, 674 .label = " description ", 675 .bottom_label = if (preview_end != null) " click or press enter to see more " else "", 676 }); 677 errdefer description.deinit(allocator); 678 description.getFocus().mode = .all; 679 if (preview_end != null) { 680 const route = ui.RoutablePage.repoThreadDescriptionRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 681 description.getFocus().kind = .{ .custom = try std.fmt.allocPrint(self.session.page_arena.allocator(), "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 682 } 683 try inner_box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_box = description }, .rect = null, .min_size = null }); 684 self.description_id = description.getFocus().id; 685 686 if (!supports_drafts or !entryDraft(entry)) { 687 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 688 try Attachment.appendRows(allocator, inner_box, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 689 690 if (!description_page) { 691 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoThreadCommentNewRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong); 692 errdefer reply.deinit(allocator); 693 try inner_box.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 694 695 try Comment.appendCount(allocator, inner_box, entry.comments.count, "comment", "comments"); 696 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner_box, self.session, self.data.identity, kind, comment); 697 try Comment.appendWindowNav(allocator, inner_box, self.session, self.data.identity, kind, entry.id, null, entry.comments); 698 } 699 } 700 701 if (!description_page) { 702 var spacer = try Spacer.init(allocator); 703 errdefer spacer.deinit(allocator); 704 try inner_box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 705 } 706 707 _ = self.focusFirst(null); 708 self.resetScroll(); 709 } 710 711 fn resetScroll(self: *This) void { 712 const detail_scroll = &self.scroll; 713 detail_scroll.x = 0; 714 detail_scroll.y = 0; 715 detail_scroll.getFocus().version +%= 1; 716 } 717 718 pub fn inputWithExit(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !Exit { 719 self.exit = .none; 720 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 721 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 722 else 723 raw_key; 724 725 switch (key) { 726 .page_up => self.pageDetail(root_focus, -10), 727 .page_down => self.pageDetail(root_focus, 10), 728 .home => self.jump(root_focus, false), 729 .end => self.jump(root_focus, true), 730 else => { 731 const entry = self.entry orelse return self.exit; 732 if (self.data.comment_page != null and std.mem.eql(u8, entry.id, self.data.selected_id)) { 733 self.commentInput(key, root_focus); 734 } else if (self.toolRowFocused(root_focus)) { 735 try self.toolRowInput(allocator, key, root_focus); 736 } else { 737 try self.childInput(allocator, key, root_focus); 738 } 739 }, 740 } 741 return self.exit; 742 } 743 744 fn childInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 745 const child_index = self.focusedChild(root_focus) orelse return; 746 const inner_box = self.inner(); 747 const child_id = inner_box.children.keys()[child_index]; 748 const child = &inner_box.children.values()[child_index].widget; 749 750 if (child_id == self.title_id) { 751 switch (key) { 752 .arrow_left => self.exit = .list, 753 .arrow_up => if (!self.moveVertical(root_focus, false)) self.focusToolRow(root_focus), 754 .arrow_down => _ = self.moveVertical(root_focus, true), 755 else => {}, 756 } 757 return; 758 } 759 if (child_id == self.author_id) { 760 switch (key) { 761 .arrow_left => self.exit = .list, 762 .arrow_up => _ = self.moveVertical(root_focus, false), 763 .arrow_down => _ = self.moveVertical(root_focus, true), 764 else => {}, 765 } 766 return; 767 } 768 if (child_id == self.description_id) { 769 self.descriptionInput(key, root_focus); 770 return; 771 } 772 773 switch (child.*) { 774 .tag_flow => |*tags| self.tagsInput(child_index, tags, key, root_focus), 775 .copyable_text => |*copyable| switch (key) { 776 .arrow_up => if (!self.moveVertical(root_focus, false)) self.focusToolRow(root_focus), 777 .arrow_down => _ = self.moveVertical(root_focus, true), 778 .arrow_left => if (copyable.selected == 0) { 779 self.exit = .list; 780 } else try copyable.input(allocator, key, root_focus), 781 else => try copyable.input(allocator, key, root_focus), 782 }, 783 .box => |*row| if (Attachment.removeId(row)) |attachment_id| { 784 const remove_id = row.children.keys()[1]; 785 switch (key) { 786 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 787 self.exit = .list; 788 }, 789 .arrow_right => _ = self.moveHorizontal(root_focus, true), 790 .arrow_up => _ = self.moveVertical(root_focus, false), 791 .arrow_down => _ = self.moveVertical(root_focus, true), 792 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 793 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 794 else => {}, 795 } 796 } else self.commentInput(key, root_focus), 797 else => self.commentInput(key, root_focus), 798 } 799 } 800 801 fn toolRowInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 802 const row = self.toolRow(); 803 const child_id = row.getFocus().child_id orelse return; 804 const cur = row.children.getIndex(child_id) orelse return; 805 const on_primary = switch (row.children.values()[cur].widget.getFocus().kind) { 806 .custom => |action| std.mem.eql(u8, action, "submit"), 807 else => false, 808 }; 809 switch (key) { 810 .arrow_left => { 811 var previous = cur; 812 while (previous > 0) { 813 previous -= 1; 814 if (row.children.values()[previous].widget.getFocus().mode != .all) continue; 815 root_focus.setFocus(row.children.keys()[previous]); 816 return; 817 } 818 self.exit = .list; 819 }, 820 .arrow_right => { 821 var next = cur + 1; 822 while (next < row.children.count()) : (next += 1) { 823 if (row.children.values()[next].widget.getFocus().mode != .all) continue; 824 root_focus.setFocus(row.children.keys()[next]); 825 return; 826 } 827 }, 828 .arrow_up => self.exit = .header, 829 .arrow_down => _ = self.moveVertical(root_focus, true), 830 .enter => if (on_primary) try self.primaryAction(allocator), 831 .mouse => |mouse| if (on_primary and inp.leftClickOn(root_focus, child_id, mouse)) try self.primaryAction(allocator), 832 else => {}, 833 } 834 } 835 836 fn primaryAction(self: *This, allocator: std.mem.Allocator) !void { 837 try self.toggleStatus(allocator); 838 } 839 840 fn descriptionInput(self: *This, key: Key, root_focus: *Focus) void { 841 const detail_scroll = &self.scroll; 842 switch (key) { 843 .arrow_left => self.exit = .list, 844 .arrow_up => { 845 if (!self.session.is_terminal) { 846 _ = self.moveVertical(root_focus, false); 847 return; 848 } 849 if (self.focusAdjacentVisible(root_focus, false)) return; 850 const before = detail_scroll.y; 851 detail_scroll.y -= 1; 852 detail_scroll.clampToContent(); 853 if (detail_scroll.y == before) _ = self.moveVertical(root_focus, false); 854 }, 855 .arrow_down => { 856 if (!self.session.is_terminal) { 857 _ = self.moveVertical(root_focus, true); 858 return; 859 } 860 if (self.focusAdjacentVisible(root_focus, true)) return; 861 const before = detail_scroll.y; 862 detail_scroll.y += 1; 863 detail_scroll.clampToContent(); 864 if (detail_scroll.y == before) _ = self.moveVertical(root_focus, true); 865 }, 866 else => {}, 867 } 868 } 869 870 fn commentInput(self: *This, key: Key, root_focus: *Focus) void { 871 switch (key) { 872 .arrow_left => if (!self.moveHorizontal(root_focus, false)) { 873 self.exit = .list; 874 }, 875 .arrow_right => _ = self.moveHorizontal(root_focus, true), 876 .arrow_up => _ = self.moveVertical(root_focus, false), 877 .arrow_down => _ = self.moveVertical(root_focus, true), 878 else => {}, 879 } 880 } 881 882 fn pageDetail(self: *This, root_focus: *Focus, delta: isize) void { 883 if (!self.session.is_terminal) return; 884 const detail_scroll = &self.scroll; 885 detail_scroll.y += delta; 886 detail_scroll.clampToContent(); 887 self.focusVisible(root_focus, delta > 0); 888 } 889 890 fn jump(self: *This, root_focus: *Focus, to_end: bool) void { 891 if (self.session.is_terminal) { 892 const detail_scroll = &self.scroll; 893 detail_scroll.y = if (to_end) std.math.maxInt(isize) else 0; 894 detail_scroll.clampToContent(); 895 self.focusVisible(root_focus, to_end); 896 } else { 897 const focus = self.inner().getFocus(); 898 var chosen: ?usize = null; 899 for (focus.children.keys(), focus.children.values()) |id, child| { 900 if (child.focus.mode != .all) continue; 901 chosen = id; 902 if (!to_end) break; 903 } 904 if (chosen) |id| root_focus.setFocus(id); 905 } 906 } 907 908 fn rectVisible(self: *This, rect: layout.IRect) bool { 909 const detail_scroll = &self.scroll; 910 const viewport = detail_scroll.grid orelse return false; 911 const top = detail_scroll.y; 912 const bottom = top + @as(isize, @intCast(viewport.size.height - detail_scroll.bar_h)); 913 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 914 } 915 916 fn focusVisible(self: *This, root_focus: *Focus, last: bool) void { 917 const inner_box = self.inner(); 918 var offset: usize = 0; 919 while (offset < inner_box.children.count()) : (offset += 1) { 920 const child_index = if (last) inner_box.children.count() - 1 - offset else offset; 921 if (self.focusVisibleChild(child_index, root_focus, last)) return; 922 } 923 } 924 925 fn focusVisibleChild(self: *This, child_index: usize, root_focus: *Focus, last: bool) bool { 926 const inner_box = self.inner(); 927 const child = &inner_box.children.values()[child_index]; 928 const child_rect = child.rect orelse return false; 929 var chosen: ?usize = null; 930 if (child.widget.getFocus().mode == .all and self.rectVisible(child_rect)) { 931 const id = inner_box.children.keys()[child_index]; 932 chosen = id; 933 if (!last) { 934 root_focus.setFocus(id); 935 return true; 936 } 937 } 938 939 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 940 if (focus_child.focus.mode != .all) continue; 941 const rect: layout.IRect = .{ 942 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 943 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 944 .size = focus_child.rect.size, 945 }; 946 if (!self.rectVisible(rect)) continue; 947 chosen = id; 948 if (!last) break; 949 } 950 if (chosen) |id| { 951 root_focus.setFocus(id); 952 return true; 953 } 954 return false; 955 } 956 957 fn focusAdjacentVisible(self: *This, root_focus: *Focus, down: bool) bool { 958 const inner_box = self.inner(); 959 var next = self.focusedChild(root_focus) orelse return false; 960 while (true) { 961 if (down) { 962 next += 1; 963 if (next >= inner_box.children.count()) return false; 964 } else { 965 if (next == 0) return false; 966 next -= 1; 967 } 968 if (self.focusVisibleChild(next, root_focus, !down)) { 969 self.focusChild(next, !down, root_focus); 970 return true; 971 } 972 } 973 } 974 975 fn focusedChild(self: *This, root_focus: *Focus) ?usize { 976 const inner_box = self.inner(); 977 var id = root_focus.grandchild_id orelse return null; 978 while (inner_box.getFocus().children.get(id)) |child| { 979 if (child.parent_id == inner_box.getFocus().id) return inner_box.children.getIndex(id); 980 id = child.parent_id; 981 } 982 return null; 983 } 984 985 fn childFocusable(self: *This, child_index: usize) bool { 986 const focus = self.inner().children.values()[child_index].widget.getFocus(); 987 if (focus.mode == .all) return true; 988 for (focus.children.values()) |item| { 989 if (item.focus.mode == .all) return true; 990 } 991 return false; 992 } 993 994 fn focusChild(self: *This, child_index: usize, last: bool, root_focus: *Focus) void { 995 const inner_box = self.inner(); 996 const child = &inner_box.children.values()[child_index]; 997 var target = inner_box.children.keys()[child_index]; 998 var rect = child.rect orelse return; 999 switch (child.widget) { 1000 .repo_comment => |*comment| { 1001 if (comment.rowRect(last)) |row_rect| { 1002 rect.x += row_rect.x; 1003 rect.y += row_rect.y; 1004 rect.size = row_rect.size; 1005 } 1006 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1007 }, 1008 .box => |*box| if (box.children.count() > 0) { 1009 target = if (child_index == tool_row_index) 1010 box.getFocus().child_id orelse return 1011 else 1012 box.children.keys()[if (last) box.children.count() - 1 else 0]; 1013 root_focus.setFocus(target); 1014 }, 1015 else => root_focus.setFocus(target), 1016 } 1017 const detail_scroll = &self.scroll; 1018 if (detail_scroll.grid) |viewport| { 1019 const viewport_height = viewport.size.height - detail_scroll.bar_h; 1020 if (rect.size.height > viewport_height) { 1021 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1022 detail_scroll.y = std.math.clamp(detail_scroll.y, rect.y, bottom_aligned); 1023 detail_scroll.clampToContent(); 1024 return; 1025 } 1026 } 1027 detail_scroll.scrollToRect(rect); 1028 } 1029 1030 fn moveVertical(self: *This, root_focus: *Focus, down: bool) bool { 1031 const inner_box = self.inner(); 1032 const current = self.focusedChild(root_focus) orelse return false; 1033 if (inner_box.children.values()[current].widget == .repo_comment) { 1034 const comment = &inner_box.children.values()[current].widget.repo_comment; 1035 if (down and !comment.bodyFocused()) { 1036 self.focusChild(current, true, root_focus); 1037 return true; 1038 } 1039 if (!down and comment.bodyFocused()) { 1040 self.focusChild(current, false, root_focus); 1041 return true; 1042 } 1043 } 1044 1045 var next = current; 1046 while (true) { 1047 if (down) { 1048 next += 1; 1049 if (next >= inner_box.children.count()) return false; 1050 } else { 1051 if (next == 0) return false; 1052 next -= 1; 1053 } 1054 if (!self.childFocusable(next)) continue; 1055 self.focusChild(next, !down, root_focus); 1056 return true; 1057 } 1058 } 1059 1060 fn moveHorizontal(self: *This, root_focus: *Focus, right: bool) bool { 1061 const inner_box = self.inner(); 1062 const current = self.focusedChild(root_focus) orelse return false; 1063 const child = &inner_box.children.values()[current]; 1064 if (child.widget == .repo_comment) { 1065 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1066 if (moved) if (child.rect) |rect| self.scroll.scrollToRect(rect); 1067 return moved; 1068 } 1069 const row = switch (child.widget) { 1070 .box => |*box| box, 1071 else => return false, 1072 }; 1073 const selected = row.getFocus().child_id orelse return false; 1074 const selected_index = row.children.getIndex(selected) orelse return false; 1075 const target = if (right) selected_index + 1 else selected_index -| 1; 1076 if (target == selected_index or target >= row.children.count()) return false; 1077 root_focus.setFocus(row.children.keys()[target]); 1078 if (child.rect) |rect| self.scroll.scrollToRect(rect); 1079 return true; 1080 } 1081 1082 fn tagsInput(self: *This, child_index: usize, tags: *TagFlow, key: Key, root_focus: *Focus) void { 1083 const child_id = tags.focus.child_id orelse return; 1084 const cur = tags.indexOfFocusId(child_id) orelse return; 1085 const count = tags.text_boxes.items.len; 1086 switch (key) { 1087 .arrow_left => if (cur > 0) self.focusTag(child_index, tags, root_focus, cur - 1) else { 1088 self.exit = .list; 1089 }, 1090 .arrow_right => if (cur + 1 < count) self.focusTag(child_index, tags, root_focus, cur + 1), 1091 .arrow_up => if (tags.rowStep(cur, false)) |index| { 1092 self.focusTag(child_index, tags, root_focus, index); 1093 } else { 1094 _ = self.moveVertical(root_focus, false); 1095 }, 1096 .arrow_down => if (tags.rowStep(cur, true)) |index| { 1097 self.focusTag(child_index, tags, root_focus, index); 1098 } else { 1099 _ = self.moveVertical(root_focus, true); 1100 }, 1101 .home => self.focusTag(child_index, tags, root_focus, 0), 1102 .end => self.focusTag(child_index, tags, root_focus, count - 1), 1103 else => {}, 1104 } 1105 } 1106 1107 fn focusTag(self: *This, child_index: usize, tags: *TagFlow, root_focus: *Focus, item: usize) void { 1108 root_focus.setFocus(tags.text_boxes.items[item].getFocus().id); 1109 if (self.session.is_terminal and item < tags.rects.items.len) { 1110 if (self.inner().children.values()[child_index].rect) |flow_rect| { 1111 var rect = tags.rects.items[item]; 1112 rect.x += flow_rect.x; 1113 rect.y += flow_rect.y; 1114 self.scroll.scrollToRect(rect); 1115 } 1116 } 1117 } 1118 1119 fn toolRowFocused(self: *This, root_focus: *Focus) bool { 1120 return self.focusedChild(root_focus) == tool_row_index; 1121 } 1122 1123 fn focusToolRow(self: *This, root_focus: *Focus) void { 1124 const child_id = self.toolRow().getFocus().child_id orelse { 1125 self.exit = .header; 1126 return; 1127 }; 1128 root_focus.setFocus(child_id); 1129 if (self.inner().children.values()[tool_row_index].rect) |rect| self.scroll.scrollToRect(rect); 1130 } 1131 1132 pub fn focusFirst(self: *This, root_focus: ?*Focus) bool { 1133 const inner_box = self.inner(); 1134 for (tool_row_index + 1..inner_box.children.count()) |child_index| { 1135 if (!self.childFocusable(child_index)) continue; 1136 if (root_focus) |focus| 1137 self.focusChild(child_index, false, focus) 1138 else 1139 inner_box.getFocus().child_id = inner_box.children.keys()[child_index]; 1140 return true; 1141 } 1142 return false; 1143 } 1144 1145 pub fn atTop(self: *This, root_focus: *Focus) bool { 1146 const focused = self.focusedChild(root_focus) orelse return false; 1147 for (0..self.inner().children.count()) |child_index| { 1148 if (!self.childFocusable(child_index)) continue; 1149 return focused == child_index; 1150 } 1151 return false; 1152 } 1153 1154 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 1155 if (comptime wasm) return; 1156 1157 if (comptime supports_drafts) { 1158 const entry = self.entry orelse return; 1159 if (event_kind == kind and entryDraft(entry)) { 1160 try Data.removeDraft(self.session, allocator, &id); 1161 const route = draftsRoute(self.data.identity) orelse return; 1162 try self.session.navigate(route); 1163 return; 1164 } 1165 } 1166 1167 const io = self.session.io orelse return; 1168 const source = self.data.repo_source orelse return; 1169 const author = (try self.session.eventAuthor()) orelse return; 1170 1171 switch (source.repo_kind) { 1172 inline else => |repo_kind| { 1173 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1174 defer any_repo.deinit(io, allocator); 1175 switch (any_repo) { 1176 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 1177 } 1178 }, 1179 } 1180 1181 const id_hex = std.fmt.bytesToHex(id, .lower); 1182 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 1183 try self.session.navigate(route); 1184 } 1185 1186 fn toggleStatus(self: *This, allocator: std.mem.Allocator) !void { 1187 if (comptime (!has_status or wasm)) return; 1188 const entry = self.entry orelse return; 1189 const io = self.session.io orelse return; 1190 const source = self.data.repo_source orelse return; 1191 const author = (try self.session.eventAuthor()) orelse return; 1192 const status = (statusChange(entryStatus(entry)) orelse return).status; 1193 const id = try evt.parseEventId(entry.id); 1194 1195 switch (source.repo_kind) { 1196 inline else => |repo_kind| { 1197 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, source.localInitOpts()); 1198 defer any_repo.deinit(io, allocator); 1199 switch (any_repo) { 1200 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id, .{ .status = status }, author), 1201 } 1202 }, 1203 } 1204 1205 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 1206 try self.session.navigate(route); 1207 } 1208 }; 1209 } 1210 1211 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 1212 const Self = Data; 1213 const HeaderType = Data.Header; 1214 const Window = Data.Window; 1215 const Entry = Data.Entry; 1216 const Event = Data.Event; 1217 const feedback_tag: std.meta.Tag(ui.Session.FormFeedback) = switch (kind) { 1218 .issue => .issue, 1219 .patch => .patch, 1220 .discuss => .discussion, 1221 else => unreachable, 1222 }; 1223 const FeedbackFailure = if (kind == .patch) ui.Session.FormFeedback.PatchFailure else ui.Session.FormFeedback.ThreadFailure; 1224 const Status = Data.Status; 1225 const has_status = @hasField(Event, "status"); 1226 const supports_conflicts = Event.merge_policy == .field_conflicts; 1227 const supports_drafts = @hasField(Self, "drafts"); 1228 const supports_merge = @hasDecl(Data, "mergeRoute"); 1229 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 1230 const ViewKind = Data.ViewKind; 1231 const thread_name = Data.thread_name; 1232 const DetailType = Data.Detail; 1233 1234 return struct { 1235 const This = @This(); 1236 // a vertical box: the header tabs on top, then a stack holding a 1237 // master-detail split (thread list + description pane) per status list, 1238 // plus the tags view. 1239 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 1240 data: *const Self, 1241 session: *ui.Session, 1242 1243 const header_index: usize = 0; 1244 const stack_index: usize = 1; 1245 // indices within the stack, 1:1 with the header tabs. 1246 const status_count = @typeInfo(Status).@"enum".fields.len; 1247 const tags_view_index: usize = status_count; 1248 // the new-thread form, or the edit, comment, or resolve form when the page was 1249 // loaded at one of their urls. 1250 const form_view_index: usize = tags_view_index + 1; 1251 // the conflicts split; the tab and stack child exist only when the repo 1252 // has conflicted threads. 1253 const drafts_view_index: usize = form_view_index + 1; 1254 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 1255 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 1256 // indices within a split (the horizontal box inside the stack). 1257 const list_index: usize = 0; 1258 const detail_index: usize = 1; 1259 const list_max_width: usize = 35; 1260 const detail_min_width: usize = 40; 1261 // indices within the thread form. 1262 const title_field_index: usize = 0; 1263 const tags_field_index: usize = 1; 1264 const description_field_index: usize = 2; 1265 const target_branch_field_index: usize = 3; 1266 const submit_field_index: usize = 3 + @as(usize, @intFromBool(supports_drafts)); 1267 const comment_author_field_index: usize = 0; 1268 const comment_body_field_index: usize = 1; 1269 const comment_submit_field_index: usize = 2; 1270 1271 pub fn viewIndex(view: ViewKind) usize { 1272 const name = @tagName(view); 1273 if (std.mem.eql(u8, name, "tags")) return tags_view_index; 1274 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; 1275 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 1276 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 1277 if (std.mem.eql(u8, name, "description")) unreachable; 1278 inline for (@typeInfo(Status).@"enum".fields, 0..) |field, index| { 1279 if (std.mem.eql(u8, name, field.name)) return index; 1280 } 1281 unreachable; 1282 } 1283 1284 const Confirmation = enum { remove, publish, merge }; 1285 1286 fn confirmation(view: ViewKind) ?Confirmation { 1287 if (view == .remove) return .remove; 1288 if (supports_drafts and std.mem.eql(u8, @tagName(view), "publish")) return .publish; 1289 if (supports_merge and std.mem.eql(u8, @tagName(view), "merge")) return .merge; 1290 return null; 1291 } 1292 1293 // the status a status split lists; the conflicts split has none (its 1294 // threads carry their own). 1295 fn splitStatus(index: usize) Status { 1296 return @enumFromInt(index); 1297 } 1298 1299 fn entryConflicted(entry: Entry) bool { 1300 return if (supports_conflicts) entry.conflicted else false; 1301 } 1302 1303 fn entryStatus(entry: Entry) Status { 1304 if (comptime has_status) return Data.statusKind(entry.record.event); 1305 return @enumFromInt(0); 1306 } 1307 1308 fn entryDraft(entry: Entry) bool { 1309 return if (supports_drafts) entry.draft else false; 1310 } 1311 1312 fn listRoute(identity: []const u8, status: Status, tag: []const u8, selected: []const u8) ?ui.RoutablePage { 1313 return Data.listRoute(identity, status, tag, selected); 1314 } 1315 1316 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 1317 return if (supports_drafts) Data.draftsRoute(identity) else null; 1318 } 1319 1320 fn publishRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1321 return if (supports_drafts) Data.publishRoute(identity, selected) else null; 1322 } 1323 1324 fn mergeRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1325 return if (supports_merge) Data.mergeRoute(identity, selected) else null; 1326 } 1327 1328 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 1329 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 1330 } 1331 1332 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 1333 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 1334 } 1335 1336 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8, id: []const u8) ![]const u8 { 1337 const route = listRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1338 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1339 } 1340 1341 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 1342 const selected = std.mem.eql(u8, id, data.selected_id); 1343 const route = (if (selected and data.description_page) 1344 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 1345 else if (selected) 1346 if (data.comment_page) |page| 1347 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 1348 else 1349 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 1350 else 1351 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 1352 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 1353 } 1354 1355 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 1356 if (supports_conflicts and status_maybe == null and !drafts) { 1357 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 1358 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1359 } 1360 if (drafts and id.len == 0) { 1361 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 1362 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1363 } 1364 return listLink(page_arena, data.identity, status_maybe orelse @enumFromInt(0), data.tag, id); 1365 } 1366 1367 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8) ![]const u8 { 1368 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1369 return listLink(page_arena, identity, status, encoded, ""); 1370 } 1371 1372 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 1373 var outer = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1374 errdefer outer.deinit(allocator); 1375 1376 // the tabs at the top. 1377 { 1378 var hdr = try Data.initHeader(allocator, session, data); 1379 errdefer hdr.deinit(allocator); 1380 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 1381 } 1382 1383 // the stack enters `outer` before its children enter it, so an error 1384 // frees each child exactly once. 1385 { 1386 var stack = try wgt.Stack(Widget).init(allocator); 1387 errdefer stack.deinit(allocator); 1388 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 1389 } 1390 const stack = &outer.children.values()[stack_index].widget.stack; 1391 1392 inline for (@typeInfo(Status).@"enum".fields) |field| { 1393 var split = try initSplit(allocator, session, data, @enumFromInt(field.value), false); 1394 errdefer split.deinit(allocator); 1395 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1396 } 1397 1398 // the tags view 1399 { 1400 var tf = try TagFlow.init(allocator); 1401 errdefer tf.deinit(allocator); 1402 var items: std.ArrayList(TagFlow.Item) = .empty; 1403 defer items.deinit(allocator); 1404 // when filtered, the first item clears the filter 1405 if (data.tag.len != 0) 1406 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @enumFromInt(0), "", "") }); 1407 for (data.tags) |tag| 1408 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, @enumFromInt(0), tag) }); 1409 try tf.setItems(allocator, items.items); 1410 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 1411 } 1412 1413 // the new-thread form, or — on an edit, comment, or resolve url — that form in 1414 // its place, prefilled with the selected thread's content. a 1415 // logged-out session can't create events, so the unauthorized view 1416 // stands in. 1417 if (session.data.is_local or session.data.user_id != null) { 1418 if (confirmation(data.view)) |confirmation_kind| { 1419 const aa = session.page_arena.allocator(); 1420 const route = switch (confirmation_kind) { 1421 .remove => ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id), 1422 .publish => publishRoute(data.identity, data.selected_id), 1423 .merge => mergeRoute(data.identity, data.selected_id), 1424 } orelse return error.RouteTooLong; 1425 const page_url = try route.toUrl(session.page_arena); 1426 const action = try std.fmt.allocPrint(aa, "form:{s}", .{page_url}); 1427 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 1428 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 1429 var buttons: [2]ConfirmationButton = undefined; 1430 buttons[0] = .{ .label = switch (confirmation_kind) { 1431 .remove => try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}), 1432 .publish => "publish patch", 1433 .merge => "merge patch", 1434 } }; 1435 var button_count: usize = 1; 1436 if (confirmation_kind == .merge) { 1437 if (comptime supports_merge) { 1438 buttons[1] = .{ 1439 .label = "squash and merge patch", 1440 .action = try std.fmt.allocPrint(aa, "{s}{s}/squash", .{ ui.submit_action_prefix, page_url }), 1441 }; 1442 button_count += 1; 1443 } else unreachable; 1444 } 1445 var center = try initConfirmationForm(allocator, action, buttons[0..button_count]); 1446 errdefer center.deinit(allocator); 1447 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 1448 } else if (data.view == .new_comment or data.view == .edit_comment) { 1449 const editing = data.view == .edit_comment; 1450 const route = if (editing) 1451 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 1452 else 1453 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 1454 const page_url = try route.toUrl(session.page_arena); 1455 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 1456 const page = data.comment_page; 1457 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 1458 const parent_route = if (editing) 1459 if (top_level) 1460 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 1461 else if (page) |p| 1462 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 1463 else 1464 null 1465 else if (page) |p| 1466 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 1467 else 1468 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 1469 const parent_author: ui.Author = if (editing) 1470 if (top_level) 1471 if (data.selectedThread()) |entry| entry.author else .unknown 1472 else if (page) |p| 1473 p.selected.parent_author orelse .unknown 1474 else 1475 .unknown 1476 else if (page) |p| 1477 p.selected.author 1478 else if (data.selectedThread()) |entry| 1479 entry.author 1480 else 1481 .unknown; 1482 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 1483 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 1484 errdefer form.deinit(allocator); 1485 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1486 } else if (supports_conflicts and data.view == .resolve) { 1487 // on the web the page grows to the form's height and the 1488 // browser scrolls it; the terminal scrolls the form itself 1489 var form_widget: Widget = blk: { 1490 var form = try initResolveForm(allocator, session, data); 1491 errdefer form.deinit(allocator); 1492 break :blk if (session.is_terminal) 1493 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 1494 else 1495 .{ .box = form }; 1496 }; 1497 errdefer form_widget.deinit(allocator); 1498 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 1499 } else { 1500 const aa = session.page_arena.allocator(); 1501 const route = if (data.view == .edit) 1502 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 1503 else 1504 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 1505 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 1506 const record = if (data.view == .edit) 1507 (if (data.selectedThread()) |entry| &entry.record else null) 1508 else 1509 null; 1510 var form = try initThreadForm(allocator, session, data, action, record); 1511 errdefer form.deinit(allocator); 1512 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 1513 } 1514 } else { 1515 var message = try ui.Unauthorized.View.init(allocator); 1516 errdefer message.deinit(allocator); 1517 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 1518 } 1519 1520 if (supports_drafts) { 1521 var split = try initSplit(allocator, session, data, null, true); 1522 errdefer split.deinit(allocator); 1523 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1524 } 1525 1526 // the conflicts split; its tab exists only when the repo has 1527 // conflicts, so skip the child too to keep the stack 1:1 with the 1528 // tabs. 1529 if (supports_conflicts and data.conflicts.count > 0) { 1530 var split = try initSplit(allocator, session, data, null, false); 1531 errdefer split.deinit(allocator); 1532 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 1533 } 1534 1535 // the stack starts on the page's view. 1536 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 1537 1538 // focus entering the view lands on the tabs first. 1539 outer.getFocus().child_id = outer.children.keys()[header_index]; 1540 1541 return .{ 1542 .box = outer, 1543 .data = data, 1544 .session = session, 1545 }; 1546 } 1547 1548 // the master-detail split showing `status`'s window, or the conflicts 1549 // window when null. 1550 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 1551 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1552 errdefer box.deinit(allocator); 1553 1554 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; 1555 1556 // the thread list (one focusable row per title), plus a "next" link that 1557 // reloads the page rooted at the following thread. 1558 { 1559 var list_scroll = blk: { 1560 var list_box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 1561 errdefer list_box.deinit(allocator); 1562 if (win.prev_id) |prev| 1563 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 1564 for (win.items) |entry| { 1565 const label: []const u8 = if (entry.comments.count > 0) 1566 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 1567 else 1568 ""; 1569 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 1570 } 1571 if (win.next_id) |next| 1572 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 1573 // select the window's first thread (past a leading "previous" 1574 // row) so its description shows on load. 1575 if (win.items.len > 0) 1576 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 1577 else if (list_box.children.count() > 0) 1578 list_box.getFocus().child_id = list_box.children.keys()[0]; 1579 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 1580 }; 1581 errdefer list_scroll.deinit(allocator); 1582 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 } }); 1583 } 1584 1585 // frame the detail pane inside the master-detail split 1586 { 1587 var frame = try wgt.Box(Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 1588 errdefer frame.deinit(allocator); 1589 frame.getFocus().mode = .mouse; 1590 var detail_view = try DetailType.init(allocator, data, session, null, .{}); 1591 errdefer detail_view.deinit(allocator); 1592 frame.getFocus().child_id = detail_view.getFocus().id; 1593 try frame.children.put(allocator, detail_view.getFocus().id, .{ .widget = @unionInit(Widget, Data.detail_widget_name, detail_view), .rect = null, .min_size = null }); 1594 try box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 1595 } 1596 1597 box.getFocus().child_id = box.children.keys()[list_index]; 1598 return box; 1599 } 1600 1601 // a thread form, plus the target branch for patches, prefilled from 1602 // `record` when given. its form: subtree makes the web renderer POST 1603 // the inputs to `action`'s route. 1604 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 1605 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1606 errdefer box.deinit(allocator); 1607 box.getFocus().kind = .{ .custom = action }; 1608 1609 const saved_form = session.formFeedback(feedback_tag); 1610 const saved_fields = if (saved_form) |saved| saved.fields else null; 1611 1612 { 1613 var title = try wgt.TextInput.init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1614 errdefer title.deinit(allocator); 1615 title.getFocus().mode = .all; 1616 if (saved_fields) |saved| 1617 try title.setContent(allocator, saved.title) 1618 else if (record) |r| 1619 try title.setContent(allocator, r.event.title); 1620 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1621 } 1622 1623 { 1624 var tags = try wgt.TextInput.init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1625 errdefer tags.deinit(allocator); 1626 tags.getFocus().mode = .all; 1627 if (saved_fields) |saved| 1628 try tags.setContent(allocator, saved.tags) 1629 else if (record) |r| 1630 try tags.setContent(allocator, r.event.tags); 1631 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1632 } 1633 1634 { 1635 var description = try wgt.TextInput.init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 1636 errdefer description.deinit(allocator); 1637 description.getFocus().mode = .all; 1638 if (saved_fields) |saved| 1639 try description.setContent(allocator, saved.description) 1640 else if (record) |r| 1641 try description.setContent(allocator, r.event.description); 1642 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 1643 } 1644 1645 if (supports_drafts) { 1646 var target = try wgt.TextInput.init(allocator, .{ .label = " target branch ", .name = "target_branch", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1647 errdefer target.deinit(allocator); 1648 target.getFocus().mode = .all; 1649 const content = if (saved_fields) |saved| saved.target_branch else if (record) |r| r.event.target_branch else data.default_target_branch; 1650 try target.setContent(allocator, content); 1651 try box.children.put(allocator, target.getFocus().id, .{ .widget = .{ .text_input = target }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1652 } 1653 1654 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 1655 1656 box.getFocus().child_id = box.children.keys()[title_field_index]; 1657 return box; 1658 } 1659 1660 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) { 1661 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1662 errdefer box.deinit(allocator); 1663 box.getFocus().kind = .{ .custom = action }; 1664 1665 { 1666 var parent = try ui.authorBox(allocator, session.page_arena, author); 1667 errdefer parent.deinit(allocator); 1668 parent.options.label = " replying to "; 1669 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 1670 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1671 } 1672 1673 { 1674 var body = try wgt.TextInput.init(allocator, .{ 1675 .label = " comment ", 1676 .name = "body", 1677 .visible_width = null, 1678 .rounded_corners = true, 1679 .render_content = session.is_terminal, 1680 .multiline = true, 1681 .visible_height = 5, 1682 .scroll = .{ .fill = true }, 1683 }); 1684 errdefer body.deinit(allocator); 1685 body.getFocus().mode = .all; 1686 if (initial_body) |text| try body.setContent(allocator, text); 1687 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 1688 } 1689 1690 try addSubmitButtonLabeled(allocator, &box, "submit"); 1691 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 1692 return box; 1693 } 1694 1695 const ConfirmationButton = struct { 1696 label: []const u8, 1697 action: []const u8 = "submit", 1698 }; 1699 1700 fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 1701 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 1702 errdefer box.deinit(allocator); 1703 box.getFocus().kind = .{ .custom = action }; 1704 1705 var prompt = try wgt.Text.init(allocator, "are you sure?"); 1706 errdefer prompt.deinit(allocator); 1707 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 1708 1709 for (buttons, 0..) |options, index| { 1710 var button = try wgt.TextBox.init(allocator, options.label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1711 errdefer button.deinit(allocator); 1712 button.getFocus().mode = .all; 1713 button.getFocus().kind = .{ .custom = options.action }; 1714 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 1715 if (index == 0) box.getFocus().child_id = button.getFocus().id; 1716 } 1717 1718 return Center.init(allocator, .{ .box = box }); 1719 } 1720 1721 // a form's submit button, then a spacer absorbing the leftover 1722 // min-height the box hands its last child, so the button keeps its 1723 // natural height 1724 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 1725 { 1726 var submit = try SubmitButton.initLabeled(allocator, label); 1727 errdefer submit.deinit(allocator); 1728 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 1729 } 1730 { 1731 var spacer = try Spacer.init(allocator); 1732 errdefer spacer.deinit(allocator); 1733 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1734 } 1735 } 1736 1737 // the conflict resolve form. the "use this" links are navigations that 1738 // flip the url's theirs: pick, reloading the prefills; one submit 1739 // settles every conflict. the form: subtree makes the web renderer POST 1740 // to the resolve route. 1741 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 1742 const aa = session.page_arena.allocator(); 1743 // init only picks the resolve view once it has read the conflict 1744 const conflict = if (data.conflict) |*c| c else unreachable; 1745 1746 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1747 errdefer box.deinit(allocator); 1748 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 1749 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 1750 1751 if (conflict.title) |*fc| { 1752 try addLabel(allocator, &box, "title conflict:"); 1753 try addFieldConflict(allocator, &box, session, data, "title", fc); 1754 } 1755 if (conflict.tags) |*fc| { 1756 try addLabel(allocator, &box, "tags conflict:"); 1757 try addFieldConflict(allocator, &box, session, data, "tags", fc); 1758 } 1759 if (comptime @hasField(Data.Conflict, "status")) { 1760 if (conflict.status) |*fc| { 1761 try addLabel(allocator, &box, "status conflict:"); 1762 try addAtomicConflict(allocator, &box, session, data, "status", fc); 1763 } 1764 } 1765 if (comptime @hasField(Data.Conflict, "revision")) { 1766 if (conflict.revision) |*fc| { 1767 try addLabel(allocator, &box, "revision conflict:"); 1768 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 1769 } 1770 if (conflict.target_branch) |*fc| { 1771 try addLabel(allocator, &box, "target branch conflict:"); 1772 try addAtomicConflict(allocator, &box, session, data, "target_branch", fc); 1773 } 1774 } 1775 1776 if (conflict.description) |*desc| { 1777 try addLabel(allocator, &box, "description conflict:"); 1778 var hunk_index: usize = 0; 1779 for (desc.chunks, 0..) |*chunk, chunk_index| { 1780 switch (chunk.*) { 1781 .same => |text| { 1782 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 1783 errdefer tb.deinit(allocator); 1784 tb.getFocus().mode = .all; 1785 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1786 }, 1787 // an auto-resolved chunk: only one side changed it, so its 1788 // version stands, shown distinctly with its author 1789 .auto => |auto| { 1790 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 1791 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 1792 const label = switch (author) { 1793 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 1794 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 1795 }; 1796 var tb = try wgt.TextBox.init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1797 errdefer tb.deinit(allocator); 1798 tb.getFocus().mode = .all; 1799 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1800 }, 1801 .conflict => |hunk| { 1802 // a blank row on each side sets the group apart from 1803 // the flowing shared and auto-resolved chunks 1804 if (chunk_index > 0) try addGap(allocator, &box); 1805 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 1806 hunk_index += 1; 1807 const picked_theirs = data.theirsPicked(name); 1808 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 1809 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 1810 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 1811 errdefer resolution_input.deinit(allocator); 1812 resolution_input.getFocus().mode = .all; 1813 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 1814 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1815 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 1816 }, 1817 } 1818 } 1819 } 1820 1821 try addSubmitButtonLabeled(allocator, &box, "submit"); 1822 1823 box.getFocus().child_id = box.children.keys()[0]; 1824 return box; 1825 } 1826 1827 // one conflicted scalar field: both sides' version rows, then the 1828 // resolution input prefilled from the picked side. 1829 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1830 const aa = session.page_arena.allocator(); 1831 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1832 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1833 1834 var resolution_input = try wgt.TextInput.init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 1835 errdefer resolution_input.deinit(allocator); 1836 resolution_input.getFocus().mode = .all; 1837 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 1838 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 1839 } 1840 1841 // atomic fields can only pick one complete side 1842 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 1843 const aa = session.page_arena.allocator(); 1844 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 1845 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 1846 var selected = try wgt.TextBox.init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 1847 .border_style = .single, 1848 .rounded_corners = true, 1849 .wrap_kind = .word, 1850 .label = " resolution ", 1851 }); 1852 errdefer selected.deinit(allocator); 1853 selected.getFocus().mode = .all; 1854 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 1855 } 1856 1857 // a blank row setting a conflict group apart from its neighbors 1858 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 1859 var text = try wgt.Text.init(allocator, " "); 1860 errdefer text.deinit(allocator); 1861 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 1862 } 1863 1864 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 1865 var label = try SectionLabel.init(allocator, content); 1866 errdefer label.deinit(allocator); 1867 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 1868 } 1869 1870 // one side of a conflict: the "use this" link on the left, the version 1871 // itself as a labeled read-only box. 1872 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 1873 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1874 errdefer row.deinit(allocator); 1875 1876 { 1877 const use_label = "use this"; 1878 var use = try wgt.TextBox.init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1879 errdefer use.deinit(allocator); 1880 use.getFocus().mode = .all; 1881 use.getFocus().kind = .{ .custom = link }; 1882 try row.children.put(allocator, use.getFocus().id, .{ 1883 .widget = .{ .text_box = use }, 1884 .rect = null, 1885 .min_size = .{ .width = use_label.len + 2, .height = null }, 1886 .max_size = .{ .width = use_label.len + 2, .height = null }, 1887 }); 1888 } 1889 1890 { 1891 var tb = try wgt.TextBox.init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1892 errdefer tb.deinit(allocator); 1893 tb.getFocus().mode = .all; 1894 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1895 } 1896 1897 row.getFocus().child_id = row.children.keys()[0]; 1898 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1899 } 1900 1901 // the label of one side's version box, naming its author 1902 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1903 return switch (author) { 1904 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1905 .unknown => if (ours) " current version " else " their version ", 1906 }; 1907 } 1908 1909 // the a: link that reloads the resolve page with `name` picked from ours 1910 // or theirs, reprefilling its resolution input 1911 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1912 const aa = session.page_arena.allocator(); 1913 var picks: std.ArrayList(u8) = .empty; 1914 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1915 while (it.next()) |pick| { 1916 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1917 if (picks.items.len > 0) try picks.append(aa, ','); 1918 try picks.appendSlice(aa, pick); 1919 } 1920 if (theirs) { 1921 if (picks.items.len > 0) try picks.append(aa, ','); 1922 try picks.appendSlice(aa, name); 1923 } 1924 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1925 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1926 } 1927 1928 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1929 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1930 errdefer row.deinit(allocator); 1931 row.getFocus().mode = .all; 1932 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1933 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1934 } 1935 1936 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 1937 self.box.deinit(allocator); 1938 } 1939 1940 fn header(self: *This) *HeaderType { 1941 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 1942 } 1943 1944 fn viewStack(self: *This) *wgt.Stack(Widget) { 1945 return &self.box.children.values()[stack_index].widget.stack; 1946 } 1947 1948 // `index`'s master-detail split inside the stack. 1949 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 1950 return &self.viewStack().children.values()[index].box; 1951 } 1952 1953 // the tags view's flow inside the stack. 1954 fn tagsView(self: *This) *TagFlow { 1955 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1956 } 1957 1958 // the non-confirmation form inside the stack 1959 fn formBox(self: *This) ?*wgt.Box(Widget) { 1960 return switch (self.viewStack().children.values()[form_view_index]) { 1961 .box => |*box| box, 1962 // the resolve form sits inside a scroll on the terminal 1963 .scroll => |*scroll| &scroll.child.box, 1964 else => null, 1965 }; 1966 } 1967 1968 fn threadForm(self: *This) ?*wgt.Box(Widget) { 1969 return switch (self.data.view) { 1970 .new, .edit => self.formBox(), 1971 else => null, 1972 }; 1973 } 1974 1975 fn confirmationForm(self: *This) ?*wgt.Box(Widget) { 1976 return switch (self.viewStack().children.values()[form_view_index]) { 1977 .center => |*center| switch (center.child.*) { 1978 .box => |*box| box, 1979 else => null, 1980 }, 1981 else => null, 1982 }; 1983 } 1984 1985 // the resolve form's scroll on the terminal (the web page scrolls itself) 1986 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 1987 return switch (self.viewStack().children.values()[form_view_index]) { 1988 .scroll => |*scroll| scroll, 1989 else => null, 1990 }; 1991 } 1992 1993 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 1994 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1995 } 1996 1997 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 1998 return &self.listScroll(index).child.box; 1999 } 2000 2001 fn detail(self: *This, index: usize) *DetailType { 2002 const frame = &self.resultsBox(index).children.values()[detail_index].widget.box; 2003 return &@field(frame.children.values()[0].widget, Data.detail_widget_name); 2004 } 2005 2006 fn window(self: *This, index: usize) *const Window { 2007 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 2008 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 2009 return self.data.window(splitStatus(index)); 2010 } 2011 2012 fn stackSelectedIndex(self: *This) ?usize { 2013 const stack = self.viewStack(); 2014 const cid = stack.getFocus().child_id orelse return null; 2015 return stack.children.getIndex(cid); 2016 } 2017 2018 // the stack's selected master-detail split (null when the tags view or the 2019 // new-thread form shows). 2020 fn selectedSplitIndex(self: *This) ?usize { 2021 const idx = self.stackSelectedIndex() orelse return null; 2022 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 2023 } 2024 2025 fn detailActive(self: *This, index: usize) bool { 2026 const rb = self.resultsBox(index); 2027 const cid = rb.getFocus().child_id orelse return false; 2028 return rb.children.getIndex(cid) == detail_index; 2029 } 2030 2031 fn headerActive(self: *This) bool { 2032 const cid = self.box.getFocus().child_id orelse return false; 2033 return self.box.children.getIndex(cid) == header_index; 2034 } 2035 2036 fn tagsViewActive(self: *This) bool { 2037 if (self.headerActive()) return false; 2038 return self.stackSelectedIndex() == tags_view_index; 2039 } 2040 2041 fn formViewActive(self: *This) bool { 2042 if (self.headerActive()) return false; 2043 return self.stackSelectedIndex() == form_view_index; 2044 } 2045 2046 // the selected thread's index, or null when a window-navigation row is 2047 // selected (a leading "previous" row shifts the thread rows down by one). 2048 fn selectedThreadIndex(self: *This, index: usize) ?usize { 2049 const lb = self.listBox(index); 2050 const cid = lb.getFocus().child_id orelse return null; 2051 const idx = lb.children.getIndex(cid) orelse return null; 2052 const win = self.window(index); 2053 const lead: usize = if (win.prev_id != null) 1 else 0; 2054 if (idx < lead or idx - lead >= win.items.len) return null; 2055 return idx - lead; 2056 } 2057 2058 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2059 self.clearGrid(); 2060 2061 // each header tab maps 1:1 to a stack child by position 2062 if (self.header().getSelectedIndex()) |index| { 2063 const stack = self.viewStack(); 2064 stack.getFocus().child_id = stack.children.keys()[index]; 2065 } 2066 2067 if (self.selectedSplitIndex()) |i| { 2068 // swap the detail pane to the selected thread when it changes. 2069 if (self.selectedThreadIndex(i)) |selected| { 2070 const entry = self.window(i).items[selected]; 2071 const changed = if (self.detail(i).entry) |current| !std.mem.eql(u8, current.id, entry.id) else true; 2072 if (changed) { 2073 try self.detail(i).setEntry(allocator, entry); 2074 } 2075 } 2076 2077 // the selected list row shows a border (the focused TextBox 2078 // upgrades it to a double border itself); the rest stay borderless. 2079 const lb = self.listBox(i); 2080 for (lb.children.keys(), lb.children.values()) |id, *child| { 2081 switch (child.widget) { 2082 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 2083 else => {}, 2084 } 2085 } 2086 2087 // cap the list at list_max_width only while the detail pane fits 2088 // beside it. the box drops the detail when the width can't hold 2089 // both minimums, so when it's that narrow we lift the cap and let 2090 // the list fill the whole width. 2091 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 2092 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 2093 2094 // stretch the detail pane across the rest of the width so it fills 2095 // the area rather than shrinking to its content; its scroll fills 2096 // the pane. 2097 if (constraint.max_size.width) |w| { 2098 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 2099 } else { 2100 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 2101 } 2102 } 2103 2104 // refresh the form inputs' entries in the session's focus-id -> input 2105 // map with this frame's addresses, so the web/wasm form handling can 2106 // find them by focus id 2107 if (self.threadForm()) |form| { 2108 const failure = if (self.session.formFeedback(feedback_tag)) |saved| saved.failure else null; 2109 form.children.values()[title_field_index].widget.text_input.options.label = if (failure == .required_title) " title (required) " else " title "; 2110 if (comptime supports_drafts) { 2111 const invalid_target = failure == .invalid_target_branch; 2112 form.children.values()[target_branch_field_index].widget.text_input.options.label = if (invalid_target) " target branch (not found) " else " target branch "; 2113 } 2114 } 2115 if (self.formBox()) |form| { 2116 const inputs_arena = self.session.arena.allocator(); 2117 for (form.children.values()) |*child| switch (child.widget) { 2118 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 2119 else => {}, 2120 }; 2121 } 2122 2123 try self.box.build(allocator, constraint, root_focus); 2124 } 2125 2126 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 2127 // wheel input moves focus like the arrows 2128 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 2129 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 2130 else 2131 raw_key; 2132 2133 if (self.headerActive()) { 2134 // down from the tabs re-enters the stack if the selected view has 2135 // something to focus; other keys move the tabs. 2136 if (inp.vertDirection(key) == .down) { 2137 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 2138 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 2139 // nothing focusable in the unauthorized view 2140 .unauthorized => false, 2141 else => true, 2142 } else false; 2143 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 2144 } else { 2145 try self.header().input(allocator, key, root_focus); 2146 } 2147 return; 2148 } 2149 if (self.tagsViewActive()) { 2150 self.tagsViewInput(key, root_focus); 2151 return; 2152 } 2153 if (self.formViewActive()) { 2154 if (confirmation(self.data.view) != null) { 2155 try self.confirmationFormInput(allocator, key, root_focus); 2156 } else if (supports_conflicts and self.data.view == .resolve) { 2157 try self.resolveInput(allocator, key, root_focus); 2158 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 2159 try self.commentFormInput(allocator, key, root_focus); 2160 } else { 2161 try self.formInput(allocator, key, root_focus); 2162 } 2163 return; 2164 } 2165 const i = self.selectedSplitIndex() orelse return; 2166 if (self.detailActive(i)) { 2167 switch (try self.detail(i).inputWithExit(allocator, key, root_focus)) { 2168 .none => {}, 2169 .list => self.focusList(i, root_focus), 2170 .header => self.focusHeader(root_focus), 2171 } 2172 } else { 2173 self.listInput(i, key, root_focus); 2174 } 2175 } 2176 2177 // arrow keys move the tag selection; up from the top row crosses to the 2178 // header tabs. 2179 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 2180 const tf = self.tagsView(); 2181 const cid = tf.focus.child_id orelse return; 2182 const cur = tf.indexOfFocusId(cid) orelse return; 2183 const count = tf.text_boxes.items.len; 2184 switch (key) { 2185 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 2186 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 2187 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 2188 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 2189 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 2190 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 2191 else => {}, 2192 } 2193 } 2194 2195 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 2196 // up/down (and the scroll wheel) move the selection a row; page up/down 2197 // jump a fixed amount. right/Enter cross into the detail pane. up from 2198 // the top row crosses into the header tabs. 2199 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 2200 const lb = self.listBox(index); 2201 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 2202 if (delta < 0 and at_top) return self.focusHeader(root_focus); 2203 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 2204 return; 2205 } 2206 switch (key) { 2207 .enter, .arrow_right => _ = self.detail(index).focusFirst(root_focus), 2208 else => {}, 2209 } 2210 } 2211 2212 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2213 const form = self.threadForm() orelse return; 2214 const cid = form.getFocus().child_id orelse return; 2215 const cur = form.children.getIndex(cid) orelse return; 2216 2217 if (cur == description_field_index) { 2218 const child = &form.children.values()[cur]; 2219 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2220 return child.widget.input(allocator, key, root_focus); 2221 } 2222 2223 switch (key) { 2224 .arrow_up, .back_tab => if (cur > 0) 2225 root_focus.setFocus(form.children.keys()[cur - 1]) 2226 else 2227 self.focusHeader(root_focus), 2228 .arrow_down, .tab => if (cur < submit_field_index) { 2229 root_focus.setFocus(form.children.keys()[cur + 1]); 2230 }, 2231 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2232 .mouse => |mouse| if (cur == submit_field_index) { 2233 const submit = &form.children.values()[cur].widget.submit_button; 2234 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2235 }, 2236 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2237 } 2238 } 2239 2240 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2241 const form = self.formBox() orelse return; 2242 const cid = form.getFocus().child_id orelse return; 2243 const cur = form.children.getIndex(cid) orelse return; 2244 const child = &form.children.values()[cur]; 2245 2246 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2247 return child.widget.input(allocator, key, root_focus); 2248 2249 switch (key) { 2250 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2251 root_focus.setFocus(form.children.keys()[cur - 1]) 2252 else 2253 self.focusHeader(root_focus), 2254 .arrow_down, .tab => if (cur < comment_submit_field_index) 2255 root_focus.setFocus(form.children.keys()[cur + 1]), 2256 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2257 .mouse => |mouse| if (cur == comment_submit_field_index) { 2258 const submit = &child.widget.submit_button; 2259 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2260 }, 2261 else => try child.widget.input(allocator, key, root_focus), 2262 } 2263 } 2264 2265 fn confirmationFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2266 const form = self.confirmationForm() orelse return; 2267 const child_id = form.getFocus().child_id orelse return; 2268 const index = form.children.getIndex(child_id) orelse return; 2269 switch (key) { 2270 .arrow_up, .back_tab => if (index > 1) 2271 root_focus.setFocus(form.children.keys()[index - 1]) 2272 else 2273 self.focusHeader(root_focus), 2274 .arrow_down, .tab => if (index + 1 < form.children.count()) root_focus.setFocus(form.children.keys()[index + 1]), 2275 .enter => try self.submitConfirmation(allocator, index), 2276 .mouse => |mouse| for (form.children.keys()[1..], 1..) |button_id, button_index| { 2277 if (inp.leftClickOn(root_focus, button_id, mouse)) { 2278 try self.submitConfirmation(allocator, button_index); 2279 break; 2280 } 2281 }, 2282 else => {}, 2283 } 2284 } 2285 2286 // the multiline hunk inputs keep enter and any up/down that has a row to 2287 // move to; enter on a "use this" link is a navigation the host follows 2288 // before this runs. 2289 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2290 const form = self.formBox() orelse return; 2291 const cid = form.getFocus().child_id orelse return; 2292 const cur = form.children.getIndex(cid) orelse return; 2293 const child = &form.children.values()[cur]; 2294 2295 if (child.widget == .text_input) { 2296 const ti = &child.widget.text_input; 2297 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2298 return child.widget.input(allocator, key, root_focus); 2299 } 2300 2301 const on_submit = child.widget == .submit_button; 2302 2303 switch (key) { 2304 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2305 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2306 .arrow_left, .arrow_right => switch (child.widget) { 2307 .box => |*row| { 2308 const row_cid = row.getFocus().child_id orelse return; 2309 const row_index = row.children.getIndex(row_cid) orelse return; 2310 if (key == .arrow_left) { 2311 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2312 } else if (row_index + 1 < row.children.count()) { 2313 root_focus.setFocus(row.children.keys()[row_index + 1]); 2314 } 2315 }, 2316 else => try child.widget.input(allocator, key, root_focus), 2317 }, 2318 .enter => if (on_submit) try self.submitResolution(allocator), 2319 .mouse => |mouse| if (on_submit) { 2320 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2321 }, 2322 else => try child.widget.input(allocator, key, root_focus), 2323 } 2324 } 2325 2326 // a multiline input keeps enter and any up/down that has a row to move to 2327 fn multilineKeepsKey(text_input: *wgt.TextInput, allocator: std.mem.Allocator, key: Key) !bool { 2328 return switch (key) { 2329 .enter => true, 2330 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2331 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2332 else => false, 2333 }; 2334 } 2335 2336 // the neighboring focusable resolve-form child, skipping the spacer and 2337 // the blank gap rows 2338 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2339 var i = cur; 2340 while (true) { 2341 if (down) { 2342 i += 1; 2343 if (i >= form.children.count()) return null; 2344 } else { 2345 if (i == 0) return null; 2346 i -= 1; 2347 } 2348 switch (form.children.values()[i].widget) { 2349 .spacer, .text => continue, 2350 else => return i, 2351 } 2352 } 2353 } 2354 2355 // focus the resolve-form child at `i` (a version row focuses its selected 2356 // child) and keep it visible on the terminal 2357 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2358 const child = &form.children.values()[i]; 2359 switch (child.widget) { 2360 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2361 else => root_focus.setFocus(form.children.keys()[i]), 2362 } 2363 if (self.session.is_terminal) { 2364 if (self.resolveScroll()) |sc| { 2365 if (child.rect) |rect| sc.scrollToRect(rect); 2366 } 2367 } 2368 } 2369 2370 // re-emit the conflicted thread's event with the resolved content; any 2371 // event on the thread settles its conflict entry. this is the terminal 2372 // path; the web posts the form to the resolve route. 2373 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2374 if (comptime wasm) return; 2375 const io = self.session.io orelse return; 2376 const src = self.data.repo_source orelse return; 2377 const entry = self.data.selectedThread() orelse return; 2378 const author = (try self.session.eventAuthor()) orelse return; 2379 const form = self.formBox() orelse return; 2380 2381 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2382 var title: ?[]u8 = null; 2383 var tags: ?[]u8 = null; 2384 var hunks: std.ArrayList([]const u8) = .empty; 2385 defer { 2386 if (title) |t| allocator.free(t); 2387 if (tags) |t| allocator.free(t); 2388 for (hunks.items) |h| allocator.free(h); 2389 hunks.deinit(allocator); 2390 } 2391 for (form.children.values()) |*child| switch (child.widget) { 2392 .text_input => |*ti| { 2393 const text = try ti.text(allocator); 2394 errdefer allocator.free(text); 2395 if (std.mem.eql(u8, ti.options.name, "title")) { 2396 title = text; 2397 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2398 tags = text; 2399 } else { 2400 try hunks.append(allocator, text); 2401 } 2402 }, 2403 else => {}, 2404 }; 2405 2406 const id_bytes = try evt.parseEventId(entry.id); 2407 2408 switch (src.repo_kind) { 2409 inline else => |repo_kind| { 2410 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2411 defer any_repo.deinit(io, allocator); 2412 switch (any_repo) { 2413 inline else => |*repo| { 2414 var resolution: Event.Resolve = .{ 2415 .title = title, 2416 .tags = tags, 2417 .hunks = hunks.items, 2418 }; 2419 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2420 const change = Event.Update{ .resolve = resolution }; 2421 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2422 // leave the form up for correction 2423 error.InvalidFields => return, 2424 else => |e| return e, 2425 }; 2426 }, 2427 } 2428 }, 2429 } 2430 2431 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2432 try self.session.navigate(route); 2433 } 2434 2435 // commit a new or edited comment and navigate to its permalink. this is the 2436 // terminal path; the web posts the form to its comment route. 2437 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2438 if (comptime wasm) return; 2439 const io = self.session.io orelse return; 2440 const src = self.data.repo_source orelse return; 2441 const author = (try self.session.eventAuthor()) orelse return; 2442 const form = self.formBox() orelse return; 2443 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2444 const body = try body_input.text(allocator); 2445 defer allocator.free(body); 2446 if (!evt.Comment.fieldsValid(body)) return; 2447 2448 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2449 @memcpy(&thread_id, self.data.selected_id); 2450 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2451 var parent: [evt.event_id_size * 2]u8 = undefined; 2452 @memcpy(&parent, self.data.comment_id); 2453 break :blk parent; 2454 }; 2455 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2456 const editing = self.data.view == .edit_comment; 2457 switch (src.repo_kind) { 2458 inline else => |repo_kind| { 2459 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2460 defer any_repo.deinit(io, allocator); 2461 switch (any_repo) { 2462 inline else => |*repo| if (editing) { 2463 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2464 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2465 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2466 } else { 2467 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2468 }, 2469 } 2470 }, 2471 } 2472 2473 body_input.clear(allocator); 2474 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2475 try self.session.navigate(route); 2476 } 2477 2478 // the terminal submit paths: the new form commits a new thread event, the 2479 // edit form re-emits the selected thread's event with the form's content. 2480 // the web posts the forms to their routes instead. 2481 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2482 if (self.data.view == .edit) { 2483 try self.submitEditedThread(allocator); 2484 } else { 2485 try self.submitNewThread(allocator); 2486 } 2487 } 2488 2489 // commit the new thread to the repo's events branch and navigate to it. 2490 // this is the terminal path; the web posts the form to the thread route, 2491 // so the wasm side never runs (or compiles) the repo access below. 2492 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2493 if (comptime wasm) return; 2494 const io = self.session.io orelse return; 2495 const src = self.data.repo_source orelse return; 2496 const author = (try self.session.eventAuthor()) orelse return; 2497 2498 const form = self.threadForm() orelse return; 2499 const title_input = &form.children.values()[title_field_index].widget.text_input; 2500 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2501 const description_input = &form.children.values()[description_field_index].widget.text_input; 2502 2503 const title = try title_input.text(allocator); 2504 defer allocator.free(title); 2505 const tags = try tags_input.text(allocator); 2506 defer allocator.free(tags); 2507 const description = try description_input.text(allocator); 2508 defer allocator.free(description); 2509 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2510 defer if (comptime supports_drafts) allocator.free(target_branch); 2511 2512 if (!Event.fieldsValid(title, tags)) { 2513 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2514 return; 2515 } 2516 2517 if (comptime supports_drafts) { 2518 const event_id_hex = Data.createDraft(self.data, self.session, allocator, title, tags, description, target_branch) catch |err| switch (err) { 2519 error.InvalidFields => { 2520 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2521 return; 2522 }, 2523 else => |other| return other, 2524 }; 2525 title_input.clear(allocator); 2526 tags_input.clear(allocator); 2527 description_input.clear(allocator); 2528 form.children.values()[target_branch_field_index].widget.text_input.clear(allocator); 2529 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2530 try self.session.navigate(route); 2531 return; 2532 } 2533 2534 var id_bytes: [evt.event_id_size]u8 = undefined; 2535 io.random(&id_bytes); 2536 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2537 2538 const event = evt.EventWithId{ 2539 .id = event_id_hex, 2540 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2541 .author = author, 2542 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2543 .title = title, 2544 .description = description, 2545 .tags = tags, 2546 }), 2547 }; 2548 2549 switch (src.repo_kind) { 2550 inline else => |repo_kind| { 2551 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2552 defer any_repo.deinit(io, allocator); 2553 switch (any_repo) { 2554 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2555 } 2556 }, 2557 } 2558 2559 // wipe the form so a return visit starts fresh 2560 title_input.clear(allocator); 2561 tags_input.clear(allocator); 2562 description_input.clear(allocator); 2563 2564 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2565 try self.session.navigate(route); 2566 } 2567 2568 // re-emit the selected thread's event with the form's content (status 2569 // preserved), then reload the thread's page. this is the terminal path; 2570 // the web posts the form to the edit route. 2571 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2572 if (comptime wasm) return; 2573 const io = self.session.io orelse return; 2574 const entry = self.data.selectedThread() orelse return; 2575 2576 const form = self.threadForm() orelse return; 2577 const title_input = &form.children.values()[title_field_index].widget.text_input; 2578 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2579 const description_input = &form.children.values()[description_field_index].widget.text_input; 2580 2581 const title = try title_input.text(allocator); 2582 defer allocator.free(title); 2583 const tags = try tags_input.text(allocator); 2584 defer allocator.free(tags); 2585 const description = try description_input.text(allocator); 2586 defer allocator.free(description); 2587 const target_branch = if (comptime supports_drafts) try form.children.values()[target_branch_field_index].widget.text_input.text(allocator) else ""; 2588 defer if (comptime supports_drafts) allocator.free(target_branch); 2589 2590 if (!Event.fieldsValid(title, tags)) { 2591 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, tags, description, target_branch); 2592 return; 2593 } 2594 const src = self.data.repo_source orelse return; 2595 2596 if (comptime supports_drafts) { 2597 if (entryDraft(entry.*)) { 2598 self.data.editDraft(self.session, allocator, entry.id, title, tags, description, target_branch) catch |err| switch (err) { 2599 error.InvalidFields => { 2600 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2601 return; 2602 }, 2603 else => |other| return other, 2604 }; 2605 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2606 try self.session.navigate(route); 2607 return; 2608 } 2609 } 2610 2611 const id_bytes = try evt.parseEventId(entry.id); 2612 const author = (try self.session.eventAuthor()) orelse return; 2613 2614 switch (src.repo_kind) { 2615 inline else => |repo_kind| { 2616 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2617 defer any_repo.deinit(io, allocator); 2618 switch (any_repo) { 2619 inline else => |*repo| if (supports_drafts) { 2620 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2621 .title = title, 2622 .tags = tags, 2623 .description = description, 2624 .target_branch = target_branch, 2625 } }, author) catch |err| switch (err) { 2626 error.InvalidFields => { 2627 try self.rememberThreadFeedback(.invalid_target_branch, title, tags, description, target_branch); 2628 return; 2629 }, 2630 else => |other| return other, 2631 }; 2632 } else if (has_status) 2633 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2634 .title = title, 2635 .tags = tags, 2636 .description = description, 2637 } }, author) 2638 else 2639 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2640 } 2641 }, 2642 } 2643 2644 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2645 try self.session.navigate(route); 2646 } 2647 2648 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, tags: []const u8, description: []const u8, target_branch: []const u8) !void { 2649 const aa = self.session.arena.allocator(); 2650 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2651 .failure = failure, 2652 .fields = if (comptime supports_drafts) .{ 2653 .title = try aa.dupe(u8, title), 2654 .tags = try aa.dupe(u8, tags), 2655 .description = try aa.dupe(u8, description), 2656 .target_branch = try aa.dupe(u8, target_branch), 2657 } else .{ 2658 .title = try aa.dupe(u8, title), 2659 .tags = try aa.dupe(u8, tags), 2660 .description = try aa.dupe(u8, description), 2661 }, 2662 }); 2663 } 2664 2665 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2666 switch (confirmation(self.data.view) orelse return) { 2667 .publish => if (comptime supports_drafts) { 2668 if (comptime wasm) return; 2669 const entry = self.data.selectedThread() orelse return; 2670 if (!entryDraft(entry.*)) return; 2671 try self.data.publishDraft(self.session, allocator, entry.id); 2672 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2673 try self.session.navigate(route); 2674 } else unreachable, 2675 .merge => if (comptime supports_merge) { 2676 if (comptime wasm) return; 2677 const entry = self.data.selectedThread() orelse return; 2678 try self.data.mergePatch(self.session, allocator, entry.id, button_index > 1); 2679 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2680 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2681 try self.session.navigate(route); 2682 } else unreachable, 2683 .remove => try self.submitRemove(allocator), 2684 } 2685 } 2686 2687 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2688 const comment = self.data.comment_id.len != 0; 2689 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2690 const id = evt.parseEventId(event_id) catch return; 2691 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2692 } 2693 2694 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2695 if (comptime wasm) return; 2696 2697 if (comptime supports_drafts) { 2698 const selected = self.data.selectedThread() orelse return; 2699 if (event_kind == kind and entryDraft(selected.*)) { 2700 try Data.removeDraft(self.session, allocator, &id); 2701 const route = draftsRoute(self.data.identity) orelse return; 2702 try self.session.navigate(route); 2703 return; 2704 } 2705 } 2706 2707 const io = self.session.io orelse return; 2708 const src = self.data.repo_source orelse return; 2709 const author = (try self.session.eventAuthor()) orelse return; 2710 2711 switch (src.repo_kind) { 2712 inline else => |repo_kind| { 2713 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2714 defer any_repo.deinit(io, allocator); 2715 switch (any_repo) { 2716 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2717 } 2718 }, 2719 } 2720 2721 const id_hex = std.fmt.bytesToHex(id, .lower); 2722 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2723 try self.session.navigate(route); 2724 } 2725 2726 // return to the list. 2727 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2728 root_focus.setFocus(self.listScroll(index).getFocus().id); 2729 } 2730 2731 // cross to the header tabs above the stack. 2732 fn focusHeader(self: *This, root_focus: *Focus) void { 2733 root_focus.setFocus(self.box.children.keys()[header_index]); 2734 } 2735 2736 pub fn clearGrid(self: *This) void { 2737 self.box.clearGrid(); 2738 } 2739 2740 pub fn getGrid(self: This) ?Grid { 2741 return self.box.getGrid(); 2742 } 2743 2744 pub fn getFocus(self: *This) *Focus { 2745 return self.box.getFocus(); 2746 } 2747 2748 // for the parent's "scroll up at the top jumps to the header" check: at the 2749 // top only while the header tabs hold focus, so up from the split 2750 // crosses into the tabs first. 2751 pub fn atTop(self: *This) bool { 2752 return self.headerActive(); 2753 } 2754 }; 2755 } 2756