╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
106 } 107 } 108 109 pub fn getGrid(self: Widget) ?Grid { 110 switch (self) { 111 inline else => |*case| return case.getGrid(), 112 } 113 } 114 115 pub fn getFocus(self: *Widget) *Focus { 116 switch (self.*) { 117 inline else => |*case| return case.getFocus(), 118 } 119 } 120 121 // whether moving up should return to the page header 122 pub fn atTop(self: *Widget, root_focus: *Focus) bool { 123 return switch (self.*) { 124 inline .home_about, .home_users, .user_repos, .flow_box_scroll, .new_repo, .new_user, .user_login, .user_notifications, .quit, .repo_files, .repo_commits, .diff_view, .repo_refs, .repo_issues, .repo_patches, .repo_discussions, .repo_events, .repo_undo, .repo_settings, .repo_settings_form, .repo_members, .repo_branches, .user_keys => |*view| view.atTop(), 125 inline .repo_patch_detail, .user_settings => |*view| view.atTop(root_focus), 126 else => false, 127 }; 128 } 129 }; 130 131 // whether `mouse` is a left click on the selected row of `list`. the host 132 // selects a clicked row before forwarding the press. 133 pub fn clickOnSelectedRow(list: *wgt.Box(Widget), root_focus: *Focus, mouse: xitui.input.Mouse) bool { 134 const cid = list.getFocus().child_id orelse return false; 135 return inp.leftClickOn(root_focus, cid, mouse); 136 } 137 138 // a detail pane's way back to a list that isn't shown beside it. it leads the 139 // pane's frame, above the pane's content, hidden while the list shows. 140 const list_back_index = 0; 141 pub const list_pane_index = 1; 142 143 pub fn addListBack(allocator: std.mem.Allocator, frame: *wgt.Box(Widget), label: []const u8) !void { 144 var back = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 145 errdefer back.deinit(allocator); 146 back.getFocus().mode = .all; 147 try frame.children.put(allocator, back.getFocus().id, .{ .widget = .{ .text_box = back }, .rect = null, .min_size = null, .hidden = true }); 148 } 149 150 // point the way back at the shown list row's in-page link, so both hosts hand 151 // its press and enter to the view instead of navigating 152 pub fn setListBackLink(frame: *wgt.Box(Widget), link: []const u8) void { 153 frame.children.values()[list_back_index].widget.text_box.getFocus().kind = .{ .custom = link }; 154 } 155 156 pub fn showListBack(frame: *wgt.Box(Widget), shown: bool) void { 157 frame.children.values()[list_back_index].hidden = !shown; 158 if (!shown and frame.getFocus().child_id == frame.children.keys()[list_back_index]) 159 frame.getFocus().child_id = frame.children.keys()[list_pane_index]; 160 } 161 162 // the way back's focus id while it shows 163 pub fn listBackId(frame: *wgt.Box(Widget)) ?usize { 164 return if (frame.children.values()[list_back_index].hidden) null else frame.children.keys()[list_back_index]; 165 } 166 167 // enter the pane on the way back. selecting it (then focusing the frame) also 168 // handles the too-narrow case where the pane isn't laid out yet. 169 pub fn focusListBack(frame: *wgt.Box(Widget), root_focus: *Focus) void { 170 frame.getFocus().child_id = frame.children.keys()[list_back_index]; 171 root_focus.setFocus(frame.getFocus().id); 172 } 173 174 pub const back_button_width = 3; 175 const back_button_height = 3; 176 const back_kind = "back"; 177 178 // draw a text box as the selected one of its group: bordered and inverted, 179 // while the rest stay borderless 180 pub fn markSelected(text_box: *wgt.TextBox, selected: bool) void { 181 text_box.options.border = if (selected) .single else .hidden; 182 text_box.options.invert = selected; 183 } 184 185 // explanatory text atop a view's rows: a frame that takes focus like a row, so 186 // the arrows scroll it into view, then a blank line 187 pub fn initAbout(allocator: std.mem.Allocator, spans: []const RichText.Span) !wgt.Box(Widget) { 188 var row = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 189 errdefer row.deinit(allocator); 190 { 191 var frame = try wgt.Box(Widget).init(allocator, .{ .border = .single, .round_corners = true, .direction = .vert, .top_label = .{ .text = " note " } }); 192 errdefer frame.deinit(allocator); 193 frame.getFocus().mode = .all; 194 { 195 var text = try RichText.init(allocator, spans); 196 errdefer text.deinit(allocator); 197 try frame.children.put(allocator, text.getFocus().id, .{ .widget = .{ .rich_text = text }, .rect = null, .min_size = null }); 198 } 199 row.getFocus().child_id = frame.getFocus().id; 200 try row.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = null }); 201 } 202 { 203 var blank = try wgt.Text.init(allocator, " "); 204 errdefer blank.deinit(allocator); 205 try row.children.put(allocator, blank.getFocus().id, .{ .widget = .{ .text = blank }, .rect = null, .min_size = null }); 206 } 207 return row; 208 } 209 210 // double the about frame's border while it has focus 211 pub fn markAboutFocused(row: *wgt.Box(Widget), root_focus: *const Focus) void { 212 const frame = &row.children.values()[0].widget.box; 213 frame.options.border = if (root_focus.grandchild_id == frame.getFocus().id) .double else .single; 214 } 215 216 pub fn addBackButton(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session) !void { 217 if (!session.is_terminal) return; 218 var back = try wgt.TextBox.init(allocator, "←", .{ .border = .hidden, .wrap_kind = .none }); 219 errdefer back.deinit(allocator); 220 back.getFocus().kind = .{ .custom = back_kind }; 221 back.getFocus().mode = .mouse; 222 try box.children.put(allocator, back.getFocus().id, .{ 223 .widget = .{ .text_box = back }, 224 .rect = null, 225 .min_size = .{ .width = back_button_width, .height = back_button_height }, 226 .max_size = .{ .width = back_button_width, .height = back_button_height }, 227 .hidden = session.back != .available, 228 }); 229 } 230 231 pub fn setBackButtonVisible(box: *wgt.Box(Widget), visible: bool) void { 232 for (box.children.values()) |*child| { 233 if (isBackButton(child.widget.getFocus())) { 234 child.hidden = !visible; 235 continue; 236 } 237 switch (child.widget) { 238 .box => |*nested| setBackButtonVisible(nested, visible), 239 else => {}, 240 } 241 } 242 } 243 244 pub fn isBackButton(focus: *Focus) bool { 245 return switch (focus.kind) { 246 .custom => |kind| std.mem.eql(u8, kind, back_kind), 247 else => false, 248 }; 249 } 250 251 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 252 // `scroll` to keep it visible. in the terminal, a row taller than the view is 253 // scrolled through before focus leaves it. 254 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 255 const keys = box.children.keys(); 256 if (keys.len == 0) return; 257 const cur_id = box.getFocus().child_id orelse return; 258 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 259 // home and end jump straight to the ends 260 if (@abs(delta) < keys.len) if (box.children.values()[@intCast(cur)].rect) |rect| if (scrollThroughRow(scroll, rect, delta)) return; 261 const last: isize = @intCast(keys.len - 1); 262 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 263 if (next == @as(usize, @intCast(cur))) return; 264 root_focus.setFocus(keys[next]); 265 if (box.children.values()[next].rect) |rect| scrollToEnteredRow(scroll, rect, delta > 0); 266 } 267 268 // bring `rect`, a row in `scroll`'s content just entered from above (`down`) 269 // or below, into view. a row taller than the view keeps the view while the 270 // edge it was entered from is in it, so the following steps scroll through it, 271 // and otherwise shows that edge. 272 pub fn scrollToEnteredRow(scroll: *wgt.Scroll(Widget), rect: layout.IRect, down: bool) void { 273 const grid = scroll.grid orelse return; 274 // a row wider than the view keeps the horizontal position 275 const target: layout.IRect = if (rect.size.width > grid.size.width - scroll.bar_w) .{ .x = scroll.x, .y = rect.y, .size = .{ .width = 0, .height = rect.size.height } } else rect; 276 const h: isize = @intCast(grid.size.height - scroll.bar_h); 277 if (@as(isize, @intCast(rect.size.height)) <= h) return scroll.scrollToRect(target); 278 const edge = if (down) rect.y else rectBottom(rect) - 1; 279 if (edge >= scroll.y and edge < scroll.y + h) return; 280 scroll.y = if (down) rect.y else rectBottom(rect) - h; 281 } 282 283 // step across the boxes of `box`'s selected row, false when it isn't a row or 284 // has no box that way. 285 pub fn moveInSelectedRow(box: *wgt.Box(Widget), root_focus: *Focus, right: bool) bool { 286 const selected = box.getFocus().child_id orelse return false; 287 return switch ((box.children.getPtr(selected) orelse return false).widget) { 288 .box => |*row| moveInRow(row, root_focus, right), 289 else => false, 290 }; 291 } 292 293 // scroll a step through `rect`, a row in `scroll`'s content, when it extends 294 // past the view in `delta`'s direction. false when it doesn't, and on the web, 295 // where scrollFocusedWidget in script.js handles scrolling. 296 pub fn scrollThroughRow(scroll: *wgt.Scroll(Widget), rect: layout.IRect, delta: isize) bool { 297 const step_lines = 3; // how far a step scrolls (see stepLines in script.js for the web equivalent) 298 const h = viewHeight(scroll) orelse return false; 299 const hidden = if (delta > 0) rectBottom(rect) - (scroll.y + h) else scroll.y - rect.y; 300 if (hidden <= 0) return false; 301 scroll.y += std.math.clamp(delta * step_lines, -hidden, hidden); 302 return true; 303 } 304 305 // the terminal view's height, or null on the web 306 fn viewHeight(scroll: *wgt.Scroll(Widget)) ?isize { 307 if (scroll.options.web_native) return null; 308 const grid = scroll.grid orelse return null; 309 return @intCast(grid.size.height - scroll.bar_h); 310 } 311 312 fn rectBottom(rect: layout.IRect) isize { 313 return rect.y + @as(isize, @intCast(rect.size.height)); 314 } 315 316 // focus the box beside the selected one in `row`, false when there is none that way. 317 pub fn moveInRow(row: *wgt.Box(Widget), root_focus: *Focus, right: bool) bool { 318 const selected = row.getFocus().child_id orelse return false; 319 const index = row.children.getIndex(selected) orelse return false; 320 const target = if (right) index + 1 else index -| 1; 321 if (target == index or target >= row.children.count()) return false; 322 root_focus.setFocus(row.children.keys()[target]); 323 return true; 324 } 325 326 // a repo tab's placeholder while the repo has no commits, with the remote to 327 // push to for a viewer who can 328 pub fn initNoCommits(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !Center { 329 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 330 errdefer box.deinit(allocator); 331 332 // built first so the text can center over it 333 var remote_maybe: ?CopyableText = if (session.data.host_kind == .server and handle.canWrite()) if (session.data.git_ssh_port) |port| blk: { 334 // only repo pages flag a repo without commits 335 const identity = switch (handle.location) { 336 .repo => |identity| identity, 337 .fork => unreachable, 338 }; 339 const aa = session.page_arena.allocator(); 340 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 341 break :blk try CopyableText.init(allocator, session, &.{.{ 342 .text = url, 343 .copyable_text = try aa.print("git remote add origin {s}", .{url}), 344 .label = " git remote add origin ", 345 }}, 0); 346 } else null else null; 347 errdefer if (remote_maybe) |*remote| remote.deinit(allocator); 348 349 { 350 var text_widget: Widget = blk: { 351 var text_box = try wgt.TextBox.init(allocator, "no commits yet", .{ .border = null, .wrap_kind = .none }); 352 errdefer text_box.deinit(allocator); 353 break :blk if (remote_maybe != null) .{ .center = try Center.init(allocator, .{ .text_box = text_box }) } else .{ .text_box = text_box }; 354 }; 355 errdefer text_widget.deinit(allocator); 356 // over the remote widget, the text centers across its width 357 const size: ?layout.MaybeSize = if (remote_maybe) |*remote| .{ .width = remote.minWidth(), .height = 1 } else null; 358 try box.children.put(allocator, text_widget.getFocus().id, .{ .widget = text_widget, .rect = null, .min_size = size, .max_size = size }); 359 } 360 361 if (remote_maybe) |*remote| { 362 { 363 var gap = try wgt.Text.init(allocator, " "); 364 errdefer gap.deinit(allocator); 365 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 366 } 367 const min_width = remote.minWidth(); 368 box.getFocus().child_id = remote.getFocus().id; 369 try box.children.put(allocator, remote.getFocus().id, .{ .widget = .{ .copyable_text = remote.* }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 370 // the box owns it now 371 remote_maybe = null; 372 } 373 374 return Center.init(allocator, .{ .box = box }); 375 } 376 377 const empty_label = "nothing to see here"; 378 379 // the message a list or tab shows in place of its empty first window 380 pub fn initEmpty(allocator: std.mem.Allocator) !Center { 381 var text_box = try wgt.TextBox.init(allocator, empty_label, .{ .border = null, .wrap_kind = .none }); 382 errdefer text_box.deinit(allocator); 383 return Center.init(allocator, .{ .text_box = text_box }); 384 } 385 386 pub const ConfirmationButton = struct { 387 label: []const u8, 388 bottom_label: []const u8 = "", 389 action: []const u8 = "submit", 390 }; 391 392 // "are you sure?" over the buttons, in a form posting to `action` 393 pub fn initConfirmationForm(allocator: std.mem.Allocator, action: []const u8, buttons: []const ConfirmationButton) !Center { 394 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 395 errdefer box.deinit(allocator); 396 box.getFocus().kind = .{ .custom = action }; 397 398 var prompt = try wgt.Text.init(allocator, "are you sure?"); 399 errdefer prompt.deinit(allocator); 400 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 401 402 for (buttons, 0..) |options, index| { 403 var button = try wgt.TextBox.init(allocator, options.label, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = options.bottom_label } }); 404 errdefer button.deinit(allocator); 405 button.getFocus().mode = .all; 406 button.getFocus().kind = .{ .custom = options.action }; 407 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 408 if (index == 0) box.getFocus().child_id = button.getFocus().id; 409 } 410 411 return Center.init(allocator, .{ .box = box }); 412 } 413 414 pub const FlowBox = struct { 415 focus: *Focus, 416 grid: ?Grid, 417 text_boxes: std.ArrayList(wgt.TextBox), 418 // backs each link item's `.custom` focus kind (e.g. "a:/user/foo"). reset 419 // wholesale on each setItems so there's no per-string ownership to track. 420 arena: std.heap.ArenaAllocator, 421 // column count from the last build — FlowBox.Scroll.input uses it so arrow 422 // up/down can step by a row's worth of items. 423 last_cols: usize, 424 options: Options, 425 426 const border_rows: usize = 2; 427 428 pub const Options = struct { 429 cell_width: usize = 40, 430 cell_height: usize = 3, 431 }; 432 433 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 434 return .{ 435 .focus = try Focus.create(allocator, .container), 436 .grid = null, 437 .text_boxes = .empty, 438 .arena = std.heap.ArenaAllocator.init(allocator), 439 .last_cols = 1, 440 .options = options, 441 }; 442 } 443 444 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 445 self.focus.destroy(allocator); 446 if (self.grid) |*grid| { 447 grid.deinit(); 448 self.grid = null; 449 } 450 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 451 self.text_boxes.deinit(allocator); 452 self.arena.deinit(); 453 } 454 455 // one flow item: its display text plus an optional link and bottom label. a 456 // non-empty `link` sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, 457 // which the web renderer turns into an anchor. `bold` covers the text and the label. 458 pub const Item = struct { text: []const u8, link: []const u8 = "", bottom_label: []const u8 = "", bold: bool = false }; 459 460 // the text box copies the item text; links and bottom labels are copied into 461 // the arena, so the caller's slices needn't outlive this call. 462 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 463 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 464 self.text_boxes.clearAndFree(allocator); 465 466 // the old text boxes and their borrowed links are gone now, so the link 467 // arena can be reclaimed. 468 _ = self.arena.reset(.retain_capacity); 469 const aa = self.arena.allocator(); 470 471 self.focus.clear(); 472 self.focus.child_id = null; 473 474 for (items) |item| { 475 const style: Grid.Style = .{ .bold = item.bold }; 476 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = try aa.dupe(u8, item.bottom_label), .style = style }, .style = style }); 477 errdefer text_box.deinit(allocator); 478 text_box.getFocus().mode = .all; 479 480 if (item.link.len > 0) { 481 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 482 } 483 484 try self.text_boxes.append(allocator, text_box); 485 } 486 487 if (self.text_boxes.items.len > 0) { 488 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 489 } 490 } 491 492 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 493 self.clearGrid(); 494 self.focus.clear(); 495 496 const cell_width = self.options.cell_width; 497 const max_width = constraint.max_size.width orelse cell_width; 498 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 499 self.last_cols = cols; 500 501 const count = self.text_boxes.items.len; 502 if (count == 0) return; 503 const slot_height = self.options.cell_height + border_rows; 504 if (slot_height == 0) return; 505 506 // build at the slot size so every text box fits a single grid cell 507 for (self.text_boxes.items) |*tb| { 508 tb.options.border = if (root_focus.grandchild_id == tb.getFocus().id) .double else .hidden; 509 try tb.build(allocator, .{ 510 .min_size = .{ .width = cell_width, .height = null }, 511 .max_size = .{ .width = cell_width, .height = slot_height }, 512 }, root_focus); 513 } 514 515 const rows = (count + cols - 1) / cols; 516 const content_height = rows * slot_height; 517 const total_width = cols * cell_width; 518 if (total_width == 0 or content_height == 0) return; 519 520 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 521 errdefer grid.deinit(); 522 523 for (self.text_boxes.items, 0..) |*tb, i| { 524 const tb_grid = tb.getGrid() orelse continue; 525 const col = i % cols; 526 const row = i / cols; 527 const cell_x = col * cell_width; 528 const cell_y = row * slot_height; 529 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 530 try grid.drawGrid(tb_grid, cell_x, cell_y); 531 } 532 533 self.grid = grid; 534 } 535 536 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 537 _ = self; 538 _ = allocator; 539 _ = key; 540 _ = root_focus; 541 } 542 543 pub fn clearGrid(self: *FlowBox) void { 544 if (self.grid) |*grid| { 545 grid.deinit(); 546 self.grid = null; 547 } 548 for (self.text_boxes.items) |*tb| tb.clearGrid(); 549 } 550 551 pub fn getGrid(self: FlowBox) ?Grid { 552 return self.grid; 553 } 554 555 pub fn getFocus(self: *FlowBox) *Focus { 556 return self.focus; 557 } 558 559 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 560 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 561 const slot_height = self.options.cell_height + border_rows; 562 const col = index % self.last_cols; 563 const row = index / self.last_cols; 564 return .{ 565 .x = @intCast(col * self.options.cell_width), 566 .y = @intCast(row * slot_height), 567 .size = .{ .width = self.options.cell_width, .height = slot_height }, 568 }; 569 } 570 571 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 572 for (self.text_boxes.items, 0..) |tb, i| { 573 if (tb.focus.id == focus_id) return i; 574 } 575 return null; 576 } 577 578 pub const Scroll = struct { 579 scroll: wgt.Scroll(Widget), 580 581 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 582 var layout_inner = try FlowBox.init(allocator, options); 583 errdefer layout_inner.deinit(allocator); 584 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 585 errdefer scroll.deinit(allocator); 586 return .{ .scroll = scroll }; 587 } 588 589 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 590 self.scroll.deinit(allocator); 591 } 592 593 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 594 self.scroll.x = 0; 595 self.scroll.y = 0; 596 try self.inner().setItems(allocator, items); 597 } 598 599 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 600 try self.scroll.build(allocator, constraint, root_focus); 601 } 602 603 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 604 _ = allocator; 605 const in = self.inner(); 606 const child_id = in.focus.child_id orelse return; 607 const current_index = in.indexOfFocusId(child_id) orelse return; 608 const count = in.text_boxes.items.len; 609 if (count == 0) return; 610 const cols = in.last_cols; 611 const slot_height = in.options.cell_height + FlowBox.border_rows; 612 613 var index = current_index; 614 switch (key) { 615 .arrow_up => index -|= cols, 616 .arrow_down => if (index + cols < count) { 617 index += cols; 618 }, 619 .arrow_left => index -|= 1, 620 .arrow_right => if (index + 1 < count) { 621 index += 1; 622 }, 623 .home => index = 0, 624 .end => index = count - 1, 625 .page_up => { 626 if (self.scroll.grid) |grid| if (slot_height > 0) { 627 const rows_per_page = grid.size.height / slot_height; 628 index -|= rows_per_page * cols; 629 }; 630 }, 631 .page_down => { 632 if (self.scroll.grid) |grid| if (slot_height > 0) { 633 const rows_per_page = grid.size.height / slot_height; 634 index = @min(index + rows_per_page * cols, count - 1); 635 }; 636 }, 637 // scroll wheel moves the focused cell by a full row so the 638 // viewport (via scrollToRect) follows in row-sized steps, 639 // matching how a scroll wheel feels in a grid view. 640 .scroll_up => index -|= cols, 641 .scroll_down => if (index + cols < count) { 642 index += cols; 643 }, 644 else => {}, 645 } 646 647 if (index != current_index) { 648 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 649 if (in.cellRect(index)) |rect| { 650 self.scroll.scrollToRect(rect); 651 } 652 } 653 } 654 655 pub fn clearGrid(self: *Scroll) void { 656 self.scroll.clearGrid(); 657 } 658 659 pub fn getGrid(self: Scroll) ?Grid { 660 return self.scroll.getGrid(); 661 } 662 663 pub fn getFocus(self: *Scroll) *Focus { 664 return self.scroll.getFocus(); 665 } 666 667 pub fn atTop(self: Scroll) bool { 668 const in = self.scroll.child.flow_box; 669 const child_id = in.focus.child_id orelse return false; 670 return in.indexOfFocusId(child_id) == 0; 671 } 672 673 fn inner(self: *Scroll) *FlowBox { 674 return &self.scroll.child.flow_box; 675 } 676 }; 677 }; 678 679 // a left-to-right flow of variable-width focusable items that wraps at the 680 // available width, like words in a paragraph. unlike FlowBox there is no cell 681 // grid: each item is as wide as its text. selection movement is driven by the 682 // owning view via indexOfFocusId/verticalNeighbor. 683 pub const WordFlow = struct { 684 focus: *Focus, 685 grid: ?Grid, 686 text_boxes: std.ArrayList(wgt.TextBox), 687 // last build's item rects (content space), for vertical navigation and 688 // scroll-into-view. 689 rects: std.ArrayList(layout.IRect), 690 // backs each link item's `.custom` focus kind. 691 arena: std.heap.ArenaAllocator, 692 693 pub const Item = struct { text: []const u8, link: []const u8 = "", top_label: []const u8 = "" }; 694 695 pub fn init(allocator: std.mem.Allocator) !WordFlow { 696 return .{ 697 .focus = try Focus.create(allocator, .container), 698 .grid = null, 699 .text_boxes = .empty, 700 .rects = .empty, 701 .arena = std.heap.ArenaAllocator.init(allocator), 702 }; 703 } 704 705 pub fn deinit(self: *WordFlow, allocator: std.mem.Allocator) void { 706 self.focus.destroy(allocator); 707 if (self.grid) |*grid| { 708 grid.deinit(); 709 self.grid = null; 710 } 711 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 712 self.text_boxes.deinit(allocator); 713 self.rects.deinit(allocator); 714 self.arena.deinit(); 715 } 716 717 // the text box copies the item text; links and top labels are copied into the 718 // arena, so the caller's slices needn't outlive this call. 719 pub fn setItems(self: *WordFlow, allocator: std.mem.Allocator, items: []const Item) !void { 720 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 721 self.text_boxes.clearAndFree(allocator); 722 723 _ = self.arena.reset(.retain_capacity); 724 const aa = self.arena.allocator(); 725 726 self.focus.clear(); 727 self.focus.child_id = null; 728 729 for (items) |item| { 730 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = try aa.dupe(u8, item.top_label) } }); 731 errdefer text_box.deinit(allocator); 732 text_box.getFocus().mode = .all; 733 734 if (item.link.len > 0) { 735 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 736 } 737 738 try self.text_boxes.append(allocator, text_box); 739 } 740 741 if (self.text_boxes.items.len > 0) { 742 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 743 } 744 } 745 746 pub fn build(self: *WordFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 747 self.clearGrid(); 748 self.focus.clear(); 749 750 if (self.text_boxes.items.len == 0) return; 751 const max_width = constraint.max_size.width orelse 80; 752 if (max_width == 0) return; 753 754 // lay the items out left to right, wrapping to a new row when the next 755 // one wouldn't fit. 756 self.rects.clearRetainingCapacity(); 757 var x: usize = 0; 758 var y: usize = 0; 759 var row_height: usize = 0; 760 var total_width: usize = 0; 761 for (self.text_boxes.items) |*tb| { 762 try tb.build(allocator, .{ 763 .min_size = .{ .width = null, .height = null }, 764 .max_size = .{ .width = max_width, .height = null }, 765 }, root_focus); 766 const tb_grid_maybe = tb.getGrid(); 767 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 768 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 769 if (x > 0 and x + w > max_width) { 770 x = 0; 771 y += row_height; 772 row_height = 0; 773 } 774 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 775 if (h > row_height) row_height = h; 776 x += w; 777 if (x > total_width) total_width = x; 778 } 779 780 const total_height = y + row_height; 781 if (total_width == 0 or total_height == 0) return; 782 783 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 784 errdefer grid.deinit(); 785 786 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 787 const tb_grid = tb.getGrid() orelse continue; 788 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 789 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 790 } 791 792 self.grid = grid; 793 } 794 795 pub fn input(self: *WordFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 796 _ = self; 797 _ = allocator; 798 _ = key; 799 _ = root_focus; 800 } 801 802 pub fn clearGrid(self: *WordFlow) void { 803 if (self.grid) |*grid| { 804 grid.deinit(); 805 self.grid = null; 806 } 807 for (self.text_boxes.items) |*tb| tb.clearGrid(); 808 } 809 810 pub fn getGrid(self: WordFlow) ?Grid { 811 return self.grid; 812 } 813 814 pub fn getFocus(self: *WordFlow) *Focus { 815 return self.focus; 816 } 817 818 pub fn indexOfFocusId(self: WordFlow, focus_id: usize) ?usize { 819 for (self.text_boxes.items, 0..) |tb, i| { 820 if (tb.focus.id == focus_id) return i; 821 } 822 return null; 823 } 824 825 // the first item of the row below (when `downward`) or above, or null at 826 // the flow's edge. items are laid out in order, so y is nondecreasing. 827 pub fn rowStep(self: WordFlow, index: usize, downward: bool) ?usize { 828 const rects = self.rects.items; 829 if (index >= rects.len) return null; 830 const cur_y = rects[index].y; 831 if (downward) { 832 for (rects[index + 1 ..], index + 1..) |r, i| { 833 if (r.y > cur_y) return i; 834 } 835 return null; 836 } 837 // walk the earlier rows; the last one visited is the row above. 838 var prev_first: ?usize = null; 839 var i: usize = 0; 840 while (i < rects.len and rects[i].y < cur_y) { 841 prev_first = i; 842 const y = rects[i].y; 843 while (i < rects.len and rects[i].y == y) i += 1; 844 } 845 return prev_first; 846 } 847 }; 848 849 // a word-wrapped paragraph of styled runs. each line a link touches is a 850 // focusable piece, and a focused link draws all its pieces inverted. 851 pub const RichText = struct { 852 focus: *Focus, 853 grid: ?Grid, 854 // backs the decoded content and the link targets 855 arena: std.heap.ArenaAllocator, 856 runes: []const u21, 857 // each rune's span 858 span_of: []const usize, 859 styles: []const Grid.Style, 860 // each span's link index, null outside a link 861 span_links: []const ?usize, 862 links: []const []const u8, 863 // each link's piece focuses in line order, kept across builds so a link 864 // keeps its ids when the text rewraps 865 link_focuses: []std.ArrayList(*Focus), 866 lines: std.ArrayList(wgt.Line), 867 // scratch for each build's link pieces 868 pieces: std.ArrayList(Piece), 869 870 pub const Span = struct { 871 text: []const u8, 872 style: Grid.Style = .{}, 873 // the focus kind a click or enter follows; empty for plain text 874 link: []const u8 = "", 875 }; 876 877 const Piece = struct { 878 link: usize, 879 rect: layout.URect, 880 }; 881 882 pub fn init(allocator: std.mem.Allocator, spans: []const Span) !RichText { 883 var arena = std.heap.ArenaAllocator.init(allocator); 884 errdefer arena.deinit(); 885 const aa = arena.allocator(); 886 887 var runes: std.ArrayList(u21) = .empty; 888 var span_of: std.ArrayList(usize) = .empty; 889 const styles = try aa.alloc(Grid.Style, spans.len); 890 const span_links = try aa.alloc(?usize, spans.len); 891 var links: std.ArrayList([]const u8) = .empty; 892 for (spans, 0..) |span, i| { 893 styles[i] = span.style; 894 span_links[i] = null; 895 if (span.link.len > 0) { 896 // adjacent spans with the same target are one link 897 const same = i > 0 and spans[i - 1].link.len > 0 and std.mem.eql(u8, spans[i - 1].link, span.link); 898 if (!same) try links.append(aa, try aa.dupe(u8, span.link)); 899 span_links[i] = links.items.len - 1; 900 } 901 var utf8 = (try std.unicode.Utf8View.init(span.text)).iterator(); 902 while (utf8.nextCodepoint()) |codepoint| { 903 try runes.append(aa, codepoint); 904 try span_of.append(aa, i); 905 } 906 } 907 908 const link_focuses = try aa.alloc(std.ArrayList(*Focus), links.items.len); 909 @memset(link_focuses, .empty); 910 911 return .{ 912 .focus = try Focus.create(allocator, .container), 913 .grid = null, 914 .arena = arena, 915 .runes = runes.items, 916 .span_of = span_of.items, 917 .styles = styles, 918 .span_links = span_links, 919 .links = links.items, 920 .link_focuses = link_focuses, 921 .lines = .empty, 922 .pieces = .empty, 923 }; 924 } 925 926 pub fn deinit(self: *RichText, allocator: std.mem.Allocator) void { 927 self.focus.destroy(allocator); 928 self.clearGrid(); 929 for (self.link_focuses) |*focuses| { 930 for (focuses.items) |focus| focus.destroy(allocator); 931 focuses.deinit(allocator); 932 } 933 self.pieces.deinit(allocator); 934 self.lines.deinit(allocator); 935 self.arena.deinit(); 936 } 937 938 // the url a terminal can open for a link: raw links to absolute urls 939 fn terminalLink(link: []const u8) ?[]const u8 { 940 if (!std.mem.startsWith(u8, link, ui.raw_link_prefix)) return null; 941 const url = link[ui.raw_link_prefix.len..]; 942 return if (std.mem.startsWith(u8, url, "/")) null else url; 943 } 944 945 // the link one of whose pieces has focus id `id` 946 fn linkOf(self: *const RichText, id: ?usize) ?usize { 947 for (self.link_focuses, 0..) |focuses, l| { 948 for (focuses.items) |focus| if (focus.id == id) return l; 949 } 950 return null; 951 } 952 953 pub fn build(self: *RichText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 954 self.clearGrid(); 955 self.focus.clear(); 956 const max_width = constraint.max_size.width; 957 if (max_width == 0 or constraint.max_size.height == 0) return; 958 const focused_link = self.linkOf(root_focus.grandchild_id); 959 960 try wgt.wrapLines(allocator, &self.lines, self.runes, .word, max_width); 961 // trailing spaces are dropped so a wrapped link doesn't highlight them 962 for (self.lines.items) |*line| { 963 while (line.end > line.start and self.runes[line.end - 1] == ' ') { 964 line.end -= 1; 965 line.width -= 1; 966 } 967 } 968 const visible = @min(self.lines.items.len, constraint.max_size.height orelse self.lines.items.len); 969 var width: usize = 1; 970 for (self.lines.items[0..visible]) |line| width = @max(width, line.width); 971 if (max_width) |w| width = @min(width, w); 972 973 var grid = try Grid.init(allocator, .{ .width = width, .height = visible }); 974 errdefer grid.deinit(); 975 self.pieces.clearRetainingCapacity(); 976 for (self.lines.items[0..visible], 0..) |line, y| { 977 var x: usize = 0; 978 for (self.runes[line.start..line.end], line.start..) |rune, i| { 979 const rune_width = xitui.width.cellWidth(rune); 980 if (x + rune_width > width) break; 981 const link = self.span_links[self.span_of[i]]; 982 const cell = try grid.cell(x, y); 983 cell.style = self.styles[self.span_of[i]]; 984 if (link) |l| { 985 cell.link = terminalLink(self.links[l]); 986 if (l == focused_link) cell.style.inverted = true; 987 // extend this line's piece of the link, or start one 988 const items = self.pieces.items; 989 if (items.len > 0 and items[items.len - 1].link == l and items[items.len - 1].rect.y == y) { 990 items[items.len - 1].rect.size.width += rune_width; 991 } else { 992 try self.pieces.append(allocator, .{ .link = l, .rect = .{ .x = x, .y = y, .size = .{ .width = rune_width, .height = 1 } } }); 993 } 994 } 995 try grid.setRune(x, y, rune); 996 x += rune_width; 997 } 998 } 999 1000 // a link's pieces are consecutive, since its runes are 1001 var piece_index: usize = 0; 1002 for (self.pieces.items, 0..) |piece, i| { 1003 piece_index = if (i > 0 and self.pieces.items[i - 1].link == piece.link) piece_index + 1 else 0; 1004 const focuses = &self.link_focuses[piece.link]; 1005 if (piece_index == focuses.items.len) { 1006 const focus = try Focus.create(allocator, .{ .custom = self.links[piece.link] }); 1007 errdefer focus.destroy(allocator); 1008 focus.mode = .all; 1009 try focuses.append(allocator, focus); 1010 } 1011 try self.focus.addChild(allocator, focuses.items[piece_index], piece.rect.size, piece.rect.x, piece.rect.y); 1012 } 1013 1014 // focus rests on a link's first piece, which stays while the link shows 1015 if (focused_link) |l| if (self.link_focuses[l].items.len > 0) { 1016 const first = self.link_focuses[l].items[0].id; 1017 if (self.focus.children.contains(first)) { 1018 root_focus.grandchild_id = first; 1019 self.focus.child_id = first; 1020 } 1021 }; 1022 1023 self.grid = grid; 1024 } 1025 1026 pub fn input(self: *RichText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1027 _ = self; 1028 _ = allocator; 1029 _ = key; 1030 _ = root_focus; 1031 } 1032 1033 pub fn clearGrid(self: *RichText) void { 1034 if (self.grid) |*grid| { 1035 grid.deinit(); 1036 self.grid = null; 1037 } 1038 } 1039 1040 pub fn getGrid(self: RichText) ?Grid { 1041 return self.grid; 1042 } 1043 1044 pub fn getFocus(self: *RichText) *Focus { 1045 return self.focus; 1046 } 1047 }; 1048 1049 // an invisible widget that fills the horizontal space granted by its parent. 1050 // used inside Box(horiz) with a min_size so the box reserves space for the 1051 // children that follow, pushing them to the right. 1052 pub const Spacer = struct { 1053 focus: *Focus, 1054 grid: ?Grid, 1055 1056 pub fn init(allocator: std.mem.Allocator) !Spacer { 1057 return .{ 1058 .focus = try Focus.create(allocator, .container), 1059 .grid = null, 1060 }; 1061 } 1062 1063 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1064 self.focus.destroy(allocator); 1065 if (self.grid) |*grid| { 1066 grid.deinit(); 1067 self.grid = null; 1068 } 1069 } 1070 1071 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1072 _ = root_focus; 1073 self.clearGrid(); 1074 const width = constraint.max_size.width orelse return; 1075 if (width == 0) return; 1076 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1077 } 1078 1079 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1080 _ = self; 1081 _ = allocator; 1082 _ = key; 1083 _ = root_focus; 1084 } 1085 1086 pub fn clearGrid(self: *Spacer) void { 1087 if (self.grid) |*grid| { 1088 grid.deinit(); 1089 self.grid = null; 1090 } 1091 } 1092 1093 pub fn getGrid(self: Spacer) ?Grid { 1094 return self.grid; 1095 } 1096 1097 pub fn getFocus(self: *Spacer) *Focus { 1098 return self.focus; 1099 } 1100 }; 1101 1102 // a sub-header's search box: a labeled text input whose enter the owning view 1103 // turns into a navigation. shared by the files, commits and thread tabs. 1104 pub const SearchBox = struct { 1105 text_input: wgt.TextInput, 1106 session: *ui.Session, 1107 1108 // the text width, narrowed toward the minimum when the row is tight 1109 const min_width = 18; 1110 const max_width = 30; 1111 1112 // the room the box asks its row for 1113 pub const min_size: layout.MaybeSize = .{ .width = min_width + 2, .height = 3 }; 1114 1115 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, label: []const u8, name: []const u8, text_value: ?[]const u8) !SearchBox { 1116 var text_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = label }, .name = name, .round_corners = true, .visible_width = max_width, .render_content = session.is_terminal }); 1117 errdefer text_input.deinit(allocator); 1118 text_input.getFocus().mode = .all; 1119 if (text_value) |value| try text_input.setContent(allocator, value); 1120 return .{ .text_input = text_input, .session = session }; 1121 } 1122 1123 pub fn deinit(self: *SearchBox, allocator: std.mem.Allocator) void { 1124 self.text_input.deinit(allocator); 1125 } 1126 1127 pub fn build(self: *SearchBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1128 // the row leaves room for its other children's minimums, so this is what can be spared 1129 const room = (constraint.max_size.width orelse max_width + 2) -| 2; 1130 self.text_input.options.visible_width = std.math.clamp(room, min_width, max_width); 1131 self.text_input.options.bottom_label.text = if (root_focus.grandchild_id == self.getFocus().id) " press enter " else ""; 1132 try self.text_input.build(allocator, constraint, root_focus); 1133 if (!self.session.is_terminal) { 1134 try self.session.text_inputs.put(self.session.arena.allocator(), self.getFocus().id, &self.text_input); 1135 } 1136 } 1137 1138 pub fn input(self: *SearchBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1139 try self.text_input.input(allocator, key, root_focus); 1140 } 1141 1142 // the typed text; the caller owns it 1143 pub fn text(self: *SearchBox, allocator: std.mem.Allocator) ![]u8 { 1144 return self.text_input.text(allocator); 1145 } 1146 1147 // whether the cursor sits past the last character, where right-arrow leaves 1148 // the box rather than moving within it 1149 pub fn atEnd(self: *SearchBox) bool { 1150 return self.text_input.cursor == self.text_input.content.items.len; 1151 } 1152 1153 pub fn clearGrid(self: *SearchBox) void { 1154 self.text_input.clearGrid(); 1155 } 1156 1157 pub fn getGrid(self: SearchBox) ?Grid { 1158 return self.text_input.getGrid(); 1159 } 1160 1161 pub fn getFocus(self: *SearchBox) *Focus { 1162 return self.text_input.getFocus(); 1163 } 1164 }; 1165 1166 // a wrapping row of bordered options posted as one form field. the selection 1167 // is the focused option, so a click selects on the web too. 1168 pub const Radio = struct { 1169 // holds the options' flow, framed under the label when there is one 1170 box: wgt.Box(Widget), 1171 name: []const u8, 1172 values: []const []const u8, 1173 1174 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, name: []const u8, values: []const []const u8, initial: []const u8, label: ?[]const u8) !Radio { 1175 var box = try wgt.Box(Widget).init(allocator, .{ 1176 .border = if (label != null) .single else null, 1177 .round_corners = true, 1178 .direction = .vert, 1179 .top_label = .{ .text = label orelse "" }, 1180 }); 1181 errdefer box.deinit(allocator); 1182 1183 var flow = try WordFlow.init(allocator); 1184 errdefer flow.deinit(allocator); 1185 const pa = session.page_arena.allocator(); 1186 const items = try pa.alloc(WordFlow.Item, values.len); 1187 for (items, values) |*item, value| item.* = .{ .text = value, .link = try pa.print(ui.radio_prefix ++ "{s}={s}", .{ name, value }) }; 1188 try flow.setItems(allocator, items); 1189 for (flow.text_boxes.items, values) |*option, value| { 1190 if (std.mem.eql(u8, value, initial)) flow.focus.child_id = option.getFocus().id; 1191 } 1192 1193 box.getFocus().child_id = flow.getFocus().id; 1194 try box.children.put(allocator, flow.getFocus().id, .{ .widget = .{ .word_flow = flow }, .rect = null, .min_size = null }); 1195 return .{ .box = box, .name = name, .values = values }; 1196 } 1197 1198 pub fn deinit(self: *Radio, allocator: std.mem.Allocator) void { 1199 self.box.deinit(allocator); 1200 } 1201 1202 fn optionFlow(self: *Radio) *WordFlow { 1203 return &self.box.children.values()[0].widget.word_flow; 1204 } 1205 1206 // the focused option's value 1207 pub fn selected(self: *Radio) []const u8 { 1208 const options = self.optionFlow(); 1209 return self.values[options.indexOfFocusId(options.focus.child_id orelse unreachable) orelse unreachable]; 1210 } 1211 1212 pub fn build(self: *Radio, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1213 self.clearGrid(); 1214 const options = self.optionFlow(); 1215 const selected_id = options.focus.child_id; 1216 for (options.text_boxes.items) |*option| { 1217 option.options.border = if (option.getFocus().id == selected_id) .single else .hidden; 1218 } 1219 try self.box.build(allocator, constraint, root_focus); 1220 } 1221 1222 // left and right step through the options in order, however they wrap 1223 pub fn input(self: *Radio, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1224 _ = allocator; 1225 const options = self.optionFlow(); 1226 const index = options.indexOfFocusId(options.focus.child_id orelse return) orelse return; 1227 const option_boxes = options.text_boxes.items; 1228 switch (key) { 1229 .arrow_left => if (index > 0) root_focus.setFocus(option_boxes[index - 1].getFocus().id), 1230 .arrow_right => if (index + 1 < option_boxes.len) root_focus.setFocus(option_boxes[index + 1].getFocus().id), 1231 else => {}, 1232 } 1233 } 1234 1235 pub fn clearGrid(self: *Radio) void { 1236 self.box.clearGrid(); 1237 } 1238 1239 pub fn getGrid(self: Radio) ?Grid { 1240 return self.box.getGrid(); 1241 } 1242 1243 pub fn getFocus(self: *Radio) *Focus { 1244 return self.box.getFocus(); 1245 } 1246 }; 1247 1248 // a sub-header's row, which scrolls sideways once its children's minimum widths 1249 // outgrow the window, like the page header: natively on the web, following the 1250 // focused child in the terminal 1251 pub const HeaderScroll = struct { 1252 scroll: wgt.Scroll(Widget), 1253 1254 // an empty row, which the caller fills through `row` 1255 pub fn init(allocator: std.mem.Allocator, session: *const ui.Session) !HeaderScroll { 1256 // the outer box hands the row a max width, which the scroll alone can't 1257 var outer = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1258 errdefer outer.deinit(allocator); 1259 { 1260 var row_box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1261 errdefer row_box.deinit(allocator); 1262 try outer.children.put(allocator, row_box.getFocus().id, .{ .widget = .{ .box = row_box }, .rect = null, .min_size = null }); 1263 outer.getFocus().child_id = row_box.getFocus().id; 1264 } 1265 return .{ .scroll = try wgt.Scroll(Widget).init(allocator, .{ .box = outer }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }) }; 1266 } 1267 1268 pub fn deinit(self: *HeaderScroll, allocator: std.mem.Allocator) void { 1269 self.scroll.deinit(allocator); 1270 } 1271 1272 fn rowChild(self: *const HeaderScroll) *wgt.Box(Widget).Child { 1273 return &self.scroll.child.box.children.values()[0]; 1274 } 1275 1276 pub fn row(self: *const HeaderScroll) *wgt.Box(Widget) { 1277 return &self.rowChild().widget.box; 1278 } 1279 1280 pub fn build(self: *HeaderScroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1281 self.clearGrid(); 1282 const row_box = self.row(); 1283 // bring the focused child into view where the last build put it 1284 if (!self.scroll.options.web_native) if (row_box.getFocus().child_id) |id| if (row_box.children.get(id)) |child| if (child.rect) |rect| self.scroll.scrollToRect(rect); 1285 1286 // the window's width, or the children's minimums when they need more. 1287 // only a max, since a min would stretch the row's last laid out child. 1288 const width: ?usize = if (constraint.max_size.width) |viewport| blk: { 1289 var needed: usize = 0; 1290 for (row_box.children.values()) |child| { 1291 if (child.min_size) |min_size| needed += min_size.width orelse 0; 1292 } 1293 break :blk @max(viewport, needed); 1294 } else null; 1295 self.rowChild().max_size = .{ .width = width, .height = null }; 1296 try self.scroll.build(allocator, constraint, root_focus); 1297 } 1298 1299 pub fn input(self: *HeaderScroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1300 try self.scroll.input(allocator, key, root_focus); 1301 } 1302 1303 pub fn clearGrid(self: *HeaderScroll) void { 1304 self.scroll.clearGrid(); 1305 } 1306 1307 pub fn getGrid(self: HeaderScroll) ?Grid { 1308 return self.scroll.getGrid(); 1309 } 1310 1311 pub fn getFocus(self: *HeaderScroll) *Focus { 1312 return self.scroll.getFocus(); 1313 } 1314 }; 1315 1316 // a tab's sub-header: an optional search box on the left, then a spacer, then 1317 // the clone url on the right. the owning view turns a submitted query into a 1318 // route, so route building stays with the tab that knows its own url. 1319 pub const SearchHeader = struct { 1320 scroll: HeaderScroll, 1321 session: *ui.Session, 1322 1323 pub fn init( 1324 allocator: std.mem.Allocator, 1325 session: *ui.Session, 1326 handle: ui.RepoHandle, 1327 label: []const u8, 1328 name: []const u8, 1329 text: ?[]const u8, 1330 show_input: bool, 1331 ) !SearchHeader { 1332 var scroll = try HeaderScroll.init(allocator, session); 1333 errdefer scroll.deinit(allocator); 1334 const box = scroll.row(); 1335 1336 // the search box only appears where an index can serve it. 1337 if (show_input) { 1338 var search_box = try SearchBox.init(allocator, session, label, name, text); 1339 errdefer search_box.deinit(allocator); 1340 box.getFocus().child_id = search_box.getFocus().id; 1341 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 1342 } 1343 1344 // the spacer pushes the clone url to the right edge. 1345 { 1346 var spacer = try Spacer.init(allocator); 1347 errdefer spacer.deinit(allocator); 1348 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1349 } 1350 1351 if (session.data.host_kind == .server) { 1352 if (try CopyableText.initClone(allocator, session, handle)) |value| { 1353 var clone_url = value; 1354 errdefer clone_url.deinit(allocator); 1355 const min_width = clone_url.minWidth(); 1356 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 1357 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 1358 } 1359 } 1360 1361 return .{ .scroll = scroll, .session = session }; 1362 } 1363 1364 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 1365 self.scroll.deinit(allocator); 1366 } 1367 1368 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1369 self.clearGrid(); 1370 try self.scroll.build(allocator, constraint, root_focus); 1371 } 1372 1373 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1374 const current = self.scroll.row().getFocus().child_id orelse return; 1375 if (self.searchBox()) |search_box| { 1376 if (current == search_box.getFocus().id) { 1377 // tab, and right-arrow past the last character, step to the 1378 // clone url beside the box 1379 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1380 if (self.cloneUrl()) |clone_url| { 1381 root_focus.setFocus(clone_url.getFocus().id); 1382 return; 1383 } 1384 } 1385 return search_box.input(allocator, key, root_focus); 1386 } 1387 } 1388 const clone_url = self.cloneUrl() orelse return; 1389 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1390 if (self.searchBox()) |search_box| { 1391 root_focus.setFocus(search_box.getFocus().id); 1392 return; 1393 } 1394 } 1395 try clone_url.input(allocator, key, root_focus); 1396 } 1397 1398 // the typed query when enter should submit it: the box exists and holds 1399 // focus. the caller owns the returned text. 1400 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1401 const search_box = self.searchBox() orelse return null; 1402 if (self.scroll.row().getFocus().child_id != search_box.getFocus().id) return null; 1403 return try search_box.text(allocator); 1404 } 1405 1406 // enter the header, preferring the search box. 1407 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1408 if (self.searchBox()) |search_box| { 1409 root_focus.setFocus(search_box.getFocus().id); 1410 return true; 1411 } 1412 const clone_url = self.cloneUrl() orelse return false; 1413 root_focus.setFocus(clone_url.getFocus().id); 1414 return true; 1415 } 1416 1417 pub fn hasFocusable(self: *SearchHeader) bool { 1418 return self.cloneUrl() != null or self.searchBox() != null; 1419 } 1420 1421 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1422 for (self.scroll.row().children.values()) |*child| switch (child.widget) { 1423 .copyable_text => |*copyable_text| return copyable_text, 1424 else => {}, 1425 }; 1426 return null; 1427 } 1428 1429 fn searchBox(self: *SearchHeader) ?*SearchBox { 1430 for (self.scroll.row().children.values()) |*child| switch (child.widget) { 1431 .search_box => |*search_box| return search_box, 1432 else => {}, 1433 }; 1434 return null; 1435 } 1436 1437 pub fn clearGrid(self: *SearchHeader) void { 1438 self.scroll.clearGrid(); 1439 } 1440 1441 pub fn getGrid(self: SearchHeader) ?Grid { 1442 return self.scroll.getGrid(); 1443 } 1444 1445 pub fn getFocus(self: *SearchHeader) *Focus { 1446 return self.scroll.getFocus(); 1447 } 1448 }; 1449 1450 // a one-row, non-focusable status bar showing the current page's url 1451 pub const Footer = struct { 1452 focus: *Focus, 1453 grid: ?Grid, 1454 session: *ui.Session, 1455 arena: std.heap.ArenaAllocator, 1456 url: []const u8 = "", 1457 url_width: usize = 0, 1458 1459 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1460 return .{ 1461 .focus = try Focus.create(allocator, .container), 1462 .grid = null, 1463 .session = session, 1464 .arena = std.heap.ArenaAllocator.init(allocator), 1465 }; 1466 } 1467 1468 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1469 self.focus.destroy(allocator); 1470 if (self.grid) |*grid| grid.deinit(); 1471 self.arena.deinit(); 1472 } 1473 1474 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1475 _ = root_focus; 1476 self.clearGrid(); 1477 const width = constraint.max_size.width orelse return; 1478 if (width == 0) return; 1479 1480 _ = self.arena.reset(.retain_capacity); 1481 self.url = ""; 1482 self.url_width = 0; 1483 const aa = self.arena.allocator(); 1484 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1485 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1486 self.url = text; 1487 1488 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1489 errdefer grid.deinit(); 1490 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1491 var i: usize = 0; 1492 while (utf8.nextCodepoint()) |ch| { 1493 const rune_width = xitui.width.cellWidth(ch); 1494 if (i + rune_width > width) break; 1495 // a terminal hyperlink to the page 1496 (try grid.cell(i, 0)).link = text; 1497 try grid.setRune(i, 0, ch); 1498 i += rune_width; 1499 } 1500 self.url_width = i; 1501 self.grid = grid; 1502 } 1503 1504 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1505 _ = allocator; 1506 switch (key) { 1507 .mouse => |mouse| { 1508 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1509 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1510 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1511 }, 1512 else => {}, 1513 } 1514 } 1515 1516 pub fn clearGrid(self: *Footer) void { 1517 if (self.grid) |*grid| { 1518 grid.deinit(); 1519 self.grid = null; 1520 } 1521 } 1522 1523 pub fn getGrid(self: Footer) ?Grid { 1524 return self.grid; 1525 } 1526 1527 pub fn getFocus(self: *Footer) *Focus { 1528 return self.focus; 1529 } 1530 }; 1531 1532 // a single-child wrapper that builds the child at its natural size and 1533 // positions its grid in the middle of the area granted by the parent 1534 // a bordered label, indented past the content it precedes 1535 pub const SectionLabel = struct { 1536 box: wgt.Box(Widget), 1537 1538 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1539 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1540 errdefer box.deinit(allocator); 1541 1542 { 1543 var gap = try wgt.Text.init(allocator, " "); 1544 errdefer gap.deinit(allocator); 1545 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1546 } 1547 1548 { 1549 var tb = try wgt.TextBox.init(allocator, content, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1550 errdefer tb.deinit(allocator); 1551 tb.getFocus().mode = .all; 1552 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1553 } 1554 1555 box.getFocus().child_id = box.children.keys()[1]; 1556 return .{ .box = box }; 1557 } 1558 1559 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1560 self.box.deinit(allocator); 1561 } 1562 1563 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1564 try self.box.build(allocator, constraint, root_focus); 1565 } 1566 1567 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1568 _ = self; 1569 _ = allocator; 1570 _ = key; 1571 _ = root_focus; 1572 } 1573 1574 pub fn clearGrid(self: *SectionLabel) void { 1575 self.box.clearGrid(); 1576 } 1577 1578 pub fn getGrid(self: SectionLabel) ?Grid { 1579 return self.box.getGrid(); 1580 } 1581 1582 pub fn getFocus(self: *SectionLabel) *Focus { 1583 return self.box.getFocus(); 1584 } 1585 }; 1586 1587 // a submit button, indented past its form's content. the web form POSTs 1588 // the button's form; terminal hosts detect presses through `buttonId`. 1589 pub const SubmitButton = struct { 1590 box: wgt.Box(Widget), 1591 1592 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1593 return initLabeled(allocator, "submit"); 1594 } 1595 1596 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1597 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1598 errdefer box.deinit(allocator); 1599 1600 { 1601 var gap = try wgt.Text.init(allocator, " "); 1602 errdefer gap.deinit(allocator); 1603 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1604 } 1605 1606 { 1607 var button = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1608 errdefer button.deinit(allocator); 1609 button.getFocus().mode = .all; 1610 // the renderer distinguishes plain clickables from buttons that 1611 // should POST to a server route by this kind. 1612 button.getFocus().kind = .{ .custom = "submit" }; 1613 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1614 } 1615 1616 box.getFocus().child_id = box.children.keys()[1]; 1617 return .{ .box = box }; 1618 } 1619 1620 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1621 self.box.deinit(allocator); 1622 } 1623 1624 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1625 const button = &self.box.children.values()[1]; 1626 try button.widget.text_box.setContent(allocator, label); 1627 button.min_size = .{ .width = label.len + 2, .height = null }; 1628 } 1629 1630 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1631 try self.box.build(allocator, constraint, root_focus); 1632 } 1633 1634 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1635 _ = self; 1636 _ = allocator; 1637 _ = key; 1638 _ = root_focus; 1639 } 1640 1641 pub fn clearGrid(self: *SubmitButton) void { 1642 self.box.clearGrid(); 1643 } 1644 1645 pub fn getGrid(self: SubmitButton) ?Grid { 1646 return self.box.getGrid(); 1647 } 1648 1649 pub fn getFocus(self: *SubmitButton) *Focus { 1650 return self.box.getFocus(); 1651 } 1652 1653 // the button's focus id, for click hit-testing 1654 pub fn buttonId(self: *const SubmitButton) usize { 1655 return self.box.children.keys()[1]; 1656 } 1657 }; 1658 1659 // keep `rect`, in a centered widget's space, in view within its scroll 1660 pub fn scrollToCenteredRect(scroll: *wgt.Scroll(Widget), rect: layout.IRect) void { 1661 const center = &scroll.child.center; 1662 const center_grid = center.getGrid() orelse return; 1663 const child_grid = center.child.getGrid() orelse return; 1664 const offset_y: isize = @intCast((center_grid.size.height -| child_grid.size.height) / 2); 1665 scroll.scrollToRect(.{ .x = rect.x, .y = rect.y + offset_y, .size = rect.size }); 1666 } 1667 1668 pub const Center = struct { 1669 focus: *Focus, 1670 grid: ?Grid, 1671 child: *Widget, 1672 1673 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1674 const child = try allocator.create(Widget); 1675 errdefer allocator.destroy(child); 1676 child.* = child_widget; 1677 return .{ 1678 .focus = try Focus.create(allocator, .container), 1679 .grid = null, 1680 .child = child, 1681 }; 1682 } 1683 1684 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1685 self.focus.destroy(allocator); 1686 if (self.grid) |*grid| { 1687 grid.deinit(); 1688 self.grid = null; 1689 } 1690 self.child.deinit(allocator); 1691 allocator.destroy(self.child); 1692 } 1693 1694 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1695 self.clearGrid(); 1696 self.getFocus().clear(); 1697 1698 // build the child without forcing it to fill min_size; it sizes 1699 // itself to its content within the available max. 1700 try self.child.build(allocator, .{ 1701 .min_size = .{ .width = null, .height = null }, 1702 .max_size = constraint.max_size, 1703 }, root_focus); 1704 1705 const child_grid = self.child.getGrid() orelse return; 1706 1707 // prefer max when given; otherwise grow to max(min, child) so that 1708 // when only a min is set (e.g. the wasm path passes viewport rows 1709 // as min_height with no max), we can still vertically center while 1710 // letting taller content extend past the min. 1711 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 1712 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 1713 if (width == 0 or height == 0) return; 1714 1715 const offset_x: usize = (width -| child_grid.size.width) / 2; 1716 const offset_y: usize = (height -| child_grid.size.height) / 2; 1717 1718 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1719 errdefer grid.deinit(); 1720 try grid.drawGrid(child_grid, offset_x, offset_y); 1721 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1722 self.getFocus().child_id = self.child.getFocus().id; 1723 1724 self.grid = grid; 1725 } 1726 1727 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1728 try self.child.input(allocator, key, root_focus); 1729 } 1730 1731 pub fn clearGrid(self: *Center) void { 1732 if (self.grid) |*grid| { 1733 grid.deinit(); 1734 self.grid = null; 1735 } 1736 self.child.clearGrid(); 1737 } 1738 1739 pub fn getGrid(self: Center) ?Grid { 1740 return self.grid; 1741 } 1742 1743 pub fn getFocus(self: *Center) *Focus { 1744 return self.focus; 1745 } 1746 }; 1747 1748 // renders truecolor ANSI art 1749 pub const AnsiArt = struct { 1750 focus: *Focus, 1751 grid: ?Grid, 1752 content: []const u8, 1753 1754 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1755 return .{ 1756 .focus = try Focus.create(allocator, .container), 1757 .grid = null, 1758 .content = content, 1759 }; 1760 } 1761 1762 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1763 self.focus.destroy(allocator); 1764 if (self.grid) |*grid| { 1765 grid.deinit(); 1766 self.grid = null; 1767 } 1768 } 1769 1770 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1771 _ = root_focus; 1772 self.clearGrid(); 1773 1774 // parse into rows of cells, then copy into a rectangular grid 1775 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1776 defer { 1777 for (rows.items) |*row| row.deinit(allocator); 1778 rows.deinit(allocator); 1779 } 1780 var row: std.ArrayList(Grid.Cell) = .empty; 1781 errdefer row.deinit(allocator); 1782 1783 var style: Grid.Style = .{}; 1784 var width: usize = 0; 1785 const content = self.content; 1786 var i: usize = 0; 1787 while (i < content.len) { 1788 const byte = content[i]; 1789 if (byte == '\n') { 1790 width = @max(width, row.items.len); 1791 try rows.append(allocator, row); 1792 row = .empty; 1793 i += 1; 1794 } else if (byte == '\r') { 1795 // git may checkout the ansi art with crlf line endings on windows 1796 i += 1; 1797 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1798 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1799 var j = i + 2; 1800 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1801 if (j >= content.len) { 1802 i = content.len; // malformed trailing escape; stop 1803 } else { 1804 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1805 i = j + 1; 1806 } 1807 } else { 1808 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1809 const end = @min(content.len, i + len); 1810 const rune = content[i..end]; 1811 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1812 // a foreground color does not make a space visible. only an 1813 // effective background makes it opaque; foreground state often 1814 // remains active through the padding at the end of a row. 1815 const effective_background = if (style.inverted) style.fg else style.bg; 1816 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1817 try row.append(allocator, .{ 1818 .rune = if (transparent) null else codepoint, 1819 .style = style, 1820 }); 1821 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1822 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1823 } 1824 i = end; 1825 } 1826 } 1827 // a trailing row with content but no closing newline 1828 if (row.items.len > 0) { 1829 width = @max(width, row.items.len); 1830 try rows.append(allocator, row); 1831 } else { 1832 row.deinit(allocator); 1833 } 1834 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1835 1836 const height = rows.items.len; 1837 if (width == 0 or height == 0) return; 1838 1839 const clamped_w = @min(width, constraint.max_size.width orelse width); 1840 const clamped_h = @min(height, constraint.max_size.height orelse height); 1841 1842 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1843 errdefer grid.deinit(); 1844 for (rows.items[0..clamped_h], 0..) |r, y| { 1845 const n = @min(r.items.len, clamped_w); 1846 for (r.items[0..n], 0..) |cell, x| { 1847 if (cell.continuation) continue; 1848 (try grid.cell(x, y)).style = cell.style; 1849 try grid.setRune(x, y, cell.rune); 1850 } 1851 } 1852 self.grid = grid; 1853 } 1854 1855 pub fn clearGrid(self: *AnsiArt) void { 1856 if (self.grid) |*grid| { 1857 grid.deinit(); 1858 self.grid = null; 1859 } 1860 } 1861 1862 pub fn getGrid(self: AnsiArt) ?Grid { 1863 return self.grid; 1864 } 1865 1866 pub fn getFocus(self: *AnsiArt) *Focus { 1867 return self.focus; 1868 } 1869 1870 fn applySgr(style: *Grid.Style, params: []const u8) void { 1871 var nums: [16]u32 = undefined; 1872 var n: usize = 0; 1873 var it = std.mem.splitScalar(u8, params, ';'); 1874 while (it.next()) |tok| { 1875 if (n >= nums.len) break; 1876 // an empty parameter (e.g. bare "\x1b[m") means 0 1877 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1878 n += 1; 1879 } 1880 if (n == 0) { 1881 style.* = .{}; 1882 return; 1883 } 1884 var i: usize = 0; 1885 while (i < n) : (i += 1) { 1886 switch (nums[i]) { 1887 0 => style.* = .{}, 1888 7 => style.inverted = true, 1889 27 => style.inverted = false, 1890 39 => style.fg = null, 1891 49 => style.bg = null, 1892 38, 48 => { 1893 // truecolor form: 38;2;r;g;b — anything else is ignored 1894 if (i + 4 < n and nums[i + 1] == 2) { 1895 const c = Grid.Color{ .rgb = .{ 1896 .r = @truncate(nums[i + 2]), 1897 .g = @truncate(nums[i + 3]), 1898 .b = @truncate(nums[i + 4]), 1899 } }; 1900 if (nums[i] == 38) style.fg = c else style.bg = c; 1901 i += 4; 1902 } 1903 }, 1904 else => {}, 1905 } 1906 } 1907 } 1908 }; 1909 1910 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1911 pub const AnsiBackground = struct { 1912 grid: ?Grid, 1913 child: *Widget, 1914 art: AnsiArt, 1915 session: *ui.Session, 1916 1917 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1918 var cw = child_widget; 1919 const child = allocator.create(Widget) catch |e| { 1920 cw.deinit(allocator); 1921 return e; 1922 }; 1923 child.* = cw; 1924 errdefer { 1925 child.deinit(allocator); 1926 allocator.destroy(child); 1927 } 1928 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1929 } 1930 1931 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1932 if (self.grid) |*grid| grid.deinit(); 1933 self.art.deinit(allocator); 1934 self.child.deinit(allocator); 1935 allocator.destroy(self.child); 1936 } 1937 1938 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1939 self.clearGrid(); 1940 1941 // make the wrapped page fill the whole available area 1942 try self.child.build(allocator, .{ 1943 .min_size = .{ 1944 .width = constraint.max_size.width orelse constraint.min_size.width, 1945 .height = constraint.max_size.height orelse constraint.min_size.height, 1946 }, 1947 .max_size = constraint.max_size, 1948 }, root_focus); 1949 1950 const foreground = self.child.getGrid() orelse return; 1951 if (!self.session.color) return; 1952 // terminals show the art only once they report a background to fade it toward 1953 const backdrop: ?Grid.Color.Rgb = if (self.session.is_terminal) self.session.terminal_background orelse return else null; 1954 self.art.content = self.session.data.ansi_art; 1955 try self.art.build(allocator, .{ 1956 .min_size = .{ .width = null, .height = null }, 1957 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1958 }, root_focus); 1959 // the web fades the art with css opacity instead 1960 const art_backdrop = backdrop orelse return; 1961 if (self.art.grid) |*art_grid| { 1962 for (art_grid.cells) |*cell| { 1963 cell.style.fg = fadeColor(cell.style.fg, art_backdrop); 1964 cell.style.bg = fadeColor(cell.style.bg, art_backdrop); 1965 } 1966 self.grid = try artBehind(allocator, foreground, art_grid.*); 1967 } 1968 } 1969 1970 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1971 try self.child.input(allocator, key, root_focus); 1972 } 1973 1974 pub fn clearGrid(self: *AnsiBackground) void { 1975 if (self.grid) |*grid| { 1976 grid.deinit(); 1977 self.grid = null; 1978 } 1979 self.child.clearGrid(); 1980 self.art.clearGrid(); 1981 } 1982 1983 pub fn getGrid(self: AnsiBackground) ?Grid { 1984 return self.grid orelse self.child.getGrid(); 1985 } 1986 1987 pub fn getFocus(self: *AnsiBackground) *Focus { 1988 return self.child.getFocus(); 1989 } 1990 1991 // a foreground cell counts as blank if it's empty or a bare space without styling 1992 fn cellIsBlank(cell: Grid.Cell) bool { 1993 if (cell.continuation) return false; 1994 if (cell.rune) |rune| { 1995 return rune == ' ' and 1996 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1997 } 1998 return true; 1999 } 2000 2001 // the art is faded toward the background at this opacity so foreground 2002 // text stays legible over it 2003 pub const art_opacity = 25; // percent 2004 2005 // the art parser only emits truecolor, so a palette color never occurs 2006 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 2007 const c = color orelse return null; 2008 return switch (c) { 2009 .rgb => |v| v, 2010 else => null, 2011 }; 2012 } 2013 2014 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 2015 const v = artRgb(color) orelse return color; 2016 const blend = struct { 2017 fn f(c: u8, b: u8) u8 { 2018 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 2019 } 2020 }.f; 2021 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 2022 } 2023 2024 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2025 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2026 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2027 // snap such channels clear of that range so the backdrop doesn't blink or 2028 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2029 // actually render truecolor. 2030 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2031 const v = artRgb(color) orelse return color; 2032 const snap = struct { 2033 fn f(c: u8) u8 { 2034 return if (c >= 1 and c <= 9) 10 else c; 2035 } 2036 }.f; 2037 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 2038 } 2039 2040 // composites ANSI art behind a foreground grid 2041 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 2042 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2043 errdefer out.deinit(); 2044 2045 const anchor_x = foreground.size.width -| art_grid.size.width; 2046 for (0..art_grid.size.height) |y| { 2047 for (0..art_grid.size.width) |x| { 2048 const src = (try art_grid.cell(x, y)).*; 2049 if (src.rune == null) continue; 2050 const dst = try out.cell(anchor_x + x, y); 2051 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 2052 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 2053 // blank cells take the art; occupied cells take its background 2054 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 2055 dst.style = src.style; 2056 dst.style.fg = sgrSafe(dst.style.fg); 2057 dst.style.bg = sgrSafe(dst.style.bg); 2058 try out.setRune(anchor_x + x, y, src.rune); 2059 } else { 2060 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 2061 dst.style.bg = art_bg; 2062 if (next_dst) |next| next.style.bg = art_bg; 2063 } 2064 } 2065 } 2066 return out; 2067 } 2068 }; 2069 2070 pub const CopyableText = struct { 2071 pub const Choice = struct { 2072 selector: []const u8 = "", 2073 text: []const u8, 2074 copyable_text: ?[]const u8 = null, 2075 label: []const u8 = "", 2076 bottom_label: []const u8 = "", 2077 }; 2078 2079 box: wgt.Box(Widget), 2080 session: *ui.Session, 2081 choices: []Choice, 2082 selected: usize = 0, 2083 2084 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 2085 if (choices.len == 0) return error.MissingCopyableText; 2086 2087 const owned_choices = try allocator.dupe(Choice, choices); 2088 errdefer allocator.free(owned_choices); 2089 2090 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2091 errdefer box.deinit(allocator); 2092 2093 if (choices.len > 1) for (choices) |choice| { 2094 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 2095 errdefer selector_box.deinit(allocator); 2096 selector_box.getFocus().mode = .all; 2097 try box.children.put(allocator, selector_box.getFocus().id, .{ .widget = .{ .text_box = selector_box }, .rect = null, .min_size = .{ .width = choice.selector.len + 2, .height = 3 } }); 2098 }; 2099 2100 var text_input = try wgt.TextInput.init(allocator, .{ 2101 .border = .single, 2102 .round_corners = true, 2103 .top_label = .{ .text = choices[initial].label }, 2104 .bottom_label = .{ .text = choices[initial].bottom_label }, 2105 .read_only = true, 2106 .render_content = session.is_terminal, 2107 .visible_width = null, 2108 }); 2109 errdefer text_input.deinit(allocator); 2110 text_input.getFocus().mode = .all; 2111 try text_input.setContent(allocator, choices[initial].text); 2112 box.getFocus().child_id = text_input.getFocus().id; 2113 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 2114 2115 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 2116 } 2117 2118 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 2119 const aa = session.page_arena.allocator(); 2120 var choices: [2]Choice = undefined; 2121 var count: usize = 0; 2122 var ssh_index: ?usize = null; 2123 switch (handle.location) { 2124 .repo => |identity| { 2125 const http_port = if (handle.read_access == .public) session.data.git_http_port else null; 2126 if (http_port) |port| { 2127 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 2128 choices[count] = .{ 2129 .selector = "http", 2130 .text = url, 2131 .copyable_text = try aa.print("git clone {s}", .{url}), 2132 .label = " clone ", 2133 }; 2134 count += 1; 2135 } 2136 if (session.data.git_ssh_port) |port| { 2137 ssh_index = count; 2138 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 2139 choices[count] = .{ 2140 .selector = "ssh", 2141 .text = url, 2142 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2143 .label = " clone ", 2144 }; 2145 count += 1; 2146 } 2147 }, 2148 .fork => |f| if (session.data.git_ssh_port) |port| { 2149 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 2150 choices[count] = .{ 2151 .text = url, 2152 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2153 .label = " clone ", 2154 }; 2155 count += 1; 2156 }, 2157 } 2158 if (count == 0) return null; 2159 // a viewer who can push starts on the ssh url 2160 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 2161 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 2162 } 2163 2164 pub fn minWidth(self: *const CopyableText) usize { 2165 var width = maxWidth(self.choices) + 2; 2166 if (self.choices.len > 1) { 2167 for (self.choices) |choice| width += choice.selector.len + 2; 2168 } 2169 return width; 2170 } 2171 2172 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 2173 self.box.deinit(allocator); 2174 allocator.free(self.choices); 2175 } 2176 2177 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2178 self.clearGrid(); 2179 2180 if (self.choices.len > 1) { 2181 if (root_focus.grandchild_id) |id| { 2182 for (0..self.choices.len) |i| { 2183 if (id != self.selector(i).getFocus().id) continue; 2184 if (i != self.selected) try self.select(allocator, i); 2185 root_focus.setFocus(self.textInput().getFocus().id); 2186 break; 2187 } 2188 } 2189 2190 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2191 for (0..self.choices.len) |i| { 2192 self.selector(i).options.border = if (i == self.selected) 2193 (if (focused) .double else .single) 2194 else 2195 .hidden; 2196 } 2197 } 2198 self.textInput().options.border = .single; 2199 self.textInput().cursor = 0; 2200 self.textInput().scroll_offset = 0; 2201 try self.box.build(allocator, constraint, root_focus); 2202 2203 if (!self.session.is_terminal) { 2204 const aa = self.session.arena.allocator(); 2205 const id = self.textInput().getFocus().id; 2206 try self.session.text_inputs.put(aa, id, self.textInput()); 2207 } 2208 } 2209 2210 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2211 switch (key) { 2212 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2213 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2214 .enter => if (self.session.is_terminal) self.show(), 2215 .mouse => |mouse| { 2216 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2217 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2218 try self.select(allocator, i); 2219 root_focus.setFocus(self.textInput().getFocus().id); 2220 return; 2221 }; 2222 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2223 }, 2224 else => {}, 2225 } 2226 } 2227 2228 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2229 self.selected = selected; 2230 const choice = self.choices[selected]; 2231 const text_input = self.textInput(); 2232 text_input.options.top_label.text = choice.label; 2233 text_input.options.bottom_label.text = choice.bottom_label; 2234 try text_input.setContent(allocator, choice.text); 2235 } 2236 2237 fn show(self: *CopyableText) void { 2238 const choice = self.choices[self.selected]; 2239 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2240 } 2241 2242 fn textInput(self: *CopyableText) *wgt.TextInput { 2243 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2244 } 2245 2246 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2247 return &self.box.children.values()[index].widget.text_box; 2248 } 2249 2250 fn maxWidth(choices: []const Choice) usize { 2251 var width: usize = 0; 2252 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2253 return width; 2254 } 2255 2256 pub fn clearGrid(self: *CopyableText) void { 2257 self.box.clearGrid(); 2258 } 2259 2260 pub fn getGrid(self: CopyableText) ?Grid { 2261 return self.box.getGrid(); 2262 } 2263 2264 pub fn getFocus(self: *CopyableText) *Focus { 2265 return self.box.getFocus(); 2266 } 2267 }; 2268