Ejemplo n.º 1
0
        public void PasteImage(NSObject sender)
        {
            // Initialize the pasteboard
            NSPasteboard pasteboard = NSPasteboard.GeneralPasteboard;

            Class [] classArray = { new Class("NSImage") };

            bool ok = pasteboard.CanReadObjectForClasses(classArray, null);

            if (ok)
            {
                // Read the image off of the pasteboard
                NSObject [] objectsToPaste = pasteboard.ReadObjectsForClasses(classArray, null);
                NSImage     image          = (NSImage)objectsToPaste[0];

                // Display the new image
                ImageView.Image = image;
            }

            Class [] classArray2 = { new Class("ImageInfo") };
            ok = pasteboard.CanReadObjectForClasses(classArray2, null);
            if (ok)
            {
                // Read the image off of the pasteboard
                NSObject[] objectsToPaste = pasteboard.ReadObjectsForClasses(classArray2, null);
                if (objectsToPaste.Length > 0)
                {
                    ImageInfo info = (ImageInfo)objectsToPaste[0];
                }
            }
        }
Ejemplo n.º 2
0
        public void PasteImage(NSObject sender)
        {
            // Initialize the pasteboard
            NSPasteboard pasteboard = NSPasteboard.GeneralPasteboard;

            Class [] classArray = { new Class("NSImage") };

            // NOTE: Sending messages directly to the base Objective-C API because of this defect:
            // https://bugzilla.xamarin.com/show_bug.cgi?id=31760
            // Check to see if an image is on the pasteboard
            bool ok = pasteboard.CanReadObjectForClasses(classArray, null);

            if (ok)
            {
                // Read the image off of the pasteboard
                NSObject [] objectsToPaste = pasteboard.ReadObjectsForClasses(classArray, null);
                NSImage     image          = (NSImage)objectsToPaste[0];

                // Display the new image
                ImageView.Image = image;
            }

            Class [] classArray2 = { new Class("ImageInfo") };
            ok = pasteboard.CanReadObjectForClasses(classArray2, null);
            if (ok)
            {
                // Read the image off of the pasteboard
                NSObject [] objectsToPaste = pasteboard.ReadObjectsForClasses(classArray2, null);
                ImageInfo   info           = (ImageInfo)objectsToPaste[0];
            }
        }
        private bool HasUrlOnPasteboard()
        {
            NSPasteboard pasteboard = NSPasteboard.GeneralPasteboard;
            var          classArray = new[] { new Class(typeof(NSUrl)) };

            return(pasteboard.CanReadObjectForClasses(classArray, null));
        }
Ejemplo n.º 4
0
        private IntPtr getFromPasteboard(IntPtr @class)
        {
            NSArray classArray = NSArray.ArrayWithObject(@class);

            if (!generalPasteboard.CanReadObjectForClasses(classArray, null))
            {
                return(IntPtr.Zero);
            }

            var result  = generalPasteboard.ReadObjectsForClasses(classArray, null);
            var objects = result?.ToArray();

            return(objects?.Length > 0 ? objects[0] : IntPtr.Zero);
        }
Ejemplo n.º 5
0
        public override string GetText()
        {
            NSArray classArray = NSArray.ArrayWithObject(Class.Get("NSString"));

            if (GeneralPasteboard.CanReadObjectForClasses(classArray, null))
            {
                var result  = GeneralPasteboard.ReadObjectsForClasses(classArray, null);
                var objects = result?.ToArray() ?? new IntPtr[0];
                if (objects.Length > 0 && objects[0] != IntPtr.Zero)
                {
                    return(Cocoa.FromNSString(objects[0]));
                }
            }
            return(string.Empty);
        }
Ejemplo n.º 6
0
        public void Paste(NSObject sender)
        {
            NSPasteboard pasteboard = NSPasteboard.GeneralPasteboard;
            var          classArray = new[] { new Class(typeof(NSUrl)) };

            bool validContent = pasteboard.CanReadObjectForClasses(classArray, null);

            if (validContent)
            {
                NSObject[] copiedItems = pasteboard.ReadObjectsForClasses(classArray, null);
                NSUrl      url         = (NSUrl)copiedItems[0];

                if (LottieFilesUrl.IsValidUrl(url))
                {
                    LottieFilesUrl lottieUrl = new LottieFilesUrl(url);
                    this.View.Window.Title = lottieUrl.AnimationName;
                    this.lottieView.OpenAnimationUrl(lottieUrl.JsonUrl);
                }
            }
        }
Ejemplo n.º 7
0
        private static DataPackageView GetFromNative(NSPasteboard pasteboard)
        {
            if (pasteboard is null)
            {
                throw new ArgumentException(nameof(pasteboard));
            }

            var dataPackage = new DataPackage();

            // Extract all the standard data format information from the pasteboard items.
            // Each format can only be used once; therefore, generally the last occurrence of the format will be the one used.
            foreach (NSPasteboardItem item in pasteboard.PasteboardItems)
            {
                if (item.Types.Contains(NSPasteboard.NSPasteboardTypeTIFF) ||
                    item.Types.Contains(NSPasteboard.NSPasteboardTypePNG))
                {
                    // Images may be very large, we never want to load them until they are needed.
                    // Therefore, create a data provider used to asynchronously fetch the image.
                    dataPackage.SetDataProvider(
                        StandardDataFormats.Bitmap,
                        async cancellationToken =>
                    {
                        NSImage?image = null;

                        /* Some apps, including Photos, don't appear to put image data in the pasteboard.
                         * Instead, the image URL is provided although the type indicates it is an image.
                         *
                         * To get around this an image is read as follows:
                         *   (1) If the pasteboard contains an image type then:
                         *   (2) Attempt to read the image as an object (NSImage).
                         *       This may fail as some tested apps provide a URL although declare an image (Photos).
                         *       With other apps (such as web browsers) an image will be read correctly here.
                         *   (3) If reading as an NSImage object fails, attempt to read the image from a file URL (local images)
                         *   (4) If reading from a file URL fails, attempt to read the image from a URL (remote images)
                         *
                         * Reading as an NSImage object follows the docs here:
                         *   https://docs.microsoft.com/en-us/xamarin/mac/app-fundamentals/copy-paste#add-an-nsdocument
                         */

                        var classArray = new Class[] { new Class("NSImage") };
                        if (pasteboard.CanReadObjectForClasses(classArray, null))
                        {
                            NSObject[] objects = pasteboard.ReadObjectsForClasses(classArray, null);

                            if (objects.Length > 0)
                            {
                                // Only use the first image found
                                image = objects[0] as NSImage;
                            }
                        }

                        // In order to get here the pasteboard must have declared it had image types.
                        // However, if image is null, no objects were found and the image is likely a URL instead.
                        if (image == null &&
                            item.Types.Contains(NSPasteboard.NSPasteboardTypeFileUrl))
                        {
                            var url = item.GetStringForType(NSPasteboard.NSPasteboardTypeFileUrl);
                            image   = new NSImage(new NSUrl(url));
                        }

                        if (image == null &&
                            item.Types.Contains(NSPasteboard.NSPasteboardTypeUrl))
                        {
                            var url = item.GetStringForType(NSPasteboard.NSPasteboardTypeUrl);
                            image   = new NSImage(new NSUrl(url));
                        }

                        if (image != null)
                        {
                            // Thanks to: https://stackoverflow.com/questions/13305028/monomac-best-way-to-convert-bitmap-to-nsimage/13355747
                            using (var imageData = image.AsTiff())
                            {
                                var imgRep = NSBitmapImageRep.ImageRepFromData(imageData !) as NSBitmapImageRep;
                                var data   = imgRep !.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png, null);

                                return(new RandomAccessStreamReference(async ct =>
                                {
                                    return data.AsStream().AsRandomAccessStream().TrySetContentType("image/png");
                                }));
                            }
                        }
                        else
                        {
                            // Return an empty image
                            return(new RandomAccessStreamReference(async ct =>
                            {
                                var stream = new MemoryStream();
                                stream.Position = 0;

                                return stream.AsRandomAccessStream().TrySetContentType("image/png");
                            }));
                        }
                    });
                }

                if (item.Types.Contains(NSPasteboard.NSPasteboardTypeHTML))
                {
                    var html = item.GetStringForType(NSPasteboard.NSPasteboardTypeHTML);
                    if (html != null)
                    {
                        dataPackage.SetHtmlFormat(html);
                    }
                }

                if (item.Types.Contains(NSPasteboard.NSPasteboardTypeRTF))
                {
                    var rtf = item.GetStringForType(NSPasteboard.NSPasteboardTypeRTF);
                    if (rtf != null)
                    {
                        dataPackage.SetRtf(rtf);
                    }
                }

                if (item.Types.Contains(NSPasteboard.NSPasteboardTypeFileUrl))
                {
                    // Drag and drop will use temporary URLs similar to: file:///.file/id=1234567.1234567
                    var tempFileUrl = item.GetStringForType(NSPasteboard.NSPasteboardTypeFileUrl);

                    // Files may be very large, we never want to load them until they are needed.
                    // Therefore, create a data provider used to asynchronously fetch the file.
                    dataPackage.SetDataProvider(
                        StandardDataFormats.StorageItems,
                        async cancellationToken =>
                    {
                        // Convert from a temp Url (see above example) into an absolute file path
                        var fileUrl = new NSUrl(tempFileUrl);
                        var file    = await StorageFile.GetFileFromPathAsync(fileUrl.FilePathUrl.AbsoluteString);

                        var storageItems = new List <IStorageItem>();
                        storageItems.Add(file);

                        return(storageItems.AsReadOnly());
                    });
                }

                if (item.Types.Contains(NSPasteboard.NSPasteboardTypeString))
                {
                    var text = item.GetStringForType(NSPasteboard.NSPasteboardTypeString);
                    if (text != null)
                    {
                        dataPackage.SetText(text);
                    }
                }

                if (item.Types.Contains(NSPasteboard.NSPasteboardTypeUrl))
                {
                    var url = item.GetStringForType(NSPasteboard.NSPasteboardTypeUrl);
                    if (url != null)
                    {
                        DataPackage.SeparateUri(
                            url,
                            out string?webLink,
                            out string?applicationLink);

                        if (webLink != null)
                        {
                            dataPackage.SetWebLink(new Uri(webLink));
                        }

                        if (applicationLink != null)
                        {
                            dataPackage.SetApplicationLink(new Uri(applicationLink));
                        }

                        // Deprecated but still added for compatibility
                        dataPackage.SetUri(new Uri(url));
                    }
                }
            }

            return(dataPackage.GetView());
        }