Skip to content

Clipboard: no way to read or write every representation it holds, so an app cannot save the user's clipboard and put it back #264

Open

Description

@houko

Context

The clipboard API on Scope covers three kinds of content, and each kind is read and written on its own. As of aadbe7e:

MyGo's Clipboard.Read() / Clipboard.Write(data) copy every representation and write them back, and ReadFormat reads one by name. The roadmap already ports other parts of MyGo's clipboard.go (the MathML row, docs/roadmap.md:139), but has no row for this.

Why it matters

Some desktop apps have to use the clipboard for a moment and then give the user's content back:

  • A dictionary or translation app that looks up the selected text in another app. When Accessibility gives no selected text, the usual fallback is to save the clipboard, send Copy, read the text, and restore the clipboard. The app can do the Accessibility and keystroke parts itself, but the restore is NSGo's job, and NSGo can only put back plain text. If the user had rich text, HTML, files from Finder or Explorer, or an app-specific type on the clipboard, it is lost.
  • Clipboard managers and "paste as" tools need to read every representation and write several together. Today SetClipboard and SetClipboardImage each clear the clipboard, so an app cannot even write text and an image in one item.
  • Without a change count, an app cannot tell whether a Copy it sent did anything. It has to compare text, which fails when the same text was copied twice.

Today an app has to bypass NSGo and drive NSPasteboard through purego and the Win32 clipboard itself. On Windows that also needs its own message-only window, because OpenClipboard(NULL) followed by EmptyClipboard makes SetClipboardData fail, and NSGo does not expose its HWND.

Minimal repro:

// Before this runs, the user copies a formatted paragraph in TextEdit or Word
// (RTF + HTML + plain text), or a file in Finder / Explorer.
cx.ReadClipboard(func(prev string) {
	cx.SetClipboard("temporary")
	// ... use the clipboard ...
	cx.SetClipboard(prev)
	// Paste in the original app: only plain text comes back. The RTF, HTML,
	// file URLs and any custom types are gone, and if the user had copied
	// only a file, prev was "" and the clipboard is now empty text.
})

Proposal

  1. Scope.ReadClipboardItems(done func(items []ClipboardItem, err error)), where each ClipboardItem holds its representations as (type string, data []byte) pairs: UTIs on macOS and iOS, format names on Windows (registered names, or the standard CF_* names for the predefined formats), MIME types on Linux and the web. Use the same size cap as MaxClipboardImage and fail with CodeQuotaExceeded when it is exceeded.
  2. Scope.WriteClipboardItems(items []ClipboardItem), which clears the clipboard once and writes every representation (macOS writeObjects: with NSPasteboardItem, Windows one OpenClipboard with SetClipboardData per format, Linux the offered targets). Writing back what ReadClipboardItems returned restores the clipboard. An empty slice clears it.
  3. Scope.ClipboardChangeCount(done func(int64)), from changeCount on macOS/iOS and GetClipboardSequenceNumber on Windows, and a counter on platforms that only give change events.
  4. Platforms that cannot do this (iOS paste notice rules, the web's async clipboard permissions, tvOS and watchOS) answer ErrUnsupported or their platform's refusal, as the image reads do.
  5. Windows formats that are GDI handles rather than HGLOBAL (CF_BITMAP, CF_ENHMETAFILE, CF_PALETTE) are converted (CF_DIB, CF_ENHMETAFILE bytes) or left out, and the docs say which.
  6. nsgotest models the items and the change count, so apps can test a save-and-restore without a real clipboard.

Acceptance

  • On macOS and Windows, an app that calls ReadClipboardItems, then SetClipboard("x"), then WriteClipboardItems with what it read, leaves the clipboard as it was. Rich text from TextEdit or Word pastes with its formatting, and a file copied in Finder or Explorer pastes as the file.
  • ClipboardChangeCount goes up after another app copies, and also after the app's own SetClipboard.
  • docs/architecture/services.md and docs/public/*/services.md describe the new calls and what each platform keeps.
  • An nsgotest test covers the save, overwrite and restore round trip.

Activity

  1. self-assigned this
    on Nov 20, 2026
  2. houko commented on Nov 20, 2026

    @houko
    CollaboratorAuthor

    I audited #264 against the latest origin/main. The public API still exposes separate text, image, and file clipboard operations only. There is no representation listing/read-write API, multi-format write, change count, or nsgotest model for preserving arbitrary clipboard data.

    A complete implementation requires a coordinated driver protocol and native format handling on macOS, Windows, Linux, and the web, including ownership and size limits for platform-specific clipboard handles. I did not submit a partial text/image extension that would still lose rich text, custom formats, or files during save-and-restore. The issue remains open for that cross-platform clipboard representation work.

  3. Heyana commented on Nov 21, 2026

    @Heyana
    Contributor

    Adding a clipboard-manager / history-watcher use case here as well, since it directly overlaps with ClipboardChangeCount and reading clipboard formats in this issue:

    1. Change count before reading content: a background history watcher polls ClipboardChangeCount and only reads clipboard content when the count changes.
    2. Privacy / password-manager markers: before reading text/images/files, a watcher inspects offered types/formats to skip concealed or transient items:
      • macOS: org.nspasteboard.TransientType, org.nspasteboard.AutoGeneratedType, org.nspasteboard.ConcealedType, com.agilebits.onepassword.
      • Windows: Windows has no nspasteboard.org UTIs; password managers and Windows Clipboard History look at registered format names (>= 0xC000), notably "Clipboard Viewer Ignore", "ExcludeClipboardContentFromMonitorProcessing", and "CanIncludeInClipboardHistory" (where a DWORD value of 0 forbids keeping it, while a non-zero DWORD explicitly allows keeping it — so returning raw format names alone without either exposing the data or noting CanIncludeInClipboardHistory's value can misclassify allowed copies as private).

    Hope this helps shape the cross-platform format/representation design in #264!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions