99using Flow . Launcher . Plugin . BrowserBookmark . Models ;
1010using Flow . Launcher . Plugin . BrowserBookmark . Views ;
1111using Flow . Launcher . Plugin . SharedCommands ;
12+ using System . IO ;
13+ using System . Threading . Channels ;
14+ using System . Threading . Tasks ;
1215
1316namespace Flow . Launcher . Plugin . BrowserBookmark
1417{
15- public class Main : ISettingProvider , IPlugin , IReloadable , IPluginI18n , IContextMenu
18+ public class Main : ISettingProvider , IPlugin , IReloadable , IPluginI18n , IContextMenu , IDisposable
1619 {
1720 private PluginInitContext context ;
1821
1922 private List < Bookmark > cachedBookmarks = new List < Bookmark > ( ) ;
2023
21- private Settings _settings { get ; set ; }
24+ private Settings _settings { get ; set ; }
2225
2326 public void Init ( PluginInitContext context )
2427 {
2528 this . context = context ;
26-
29+
2730 _settings = context . API . LoadSettingJsonStorage < Settings > ( ) ;
2831
2932 cachedBookmarks = BookmarkLoader . LoadAllBookmarks ( _settings ) ;
33+
34+ _ = MonitorRefreshQueue ( ) ;
3035 }
3136
3237 public List < Result > Query ( Query query )
@@ -52,7 +57,10 @@ public List<Result> Query(Query query)
5257
5358 return true ;
5459 } ,
55- ContextData = new BookmarkAttributes { Url = c . Url }
60+ ContextData = new BookmarkAttributes
61+ {
62+ Url = c . Url
63+ }
5664 } ) . Where ( r => r . Score > 0 ) ;
5765 return returnList . ToList ( ) ;
5866 }
@@ -69,11 +77,64 @@ public List<Result> Query(Query query)
6977 context . API . OpenUrl ( c . Url ) ;
7078 return true ;
7179 } ,
72- ContextData = new BookmarkAttributes { Url = c . Url }
80+ ContextData = new BookmarkAttributes
81+ {
82+ Url = c . Url
83+ }
7384 } ) . ToList ( ) ;
7485 }
7586 }
7687
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+
77138 public void ReloadData ( )
78139 {
79140 cachedBookmarks . Clear ( ) ;
@@ -98,7 +159,8 @@ public Control CreateSettingPanel()
98159
99160 public List < Result > LoadContextMenus ( Result selectedResult )
100161 {
101- return new List < Result > ( ) {
162+ return new List < Result > ( )
163+ {
102164 new Result
103165 {
104166 Title = context . API . GetTranslation ( "flowlauncher_plugin_browserbookmark_copyurl_title" ) ,
@@ -114,20 +176,28 @@ public List<Result> LoadContextMenus(Result selectedResult)
114176 catch ( Exception e )
115177 {
116178 var message = "Failed to set url in clipboard" ;
117- Log . Exception ( "Main" , message , e , "LoadContextMenus" ) ;
179+ Log . Exception ( "Main" , message , e , "LoadContextMenus" ) ;
118180
119181 context . API . ShowMsg ( message ) ;
120182
121183 return false ;
122184 }
123185 } ,
124186 IcoPath = "Images\\ copylink.png"
125- } } ;
187+ }
188+ } ;
126189 }
127190
128191 internal class BookmarkAttributes
129192 {
130193 internal string Url { get ; set ; }
131194 }
195+ public void Dispose ( )
196+ {
197+ foreach ( var watcher in Watchers )
198+ {
199+ watcher . Dispose ( ) ;
200+ }
201+ }
132202 }
133- }
203+ }
0 commit comments