dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.04k stars 1.73k forks source link

Extend DataPackage API for non-textual Clipboard and Drag contents #3856

Open bwoebi opened 2 years ago

bwoebi commented 2 years ago

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.

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

Dave-Quick commented 3 months ago

I could use this.