Clipboard

backend.Clipboard (Class)
Implements: spec.Clipboard

Clay backend implementation for system clipboard operations.

This class provides cross-platform clipboard text access with support for:

  • Native SDL clipboard on desktop platforms
  • Electron clipboard API when running in Electron
  • Browser clipboard API as a fallback on web
  • Internal text storage as a last resort

Platform-specific behavior:

  • Desktop (SDL): Direct system clipboard access
  • Electron: Uses Electron's clipboard module with retry logic
  • Web Browser: Uses navigator.clipboard API with permission handling
  • Fallback: Internal string storage when system access unavailable

The browser clipboard implementation requires user permission and automatically syncs when the window gains focus.

See: spec.Clipboard The interface this class implements

Instance Members

clay
bindBrowserClipboard(): Void

Initializes browser clipboard API access. Sets up automatic clipboard syncing when the window gains focus to detect external clipboard changes.


clay
getText(): String

Gets the current clipboard text content.

Attempts to read from the system clipboard in this order:

  1. Electron clipboard (if available)
  2. SDL system clipboard (on desktop)
  3. Internal clipboard storage (fallback)
Returns Description
String The clipboard text content, or null if empty

clay
setText(text: String): Void

Sets the clipboard text content.

Writes to all available clipboard targets:

  • Internal storage (always)
  • Electron clipboard (if available, with retry)
  • Browser clipboard (if permissions granted)
  • SDL system clipboard (on desktop)

The Electron implementation includes a 100ms delayed retry to work around timing issues with clipboard access.

Name Type Description
text String The text to copy to the clipboard

clay
new(): Void

Creates a new clipboard handler. Automatically detects and initializes the appropriate clipboard API based on the platform and available features.

Private Members

clay
clipboardText: String

Internal clipboard text storage used as fallback


clay
didLogBrowserClipboardReadWarning: Bool

Whether we've already logged a read permission warning


clay
didLogBrowserClipboardWriteWarning: Bool

Whether we've already logged a write permission warning


clay
didBindBrowserClipboard: Bool

Whether browser clipboard API has been initialized


clay
readBrowserClipboard(): Void

Attempts to read text from the browser clipboard. This may fail due to permissions or browser security policies. Failures are logged once to avoid console spam.


clay
writeBrowserClipboard(text: String): Void

Attempts to write text to the browser clipboard. This may fail due to permissions or browser security policies. Failures are logged once to avoid console spam.

Name Type Description
text String The text to write to the clipboard