Skip to content

Commit

Permalink
Merge pull request #13 from CoreyHayward/feature/clipboard-history
Browse files Browse the repository at this point in the history
Feature/clipboard history
  • Loading branch information
CoreyHayward authored Nov 12, 2024
2 parents c65a43a + 201300d commit 6b74fdc
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 48 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<UseWindowsForms>true</UseWindowsForms>
<RootNamespace>Community.PowerToys.Run.Plugin.ClipboardManager</RootNamespace>
<AssemblyName>Community.PowerToys.Run.Plugin.ClipboardManager</AssemblyName>
<Version>0.7.0</Version>
<Version>0.8.0</Version>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<Platforms>x64;ARM64</Platforms>
Expand Down
73 changes: 27 additions & 46 deletions Community.PowerToys.Run.Plugin.ClipboardManager/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Input;
using Windows.ApplicationModel.DataTransfer;
using Wox.Infrastructure;
using Wox.Plugin;
using Wox.Plugin.Common.Win32;
Expand All @@ -19,8 +18,9 @@ namespace Community.PowerToys.Run.Plugin.ClipboardManager
{
public class Main : IPlugin, ISettingProvider, IContextMenu
{
private ClipboardManager _clipboardManager = new();
private PluginInitContext _context;
private string _iconPath;
private string _iconPath = "Images/ClipboardManager.light.png";
private bool _directPaste;
private int _beginTypeDelay;
private string _pasterPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Paster", "Paster.exe");
Expand Down Expand Up @@ -68,57 +68,41 @@ public List<Result> Query(Query query)
return [GetHistoryDisabledResult()];
}

var clipboardTextItems = GetTextItemsFromClipboardHistory();
var clipboardTextItems = _clipboardManager.ClipboardItems;
if (clipboardTextItems.Count == 0)
{
return [GetNoItemsResult()];
}

var results = new List<Result>();
if (!string.IsNullOrWhiteSpace(query?.Search))
if (string.IsNullOrWhiteSpace(query?.Search))
{
if (query.Search == "-")
{
results.Add(GetClearHistoryResult(query));
}

foreach (var item in clipboardTextItems)
{
var text = item.Content.GetTextAsync().AsTask().Result;
if (text.Contains(query.Search, StringComparison.OrdinalIgnoreCase))
{
results.Add(CreateResult(item, text));
}
}
return clipboardTextItems.Take(5).Select(CreateResult).ToList();
}
else

var results = new List<Result>();
if (query.Search == "-")
{
foreach (var item in clipboardTextItems.Take(5))
{
var text = item.Content.GetTextAsync().AsTask().Result;
results.Add(CreateResult(item, text));
}
results.Add(GetClearHistoryResult(query));
}

return results;
}
var matchingItems =
clipboardTextItems
.Where(x => x.Contains(query.Search, StringComparison.OrdinalIgnoreCase))
.Select(CreateResult);

private List<ClipboardHistoryItem> GetTextItemsFromClipboardHistory()
{
var clipboardHistoryResult = Clipboard.GetHistoryItemsAsync().GetAwaiter().GetResult();
var clipboardTextItems = clipboardHistoryResult.Items.Where(x => x.Content.Contains(StandardDataFormats.Text)).ToList();
return clipboardTextItems;
results.AddRange(matchingItems);
return results;
}

private Result CreateResult(ClipboardHistoryItem item, string text)
private Result CreateResult(string text)
=> new Result()
{
Title = text.Trim(),
SubTitle = "Paste this value",
IcoPath = _iconPath,
Action = (context) =>
{
Clipboard.SetHistoryItemAsContent(item);
ClipboardManager.SetStringAsClipboardContent(text);
if (!_directPaste)
{
return true;
Expand All @@ -131,7 +115,7 @@ private Result CreateResult(ClipboardHistoryItem item, string text)
}));
return true;
},
ContextData = item,
ContextData = text,
};

private Result GetHistoryDisabledResult()
Expand Down Expand Up @@ -173,7 +157,7 @@ private Result GetClearHistoryResult(Query query)
IcoPath = _iconPath,
Action = (context) =>
{
Clipboard.ClearHistory();
_clipboardManager.ClearHistory();
_context.API.ChangeQuery(query.ActionKeyword, true);
return true;
}
Expand All @@ -200,7 +184,7 @@ public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
Clipboard.SetHistoryItemAsContent((ClipboardHistoryItem)selectedResult.ContextData);
ClipboardManager.SetStringAsClipboardContent((string)selectedResult.ContextData);
if (!_directPaste)
{
return true;
Expand Down Expand Up @@ -232,9 +216,9 @@ public List<ContextMenuResult> LoadContextMenus(Result selectedResult)

static async Task EditAsync(Result selectedResult)
{
var clipboard = (ClipboardHistoryItem)selectedResult.ContextData;
var text = (string)selectedResult.ContextData;
var tempFile = Path.GetTempFileName();
File.WriteAllText(tempFile, await clipboard.Content.GetTextAsync());
File.WriteAllText(tempFile, text);
Process process = new()
{
StartInfo =
Expand All @@ -246,15 +230,14 @@ static async Task EditAsync(Result selectedResult)
process.Start();
process.WaitForExit();

DataPackage data = new();
data.SetText(File.ReadAllText(tempFile));
Clipboard.SetContent(data);
ClipboardManager.SetStringAsClipboardContent(File.ReadAllText(tempFile));
File.Delete(tempFile);
}
}
}
};
}

private void OnThemeChanged(Theme currentTheme, Theme newTheme)
{
UpdateIconPath(newTheme);
Expand All @@ -264,12 +247,10 @@ private void UpdateIconPath(Theme theme)
{
if (theme == Theme.Light || theme == Theme.HighContrastWhite)
{
_iconPath = "Images/ClipboardManager.light.png";
}
else
{
_iconPath = "Images/ClipboardManager.dark.png";
return;
}

_iconPath = "Images/ClipboardManager.dark.png";
}

public System.Windows.Controls.Control CreateSettingPanel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"IsGlobal": false,
"Name": "ClipboardManager",
"Author": "Corey Hayward",
"Version": "0.7.0",
"Version": "0.8.0",
"Language": "csharp",
"Website": "https://github.com/CoreyHayward/PowerToys-Run-ClipboardManager",
"IcoPathDark": "Images\\ClipboardManager.dark.png",
Expand Down

0 comments on commit 6b74fdc

Please sign in to comment.