11 changed files with 239 additions and 3 deletions
@ -0,0 +1,40 @@ |
|||||
|
namespace Perspex.Gtk |
||||
|
{ |
||||
|
using Gdk; |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Perspex.Input.Platform; |
||||
|
using Gtk = global::Gtk; |
||||
|
class ClipboardImpl : IClipboard |
||||
|
{ |
||||
|
private static Gtk.Clipboard GetClipboard() => Gtk.Clipboard.GetForDisplay(Gdk.Display.Default, new Atom(IntPtr.Zero)); |
||||
|
|
||||
|
public Task<string> GetTextAsync() |
||||
|
{ |
||||
|
var clip = GetClipboard(); |
||||
|
var tcs = new TaskCompletionSource<string>(); |
||||
|
clip.RequestText((_, text) => |
||||
|
{ |
||||
|
tcs.TrySetResult(text); |
||||
|
}); |
||||
|
return tcs.Task; |
||||
|
} |
||||
|
|
||||
|
public Task SetTextAsync(string text) |
||||
|
{ |
||||
|
using (var cl = GetClipboard()) |
||||
|
cl.Text = text; |
||||
|
return Task.FromResult(0); |
||||
|
} |
||||
|
|
||||
|
public Task ClearAsync() |
||||
|
{ |
||||
|
using (var cl = GetClipboard()) |
||||
|
cl.Clear(); |
||||
|
return Task.FromResult(0); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
namespace Perspex.Input.Platform |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
public interface IClipboard |
||||
|
{ |
||||
|
Task<string> GetTextAsync(); |
||||
|
|
||||
|
Task SetTextAsync(string text); |
||||
|
|
||||
|
Task ClearAsync(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,81 @@ |
|||||
|
namespace Perspex.Win32 |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Perspex.Input.Platform; |
||||
|
using Perspex.Win32.Interop; |
||||
|
using System.Runtime.InteropServices; |
||||
|
|
||||
|
class ClipboardImpl : IClipboard |
||||
|
{ |
||||
|
async Task OpenClipboard() |
||||
|
{ |
||||
|
while (!UnmanagedMethods.OpenClipboard(IntPtr.Zero)) |
||||
|
{ |
||||
|
await Task.Delay(100); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public async Task<string> GetTextAsync() |
||||
|
{ |
||||
|
await this.OpenClipboard(); |
||||
|
try |
||||
|
{ |
||||
|
IntPtr hText = UnmanagedMethods.GetClipboardData(UnmanagedMethods.ClipboardFormat.CF_UNICODETEXT); |
||||
|
if (hText == IntPtr.Zero) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
var pText = UnmanagedMethods.GlobalLock(hText); |
||||
|
if (pText == IntPtr.Zero) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
var rv = Marshal.PtrToStringUni(pText); |
||||
|
UnmanagedMethods.GlobalUnlock(hText); |
||||
|
return rv; |
||||
|
} |
||||
|
finally |
||||
|
{ |
||||
|
UnmanagedMethods.CloseClipboard(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public async Task SetTextAsync(string text) |
||||
|
{ |
||||
|
if (text == null) |
||||
|
{ |
||||
|
throw new ArgumentNullException(nameof(text)); |
||||
|
} |
||||
|
|
||||
|
await this.OpenClipboard(); |
||||
|
try |
||||
|
{ |
||||
|
var hGlobal = Marshal.StringToHGlobalUni(text); |
||||
|
UnmanagedMethods.SetClipboardData(UnmanagedMethods.ClipboardFormat.CF_UNICODETEXT, hGlobal); |
||||
|
} |
||||
|
finally |
||||
|
{ |
||||
|
UnmanagedMethods.CloseClipboard(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public async Task ClearAsync() |
||||
|
{ |
||||
|
await this.OpenClipboard(); |
||||
|
try |
||||
|
{ |
||||
|
UnmanagedMethods.EmptyClipboard(); |
||||
|
} |
||||
|
finally |
||||
|
{ |
||||
|
UnmanagedMethods.CloseClipboard(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue