ArbitratorWindow.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. EditorGUILayout.BeginHorizontal();
  131. GUILayout.FlexibleSpace();
  132. GUILayout.Label("Loading...", EditorStyles.largeLabel);
  133. GUILayout.FlexibleSpace();
  134. EditorGUILayout.EndHorizontal();
  135. GUILayout.FlexibleSpace();
  136. }
  137. else if (_controller.Changes != null && _controller.Changes.Any())
  138. {
  139. DrawChangesList();
  140. DrawCommitSection();
  141. }
  142. else
  143. {
  144. EditorGUILayout.HelpBox("You are up-to-date! No local changes detected.", MessageType.Info);
  145. }
  146. }
  147. private void DrawChangesList()
  148. {
  149. EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
  150. DrawHeaderButton("Commit", SortColumn.Commit, 45);
  151. GUILayout.Space(10);
  152. DrawHeaderButton("Status", SortColumn.Status, 50);
  153. DrawHeaderButton("File Path", SortColumn.FilePath, -1);
  154. EditorGUILayout.LabelField("Actions", GUILayout.Width(100));
  155. EditorGUILayout.EndHorizontal();
  156. _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, GUILayout.ExpandHeight(true));
  157. for (var i = 0; i < _controller.Changes.Count; i++)
  158. {
  159. var change = _controller.Changes[i];
  160. var rowStyle = i % 2 == 0 ? _evenRowStyle : GUIStyle.none;
  161. EditorGUILayout.BeginHorizontal(rowStyle);
  162. GUILayout.Space(15);
  163. if (change.Status == LibGit2Sharp.ChangeKind.Conflicted)
  164. {
  165. EditorGUI.BeginDisabledGroup(true);
  166. EditorGUILayout.Toggle(false, GUILayout.Width(45));
  167. EditorGUI.EndDisabledGroup();
  168. }
  169. else
  170. {
  171. change.IsSelectedForCommit = EditorGUILayout.Toggle(change.IsSelectedForCommit, GUILayout.Width(45));
  172. }
  173. string status;
  174. Color statusColor;
  175. switch (change.Status)
  176. {
  177. case LibGit2Sharp.ChangeKind.Added: status = "[+]"; statusColor = Color.green; break;
  178. case LibGit2Sharp.ChangeKind.Deleted: status = "[-]"; statusColor = Color.red; break;
  179. case LibGit2Sharp.ChangeKind.Modified: status = "[M]"; statusColor = new Color(1.0f, 0.6f, 0.0f); break;
  180. case LibGit2Sharp.ChangeKind.Renamed: status = "[R]"; statusColor = new Color(0.6f, 0.6f, 1.0f); break;
  181. case LibGit2Sharp.ChangeKind.Conflicted: status = "[C]"; statusColor = Color.magenta; break;
  182. default: status = "[?]"; statusColor = Color.white; break;
  183. }
  184. var filePathDisplay = change.Status == LibGit2Sharp.ChangeKind.Renamed
  185. ? $"{change.OldFilePath} -> {change.FilePath}"
  186. : change.FilePath;
  187. var originalColor = UnityEngine.GUI.color;
  188. UnityEngine.GUI.color = statusColor;
  189. EditorGUILayout.LabelField(new GUIContent(status, change.Status.ToString()), GUILayout.Width(50));
  190. UnityEngine.GUI.color = originalColor;
  191. EditorGUILayout.LabelField(new GUIContent(filePathDisplay, filePathDisplay));
  192. EditorGUILayout.BeginHorizontal(GUILayout.Width(120));
  193. EditorGUI.BeginDisabledGroup(_controller.IsLoading);
  194. if (change.Status == LibGit2Sharp.ChangeKind.Conflicted)
  195. {
  196. if (GUILayout.Button("Resolve", GUILayout.Width(70)))
  197. {
  198. EditorApplication.delayCall += () => _controller.ResolveConflict(change);
  199. }
  200. }
  201. else
  202. {
  203. if (GUILayout.Button("Diff", GUILayout.Width(45)))
  204. {
  205. EditorApplication.delayCall += () => _controller.DiffFile(change);
  206. }
  207. }
  208. if (GUILayout.Button(new GUIContent("Reset", "Revert changes for this file"), GUILayout.Width(55)))
  209. {
  210. EditorApplication.delayCall += () => _controller.ResetFile(change);
  211. }
  212. EditorGUI.EndDisabledGroup();
  213. EditorGUILayout.EndHorizontal();
  214. EditorGUILayout.EndHorizontal();
  215. }
  216. EditorGUILayout.EndScrollView();
  217. }
  218. private void DrawHeaderButton(string text, SortColumn column, float width)
  219. {
  220. var label = text;
  221. if (_controller.CurrentSortColumn == column)
  222. {
  223. label = $"[*] {text}";
  224. }
  225. var buttonStyle = new GUIStyle(EditorStyles.label) { alignment = TextAnchor.MiddleLeft };
  226. if (width > 0)
  227. {
  228. if (GUILayout.Button(label, buttonStyle, GUILayout.Width(width))) { _controller.SetSortColumn(column); }
  229. }
  230. else
  231. {
  232. if (GUILayout.Button(label, buttonStyle)) { _controller.SetSortColumn(column); }
  233. }
  234. }
  235. private void DrawCommitSection()
  236. {
  237. EditorGUILayout.Space(10);
  238. if (_controller.Changes.Any(c => c.Status == LibGit2Sharp.ChangeKind.Conflicted))
  239. {
  240. EditorGUILayout.HelpBox("You must resolve all conflicts before you can commit.", MessageType.Warning);
  241. return;
  242. }
  243. EditorGUILayout.LabelField("Commit & Push", EditorStyles.boldLabel);
  244. _commitMessage = EditorGUILayout.TextArea(_commitMessage, GUILayout.Height(60), GUILayout.ExpandWidth(true));
  245. var isPushDisabled = string.IsNullOrWhiteSpace(_commitMessage) || !_controller.Changes.Any(c => c.IsSelectedForCommit);
  246. EditorGUI.BeginDisabledGroup(isPushDisabled);
  247. if (GUILayout.Button("Commit & Push Selected Files", GUILayout.Height(40)))
  248. {
  249. _controller.CommitAndPush(_commitMessage);
  250. _commitMessage = "";
  251. }
  252. EditorGUI.EndDisabledGroup();
  253. if (isPushDisabled)
  254. {
  255. EditorGUILayout.HelpBox("Please enter a commit message and select at least one file.", MessageType.Warning);
  256. }
  257. }
  258. private static UserAction PromptForUnsavedChanges()
  259. {
  260. var result = EditorUtility.DisplayDialogComplex(
  261. "Unsaved Scene Changes",
  262. "You have unsaved changes in the current scene. Would you like to save them before proceeding?",
  263. "Save and Continue",
  264. "Cancel",
  265. "Continue without Saving");
  266. return result switch
  267. {
  268. 0 => UserAction.SaveAndProceed,
  269. 1 => UserAction.Cancel,
  270. 2 => UserAction.Proceed,
  271. _ => UserAction.Cancel
  272. };
  273. }
  274. }
  275. }