@@ -15,6 +15,13 @@ class BranchesView : Subview
1515 private const string ConfirmSwitchMessage = "Switch branch to {0}?" ;
1616 private const string ConfirmSwitchOK = "Switch" ;
1717 private const string ConfirmSwitchCancel = "Cancel" ;
18+ private const string ConfirmCheckoutBranchTitle = "Confirm branch checkout" ;
19+ private const string ConfirmCheckoutBranchMessage = "Checkout branch {0} from {1}?" ;
20+ private const string ConfirmCheckoutBranchOK = "Checkout" ;
21+ private const string ConfirmCheckoutBranchCancel = "Cancel" ;
22+ private const string WarningCheckoutBranchExistsTitle = "Branch already exists" ;
23+ private const string WarningCheckoutBranchExistsMessage = "Branch {0} already exists" ;
24+ private const string WarningCheckoutBranchExistsOK = "Ok" ;
1825 private const string NewBranchCancelButton = "x" ;
1926 private const string NewBranchConfirmButton = "Create" ;
2027 private const string FavoritesSetting = "Favorites" ;
@@ -23,6 +30,10 @@ class BranchesView : Subview
2330 private const string LocalTitle = "Local branches" ;
2431 private const string RemoteTitle = "Remote branches" ;
2532 private const string CreateBranchButton = "New Branch" ;
33+ private const string DeleteBranchMessageFormatString = "Are you sure you want to delete the branch: {0}?" ;
34+ private const string DeleteBranchTitle = "Delete Branch?" ;
35+ private const string DeleteBranchButton = "Delete" ;
36+ private const string CancelButtonLabel = "Cancel" ;
2637
2738 private bool showLocalBranches = true ;
2839 private bool showRemoteBranches = true ;
@@ -147,7 +158,7 @@ public void OnEmbeddedGUI()
147158
148159 GUILayout . BeginHorizontal ( ) ;
149160 {
150- OnCreateGUI ( ) ;
161+ OnButtonBarGUI ( ) ;
151162 }
152163 GUILayout . EndHorizontal ( ) ;
153164
@@ -308,6 +319,9 @@ private void OnRemoteBranchesUpdate(IEnumerable<GitBranch> list)
308319
309320 private void BuildTree ( IEnumerable < GitBranch > local , IEnumerable < GitBranch > remote )
310321 {
322+ //Clear the selected node
323+ selectedNode = null ;
324+
311325 // Sort
312326 var localBranches = new List < GitBranch > ( local ) ;
313327 var remoteBranches = new List < GitBranch > ( remote ) ;
@@ -451,28 +465,28 @@ private void SetFavourite(BranchTreeNode branch, bool favourite)
451465 }
452466 }
453467
454- private void OnCreateGUI ( )
468+ private void OnButtonBarGUI ( )
455469 {
456- // Create button
457470 if ( mode == BranchesMode . Default )
458471 {
472+ // Delete button
459473 // If the current branch is selected, then do not enable the Delete button
460- var disableDelete = activeBranchNode == selectedNode ;
474+ var disableDelete = selectedNode == null || selectedNode . Type == NodeType . Folder || activeBranchNode == selectedNode ;
461475 EditorGUI . BeginDisabledGroup ( disableDelete ) ;
462476 {
463- if ( GUILayout . Button ( "Delete" , EditorStyles . miniButton , GUILayout . ExpandWidth ( false ) ) )
477+ if ( GUILayout . Button ( DeleteBranchButton , EditorStyles . miniButton , GUILayout . ExpandWidth ( false ) ) )
464478 {
465479 var selectedBranchName = selectedNode . Name ;
466- var dialogTitle = "Delete Branch: " + selectedBranchName ;
467- var dialogMessage = "Are you sure you want to delete the branch: " + selectedBranchName + "?" ;
468- if ( EditorUtility . DisplayDialog ( "Delete Branch?" , dialogMessage , "Delete" , "Cancel" ) )
480+ var dialogMessage = string . Format ( DeleteBranchMessageFormatString , selectedBranchName ) ;
481+ if ( EditorUtility . DisplayDialog ( DeleteBranchTitle , dialogMessage , DeleteBranchButton , CancelButtonLabel ) )
469482 {
470483 GitClient . DeleteBranch ( selectedBranchName , true ) . Start ( ) ;
471484 }
472485 }
473486 }
474487 EditorGUI . EndDisabledGroup ( ) ;
475488
489+ // Create button
476490 GUILayout . FlexibleSpace ( ) ;
477491 if ( GUILayout . Button ( CreateBranchButton , EditorStyles . miniButton , GUILayout . ExpandWidth ( false ) ) )
478492 {
@@ -649,37 +663,62 @@ private void OnTreeNodeGUI(BranchTreeNode node)
649663
650664 if ( Event . current . clickCount > 1 && mode == BranchesMode . Default )
651665 {
652- if ( node . Type == NodeType . LocalBranch &&
653- EditorUtility . DisplayDialog ( ConfirmSwitchTitle , String . Format ( ConfirmSwitchMessage , node . Name ) , ConfirmSwitchOK ,
654- ConfirmSwitchCancel ) )
666+ if ( node . Type == NodeType . LocalBranch )
655667 {
656- GitClient . SwitchBranch ( node . Name )
657- . FinallyInUI ( ( success , e ) =>
658- {
659- if ( success )
660- Refresh ( ) ;
661- else
668+ if ( EditorUtility . DisplayDialog ( ConfirmSwitchTitle , String . Format ( ConfirmSwitchMessage , node . Name ) , ConfirmSwitchOK , ConfirmSwitchCancel ) )
669+ {
670+ GitClient . SwitchBranch ( node . Name )
671+ . FinallyInUI ( ( success , e ) =>
662672 {
663- EditorUtility . DisplayDialog ( Localization . SwitchBranchTitle ,
664- String . Format ( Localization . SwitchBranchFailedDescription , node . Name ) ,
665- Localization . Ok ) ;
666- }
667- } ) . Start ( ) ;
673+ if ( success )
674+ {
675+ Refresh ( ) ;
676+ }
677+ else
678+ {
679+ EditorUtility . DisplayDialog ( Localization . SwitchBranchTitle ,
680+ String . Format ( Localization . SwitchBranchFailedDescription , node . Name ) ,
681+ Localization . Ok ) ;
682+ }
683+ } ) . Start ( ) ;
684+ }
668685 }
669686 else if ( node . Type == NodeType . RemoteBranch )
670687 {
671- GitClient . CreateBranch ( selectedNode . Name . Substring ( selectedNode . Name . IndexOf ( '/' ) + 1 ) , selectedNode . Name )
672- . FinallyInUI ( ( success , e ) =>
688+ var indexOfFirstSlash = selectedNode . Name . IndexOf ( '/' ) ;
689+ var originName = selectedNode . Name . Substring ( 0 , indexOfFirstSlash ) ;
690+ var branchName = selectedNode . Name . Substring ( indexOfFirstSlash + 1 ) ;
691+
692+ if ( Repository . LocalBranches . Any ( localBranch => localBranch . Name == branchName ) )
693+ {
694+ EditorUtility . DisplayDialog ( WarningCheckoutBranchExistsTitle ,
695+ String . Format ( WarningCheckoutBranchExistsMessage , branchName ) ,
696+ WarningCheckoutBranchExistsOK ) ;
697+ }
698+ else
699+ {
700+ var confirmCheckout = EditorUtility . DisplayDialog ( ConfirmCheckoutBranchTitle ,
701+ String . Format ( ConfirmCheckoutBranchMessage , node . Name , originName ) ,
702+ ConfirmCheckoutBranchOK , ConfirmCheckoutBranchCancel ) ;
703+
704+ if ( confirmCheckout )
673705 {
674- if ( success )
675- Refresh ( ) ;
676- else
677- {
678- EditorUtility . DisplayDialog ( Localization . SwitchBranchTitle ,
679- String . Format ( Localization . SwitchBranchFailedDescription , node . Name ) ,
680- Localization . Ok ) ;
681- }
682- } ) . Start ( ) ;
706+ GitClient . CreateBranch ( branchName , selectedNode . Name )
707+ . FinallyInUI ( ( success , e ) =>
708+ {
709+ if ( success )
710+ {
711+ Refresh ( ) ;
712+ }
713+ else
714+ {
715+ EditorUtility . DisplayDialog ( Localization . SwitchBranchTitle ,
716+ String . Format ( Localization . SwitchBranchFailedDescription , node . Name ) ,
717+ Localization . Ok ) ;
718+ }
719+ } ) . Start ( ) ;
720+ }
721+ }
683722 }
684723 }
685724 }
0 commit comments