6
6
using System . Diagnostics ;
7
7
using RepoZ . Api . Common . Common ;
8
8
using RepoZ . Api . Common . Git . AutoFetch ;
9
+ using RepoZ . Api . Common . Git ;
10
+ using System . IO ;
9
11
10
12
namespace RepoZ . App . Mac
11
13
{
12
14
public partial class PopupViewController : AppKit . NSViewController
13
15
{
14
16
private const int MENU_MANAGE = 4711 ;
15
17
private const int MENU_AUTOFETCH = 4712 ;
16
- private const int MENU_PINGBACK = 4713 ;
18
+ private const int MENU_ADVANCED = 4713 ;
19
+ private const int MENU_PINGBACK = 4714 ;
17
20
18
21
private IRepositoryInformationAggregator _aggregator ;
19
22
private IRepositoryMonitor _monitor ;
20
23
private IAppSettingsService _appSettingsService ;
21
24
private IRepositoryIgnoreStore _repositoryIgnoreStore ;
22
25
private ITranslationService _translationService ;
26
+ private IRepositoryActionConfigurationStore _actionConfigurationStore ;
23
27
private HeaderMetrics _initialHeaderMetrics ;
24
28
25
29
#region Constructors
@@ -74,6 +78,7 @@ public override void ViewWillAppear()
74
78
_appSettingsService = container . Resolve < IAppSettingsService > ( ) ;
75
79
_repositoryIgnoreStore = container . Resolve < IRepositoryIgnoreStore > ( ) ;
76
80
_translationService = container . Resolve < ITranslationService > ( ) ;
81
+ _actionConfigurationStore = container . Resolve < IRepositoryActionConfigurationStore > ( ) ;
77
82
78
83
// Do any additional setup after loading the view.
79
84
var datasource = new RepositoryTableDataSource ( _aggregator . Repositories ) ;
@@ -204,6 +209,7 @@ private void CreateMenu()
204
209
205
210
new NSMenuItem ( _translationService . Translate ( "ManageRepos" ) ) { Tag = MENU_MANAGE } ,
206
211
new NSMenuItem ( _translationService . Translate ( "AutoFetch" ) ) { Tag = MENU_AUTOFETCH } ,
212
+ new NSMenuItem ( _translationService . Translate ( "Advanced" ) ) { Tag = MENU_ADVANCED } ,
207
213
NSMenuItem . SeparatorItem ,
208
214
new NSMenuItem ( _translationService . Translate ( "PingBack" ) ) { Tag = MENU_PINGBACK } ,
209
215
NSMenuItem . SeparatorItem ,
@@ -218,6 +224,8 @@ private void CreateMenu()
218
224
new NSMenuItem ( _translationService . Translate ( "ScanMac" ) , async ( s , e ) => await _monitor . ScanForLocalRepositoriesAsync ( ) ) ,
219
225
new NSMenuItem ( _translationService . Translate ( "Clear" ) , ( s , e ) => _monitor . Reset ( ) ) ,
220
226
NSMenuItem . SeparatorItem ,
227
+ new NSMenuItem ( _translationService . Translate ( "CustomizeRepositoryActions" ) , HandleCustomizeRepositoryAction ) ,
228
+ NSMenuItem . SeparatorItem ,
221
229
new NSMenuItem ( _translationService . Translate ( "ResetIgnoreRules" ) , ( s , e ) => _repositoryIgnoreStore . Reset ( ) )
222
230
} ;
223
231
@@ -241,6 +249,16 @@ private void CreateMenu()
241
249
foreach ( var item in autoFetchItems )
242
250
autoFetchItem . Submenu . AddItem ( item ) ;
243
251
252
+ var advancedItems = new NSMenuItem [ ] {
253
+ new CheckableMenuItem ( _translationService . Translate ( "PruneOnFetch" ) , value => _appSettingsService . PruneOnFetch = value , ( ) => _appSettingsService . PruneOnFetch )
254
+ } ;
255
+
256
+ var advancedItem = MenuButton . Menu . ItemWithTag ( MENU_ADVANCED ) ;
257
+ advancedItem . Submenu = new NSMenu { AutoEnablesItems = false } ;
258
+
259
+ foreach ( var item in advancedItems )
260
+ advancedItem . Submenu . AddItem ( item ) ;
261
+
244
262
var pingbackItems = new NSMenuItem [ ] {
245
263
new NSMenuItem ( _translationService . Translate ( "Donate" ) , ( s , e ) => Navigate ( "https://github.com/sponsors/awaescher" ) ) ,
246
264
NSMenuItem . SeparatorItem ,
@@ -257,8 +275,9 @@ private void CreateMenu()
257
275
258
276
partial void MenuButton_Click ( NSObject sender )
259
277
{
260
- var currentMode = _appSettingsService . AutoFetchMode ;
278
+ var currentAutoFetchMode = _appSettingsService . AutoFetchMode ;
261
279
var autoFetchItem = MenuButton . Menu . ItemWithTag ( MENU_AUTOFETCH ) ;
280
+ var advancedItem = MenuButton . Menu . ItemWithTag ( MENU_ADVANCED ) ;
262
281
263
282
for ( int i = 0 ; i < autoFetchItem . Submenu . Count ; i ++ )
264
283
{
@@ -268,18 +287,43 @@ partial void MenuButton_Click(NSObject sender)
268
287
continue ;
269
288
270
289
var itemMode = ( AutoFetchMode ) Enum . Parse ( typeof ( AutoFetchMode ) , item . Identifier ) ;
271
- item . State = itemMode == currentMode ? NSCellStateValue . On : NSCellStateValue . Off ;
290
+ item . State = itemMode == currentAutoFetchMode ? NSCellStateValue . On : NSCellStateValue . Off ;
272
291
}
273
292
293
+ UpdateCheckableItems ( MenuButton . Menu ) ;
294
+
274
295
MenuButton . Menu . PopUpMenu ( null , new CoreGraphics . CGPoint ( ) { X = 0 , Y = MenuButton . Frame . Height } , MenuButton ) ;
275
296
}
276
297
298
+ void UpdateCheckableItems ( NSMenu menu )
299
+ {
300
+ if ( menu is null )
301
+ return ;
302
+
303
+ foreach ( var item in menu . Items )
304
+ {
305
+ ( item as CheckableMenuItem ) ? . Update ( ) ;
306
+ UpdateCheckableItems ( item . Submenu ) ;
307
+ }
308
+ }
309
+
277
310
void HandleAutoFetchChange ( object sender , EventArgs e )
278
311
{
279
312
var newMode = ( AutoFetchMode ) Enum . Parse ( typeof ( AutoFetchMode ) , ( sender as NSMenuItem ) . Identifier ) ;
280
313
_appSettingsService . AutoFetchMode = newMode ;
281
314
}
282
315
316
+ void HandleCustomizeRepositoryAction ( object sender , EventArgs e )
317
+ {
318
+ Navigate ( "https://github.com/awaescher/RepoZ-RepositoryActions" ) ;
319
+
320
+ var fileName = ( ( FileRepositoryStore ) _actionConfigurationStore ) . GetFileName ( ) ;
321
+ var directoryName = Path . GetDirectoryName ( fileName ) ;
322
+
323
+ if ( Directory . Exists ( directoryName ) )
324
+ Process . Start ( directoryName ) ;
325
+ }
326
+
283
327
void Datasource_CollectionChanged ( object sender , System . Collections . Specialized . NotifyCollectionChangedEventArgs e )
284
328
{
285
329
this . InvokeOnMainThread ( SetControlVisibilityByRepositoryAvailability ) ;
@@ -334,5 +378,31 @@ private class HeaderMetrics
334
378
public nfloat MenuButtonLeft { get ; set ; }
335
379
public nfloat SearchBoxWidth { get ; set ; }
336
380
}
381
+
382
+ private class CheckableMenuItem : NSMenuItem
383
+ {
384
+ public CheckableMenuItem ( string title , Action < bool > onChange , Func < bool > isChecked ) : base ( title )
385
+ {
386
+ OnChange = onChange ?? throw new ArgumentNullException ( nameof ( onChange ) ) ;
387
+ IsChecked = isChecked ?? throw new ArgumentNullException ( nameof ( isChecked ) ) ;
388
+
389
+ base . Activated += CheckableMenuItem_Activated ;
390
+ }
391
+
392
+ private void CheckableMenuItem_Activated ( object sender , EventArgs e )
393
+ {
394
+ State = ( State == NSCellStateValue . On ) ? NSCellStateValue . Off : NSCellStateValue . On ;
395
+ OnChange . Invoke ( State == NSCellStateValue . On ) ;
396
+ }
397
+
398
+ public void Update ( )
399
+ {
400
+ State = IsChecked . Invoke ( ) ? NSCellStateValue . On : NSCellStateValue . Off ;
401
+ }
402
+
403
+ public Action < bool > OnChange { get ; }
404
+
405
+ public Func < bool > IsChecked { get ; }
406
+ }
337
407
}
338
408
}
0 commit comments