IDesktopClipboard interface

Namespace Runic.Platform in Runic.Platform 0.23.0

public interface IDesktopClipboard : ITextClipboard

Inherits or implements ITextClipboard.

The window's clipboard with text, HTML, Rich Text Format, PNG images and file lists, and a watch of its formats.

Remarks

Every window clipboard of Runic implements this interface: take it in a ViewModel's constructor, or test IWindowPlatformServices.Clipboard with as IDesktopClipboard. A custom ITextClipboard stays text-only. Each operation runs on the window's native thread, as the text operations do; before the window opens it reports PlatformUnavailableReason.OwnerUnavailable, and once it closed PlatformUnavailableReason.OwnerClosed.

A read returns only the one format it names, bounded like ITextClipboard.ReadTextAsync: more than the bound reports PlatformFailureCode.TooLarge. A successful null, or an empty list for files, means the clipboard has no such format. The capabilities are PlatformCapabilityIds.ClipboardReadData, PlatformCapabilityIds.ClipboardWriteData and PlatformCapabilityIds.ClipboardWatch.

This is C# code's clipboard, for a ViewModel or background work such as copying an export. A page can still use the browser's clipboard, under the web view's own rules.

Methods

GetFormatsAsync

ValueTask<PlatformResult<DesktopClipboardFormats>> GetFormatsAsync(CancellationToken cancellationToken = default)

Reads which formats the clipboard has, without reading any of them.

Parameters

cancellationToken

Cancels before the request reaches the window.

Returns

The formats; DesktopClipboardFormats.None for an empty clipboard or one with only formats Runic does not read.

ReadFilesAsync

ValueTask<PlatformResult<ImmutableArray<string>>> ReadFilesAsync(int maximumCount, CancellationToken cancellationToken = default)

Reads the clipboard's file list as fully qualified local paths; an empty list means no files.

Parameters

maximumCount

The most paths to return; more reports PlatformFailureCode.TooLarge.

cancellationToken

Cancels before the request reaches the window.

Exceptions

ArgumentOutOfRangeException

maximumCount is negative.

Remarks

Entries that are not local files, such as web URIs, are skipped.

ReadHtmlAsync

ValueTask<PlatformResult<string?>> ReadHtmlAsync(int maximumCharacters, CancellationToken cancellationToken = default)

Reads bounded HTML; a successful null means no HTML.

Parameters

maximumCharacters

The most characters to return; more reports PlatformFailureCode.TooLarge.

cancellationToken

Cancels before the request reaches the window.

Exceptions

ArgumentOutOfRangeException

maximumCharacters is negative.

Remarks

On Windows the result is the copied fragment of the CF_HTML document; elsewhere the HTML as offered.

ReadImageAsync

ValueTask<PlatformResult<byte[]?>> ReadImageAsync(int maximumBytes, CancellationToken cancellationToken = default)

Reads the clipboard's image as PNG bytes; a successful null means no image.

Parameters

maximumBytes

The most PNG bytes to return; more reports PlatformFailureCode.TooLarge.

cancellationToken

Cancels before the request reaches the window.

Exceptions

ArgumentOutOfRangeException

maximumBytes is negative.

Remarks

An image that another application offers in another format, such as a bitmap on Windows or TIFF on macOS, is converted to PNG.

ReadRtfAsync

ValueTask<PlatformResult<string?>> ReadRtfAsync(int maximumCharacters, CancellationToken cancellationToken = default)

Reads bounded Rich Text Format source; a successful null means no RTF.

Parameters

maximumCharacters

The most characters, which are bytes of RTF, to return; more reports PlatformFailureCode.TooLarge.

cancellationToken

Cancels before the request reaches the window.

Exceptions

ArgumentOutOfRangeException

maximumCharacters is negative.

Remarks

Each byte of the RTF becomes one character (Latin-1), as DesktopClipboardContent.Rtf describes.

WatchAsync

IAsyncEnumerable<PlatformResult<DesktopClipboardFormats>> WatchAsync(CancellationToken cancellationToken = default)

Reports the clipboard's formats now, then after each change; never the content.

Parameters

cancellationToken

Ends the watch, which then throws OperationCanceledException.

Remarks

Every change is reported, also one that leaves the same formats, such as other copied text; changes close together may arrive as one. Read the content when the user pastes, not on every change: other applications' content is theirs. When the window closes the watch reports PlatformUnavailableReason.OwnerClosed and ends; a window that cannot watch reports Unavailable once and ends.

macOS has no change notification: the watch compares the pasteboard's change count four times a second. On Wayland the compositor tells only a focused application about the clipboard, so changes arrive when a window of the application gains focus.

WriteAsync

ValueTask<PlatformResult<PlatformUnit>> WriteAsync(DesktopClipboardContent content, CancellationToken cancellationToken = default)

Replaces the clipboard with every format of content, in one ownership.

Parameters

content

The formats to offer.

cancellationToken

Prevents queued work; an initiated native write returns its actual outcome.

Returns

Success once the clipboard offers the content, or Failed with PlatformFailureCode.InvalidData or PlatformFailureCode.NotFound for content that breaks the rules of DesktopClipboardContent, before anything changed.

Exceptions

ArgumentException

content sets no format, or DesktopClipboardContent.Files has more than 4096 entries or an entry that is null, empty or not fully qualified.

ArgumentNullException

content is null.