ArbitratorWindow.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // Copyright (c) 2025 TerraByte Inc.
  2. //
  3. // This script is the View component for the Better Git tool. Its only
  4. // responsibility is to draw the UI based on the state provided by the
  5. // ArbitratorController.
  6. using System;
  7. using System.Linq;
  8. using UnityEngine;
  9. using UnityEditor;
  10. using UnityEngine.Scripting;
  11. namespace Terra.Arbitrator.GUI
  12. {
  13. [Preserve]
  14. public class ArbitratorWindow : EditorWindow
  15. {
  16. private const string DockNextTo = "UnityEditor.GameView,UnityEditor.dll";
  17. internal static ArbitratorWindow Instance { get; private set; }
  18. private ArbitratorController _controller;
  19. private string _commitMessage = "";
  20. private Vector2 _scrollPosition;
  21. private GUIStyle _evenRowStyle;
  22. private bool _stylesInitialized;
  23. [MenuItem("Version Control/Better Git")]
  24. public static void ShowWindow()
  25. {
  26. var type = Type.GetType(DockNextTo);
  27. var window = GetWindow<ArbitratorWindow>(type);
  28. window.titleContent = new GUIContent("Better Git", EditorGUIUtility.IconContent("d_UnityEditor.VersionControl").image);
  29. }
  30. private void OnEnable()
  31. {
  32. Instance = this;
  33. _controller = new ArbitratorController(
  34. requestRepaint: Repaint,
  35. displayDialog: EditorUtility.DisplayDialog,
  36. promptForUnsavedChanges: PromptForUnsavedChanges
  37. );
  38. _controller.OnEnable();
  39. }
  40. private void OnDisable()
  41. {
  42. Instance = null;
  43. }
  44. private void InitializeStyles()
  45. {
  46. if (_stylesInitialized) return;
  47. _evenRowStyle = new GUIStyle();
  48. var texture = new Texture2D(1, 1);
  49. var color = EditorGUIUtility.isProSkin
  50. ? new Color(0.3f, 0.3f, 0.3f, 0.3f)
  51. : new Color(0.8f, 0.8f, 0.8f, 0.5f);
  52. texture.SetPixel(0, 0, color);
  53. texture.Apply();
  54. _evenRowStyle.normal.background = texture;
  55. _stylesInitialized = true;
  56. }
  57. public void TriggerAutoRefresh()
  58. {
  59. if (_controller is { IsLoading: false })
  60. {
  61. _controller.Refresh();
  62. }
  63. }
  64. private void OnGUI()
  65. {
  66. InitializeStyles();
  67. DrawToolbar();
  68. EditorGUILayout.Space();
  69. DrawMessageArea();
  70. DrawMainContent();
  71. }
  72. private void DrawToolbar()
  73. {
  74. EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
  75. EditorGUI.BeginDisabledGroup(_controller.IsLoading);
  76. DrawBranchSelector();
  77. if (GUILayout.Button(new GUIContent("Refresh", EditorGUIUtility.IconContent("Refresh").image), EditorStyles.toolbarButton, GUILayout.Width(80)))
  78. {
  79. _controller.Refresh();
  80. }
  81. if (!_controller.IsInConflictState)
  82. {
  83. var commitsToPull = _controller.CommitsToPull;
  84. EditorGUI.BeginDisabledGroup(commitsToPull == 0);
  85. var pullLabel = $"Pull ({_controller.CommitsToPull})";
  86. if (GUILayout.Button(new GUIContent(pullLabel), EditorStyles.toolbarButton, GUILayout.Width(80)))
  87. {
  88. _controller.Pull();
  89. }
  90. EditorGUI.EndDisabledGroup();
  91. }
  92. EditorGUI.BeginDisabledGroup(!_controller.HasStash);
  93. if (GUILayout.Button("See Stash Changes", EditorStyles.toolbarButton, GUILayout.Width(130)))
  94. {
  95. _controller.ShowStashedChangesWindow();
  96. }
  97. EditorGUI.EndDisabledGroup();
  98. if (_controller.Changes is { Count: > 0 })
  99. {
  100. if (GUILayout.Button("Select All", EditorStyles.toolbarButton, GUILayout.Width(80)))
  101. {
  102. _controller.SetAllSelection(true);
  103. }
  104. if (GUILayout.Button("Deselect All", EditorStyles.toolbarButton, GUILayout.Width(80)))
  105. {
  106. _controller.SetAllSelection(false);
  107. }
  108. GUILayout.FlexibleSpace();
  109. var stashSelections = _controller.Changes?.Count(c => c.IsSelectedForCommit) ?? 0;
  110. EditorGUI.BeginDisabledGroup(stashSelections == 0);
  111. if (GUILayout.Button(new GUIContent("Stash", EditorGUIUtility.IconContent("d_AlphabeticalSorting").image), EditorStyles.toolbarButton, GUILayout.Width(80)))
  112. {
  113. _controller.StashSelectedFiles();
  114. }
  115. EditorGUI.EndDisabledGroup();
  116. var noChanges = _controller.Changes != null && !_controller.Changes.Any();
  117. if (_controller.Changes != null)
  118. {
  119. var selectedCount = _controller.Changes.Count(c => c.IsSelectedForCommit);
  120. EditorGUI.BeginDisabledGroup(selectedCount == 0 || noChanges);
  121. var originalColor = UnityEngine.GUI.backgroundColor;
  122. UnityEngine.GUI.backgroundColor = new Color(1f, 0.5f, 0.5f, 0.8f);
  123. if (GUILayout.Button(new GUIContent($"Reset Selected ({selectedCount})", EditorGUIUtility.IconContent("d_TreeEditor.Trash").image), EditorStyles.toolbarButton, GUILayout.Width(200)))
  124. {
  125. EditorApplication.delayCall += _controller.ResetSelected;
  126. }
  127. UnityEngine.GUI.backgroundColor = originalColor;
  128. EditorGUI.EndDisabledGroup();
  129. }
  130. }
  131. EditorGUI.EndDisabledGroup();
  132. if (_controller.IsLoading)
  133. {
  134. GUILayout.FlexibleSpace();
  135. GUILayout.Label(_controller.LoadingMessage);
  136. }
  137. EditorGUILayout.EndHorizontal();
  138. }
  139. private void DrawBranchSelector()
  140. {
  141. var branches = _controller.RemoteBranchList.ToArray();
  142. var currentIndex = System.Array.IndexOf(branches, _controller.CurrentBranchName);
  143. if (currentIndex == -1) currentIndex = 0;
  144. var newIndex = EditorGUILayout.Popup(currentIndex, branches, EditorStyles.toolbarPopup, GUILayout.Width(150));
  145. if (newIndex == currentIndex) return;
  146. var selectedBranch = branches[newIndex];
  147. EditorApplication.delayCall += () => _controller.SwitchToBranch(selectedBranch);
  148. }
  149. private void DrawMessageArea()
  150. {
  151. if (!string.IsNullOrEmpty(_controller.ErrorMessage))
  152. {
  153. EditorGUILayout.HelpBox(_controller.ErrorMessage, MessageType.Error);
  154. }
  155. else if (!string.IsNullOrEmpty(_controller.InfoMessage) && !_controller.IsLoading)
  156. {
  157. EditorGUILayout.HelpBox(_controller.InfoMessage, MessageType.Info);
  158. }
  159. }
  160. private void DrawMainContent()
  161. {
  162. if (_controller.IsLoading)
  163. {
  164. GUILayout.FlexibleSpace();
  165. if (_controller.OperationProgress > 0)
  166. {
  167. EditorGUILayout.LabelField(_controller.OperationProgressMessage, EditorStyles.centeredGreyMiniLabel);
  168. var rect = EditorGUILayout.GetControlRect(false, 20);
  169. EditorGUI.ProgressBar(rect, _controller.OperationProgress, $"{_controller.OperationProgress:P0}");
  170. }
  171. else
  172. {
  173. EditorGUILayout.BeginHorizontal();
  174. GUILayout.FlexibleSpace();
  175. GUILayout.Label(_controller.LoadingMessage, EditorStyles.largeLabel);
  176. GUILayout.FlexibleSpace();
  177. EditorGUILayout.EndHorizontal();
  178. }
  179. GUILayout.FlexibleSpace();
  180. }
  181. else if (_controller.Changes != null && _controller.Changes.Any())
  182. {
  183. DrawChangesList();
  184. DrawCommitSection();
  185. }
  186. else if (_controller.ErrorMessage == null && _controller.InfoMessage == null)
  187. {
  188. EditorGUILayout.HelpBox("You are up-to-date! No local changes detected.", MessageType.Info);
  189. }
  190. }
  191. private void DrawChangesList()
  192. {
  193. EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
  194. DrawHeaderButton("Commit", SortColumn.Commit, 45);
  195. GUILayout.Space(10);
  196. DrawHeaderButton("Status", SortColumn.Status, 50);
  197. DrawHeaderButton("File Path", SortColumn.FilePath, -1);
  198. EditorGUILayout.LabelField("Actions", GUILayout.Width(100));
  199. EditorGUILayout.EndHorizontal();
  200. _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, GUILayout.ExpandHeight(true));
  201. for (var i = 0; i < _controller.Changes.Count; i++)
  202. {
  203. var change = _controller.Changes[i];
  204. var rowStyle = i % 2 == 0 ? _evenRowStyle : GUIStyle.none;
  205. EditorGUILayout.BeginHorizontal(rowStyle);
  206. GUILayout.Space(15);
  207. if (change.Status == LibGit2Sharp.ChangeKind.Conflicted)
  208. {
  209. EditorGUI.BeginDisabledGroup(true);
  210. EditorGUILayout.Toggle(false, GUILayout.Width(45));
  211. EditorGUI.EndDisabledGroup();
  212. }
  213. else
  214. {
  215. change.IsSelectedForCommit = EditorGUILayout.Toggle(change.IsSelectedForCommit, GUILayout.Width(45));
  216. }
  217. string status;
  218. Color statusColor;
  219. switch (change.Status)
  220. {
  221. case LibGit2Sharp.ChangeKind.Added: status = "[+]"; statusColor = Color.green; break;
  222. case LibGit2Sharp.ChangeKind.Deleted: status = "[-]"; statusColor = Color.red; break;
  223. case LibGit2Sharp.ChangeKind.Modified: status = "[M]"; statusColor = new Color(1.0f, 0.6f, 0.0f); break;
  224. case LibGit2Sharp.ChangeKind.Renamed: status = "[R]"; statusColor = new Color(0.6f, 0.6f, 1.0f); break;
  225. case LibGit2Sharp.ChangeKind.Conflicted: status = "[C]"; statusColor = Color.magenta; break;
  226. default: status = "[?]"; statusColor = Color.white; break;
  227. }
  228. var filePathDisplay = change.Status == LibGit2Sharp.ChangeKind.Renamed
  229. ? $"{change.OldFilePath} -> {change.FilePath}"
  230. : change.FilePath;
  231. var originalColor = UnityEngine.GUI.color;
  232. UnityEngine.GUI.color = statusColor;
  233. EditorGUILayout.LabelField(new GUIContent(status, change.Status.ToString()), GUILayout.Width(50));
  234. UnityEngine.GUI.color = originalColor;
  235. EditorGUILayout.LabelField(new GUIContent(filePathDisplay, filePathDisplay));
  236. EditorGUILayout.BeginHorizontal(GUILayout.Width(120));
  237. EditorGUI.BeginDisabledGroup(_controller.IsLoading);
  238. if (change.Status == LibGit2Sharp.ChangeKind.Conflicted)
  239. {
  240. if (GUILayout.Button("Resolve", GUILayout.Width(70)))
  241. {
  242. EditorApplication.delayCall += () => _controller.ResolveConflict(change);
  243. }
  244. }
  245. else
  246. {
  247. if (GUILayout.Button("Diff", GUILayout.Width(45)))
  248. {
  249. EditorApplication.delayCall += () => _controller.DiffFile(change);
  250. }
  251. }
  252. if (GUILayout.Button(new GUIContent("Reset", "Revert changes for this file"), GUILayout.Width(55)))
  253. {
  254. EditorApplication.delayCall += () => _controller.ResetFile(change);
  255. }
  256. EditorGUI.EndDisabledGroup();
  257. EditorGUILayout.EndHorizontal();
  258. EditorGUILayout.EndHorizontal();
  259. }
  260. EditorGUILayout.EndScrollView();
  261. }
  262. private void DrawHeaderButton(string text, SortColumn column, float width)
  263. {
  264. var label = text;
  265. if (_controller.CurrentSortColumn == column)
  266. {
  267. label = $"[*] {text}";
  268. }
  269. var buttonStyle = new GUIStyle(EditorStyles.label) { alignment = TextAnchor.MiddleLeft };
  270. if (width > 0)
  271. {
  272. if (GUILayout.Button(label, buttonStyle, GUILayout.Width(width))) { _controller.SetSortColumn(column); }
  273. }
  274. else
  275. {
  276. if (GUILayout.Button(label, buttonStyle)) { _controller.SetSortColumn(column); }
  277. }
  278. }
  279. private void DrawCommitSection()
  280. {
  281. EditorGUILayout.Space(10);
  282. if (_controller.Changes.Any(c => c.Status == LibGit2Sharp.ChangeKind.Conflicted))
  283. {
  284. EditorGUILayout.HelpBox("You must resolve all conflicts before you can commit.", MessageType.Warning);
  285. return;
  286. }
  287. EditorGUILayout.LabelField("Commit & Push", EditorStyles.boldLabel);
  288. _commitMessage = EditorGUILayout.TextArea(_commitMessage, GUILayout.Height(60), GUILayout.ExpandWidth(true));
  289. var isPushDisabled = string.IsNullOrWhiteSpace(_commitMessage) || !_controller.Changes.Any(c => c.IsSelectedForCommit);
  290. EditorGUI.BeginDisabledGroup(isPushDisabled);
  291. if (GUILayout.Button("Commit & Push Selected Files", GUILayout.Height(40)))
  292. {
  293. _controller.CommitAndPush(_commitMessage);
  294. _commitMessage = "";
  295. }
  296. EditorGUI.EndDisabledGroup();
  297. if (isPushDisabled)
  298. {
  299. EditorGUILayout.HelpBox("Please enter a commit message and select at least one file.", MessageType.Warning);
  300. }
  301. }
  302. private static UserAction PromptForUnsavedChanges()
  303. {
  304. var result = EditorUtility.DisplayDialogComplex(
  305. "Unsaved Scene Changes",
  306. "You have unsaved changes in the current scene. Would you like to save them before proceeding?",
  307. "Save and Continue",
  308. "Cancel",
  309. "Continue without Saving");
  310. return result switch
  311. {
  312. 0 => UserAction.SaveAndProceed,
  313. 1 => UserAction.Cancel,
  314. 2 => UserAction.Proceed,
  315. _ => UserAction.Cancel
  316. };
  317. }
  318. }
  319. }