Skip to content

Commit 3c11f4e

Browse files
committedFeb 18, 2025
Add Windows clipboard enabled check
1 parent e5a6ce9 commit 3c11f4e

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed
 

‎src/Flow.Launcher.Plugin.ClipboardPlus.Core/Helpers/WindowsClipboardHelper.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public WindowsClipboardHelper()
8484
{
8585
Clipboard_HistoryChanged(this, null!);
8686
Windows.ApplicationModel.DataTransfer.Clipboard.HistoryChanged += Clipboard_HistoryChanged;
87+
Windows.ApplicationModel.DataTransfer.Clipboard.HistoryEnabledChanged += Clipboard_HistoryEnabledChanged;
8788
}
8889
}
8990

@@ -100,6 +101,8 @@ public void SetClipboardPlus(IClipboardPlus clipboardPlus)
100101
public event EventHandler<string[]>? OnHistoryItemRemoved;
101102
public event EventHandler<ClipboardData>? OnHistoryItemPinUpdated;
102103

104+
public event EventHandler<bool>? OnHistoryEnabledChanged;
105+
103106
private readonly SemaphoreSlim _historyItemLock = new(1, 1);
104107

105108
private readonly List<string> _clipboardHistoryItemsIds = new();
@@ -181,7 +184,12 @@ private void Clipboard_HistoryChanged(object? sender, ClipboardHistoryChangedEve
181184
});
182185
}
183186

184-
#endregion
187+
private void Clipboard_HistoryEnabledChanged(object? sender, object e)
188+
{
189+
OnHistoryEnabledChanged?.Invoke(this, IsHistoryEnabled());
190+
}
191+
192+
#endregion
185193

186194
#region History Items
187195

‎src/Flow.Launcher.Plugin.ClipboardPlus/Main.cs

+42-15
Original file line numberDiff line numberDiff line change
@@ -871,37 +871,35 @@ private void AddClipboardDataItem(ClipboardData clipboardData)
871871
public void RegisterEventsForWindowsClipboardHelper()
872872
{
873873
WindowsClipboardHelper.OnHistoryItemRemoved += WindowsClipboardHelper_OnHistoryItemRemoved;
874+
WindowsClipboardHelper.OnHistoryEnabledChanged += WindowsClipboardHelper_OnHistoryEnabledChanged;
874875
}
875876

876877
public void UnregisterEventsForWindowsClipboardHelper()
877878
{
878879
WindowsClipboardHelper.OnHistoryItemRemoved -= WindowsClipboardHelper_OnHistoryItemRemoved;
880+
WindowsClipboardHelper.OnHistoryEnabledChanged -= WindowsClipboardHelper_OnHistoryEnabledChanged;
879881
}
880882

881-
private async void WindowsClipboardHelper_OnHistoryItemRemoved(object? sender, string[] e)
883+
private void WindowsClipboardHelper_OnHistoryItemRemoved(object? sender, string[] e)
882884
{
883-
await RecordsLock.WaitAsync();
884-
try
885+
_ = RemoveRecordsFromSystemAsync(r => e.Contains(r.HashId));
886+
}
887+
888+
private void WindowsClipboardHelper_OnHistoryEnabledChanged(object? sender, bool e)
889+
{
890+
if (e)
885891
{
886-
var recordsToRemove = RecordsList.Where(r => r.ClipboardData.FromWindowsClipboardHistory() && e.Contains(r.ClipboardData.HashId)).ToList();
887-
while (recordsToRemove.Any())
888-
{
889-
var record = recordsToRemove.First();
890-
RecordsList.Remove(record);
891-
recordsToRemove.Remove(record);
892-
record.Dispose();
893-
}
892+
_ = InitRecordsFromSystemAsync();
894893
}
895-
finally
894+
else
896895
{
897-
RecordsLock.Release();
898-
GarbageCollect();
896+
_ = RemoveRecordsFromSystemAsync();
899897
}
900898
}
901899

902900
#endregion
903901

904-
#region List & Database
902+
#region List & Database & Windows History
905903

906904
public async Task InitRecordsFromDatabaseAndSystemAsync()
907905
{
@@ -974,6 +972,35 @@ public async Task InitRecordsFromSystemAsync()
974972
RecordsLock.Release();
975973
}
976974

975+
private async Task RemoveRecordsFromSystemAsync(Func<ClipboardData, bool>? func = null)
976+
{
977+
await RecordsLock.WaitAsync();
978+
try
979+
{
980+
List<ClipboardDataPair> recordsToRemove;
981+
if (func == null)
982+
{
983+
recordsToRemove = RecordsList.Where(r => r.ClipboardData.FromWindowsClipboardHistory()).ToList();
984+
}
985+
else
986+
{
987+
recordsToRemove = RecordsList.Where(r => r.ClipboardData.FromWindowsClipboardHistory() && func(r.ClipboardData)).ToList();
988+
}
989+
while (recordsToRemove.Any())
990+
{
991+
var record = recordsToRemove.First();
992+
RecordsList.Remove(record);
993+
recordsToRemove.Remove(record);
994+
record.Dispose();
995+
}
996+
}
997+
finally
998+
{
999+
RecordsLock.Release();
1000+
GarbageCollect();
1001+
}
1002+
}
1003+
9771004
private async Task<int> DeleteAllRecordsFromList()
9781005
{
9791006
await RecordsLock.WaitAsync();

0 commit comments

Comments
 (0)