Skip to content

Commit ed326cd

Browse files
committed
multiple fixes
1. auto paste on select 2. convert multiple lines to one line 3. fix plugin thread preventing main program from exiting 4. increase max history count, fix new record not appending after limit reached
1 parent cc1c564 commit ed326cd

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

ClipboardMonitor.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,36 @@ public static void Stop()
2626
ClipboardWatcher.Stop();
2727
}
2828

29+
public static class ClipboardWrapper {
30+
private static T LoopCall<T>(Func<T> func, int timeout = 1) {
31+
T result = default(T);
32+
DateTimeOffset startTime = DateTimeOffset.UtcNow;
33+
while (true) {
34+
try {
35+
result = func();
36+
} catch (ExternalException) {
37+
Thread.Sleep(10);
38+
if (DateTimeOffset.UtcNow - startTime > TimeSpan.FromSeconds(timeout))
39+
return result;
40+
continue;
41+
}
42+
break;
43+
}
44+
return result;
45+
}
46+
47+
public static IDataObject GetDataObject() {
48+
return LoopCall(() => System.Windows.Forms.Clipboard.GetDataObject());
49+
}
50+
51+
public static bool SetText(string text) {
52+
return LoopCall(() => {
53+
System.Windows.Forms.Clipboard.SetText(text);
54+
return true;
55+
});
56+
}
57+
}
58+
2959
class ClipboardWatcher : Form
3060
{
3161
// static instance of this form
@@ -49,6 +79,7 @@ public static void Start()
4979
Application.Run(new ClipboardWatcher());
5080
}));
5181
t.SetApartmentState(ApartmentState.STA); // give the [STAThread] attribute
82+
t.IsBackground = true;
5283
t.Start();
5384
}
5485

@@ -117,7 +148,9 @@ protected override void WndProc(ref Message m)
117148

118149
private void ClipChanged()
119150
{
120-
IDataObject iData = System.Windows.Forms.Clipboard.GetDataObject();
151+
IDataObject iData = ClipboardWrapper.GetDataObject();
152+
if (iData == null)
153+
return;
121154

122155
ClipboardFormat? format = null;
123156

Flow.Launcher.Plugin.ClipboardHistory.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
<ItemGroup>
2626
<PackageReference Include="Flow.Launcher.Plugin" Version="1.3.0" />
27+
<PackageReference Include="InputSimulatorCore" Version="1.0.5" />
2728
</ItemGroup>
2829

2930
</Project>

Main.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,47 @@
22
using System.Linq;
33
using System.Collections.Generic;
44
using Flow.Launcher.Plugin;
5+
using System.Threading.Tasks;
6+
using WindowsInput;
7+
using WindowsInput.Native;
58

69
namespace Flow.Launcher.Plugin.ClipboardHistory
710
{
811
public class ClipboardHistory : IPlugin
912
{
10-
private const int MaxDataCount = 300;
13+
private const int MaxDataCount = 5000;
1114
//private readonly KeyboardSimulator keyboardSimulator = new KeyboardSimulator(new InputSimulator());
15+
private readonly InputSimulator inputSimulator = new InputSimulator();
1216
private PluginInitContext context;
13-
List<string> dataList = new List<string>();
17+
LinkedList<string> dataList = new LinkedList<string>();
1418

1519
public List<Result> Query(Query query)
1620
{
1721
var results = new List<Result>();
18-
List<string> displayData;
22+
IEnumerable<string> displayData;
1923

2024
if (query.Terms.Length == 0)
2125
{
2226
displayData = dataList;
2327
}
2428
else
2529
{
26-
displayData = dataList.Where(i => i.ToLower().Contains(query.SecondToEndSearch.ToLower()))
27-
.ToList();
30+
displayData = dataList.Where(i => i.ToLower().Contains(query.SecondToEndSearch.ToLower()));
2831
}
2932

3033
results.AddRange(displayData.Select(o => new Result
3134
{
32-
Title = o.Trim(),
35+
Title = o.Trim().Replace("\r\n", " ").Replace('\n', ' '),
3336
IcoPath = "Images\\clipboard.png",
3437
Action = c =>
3538
{
36-
try
37-
{
38-
System.Windows.Forms.Clipboard.SetText(o);
39-
return true;
40-
}
41-
catch (Exception e)
42-
{
43-
context.API.ShowMsg("Error", e.Message, null);
39+
if (!ClipboardMonitor.ClipboardWrapper.SetText(o))
4440
return false;
45-
}
41+
42+
Task.Delay(50).ContinueWith(t => inputSimulator.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V));
43+
return true;
4644
}
47-
}).Reverse());
45+
}));
4846
return results;
4947
}
5048

@@ -64,15 +62,16 @@ void ClipboardMonitor_OnClipboardChange(ClipboardFormat format, object data)
6462
{
6563
if (data != null && !string.IsNullOrEmpty(data.ToString().Trim()))
6664
{
67-
if (dataList.Contains(data.ToString()))
65+
LinkedListNode<string> node = dataList.Find(data.ToString());
66+
if (node != null)
6867
{
69-
dataList.Remove(data.ToString());
68+
dataList.Remove(node);
7069
}
71-
dataList.Add(data.ToString());
70+
dataList.AddFirst(data.ToString());
7271

7372
if (dataList.Count > MaxDataCount)
7473
{
75-
dataList.Remove(dataList.Last());
74+
dataList.RemoveLast();
7675
}
7776
}
7877
}

0 commit comments

Comments
 (0)