Skip to content

Commit

Permalink
xamarinGH-969 Clipboard Changed (xamarin#720)
Browse files Browse the repository at this point in the history
* Use SharedPreferences.Editor.Apply when editing to force disk write to be asynchronous (xamarin#637)

* xamarinGH-182 Color/Point/Rect/Size Extension Converters (xamarin#651)

* Add extension helpers for iOS and Android

* Add UWP colors and adjsut android colors.

* Add UWP Point/Rect/Size

* Rename and optimize!

* Add tests and color helpers!

* Fix unit test and add WithAlpha

* Cleanup iOS as it stores it from 0-1

* Tests are all green!

* Add generate-docs cmd :)

* Add docs config to all :)

* Additional clenaup for docs

* Added some docs

* Updated the mdoc target and regenerated the docs

* Allow for code reuse on macOS (xamarin#665)

Even though macOS is not yet officially supported, it is nice to reuse this code. macOS is exactly the same, except for this single property not supported.

* xamarinGH-196 Browser Customization (xamarin#646)

* Implemented xamarinGH-196

* Apply suggestions from code review: Naming

Renamed incorrectly spelled variables

Co-Authored-By: Mrnikbobjeff <schillinik@yahoo.de>

* Implemented xamarinGH-196

* Browser update work as requested. Using System.Drawing.Color now

* Adopted more review changes. Type forwarded.

* Review changes

* Made equals operator null safe, removed constructor

* Add documentation and simplify the API!

* Update viewmodel

* Back to 7.2

* xamarinGH-676 Require To Check GPS Location is Fake Or Not In GeoLocation (xamarin#677)

* Added  bool IsFromMockProvider in Location class

* Checkin

* csproj original

* Revert "csproj original"

This reverts commit 6302b95.

* Revert "Checkin"

This reverts commit 0881e4f.

* Revert "Added  bool IsFromMockProvider in Location class"

This reverts commit e56d940.

* Revert the Xamarin.Essentials.csproj and Samples.Android.csproj to original;

Adjust the property IsFromMockProvider in Location.shared and LocationExetensions.

* Edited sample to show if the location is from moking provider.

* Update docs

* Fixes xamarin#694 (xamarin#699)

* Update the mdoc to use the new minimum for VS2019

* xamarinGH-126: Finish Shake Detector API (xamarin#693)

* Update CONTRIBUTING.md (xamarin#692)

* xamarinGH-126 Detect Shake API (xamarin#666)

* Added shaken support

* name vhange

* check-in

* doc update

* Implements the Shake API inside Accelerometer Class;
Change sample to AccelorometerViewModel

* Fix the sample project

* Update the docs

* Added Queue mechanism based off seismic with tests as well. Something is not  right yet with calulating isaccelerating.

* Finalize shake detection!!!

* Re-generated the docs.

* Fix the VM after merge

* xamarinGH-704 Handle duplicate item in keychain (xamarin#705)

* Update CONTRIBUTING.md (xamarin#692)

* xamarin#704 if we get a duplicate item try to remove and then re-add if possible.

* Initial commit Clipboard Change

* Implemented Clipboard listeners on iOS, tested on android and uwp and fixed other issues related to Visual Studio for Mac and Essentials support.

* Initial commit Clipboard Change

* Implemented Clipboard listeners on iOS, tested on android and uwp and fixed other issues related to Visual Studio for Mac and Essentials support.

* Naming fix

* UI improvements

* Docs update

* docs update part 2

* docs tool breaking fix

* reverted change to csproj
  • Loading branch information
Niklas Schilli authored and ScottBTR committed Jan 22, 2020
1 parent 2dfb373 commit 48e0522
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 4 deletions.
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

0 comments on commit 48e0522

Please sign in to comment.