A game about forced loneliness, made by TACStudios
1using UnityEditor;
2using UnityEngine;
3
4using Codice.Client.Common.EventTracking;
5using Codice.CM.Common;
6using PlasticGui;
7using PlasticGui.WorkspaceWindow.Diff;
8using Unity.PlasticSCM.Editor.UI;
9using Unity.PlasticSCM.Editor.Tool;
10
11namespace Unity.PlasticSCM.Editor.Views.Diff
12{
13 internal class DiffTreeViewMenu
14 {
15 internal interface IMetaMenuOperations
16 {
17 bool SelectionHasMeta();
18 void DiffMeta();
19 void HistoryMeta();
20 }
21
22 internal DiffTreeViewMenu(
23 IDiffTreeViewMenuOperations operations,
24 IMetaMenuOperations metaMenuOperations)
25 {
26 mOperations = operations;
27 mMetaMenuOperations = metaMenuOperations;
28 BuildComponents();
29 }
30
31 internal void Popup()
32 {
33 GenericMenu menu = new GenericMenu();
34
35 UpdateMenuItems(menu);
36
37 menu.ShowAsContext();
38 }
39
40 internal bool ProcessKeyActionIfNeeded(Event e)
41 {
42 DiffTreeViewMenuOperations operationToExecute = GetMenuOperation(e);
43
44 if (operationToExecute == DiffTreeViewMenuOperations.None)
45 return false;
46
47 SelectedDiffsGroupInfo info =
48 mOperations.GetSelectedDiffsGroupInfo();
49
50 DiffTreeViewMenuOperations operations =
51 DiffTreeViewMenuUpdater.GetAvailableMenuOperations(info);
52
53 if (!operations.HasFlag(operationToExecute))
54 return false;
55
56 ProcessMenuOperation(operationToExecute);
57 return true;
58 }
59
60 void SaveRevisionAsMenuItem_Click()
61 {
62 mOperations.SaveRevisionAs();
63 }
64
65 void DiffMenuItem_Click()
66 {
67 mOperations.Diff();
68 }
69
70 void DiffMetaMenuItem_Click()
71 {
72 mMetaMenuOperations.DiffMeta();
73 }
74
75 void HistoryMenuItem_Click()
76 {
77 mOperations.History();
78 }
79
80 void HistoryMetaMenuItem_Click()
81 {
82 mMetaMenuOperations.HistoryMeta();
83 }
84
85 void RevertMenuItem_Click()
86 {
87 mOperations.RevertChanges();
88 }
89
90 void UndeleteMenuItem_Click()
91 {
92 mOperations.Undelete();
93 }
94
95 void UndeleteToSpecifiedPathMenuItem_Click()
96 {
97 mOperations.UndeleteToSpecifiedPaths();
98 }
99
100 void CopyFilePathMenuItem_Click()
101 {
102 mOperations.CopyFilePath(relativePath: false);
103 }
104
105 void CopyRelativeFilePathMenuItem_Click()
106 {
107 mOperations.CopyFilePath(relativePath: true);
108 }
109
110 void UpdateMenuItems(GenericMenu menu)
111 {
112 SelectedDiffsGroupInfo groupInfo =
113 mOperations.GetSelectedDiffsGroupInfo();
114
115 DiffTreeViewMenuOperations operations =
116 DiffTreeViewMenuUpdater.GetAvailableMenuOperations(groupInfo);
117
118 if (operations == DiffTreeViewMenuOperations.None)
119 {
120 menu.AddDisabledItem(GetNoActionMenuItemContent());
121 return;
122 }
123
124 bool isMultipleSelection = groupInfo.SelectedItemsCount > 1;
125 bool selectionHasMeta = mMetaMenuOperations.SelectionHasMeta();
126
127 if (operations.HasFlag(DiffTreeViewMenuOperations.SaveAs))
128 menu.AddItem(mSaveRevisionAsMenuItemContent, false, SaveRevisionAsMenuItem_Click);
129
130 if (operations.HasFlag(DiffTreeViewMenuOperations.Diff))
131 menu.AddItem(mDiffMenuItemContent, false, DiffMenuItem_Click);
132 else
133 menu.AddDisabledItem(mDiffMenuItemContent, false);
134
135 if (mMetaMenuOperations.SelectionHasMeta())
136 {
137 if (operations.HasFlag(DiffTreeViewMenuOperations.Diff))
138 menu.AddItem(mDiffMetaMenuItemContent, false, DiffMetaMenuItem_Click);
139 else
140 menu.AddDisabledItem(mDiffMetaMenuItemContent);
141 }
142
143 menu.AddSeparator(string.Empty);
144
145 if (operations.HasFlag(DiffTreeViewMenuOperations.History))
146 menu.AddItem(mViewHistoryMenuItemContent, false, HistoryMenuItem_Click);
147 else
148 menu.AddDisabledItem(mViewHistoryMenuItemContent, false);
149
150 if (mMetaMenuOperations.SelectionHasMeta())
151 {
152 if (operations.HasFlag(DiffTreeViewMenuOperations.History))
153 menu.AddItem(mViewHistoryMetaMenuItemContent, false, HistoryMetaMenuItem_Click);
154 else
155 menu.AddDisabledItem(mViewHistoryMetaMenuItemContent, false);
156 }
157
158 if (operations.HasFlag(DiffTreeViewMenuOperations.RevertChanges))
159 {
160 menu.AddSeparator(string.Empty);
161
162 mRevertMenuItemContent.text = GetRevertMenuItemText(
163 isMultipleSelection,
164 selectionHasMeta);
165
166 menu.AddItem(mRevertMenuItemContent, false, RevertMenuItem_Click);
167 }
168
169 if (operations.HasFlag(DiffTreeViewMenuOperations.Undelete) ||
170 operations.HasFlag(DiffTreeViewMenuOperations.UndeleteToSpecifiedPaths))
171 {
172 menu.AddSeparator(string.Empty);
173 }
174
175 if (operations.HasFlag(DiffTreeViewMenuOperations.Undelete))
176 {
177 mUndeleteMenuItemContent.text = GetUndeleteMenuItemText(
178 isMultipleSelection,
179 selectionHasMeta);
180
181 menu.AddItem(mUndeleteMenuItemContent, false, UndeleteMenuItem_Click);
182 }
183
184 if (operations.HasFlag(DiffTreeViewMenuOperations.UndeleteToSpecifiedPaths))
185 {
186 mUndeleteToSpecifiedPathMenuItemContent.text = GetUndeleteToSpecifiedPathMenuItemText(
187 isMultipleSelection,
188 selectionHasMeta);
189
190 menu.AddItem(mUndeleteToSpecifiedPathMenuItemContent, false, UndeleteToSpecifiedPathMenuItem_Click);
191 }
192
193 if (operations.HasFlag(DiffTreeViewMenuOperations.CopyFilePath))
194 {
195 menu.AddSeparator(string.Empty);
196
197 menu.AddItem(
198 mCopyFilePathMenuItemContent, false, CopyFilePathMenuItem_Click);
199 menu.AddItem(
200 mCopyRelativeFilePathMenuItemContent, false, CopyRelativeFilePathMenuItem_Click);
201 }
202 }
203
204 GUIContent GetNoActionMenuItemContent()
205 {
206 if (mNoActionMenuItemContent == null)
207 {
208 mNoActionMenuItemContent = new GUIContent(
209 PlasticLocalization.GetString(PlasticLocalization.
210 Name.NoActionMenuItem));
211 }
212
213 return mNoActionMenuItemContent;
214 }
215
216 static string GetRevertMenuItemText(
217 bool isMultipleSelection,
218 bool selectionHasMeta)
219 {
220 if (selectionHasMeta && !isMultipleSelection)
221 return PlasticLocalization.GetString(PlasticLocalization.Name.RevertThisFilePlusMeta);
222
223 return isMultipleSelection ?
224 PlasticLocalization.GetString(PlasticLocalization.Name.RevertSelectedFiles) :
225 PlasticLocalization.GetString(PlasticLocalization.Name.RevertThisFile);
226 }
227
228 static string GetUndeleteMenuItemText(
229 bool isMultipleSelection,
230 bool selectionHasMeta)
231 {
232 if (selectionHasMeta && !isMultipleSelection)
233 return PlasticLocalization.GetString(PlasticLocalization.Name.UndeleteRevisionPlusMeta);
234
235 return isMultipleSelection ?
236 PlasticLocalization.GetString(PlasticLocalization.Name.UndeleteSelectedRevisions) :
237 PlasticLocalization.GetString(PlasticLocalization.Name.UndeleteRevisions);
238 }
239
240 static string GetUndeleteToSpecifiedPathMenuItemText(
241 bool isMultipleSelection,
242 bool selectionHasMeta)
243 {
244 if (selectionHasMeta && !isMultipleSelection)
245 return PlasticLocalization.GetString(PlasticLocalization.Name.UndeleteRevisionPlusMetaPath);
246
247 return isMultipleSelection ?
248 PlasticLocalization.GetString(PlasticLocalization.Name.UndeleteSelectedRevisionsPaths) :
249 PlasticLocalization.GetString(PlasticLocalization.Name.UndeleteRevisionPath);
250 }
251
252 void ProcessMenuOperation(DiffTreeViewMenuOperations operationToExecute)
253 {
254 if (operationToExecute == DiffTreeViewMenuOperations.SaveAs)
255 {
256 SaveRevisionAsMenuItem_Click();
257 return;
258 }
259
260 if (operationToExecute == DiffTreeViewMenuOperations.Diff)
261 {
262 DiffMenuItem_Click();
263 return;
264 }
265
266 if (operationToExecute == DiffTreeViewMenuOperations.History)
267 {
268 HistoryMenuItem_Click();
269 }
270 }
271
272 static DiffTreeViewMenuOperations GetMenuOperation(Event e)
273 {
274 if (Keyboard.IsControlOrCommandKeyPressed(e) && Keyboard.IsKeyPressed(e, KeyCode.D))
275 return DiffTreeViewMenuOperations.Diff;
276
277 if (Keyboard.IsControlOrCommandKeyPressed(e) && Keyboard.IsKeyPressed(e, KeyCode.H))
278 return DiffTreeViewMenuOperations.History;
279
280 return DiffTreeViewMenuOperations.None;
281 }
282
283 void BuildComponents()
284 {
285 mSaveRevisionAsMenuItemContent = new GUIContent(
286 PlasticLocalization.GetString(PlasticLocalization.Name.DiffMenuItemSaveRevisionAs));
287 mDiffMenuItemContent = new GUIContent(
288 string.Format("{0} {1}",
289 PlasticLocalization.GetString(PlasticLocalization.Name.DiffMenuItem),
290 GetPlasticShortcut.ForDiff()));
291 mDiffMetaMenuItemContent = new GUIContent(
292 PlasticLocalization.GetString(PlasticLocalization.Name.DiffMetaMenuItem));
293 mViewHistoryMenuItemContent = new GUIContent(
294 string.Format("{0} {1}",
295 PlasticLocalization.GetString(PlasticLocalization.Name.ViewHistoryMenuItem),
296 GetPlasticShortcut.ForHistory()));
297 mViewHistoryMetaMenuItemContent = new GUIContent(
298 PlasticLocalization.GetString(PlasticLocalization.Name.ViewHistoryMetaMenuItem));
299 mRevertMenuItemContent = new GUIContent();
300 mUndeleteMenuItemContent = new GUIContent();
301 mUndeleteToSpecifiedPathMenuItemContent = new GUIContent();
302 mCopyFilePathMenuItemContent = new GUIContent(PlasticLocalization.Name.CopyFilePathMenuItem.GetString());
303 mCopyRelativeFilePathMenuItemContent =
304 new GUIContent(PlasticLocalization.Name.CopyRelativeFilePathMenuItem.GetString());
305 }
306
307 GUIContent mNoActionMenuItemContent;
308
309 GUIContent mSaveRevisionAsMenuItemContent;
310 GUIContent mDiffMenuItemContent;
311 GUIContent mDiffMetaMenuItemContent;
312 GUIContent mViewHistoryMenuItemContent;
313 GUIContent mViewHistoryMetaMenuItemContent;
314 GUIContent mRevertMenuItemContent;
315 GUIContent mUndeleteMenuItemContent;
316 GUIContent mUndeleteToSpecifiedPathMenuItemContent;
317 GUIContent mCopyFilePathMenuItemContent;
318 GUIContent mCopyRelativeFilePathMenuItemContent;
319
320 readonly IDiffTreeViewMenuOperations mOperations;
321 readonly IMetaMenuOperations mMetaMenuOperations;
322 }
323}