-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from CoreyHayward/feature/clipboard-history
Feature/clipboard history
- Loading branch information
Showing
4 changed files
with
80 additions
and
48 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
Community.PowerToys.Run.Plugin.ClipboardManager/ClipboardManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Windows.ApplicationModel.DataTransfer; | ||
using Clipboard = Windows.ApplicationModel.DataTransfer.Clipboard; | ||
|
||
namespace Community.PowerToys.Run.Plugin.ClipboardManager; | ||
internal class ClipboardManager | ||
{ | ||
private readonly HashSet<string> _clipboardItems = []; | ||
public IReadOnlySet<string> ClipboardItems => _clipboardItems.Reverse().ToHashSet(); | ||
|
||
public ClipboardManager() | ||
{ | ||
var clipboardHistoryResult = Clipboard.GetHistoryItemsAsync().AsTask().Result; | ||
var clipboardTextItems = | ||
clipboardHistoryResult.Items | ||
.Where(x => x.Content.Contains(StandardDataFormats.Text)) | ||
.Select(x => x.Content.GetTextAsync().AsTask().Result) | ||
.ToList(); | ||
|
||
foreach (var clipboardItemText in clipboardTextItems) | ||
{ | ||
_clipboardItems.Add(clipboardItemText); | ||
} | ||
|
||
Clipboard.HistoryChanged += Clipboard_HistoryChanged; | ||
} | ||
|
||
public void ClearHistory() | ||
{ | ||
_clipboardItems.Clear(); | ||
Clipboard.ClearHistory(); | ||
} | ||
|
||
public static void SetStringAsClipboardContent(string text) | ||
{ | ||
var dataPackage = new DataPackage(); | ||
dataPackage.SetText(text); | ||
Clipboard.SetContent(dataPackage); | ||
} | ||
|
||
private void Clipboard_HistoryChanged(object? sender, ClipboardHistoryChangedEventArgs e) | ||
{ | ||
var item = Clipboard.GetContent(); | ||
if (!item.Contains(StandardDataFormats.Text)) | ||
{ | ||
return; | ||
} | ||
|
||
var text = item.GetTextAsync().AsTask().Result; | ||
_clipboardItems.Add(text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters