Skip to content

Commit b7c8b3d

Browse files
authored
add instance API (#197)
1 parent 5b421d2 commit b7c8b3d

24 files changed

+212
-63
lines changed

readme.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Support is available via a [Tidelift Subscription](https://tidelift.com/subscrip
2323
* [SetText](#settext)
2424
* [GetTextAsync](#gettextasync)
2525
* [GetText](#gettext)
26+
* [Instance API](#instance-api)
2627
* [Supported on](#supported-on)
2728
* [Notes on Linux](#notes-on-linux)
2829
* [Security contact information](#security-contact-information)<!-- endtoc -->
@@ -41,9 +42,9 @@ https://nuget.org/packages/TextCopy/
4142
<!-- snippet: SetTextAsync -->
4243
<a id='snippet-settextasync'/></a>
4344
```cs
44-
await TextCopy.Clipboard.SetTextAsync("Text to place in clipboard");
45+
await ClipboardService.SetTextAsync("Text to place in clipboard");
4546
```
46-
<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>
47+
<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>
4748
<!-- endsnippet -->
4849

4950

@@ -52,9 +53,9 @@ await TextCopy.Clipboard.SetTextAsync("Text to place in clipboard");
5253
<!-- snippet: SetText -->
5354
<a id='snippet-settext'/></a>
5455
```cs
55-
TextCopy.Clipboard.SetText("Text to place in clipboard");
56+
ClipboardService.SetText("Text to place in clipboard");
5657
```
57-
<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>
58+
<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>
5859
<!-- endsnippet -->
5960

6061

@@ -63,9 +64,9 @@ TextCopy.Clipboard.SetText("Text to place in clipboard");
6364
<!-- snippet: GetTextAsync -->
6465
<a id='snippet-gettextasync'/></a>
6566
```cs
66-
var text = await TextCopy.Clipboard.GetTextAsync();
67+
var text = await ClipboardService.GetTextAsync();
6768
```
68-
<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>
69+
<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>
6970
<!-- endsnippet -->
7071

7172

@@ -74,9 +75,23 @@ var text = await TextCopy.Clipboard.GetTextAsync();
7475
<!-- snippet: GetText -->
7576
<a id='snippet-gettext'/></a>
7677
```cs
77-
var text = TextCopy.Clipboard.GetText();
78+
var text = ClipboardService.GetText();
7879
```
79-
<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>
80+
<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>
81+
<!-- endsnippet -->
82+
83+
84+
## Instance API
85+
86+
In adition to the above static API, there is an instance API exposed:
87+
88+
<!-- snippet: SetTextInstance -->
89+
<a id='snippet-settextinstance'/></a>
90+
```cs
91+
var clipboard = new Clipboard();
92+
clipboard.SetText("Text to place in clipboard");
93+
```
94+
<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>
8095
<!-- endsnippet -->
8196

8297

readme.source.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ snippet: GetTextAsync
3939
snippet: GetText
4040

4141

42+
## Instance API
43+
44+
In adition to the above static API, there is an instance API exposed:
45+
46+
snippet: SetTextInstance
47+
48+
4249
## Supported on
4350

4451
* Windows with .NET Framework 4.6.1 and up

src/AndroidApp/MainActivity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ protected override void OnResume()
3636

3737
void Input_TextChanged(object sender, TextChangedEventArgs e)
3838
{
39-
TextCopy.Clipboard.SetText(e.Text.ToString());
39+
TextCopy.ClipboardService.SetText(e.Text.ToString());
4040

41-
clipboardContent.Text = TextCopy.Clipboard.GetText();
41+
clipboardContent.Text = TextCopy.ClipboardService.GetText();
4242
}
4343
}

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project>
33
<PropertyGroup>
4-
<Version>3.3.0</Version>
4+
<Version>4.0.0</Version>
55
<AssemblyVersion>1.0.0</AssemblyVersion>
66
<PackageTags>Clipboard, Copy</PackageTags>
77
<Description>A cross platform package to copy text to the clipboard.</Description>

src/TestConsole/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class Program
66
static async Task<int> Main()
77
{
88
var text = "Hello World!";
9-
await Clipboard.SetTextAsync(text);
10-
var result = await Clipboard.GetTextAsync();
9+
await ClipboardService.SetTextAsync(text);
10+
var result = await ClipboardService.GetTextAsync();
1111
if (result == text)
1212
{
1313
return 0;

src/Tests/ClipboardServiceTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Threading.Tasks;
2+
using TextCopy;
3+
using VerifyXunit;
4+
using Xunit;
5+
using Xunit.Abstractions;
6+
7+
public class ClipboardServiceTests :
8+
VerifyBase
9+
{
10+
[Fact]
11+
public async Task Simple()
12+
{
13+
VerifyInner("Foo");
14+
VerifyInner("🅢");
15+
await VerifyInnerAsync("Foo");
16+
await VerifyInnerAsync("🅢");
17+
}
18+
19+
static void VerifyInner(string expected)
20+
{
21+
ClipboardService.SetText(expected);
22+
23+
var actual = ClipboardService.GetText();
24+
Assert.Equal(expected, actual);
25+
}
26+
27+
static async Task VerifyInnerAsync(string expected)
28+
{
29+
await ClipboardService.SetTextAsync(expected);
30+
31+
var actual = await ClipboardService.GetTextAsync();
32+
Assert.Equal(expected, actual);
33+
}
34+
35+
public ClipboardServiceTests(ITestOutputHelper output) :
36+
base(output)
37+
{
38+
}
39+
}

src/Tests/ClipboardTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ public async Task Simple()
1818

1919
static void VerifyInner(string expected)
2020
{
21-
Clipboard.SetText(expected);
21+
ClipboardService.SetText(expected);
2222

23-
var actual = Clipboard.GetText();
23+
var actual = new Clipboard().GetText();
2424
Assert.Equal(expected, actual);
2525
}
2626

2727
static async Task VerifyInnerAsync(string expected)
2828
{
29-
await Clipboard.SetTextAsync(expected);
29+
await new Clipboard().SetTextAsync(expected);
3030

31-
var actual = await Clipboard.GetTextAsync();
31+
var actual = await ClipboardService.GetTextAsync();
3232
Assert.Equal(expected, actual);
3333
}
3434

src/Tests/Snippets.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Threading.Tasks;
2+
using TextCopy;
3+
24
// ReSharper disable UnusedVariable
35

46
class Snippets
@@ -7,7 +9,14 @@ void SetText()
79
{
810
#region SetText
911

10-
TextCopy.Clipboard.SetText("Text to place in clipboard");
12+
ClipboardService.SetText("Text to place in clipboard");
13+
14+
#endregion
15+
16+
#region SetTextInstance
17+
18+
var clipboard = new Clipboard();
19+
clipboard.SetText("Text to place in clipboard");
1120

1221
#endregion
1322
}
@@ -16,7 +25,7 @@ void GetText()
1625
{
1726
#region GetText
1827

19-
var text = TextCopy.Clipboard.GetText();
28+
var text = ClipboardService.GetText();
2029

2130
#endregion
2231
}
@@ -25,7 +34,7 @@ async Task SetTextAsync()
2534
{
2635
#region SetTextAsync
2736

28-
await TextCopy.Clipboard.SetTextAsync("Text to place in clipboard");
37+
await ClipboardService.SetTextAsync("Text to place in clipboard");
2938

3039
#endregion
3140
}
@@ -34,7 +43,7 @@ async Task GetTextAsync()
3443
{
3544
#region GetTextAsync
3645

37-
var text = await TextCopy.Clipboard.GetTextAsync();
46+
var text = await ClipboardService.GetTextAsync();
3847

3948
#endregion
4049
}

src/Tests/TestSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using Xunit;
2+
3+
[assembly: CollectionBehavior(DisableTestParallelization = true)]

src/TextCopy.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
88
.travis.yml = .travis.yml
99
appveyor.yml = appveyor.yml
1010
Directory.Build.props = Directory.Build.props
11+
..\readme.source.md = ..\readme.source.md
1112
EndProjectSection
1213
EndProject
1314
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TextCopy", "TextCopy\TextCopy.csproj", "{92E64F4E-6A8E-43C2-9C8D-694E0327BEF0}"

src/TextCopy/Clipboard.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,44 @@
1-
using System;
2-
using System.Threading;
1+
using System.Threading;
32
using System.Threading.Tasks;
43

54
namespace TextCopy
65
{
76
/// <summary>
87
/// Provides methods to place text on and retrieve text from the system Clipboard.
98
/// </summary>
10-
public static partial class Clipboard
9+
public class Clipboard :
10+
IClipboard
1111
{
12-
static Func<CancellationToken, Task<string?>> getAsyncFunc = CreateAsyncGet();
13-
static Func<string?> getFunc = CreateGet();
14-
1512
/// <summary>
1613
/// Retrieves text data from the Clipboard.
1714
/// </summary>
18-
public static Task<string?> GetTextAsync(CancellationToken cancellation = default)
15+
public virtual Task<string?> GetTextAsync(CancellationToken cancellation = default)
1916
{
20-
return getAsyncFunc(cancellation);
17+
return ClipboardService.GetTextAsync(cancellation);
2118
}
2219

2320
/// <summary>
2421
/// Retrieves text data from the Clipboard.
2522
/// </summary>
26-
public static string? GetText()
23+
public virtual string? GetText()
2724
{
28-
return getFunc();
25+
return ClipboardService.GetText();
2926
}
3027

31-
static Func<string, CancellationToken, Task> setAsyncAction = CreateAsyncSet();
32-
static Action<string> setAction = CreateSet();
33-
3428
/// <summary>
3529
/// Clears the Clipboard and then adds text data to it.
3630
/// </summary>
37-
public static Task SetTextAsync(string text, CancellationToken cancellation = default)
31+
public virtual Task SetTextAsync(string text, CancellationToken cancellation = default)
3832
{
39-
Guard.AgainstNull(text, nameof(text));
40-
return setAsyncAction(text, cancellation);
33+
return ClipboardService.SetTextAsync(text, cancellation);
4134
}
4235

4336
/// <summary>
4437
/// Clears the Clipboard and then adds text data to it.
4538
/// </summary>
46-
public static void SetText(string text)
39+
public virtual void SetText(string text)
4740
{
48-
Guard.AgainstNull(text, nameof(text));
49-
setAction(text);
41+
ClipboardService.SetText(text);
5042
}
5143
}
5244
}

src/TextCopy/ClipboardService.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace TextCopy
6+
{
7+
/// <summary>
8+
/// Provides methods to place text on and retrieve text from the system Clipboard.
9+
/// </summary>
10+
public static partial class ClipboardService
11+
{
12+
static Func<CancellationToken, Task<string?>> getAsyncFunc = CreateAsyncGet();
13+
static Func<string?> getFunc = CreateGet();
14+
15+
/// <summary>
16+
/// Retrieves text data from the Clipboard.
17+
/// </summary>
18+
public static Task<string?> GetTextAsync(CancellationToken cancellation = default)
19+
{
20+
return getAsyncFunc(cancellation);
21+
}
22+
23+
/// <summary>
24+
/// Retrieves text data from the Clipboard.
25+
/// </summary>
26+
public static string? GetText()
27+
{
28+
return getFunc();
29+
}
30+
31+
static Func<string, CancellationToken, Task> setAsyncAction = CreateAsyncSet();
32+
static Action<string> setAction = CreateSet();
33+
34+
/// <summary>
35+
/// Clears the Clipboard and then adds text data to it.
36+
/// </summary>
37+
public static Task SetTextAsync(string text, CancellationToken cancellation = default)
38+
{
39+
Guard.AgainstNull(text, nameof(text));
40+
return setAsyncAction(text, cancellation);
41+
}
42+
43+
/// <summary>
44+
/// Clears the Clipboard and then adds text data to it.
45+
/// </summary>
46+
public static void SetText(string text)
47+
{
48+
Guard.AgainstNull(text, nameof(text));
49+
setAction(text);
50+
}
51+
}
52+
}

src/TextCopy/Clipboard_GetAndroid.cs renamed to src/TextCopy/ClipboardService_GetAndroid.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace TextCopy
88
{
9-
public static partial class Clipboard
9+
public static partial class ClipboardService
1010
{
1111
static Func<CancellationToken, Task<string?>> CreateAsyncGet()
1212
{

src/TextCopy/Clipboard_GetStandard.cs renamed to src/TextCopy/ClipboardService_GetStandard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace TextCopy
88
{
9-
public static partial class Clipboard
9+
public static partial class ClipboardService
1010
{
1111
static Func<CancellationToken, Task<string?>> CreateAsyncGet()
1212
{

src/TextCopy/Clipboard_GetUap.cs renamed to src/TextCopy/ClipboardService_GetUap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace TextCopy
1111
{
12-
public static partial class Clipboard
12+
public static partial class ClipboardService
1313
{
1414
static Func<CancellationToken, Task<string?>> CreateAsyncGet()
1515
{

0 commit comments

Comments
 (0)