Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

GH-969 Clipboard Changed #720

Merged
merged 32 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
420ce10
Use SharedPreferences.Editor.Apply when editing to force disk write t…
csdinon Dec 10, 2018
05cadb9
GH-182 Color/Point/Rect/Size Extension Converters (#651)
jamesmontemagno Dec 22, 2018
cdf9e1c
Merge branch 'master' into dev/1.1.0
jamesmontemagno Dec 22, 2018
37f883f
Merge branch 'master' into dev/1.1.0
mattleibow Dec 23, 2018
ef2231a
Allow for code reuse on macOS (#665)
mattleibow Dec 25, 2018
9d3c78f
Merge branch 'master' into dev/1.1.0
jamesmontemagno Jan 7, 2019
d109e87
Merge branch 'master' into dev/1.1.0
jamesmontemagno Jan 14, 2019
2d52855
GH-196 Browser Customization (#646)
Jan 25, 2019
645246f
Add documentation and simplify the API!
jamesmontemagno Jan 26, 2019
bb9397a
Update viewmodel
jamesmontemagno Jan 26, 2019
e656191
Back to 7.2
jamesmontemagno Jan 26, 2019
c68e7cf
GH-676 Require To Check GPS Location is Fake Or Not In GeoLocation (#…
pictos Jan 28, 2019
ac54747
Fixes #694 (#699)
jamesmontemagno Feb 4, 2019
0690b8a
Update the mdoc to use the new minimum for VS2019
mattleibow Feb 16, 2019
46d0d42
Merge branch 'dev/1.1.0-issue-196' into dev/1.1.0
mattleibow Feb 16, 2019
ead1eba
GH-126: Finish Shake Detector API (#693)
jamesmontemagno Feb 19, 2019
a7fc2c7
GH-704 Handle duplicate item in keychain (#705)
jamesmontemagno Feb 19, 2019
6c116e0
Merge branch 'master' into dev/1.1.0
mattleibow Feb 19, 2019
9e83149
Initial commit Clipboard Change
Feb 25, 2019
eb8805f
Implemented Clipboard listeners on iOS, tested on android and uwp and…
Feb 26, 2019
5973f18
Initial commit Clipboard Change
Feb 25, 2019
0ec0311
Implemented Clipboard listeners on iOS, tested on android and uwp and…
Feb 26, 2019
6df8c36
Merge branch 'feature/GH-696' of https://github.com/Mrnikbobjeff/Esse…
Feb 26, 2019
e9c21ad
Naming fix
Feb 26, 2019
72ff1ae
UI improvements
Feb 28, 2019
cf9750a
Merge branch 'master' into feature/GH-696
jamesmontemagno May 23, 2019
b418f7b
Docs update
Aug 26, 2019
9b41946
docs update part 2
Aug 26, 2019
bfab86e
Resolved conflicts
Aug 26, 2019
38138e4
docs tool breaking fix
Aug 26, 2019
ec84740
reverted change to csproj
Aug 28, 2019
fff26cf
Merge branch 'master' into feature/GH-696
jamesmontemagno Dec 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Samples/Samples/View/ClipboardPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<Entry Placeholder="Enter text..." Text="{Binding FieldValue}" />
<Button Text="Copy to clipboard" Command="{Binding CopyCommand}" />
<Button Text="Paste from clipboard" Command="{Binding PasteCommand}" />
<Label Text="{Binding LastCopied}" />
</StackLayout>
</ScrollView>
</StackLayout>
Expand Down
25 changes: 24 additions & 1 deletion Samples/Samples/ViewModel/ClipboardViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows.Input;
using System;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;

Expand All @@ -7,6 +8,7 @@ namespace Samples.ViewModel
class ClipboardViewModel : BaseViewModel
{
string fieldValue;
string lastCopied;

public ClipboardViewModel()
{
Expand All @@ -24,6 +26,27 @@ public string FieldValue
set => SetProperty(ref fieldValue, value);
}

public string LastCopied
{
get => lastCopied;
set => SetProperty(ref lastCopied, value);
}

public override void OnAppearing()
{
Clipboard.ClipboardContentChanged += OnClipboardContentChanged;
}

public override void OnDisappearing()
{
Clipboard.ClipboardContentChanged -= OnClipboardContentChanged;
}

void OnClipboardContentChanged(object sender, EventArgs args)
{
LastCopied = $"Last copied text at {DateTime.UtcNow:T}";
}

async void OnCopy() => await Clipboard.SetTextAsync(FieldValue);

async void OnPaste()
Expand Down
20 changes: 19 additions & 1 deletion Xamarin.Essentials/Clipboard/Clipboard.android.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Android.Content;

namespace Xamarin.Essentials
{
public static partial class Clipboard
{
static readonly Lazy<ClipboardChangeListener> clipboardListener
= new Lazy<ClipboardChangeListener>(() => new ClipboardChangeListener());

static Task PlatformSetTextAsync(string text)
{
Platform.ClipboardManager.PrimaryClip = ClipData.NewPlainText("Text", text);
Expand All @@ -16,5 +20,19 @@ static bool PlatformHasText

static Task<string> PlatformGetTextAsync()
=> Task.FromResult(Platform.ClipboardManager.PrimaryClip?.GetItemAt(0)?.Text);

static void StartClipboardListeners()
=> Platform.ClipboardManager.AddPrimaryClipChangedListener(clipboardListener.Value);

static void StopClipboardListeners()
=> Platform.ClipboardManager.RemovePrimaryClipChangedListener(clipboardListener.Value);
}

public class ClipboardChangeListener : Java.Lang.Object, ClipboardManager.IOnPrimaryClipChangedListener
{
public void OnPrimaryClipChanged()
{
Clipboard.ClipboardChangedInternal();
}
}
}
19 changes: 18 additions & 1 deletion Xamarin.Essentials/Clipboard/Clipboard.ios.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Foundation;
using UIKit;

namespace Xamarin.Essentials
Expand All @@ -11,10 +13,25 @@ static Task PlatformSetTextAsync(string text)
return Task.CompletedTask;
}

static NSObject observer;

static bool PlatformHasText
=> UIPasteboard.General.HasStrings;

static Task<string> PlatformGetTextAsync()
=> Task.FromResult(UIPasteboard.General.String);

static void StartClipboardListeners()
{
observer = NSNotificationCenter.DefaultCenter.AddObserver(
UIPasteboard.ChangedNotification,
ClipboardChangedObserver);
}

static void StopClipboardListeners()
=> NSNotificationCenter.DefaultCenter.RemoveObserver(observer);

static void ClipboardChangedObserver(NSNotification notification)
=> ClipboardChangedInternal();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ static bool PlatformHasText

static Task<string> PlatformGetTextAsync()
=> throw ExceptionUtils.NotSupportedOrImplementedException;

static void StartClipboardListeners()
=> throw ExceptionUtils.NotSupportedOrImplementedException;

static void StopClipboardListeners()
=> throw ExceptionUtils.NotSupportedOrImplementedException;
}
}
32 changes: 31 additions & 1 deletion Xamarin.Essentials/Clipboard/Clipboard.shared.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;

namespace Xamarin.Essentials
{
Expand All @@ -12,5 +13,34 @@ public static bool HasText

public static Task<string> GetTextAsync()
=> PlatformGetTextAsync();

public static event EventHandler<EventArgs> ClipboardContentChanged
{
add
{
var wasRunning = ClipboardContentChangedInternal != null;

ClipboardContentChangedInternal += value;

if (!wasRunning && ClipboardContentChangedInternal != null)
{
StartClipboardListeners();
}
}

remove
{
var wasRunning = ClipboardContentChangedInternal != null;

ClipboardContentChangedInternal -= value;

if (wasRunning && ClipboardContentChangedInternal == null)
StopClipboardListeners();
}
}

static event EventHandler<EventArgs> ClipboardContentChangedInternal;

internal static void ClipboardChangedInternal() => ClipboardContentChangedInternal?.Invoke(null, EventArgs.Empty);
}
}
8 changes: 8 additions & 0 deletions Xamarin.Essentials/Clipboard/Clipboard.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,13 @@ static Task<string> PlatformGetTextAsync()
? clipboardContent.GetTextAsync().AsTask()
: Task.FromResult<string>(null);
}

static void StartClipboardListeners()
=> WindowsClipboard.ContentChanged += ClipboardChangedEventListener;

static void StopClipboardListeners()
=> WindowsClipboard.ContentChanged -= ClipboardChangedEventListener;

static void ClipboardChangedEventListener(object sender, object val) => ClipboardChangedInternal();
}
}
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-android.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<Member Id="F:Xamarin.Essentials.BrowserTitleMode.Show" />
</Type>
<Type Name="Xamarin.Essentials.Clipboard" Id="T:Xamarin.Essentials.Clipboard">
<Member Id="E:Xamarin.Essentials.Clipboard.ClipboardContentChanged" />
<Member Id="M:Xamarin.Essentials.Clipboard.GetTextAsync" />
<Member Id="M:Xamarin.Essentials.Clipboard.SetTextAsync(System.String)" />
<Member Id="P:Xamarin.Essentials.Clipboard.HasText" />
Expand Down
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-ios.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<Member Id="F:Xamarin.Essentials.BrowserTitleMode.Show" />
</Type>
<Type Name="Xamarin.Essentials.Clipboard" Id="T:Xamarin.Essentials.Clipboard">
<Member Id="E:Xamarin.Essentials.Clipboard.ClipboardContentChanged" />
<Member Id="M:Xamarin.Essentials.Clipboard.GetTextAsync" />
<Member Id="M:Xamarin.Essentials.Clipboard.SetTextAsync(System.String)" />
<Member Id="P:Xamarin.Essentials.Clipboard.HasText" />
Expand Down
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-uwp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<Member Id="F:Xamarin.Essentials.BrowserTitleMode.Show" />
</Type>
<Type Name="Xamarin.Essentials.Clipboard" Id="T:Xamarin.Essentials.Clipboard">
<Member Id="E:Xamarin.Essentials.Clipboard.ClipboardContentChanged" />
<Member Id="M:Xamarin.Essentials.Clipboard.GetTextAsync" />
<Member Id="M:Xamarin.Essentials.Clipboard.SetTextAsync(System.String)" />
<Member Id="P:Xamarin.Essentials.Clipboard.HasText" />
Expand Down
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<Member Id="F:Xamarin.Essentials.BrowserTitleMode.Show" />
</Type>
<Type Name="Xamarin.Essentials.Clipboard" Id="T:Xamarin.Essentials.Clipboard">
<Member Id="E:Xamarin.Essentials.Clipboard.ClipboardContentChanged" />
<Member Id="M:Xamarin.Essentials.Clipboard.GetTextAsync" />
<Member Id="M:Xamarin.Essentials.Clipboard.SetTextAsync(System.String)" />
<Member Id="P:Xamarin.Essentials.Clipboard.HasText" />
Expand Down
19 changes: 19 additions & 0 deletions docs/en/Xamarin.Essentials/Clipboard.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@
<remarks></remarks>
</Docs>
<Members>
<Member MemberName="ClipboardContentChanged">
<MemberSignature Language="C#" Value="public static event EventHandler&lt;EventArgs&gt; ClipboardContentChanged;" />
<MemberSignature Language="ILAsm" Value=".event class System.EventHandler`1&lt;class System.EventArgs&gt; ClipboardContentChanged" />
<MemberSignature Language="DocId" Value="E:Xamarin.Essentials.Clipboard.ClipboardContentChanged" />
<MemberType>Event</MemberType>
<AssemblyInfo>
<AssemblyName>Xamarin.Essentials</AssemblyName>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.EventHandler&lt;System.EventArgs&gt;</ReturnType>
</ReturnValue>
<Docs>
<summary>Fires when the clipboard content changes.</summary>
<remarks>
<para />
</remarks>
</Docs>
</Member>
<Member MemberName="GetTextAsync">
<MemberSignature Language="C#" Value="public static System.Threading.Tasks.Task&lt;string&gt; GetTextAsync ();" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig class System.Threading.Tasks.Task`1&lt;string&gt; GetTextAsync() cil managed" />
Expand Down