Skip to content

[API Proposal]: Unified, cross-platform Clipboard type #115041

@just-ero

Description

@just-ero

Background and motivation

Currently, there exist the System.Windows.Clipboard and System.Windows.Forms.Clipboard types, which, of course, are only available on Windows. They also require enabling the UseWpf or UseWindowsForms MSBuild properties in the project.
A single cross-platform equivalent in the base library would introduce the ability to get and set the data in the user's clipboard regardless of the type of app being written and regardless of the OS being targeted.

I don't know the intricacies of clipboards on other operating systems, so the implementation may be a lot more daunting than I anticipate. It may also be relevant to know what versions of a certain OS support which clipboard features.

API Proposal

namespace System;

public static class Clipboard
{
    public static void Clear();

    public static T Get<T>() where T : unmanaged;
    public static void Set<T>(T value) where T : unmanaged;

    public static byte[] GetData();
    public static void SetData(ReadOnlySpan<byte> data);

    public static Stream GetDataStream();
    public static void SetDataStream(Stream dataStream);

    public static string GetString();
    public static void SetString(string value);

    public static byte[] GetAudio();
    public static void SetAudio(ReadOnlySpan<byte> audio);

    public static Stream GetAudioStream();
    public static void SetAudioStream(Stream audioStream);

    public static byte[] GetImage();
    public static void SetImage(ReadOnlySpan<byte> image);

    public static Stream GetImageStream();
    public static void SetImageStream(Stream imageStream);

    public static string GetFile();
    public static void SetFile(string path);

    public static Stream GetFileStream();
    public static void SetFileStream(Stream fileStream);

    public static string[] GetFiles();
    public static void SetFiles(ReadOnlySpan<string> paths);
}

API Usage

Clipboard.SetString("Hello, world!");
string str = Clipboard.GetString();

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationneeds-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions