@@ -18,8 +18,33 @@ class GitPathView : Subview
18
18
private const string GitInstallPickInvalidMessage = "The selected file is not a valid Git install. {0}" ;
19
19
private const string GitInstallFindButton = "Find install" ;
20
20
private const string GitInstallPickInvalidOK = "OK" ;
21
+ private const string PathToGit = "Path to Git" ;
22
+ private const string GitPathSaveButton = "Save Path" ;
23
+
24
+ [ SerializeField ] private string gitExec ;
25
+ [ SerializeField ] private string gitExecParent ;
26
+ [ SerializeField ] private string gitExecExtension ;
27
+ [ SerializeField ] private string newGitExec ;
28
+ [ NonSerialized ] private bool gitExecHasChanged ;
21
29
22
30
[ NonSerialized ] private bool isBusy ;
31
+ public override void OnEnable ( )
32
+ {
33
+ base . OnEnable ( ) ;
34
+
35
+ gitExecHasChanged = true ;
36
+ }
37
+
38
+ public override void OnDisable ( )
39
+ {
40
+ base . OnDisable ( ) ;
41
+ }
42
+
43
+ public override void OnDataUpdate ( )
44
+ {
45
+ base . OnDataUpdate ( ) ;
46
+ MaybeUpdateData ( ) ;
47
+ }
23
48
24
49
public override bool IsBusy
25
50
{
@@ -28,111 +53,113 @@ public override bool IsBusy
28
53
29
54
public override void OnGUI ( )
30
55
{
31
- string gitExecPath = null ;
32
- string gitExecParentPath = null ;
33
-
34
- string extension = null ;
35
-
36
- if ( Environment != null )
37
- {
38
- extension = Environment . ExecutableExtension ;
39
-
40
- if ( Environment . IsWindows )
41
- {
42
- extension = extension . TrimStart ( '.' ) ;
43
- }
44
-
45
- if ( Environment . GitExecutablePath != null )
46
- {
47
- gitExecPath = Environment . GitExecutablePath . ToString ( ) ;
48
- gitExecParentPath = Environment . GitExecutablePath . Parent . ToString ( ) ;
49
- }
50
-
51
- if ( gitExecParentPath == null )
52
- {
53
- gitExecParentPath = Environment . GitInstallPath ;
54
- }
55
- }
56
-
57
56
// Install path
58
57
GUILayout . Label ( GitInstallTitle , EditorStyles . boldLabel ) ;
59
58
60
- EditorGUI . BeginDisabledGroup ( IsBusy || Parent . IsBusy || gitExecPath == null ) ;
59
+ EditorGUI . BeginDisabledGroup ( IsBusy || Parent . IsBusy ) ;
61
60
{
62
61
// Install path field
63
- EditorGUI . BeginChangeCheck ( ) ;
62
+ // EditorGUI.BeginChangeCheck();
64
63
{
65
64
GUILayout . BeginHorizontal ( ) ;
66
- gitExecPath = EditorGUILayout . TextField ( "Path to Git" , gitExecPath ) ;
67
- if ( GUILayout . Button ( BrowseButton , EditorStyles . miniButton , GUILayout . Width ( 25 ) ) )
68
65
{
69
- var newValue = EditorUtility . OpenFilePanel ( GitInstallBrowseTitle , gitExecParentPath , extension ) ;
66
+ newGitExec = EditorGUILayout . TextField ( PathToGit , newGitExec ) ;
70
67
71
- if ( ! string . IsNullOrEmpty ( newValue ) )
68
+ if ( GUILayout . Button ( BrowseButton , EditorStyles . miniButton , GUILayout . Width ( 25 ) ) )
72
69
{
73
- isBusy = true ;
70
+ GUI . FocusControl ( null ) ;
74
71
75
- var validateGitInstall = ! string . IsNullOrEmpty ( newValue ) ;
76
- if ( validateGitInstall )
77
- {
78
- var nPath = newValue . ToNPath ( ) ;
79
- if ( ! nPath . FileExists ( ) )
80
- {
81
- EditorUtility . DisplayDialog ( GitInstallPickInvalidTitle ,
82
- String . Format ( GitInstallPickInvalidMessage , newValue ) ,
83
- GitInstallPickInvalidOK ) ;
72
+ var newValue = EditorUtility . OpenFilePanel ( GitInstallBrowseTitle ,
73
+ gitExecParent ,
74
+ gitExecExtension ) ;
84
75
85
- validateGitInstall = false ;
86
- }
87
- }
88
-
89
- if ( validateGitInstall )
76
+ if ( ! string . IsNullOrEmpty ( newValue ) )
90
77
{
91
- gitExecPath = newValue ;
92
- GUIUtility . keyboardControl = GUIUtility . hotControl = 0 ;
93
- GUI . changed = true ;
78
+ newGitExec = newValue ;
94
79
}
95
80
}
96
81
}
97
82
GUILayout . EndHorizontal ( ) ;
98
83
}
99
84
100
- if ( EditorGUI . EndChangeCheck ( ) )
85
+ GUILayout . Space ( EditorGUIUtility . standardVerticalSpacing ) ;
86
+
87
+ GUILayout . BeginHorizontal ( ) ;
101
88
{
102
- Logger . Trace ( "Setting GitExecPath: " + gitExecPath ) ;
89
+ var needsSaving = ! string . IsNullOrEmpty ( newGitExec )
90
+ && newGitExec != gitExec
91
+ && newGitExec . ToNPath ( ) . FileExists ( ) ;
103
92
104
- Manager . SystemSettings . Set ( Constants . GitInstallPathKey , gitExecPath ) ;
105
- Environment . GitExecutablePath = gitExecPath . ToNPath ( ) ;
93
+ EditorGUI . BeginDisabledGroup ( ! needsSaving ) ;
94
+ {
95
+ if ( GUILayout . Button ( GitPathSaveButton , GUILayout . ExpandWidth ( false ) ) )
96
+ {
97
+ Logger . Trace ( "Saving Git Path:{0}" , newGitExec ) ;
106
98
107
- isBusy = false ;
108
- }
99
+ GUI . FocusControl ( null ) ;
109
100
110
- GUILayout . Space ( EditorGUIUtility . standardVerticalSpacing ) ;
101
+ Manager . SystemSettings . Set ( Constants . GitInstallPathKey , newGitExec ) ;
102
+ Environment . GitExecutablePath = newGitExec . ToNPath ( ) ;
103
+ gitExecHasChanged = true ;
104
+ }
105
+ }
106
+ EditorGUI . EndDisabledGroup ( ) ;
111
107
112
- GUILayout . BeginHorizontal ( ) ;
113
- {
114
- // Find button - for attempting to locate a new install
108
+ //Find button - for attempting to locate a new install
115
109
if ( GUILayout . Button ( GitInstallFindButton , GUILayout . ExpandWidth ( false ) ) )
116
110
{
117
- isBusy = true ;
111
+ GUI . FocusControl ( null ) ;
118
112
119
113
var task = new ProcessTask < NPath > ( Manager . CancellationToken , new FirstLineIsPathOutputProcessor ( ) )
120
114
. Configure ( Manager . ProcessManager , Environment . IsWindows ? "where" : "which" , "git" )
121
115
. FinallyInUI ( ( success , ex , path ) =>
122
116
{
117
+ Logger . Trace ( "Find Git Completed Success:{0} Path:{1}" , success , path ) ;
118
+
123
119
if ( success && ! string . IsNullOrEmpty ( path ) )
124
120
{
121
+ Manager . SystemSettings . Set ( Constants . GitInstallPathKey , path ) ;
125
122
Environment . GitExecutablePath = path ;
126
- GUIUtility . keyboardControl = GUIUtility . hotControl = 0 ;
123
+ gitExecHasChanged = true ;
127
124
}
128
-
129
- isBusy = false ;
130
125
} ) ;
131
126
}
132
127
}
133
128
GUILayout . EndHorizontal ( ) ;
134
129
}
135
130
EditorGUI . EndDisabledGroup ( ) ;
136
131
}
132
+
133
+ private void MaybeUpdateData ( )
134
+ {
135
+ if ( gitExecHasChanged )
136
+ {
137
+ if ( Environment != null )
138
+ {
139
+ if ( gitExecExtension == null )
140
+ {
141
+ gitExecExtension = Environment . ExecutableExtension ;
142
+
143
+ if ( Environment . IsWindows )
144
+ {
145
+ gitExecExtension = gitExecExtension . TrimStart ( '.' ) ;
146
+ }
147
+ }
148
+
149
+ if ( Environment . GitExecutablePath != null )
150
+ {
151
+ newGitExec = gitExec = Environment . GitExecutablePath . ToString ( ) ;
152
+ gitExecParent = Environment . GitExecutablePath . Parent . ToString ( ) ;
153
+ }
154
+
155
+ if ( gitExecParent == null )
156
+ {
157
+ gitExecParent = Environment . GitInstallPath ;
158
+ }
159
+ }
160
+
161
+ gitExecHasChanged = false ;
162
+ }
163
+ }
137
164
}
138
165
}
0 commit comments