@@ -32,6 +32,9 @@ class BranchesView : Subview
32
32
private const string DeleteBranchTitle = "Delete Branch?" ;
33
33
private const string DeleteBranchButton = "Delete" ;
34
34
private const string CancelButtonLabel = "Cancel" ;
35
+ private const string DeleteBranchContextMenuLabel = "Delete" ;
36
+ private const string SwitchBranchContextMenuLabel = "Switch" ;
37
+ private const string CheckoutBranchContextMenuLabel = "Checkout" ;
35
38
36
39
[ NonSerialized ] private int listID = - 1 ;
37
40
[ NonSerialized ] private BranchesMode targetMode ;
@@ -226,12 +229,7 @@ private void OnButtonBarGUI()
226
229
{
227
230
if ( GUILayout . Button ( DeleteBranchButton , EditorStyles . miniButton , GUILayout . ExpandWidth ( false ) ) )
228
231
{
229
- var selectedBranchName = treeLocals . SelectedNode . Name ;
230
- var dialogMessage = string . Format ( DeleteBranchMessageFormatString , selectedBranchName ) ;
231
- if ( EditorUtility . DisplayDialog ( DeleteBranchTitle , dialogMessage , DeleteBranchButton , CancelButtonLabel ) )
232
- {
233
- GitClient . DeleteBranch ( selectedBranchName , true ) . Start ( ) ;
234
- }
232
+ DeleteLocalBranch ( treeLocals . SelectedNode . Name ) ;
235
233
}
236
234
}
237
235
EditorGUI . EndDisabledGroup ( ) ;
@@ -345,26 +343,20 @@ private void OnTreeGUI(Rect rect)
345
343
346
344
var treeHadFocus = treeLocals . SelectedNode != null ;
347
345
348
- rect = treeLocals . Render ( rect , scroll , _ => { } , node =>
349
- {
350
- if ( EditorUtility . DisplayDialog ( ConfirmSwitchTitle , String . Format ( ConfirmSwitchMessage , node . Name ) , ConfirmSwitchOK ,
351
- ConfirmSwitchCancel ) )
352
- {
353
- GitClient . SwitchBranch ( node . Name )
354
- . FinallyInUI ( ( success , e ) =>
355
- {
356
- if ( success )
357
- {
358
- Redraw ( ) ;
359
- }
360
- else
361
- {
362
- EditorUtility . DisplayDialog ( Localization . SwitchBranchTitle ,
363
- String . Format ( Localization . SwitchBranchFailedDescription , node . Name ) ,
364
- Localization . Ok ) ;
365
- }
366
- } ) . Start ( ) ;
367
- }
346
+ rect = treeLocals . Render ( rect , scroll ,
347
+ node => { } ,
348
+ node => {
349
+ if ( node . IsFolder )
350
+ return ;
351
+
352
+ SwitchBranch ( node . Name ) ;
353
+ } ,
354
+ node => {
355
+ if ( node . IsFolder )
356
+ return ;
357
+
358
+ var menu = CreateContextMenuForLocalBranchNode ( node ) ;
359
+ menu . ShowAsContext ( ) ;
368
360
} ) ;
369
361
370
362
if ( treeHadFocus && treeLocals . SelectedNode == null )
@@ -379,55 +371,26 @@ private void OnTreeGUI(Rect rect)
379
371
380
372
rect . y += Styles . TreePadding ;
381
373
382
- rect = treeRemotes . Render ( rect , scroll , _ => { } , selectedNode =>
383
- {
384
- var indexOfFirstSlash = selectedNode . Name . IndexOf ( '/' ) ;
385
- var originName = selectedNode . Name . Substring ( 0 , indexOfFirstSlash ) ;
386
- var branchName = selectedNode . Name . Substring ( indexOfFirstSlash + 1 ) ;
374
+ rect = treeRemotes . Render ( rect , scroll ,
375
+ node => { } ,
376
+ node => {
377
+ if ( node . IsFolder )
378
+ return ;
387
379
388
- if ( Repository . LocalBranches . Any ( localBranch => localBranch . Name == branchName ) )
389
- {
390
- EditorUtility . DisplayDialog ( WarningCheckoutBranchExistsTitle ,
391
- String . Format ( WarningCheckoutBranchExistsMessage , branchName ) ,
392
- WarningCheckoutBranchExistsOK ) ;
393
- }
394
- else
395
- {
396
- var confirmCheckout = EditorUtility . DisplayDialog ( ConfirmCheckoutBranchTitle ,
397
- String . Format ( ConfirmCheckoutBranchMessage , selectedNode . Name , originName ) ,
398
- ConfirmCheckoutBranchOK ,
399
- ConfirmCheckoutBranchCancel ) ;
380
+ CheckoutRemoteBranch ( node . Name ) ;
381
+ } ,
382
+ node => {
383
+ if ( node . IsFolder )
384
+ return ;
400
385
401
- if ( confirmCheckout )
402
- {
403
- GitClient
404
- . CreateBranch ( branchName , selectedNode . Name )
405
- . FinallyInUI ( ( success , e ) =>
406
- {
407
- if ( success )
408
- {
409
- Redraw ( ) ;
410
- }
411
- else
412
- {
413
- EditorUtility . DisplayDialog ( Localization . SwitchBranchTitle ,
414
- String . Format ( Localization . SwitchBranchFailedDescription , selectedNode . Name ) ,
415
- Localization . Ok ) ;
416
- }
417
- } )
418
- . Start ( ) ;
419
- }
420
- }
386
+ var menu = CreateContextMenuForRemoteBranchNode ( node ) ;
387
+ menu . ShowAsContext ( ) ;
421
388
} ) ;
422
389
423
390
if ( treeHadFocus && treeRemotes . SelectedNode == null )
424
- {
425
391
treeLocals . Focus ( ) ;
426
- }
427
392
else if ( ! treeHadFocus && treeRemotes . SelectedNode != null )
428
- {
429
393
treeLocals . Blur ( ) ;
430
- }
431
394
432
395
if ( treeRemotes . RequiresRepaint )
433
396
Redraw ( ) ;
@@ -436,18 +399,109 @@ private void OnTreeGUI(Rect rect)
436
399
GUILayout . Space ( rect . y - initialRect . y ) ;
437
400
}
438
401
439
- private int CompareBranches ( GitBranch a , GitBranch b )
402
+ private GenericMenu CreateContextMenuForLocalBranchNode ( TreeNode node )
403
+ {
404
+ var genericMenu = new GenericMenu ( ) ;
405
+
406
+ var deleteGuiContent = new GUIContent ( DeleteBranchContextMenuLabel ) ;
407
+ var switchGuiContent = new GUIContent ( SwitchBranchContextMenuLabel ) ;
408
+
409
+ if ( node . IsActive )
410
+ {
411
+ genericMenu . AddDisabledItem ( deleteGuiContent ) ;
412
+ genericMenu . AddDisabledItem ( switchGuiContent ) ;
413
+ }
414
+ else
415
+ {
416
+ genericMenu . AddItem ( deleteGuiContent , false , ( ) => {
417
+ DeleteLocalBranch ( node . Name ) ;
418
+ } ) ;
419
+
420
+ genericMenu . AddItem ( switchGuiContent , false , ( ) => {
421
+ SwitchBranch ( node . Name ) ;
422
+ } ) ;
423
+ }
424
+
425
+ return genericMenu ;
426
+ }
427
+
428
+ private GenericMenu CreateContextMenuForRemoteBranchNode ( TreeNode node )
429
+ {
430
+ var genericMenu = new GenericMenu ( ) ;
431
+
432
+ var checkoutGuiContent = new GUIContent ( CheckoutBranchContextMenuLabel ) ;
433
+
434
+ genericMenu . AddItem ( checkoutGuiContent , false , ( ) => {
435
+ CheckoutRemoteBranch ( node . Name ) ;
436
+ } ) ;
437
+
438
+ return genericMenu ;
439
+ }
440
+
441
+ private void CheckoutRemoteBranch ( string branch )
442
+ {
443
+ var indexOfFirstSlash = branch . IndexOf ( '/' ) ;
444
+ var originName = branch . Substring ( 0 , indexOfFirstSlash ) ;
445
+ var branchName = branch . Substring ( indexOfFirstSlash + 1 ) ;
446
+
447
+ if ( Repository . LocalBranches . Any ( localBranch => localBranch . Name == branchName ) )
448
+ {
449
+ EditorUtility . DisplayDialog ( WarningCheckoutBranchExistsTitle ,
450
+ String . Format ( WarningCheckoutBranchExistsMessage , branchName ) , WarningCheckoutBranchExistsOK ) ;
451
+ }
452
+ else
453
+ {
454
+ var confirmCheckout = EditorUtility . DisplayDialog ( ConfirmCheckoutBranchTitle ,
455
+ String . Format ( ConfirmCheckoutBranchMessage , branch , originName ) , ConfirmCheckoutBranchOK ,
456
+ ConfirmCheckoutBranchCancel ) ;
457
+
458
+ if ( confirmCheckout )
459
+ {
460
+ GitClient . CreateBranch ( branchName , branch ) . FinallyInUI ( ( success , e ) => {
461
+ if ( success )
462
+ {
463
+ Redraw ( ) ;
464
+ }
465
+ else
466
+ {
467
+ EditorUtility . DisplayDialog ( Localization . SwitchBranchTitle ,
468
+ String . Format ( Localization . SwitchBranchFailedDescription , branch ) , Localization . Ok ) ;
469
+ }
470
+ } ) . Start ( ) ;
471
+ }
472
+ }
473
+ }
474
+
475
+ private void SwitchBranch ( string branch )
440
476
{
441
- //if (IsFavorite(a.Name))
442
- //{
443
- // return -1;
444
- //}
477
+ if ( EditorUtility . DisplayDialog ( ConfirmSwitchTitle , String . Format ( ConfirmSwitchMessage , branch ) , ConfirmSwitchOK ,
478
+ ConfirmSwitchCancel ) )
479
+ {
480
+ GitClient . SwitchBranch ( branch ) . FinallyInUI ( ( success , e ) => {
481
+ if ( success )
482
+ {
483
+ Redraw ( ) ;
484
+ }
485
+ else
486
+ {
487
+ EditorUtility . DisplayDialog ( Localization . SwitchBranchTitle ,
488
+ String . Format ( Localization . SwitchBranchFailedDescription , branch ) , Localization . Ok ) ;
489
+ }
490
+ } ) . Start ( ) ;
491
+ }
492
+ }
445
493
446
- //if (IsFavorite(b.Name))
447
- //{
448
- // return 1;
449
- //}
494
+ private void DeleteLocalBranch ( string branch )
495
+ {
496
+ var dialogMessage = string . Format ( DeleteBranchMessageFormatString , branch ) ;
497
+ if ( EditorUtility . DisplayDialog ( DeleteBranchTitle , dialogMessage , DeleteBranchButton , CancelButtonLabel ) )
498
+ {
499
+ GitClient . DeleteBranch ( branch , true ) . Start ( ) ;
500
+ }
501
+ }
450
502
503
+ private int CompareBranches ( GitBranch a , GitBranch b )
504
+ {
451
505
if ( a . Name . Equals ( "master" ) )
452
506
{
453
507
return - 1 ;
@@ -461,31 +515,6 @@ private int CompareBranches(GitBranch a, GitBranch b)
461
515
return a . Name . CompareTo ( b . Name ) ;
462
516
}
463
517
464
- //private bool IsFavorite(string branchName)
465
- //{
466
- // return !String.IsNullOrEmpty(branchName) && favoritesList.Contains(branchName);
467
- //}
468
-
469
- //private void SetFavorite(TreeNode branch, bool favorite)
470
- //{
471
- // if (string.IsNullOrEmpty(branch.Name))
472
- // {
473
- // return;
474
- // }
475
-
476
- // if (!favorite)
477
- // {
478
- // favorites.Remove(branch);
479
- // Manager.LocalSettings.Set(FavoritesSetting, favorites.Select(x => x.Name).ToList());
480
- // }
481
- // else
482
- // {
483
- // favorites.Remove(branch);
484
- // favorites.Add(branch);
485
- // Manager.LocalSettings.Set(FavoritesSetting, favorites.Select(x => x.Name).ToList());
486
- // }
487
- //}
488
-
489
518
public override bool IsBusy
490
519
{
491
520
get { return false ; }
0 commit comments