Skip to content

Commit

Permalink
add instance API (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored May 25, 2020
1 parent 5b421d2 commit b7c8b3d
Show file tree
Hide file tree
Showing 24 changed files with 212 additions and 63 deletions.
31 changes: 23 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Support is available via a [Tidelift Subscription](https://tidelift.com/subscrip
* [SetText](#settext)
* [GetTextAsync](#gettextasync)
* [GetText](#gettext)
* [Instance API](#instance-api)
* [Supported on](#supported-on)
* [Notes on Linux](#notes-on-linux)
* [Security contact information](#security-contact-information)<!-- endtoc -->
Expand All @@ -41,9 +42,9 @@ https://nuget.org/packages/TextCopy/
<!-- snippet: SetTextAsync -->
<a id='snippet-settextasync'/></a>
```cs
await TextCopy.Clipboard.SetTextAsync("Text to place in clipboard");
await ClipboardService.SetTextAsync("Text to place in clipboard");
```
<sup><a href='/src/Tests/Snippets.cs#L26-L30' title='File snippet `settextasync` was extracted from'>snippet source</a> | <a href='#snippet-settextasync' title='Navigate to start of snippet `settextasync`'>anchor</a></sup>
<sup><a href='/src/Tests/Snippets.cs#L35-L39' title='File snippet `settextasync` was extracted from'>snippet source</a> | <a href='#snippet-settextasync' title='Navigate to start of snippet `settextasync`'>anchor</a></sup>
<!-- endsnippet -->


Expand All @@ -52,9 +53,9 @@ await TextCopy.Clipboard.SetTextAsync("Text to place in clipboard");
<!-- snippet: SetText -->
<a id='snippet-settext'/></a>
```cs
TextCopy.Clipboard.SetText("Text to place in clipboard");
ClipboardService.SetText("Text to place in clipboard");
```
<sup><a href='/src/Tests/Snippets.cs#L8-L12' title='File snippet `settext` was extracted from'>snippet source</a> | <a href='#snippet-settext' title='Navigate to start of snippet `settext`'>anchor</a></sup>
<sup><a href='/src/Tests/Snippets.cs#L10-L14' title='File snippet `settext` was extracted from'>snippet source</a> | <a href='#snippet-settext' title='Navigate to start of snippet `settext`'>anchor</a></sup>
<!-- endsnippet -->


Expand All @@ -63,9 +64,9 @@ TextCopy.Clipboard.SetText("Text to place in clipboard");
<!-- snippet: GetTextAsync -->
<a id='snippet-gettextasync'/></a>
```cs
var text = await TextCopy.Clipboard.GetTextAsync();
var text = await ClipboardService.GetTextAsync();
```
<sup><a href='/src/Tests/Snippets.cs#L35-L39' title='File snippet `gettextasync` was extracted from'>snippet source</a> | <a href='#snippet-gettextasync' title='Navigate to start of snippet `gettextasync`'>anchor</a></sup>
<sup><a href='/src/Tests/Snippets.cs#L44-L48' title='File snippet `gettextasync` was extracted from'>snippet source</a> | <a href='#snippet-gettextasync' title='Navigate to start of snippet `gettextasync`'>anchor</a></sup>
<!-- endsnippet -->


Expand All @@ -74,9 +75,23 @@ var text = await TextCopy.Clipboard.GetTextAsync();
<!-- snippet: GetText -->
<a id='snippet-gettext'/></a>
```cs
var text = TextCopy.Clipboard.GetText();
var text = ClipboardService.GetText();
```
<sup><a href='/src/Tests/Snippets.cs#L17-L21' title='File snippet `gettext` was extracted from'>snippet source</a> | <a href='#snippet-gettext' title='Navigate to start of snippet `gettext`'>anchor</a></sup>
<sup><a href='/src/Tests/Snippets.cs#L26-L30' title='File snippet `gettext` was extracted from'>snippet source</a> | <a href='#snippet-gettext' title='Navigate to start of snippet `gettext`'>anchor</a></sup>
<!-- endsnippet -->


## Instance API

In adition to the above static API, there is an instance API exposed:

<!-- snippet: SetTextInstance -->
<a id='snippet-settextinstance'/></a>
```cs
var clipboard = new Clipboard();
clipboard.SetText("Text to place in clipboard");
```
<sup><a href='/src/Tests/Snippets.cs#L16-L21' title='File snippet `settextinstance` was extracted from'>snippet source</a> | <a href='#snippet-settextinstance' title='Navigate to start of snippet `settextinstance`'>anchor</a></sup>
<!-- endsnippet -->


Expand Down
7 changes: 7 additions & 0 deletions readme.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ snippet: GetTextAsync
snippet: GetText


## Instance API

In adition to the above static API, there is an instance API exposed:

snippet: SetTextInstance


## Supported on

* Windows with .NET Framework 4.6.1 and up
Expand Down
4 changes: 2 additions & 2 deletions src/AndroidApp/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ protected override void OnResume()

void Input_TextChanged(object sender, TextChangedEventArgs e)
{
TextCopy.Clipboard.SetText(e.Text.ToString());
TextCopy.ClipboardService.SetText(e.Text.ToString());

clipboardContent.Text = TextCopy.Clipboard.GetText();
clipboardContent.Text = TextCopy.ClipboardService.GetText();
}
}
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>3.3.0</Version>
<Version>4.0.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Clipboard, Copy</PackageTags>
<Description>A cross platform package to copy text to the clipboard.</Description>
Expand Down
4 changes: 2 additions & 2 deletions src/TestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Program
static async Task<int> Main()
{
var text = "Hello World!";
await Clipboard.SetTextAsync(text);
var result = await Clipboard.GetTextAsync();
await ClipboardService.SetTextAsync(text);
var result = await ClipboardService.GetTextAsync();
if (result == text)
{
return 0;
Expand Down
39 changes: 39 additions & 0 deletions src/Tests/ClipboardServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Threading.Tasks;
using TextCopy;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;

public class ClipboardServiceTests :
VerifyBase
{
[Fact]
public async Task Simple()
{
VerifyInner("Foo");
VerifyInner("🅢");
await VerifyInnerAsync("Foo");
await VerifyInnerAsync("🅢");
}

static void VerifyInner(string expected)
{
ClipboardService.SetText(expected);

var actual = ClipboardService.GetText();
Assert.Equal(expected, actual);
}

static async Task VerifyInnerAsync(string expected)
{
await ClipboardService.SetTextAsync(expected);

var actual = await ClipboardService.GetTextAsync();
Assert.Equal(expected, actual);
}

public ClipboardServiceTests(ITestOutputHelper output) :
base(output)
{
}
}
8 changes: 4 additions & 4 deletions src/Tests/ClipboardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ public async Task Simple()

static void VerifyInner(string expected)
{
Clipboard.SetText(expected);
ClipboardService.SetText(expected);

var actual = Clipboard.GetText();
var actual = new Clipboard().GetText();
Assert.Equal(expected, actual);
}

static async Task VerifyInnerAsync(string expected)
{
await Clipboard.SetTextAsync(expected);
await new Clipboard().SetTextAsync(expected);

var actual = await Clipboard.GetTextAsync();
var actual = await ClipboardService.GetTextAsync();
Assert.Equal(expected, actual);
}

Expand Down
17 changes: 13 additions & 4 deletions src/Tests/Snippets.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using TextCopy;

// ReSharper disable UnusedVariable

class Snippets
Expand All @@ -7,7 +9,14 @@ void SetText()
{
#region SetText

TextCopy.Clipboard.SetText("Text to place in clipboard");
ClipboardService.SetText("Text to place in clipboard");

#endregion

#region SetTextInstance

var clipboard = new Clipboard();
clipboard.SetText("Text to place in clipboard");

#endregion
}
Expand All @@ -16,7 +25,7 @@ void GetText()
{
#region GetText

var text = TextCopy.Clipboard.GetText();
var text = ClipboardService.GetText();

#endregion
}
Expand All @@ -25,7 +34,7 @@ async Task SetTextAsync()
{
#region SetTextAsync

await TextCopy.Clipboard.SetTextAsync("Text to place in clipboard");
await ClipboardService.SetTextAsync("Text to place in clipboard");

#endregion
}
Expand All @@ -34,7 +43,7 @@ async Task GetTextAsync()
{
#region GetTextAsync

var text = await TextCopy.Clipboard.GetTextAsync();
var text = await ClipboardService.GetTextAsync();

#endregion
}
Expand Down
3 changes: 3 additions & 0 deletions src/Tests/TestSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using Xunit;

[assembly: CollectionBehavior(DisableTestParallelization = true)]
1 change: 1 addition & 0 deletions src/TextCopy.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.travis.yml = .travis.yml
appveyor.yml = appveyor.yml
Directory.Build.props = Directory.Build.props
..\readme.source.md = ..\readme.source.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TextCopy", "TextCopy\TextCopy.csproj", "{92E64F4E-6A8E-43C2-9C8D-694E0327BEF0}"
Expand Down
30 changes: 11 additions & 19 deletions src/TextCopy/Clipboard.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,44 @@
using System;
using System.Threading;
using System.Threading;
using System.Threading.Tasks;

namespace TextCopy
{
/// <summary>
/// Provides methods to place text on and retrieve text from the system Clipboard.
/// </summary>
public static partial class Clipboard
public class Clipboard :
IClipboard
{
static Func<CancellationToken, Task<string?>> getAsyncFunc = CreateAsyncGet();
static Func<string?> getFunc = CreateGet();

/// <summary>
/// Retrieves text data from the Clipboard.
/// </summary>
public static Task<string?> GetTextAsync(CancellationToken cancellation = default)
public virtual Task<string?> GetTextAsync(CancellationToken cancellation = default)
{
return getAsyncFunc(cancellation);
return ClipboardService.GetTextAsync(cancellation);
}

/// <summary>
/// Retrieves text data from the Clipboard.
/// </summary>
public static string? GetText()
public virtual string? GetText()
{
return getFunc();
return ClipboardService.GetText();
}

static Func<string, CancellationToken, Task> setAsyncAction = CreateAsyncSet();
static Action<string> setAction = CreateSet();

/// <summary>
/// Clears the Clipboard and then adds text data to it.
/// </summary>
public static Task SetTextAsync(string text, CancellationToken cancellation = default)
public virtual Task SetTextAsync(string text, CancellationToken cancellation = default)
{
Guard.AgainstNull(text, nameof(text));
return setAsyncAction(text, cancellation);
return ClipboardService.SetTextAsync(text, cancellation);
}

/// <summary>
/// Clears the Clipboard and then adds text data to it.
/// </summary>
public static void SetText(string text)
public virtual void SetText(string text)
{
Guard.AgainstNull(text, nameof(text));
setAction(text);
ClipboardService.SetText(text);
}
}
}
52 changes: 52 additions & 0 deletions src/TextCopy/ClipboardService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace TextCopy
{
/// <summary>
/// Provides methods to place text on and retrieve text from the system Clipboard.
/// </summary>
public static partial class ClipboardService
{
static Func<CancellationToken, Task<string?>> getAsyncFunc = CreateAsyncGet();
static Func<string?> getFunc = CreateGet();

/// <summary>
/// Retrieves text data from the Clipboard.
/// </summary>
public static Task<string?> GetTextAsync(CancellationToken cancellation = default)
{
return getAsyncFunc(cancellation);
}

/// <summary>
/// Retrieves text data from the Clipboard.
/// </summary>
public static string? GetText()
{
return getFunc();
}

static Func<string, CancellationToken, Task> setAsyncAction = CreateAsyncSet();
static Action<string> setAction = CreateSet();

/// <summary>
/// Clears the Clipboard and then adds text data to it.
/// </summary>
public static Task SetTextAsync(string text, CancellationToken cancellation = default)
{
Guard.AgainstNull(text, nameof(text));
return setAsyncAction(text, cancellation);
}

/// <summary>
/// Clears the Clipboard and then adds text data to it.
/// </summary>
public static void SetText(string text)
{
Guard.AgainstNull(text, nameof(text));
setAction(text);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace TextCopy
{
public static partial class Clipboard
public static partial class ClipboardService
{
static Func<CancellationToken, Task<string?>> CreateAsyncGet()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace TextCopy
{
public static partial class Clipboard
public static partial class ClipboardService
{
static Func<CancellationToken, Task<string?>> CreateAsyncGet()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace TextCopy
{
public static partial class Clipboard
public static partial class ClipboardService
{
static Func<CancellationToken, Task<string?>> CreateAsyncGet()
{
Expand Down
Loading

0 comments on commit b7c8b3d

Please sign in to comment.