dotnet / dotnet/maui

Extend DataPackage API for non-textual Clipboard and Drag contents

Open
#3,856 4 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area-essentials proposal/open t/enhancement ☀️
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 14h
Merged PRs (30d)
296

Description

Description

Generally, everything which can be present in a clipboard also can be in drag&drop, so I would propose to also directly expose Microsoft.Maui.Controls.DataPackage on Microsoft.Maui.Essentials.Clipboard.

Obviously, this includes being able to fetch images and also possibly files (and maybe rich text?) from the clipboard and the drag&drop interface.
As far as I saw in the code, the current drag&drop has ImageSource in its package, but that's only image sources moved around from within the local application.

While drag&drop (from external applications) is not so relevant on mobile, given that MAUI supports MacCatalyst, it would be nice to have a common API for at least Windows&Mac.
For the clipboard, it is relevant on all targets.

As a bonus, it would be great to be able to fetch the raw bytes from the ImageSource. (i.e. to actually read and process the incoming data). [Maybe this should be opened as a different feature request?]

Public API Changes

Note that currently DataPackage has no Is*properties. I think it's a good idea to do that rather than distinction via null checks (?).

DataPackage data = Clipboard.Data;
if (data.IsImage) {
  MyImageControl.Source = data.Image;
  MyImageControl.IsVisible = true;
  ImageData imageData = await data.Image.AsDataAsync(); // to allow the source to be properly loaded in general
  File.WriteAllBytes("myimage.png", imageData.PngBytes); // similarly, JpegBytes, GifBytes etc.
  // or maybe save as the originally copied format (where Format property is some enum { jpg, gif, bmp, ... } etc.)
  File.WriteAllBytes($"myimage.{imageData.Format}", data.Image.RawBytes);
} else if (data.IsFiles) {
  IEnumerable<FileResult> file = data.Files;
  // consume like if returned by a FilePicker, we'll just use Microsoft.Maui.Essentials.FileResult here
} else if (data.IsText) {
  string text = data.Text;
  // this API already exists :-)
}
Intended Use-Case

I generally need to have access to the commonly available contents of a clipboard & drag&drop. Especially raw clipboard image contents and files to process these, without going through the platform specific APIs for a streamlined coding experience.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing Clipboard, drag&drop, and DataPackage APIs, including how ImageSource is currently exposed. Define a common cross-platform API for non-text clipboard and drag contents, then verify that images and files can be consumed consistently across the supported desktop and mobile targets.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.