22using System . Linq ;
33using System . Collections . Generic ;
44using Flow . Launcher . Plugin ;
5+ using System . Threading . Tasks ;
6+ using WindowsInput ;
7+ using WindowsInput . Native ;
58
69namespace 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