╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
81 .ours = .{ .text = ours.event.tags, .author = our_author }, 82 .theirs = .{ .text = theirs.event.tags, .author = their_author }, 83 }; 84 } else if (std.mem.eql(u8, field, "description")) { 85 conflict.description = .{ 86 .chunks = try diff3.chunks(io, arena.child_allocator, arena, if (base) |b| b.event.description else "", ours.event.description, theirs.event.description), 87 .ours_author = our_author, 88 .theirs_author = their_author, 89 }; 90 } else if (comptime @hasField(Data.Conflict, "status")) { 91 if (std.mem.eql(u8, field, "status")) { 92 conflict.status = .{ 93 .ours = .{ .text = @tagName(ours.event.status), .author = our_author }, 94 .theirs = .{ .text = @tagName(theirs.event.status), .author = their_author }, 95 }; 96 } else if (std.mem.eql(u8, field, "revision")) { 97 conflict.revision = .{ 98 .ours = .{ .text = try revisionSummary(aa, ours.event.revision), .author = our_author }, 99 .theirs = .{ .text = try revisionSummary(aa, theirs.event.revision), .author = their_author }, 100 }; 101 } 102 } 103 } 104 return conflict; 105 } 106 107 fn revisionSummary(allocator: std.mem.Allocator, revision_maybe: ?evt.Patch.Revision) ![]const u8 { 108 const revision = revision_maybe orelse return "(none)"; 109 return std.fmt.allocPrint(allocator, "{s}\nsquash {s}\nsource {s}", .{ revision.target_ref, revision.squash_oid, revision.source_oid }); 110 } 111 112 fn readFieldOid( 113 comptime hash_kind: hash.HashKind, 114 field_oids_maybe: ?evt.EventDB(hash_kind).SortedMap(.read_only), 115 field: []const u8, 116 ) !?[hash.byteLen(hash_kind)]u8 { 117 const field_oids = field_oids_maybe orelse return null; 118 const cursor = (try field_oids.getCursor(field)) orelse return null; 119 var oid: [hash.byteLen(hash_kind)]u8 = undefined; 120 _ = try cursor.readBytes(&oid); 121 return oid; 122 } 123 124 fn oidAuthor( 125 comptime repo_kind: rp.RepoKind, 126 comptime repo_opts: rp.RepoOpts(repo_kind), 127 arena: *std.heap.ArenaAllocator, 128 repo: *rp.Repo(repo_kind, repo_opts), 129 io: std.Io, 130 admin_moment: ?evt.AdminDB.HashMap(.read_only), 131 oid_maybe: ?[hash.byteLen(repo_opts.hash)]u8, 132 ) !ui.Author { 133 const oid = oid_maybe orelse return .unknown; 134 const gpa = arena.child_allocator; 135 var start_oids = [_][hash.hexLen(repo_opts.hash)]u8{std.fmt.bytesToHex(oid, .lower)}; 136 var commit_iter = repo.log(io, gpa, start_oids[0..1]) catch return .unknown; 137 defer commit_iter.deinit(); 138 const commit_object = (commit_iter.next(gpa) catch return .unknown) orelse return .unknown; 139 defer commit_object.deinit(); 140 return try ui.Author.init(admin_moment, arena, commit_object.content.commit.metadata.author orelse ""); 141 } 142 143 pub fn statusSet( 144 comptime Data: type, 145 comptime DB: type, 146 statuses: DB.SortedMap(.read_only), 147 status: Data.Status, 148 ) !?DB.SortedSet(.read_only) { 149 const cursor = (try statuses.getCursor(@tagName(status))) orelse return null; 150 return try DB.SortedSet(.read_only).init(cursor); 151 } 152 153 pub fn tagStatusSet( 154 comptime Data: type, 155 comptime DB: type, 156 tag_statuses: DB.SortedMap(.read_only), 157 tag: []const u8, 158 status: Data.Status, 159 ) !?DB.SortedSet(.read_only) { 160 var key_buffer: Data.Event.TagStatusKey = undefined; 161 const key = Data.Event.tagStatusKey(&key_buffer, tag, status) catch return null; 162 const cursor = (try tag_statuses.getCursor(key)) orelse return null; 163 return try DB.SortedSet(.read_only).init(cursor); 164 } 165 166 pub fn loadTags( 167 comptime Data: type, 168 comptime hash_kind: hash.HashKind, 169 arena: *std.heap.ArenaAllocator, 170 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 171 ) ![]const []const u8 { 172 const aa = arena.allocator(); 173 const DB = evt.EventDB(hash_kind); 174 const cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, Data.Event.tag_status_to_id_set_key)) orelse return &.{}; 175 const tag_statuses = try DB.SortedMap(.read_only).init(cursor); 176 var tags: std.ArrayList([]const u8) = .empty; 177 var iter = try tag_statuses.iterator(); 178 while (try iter.next()) |pair_cursor| { 179 if (tags.items.len == Data.max_tags) break; 180 var entry_cursor = pair_cursor; 181 const pair = try entry_cursor.readKeyValuePair(); 182 const key = try pair.key_cursor.readBytesAlloc(aa, null); 183 const space = std.mem.indexOfScalar(u8, key, ' ') orelse continue; 184 const tag = key[0..space]; 185 if (tags.getLastOrNull()) |last| { 186 if (std.mem.eql(u8, last, tag)) continue; 187 } 188 try tags.append(aa, tag); 189 } 190 return tags.items; 191 } 192 193 pub fn loadWindow( 194 comptime Data: type, 195 comptime hash_kind: hash.HashKind, 196 arena: *std.heap.ArenaAllocator, 197 admin_moment: ?evt.AdminDB.HashMap(.read_only), 198 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 199 records: evt.EventDB(hash_kind).HashMap(.read_only), 200 set_maybe: ?evt.EventDB(hash_kind).SortedSet(.read_only), 201 root_key: ?[]const u8, 202 conflict_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 203 selected_id: []const u8, 204 comments_start: usize, 205 ) !Data.Window { 206 const set = set_maybe orelse return .empty; 207 const DB = evt.EventDB(hash_kind); 208 const aa = arena.allocator(); 209 210 var prev_id: ?[]const u8 = null; 211 var iter = if (root_key) |key| blk: { 212 const rank = try set.rank(key); 213 if (rank > 0 and rank <= Data.page_size) { 214 prev_id = ""; 215 } else if (rank > Data.page_size) { 216 const pair = try set.getIndexKeyValuePair(@intCast(rank - Data.page_size)) orelse return error.NotFound; 217 var previous: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 218 _ = try pair.key_cursor.readBytes(&previous); 219 const hex = std.fmt.bytesToHex(previous[@sizeOf(u64)..].*, .lower); 220 prev_id = try aa.dupe(u8, &hex); 221 } 222 break :blk try set.iteratorFrom(key); 223 } else try set.iteratorFromIndex(0); 224 225 var items: std.ArrayList(Data.Entry) = .empty; 226 var next_id: ?[]const u8 = null; 227 while (try iter.next()) |cursor_value| { 228 var cursor = cursor_value; 229 const pair = try cursor.readKeyValuePair(); 230 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 231 _ = try pair.key_cursor.readBytes(&order_key); 232 const id = order_key[@sizeOf(u64)..]; 233 const id_hex = std.fmt.bytesToHex(id.*, .lower); 234 if (items.items.len == Data.page_size) { 235 next_id = try aa.dupe(u8, &id_hex); 236 break; 237 } 238 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse continue; 239 const record = try evt.read(Data.Event.Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 240 try items.append(aa, .{ 241 .id = try aa.dupe(u8, &id_hex), 242 .record = record, 243 .author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email), 244 .conflicted = if (conflict_set) |conflicts| try conflicts.contains(&order_key) else false, 245 .comments = try Comment.loadWindow( 246 hash_kind, 247 arena, 248 admin_moment, 249 haxy_moment, 250 evt.Comment.thread_id_to_comment_id_set_key, 251 &id_hex, 252 if (std.mem.eql(u8, &id_hex, selected_id)) comments_start else 0, 253 ), 254 .attachments = try Attachment.load(hash_kind, arena, haxy_moment, &id_hex), 255 }); 256 } 257 258 return .{ 259 .items = items.items, 260 .prev_id = prev_id, 261 .next_id = next_id, 262 .count = @intCast(try set.count()), 263 }; 264 } 265 266 // the common tab strip used by each thread page's sub-header 267 pub const Header = struct { 268 box: wgt.Box(Widget), 269 // focus id -> semantic stack index; some pages omit unavailable tabs 270 tab_ids: std.AutoArrayHashMapUnmanaged(usize, usize), 271 272 pub fn init(allocator: std.mem.Allocator) !Header { 273 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 274 errdefer box.deinit(allocator); 275 return .{ .box = box, .tab_ids = .empty }; 276 } 277 278 pub fn addTab(self: *Header, allocator: std.mem.Allocator, label: []const u8, link: []const u8, view_index: usize) !void { 279 var text_box = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 280 errdefer text_box.deinit(allocator); 281 text_box.getFocus().focusable = true; 282 text_box.getFocus().kind = .{ .custom = link }; 283 try self.tab_ids.put(allocator, text_box.getFocus().id, view_index); 284 try self.box.children.put(allocator, text_box.getFocus().id, .{ 285 .widget = .{ .text_box = text_box }, 286 .rect = null, 287 .min_size = .{ .width = label.len + 2, .height = null }, 288 }); 289 } 290 291 pub fn select(self: *Header, view_index: usize) void { 292 for (self.tab_ids.keys(), self.tab_ids.values()) |id, index| { 293 if (index == view_index) { 294 self.getFocus().child_id = id; 295 return; 296 } 297 } 298 } 299 300 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 301 self.box.deinit(allocator); 302 self.tab_ids.deinit(allocator); 303 } 304 305 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 306 self.clearGrid(); 307 // only the selected tab shows its border 308 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 309 switch (child.widget) { 310 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 311 else => {}, 312 } 313 } 314 try self.box.build(allocator, constraint, root_focus); 315 } 316 317 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 318 _ = allocator; 319 const current_tab = self.currentTabIndex() orelse return; 320 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 321 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 322 } 323 } 324 325 pub fn clearGrid(self: *Header) void { 326 self.box.clearGrid(); 327 } 328 329 pub fn getGrid(self: Header) ?Grid { 330 return self.box.getGrid(); 331 } 332 333 pub fn getFocus(self: *Header) *Focus { 334 return self.box.getFocus(); 335 } 336 337 pub fn getSelectedIndex(self: Header) ?usize { 338 const index = self.currentTabIndex() orelse return null; 339 return self.tab_ids.values()[index]; 340 } 341 342 fn currentTabIndex(self: Header) ?usize { 343 const child_id = self.box.focus.child_id orelse return null; 344 return self.tab_ids.getIndex(child_id); 345 } 346 }; 347 348 pub fn View(comptime kind: evt.EventKind, comptime Data: type) type { 349 const Self = Data; 350 const HeaderType = Data.Header; 351 const Window = Data.Window; 352 const Entry = Data.Entry; 353 const Event = Data.Event; 354 const Status = Data.Status; 355 const has_status = @hasField(Event, "status"); 356 const supports_conflicts = Event.merge_policy == .field_conflicts; 357 const supports_drafts = @hasField(Self, "drafts"); 358 const FieldConflict = if (supports_conflicts) Data.FieldConflict else void; 359 const ViewKind = Data.ViewKind; 360 const thread_name = Data.thread_name; 361 362 return struct { 363 const This = @This(); 364 // a vertical box: the header tabs on top, then a stack holding a 365 // master-detail split (thread list + description pane) per status list, 366 // plus the tags view. 367 box: wgt.Box(Widget), // vert: [header_index] = tabs, [stack_index] = stack 368 data: *const Self, 369 session: *ui.Session, 370 // per-split state, indexed like the stack's split children: the thread the 371 // pane shows and its focus ids. 372 detailed_index: [view_count]?usize, 373 title_id: [view_count]?usize, 374 description_id: [view_count]?usize, 375 author_id: [view_count]?usize, 376 377 const header_index: usize = 0; 378 const stack_index: usize = 1; 379 // indices within the stack, 1:1 with the header tabs. 380 const status_count = @typeInfo(Status).@"enum".fields.len; 381 const tags_view_index: usize = status_count; 382 // the new-thread form, or the edit, comment, or resolve form when the page was 383 // loaded at one of their urls. 384 const form_view_index: usize = tags_view_index + 1; 385 // the conflicts split; the tab and stack child exist only when the repo 386 // has conflicted threads. 387 const drafts_view_index: usize = form_view_index + 1; 388 const conflict_view_index: usize = drafts_view_index + @intFromBool(supports_drafts); 389 const view_count: usize = conflict_view_index + @intFromBool(supports_conflicts); 390 // indices within a split (the horizontal box inside the stack). 391 const list_index: usize = 0; 392 const detail_index: usize = 1; 393 const list_max_width: usize = 35; 394 const detail_min_width: usize = 40; 395 // indices within the thread form. 396 const title_field_index: usize = 0; 397 const tags_field_index: usize = 1; 398 const description_field_index: usize = 2; 399 const submit_field_index: usize = 3; 400 const comment_author_field_index: usize = 0; 401 const comment_body_field_index: usize = 1; 402 const comment_submit_field_index: usize = 2; 403 404 pub fn viewIndex(view: ViewKind) usize { 405 const name = @tagName(view); 406 if (std.mem.eql(u8, name, "tags")) return tags_view_index; 407 if (std.mem.eql(u8, name, "new") or std.mem.eql(u8, name, "edit") 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; 408 if (std.mem.eql(u8, name, "conflicts")) return conflict_view_index; 409 if (std.mem.eql(u8, name, "drafts")) return drafts_view_index; 410 if (std.mem.eql(u8, name, "description")) unreachable; 411 inline for (@typeInfo(Status).@"enum".fields, 0..) |field, index| { 412 if (std.mem.eql(u8, name, field.name)) return index; 413 } 414 unreachable; 415 } 416 417 // the status a status split lists; the conflicts split has none (its 418 // threads carry their own). 419 fn splitStatus(index: usize) Status { 420 return @enumFromInt(index); 421 } 422 423 fn entryConflicted(entry: Entry) bool { 424 return if (supports_conflicts) entry.conflicted else false; 425 } 426 427 fn entryStatus(entry: Entry) Status { 428 return if (has_status) entry.record.event.status else @enumFromInt(0); 429 } 430 431 fn entryDraft(entry: Entry) bool { 432 return if (supports_drafts) entry.draft else false; 433 } 434 435 const StatusChange = struct { 436 action: []const u8, 437 status: Status, 438 }; 439 440 fn statusChange(status: Status) ?StatusChange { 441 const name = @tagName(status); 442 if (std.mem.eql(u8, name, "open")) return .{ 443 .action = "close", 444 .status = std.meta.stringToEnum(Status, "closed") orelse return null, 445 }; 446 if (std.mem.eql(u8, name, "closed")) return .{ 447 .action = "open", 448 .status = std.meta.stringToEnum(Status, "open") orelse return null, 449 }; 450 return null; 451 } 452 453 fn listRoute(identity: []const u8, status: Status, tag: []const u8, selected: []const u8) ?ui.RoutablePage { 454 return Data.listRoute(identity, status, tag, selected); 455 } 456 457 fn draftsRoute(identity: []const u8) ?ui.RoutablePage { 458 return if (supports_drafts) Data.draftsRoute(identity) else null; 459 } 460 461 fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 462 return if (supports_conflicts) Data.conflictsRoute(identity, selected) else null; 463 } 464 465 fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 466 return if (supports_conflicts) Data.resolveRoute(identity, selected, picks) else null; 467 } 468 469 fn listLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: Status, tag: []const u8, id: []const u8) ![]const u8 { 470 const route = listRoute(identity, status, tag, id) orelse return error.RouteTooLong; 471 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 472 } 473 474 fn rowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, id: []const u8) ![]const u8 { 475 const selected = std.mem.eql(u8, id, data.selected_id); 476 const route = (if (selected and data.description_page) 477 ui.RoutablePage.repoThreadDescriptionRoute(kind, data.identity, id) 478 else if (selected) 479 if (data.comment_page) |page| 480 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, id, &page.selected.id, page.replies.start) 481 else 482 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, data.comments_start) 483 else 484 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, id, 0)) orelse return error.RouteTooLong; 485 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 486 } 487 488 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?Status, drafts: bool, id: []const u8) ![]const u8 { 489 if (supports_conflicts and status_maybe == null and !drafts) { 490 const route = conflictsRoute(data.identity, id) orelse return error.RouteTooLong; 491 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 492 } 493 if (drafts and id.len == 0) { 494 const route = draftsRoute(data.identity) orelse return error.RouteTooLong; 495 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 496 } 497 return listLink(page_arena, data.identity, status_maybe orelse @enumFromInt(0), data.tag, id); 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 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !This { 506 var outer = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 507 errdefer outer.deinit(allocator); 508 509 // the tabs at the top. 510 { 511 var hdr = try Data.initHeader(allocator, session, data); 512 errdefer hdr.deinit(allocator); 513 try outer.children.put(allocator, hdr.getFocus().id, .{ .widget = @unionInit(Widget, Data.header_widget_name, hdr), .rect = null, .min_size = null }); 514 } 515 516 // the stack enters `outer` before its children enter it, so an error 517 // frees each child exactly once. 518 { 519 var stack = try wgt.Stack(Widget).init(allocator); 520 errdefer stack.deinit(allocator); 521 try outer.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 522 } 523 const stack = &outer.children.values()[stack_index].widget.stack; 524 525 inline for (@typeInfo(Status).@"enum".fields) |field| { 526 var split = try initSplit(allocator, session, data, @enumFromInt(field.value), false); 527 errdefer split.deinit(allocator); 528 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 529 } 530 531 // the tags view 532 { 533 var tf = try TagFlow.init(allocator); 534 errdefer tf.deinit(allocator); 535 var items: std.ArrayList(TagFlow.Item) = .empty; 536 defer items.deinit(allocator); 537 // when filtered, the first item clears the filter 538 if (data.tag.len != 0) 539 try items.append(allocator, .{ .text = "✕", .link = try listLink(session.page_arena, data.identity, @enumFromInt(0), "", "") }); 540 for (data.tags) |tag| 541 try items.append(allocator, .{ .text = tag, .link = try tagLink(session.page_arena, data.identity, @enumFromInt(0), tag) }); 542 try tf.setItems(allocator, items.items); 543 try stack.children.put(allocator, tf.getFocus().id, .{ .tag_flow = tf }); 544 } 545 546 // the new-thread form, or — on an edit, comment, or resolve url — that form in 547 // its place, prefilled with the selected thread's content. a 548 // logged-out session can't create events, so the unauthorized view 549 // stands in. 550 if (session.data.is_local or session.data.user_id != null) { 551 if (data.view == .remove) { 552 const aa = session.page_arena.allocator(); 553 const route = ui.RoutablePage.repoThreadRemoveRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 554 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 555 const event_name = if (data.comment_id.len == 0) thread_name else "comment"; 556 var label_buf: ["remove ".len + @max(thread_name.len, "comment".len)]u8 = undefined; 557 const label = try std.fmt.bufPrint(&label_buf, "remove {s}", .{event_name}); 558 var center = try initRemoveForm(allocator, action, label); 559 errdefer center.deinit(allocator); 560 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 561 } else if (data.view == .new_comment or data.view == .edit_comment) { 562 const editing = data.view == .edit_comment; 563 const route = if (editing) 564 ui.RoutablePage.repoThreadCommentEditRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong 565 else 566 ui.RoutablePage.repoThreadCommentNewRoute(kind, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong; 567 const page_url = try route.toUrl(session.page_arena); 568 const action = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{page_url}); 569 const page = data.comment_page; 570 const top_level = if (page) |p| std.mem.eql(u8, &p.selected.comment.event.parent_id, &p.selected.comment.event.thread_id) else false; 571 const parent_route = if (editing) 572 if (top_level) 573 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0) 574 else if (page) |p| 575 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.comment.event.parent_id, 0) 576 else 577 null 578 else if (page) |p| 579 ui.RoutablePage.repoThreadCommentRoute(kind, data.identity, data.selected_id, &p.selected.id, 0) 580 else 581 ui.RoutablePage.repoThreadCommentsRoute(kind, data.identity, data.selected_id, 0); 582 const parent_author: ui.Author = if (editing) 583 if (top_level) 584 if (data.selectedThread()) |entry| entry.author else .unknown 585 else if (page) |p| 586 p.selected.parent_author orelse .unknown 587 else 588 .unknown 589 else if (page) |p| 590 p.selected.author 591 else if (data.selectedThread()) |entry| 592 entry.author 593 else 594 .unknown; 595 const initial_body: ?[]const u8 = if (editing) if (page) |p| p.selected.comment.event.body else null else null; 596 var form = try initCommentForm(allocator, session, action, parent_author, parent_route orelse return error.RouteTooLong, initial_body); 597 errdefer form.deinit(allocator); 598 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 599 } else if (supports_conflicts and data.view == .resolve) { 600 // on the web the page grows to the form's height and the 601 // browser scrolls it; the terminal scrolls the form itself 602 var form_widget: Widget = blk: { 603 var form = try initResolveForm(allocator, session, data); 604 errdefer form.deinit(allocator); 605 break :blk if (session.is_terminal) 606 .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = form }, .{ .direction = .vert }) } 607 else 608 .{ .box = form }; 609 }; 610 errdefer form_widget.deinit(allocator); 611 try stack.children.put(allocator, form_widget.getFocus().id, form_widget); 612 } else { 613 const aa = session.page_arena.allocator(); 614 const route = if (data.view == .edit) 615 ui.RoutablePage.repoThreadEditRoute(kind, data.identity, data.selected_id) orelse return error.RouteTooLong 616 else 617 ui.RoutablePage.repoThreadNewRoute(kind, data.identity) orelse return error.RouteTooLong; 618 const action = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}); 619 const record = if (data.view == .edit) 620 (if (data.selectedThread()) |entry| &entry.record else null) 621 else 622 null; 623 var form = try initThreadForm(allocator, session, action, record); 624 errdefer form.deinit(allocator); 625 try stack.children.put(allocator, form.getFocus().id, .{ .box = form }); 626 } 627 } else { 628 var message = try ui.Unauthorized.View.init(allocator); 629 errdefer message.deinit(allocator); 630 try stack.children.put(allocator, message.getFocus().id, .{ .unauthorized = message }); 631 } 632 633 if (supports_drafts) { 634 var split = try initSplit(allocator, session, data, null, true); 635 errdefer split.deinit(allocator); 636 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 637 } 638 639 // the conflicts split; its tab exists only when the repo has 640 // conflicts, so skip the child too to keep the stack 1:1 with the 641 // tabs. 642 if (supports_conflicts and data.conflicts.count > 0) { 643 var split = try initSplit(allocator, session, data, null, false); 644 errdefer split.deinit(allocator); 645 try stack.children.put(allocator, split.getFocus().id, .{ .box = split }); 646 } 647 648 // the stack starts on the page's view. 649 stack.getFocus().child_id = stack.children.keys()[viewIndex(data.view)]; 650 651 // focus entering the view lands on the tabs first. 652 outer.getFocus().child_id = outer.children.keys()[header_index]; 653 654 return .{ 655 .box = outer, 656 .data = data, 657 .session = session, 658 .detailed_index = @splat(null), 659 .title_id = @splat(null), 660 .description_id = @splat(null), 661 .author_id = @splat(null), 662 }; 663 } 664 665 // the master-detail split showing `status`'s window, or the conflicts 666 // window when null. 667 fn initSplit(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self, status_maybe: ?Status, drafts: bool) !wgt.Box(Widget) { 668 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 669 errdefer box.deinit(allocator); 670 671 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; 672 673 // the thread list (one focusable row per title), plus a "next" link that 674 // reloads the page rooted at the following thread. 675 { 676 var list_scroll = blk: { 677 var list_box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 678 errdefer list_box.deinit(allocator); 679 if (win.prev_id) |prev| 680 try addRow(allocator, &list_box, "← previous", "", try windowLink(session.page_arena, data, status_maybe, drafts, prev)); 681 for (win.items) |entry| { 682 // conflicts take precedence over the comment count 683 const label: []const u8 = if (entryConflicted(entry) and status_maybe != null) 684 " conflict " 685 else if (entry.comments.count > 0) 686 try std.fmt.allocPrint(session.page_arena.allocator(), " {d} comments ", .{entry.comments.count}) 687 else 688 ""; 689 try addRow(allocator, &list_box, entry.record.event.title, label, try rowLink(session.page_arena, data, entry.id)); 690 } 691 if (win.next_id) |next| 692 try addRow(allocator, &list_box, "next →", "", try windowLink(session.page_arena, data, status_maybe, drafts, next)); 693 // select the window's first thread (past a leading "previous" 694 // row) so its description shows on load. 695 if (win.items.len > 0) 696 list_box.getFocus().child_id = list_box.children.keys()[if (win.prev_id != null) 1 else 0] 697 else if (list_box.children.count() > 0) 698 list_box.getFocus().child_id = list_box.children.keys()[0]; 699 break :blk try wgt.Scroll(Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal }); 700 }; 701 errdefer list_scroll.deinit(allocator); 702 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 } }); 703 } 704 705 // the detail pane — a frame around a scroll of the description 706 { 707 var detail_outer = blk: { 708 var detail_scroll = blk2: { 709 var detail_inner = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 710 errdefer detail_inner.deinit(allocator); 711 // fill the pane (content top-left, scroll bar pinned to the 712 // edge) rather than shrinking to the description. 713 break :blk2 try wgt.Scroll(Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 714 }; 715 errdefer detail_scroll.deinit(allocator); 716 var frame = try wgt.Box(Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 717 errdefer frame.deinit(allocator); 718 // the tool row sits above the scroll (populateDetail fills 719 // it per thread) so it can't scroll out from under the web 720 // overlay <form>, whose position doesn't track pane scrolling. 721 { 722 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 723 errdefer row.deinit(allocator); 724 try frame.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 725 } 726 // the frame's selected child is its scroll, so the focus chain 727 // reaches the description (populateDetail points the scroll's 728 // inner box at it), letting focus recovery descend into the pane. 729 frame.getFocus().child_id = detail_scroll.getFocus().id; 730 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 731 break :blk frame; 732 }; 733 errdefer detail_outer.deinit(allocator); 734 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 735 } 736 737 box.getFocus().child_id = box.children.keys()[list_index]; 738 return box; 739 } 740 741 // a thread form: title/tags/description inputs and a submit button, 742 // prefilled from `record` when given. its form: subtree makes the web 743 // overlay wrap them in a <form> POSTing to `action`'s route. 744 fn initThreadForm(allocator: std.mem.Allocator, session: *ui.Session, action: []const u8, record: ?*const Event.Record) !wgt.Box(Widget) { 745 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 746 errdefer box.deinit(allocator); 747 box.getFocus().kind = .{ .custom = action }; 748 749 { 750 var title = try wgt.TextInput(Widget).init(allocator, .{ .label = " title ", .name = "title", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 751 errdefer title.deinit(allocator); 752 title.getFocus().focusable = true; 753 if (record) |r| try title.setContent(allocator, r.event.title); 754 try box.children.put(allocator, title.getFocus().id, .{ .widget = .{ .text_input = title }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 755 } 756 757 { 758 var tags = try wgt.TextInput(Widget).init(allocator, .{ .label = " tags (separate with spaces) ", .name = "tags", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 759 errdefer tags.deinit(allocator); 760 tags.getFocus().focusable = true; 761 if (record) |r| try tags.setContent(allocator, r.event.tags); 762 try box.children.put(allocator, tags.getFocus().id, .{ .widget = .{ .text_input = tags }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 763 } 764 765 { 766 var description = try wgt.TextInput(Widget).init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true, .scroll = .{ .fill = true } }); 767 errdefer description.deinit(allocator); 768 description.getFocus().focusable = true; 769 if (record) |r| try description.setContent(allocator, r.event.description); 770 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = null }); 771 } 772 773 try addSubmitButtonLabeled(allocator, &box, if (supports_drafts and record == null) "submit draft" else "submit"); 774 775 box.getFocus().child_id = box.children.keys()[title_field_index]; 776 return box; 777 } 778 779 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) { 780 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 781 errdefer box.deinit(allocator); 782 box.getFocus().kind = .{ .custom = action }; 783 784 { 785 var parent = try ui.authorBox(allocator, session.page_arena, author); 786 errdefer parent.deinit(allocator); 787 parent.options.label = " replying to "; 788 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try parent_route.toUrl(session.page_arena)}) }; 789 try box.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 790 } 791 792 { 793 var body = try wgt.TextInput(Widget).init(allocator, .{ 794 .label = " comment ", 795 .name = "body", 796 .visible_width = null, 797 .rounded_corners = true, 798 .render_content = session.is_terminal, 799 .multiline = true, 800 .visible_height = 5, 801 .scroll = .{ .fill = true }, 802 }); 803 errdefer body.deinit(allocator); 804 body.getFocus().focusable = true; 805 if (initial_body) |text| try body.setContent(allocator, text); 806 try box.children.put(allocator, body.getFocus().id, .{ .widget = .{ .text_input = body }, .rect = null, .min_size = null }); 807 } 808 809 try addSubmitButtonLabeled(allocator, &box, "submit"); 810 box.getFocus().child_id = box.children.keys()[comment_author_field_index]; 811 return box; 812 } 813 814 fn initRemoveForm(allocator: std.mem.Allocator, action: []const u8, label: []const u8) !Center { 815 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 816 errdefer box.deinit(allocator); 817 box.getFocus().kind = .{ .custom = action }; 818 819 var prompt = try wgt.Text(Widget).init(allocator, "are you sure?"); 820 errdefer prompt.deinit(allocator); 821 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 822 823 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 824 errdefer button.deinit(allocator); 825 button.getFocus().focusable = true; 826 button.getFocus().kind = .{ .custom = "submit" }; 827 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 828 box.getFocus().child_id = button.getFocus().id; 829 830 return Center.init(allocator, .{ .box = box }); 831 } 832 833 // a form's submit button, then a spacer absorbing the leftover 834 // min-height the box hands its last child, so the button keeps its 835 // natural height 836 fn addSubmitButtonLabeled(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8) !void { 837 { 838 var submit = try SubmitButton.initLabeled(allocator, label); 839 errdefer submit.deinit(allocator); 840 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 841 } 842 { 843 var spacer = try Spacer.init(allocator); 844 errdefer spacer.deinit(allocator); 845 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 846 } 847 } 848 849 // the conflict resolve form. the "use this" links are navigations that 850 // flip the url's theirs: pick, reloading the prefills; one submit 851 // settles every conflict. the form: subtree makes the web overlay POST 852 // to the resolve route. 853 fn initResolveForm(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !wgt.Box(Widget) { 854 const aa = session.page_arena.allocator(); 855 // init only picks the resolve view once it has read the conflict 856 const conflict = if (data.conflict) |*c| c else unreachable; 857 858 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 859 errdefer box.deinit(allocator); 860 const route = resolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong; 861 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(aa, "form:{s}", .{try route.toUrl(session.page_arena)}) }; 862 863 if (conflict.title) |*fc| { 864 try addLabel(allocator, &box, "title conflict:"); 865 try addFieldConflict(allocator, &box, session, data, "title", fc); 866 } 867 if (conflict.tags) |*fc| { 868 try addLabel(allocator, &box, "tags conflict:"); 869 try addFieldConflict(allocator, &box, session, data, "tags", fc); 870 } 871 if (comptime @hasField(Data.Conflict, "status")) { 872 if (conflict.status) |*fc| { 873 try addLabel(allocator, &box, "status conflict:"); 874 try addAtomicConflict(allocator, &box, session, data, "status", fc); 875 } 876 } 877 if (comptime @hasField(Data.Conflict, "revision")) { 878 if (conflict.revision) |*fc| { 879 try addLabel(allocator, &box, "revision conflict:"); 880 try addAtomicConflict(allocator, &box, session, data, "revision", fc); 881 } 882 } 883 884 if (conflict.description) |*desc| { 885 try addLabel(allocator, &box, "description conflict:"); 886 var hunk_index: usize = 0; 887 for (desc.chunks, 0..) |*chunk, chunk_index| { 888 switch (chunk.*) { 889 .same => |text| { 890 var tb = try wgt.TextBox(Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 891 errdefer tb.deinit(allocator); 892 tb.getFocus().focusable = true; 893 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 894 }, 895 // an auto-resolved chunk: only one side changed it, so its 896 // version stands, shown distinctly with its author 897 .auto => |auto| { 898 const author = if (auto.theirs) desc.theirs_author else desc.ours_author; 899 const verb: []const u8 = if (auto.text == null) "removed" else "edited"; 900 const label = switch (author) { 901 .user_name, .email => |name| try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, name }), 902 .unknown => try std.fmt.allocPrint(aa, " {s} by {s} ", .{ verb, if (auto.theirs) @as([]const u8, "them") else "us" }), 903 }; 904 var tb = try wgt.TextBox(Widget).init(allocator, auto.text orelse "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 905 errdefer tb.deinit(allocator); 906 tb.getFocus().focusable = true; 907 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 908 }, 909 .conflict => |hunk| { 910 // a blank row on each side sets the group apart from 911 // the flowing shared and auto-resolved chunks 912 if (chunk_index > 0) try addGap(allocator, &box); 913 const name = try std.fmt.allocPrint(aa, "d{d}", .{hunk_index}); 914 hunk_index += 1; 915 const picked_theirs = data.theirsPicked(name); 916 try addVersionRow(allocator, &box, try sideLabel(aa, desc.ours_author, true), hunk.ours orelse "", try useThisLink(session, data, name, false)); 917 try addVersionRow(allocator, &box, try sideLabel(aa, desc.theirs_author, false), hunk.theirs orelse "", try useThisLink(session, data, name, true)); 918 var resolution_input = try wgt.TextInput(Widget).init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal, .multiline = true }); 919 errdefer resolution_input.deinit(allocator); 920 resolution_input.getFocus().focusable = true; 921 try resolution_input.setContent(allocator, (if (picked_theirs) hunk.theirs else hunk.ours) orelse ""); 922 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 923 if (chunk_index + 1 < desc.chunks.len) try addGap(allocator, &box); 924 }, 925 } 926 } 927 } 928 929 try addSubmitButtonLabeled(allocator, &box, "submit"); 930 931 box.getFocus().child_id = box.children.keys()[0]; 932 return box; 933 } 934 935 // one conflicted scalar field: both sides' version rows, then the 936 // resolution input prefilled from the picked side. 937 fn addFieldConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 938 const aa = session.page_arena.allocator(); 939 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 940 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 941 942 var resolution_input = try wgt.TextInput(Widget).init(allocator, .{ .label = " resolution ", .name = name, .visible_width = null, .rounded_corners = true, .render_content = session.is_terminal }); 943 errdefer resolution_input.deinit(allocator); 944 resolution_input.getFocus().focusable = true; 945 try resolution_input.setContent(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text); 946 try box.children.put(allocator, resolution_input.getFocus().id, .{ .widget = .{ .text_input = resolution_input }, .rect = null, .min_size = null }); 947 } 948 949 // atomic fields can only pick one complete side 950 fn addAtomicConflict(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session, data: *const Self, comptime name: []const u8, fc: *const FieldConflict) !void { 951 const aa = session.page_arena.allocator(); 952 try addVersionRow(allocator, box, try sideLabel(aa, fc.ours.author, true), fc.ours.text, try useThisLink(session, data, name, false)); 953 try addVersionRow(allocator, box, try sideLabel(aa, fc.theirs.author, false), fc.theirs.text, try useThisLink(session, data, name, true)); 954 var selected = try wgt.TextBox(Widget).init(allocator, if (data.theirsPicked(name)) fc.theirs.text else fc.ours.text, .{ 955 .border_style = .single, 956 .rounded_corners = true, 957 .wrap_kind = .word, 958 .label = " resolution ", 959 }); 960 errdefer selected.deinit(allocator); 961 selected.getFocus().focusable = true; 962 try box.children.put(allocator, selected.getFocus().id, .{ .widget = .{ .text_box = selected }, .rect = null, .min_size = null }); 963 } 964 965 // a blank row setting a conflict group apart from its neighbors 966 fn addGap(allocator: std.mem.Allocator, box: *wgt.Box(Widget)) !void { 967 var text = try wgt.Text(Widget).init(allocator, " "); 968 errdefer text.deinit(allocator); 969 try box.children.put(allocator, text.getFocus().id, .{ .widget = .{ .text = text }, .rect = null, .min_size = null }); 970 } 971 972 fn addLabel(allocator: std.mem.Allocator, box: *wgt.Box(Widget), content: []const u8) !void { 973 var label = try SectionLabel.init(allocator, content); 974 errdefer label.deinit(allocator); 975 try box.children.put(allocator, label.getFocus().id, .{ .widget = .{ .section_label = label }, .rect = null, .min_size = null }); 976 } 977 978 // one side of a conflict: the "use this" link on the left, the version 979 // itself as a labeled read-only box. 980 fn addVersionRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), label: []const u8, text: []const u8, link: []const u8) !void { 981 var row = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 982 errdefer row.deinit(allocator); 983 984 { 985 const use_label = "use this"; 986 var use = try wgt.TextBox(Widget).init(allocator, use_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 987 errdefer use.deinit(allocator); 988 use.getFocus().focusable = true; 989 use.getFocus().kind = .{ .custom = link }; 990 try row.children.put(allocator, use.getFocus().id, .{ 991 .widget = .{ .text_box = use }, 992 .rect = null, 993 .min_size = .{ .width = use_label.len + 2, .height = null }, 994 .max_size = .{ .width = use_label.len + 2, .height = null }, 995 }); 996 } 997 998 { 999 var tb = try wgt.TextBox(Widget).init(allocator, if (text.len > 0) text else "(removed)", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = label }); 1000 errdefer tb.deinit(allocator); 1001 tb.getFocus().focusable = true; 1002 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1003 } 1004 1005 row.getFocus().child_id = row.children.keys()[0]; 1006 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 1007 } 1008 1009 // the label of one side's version box, naming its author 1010 fn sideLabel(aa: std.mem.Allocator, author: ui.Author, ours: bool) ![]const u8 { 1011 return switch (author) { 1012 .user_name, .email => |name| try std.fmt.allocPrint(aa, " edited by {s} ", .{name}), 1013 .unknown => if (ours) " current version " else " their version ", 1014 }; 1015 } 1016 1017 // the a: link that reloads the resolve page with `name` picked from ours 1018 // or theirs, reprefilling its resolution input 1019 fn useThisLink(session: *ui.Session, data: *const Self, name: []const u8, theirs: bool) ![]const u8 { 1020 const aa = session.page_arena.allocator(); 1021 var picks: std.ArrayList(u8) = .empty; 1022 var it = std.mem.splitScalar(u8, data.theirs_picks, ','); 1023 while (it.next()) |pick| { 1024 if (pick.len == 0 or std.mem.eql(u8, pick, name)) continue; 1025 if (picks.items.len > 0) try picks.append(aa, ','); 1026 try picks.appendSlice(aa, pick); 1027 } 1028 if (theirs) { 1029 if (picks.items.len > 0) try picks.append(aa, ','); 1030 try picks.appendSlice(aa, name); 1031 } 1032 const route = resolveRoute(data.identity, data.selected_id, picks.items) orelse return error.RouteTooLong; 1033 return std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(session.page_arena)}); 1034 } 1035 1036 // the resolve form's page-constant initial value for the input `name`: 1037 // the picked side of its conflicted field or hunk 1038 fn resolvePrefill(self: *This, name: []const u8) ?[]const u8 { 1039 const conflict = if (self.data.conflict) |*c| c else return null; 1040 const theirs = self.data.theirsPicked(name); 1041 if (std.mem.eql(u8, name, "title")) { 1042 const fc = if (conflict.title) |*f| f else return null; 1043 return if (theirs) fc.theirs.text else fc.ours.text; 1044 } 1045 if (std.mem.eql(u8, name, "tags")) { 1046 const fc = if (conflict.tags) |*f| f else return null; 1047 return if (theirs) fc.theirs.text else fc.ours.text; 1048 } 1049 if (std.mem.startsWith(u8, name, "d")) { 1050 const desc = if (conflict.description) |*d| d else return null; 1051 const hunk_index = std.fmt.parseInt(usize, name[1..], 10) catch return null; 1052 var seen: usize = 0; 1053 for (desc.chunks) |chunk| switch (chunk) { 1054 .conflict => |hunk| { 1055 if (seen == hunk_index) return (if (theirs) hunk.theirs else hunk.ours) orelse ""; 1056 seen += 1; 1057 }, 1058 else => {}, 1059 }; 1060 } 1061 return null; 1062 } 1063 1064 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(Widget), text: []const u8, bottom_label: []const u8, link: []const u8) !void { 1065 var row = try wgt.TextBox(Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 1066 errdefer row.deinit(allocator); 1067 row.getFocus().focusable = true; 1068 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 1069 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 1070 } 1071 1072 fn addToolButton(allocator: std.mem.Allocator, row: *wgt.Box(Widget), label: []const u8, action: []const u8) !void { 1073 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1074 errdefer button.deinit(allocator); 1075 button.getFocus().focusable = true; 1076 button.getFocus().kind = .{ .custom = action }; 1077 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 } }); 1078 } 1079 1080 pub fn deinit(self: *This, allocator: std.mem.Allocator) void { 1081 self.box.deinit(allocator); 1082 } 1083 1084 fn header(self: *This) *HeaderType { 1085 return &@field(self.box.children.values()[header_index].widget, Data.header_widget_name); 1086 } 1087 1088 fn viewStack(self: *This) *wgt.Stack(Widget) { 1089 return &self.box.children.values()[stack_index].widget.stack; 1090 } 1091 1092 // `index`'s master-detail split inside the stack. 1093 fn resultsBox(self: *This, index: usize) *wgt.Box(Widget) { 1094 return &self.viewStack().children.values()[index].box; 1095 } 1096 1097 // the tags view's flow inside the stack. 1098 fn tagsView(self: *This) *TagFlow { 1099 return &self.viewStack().children.values()[tags_view_index].tag_flow; 1100 } 1101 1102 // the new-thread, edit, or resolve form inside the stack, or null when the 1103 // unauthorized view stands in for it. 1104 fn threadForm(self: *This) ?*wgt.Box(Widget) { 1105 return switch (self.viewStack().children.values()[form_view_index]) { 1106 .box => |*box| box, 1107 // the resolve form sits inside a scroll on the terminal 1108 .scroll => |*scroll| &scroll.child.box, 1109 else => null, 1110 }; 1111 } 1112 1113 fn removeForm(self: *This) ?*wgt.Box(Widget) { 1114 return switch (self.viewStack().children.values()[form_view_index]) { 1115 .center => |*center| switch (center.child.*) { 1116 .box => |*box| box, 1117 else => null, 1118 }, 1119 else => null, 1120 }; 1121 } 1122 1123 // the resolve form's scroll on the terminal (the web page scrolls itself) 1124 fn resolveScroll(self: *This) ?*wgt.Scroll(Widget) { 1125 return switch (self.viewStack().children.values()[form_view_index]) { 1126 .scroll => |*scroll| scroll, 1127 else => null, 1128 }; 1129 } 1130 1131 fn listScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 1132 return &self.resultsBox(index).children.values()[list_index].widget.scroll; 1133 } 1134 1135 fn listBox(self: *This, index: usize) *wgt.Box(Widget) { 1136 return &self.listScroll(index).child.box; 1137 } 1138 1139 // the detail frame's children: the tool row above the scroll pane. 1140 const tool_row_index: usize = 0; 1141 const detail_scroll_index: usize = 1; 1142 1143 fn detailOuter(self: *This, index: usize) *wgt.Box(Widget) { 1144 return &self.resultsBox(index).children.values()[detail_index].widget.box; 1145 } 1146 1147 fn toolRow(self: *This, index: usize) *wgt.Box(Widget) { 1148 return &self.detailOuter(index).children.values()[tool_row_index].widget.box; 1149 } 1150 1151 fn detailScroll(self: *This, index: usize) *wgt.Scroll(Widget) { 1152 return &self.detailOuter(index).children.values()[detail_scroll_index].widget.scroll; 1153 } 1154 1155 fn detailInner(self: *This, index: usize) *wgt.Box(Widget) { 1156 return &self.detailScroll(index).child.box; 1157 } 1158 1159 fn window(self: *This, index: usize) *const Window { 1160 if (supports_conflicts and index == conflict_view_index) return &self.data.conflicts; 1161 if (supports_drafts and index == drafts_view_index) return &self.data.drafts; 1162 return self.data.window(splitStatus(index)); 1163 } 1164 1165 fn stackSelectedIndex(self: *This) ?usize { 1166 const stack = self.viewStack(); 1167 const cid = stack.getFocus().child_id orelse return null; 1168 return stack.children.getIndex(cid); 1169 } 1170 1171 // the stack's selected master-detail split (null when the tags view or the 1172 // new-thread form shows). 1173 fn selectedSplitIndex(self: *This) ?usize { 1174 const idx = self.stackSelectedIndex() orelse return null; 1175 return if (idx < status_count or (supports_drafts and idx == drafts_view_index) or idx == conflict_view_index) idx else null; 1176 } 1177 1178 fn detailActive(self: *This, index: usize) bool { 1179 const rb = self.resultsBox(index); 1180 const cid = rb.getFocus().child_id orelse return false; 1181 return rb.children.getIndex(cid) == detail_index; 1182 } 1183 1184 fn headerActive(self: *This) bool { 1185 const cid = self.box.getFocus().child_id orelse return false; 1186 return self.box.children.getIndex(cid) == header_index; 1187 } 1188 1189 fn tagsViewActive(self: *This) bool { 1190 if (self.headerActive()) return false; 1191 return self.stackSelectedIndex() == tags_view_index; 1192 } 1193 1194 fn formViewActive(self: *This) bool { 1195 if (self.headerActive()) return false; 1196 return self.stackSelectedIndex() == form_view_index; 1197 } 1198 1199 // the selected thread's index, or null when a window-navigation row is 1200 // selected (a leading "previous" row shifts the thread rows down by one). 1201 fn selectedThreadIndex(self: *This, index: usize) ?usize { 1202 const lb = self.listBox(index); 1203 const cid = lb.getFocus().child_id orelse return null; 1204 const idx = lb.children.getIndex(cid) orelse return null; 1205 const win = self.window(index); 1206 const lead: usize = if (win.prev_id != null) 1 else 0; 1207 if (idx < lead or idx - lead >= win.items.len) return null; 1208 return idx - lead; 1209 } 1210 1211 pub fn build(self: *This, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1212 self.clearGrid(); 1213 1214 // each header tab maps 1:1 to a stack child by position 1215 if (self.header().getSelectedIndex()) |index| { 1216 const stack = self.viewStack(); 1217 stack.getFocus().child_id = stack.children.keys()[index]; 1218 } 1219 1220 if (self.selectedSplitIndex()) |i| { 1221 // swap the detail pane to the selected thread when it changes. 1222 if (self.selectedThreadIndex(i)) |selected| { 1223 const changed = if (self.detailed_index[i]) |current| current != selected else true; 1224 if (changed) { 1225 try self.populateDetail(allocator, i, selected); 1226 self.detailed_index[i] = selected; 1227 } 1228 } 1229 1230 // the selected list row shows a border (the focused TextBox 1231 // upgrades it to a double border itself); the rest stay borderless. 1232 const lb = self.listBox(i); 1233 for (lb.children.keys(), lb.children.values()) |id, *child| { 1234 switch (child.widget) { 1235 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 1236 else => {}, 1237 } 1238 } 1239 1240 // the pane's selected child shows the selection border: the 1241 // description directly, the tags via the flow's selected item. 1242 // cap the list at list_max_width only while the detail pane fits 1243 // beside it. the box drops the detail when the width can't hold 1244 // both minimums, so when it's that narrow we lift the cap and let 1245 // the list fill the whole width. 1246 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 1247 self.resultsBox(i).children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 1248 1249 // stretch the detail pane across the rest of the width so it fills 1250 // the area rather than shrinking to its content; its scroll fills 1251 // the pane. 1252 if (constraint.max_size.width) |w| { 1253 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 1254 } else { 1255 self.resultsBox(i).children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 1256 } 1257 } 1258 1259 // refresh the form inputs' entries in the session's focus-id -> input 1260 // map with this frame's addresses, so the web/wasm form handling can 1261 // find them by focus id 1262 if (self.threadForm()) |form| { 1263 const inputs_arena = self.session.arena.allocator(); 1264 for (form.children.values()) |*child| switch (child.widget) { 1265 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 1266 else => {}, 1267 }; 1268 1269 // on an edit page, register the thread's content as the inputs' 1270 // page-constant initial values, which the web overlay renders 1271 // into them. 1272 if (self.data.view == .edit) { 1273 if (self.data.selectedThread()) |entry| { 1274 const fields = form.children.values(); 1275 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.record.event.title); 1276 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.record.event.tags); 1277 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.record.event.description); 1278 } 1279 } 1280 1281 if (self.data.view == .edit_comment) { 1282 if (self.data.comment_page) |page| { 1283 const body = &form.children.values()[comment_body_field_index].widget.text_input; 1284 try self.session.input_values.put(inputs_arena, body.getFocus().id, page.selected.comment.event.body); 1285 } 1286 } 1287 1288 // the resolve form's inputs prefill from the picked side, which 1289 // is url state and so page-constant like the edit values above 1290 if (supports_conflicts and self.data.view == .resolve) { 1291 for (form.children.values()) |*child| switch (child.widget) { 1292 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 1293 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 1294 }, 1295 else => {}, 1296 }; 1297 } 1298 } 1299 1300 try self.box.build(allocator, constraint, root_focus); 1301 } 1302 1303 fn populateDetail(self: *This, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1304 const entry = self.window(index).items[sel]; 1305 const inner = self.detailInner(index); 1306 // the /description page replaces its thread's detail with a back link 1307 // and the whole description; other threads keep their normal detail. 1308 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1309 const comment_page = if (std.mem.eql(u8, entry.id, self.data.selected_id)) self.data.comment_page else null; 1310 1311 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1312 inner.children.clearAndFree(allocator); 1313 inner.getFocus().child_id = null; 1314 self.title_id[index] = null; 1315 self.author_id[index] = null; 1316 1317 // the tool row; the description page shows none. 1318 { 1319 const row = self.toolRow(index); 1320 for (row.children.values()) |*child| child.widget.deinit(allocator); 1321 row.children.clearAndFree(allocator); 1322 row.getFocus().child_id = null; 1323 row.getFocus().kind = .container; 1324 } 1325 if (!description_page and comment_page == null) { 1326 const row = self.toolRow(index); 1327 const pa = self.session.page_arena.allocator(); 1328 1329 { 1330 var spacer = try Spacer.init(allocator); 1331 errdefer spacer.deinit(allocator); 1332 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1333 } 1334 1335 if (supports_drafts and entryDraft(entry)) { 1336 if (entry.revision_ready) { 1337 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return error.RouteTooLong; 1338 row.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "form:{s}/post", .{try route.toUrl(self.session.page_arena)}) }; 1339 try addToolButton(allocator, row, "post", "submit"); 1340 } 1341 if (row.children.count() > first_in_row_index) 1342 row.getFocus().child_id = row.children.keys()[first_in_row_index]; 1343 } else { 1344 // a terminal has no file picker, so attaching is web only. the 1345 // renderer covers this button with a file input, so clicking it 1346 // opens the browser's own picker and posts what it chose. 1347 if (!self.session.is_terminal and !entryConflicted(entry) and (self.session.data.is_local or self.session.data.user_id != null)) { 1348 const label = "add attachment"; 1349 const action = if (self.data.identity.len == 0) 1350 try std.fmt.allocPrint(pa, "{s}/{s}:{s}/attach", .{ ui.file_input_prefix, @tagName(kind), entry.id }) 1351 else 1352 try std.fmt.allocPrint(pa, "{s}/repo/{s}/{s}:{s}/attach", .{ ui.file_input_prefix, self.data.identity, @tagName(kind), entry.id }); 1353 try addToolButton(allocator, row, label, action); 1354 } 1355 1356 // a conflicted thread's only action is resolving it. the button 1357 // stays visible logged out; the resolve page shows the 1358 // unauthorized view then. 1359 if (supports_conflicts and entryConflicted(entry)) { 1360 const label = "resolve conflict"; 1361 const route = resolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1362 try addToolButton(allocator, row, label, try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 1363 } else { 1364 // a logged-out session can't flip the status, so it gets no 1365 // open/close button 1366 if (has_status and (self.session.data.is_local or self.session.data.user_id != null)) { 1367 if (statusChange(entryStatus(entry))) |change| { 1368 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1369 row.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "form:{s}/{s}", .{ try route.toUrl(self.session.page_arena), change.action }) }; 1370 try addToolButton(allocator, row, change.action, "submit"); 1371 } 1372 } 1373 1374 // the edit button links to the thread's edit page. 1375 { 1376 const route = ui.RoutablePage.repoThreadEditRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 1377 try addToolButton(allocator, row, "edit", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 1378 } 1379 } 1380 1381 if (self.session.data.is_local or self.session.data.user_id != null) { 1382 const route = ui.RoutablePage.repoThreadRemoveRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1383 try addToolButton(allocator, row, "✕", try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)})); 1384 } 1385 1386 // the leftmost button: the attachment button when the session has 1387 // one, else the resolve button on a conflicted thread, the 1388 // open/close button when present, the edit button otherwise 1389 row.getFocus().child_id = row.children.keys()[first_in_row_index]; 1390 } 1391 } 1392 1393 if (supports_drafts and entryDraft(entry) and !description_page and comment_page == null) { 1394 try self.data.appendDraftDetails(allocator, inner, self.session, entry); 1395 } 1396 1397 if (comment_page) |page| { 1398 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 1399 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 1400 var back = try Comment.linkBox(allocator, self.session, back_label, ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong); 1401 errdefer back.deinit(allocator); 1402 try inner.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null }); 1403 1404 try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, page.selected); 1405 try Comment.appendCount(allocator, inner, page.replies.count, "reply", "replies"); 1406 for (page.replies.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1407 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, &page.selected.comment.event.thread_id, &page.selected.id, page.replies); 1408 1409 var spacer = try Spacer.init(allocator); 1410 errdefer spacer.deinit(allocator); 1411 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1412 1413 inner.getFocus().child_id = inner.children.keys()[0]; 1414 const sc = self.detailScroll(index); 1415 sc.x = 0; 1416 sc.y = 0; 1417 sc.getFocus().version +%= 1; 1418 return; 1419 } 1420 1421 if (description_page) { 1422 // the back link, in the title slot so the pane's input handling 1423 // applies to it unchanged. 1424 var back_label_buf: ["← back to ".len + thread_name.len]u8 = undefined; 1425 const back_label = try std.fmt.bufPrint(&back_label_buf, "← back to {s}", .{thread_name}); 1426 var tb = try wgt.TextBox(Widget).init(allocator, back_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1427 errdefer tb.deinit(allocator); 1428 tb.getFocus().focusable = true; 1429 tb.getFocus().kind = .{ .custom = try listLink(self.session.page_arena, self.data.identity, entryStatus(entry), "", entry.id) }; 1430 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1431 self.title_id[index] = tb.getFocus().id; 1432 } else { 1433 // the thread's title as a focusable word-wrapped text box. 1434 self.title_id[index] = blk: { 1435 var tb = try wgt.TextBox(Widget).init(allocator, entry.record.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1436 errdefer tb.deinit(allocator); 1437 tb.getFocus().focusable = true; 1438 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1439 break :blk tb.getFocus().id; 1440 }; 1441 1442 self.author_id[index] = blk: { 1443 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1444 errdefer tb.deinit(allocator); 1445 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1446 break :blk tb.getFocus().id; 1447 }; 1448 1449 // the thread's tags, each linking to this status's list filtered to 1450 // that tag. 1451 { 1452 var items: std.ArrayList(TagFlow.Item) = .empty; 1453 defer items.deinit(allocator); 1454 var tag_iter = Event.tagIterator(entry.record.event.tags); 1455 while (tag_iter.next()) |tag| { 1456 if (tag.len == 0) continue; 1457 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entryStatus(entry), tag) }); 1458 } 1459 if (items.items.len > 0) { 1460 var tf = try TagFlow.init(allocator); 1461 errdefer tf.deinit(allocator); 1462 try tf.setItems(allocator, items.items); 1463 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1464 } 1465 } 1466 } 1467 1468 // the description as a focusable word-wrapped text box. past the limit 1469 // it's cut at the last whole line and links to the /description page, 1470 // which shows the whole thing. 1471 self.description_id[index] = blk: { 1472 const whole = entry.record.event.description; 1473 const cut_short = !description_page and whole.len > Data.max_description_size; 1474 const shown = if (cut_short) 1475 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..Data.max_description_size], '\n') orelse 0], " \t\r\n") 1476 else if (whole.len == 0) 1477 "(no description)" 1478 else 1479 whole; 1480 var tb = try wgt.TextBox(Widget).init(allocator, shown, .{ 1481 .border_style = .single, 1482 .rounded_corners = true, 1483 .wrap_kind = .word, 1484 .label = " description ", 1485 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1486 }); 1487 errdefer tb.deinit(allocator); 1488 tb.getFocus().focusable = true; 1489 if (cut_short) { 1490 const route = ui.RoutablePage.repoThreadDescriptionRoute(kind, self.data.identity, entry.id) orelse return error.RouteTooLong; 1491 tb.getFocus().kind = .{ .custom = try std.fmt.allocPrint(self.session.page_arena.allocator(), "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1492 } 1493 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1494 break :blk tb.getFocus().id; 1495 }; 1496 1497 if (supports_drafts and entryDraft(entry)) { 1498 inner.getFocus().child_id = inner.children.keys()[0]; 1499 const sc = self.detailScroll(index); 1500 sc.x = 0; 1501 sc.y = 0; 1502 sc.getFocus().version +%= 1; 1503 return; 1504 } 1505 1506 const parent_route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, entry.id, 0) orelse return error.RouteTooLong; 1507 try Attachment.appendRows(allocator, inner, self.session, self.data.identity, try parent_route.toUrl(self.session.page_arena), entry.attachments); 1508 1509 if (!description_page) { 1510 var reply = try Comment.linkBox(allocator, self.session, "new comment", ui.RoutablePage.repoThreadCommentNewRoute(kind, self.data.identity, entry.id, "") orelse return error.RouteTooLong); 1511 errdefer reply.deinit(allocator); 1512 try inner.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = null }); 1513 1514 try Comment.appendCount(allocator, inner, entry.comments.count, "comment", "comments"); 1515 for (entry.comments.comments) |comment| try Comment.appendComment(allocator, inner, self.session, self.data.identity, kind, comment); 1516 try Comment.appendWindowNav(allocator, inner, self.session, self.data.identity, kind, entry.id, null, entry.comments); 1517 1518 var spacer = try Spacer.init(allocator); 1519 errdefer spacer.deinit(allocator); 1520 try inner.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1521 } 1522 1523 // select the title by default 1524 if (self.title_id[index]) |id| inner.getFocus().child_id = id; 1525 1526 // reset the scroll to the top for the newly-shown thread: directly on the 1527 // terminal (the wasm offset), and via a version bump on the web (so the 1528 // renderer's scroll id changes and JS drops the preserved position). 1529 const sc = self.detailScroll(index); 1530 sc.x = 0; 1531 sc.y = 0; 1532 sc.getFocus().version +%= 1; 1533 } 1534 1535 pub fn input(self: *This, allocator: std.mem.Allocator, raw_key: Key, root_focus: *Focus) !void { 1536 // wheel input moves focus like the arrows 1537 const key: Key = if (raw_key == .mouse and raw_key.mouse.action == .scroll) 1538 (if (raw_key.mouse.action.scroll == .up) .arrow_up else .arrow_down) 1539 else 1540 raw_key; 1541 1542 if (self.headerActive()) { 1543 // down from the tabs re-enters the stack if the selected view has 1544 // something to focus; other keys move the tabs. 1545 if (inp.vertDirection(key) == .down) { 1546 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1547 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1548 // nothing focusable in the unauthorized view 1549 .unauthorized => false, 1550 else => true, 1551 } else false; 1552 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1553 } else { 1554 try self.header().input(allocator, key, root_focus); 1555 } 1556 return; 1557 } 1558 if (self.tagsViewActive()) { 1559 self.tagsViewInput(key, root_focus); 1560 return; 1561 } 1562 if (self.formViewActive()) { 1563 if (self.data.view == .remove) { 1564 try self.removeFormInput(allocator, key, root_focus); 1565 } else if (supports_conflicts and self.data.view == .resolve) { 1566 try self.resolveInput(allocator, key, root_focus); 1567 } else if (self.data.view == .new_comment or self.data.view == .edit_comment) { 1568 try self.commentFormInput(allocator, key, root_focus); 1569 } else { 1570 try self.formInput(allocator, key, root_focus); 1571 } 1572 return; 1573 } 1574 const i = self.selectedSplitIndex() orelse return; 1575 if (self.detailActive(i)) { 1576 try self.detailInput(allocator, i, key, root_focus); 1577 } else { 1578 self.listInput(i, key, root_focus); 1579 } 1580 } 1581 1582 // arrow keys move the tag selection; up from the top row crosses to the 1583 // header tabs. 1584 fn tagsViewInput(self: *This, key: Key, root_focus: *Focus) void { 1585 const tf = self.tagsView(); 1586 const cid = tf.focus.child_id orelse return; 1587 const cur = tf.indexOfFocusId(cid) orelse return; 1588 const count = tf.text_boxes.items.len; 1589 switch (key) { 1590 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1591 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1592 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1593 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1594 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1595 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1596 else => {}, 1597 } 1598 } 1599 1600 fn listInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1601 // up/down (and the scroll wheel) move the selection a row; page up/down 1602 // jump a fixed amount. right/Enter cross into the detail pane. up from 1603 // the top row crosses into the header tabs. 1604 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1605 const lb = self.listBox(index); 1606 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1607 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1608 moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1609 return; 1610 } 1611 switch (key) { 1612 .enter, .arrow_right => self.focusDetail(index, root_focus), 1613 else => {}, 1614 } 1615 } 1616 1617 fn detailInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1618 switch (key) { 1619 .page_up => { 1620 self.pageDetail(index, root_focus, -10); 1621 return; 1622 }, 1623 .page_down => { 1624 self.pageDetail(index, root_focus, 10); 1625 return; 1626 }, 1627 .home => { 1628 self.jumpDetail(index, root_focus, false); 1629 return; 1630 }, 1631 .end => { 1632 self.jumpDetail(index, root_focus, true); 1633 return; 1634 }, 1635 else => {}, 1636 } 1637 if (self.data.comment_page != null and std.mem.eql(u8, self.window(index).items[self.detailed_index[index] orelse return].id, self.data.selected_id)) { 1638 self.commentInput(index, key, root_focus); 1639 return; 1640 } 1641 if (self.toolRowFocused(index)) { 1642 try self.toolRowInput(allocator, index, key, root_focus); 1643 return; 1644 } 1645 1646 const child_index = self.focusedDetailChild(index, root_focus) orelse return; 1647 const inner = self.detailInner(index); 1648 const child_id = inner.children.keys()[child_index]; 1649 const child = &inner.children.values()[child_index].widget; 1650 1651 if (child_id == self.title_id[index]) { 1652 switch (key) { 1653 .arrow_left => self.focusList(index, root_focus), 1654 .arrow_up => if (!self.moveDetailVertical(index, root_focus, false)) self.focusToolRow(index, root_focus), 1655 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1656 else => {}, 1657 } 1658 return; 1659 } 1660 if (child_id == self.author_id[index]) { 1661 // the author's a: link is followed by the host; arrows cross to 1662 // neighboring widgets. 1663 switch (key) { 1664 .arrow_left => self.focusList(index, root_focus), 1665 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1666 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1667 else => {}, 1668 } 1669 return; 1670 } 1671 if (child_id == self.description_id[index]) { 1672 self.descriptionInput(index, key, root_focus); 1673 return; 1674 } 1675 1676 switch (child.*) { 1677 .tag_flow => |*tf| { 1678 self.tagsInput(index, child_index, tf, key, root_focus); 1679 return; 1680 }, 1681 .copyable_text => |*copyable| { 1682 switch (key) { 1683 .arrow_up => if (!self.moveDetailVertical(index, root_focus, false)) self.focusToolRow(index, root_focus), 1684 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1685 .arrow_left => if (copyable.selected == 0) 1686 self.focusList(index, root_focus) 1687 else 1688 try copyable.input(allocator, key, root_focus), 1689 else => try copyable.input(allocator, key, root_focus), 1690 } 1691 return; 1692 }, 1693 .box => |*row| if (Attachment.removeId(row)) |attachment_id| { 1694 const remove_id = row.children.keys()[1]; 1695 switch (key) { 1696 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) self.focusList(index, root_focus), 1697 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1698 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1699 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1700 .enter => if (row.getFocus().child_id == remove_id) try self.removeEvent(allocator, .attach, attachment_id), 1701 .mouse => |mouse| if (inp.leftClickOn(root_focus, remove_id, mouse)) try self.removeEvent(allocator, .attach, attachment_id), 1702 else => {}, 1703 } 1704 return; 1705 }, 1706 else => {}, 1707 } 1708 self.commentInput(index, key, root_focus); 1709 } 1710 1711 // enter or a click runs the primary action; links are handled by the host 1712 fn toolRowInput(self: *This, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1713 const row = self.toolRow(index); 1714 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1715 const child_id = row.children.keys()[cur]; 1716 const on_primary = switch (row.children.values()[cur].widget.getFocus().kind) { 1717 .custom => |action| std.mem.eql(u8, action, "submit"), 1718 else => false, 1719 }; 1720 switch (key) { 1721 .arrow_left => if (cur > first_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else self.focusList(index, root_focus), 1722 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1723 .arrow_up => self.focusHeader(root_focus), 1724 .arrow_down => { 1725 self.focusDetailEdge(index, root_focus, false); 1726 self.detailScroll(index).y = 0; 1727 }, 1728 .enter => if (on_primary) try self.primaryAction(allocator, index), 1729 .mouse => |mouse| if (on_primary and inp.leftClickOn(root_focus, child_id, mouse)) try self.primaryAction(allocator, index), 1730 else => {}, 1731 } 1732 } 1733 1734 fn primaryAction(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1735 const selected = self.detailed_index[index] orelse return; 1736 const entry = self.window(index).items[selected]; 1737 if (supports_drafts and entryDraft(entry)) 1738 try self.postDraft(allocator, index) 1739 else 1740 try self.toggleStatus(allocator, index); 1741 } 1742 1743 fn postDraft(self: *This, allocator: std.mem.Allocator, index: usize) !void { 1744 if (!supports_drafts or comptime wasm) return; 1745 const selected = self.detailed_index[index] orelse return; 1746 const entry = self.window(index).items[selected]; 1747 if (!entryDraft(entry) or !entry.revision_ready) return; 1748 try self.data.postDraft(self.session, allocator, entry.id); 1749 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 1750 try self.session.navigate(route); 1751 } 1752 1753 fn descriptionInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1754 const sc = self.detailScroll(index); 1755 switch (key) { 1756 .arrow_left => return self.focusList(index, root_focus), 1757 // the terminal moves through the description one line at a time; 1758 // the web lets focus changes drive its native scroll. 1759 .arrow_up => { 1760 if (!self.session.is_terminal) { 1761 _ = self.moveDetailVertical(index, root_focus, false); 1762 return; 1763 } 1764 if (self.focusAdjacentVisible(index, root_focus, false)) return; 1765 const before = sc.y; 1766 sc.y -= 1; 1767 sc.clampToContent(); 1768 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, false); 1769 return; 1770 }, 1771 .arrow_down => { 1772 if (!self.session.is_terminal) { 1773 _ = self.moveDetailVertical(index, root_focus, true); 1774 return; 1775 } 1776 if (self.focusAdjacentVisible(index, root_focus, true)) return; 1777 const before = sc.y; 1778 sc.y += 1; 1779 sc.clampToContent(); 1780 if (sc.y == before) _ = self.moveDetailVertical(index, root_focus, true); 1781 return; 1782 }, 1783 else => return, 1784 } 1785 } 1786 1787 fn commentInput(self: *This, index: usize, key: Key, root_focus: *Focus) void { 1788 switch (key) { 1789 .arrow_left => if (!self.moveCommentHorizontal(index, root_focus, false)) return self.focusList(index, root_focus), 1790 .arrow_right => _ = self.moveCommentHorizontal(index, root_focus, true), 1791 .arrow_up => _ = self.moveDetailVertical(index, root_focus, false), 1792 .arrow_down => _ = self.moveDetailVertical(index, root_focus, true), 1793 else => return, 1794 } 1795 } 1796 1797 // page the detail scroll, then focus the first or last visible widget. 1798 fn pageDetail(self: *This, index: usize, root_focus: *Focus, delta: isize) void { 1799 if (!self.session.is_terminal) return; 1800 const sc = self.detailScroll(index); 1801 sc.y += delta; 1802 sc.clampToContent(); 1803 self.focusVisible(index, root_focus, delta > 0); 1804 } 1805 1806 // jump to the detail's first or last focusable widget. the terminal pins 1807 // its synthetic scroll too; the browser scrolls to the new focus itself. 1808 fn jumpDetail(self: *This, index: usize, root_focus: *Focus, to_end: bool) void { 1809 if (self.session.is_terminal) { 1810 const sc = self.detailScroll(index); 1811 sc.y = if (to_end) std.math.maxInt(isize) else 0; 1812 sc.clampToContent(); 1813 self.focusVisible(index, root_focus, to_end); 1814 } else { 1815 self.focusDetailEdge(index, root_focus, to_end); 1816 } 1817 } 1818 1819 fn focusDetailEdge(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1820 const focus = self.detailInner(index).getFocus(); 1821 var chosen: ?usize = null; 1822 for (focus.children.keys(), focus.children.values()) |id, child| { 1823 if (!child.focus.focusable) continue; 1824 chosen = id; 1825 if (!last) break; 1826 } 1827 if (chosen) |id| root_focus.setFocus(id); 1828 } 1829 1830 fn detailRectVisible(self: *This, index: usize, rect: layout.IRect) bool { 1831 const sc = self.detailScroll(index); 1832 const viewport = sc.grid orelse return false; 1833 const top = sc.y; 1834 const bottom = top + @as(isize, @intCast(viewport.size.height - sc.bar_h)); 1835 return rect.y + @as(isize, @intCast(rect.size.height)) > top and rect.y < bottom; 1836 } 1837 1838 // focus the top- or bottom-most focusable widget intersecting the viewport. 1839 fn focusVisible(self: *This, index: usize, root_focus: *Focus, last: bool) void { 1840 const inner = self.detailInner(index); 1841 var offset: usize = 0; 1842 while (offset < inner.children.count()) : (offset += 1) { 1843 const child_index = if (last) inner.children.count() - 1 - offset else offset; 1844 if (self.focusVisibleChild(index, child_index, root_focus, last)) return; 1845 } 1846 } 1847 1848 fn focusVisibleChild(self: *This, index: usize, child_index: usize, root_focus: *Focus, last: bool) bool { 1849 const child = &self.detailInner(index).children.values()[child_index]; 1850 const child_rect = child.rect orelse return false; 1851 var chosen: ?usize = null; 1852 if (child.widget.getFocus().focusable and self.detailRectVisible(index, child_rect)) { 1853 const id = self.detailInner(index).children.keys()[child_index]; 1854 chosen = id; 1855 if (!last) { 1856 root_focus.setFocus(id); 1857 return true; 1858 } 1859 } 1860 1861 for (child.widget.getFocus().children.keys(), child.widget.getFocus().children.values()) |id, focus_child| { 1862 if (!focus_child.focus.focusable) continue; 1863 const rect: layout.IRect = .{ 1864 .x = child_rect.x + @as(isize, @intCast(focus_child.rect.x)), 1865 .y = child_rect.y + @as(isize, @intCast(focus_child.rect.y)), 1866 .size = focus_child.rect.size, 1867 }; 1868 if (!self.detailRectVisible(index, rect)) continue; 1869 chosen = id; 1870 if (!last) break; 1871 } 1872 if (chosen) |id| { 1873 root_focus.setFocus(id); 1874 return true; 1875 } 1876 return false; 1877 } 1878 1879 fn focusAdjacentVisible(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1880 const inner = self.detailInner(index); 1881 var next = self.focusedDetailChild(index, root_focus) orelse return false; 1882 while (true) { 1883 if (down) { 1884 next += 1; 1885 if (next >= inner.children.count()) return false; 1886 } else { 1887 if (next == 0) return false; 1888 next -= 1; 1889 } 1890 if (self.focusVisibleChild(index, next, root_focus, !down)) { 1891 self.focusDetailChild(index, next, !down, root_focus); 1892 return true; 1893 } 1894 } 1895 } 1896 1897 fn focusedDetailChild(self: *This, index: usize, root_focus: *Focus) ?usize { 1898 const inner = self.detailInner(index); 1899 var id = root_focus.grandchild_id orelse return null; 1900 while (inner.getFocus().children.get(id)) |child| { 1901 if (child.parent_id == inner.getFocus().id) return inner.children.getIndex(id); 1902 id = child.parent_id; 1903 } 1904 return null; 1905 } 1906 1907 fn detailChildFocusable(self: *This, index: usize, child_index: usize) bool { 1908 const child = &self.detailInner(index).children.values()[child_index].widget; 1909 const focus = child.getFocus(); 1910 if (focus.focusable) return true; 1911 for (focus.children.values()) |item| { 1912 if (item.focus.focusable) return true; 1913 } 1914 return false; 1915 } 1916 1917 fn focusDetailChild(self: *This, index: usize, child_index: usize, last: bool, root_focus: *Focus) void { 1918 const inner = self.detailInner(index); 1919 const child = &inner.children.values()[child_index]; 1920 var target = inner.children.keys()[child_index]; 1921 var rect = child.rect orelse return; 1922 switch (child.widget) { 1923 .repo_comment => |*comment| { 1924 if (comment.rowRect(last)) |row_rect| { 1925 rect.x += row_rect.x; 1926 rect.y += row_rect.y; 1927 rect.size = row_rect.size; 1928 } 1929 if (last) comment.focusBody(root_focus) else comment.focusMetadata(root_focus); 1930 }, 1931 .box => |*box| if (box.children.count() > 0) { 1932 target = box.children.keys()[if (last) box.children.count() - 1 else 0]; 1933 root_focus.setFocus(target); 1934 }, 1935 else => root_focus.setFocus(target), 1936 } 1937 const sc = self.detailScroll(index); 1938 if (sc.grid) |viewport| { 1939 const viewport_height = viewport.size.height - sc.bar_h; 1940 if (rect.size.height > viewport_height) { 1941 const bottom_aligned = rect.y + @as(isize, @intCast(rect.size.height - viewport_height)); 1942 sc.y = std.math.clamp(sc.y, rect.y, bottom_aligned); 1943 sc.clampToContent(); 1944 return; 1945 } 1946 } 1947 sc.scrollToRect(rect); 1948 } 1949 1950 fn moveDetailVertical(self: *This, index: usize, root_focus: *Focus, down: bool) bool { 1951 const inner = self.detailInner(index); 1952 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1953 if (inner.children.values()[current].widget == .repo_comment) { 1954 const comment = &inner.children.values()[current].widget.repo_comment; 1955 if (down and !comment.bodyFocused()) { 1956 self.focusDetailChild(index, current, true, root_focus); 1957 return true; 1958 } 1959 if (!down and comment.bodyFocused()) { 1960 self.focusDetailChild(index, current, false, root_focus); 1961 return true; 1962 } 1963 } 1964 1965 var next = current; 1966 while (true) { 1967 if (down) { 1968 next += 1; 1969 if (next >= inner.children.count()) return false; 1970 } else { 1971 if (next == 0) return false; 1972 next -= 1; 1973 } 1974 if (!self.detailChildFocusable(index, next)) continue; 1975 self.focusDetailChild(index, next, !down, root_focus); 1976 return true; 1977 } 1978 } 1979 1980 fn moveCommentHorizontal(self: *This, index: usize, root_focus: *Focus, right: bool) bool { 1981 const inner = self.detailInner(index); 1982 const current = self.focusedDetailChild(index, root_focus) orelse return false; 1983 const child = &inner.children.values()[current]; 1984 if (child.widget == .repo_comment) { 1985 const moved = child.widget.repo_comment.moveHorizontal(root_focus, right); 1986 if (moved) if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1987 return moved; 1988 } 1989 const row = switch (child.widget) { 1990 .box => |*box| box, 1991 else => return false, 1992 }; 1993 const selected = row.getFocus().child_id orelse return false; 1994 const selected_index = row.children.getIndex(selected) orelse return false; 1995 const target = if (right) selected_index + 1 else selected_index -| 1; 1996 if (target == selected_index or target >= row.children.count()) return false; 1997 root_focus.setFocus(row.children.keys()[target]); 1998 if (child.rect) |rect| self.detailScroll(index).scrollToRect(rect); 1999 return true; 2000 } 2001 2002 // up/down (and tab/shift+tab) move between a form's fields; up from the 2003 // title crosses into the header tabs. the multiline description keeps 2004 // enter and any up/down that has a row to move to. 2005 fn formInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2006 const form = self.threadForm() orelse return; 2007 const cid = form.getFocus().child_id orelse return; 2008 const cur = form.children.getIndex(cid) orelse return; 2009 2010 if (cur == description_field_index) { 2011 const child = &form.children.values()[cur]; 2012 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2013 return child.widget.input(allocator, key, root_focus); 2014 } 2015 2016 switch (key) { 2017 .arrow_up, .back_tab => if (cur > 0) 2018 root_focus.setFocus(form.children.keys()[cur - 1]) 2019 else 2020 self.focusHeader(root_focus), 2021 .arrow_down, .tab => if (cur < submit_field_index) { 2022 root_focus.setFocus(form.children.keys()[cur + 1]); 2023 }, 2024 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 2025 .mouse => |mouse| if (cur == submit_field_index) { 2026 const submit = &form.children.values()[cur].widget.submit_button; 2027 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitForm(allocator); 2028 }, 2029 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 2030 } 2031 } 2032 2033 fn commentFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2034 const form = self.threadForm() orelse return; 2035 const cid = form.getFocus().child_id orelse return; 2036 const cur = form.children.getIndex(cid) orelse return; 2037 const child = &form.children.values()[cur]; 2038 2039 if (cur == comment_body_field_index and try multilineKeepsKey(&child.widget.text_input, allocator, key)) 2040 return child.widget.input(allocator, key, root_focus); 2041 2042 switch (key) { 2043 .arrow_up, .back_tab => if (cur > comment_author_field_index) 2044 root_focus.setFocus(form.children.keys()[cur - 1]) 2045 else 2046 self.focusHeader(root_focus), 2047 .arrow_down, .tab => if (cur < comment_submit_field_index) 2048 root_focus.setFocus(form.children.keys()[cur + 1]), 2049 .enter => if (cur == comment_submit_field_index) try self.submitComment(allocator), 2050 .mouse => |mouse| if (cur == comment_submit_field_index) { 2051 const submit = &child.widget.submit_button; 2052 if (inp.leftClickOn(root_focus, submit.buttonId(), mouse)) try self.submitComment(allocator); 2053 }, 2054 else => try child.widget.input(allocator, key, root_focus), 2055 } 2056 } 2057 2058 fn removeFormInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2059 const form = self.removeForm() orelse return; 2060 const button_id = form.children.keys()[1]; 2061 switch (key) { 2062 .arrow_up, .back_tab => self.focusHeader(root_focus), 2063 .enter => try self.submitRemove(allocator), 2064 .mouse => |mouse| if (inp.leftClickOn(root_focus, button_id, mouse)) try self.submitRemove(allocator), 2065 else => {}, 2066 } 2067 } 2068 2069 // the multiline hunk inputs keep enter and any up/down that has a row to 2070 // move to; enter on a "use this" link is a navigation the host follows 2071 // before this runs. 2072 fn resolveInput(self: *This, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2073 const form = self.threadForm() orelse return; 2074 const cid = form.getFocus().child_id orelse return; 2075 const cur = form.children.getIndex(cid) orelse return; 2076 const child = &form.children.values()[cur]; 2077 2078 if (child.widget == .text_input) { 2079 const ti = &child.widget.text_input; 2080 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 2081 return child.widget.input(allocator, key, root_focus); 2082 } 2083 2084 const on_submit = child.widget == .submit_button; 2085 2086 switch (key) { 2087 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 2088 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 2089 .arrow_left, .arrow_right => switch (child.widget) { 2090 .box => |*row| { 2091 const row_cid = row.getFocus().child_id orelse return; 2092 const row_index = row.children.getIndex(row_cid) orelse return; 2093 if (key == .arrow_left) { 2094 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 2095 } else if (row_index + 1 < row.children.count()) { 2096 root_focus.setFocus(row.children.keys()[row_index + 1]); 2097 } 2098 }, 2099 else => try child.widget.input(allocator, key, root_focus), 2100 }, 2101 .enter => if (on_submit) try self.submitResolution(allocator), 2102 .mouse => |mouse| if (on_submit) { 2103 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) try self.submitResolution(allocator); 2104 }, 2105 else => try child.widget.input(allocator, key, root_focus), 2106 } 2107 } 2108 2109 // a multiline input keeps enter and any up/down that has a row to move to 2110 fn multilineKeepsKey(text_input: *wgt.TextInput(Widget), allocator: std.mem.Allocator, key: Key) !bool { 2111 return switch (key) { 2112 .enter => true, 2113 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 2114 .arrow_down => !try text_input.cursorOnLastRow(allocator), 2115 else => false, 2116 }; 2117 } 2118 2119 // the neighboring focusable resolve-form child, skipping the spacer and 2120 // the blank gap rows 2121 fn resolveStep(form: *wgt.Box(Widget), cur: usize, down: bool) ?usize { 2122 var i = cur; 2123 while (true) { 2124 if (down) { 2125 i += 1; 2126 if (i >= form.children.count()) return null; 2127 } else { 2128 if (i == 0) return null; 2129 i -= 1; 2130 } 2131 switch (form.children.values()[i].widget) { 2132 .spacer, .text => continue, 2133 else => return i, 2134 } 2135 } 2136 } 2137 2138 // focus the resolve-form child at `i` (a version row focuses its selected 2139 // child) and keep it visible on the terminal 2140 fn focusResolveChild(self: *This, form: *wgt.Box(Widget), i: usize, root_focus: *Focus) void { 2141 const child = &form.children.values()[i]; 2142 switch (child.widget) { 2143 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 2144 else => root_focus.setFocus(form.children.keys()[i]), 2145 } 2146 if (self.session.is_terminal) { 2147 if (self.resolveScroll()) |sc| { 2148 if (child.rect) |rect| sc.scrollToRect(rect); 2149 } 2150 } 2151 } 2152 2153 // re-emit the conflicted thread's event with the resolved content; any 2154 // event on the thread settles its conflict entry. this is the terminal 2155 // path; the web posts the form to the resolve route. 2156 fn submitResolution(self: *This, allocator: std.mem.Allocator) !void { 2157 if (comptime wasm) return; 2158 const io = self.session.io orelse return; 2159 const src = self.data.repo_source orelse return; 2160 const entry = self.data.selectedThread() orelse return; 2161 const author = (try self.session.eventAuthor()) orelse return; 2162 const form = self.threadForm() orelse return; 2163 2164 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 2165 var title: ?[]u8 = null; 2166 var tags: ?[]u8 = null; 2167 var hunks: std.ArrayList([]const u8) = .empty; 2168 defer { 2169 if (title) |t| allocator.free(t); 2170 if (tags) |t| allocator.free(t); 2171 for (hunks.items) |h| allocator.free(h); 2172 hunks.deinit(allocator); 2173 } 2174 for (form.children.values()) |*child| switch (child.widget) { 2175 .text_input => |*ti| { 2176 const text = try ti.text(allocator); 2177 errdefer allocator.free(text); 2178 if (std.mem.eql(u8, ti.options.name, "title")) { 2179 title = text; 2180 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 2181 tags = text; 2182 } else { 2183 try hunks.append(allocator, text); 2184 } 2185 }, 2186 else => {}, 2187 }; 2188 2189 const id_bytes = try evt.parseEventId(entry.id); 2190 2191 switch (src.repo_kind) { 2192 inline else => |repo_kind| { 2193 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2194 defer any_repo.deinit(io, allocator); 2195 switch (any_repo) { 2196 inline else => |*repo| { 2197 var resolution: Event.Resolve = .{ 2198 .title = title, 2199 .tags = tags, 2200 .hunks = hunks.items, 2201 }; 2202 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2203 const change = Event.Update{ .resolve = resolution }; 2204 Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2205 // leave the form up for correction 2206 error.InvalidFields => return, 2207 else => |e| return e, 2208 }; 2209 }, 2210 } 2211 }, 2212 } 2213 2214 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2215 try self.session.navigate(route); 2216 } 2217 2218 // commit a new or edited comment and navigate to its permalink. this is the 2219 // terminal path; the web posts the form to its comment route. 2220 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2221 if (comptime wasm) return; 2222 const io = self.session.io orelse return; 2223 const src = self.data.repo_source orelse return; 2224 const author = (try self.session.eventAuthor()) orelse return; 2225 const form = self.threadForm() orelse return; 2226 const body_input = &form.children.values()[comment_body_field_index].widget.text_input; 2227 const body = try body_input.text(allocator); 2228 defer allocator.free(body); 2229 if (!evt.Comment.fieldsValid(body)) return; 2230 2231 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2232 @memcpy(&thread_id, self.data.selected_id); 2233 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2234 var parent: [evt.event_id_size * 2]u8 = undefined; 2235 @memcpy(&parent, self.data.comment_id); 2236 break :blk parent; 2237 }; 2238 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2239 const editing = self.data.view == .edit_comment; 2240 switch (src.repo_kind) { 2241 inline else => |repo_kind| { 2242 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2243 defer any_repo.deinit(io, allocator); 2244 switch (any_repo) { 2245 inline else => |*repo| if (editing) { 2246 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2247 try evt.Comment.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2248 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2249 } else { 2250 event_id_hex = try evt.Comment.create(repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2251 }, 2252 } 2253 }, 2254 } 2255 2256 body_input.clear(allocator); 2257 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2258 try self.session.navigate(route); 2259 } 2260 2261 // the terminal submit paths: the new form commits a new thread event, the 2262 // edit form re-emits the selected thread's event with the form's content. 2263 // the web posts the forms to their routes instead. 2264 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2265 if (self.data.view == .edit) { 2266 try self.submitEditedThread(allocator); 2267 } else { 2268 try self.submitNewThread(allocator); 2269 } 2270 } 2271 2272 // commit the new thread to the repo's events branch and navigate to it. 2273 // this is the terminal path; the web posts the form to the thread route, 2274 // so the wasm side never runs (or compiles) the repo access below. 2275 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2276 if (comptime wasm) return; 2277 const io = self.session.io orelse return; 2278 const src = self.data.repo_source orelse return; 2279 const author = (try self.session.eventAuthor()) orelse return; 2280 2281 const form = self.threadForm() orelse return; 2282 const title_input = &form.children.values()[title_field_index].widget.text_input; 2283 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2284 const description_input = &form.children.values()[description_field_index].widget.text_input; 2285 2286 const title = try title_input.text(allocator); 2287 defer allocator.free(title); 2288 const tags = try tags_input.text(allocator); 2289 defer allocator.free(tags); 2290 const description = try description_input.text(allocator); 2291 defer allocator.free(description); 2292 2293 if (!Event.fieldsValid(title, tags)) return; 2294 2295 if (comptime supports_drafts) { 2296 const event_id_hex = try Data.createDraft(self.data, self.session, allocator, title, tags, description); 2297 title_input.clear(allocator); 2298 tags_input.clear(allocator); 2299 description_input.clear(allocator); 2300 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2301 try self.session.navigate(route); 2302 return; 2303 } 2304 2305 var id_bytes: [evt.event_id_size]u8 = undefined; 2306 io.random(&id_bytes); 2307 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2308 2309 const event = evt.EventWithId{ 2310 .id = event_id_hex, 2311 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2312 .author = author, 2313 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2314 .title = title, 2315 .description = description, 2316 .tags = tags, 2317 }), 2318 }; 2319 2320 switch (src.repo_kind) { 2321 inline else => |repo_kind| { 2322 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2323 defer any_repo.deinit(io, allocator); 2324 switch (any_repo) { 2325 inline else => |*repo| try evt.consume(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2326 } 2327 }, 2328 } 2329 2330 // wipe the form so a return visit starts fresh 2331 title_input.clear(allocator); 2332 tags_input.clear(allocator); 2333 description_input.clear(allocator); 2334 2335 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2336 try self.session.navigate(route); 2337 } 2338 2339 // re-emit the selected thread's event with the form's content (status 2340 // preserved), then reload the thread's page. this is the terminal path; 2341 // the web posts the form to the edit route. 2342 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2343 if (comptime wasm) return; 2344 const io = self.session.io orelse return; 2345 const src = self.data.repo_source orelse return; 2346 const entry = self.data.selectedThread() orelse return; 2347 const author = (try self.session.eventAuthor()) orelse return; 2348 2349 const form = self.threadForm() orelse return; 2350 const title_input = &form.children.values()[title_field_index].widget.text_input; 2351 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 2352 const description_input = &form.children.values()[description_field_index].widget.text_input; 2353 2354 const title = try title_input.text(allocator); 2355 defer allocator.free(title); 2356 const tags = try tags_input.text(allocator); 2357 defer allocator.free(tags); 2358 const description = try description_input.text(allocator); 2359 defer allocator.free(description); 2360 2361 if (!Event.fieldsValid(title, tags)) return; 2362 2363 const id_bytes = try evt.parseEventId(entry.id); 2364 2365 switch (src.repo_kind) { 2366 inline else => |repo_kind| { 2367 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2368 defer any_repo.deinit(io, allocator); 2369 switch (any_repo) { 2370 inline else => |*repo| if (has_status) 2371 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2372 .title = title, 2373 .tags = tags, 2374 .description = description, 2375 } }, author) 2376 else 2377 try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, tags, description, author), 2378 } 2379 }, 2380 } 2381 2382 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2383 try self.session.navigate(route); 2384 } 2385 2386 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2387 const comment = self.data.comment_id.len != 0; 2388 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2389 const id = evt.parseEventId(event_id) catch return; 2390 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2391 } 2392 2393 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2394 if (comptime wasm) return; 2395 const io = self.session.io orelse return; 2396 const src = self.data.repo_source orelse return; 2397 const author = (try self.session.eventAuthor()) orelse return; 2398 2399 switch (src.repo_kind) { 2400 inline else => |repo_kind| { 2401 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2402 defer any_repo.deinit(io, allocator); 2403 switch (any_repo) { 2404 inline else => |*repo| try evt.remove(.repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2405 } 2406 }, 2407 } 2408 2409 const id_hex = std.fmt.bytesToHex(id, .lower); 2410 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex) orelse return; 2411 try self.session.navigate(route); 2412 } 2413 2414 // flip the shown thread's status by re-emitting its event, then reload the 2415 // page rooted at the thread so the view reflects the change. this is the 2416 // terminal path; the web posts the button's form to the status route. 2417 fn toggleStatus(self: *This, allocator: std.mem.Allocator, index: usize) !void { 2418 if (comptime !has_status) return; 2419 if (comptime wasm) return; 2420 const io = self.session.io orelse return; 2421 const src = self.data.repo_source orelse return; 2422 const sel = self.detailed_index[index] orelse return; 2423 const entry = self.window(index).items[sel]; 2424 const author = (try self.session.eventAuthor()) orelse return; 2425 2426 const status = (statusChange(entryStatus(entry)) orelse return).status; 2427 2428 const id_bytes = try evt.parseEventId(entry.id); 2429 2430 switch (src.repo_kind) { 2431 inline else => |repo_kind| { 2432 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2433 defer any_repo.deinit(io, allocator); 2434 switch (any_repo) { 2435 inline else => |*repo| try Event.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author), 2436 } 2437 }, 2438 } 2439 2440 const route = listRoute(self.data.identity, status, "", entry.id) orelse return; 2441 try self.session.navigate(route); 2442 } 2443 2444 // arrow keys move the tag selection; at the flow's edges focus crosses to 2445 // the neighboring widgets. 2446 fn tagsInput(self: *This, index: usize, child_index: usize, tf: *TagFlow, key: Key, root_focus: *Focus) void { 2447 const cid = tf.focus.child_id orelse return; 2448 const cur = tf.indexOfFocusId(cid) orelse return; 2449 const count = tf.text_boxes.items.len; 2450 switch (key) { 2451 .arrow_left => if (cur > 0) self.focusTag(index, child_index, tf, root_focus, cur - 1) else self.focusList(index, root_focus), 2452 .arrow_right => if (cur + 1 < count) self.focusTag(index, child_index, tf, root_focus, cur + 1), 2453 .arrow_up => if (tf.rowStep(cur, false)) |i| { 2454 self.focusTag(index, child_index, tf, root_focus, i); 2455 } else { 2456 _ = self.moveDetailVertical(index, root_focus, false); 2457 }, 2458 .arrow_down => if (tf.rowStep(cur, true)) |i| { 2459 self.focusTag(index, child_index, tf, root_focus, i); 2460 } else { 2461 _ = self.moveDetailVertical(index, root_focus, true); 2462 }, 2463 .home => self.focusTag(index, child_index, tf, root_focus, 0), 2464 .end => self.focusTag(index, child_index, tf, root_focus, count - 1), 2465 else => {}, 2466 } 2467 } 2468 2469 // the tool row's first child after the spacer that pushes it right 2470 const first_in_row_index: usize = 1; 2471 2472 fn toolRowFocused(self: *This, index: usize) bool { 2473 const outer = self.detailOuter(index); 2474 const cid = outer.getFocus().child_id orelse return false; 2475 return outer.children.getIndex(cid) == tool_row_index; 2476 } 2477 2478 fn focusTag(self: *This, index: usize, child_index: usize, tf: *TagFlow, root_focus: *Focus, item: usize) void { 2479 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2480 // keep the tag visible on the terminal: its rect offset by the flow's 2481 // position in the pane. 2482 if (self.session.is_terminal and item < tf.rects.items.len) { 2483 if (self.detailInner(index).children.values()[child_index].rect) |flow_rect| { 2484 var rect = tf.rects.items[item]; 2485 rect.x += flow_rect.x; 2486 rect.y += flow_rect.y; 2487 self.detailScroll(index).scrollToRect(rect); 2488 } 2489 } 2490 } 2491 2492 // return to the tool row's last-focused button (the header when the 2493 // description page shows no row). 2494 fn focusToolRow(self: *This, index: usize, root_focus: *Focus) void { 2495 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2496 root_focus.setFocus(cid); 2497 } 2498 2499 // enter the detail pane. an empty pane can't be entered. 2500 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 2501 if (self.detailInner(index).children.count() == 0) return; 2502 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2503 } 2504 2505 // return to the list. 2506 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2507 root_focus.setFocus(self.listScroll(index).getFocus().id); 2508 } 2509 2510 // cross to the header tabs above the stack. 2511 fn focusHeader(self: *This, root_focus: *Focus) void { 2512 root_focus.setFocus(self.box.children.keys()[header_index]); 2513 } 2514 2515 pub fn clearGrid(self: *This) void { 2516 self.box.clearGrid(); 2517 } 2518 2519 pub fn getGrid(self: This) ?Grid { 2520 return self.box.getGrid(); 2521 } 2522 2523 pub fn getFocus(self: *This) *Focus { 2524 return self.box.getFocus(); 2525 } 2526 2527 // for the parent's "scroll up at the top jumps to the header" check: at the 2528 // top only while the header tabs hold focus, so up from the split 2529 // crosses into the tabs first. 2530 pub fn getSelectedIndex(self: *This) ?usize { 2531 return if (self.headerActive()) 0 else 1; 2532 } 2533 }; 2534 } 2535