Skip to content

Unified, cross-platform Clipboard type #29433

Closed
@just-ero

Description

@just-ero

Description

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.

Public API Changes

namespace Microsoft.Maui;

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);
}

Intended Use-Case

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions