ArbitratorWindow.cs 13 KB

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