Forráskód Böngészése

Merge pull request #14 from lontivero/feature/clipboard

Clipboard implementation
danwalmsley 7 éve
szülő
commit
98e8f6cf12

+ 4 - 0
src/Avalonia.Native.OSX/Avalonia.Native.OSX.xcodeproj/project.pbxproj

@@ -9,6 +9,7 @@
 /* Begin PBXBuildFile section */
 		37A517B32159597E00FBA241 /* Screens.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37A517B22159597E00FBA241 /* Screens.mm */; };
 		37C09D8821580FE4006A6758 /* SystemDialogs.mm in Sources */ = {isa = PBXBuildFile; fileRef = 37C09D8721580FE4006A6758 /* SystemDialogs.mm */; };
+		5B8BD94F215BFEA6005ED2A7 /* clipboard.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5B8BD94E215BFEA6005ED2A7 /* clipboard.mm */; };
 		AB00E4F72147CA920032A60A /* main.mm in Sources */ = {isa = PBXBuildFile; fileRef = AB00E4F62147CA920032A60A /* main.mm */; };
 		AB661C1E2148230F00291242 /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB661C1D2148230F00291242 /* AppKit.framework */; };
 		AB661C202148286E00291242 /* window.mm in Sources */ = {isa = PBXBuildFile; fileRef = AB661C1F2148286E00291242 /* window.mm */; };
@@ -20,6 +21,7 @@
 		37A517B22159597E00FBA241 /* Screens.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = Screens.mm; sourceTree = "<group>"; };
 		37C09D8721580FE4006A6758 /* SystemDialogs.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SystemDialogs.mm; sourceTree = "<group>"; };
 		37C09D8A21581EF2006A6758 /* window.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = window.h; sourceTree = "<group>"; };
+		5B8BD94E215BFEA6005ED2A7 /* clipboard.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = clipboard.mm; sourceTree = "<group>"; };
 		AB00E4F62147CA920032A60A /* main.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = main.mm; sourceTree = "<group>"; };
 		AB661C1D2148230F00291242 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
 		AB661C1F2148286E00291242 /* window.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = window.mm; sourceTree = "<group>"; };
@@ -51,6 +53,7 @@
 		AB7A61E62147C814003C5833 = {
 			isa = PBXGroup;
 			children = (
+				5B8BD94E215BFEA6005ED2A7 /* clipboard.mm */,
 				379A4506214D0F6500CC143D /* headers */,
 				AB8F7D6A21482D7F0057DBA5 /* platformthreading.mm */,
 				AB661C212148288600291242 /* common.h */,
@@ -139,6 +142,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				5B8BD94F215BFEA6005ED2A7 /* clipboard.mm in Sources */,
 				AB8F7D6B21482D7F0057DBA5 /* platformthreading.mm in Sources */,
 				37A517B32159597E00FBA241 /* Screens.mm in Sources */,
 				AB00E4F72147CA920032A60A /* main.mm in Sources */,

+ 37 - 0
src/Avalonia.Native.OSX/clipboard.mm

@@ -0,0 +1,37 @@
+#include "common.h"
+
+class Clipboard : public ComSingleObject<IAvnClipboard, &IID_IAvnClipboard>
+{
+public:
+    virtual HRESULT GetText (void** retOut)
+    {
+        @autoreleasepool {
+            NSString *str = [[NSPasteboard generalPasteboard] stringForType:NSPasteboardTypeString];
+            *retOut = (void *)str.UTF8String;
+        }
+        return S_OK;
+    }
+    
+    virtual HRESULT SetText (char* text)
+    {
+        @autoreleasepool {
+            NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
+            [pasteBoard clearContents];
+            [pasteBoard setString:@(text) forType:NSPasteboardTypeString];
+        }
+        return S_OK;
+    }
+
+    virtual HRESULT Clear()
+    {
+        @autoreleasepool {
+            [[NSPasteboard generalPasteboard] clearContents];
+        }
+        return S_OK;
+    }
+};
+
+extern IAvnClipboard* CreateClipboard()
+{
+    return new Clipboard();
+}

+ 1 - 0
src/Avalonia.Native.OSX/common.h

@@ -12,6 +12,7 @@ extern IAvnWindow* CreateAvnWindow(IAvnWindowEvents*events);
 extern IAvnPopup* CreateAvnPopup(IAvnWindowEvents*events);
 extern IAvnSystemDialogs* CreateSystemDialogs();
 extern IAvnScreens* CreateScreens();
+extern IAvnClipboard* CreateClipboard();
 
 extern NSPoint ToNSPoint (AvnPoint p);
 extern AvnPoint ToAvnPoint (NSPoint p);

+ 7 - 2
src/Avalonia.Native.OSX/main.mm

@@ -101,11 +101,17 @@ public:
         return  S_OK;
     }
     
-     virtual HRESULT CreateScreens (IAvnScreens** ppv)
+    virtual HRESULT CreateScreens (IAvnScreens** ppv)
     {
         *ppv = ::CreateScreens ();
         return S_OK;
     }
+
+    virtual HRESULT CreateClipboard(IAvnClipboard** ppv)
+    {
+        *ppv = ::CreateClipboard ();
+        return S_OK;
+    }
 };
 
 extern "C" IAvaloniaNativeFactory* CreateAvaloniaNative()
@@ -113,7 +119,6 @@ extern "C" IAvaloniaNativeFactory* CreateAvaloniaNative()
     return new AvaloniaNative();
 };
 
-
 NSPoint ToNSPoint (AvnPoint p)
 {
     @autoreleasepool

BIN
src/Avalonia.Native/.Stubs.cs.swp


+ 1 - 1
src/Avalonia.Native/AvaloniaNativePlatform.cs

@@ -70,7 +70,7 @@ namespace Avalonia.Native
                 .Bind<IMouseDevice>().ToConstant(MouseDevice)
                 .Bind<IPlatformSettings>().ToConstant(this)
                 .Bind<IWindowingPlatform>().ToConstant(this)
-                .Bind<IClipboard>().ToSingleton<ClipboardImpl>()
+                .Bind<IClipboard>().ToConstant(new ClipboardImpl(_factory.CreateClipboard()))
                 .Bind<IRenderLoop>().ToConstant(new RenderLoop())
                 .Bind<IRenderTimer>().ToConstant(new DefaultRenderTimer(60))
                 .Bind<ISystemDialogImpl>().ToConstant(new SystemDialogs(_factory.CreateSystemDialogs()))

+ 1 - 0
src/Avalonia.Native/Mappings.xml

@@ -23,5 +23,6 @@
         <map interface="*.Callback" callback="true" autogen-shadow="true"/>
         <map param=".*::.*::ppv" return="true"/>
         <map param=".*::.*::ret" return="true"/>
+        <map param=".*::.*::retOut" attribute="out" return="true"/>
     </mapping>
 </config>

+ 16 - 1
src/Avalonia.Native/Stubs.cs

@@ -1,27 +1,42 @@
 using System;
 using System.IO;
 using System.Threading.Tasks;
+using System.Runtime.InteropServices;
 using Avalonia.Controls;
 using Avalonia.Controls.Platform;
 using Avalonia.Input.Platform;
 using Avalonia.Platform;
+using Avalonia.Native.Interop;
 
 namespace Avalonia.Native
 { 
     class ClipboardImpl : IClipboard
     {
+        IAvnClipboard _native;
+
+        public ClipboardImpl(IAvnClipboard native)
+        {
+            _native = native;
+        }
+
         public Task ClearAsync()
         {
+            _native.Clear();
             return Task.CompletedTask;
         }
 
         public Task<string> GetTextAsync()
         {
-            return Task.FromResult<string>(null);
+            var outPtr = _native.GetText();
+            var text = Marshal.PtrToStringAnsi(outPtr);
+            return Task.FromResult(text);
         }
 
         public Task SetTextAsync(string text)
         {
+            _native.Clear();
+            if(text != null)
+                _native.SetText(text);
             return Task.CompletedTask;
         }
     }

+ 9 - 1
src/headers/avalonia-native.h

@@ -10,6 +10,7 @@ struct IAvnPlatformThreadingInterface;
 struct IAvnSystemDialogEvents;
 struct IAvnSystemDialogs;
 struct IAvnScreens;
+struct IAvnClipboard;
 
 struct AvnSize
 {
@@ -91,6 +92,7 @@ public:
     virtual HRESULT CreatePlatformThreadingInterface(IAvnPlatformThreadingInterface** ppv) = 0;
     virtual HRESULT CreateSystemDialogs (IAvnSystemDialogs** ppv) = 0;
     virtual HRESULT CreateScreens (IAvnScreens** ppv) = 0;
+    virtual HRESULT CreateClipboard(IAvnClipboard** ppv) = 0;
 };
 
 AVNCOM(IAvnWindowBase, 02) : virtual IUnknown
@@ -209,7 +211,13 @@ AVNCOM(IAvnScreens, 0e) : virtual IUnknown
 {
     virtual HRESULT GetScreenCount (int* ret) = 0;
     virtual HRESULT GetScreen (int index, AvnScreen* ret) = 0;
-    
+};
+
+AVNCOM(IAvnClipboard, 0f) : virtual IUnknown
+{
+    virtual HRESULT GetText (void** retOut) = 0;
+    virtual HRESULT SetText (char* text) = 0;
+    virtual HRESULT Clear() = 0;
 };
 
 extern "C" IAvaloniaNativeFactory* CreateAvaloniaNative();