9
9
using Flow . Launcher . Plugin . BrowserBookmark . Models ;
10
10
using Flow . Launcher . Plugin . BrowserBookmark . Views ;
11
11
using Flow . Launcher . Plugin . SharedCommands ;
12
+ using System . IO ;
13
+ using System . Threading . Channels ;
14
+ using System . Threading . Tasks ;
12
15
13
16
namespace Flow . Launcher . Plugin . BrowserBookmark
14
17
{
15
- public class Main : ISettingProvider , IPlugin , IReloadable , IPluginI18n , IContextMenu
18
+ public class Main : ISettingProvider , IPlugin , IReloadable , IPluginI18n , IContextMenu , IDisposable
16
19
{
17
20
private PluginInitContext context ;
18
21
19
22
private List < Bookmark > cachedBookmarks = new List < Bookmark > ( ) ;
20
23
21
- private Settings _settings { get ; set ; }
24
+ private Settings _settings { get ; set ; }
22
25
23
26
public void Init ( PluginInitContext context )
24
27
{
25
28
this . context = context ;
26
-
29
+
27
30
_settings = context . API . LoadSettingJsonStorage < Settings > ( ) ;
28
31
29
32
cachedBookmarks = BookmarkLoader . LoadAllBookmarks ( _settings ) ;
33
+
34
+ _ = MonitorRefreshQueue ( ) ;
30
35
}
31
36
32
37
public List < Result > Query ( Query query )
@@ -52,7 +57,10 @@ public List<Result> Query(Query query)
52
57
53
58
return true ;
54
59
} ,
55
- ContextData = new BookmarkAttributes { Url = c . Url }
60
+ ContextData = new BookmarkAttributes
61
+ {
62
+ Url = c . Url
63
+ }
56
64
} ) . Where ( r => r . Score > 0 ) ;
57
65
return returnList . ToList ( ) ;
58
66
}
@@ -69,11 +77,64 @@ public List<Result> Query(Query query)
69
77
context . API . OpenUrl ( c . Url ) ;
70
78
return true ;
71
79
} ,
72
- ContextData = new BookmarkAttributes { Url = c . Url }
80
+ ContextData = new BookmarkAttributes
81
+ {
82
+ Url = c . Url
83
+ }
73
84
} ) . ToList ( ) ;
74
85
}
75
86
}
76
87
88
+
89
+ private static Channel < byte > refreshQueue = Channel . CreateBounded < byte > ( 1 ) ;
90
+
91
+ private async Task MonitorRefreshQueue ( )
92
+ {
93
+ var reader = refreshQueue . Reader ;
94
+ while ( await reader . WaitToReadAsync ( ) )
95
+ {
96
+ await Task . Delay ( 2000 ) ;
97
+ if ( reader . TryRead ( out _ ) )
98
+ {
99
+ ReloadData ( ) ;
100
+ }
101
+ }
102
+ }
103
+
104
+ private static readonly List < FileSystemWatcher > Watchers = new ( ) ;
105
+
106
+ internal static void RegisterBookmarkFile ( string path )
107
+ {
108
+ var directory = Path . GetDirectoryName ( path ) ;
109
+ if ( ! Directory . Exists ( directory ) )
110
+ return ;
111
+ var watcher = new FileSystemWatcher ( directory ! ) ;
112
+ if ( File . Exists ( path ) )
113
+ {
114
+ var fileName = Path . GetFileName ( path ) ;
115
+ watcher . Filter = fileName ;
116
+ }
117
+
118
+ watcher . NotifyFilter = NotifyFilters . FileName |
119
+ NotifyFilters . LastAccess |
120
+ NotifyFilters . LastWrite |
121
+ NotifyFilters . Size ;
122
+
123
+ watcher . Changed += static ( _ , _ ) =>
124
+ {
125
+ refreshQueue . Writer . TryWrite ( default ) ;
126
+ } ;
127
+
128
+ watcher . Renamed += static ( _ , _ ) =>
129
+ {
130
+ refreshQueue . Writer . TryWrite ( default ) ;
131
+ } ;
132
+
133
+ watcher . EnableRaisingEvents = true ;
134
+
135
+ Watchers . Add ( watcher ) ;
136
+ }
137
+
77
138
public void ReloadData ( )
78
139
{
79
140
cachedBookmarks . Clear ( ) ;
@@ -98,7 +159,8 @@ public Control CreateSettingPanel()
98
159
99
160
public List < Result > LoadContextMenus ( Result selectedResult )
100
161
{
101
- return new List < Result > ( ) {
162
+ return new List < Result > ( )
163
+ {
102
164
new Result
103
165
{
104
166
Title = context . API . GetTranslation ( "flowlauncher_plugin_browserbookmark_copyurl_title" ) ,
@@ -114,20 +176,28 @@ public List<Result> LoadContextMenus(Result selectedResult)
114
176
catch ( Exception e )
115
177
{
116
178
var message = "Failed to set url in clipboard" ;
117
- Log . Exception ( "Main" , message , e , "LoadContextMenus" ) ;
179
+ Log . Exception ( "Main" , message , e , "LoadContextMenus" ) ;
118
180
119
181
context . API . ShowMsg ( message ) ;
120
182
121
183
return false ;
122
184
}
123
185
} ,
124
186
IcoPath = "Images\\ copylink.png"
125
- } } ;
187
+ }
188
+ } ;
126
189
}
127
190
128
191
internal class BookmarkAttributes
129
192
{
130
193
internal string Url { get ; set ; }
131
194
}
195
+ public void Dispose ( )
196
+ {
197
+ foreach ( var watcher in Watchers )
198
+ {
199
+ watcher . Dispose ( ) ;
200
+ }
201
+ }
132
202
}
133
- }
203
+ }
0 commit comments