-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.cs
89 lines (77 loc) · 3.44 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Linq;
using System.Collections.Generic;
using Flow.Launcher.Plugin;
using System.Threading.Tasks;
using WindowsInput;
using WindowsInput.Native;
namespace Flow.Launcher.Plugin.ClipboardHistory {
public class ClipboardHistory : IPlugin, IDisposable {
private const int MaxDataCount = 1000;
private readonly InputSimulator inputSimulator = new InputSimulator();
private PluginInitContext context;
private int currentScore = 1;
LinkedList<ClipboardData> dataList = new LinkedList<ClipboardData>();
public struct ClipboardData : IEquatable<ClipboardData> {
public object data;
public string text;
public string displayText;
public int score;
public override bool Equals(object obj) => obj is ClipboardData && Equals((ClipboardData)obj);
public bool Equals(ClipboardData other) => text.Equals(other.text);
public override int GetHashCode() => text.GetHashCode();
}
public List<Result> Query(Query query) {
IEnumerable<ClipboardData> displayData;
if (query.Search.Trim().Length == 0) {
displayData = dataList;
} else {
displayData = dataList.Where(i => i.text.ToLower().Contains(query.Search.ToLower()));
}
var results = new List<Result>();
results.AddRange(displayData.Select(o => new Result {
Title = o.displayText,
IcoPath = "Images\\clipboard.png",
Score = o.score,
Action = c => {
if (!ClipboardMonitor.ClipboardWrapper.SetDataObject(o.data)) {
return false;
}
Task.Delay(50).ContinueWith(t => inputSimulator.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V));
return true;
}
}));
return results;
}
public void Init(PluginInitContext context) {
this.context = context;
ClipboardMonitor.OnClipboardChange += ClipboardMonitor_OnClipboardChange;
ClipboardMonitor.Start();
}
void ClipboardMonitor_OnClipboardChange(ClipboardFormat format, object data) {
if (format == ClipboardFormat.Html ||
format == ClipboardFormat.SymbolicLink ||
format == ClipboardFormat.Text ||
format == ClipboardFormat.UnicodeText) {
if (data != null && !string.IsNullOrEmpty(data.ToString().Trim())) {
ClipboardData obj = new ClipboardData { };
obj.data = data;
obj.text = data.ToString();
obj.displayText = obj.text.Trim().Replace("\r\n", " ").Replace('\n', ' ');
obj.score = currentScore++ * 1000;
LinkedListNode<ClipboardData> node = dataList.Find(obj);
if (node != null) {
dataList.Remove(node);
}
dataList.AddFirst(obj);
if (dataList.Count > MaxDataCount) {
dataList.RemoveLast();
}
}
}
}
public void Dispose() {
ClipboardMonitor.Stop();
}
}
}