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