Welcome to mirror list, hosted at ThFree Co, Russian Federation.

System.Windows.Clipboard.cs « PresentationCore « FPF « Editor « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f45967c7cfc89b09d184b955da847ca3d65d78dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using AppKit;
using Foundation;

namespace System.Windows
{
    public static class Clipboard
    {
        static readonly NSPasteboard pasteboard = NSPasteboard.GeneralPasteboard;
        static readonly string[] textTypes = { DataFormats.UnicodeText };

        public static bool ContainsText()
            => pasteboard.CanReadItemWithDataConformingToTypes(textTypes);

        public static void SetDataObject(object data, bool copy)
        {
            if (data is DataObject dataObject)
            {
                pasteboard.ClearContents();

                foreach (var item in dataObject.Items)
                {
                    switch (item.Data)
                    {
                        case string stringData:
                            pasteboard.SetStringForType(
                                stringData,
                                item.Format);
                            break;
                        case bool boolItem:
                            pasteboard.SetDataForType(
                                NSData.FromArray(new byte[] { boolItem ? (byte)1 : (byte)0 }),
                                item.Format);
                            break;
                    }
                }
            }
        }

        public static IDataObject GetDataObject()
        {
            var dataObject = new DataObject ();
            // Beside copying and pasting UnicodeText to/from pasteboard
            // editor inserts booleans like "VisualStudioEditorOperationsLineCutCopyClipboardTag"
            // which allows editor to know whole line was copied into pasteboard so on paste
            // it inserts line into new line, so we enumerate over all types and if length == 1
            // we just assume it's boolean we set in method above
            foreach (var type in pasteboard.Types)
            {
                if (type == DataFormats.UnicodeText)
                {
                    dataObject.SetText (pasteboard.GetStringForType (type));
                    continue;
                }
                var data = pasteboard.GetDataForType (type);
                if (data != null && data.Length == 1)
                {
                    dataObject.SetData (type, data: data [0] != 0);
                }
            }
            return dataObject;
        }
    }
}