clipboard

This commit is contained in:
Jorijn van der Graaf 2026-05-19 00:45:22 +02:00
commit 850ef7bfb3
4 changed files with 325 additions and 10 deletions

View file

@ -20,17 +20,20 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
export module Crafter.Graphics:Clipboard;
import std;
// Native system-clipboard writes. No popen, no helper binaries — just
// the platform's own clipboard API. Implementation lives next to the
// other window-backend code (Wayland data_device on Linux, Win32 in
// the Windows build); callers don't pick a backend.
//
// `Get` is intentionally not exposed yet: paste-from-clipboard isn't
// a feature the game's UI wants right now, and the read path needs
// more lifecycle plumbing (mime negotiation, fd reads on the Wayland
// event loop) than the simple write path. Easy to add later.
// Native system-clipboard reads and writes. No popen, no helper
// binaries — just the platform's own clipboard API. Implementation
// lives next to the other window-backend code (Wayland data_device on
// Linux, Win32 in the Windows build, navigator.clipboard +
// `paste` event in the DOM build); callers don't pick a backend.
export namespace Crafter::Clipboard {
// One-time backend setup. Currently only the Wayland path uses it
// (attaches a wl_data_device listener so we observe selection
// offers from other apps). Win32 and the DOM build are no-ops.
// Called from Device::Initialize after the Wayland registry
// roundtrip — calling it again is harmless.
void Initialize();
// Place `text` on the system clipboard as UTF-8 plain text. Returns
// true if the platform accepted the request — false means the
// backend isn't initialised, no input event has been seen yet
@ -39,4 +42,15 @@ export namespace Crafter::Clipboard {
// either another app replaces the selection or the application
// exits; the caller doesn't need to keep `text` alive.
bool SetText(std::string_view text);
// Read the current clipboard contents as UTF-8 plain text. Returns
// nullopt if the clipboard is empty, holds no text-typed payload,
// the platform read failed, or — on the DOM build — the latched
// buffer is empty because the user hasn't pasted yet and
// navigator.clipboard.readText() has not resolved. The Wayland
// path negotiates one of the standard text mime types in order
// (text/plain;charset=utf-8, text/plain, UTF8_STRING, TEXT) and
// pumps the wl_display event loop while reading so the call also
// works when the source of the selection is our own process.
std::optional<std::string> GetText();
}