forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVCWindowChange.cs
More file actions
449 lines (375 loc) · 15.9 KB
/
Copy pathVCWindowChange.cs
File metadata and controls
449 lines (375 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEditorInternal.VersionControl;
using UnityEditor.VersionControl;
using UnityEditor;
namespace UnityEditor.VersionControl
{
// Change window used for either submitting a window, editing a change list or creating a new change list.
internal class WindowChange : EditorWindow
{
ListControl submitList = new ListControl();
AssetList assetList = new AssetList();
ChangeSet changeSet = new ChangeSet();
string description = string.Empty;
bool allowSubmit = false;
Task taskStatus = null;
Task taskDesc = null;
Task taskStat = null;
Task taskSubmit = null;
Task taskAdd = null;
const int kSubmitNotStartedResultCode = 256;
const int kSubmitRunningResultCode = 0;
int submitResultCode = kSubmitNotStartedResultCode;
string submitErrorMessage = null;
int m_TextAreaControlID = 0;
const string c_defaultDescription = "";
public void OnEnable()
{
position = new Rect(100, 100, 700, 395);
minSize = new Vector2(700, 395);
submitList.ReadOnly = true;
taskStatus = null;
taskDesc = null;
taskStat = null;
taskSubmit = null;
submitResultCode = kSubmitNotStartedResultCode;
submitErrorMessage = null;
}
public void OnDisable()
{
m_TextAreaControlID = 0;
}
// Open the change list window for a list of files only
static public void Open(AssetList list, bool submit)
{
Open(null, list, submit);
}
// Perform the actual window open
static public void Open(ChangeSet change, AssetList assets, bool submit)
{
WindowChange win = EditorWindow.GetWindow<WindowChange>(true, "Version Control Changeset");
win.allowSubmit = submit;
win.DoOpen(change, assets);
}
private string SanitizeDescription(string desc)
{
if (Provider.GetActivePlugin() != null && Provider.GetActivePlugin().name != "Perforce")
return desc;
// The format of the desc is "on 2013/02/03 by foo@bar *pending* 'The real description'"
// Extract the part between ' and '
int idx = desc.IndexOf('\'');
if (idx == -1)
return desc;
idx++;
int idx2 = desc.IndexOf('\'', idx);
if (idx2 == -1)
return desc;
return desc.Substring(idx, idx2 - idx).Trim(' ', '\t');
}
// Open the change list window for one of 2 modes. File list or exisiting change list.
// If changeID is null then a list of files is assumed and the user can sumbit them
// as a new change list.
void DoOpen(ChangeSet change, AssetList assets)
{
taskSubmit = null;
submitResultCode = kSubmitNotStartedResultCode;
submitErrorMessage = null;
changeSet = change;
description = change == null ? c_defaultDescription : SanitizeDescription(change.description);
assetList = null;
// Refresh the assets or changeset states
if (change == null)
{
taskStatus = Provider.Status(assets);
}
else
{
taskDesc = Provider.ChangeSetDescription(change);
taskStat = Provider.ChangeSetStatus(change);
}
}
void RefreshList()
{
submitList.Clear();
foreach (Asset it in assetList)
submitList.Add(null, it.prettyPath, it);
if (assetList.Count == 0)
{
ChangeSet change = new ChangeSet(ListControl.c_emptyChangeListMessage);
ListItem item = submitList.Add(null, change.description, change);
item.Dummy = true;
}
submitList.Refresh();
Repaint();
}
internal static void OnSubmitted(Task task)
{
var winsChange = Resources.FindObjectsOfTypeAll(typeof(WindowChange)) as WindowChange[];
if (winsChange.Length == 0)
return; // user closed submit window before submit finished. Just ignore the status callback.
var win = winsChange[0];
win.assetList = task.assetList;
win.submitResultCode = task.resultCode;
win.submitErrorMessage = null;
if ((task.resultCode & (int)SubmitResult.Error) != 0)
{
string delim = "";
foreach (Message msg in task.messages)
{
if (msg.severity == Message.Severity.Error)
win.submitErrorMessage += delim + msg.message;
}
}
if ((task.resultCode & ((int)SubmitResult.OK | (int)SubmitResult.Error)) != 0)
{
WindowPending.UpdateAllWindows();
bool isNewChangeSet = win.changeSet == null;
if (isNewChangeSet)
{
// When change list becomes empty we open it to make "delete empty changessets" button visible
Task flushTask = Provider.Status(""); // Make a dummy task and wait for it since that will guarantee up to date lists
flushTask.Wait();
WindowPending.ExpandLatestChangeSet();
}
}
if ((task.resultCode & (int)SubmitResult.OK) != 0)
win.ResetAndClose();
else
win.RefreshList();
}
internal static void OnAdded(Task task)
{
var winsChange = Resources.FindObjectsOfTypeAll(typeof(WindowChange)) as WindowChange[];
if (winsChange.Length == 0)
return; // user closed submit window before submit finished. Just ignore the status callback.
var win = winsChange[0];
win.taskSubmit = null;
win.submitResultCode = kSubmitNotStartedResultCode;
win.submitErrorMessage = null;
win.taskAdd = null;
// Refetch status
win.taskStatus = Provider.Status(win.assetList, false);
win.assetList = null;
WindowPending.UpdateAllWindows(); // reflect newly added assets in pending window
}
void OnGUI()
{
if ((submitResultCode & (int)SubmitResult.ConflictingFiles) != 0)
OnConflictingFilesGUI();
else if ((submitResultCode & (int)SubmitResult.UnaddedFiles) != 0)
OnUnaddedFilesGUI();
else if ((submitResultCode & (int)SubmitResult.Error) != 0)
OnErrorGUI();
else
OnSubmitGUI();
}
void OnSubmitGUI()
{
bool isSubmitting = submitResultCode != kSubmitNotStartedResultCode;
if (isSubmitting)
GUI.enabled = false;
Event evt = Event.current;
if (evt.isKey && evt.keyCode == KeyCode.Escape)
Close();
GUILayout.Label("Description", EditorStyles.boldLabel);
if (taskStatus != null && taskStatus.resultCode != 0)
{
const bool includeFolders = true;
assetList = taskStatus.assetList.Filter(includeFolders,
Asset.States.CheckedOutLocal,
Asset.States.DeletedLocal,
Asset.States.AddedLocal/*,
Asset.States.Branch,
Asset.States.Integrate*/);
RefreshList();
taskStatus = null;
}
else if (taskDesc != null && taskDesc.resultCode != 0)
{
description = taskDesc.text.Length > 0 ? taskDesc.text : c_defaultDescription;
if (description.Trim() == "<enter description here>")
description = string.Empty;
taskDesc = null;
}
else if (taskStat != null && taskStat.resultCode != 0)
{
assetList = taskStat.assetList;
RefreshList();
taskStat = null;
}
Task progressTask = taskStatus != null && taskStatus.resultCode == 0 ? taskStatus :
taskDesc != null && taskDesc.resultCode == 0 ? taskDesc :
taskStat != null && taskStat.resultCode == 0 ? taskStat : taskSubmit;
GUI.enabled = (taskDesc == null || taskDesc.resultCode != 0) && submitResultCode == kSubmitNotStartedResultCode;
{
description = EditorGUILayout.TextArea(description, GUILayout.Height(150)).Trim();
if (m_TextAreaControlID == 0)
m_TextAreaControlID = EditorGUIUtility.s_LastControlID;
if (m_TextAreaControlID != 0)
{
EditorGUIUtility.keyboardControl = m_TextAreaControlID;
EditorGUIUtility.editingTextField = true;
}
}
GUI.enabled = true;
GUILayout.Label("Files", EditorStyles.boldLabel);
GUILayout.FlexibleSpace();
// I would use GUIUtility.GetLastRect() here after the box but that seems to have wierd side effects.
Rect r1 = new Rect(6, 206, position.width - 12, position.height - 248);
GUILayout.BeginArea(r1);
GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
GUILayout.EndArea();
submitList.OnGUI(new Rect(r1.x + 2, r1.y + 2, r1.width - 4, r1.height - 4), true);
GUILayout.FlexibleSpace();
GUILayout.BeginHorizontal();
if (submitResultCode == kSubmitNotStartedResultCode)
{
if (progressTask != null)
{
// It is possible to have a progressTask for getting description text.
GUIContent c = GUIContent.Temp("Getting info");
c.image = WindowPending.StatusWheel.image;
GUILayout.Label(c);
c.image = null;
}
GUILayout.FlexibleSpace();
if (GUILayout.Button("Cancel"))
ResetAndClose();
GUI.enabled = progressTask == null && !string.IsNullOrEmpty(description);
bool keyboardShortcutActivated = evt.isKey && evt.shift && evt.keyCode == KeyCode.Return;
bool saveByShortcut = keyboardShortcutActivated && !allowSubmit;
if (Provider.hasChangelistSupport && (GUILayout.Button("Save") || saveByShortcut))
Save(false);
if (allowSubmit)
{
bool origEnabled = GUI.enabled;
GUI.enabled = assetList != null && assetList.Count > 0 && !string.IsNullOrEmpty(description);
if (GUILayout.Button("Submit") || keyboardShortcutActivated)
Save(true);
GUI.enabled = origEnabled;
}
}
else
{
// submit finished successfully or running
bool finished = (submitResultCode & (int)SubmitResult.OK) != 0;
GUI.enabled = finished;
string msg = "";
if (finished)
{
msg = "Finished successfully";
}
else if (progressTask != null)
{
GUILayout.Label(WindowPending.StatusWheel);
msg = progressTask.progressMessage;
if (msg.Length == 0)
msg = "Running...";
}
GUILayout.Label(msg);
GUILayout.FlexibleSpace();
if (GUILayout.Button("Close"))
ResetAndClose();
}
GUI.enabled = true;
GUILayout.EndHorizontal();
GUILayout.Space(12);
if (progressTask != null)
Repaint();
}
void OnErrorGUI()
{
GUILayout.Label("Submit failed", EditorStyles.boldLabel);
string msg = "";
if (!string.IsNullOrEmpty(submitErrorMessage))
msg = submitErrorMessage + "\n";
msg += "See console for details. You can get more details by increasing log level in EditorSettings.";
GUILayout.Label(msg);
GUILayout.FlexibleSpace();
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Close"))
{
ResetAndClose();
// The error message may be telling that the plugin is opening a window
// to handle conflicts. After that has been handled and we close this window
// we want to update our state.
WindowPending.UpdateAllWindows();
}
GUILayout.EndHorizontal();
}
void OnConflictingFilesGUI()
{
string files = "";
foreach (Asset a in assetList)
{
if (a.IsState(Asset.States.Conflicted))
{
files += a.prettyPath + "\n";
}
}
GUILayout.Label("Conflicting files", EditorStyles.boldLabel);
GUILayout.Label("Some files need to be resolved before submitting:");
GUI.enabled = false;
GUILayout.TextArea(files, GUILayout.ExpandHeight(true));
GUI.enabled = true;
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Close"))
ResetAndClose();
GUILayout.EndHorizontal();
}
void OnUnaddedFilesGUI()
{
AssetList toAdd = new AssetList();
string files = "";
foreach (Asset a in assetList)
{
if (!a.IsState(Asset.States.OutOfSync) && !a.IsState(Asset.States.Synced) && !a.IsState(Asset.States.AddedLocal))
{
files += a.prettyPath + "\n";
toAdd.Add(a);
}
}
GUILayout.Label("Files to add", EditorStyles.boldLabel);
GUILayout.Label("Some additional files need to be added:");
GUI.enabled = false;
GUILayout.TextArea(files);
GUI.enabled = true;
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Add files"))
{
taskAdd = Provider.Add(toAdd, false);
taskAdd.SetCompletionAction(CompletionAction.OnAddedChangeWindow);
}
if (GUILayout.Button("Abort"))
ResetAndClose();
GUILayout.EndHorizontal();
}
private void ResetAndClose()
{
taskSubmit = null;
submitResultCode = kSubmitNotStartedResultCode;
submitErrorMessage = null;
Close();
}
void Save(bool submit)
{
if (description.Trim() == c_defaultDescription)
{
Debug.LogError("Version control: Please enter a valid change description");
return;
}
UnityEditor.AssetDatabase.SaveAssets();
// Submit the change list. Last parameter is "save only" so invert the submit flag
taskSubmit = Provider.Submit(changeSet, assetList, description, !submit);
submitResultCode = kSubmitRunningResultCode;
submitErrorMessage = null;
taskSubmit.SetCompletionAction(CompletionAction.OnSubmittedChangeWindow); // TODO: make it CompletionAction.OnChangeSetsPendingWindow
}
}
}