ArbitratorWindow.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 pullLabel = "Pull";
  84. if (_controller.CommitsToPull > 0)
  85. {
  86. pullLabel = $"Pull ({_controller.CommitsToPull})";
  87. }
  88. if (GUILayout.Button(new GUIContent(pullLabel), EditorStyles.toolbarButton, GUILayout.Width(80)))
  89. {
  90. _controller.Pull();
  91. }
  92. }
  93. if (_controller.Changes is { Count: > 0 })
  94. {
  95. if (GUILayout.Button("Select All", EditorStyles.toolbarButton, GUILayout.Width(80)))
  96. {
  97. _controller.SetAllSelection(true);
  98. }
  99. if (GUILayout.Button("Deselect All", EditorStyles.toolbarButton, GUILayout.Width(80)))
  100. {
  101. _controller.SetAllSelection(false);
  102. }
  103. GUILayout.FlexibleSpace();
  104. var noChanges = !_controller.Changes.Any();
  105. EditorGUI.BeginDisabledGroup(noChanges);
  106. var selectedCount = _controller.Changes.Count(c => c.IsSelectedForCommit);
  107. EditorGUI.BeginDisabledGroup(selectedCount == 0);
  108. var originalColor = UnityEngine.GUI.backgroundColor;
  109. UnityEngine.GUI.backgroundColor = new Color(1f, 0.5f, 0.5f, 0.8f);
  110. if (GUILayout.Button(new GUIContent($"Reset Selected ({selectedCount})", EditorGUIUtility.IconContent("d_TreeEditor.Trash").image), EditorStyles.toolbarButton, GUILayout.Width(130)))
  111. {
  112. EditorApplication.delayCall += _controller.ResetSelected;
  113. }
  114. UnityEngine.GUI.backgroundColor = originalColor;
  115. EditorGUI.EndDisabledGroup();
  116. EditorGUI.EndDisabledGroup();
  117. }
  118. EditorGUI.EndDisabledGroup();
  119. if (_controller.IsLoading)
  120. {
  121. GUILayout.FlexibleSpace();
  122. GUILayout.Label(_controller.LoadingMessage);
  123. }
  124. EditorGUILayout.EndHorizontal();
  125. }
  126. private void DrawBranchSelector()
  127. {
  128. var branches = _controller.RemoteBranchList.ToArray();
  129. var currentIndex = System.Array.IndexOf(branches, _controller.CurrentBranchName);
  130. if (currentIndex == -1) currentIndex = 0;
  131. var newIndex = EditorGUILayout.Popup(currentIndex, branches, EditorStyles.toolbarPopup, GUILayout.Width(150));
  132. if (newIndex == currentIndex) return;
  133. var selectedBranch = branches[newIndex];
  134. EditorApplication.delayCall += () => _controller.SwitchToBranch(selectedBranch);
  135. }
  136. private void DrawMessageArea()
  137. {
  138. if (!string.IsNullOrEmpty(_controller.ErrorMessage))
  139. {
  140. EditorGUILayout.HelpBox(_controller.ErrorMessage, MessageType.Error);
  141. }
  142. else if (!string.IsNullOrEmpty(_controller.InfoMessage) && !_controller.IsLoading)
  143. {
  144. EditorGUILayout.HelpBox(_controller.InfoMessage, MessageType.Info);
  145. }
  146. }
  147. private void DrawMainContent()
  148. {
  149. if (_controller.IsLoading)
  150. {
  151. GUILayout.FlexibleSpace();
  152. if (_controller.OperationProgress > 0)
  153. {
  154. EditorGUILayout.LabelField(_controller.OperationProgressMessage, EditorStyles.centeredGreyMiniLabel);
  155. var rect = EditorGUILayout.GetControlRect(false, 20);
  156. EditorGUI.ProgressBar(rect, _controller.OperationProgress, $"{_controller.OperationProgress:P0}");
  157. }
  158. else
  159. {
  160. EditorGUILayout.BeginHorizontal();
  161. GUILayout.FlexibleSpace();
  162. GUILayout.Label(_controller.LoadingMessage, EditorStyles.largeLabel);
  163. GUILayout.FlexibleSpace();
  164. EditorGUILayout.EndHorizontal();
  165. }
  166. GUILayout.FlexibleSpace();
  167. }
  168. else if (_controller.Changes != null && _controller.Changes.Any())
  169. {
  170. DrawChangesList();
  171. DrawCommitSection();
  172. }
  173. else if (_controller.ErrorMessage == null)
  174. {
  175. EditorGUILayout.HelpBox("You are up-to-date! No local changes detected.", MessageType.Info);
  176. }
  177. }
  178. private void DrawChangesList()
  179. {
  180. EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
  181. DrawHeaderButton("Commit", SortColumn.Commit, 45);
  182. GUILayout.Space(10);
  183. DrawHeaderButton("Status", SortColumn.Status, 50);
  184. DrawHeaderButton("File Path", SortColumn.FilePath, -1);
  185. EditorGUILayout.LabelField("Actions", GUILayout.Width(100));
  186. EditorGUILayout.EndHorizontal();
  187. _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, GUILayout.ExpandHeight(true));
  188. for (var i = 0; i < _controller.Changes.Count; i++)
  189. {
  190. var change = _controller.Changes[i];
  191. var rowStyle = i % 2 == 0 ? _evenRowStyle : GUIStyle.none;
  192. EditorGUILayout.BeginHorizontal(rowStyle);
  193. GUILayout.Space(15);
  194. if (change.Status == LibGit2Sharp.ChangeKind.Conflicted)
  195. {
  196. EditorGUI.BeginDisabledGroup(true);
  197. EditorGUILayout.Toggle(false, GUILayout.Width(45));
  198. EditorGUI.EndDisabledGroup();
  199. }
  200. else
  201. {
  202. change.IsSelectedForCommit = EditorGUILayout.Toggle(change.IsSelectedForCommit, GUILayout.Width(45));
  203. }
  204. string status;
  205. Color statusColor;
  206. switch (change.Status)
  207. {
  208. case LibGit2Sharp.ChangeKind.Added: status = "[+]"; statusColor = Color.green; break;
  209. case LibGit2Sharp.ChangeKind.Deleted: status = "[-]"; statusColor = Color.red; break;
  210. case LibGit2Sharp.ChangeKind.Modified: status = "[M]"; statusColor = new Color(1.0f, 0.6f, 0.0f); break;
  211. case LibGit2Sharp.ChangeKind.Renamed: status = "[R]"; statusColor = new Color(0.6f, 0.6f, 1.0f); break;
  212. case LibGit2Sharp.ChangeKind.Conflicted: status = "[C]"; statusColor = Color.magenta; break;
  213. default: status = "[?]"; statusColor = Color.white; break;
  214. }
  215. var filePathDisplay = change.Status == LibGit2Sharp.ChangeKind.Renamed
  216. ? $"{change.OldFilePath} -> {change.FilePath}"
  217. : change.FilePath;
  218. var originalColor = UnityEngine.GUI.color;
  219. UnityEngine.GUI.color = statusColor;
  220. EditorGUILayout.LabelField(new GUIContent(status, change.Status.ToString()), GUILayout.Width(50));
  221. UnityEngine.GUI.color = originalColor;
  222. EditorGUILayout.LabelField(new GUIContent(filePathDisplay, filePathDisplay));
  223. EditorGUILayout.BeginHorizontal(GUILayout.Width(120));
  224. EditorGUI.BeginDisabledGroup(_controller.IsLoading);
  225. if (change.Status == LibGit2Sharp.ChangeKind.Conflicted)
  226. {
  227. if (GUILayout.Button("Resolve", GUILayout.Width(70)))
  228. {
  229. EditorApplication.delayCall += () => _controller.ResolveConflict(change);
  230. }
  231. }
  232. else
  233. {
  234. if (GUILayout.Button("Diff", GUILayout.Width(45)))
  235. {
  236. EditorApplication.delayCall += () => _controller.DiffFile(change);
  237. }
  238. }
  239. if (GUILayout.Button(new GUIContent("Reset", "Revert changes for this file"), GUILayout.Width(55)))
  240. {
  241. EditorApplication.delayCall += () => _controller.ResetFile(change);
  242. }
  243. EditorGUI.EndDisabledGroup();
  244. EditorGUILayout.EndHorizontal();
  245. EditorGUILayout.EndHorizontal();
  246. }
  247. EditorGUILayout.EndScrollView();
  248. }
  249. private void DrawHeaderButton(string text, SortColumn column, float width)
  250. {
  251. var label = text;
  252. if (_controller.CurrentSortColumn == column)
  253. {
  254. label = $"[*] {text}";
  255. }
  256. var buttonStyle = new GUIStyle(EditorStyles.label) { alignment = TextAnchor.MiddleLeft };
  257. if (width > 0)
  258. {
  259. if (GUILayout.Button(label, buttonStyle, GUILayout.Width(width))) { _controller.SetSortColumn(column); }
  260. }
  261. else
  262. {
  263. if (GUILayout.Button(label, buttonStyle)) { _controller.SetSortColumn(column); }
  264. }
  265. }
  266. private void DrawCommitSection()
  267. {
  268. EditorGUILayout.Space(10);
  269. if (_controller.Changes.Any(c => c.Status == LibGit2Sharp.ChangeKind.Conflicted))
  270. {
  271. EditorGUILayout.HelpBox("You must resolve all conflicts before you can commit.", MessageType.Warning);
  272. return;
  273. }
  274. EditorGUILayout.LabelField("Commit & Push", EditorStyles.boldLabel);
  275. _commitMessage = EditorGUILayout.TextArea(_commitMessage, GUILayout.Height(60), GUILayout.ExpandWidth(true));
  276. var isPushDisabled = string.IsNullOrWhiteSpace(_commitMessage) || !_controller.Changes.Any(c => c.IsSelectedForCommit);
  277. EditorGUI.BeginDisabledGroup(isPushDisabled);
  278. if (GUILayout.Button("Commit & Push Selected Files", GUILayout.Height(40)))
  279. {
  280. _controller.CommitAndPush(_commitMessage);
  281. _commitMessage = "";
  282. }
  283. EditorGUI.EndDisabledGroup();
  284. if (isPushDisabled)
  285. {
  286. EditorGUILayout.HelpBox("Please enter a commit message and select at least one file.", MessageType.Warning);
  287. }
  288. }
  289. private static UserAction PromptForUnsavedChanges()
  290. {
  291. var result = EditorUtility.DisplayDialogComplex(
  292. "Unsaved Scene Changes",
  293. "You have unsaved changes in the current scene. Would you like to save them before proceeding?",
  294. "Save and Continue",
  295. "Cancel",
  296. "Continue without Saving");
  297. return result switch
  298. {
  299. 0 => UserAction.SaveAndProceed,
  300. 1 => UserAction.Cancel,
  301. 2 => UserAction.Proceed,
  302. _ => UserAction.Cancel
  303. };
  304. }
  305. }
  306. }