/*
Crafter®.Graphics
Copyright (C) 2026 Catcrafts®
Catcrafts.net

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
*/
export module Crafter.Graphics:Clipboard;
import std;

// 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
    // (Wayland needs a recent serial), or the OS API failed. On
    // success the ownership of the clipboard contents is held until
    // 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();
}
