ArbitratorWindow.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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: (heading, message) => EditorUtility.DisplayDialog(heading, message, "Yes", "No")
  29. );
  30. _controller.OnEnable();
  31. }
  32. private void InitializeStyles()
  33. {
  34. if (_stylesInitialized) return;
  35. _evenRowStyle = new GUIStyle();
  36. var texture = new Texture2D(1, 1);
  37. var color = EditorGUIUtility.isProSkin
  38. ? new Color(0.3f, 0.3f, 0.3f, 0.3f)
  39. : new Color(0.8f, 0.8f, 0.8f, 0.5f);
  40. texture.SetPixel(0, 0, color);
  41. texture.Apply();
  42. _evenRowStyle.normal.background = texture;
  43. _stylesInitialized = true;
  44. }
  45. private void OnGUI()
  46. {
  47. InitializeStyles();
  48. DrawToolbar();
  49. EditorGUILayout.Space();
  50. DrawMessageArea();
  51. DrawMainContent();
  52. }
  53. private void DrawToolbar()
  54. {
  55. EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
  56. EditorGUI.BeginDisabledGroup(_controller.IsLoading);
  57. if (GUILayout.Button(new GUIContent("Refresh", EditorGUIUtility.IconContent("Refresh").image), EditorStyles.toolbarButton, GUILayout.Width(80)))
  58. {
  59. _controller.Refresh();
  60. }
  61. if (GUILayout.Button(new GUIContent("Pull", EditorGUIUtility.IconContent("CollabPull").image), EditorStyles.toolbarButton, GUILayout.Width(80)))
  62. {
  63. _controller.Pull();
  64. }
  65. if (_controller.Changes is { Count: > 0 })
  66. {
  67. if (GUILayout.Button("Select All", EditorStyles.toolbarButton, GUILayout.Width(80)))
  68. {
  69. _controller.SetAllSelection(true);
  70. }
  71. if (GUILayout.Button("Deselect All", EditorStyles.toolbarButton, GUILayout.Width(80)))
  72. {
  73. _controller.SetAllSelection(false);
  74. }
  75. GUILayout.FlexibleSpace();
  76. var noChanges = !_controller.Changes.Any();
  77. EditorGUI.BeginDisabledGroup(noChanges);
  78. var originalColor = UnityEngine.GUI.backgroundColor;
  79. UnityEngine.GUI.backgroundColor = new Color(1f, 0.5f, 0.5f, 0.8f);
  80. if (GUILayout.Button(new GUIContent("Reset All", EditorGUIUtility.IconContent("d_TreeEditor.Trash").image), EditorStyles.toolbarButton, GUILayout.Width(100)))
  81. {
  82. EditorApplication.delayCall += _controller.ResetAll;
  83. }
  84. UnityEngine.GUI.backgroundColor = originalColor;
  85. EditorGUI.EndDisabledGroup();
  86. }
  87. EditorGUI.EndDisabledGroup();
  88. if (_controller.IsLoading)
  89. {
  90. GUILayout.FlexibleSpace();
  91. GUILayout.Label(_controller.LoadingMessage);
  92. }
  93. EditorGUILayout.EndHorizontal();
  94. }
  95. private void DrawMessageArea()
  96. {
  97. if (!string.IsNullOrEmpty(_controller.ErrorMessage))
  98. {
  99. EditorGUILayout.HelpBox(_controller.ErrorMessage, MessageType.Error);
  100. }
  101. else if (!string.IsNullOrEmpty(_controller.InfoMessage) && !_controller.IsLoading)
  102. {
  103. EditorGUILayout.HelpBox(_controller.InfoMessage, MessageType.Info);
  104. }
  105. }
  106. private void DrawMainContent()
  107. {
  108. if (_controller.IsLoading)
  109. {
  110. GUILayout.FlexibleSpace();
  111. EditorGUILayout.BeginHorizontal();
  112. GUILayout.FlexibleSpace();
  113. GUILayout.Label("Loading...", EditorStyles.largeLabel);
  114. GUILayout.FlexibleSpace();
  115. EditorGUILayout.EndHorizontal();
  116. GUILayout.FlexibleSpace();
  117. }
  118. else if (_controller.Changes != null && _controller.Changes.Any())
  119. {
  120. DrawChangesList();
  121. DrawCommitSection();
  122. }
  123. else
  124. {
  125. EditorGUILayout.HelpBox("You are up-to-date! No local changes detected.", MessageType.Info);
  126. }
  127. }
  128. private void DrawChangesList()
  129. {
  130. EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
  131. EditorGUILayout.LabelField("Commit", GUILayout.Width(45));
  132. EditorGUILayout.LabelField("Status", GUILayout.Width(50));
  133. EditorGUILayout.LabelField("File Path");
  134. EditorGUILayout.LabelField("Actions", GUILayout.Width(130));
  135. EditorGUILayout.EndHorizontal();
  136. _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, GUILayout.ExpandHeight(true));
  137. for (var i = 0; i < _controller.Changes.Count; i++)
  138. {
  139. var change = _controller.Changes[i];
  140. var rowStyle = i % 2 == 0 ? _evenRowStyle : GUIStyle.none;
  141. EditorGUILayout.BeginHorizontal(rowStyle);
  142. if (change.Status == LibGit2Sharp.ChangeKind.Conflicted)
  143. {
  144. EditorGUI.BeginDisabledGroup(true);
  145. EditorGUILayout.Toggle(false, GUILayout.Width(45));
  146. EditorGUI.EndDisabledGroup();
  147. }
  148. else
  149. {
  150. change.IsSelectedForCommit = EditorGUILayout.Toggle(change.IsSelectedForCommit, GUILayout.Width(45));
  151. }
  152. string status;
  153. Color statusColor;
  154. switch (change.Status)
  155. {
  156. case LibGit2Sharp.ChangeKind.Added: status = "[+]"; statusColor = Color.green; break;
  157. case LibGit2Sharp.ChangeKind.Deleted: status = "[-]"; statusColor = Color.red; break;
  158. case LibGit2Sharp.ChangeKind.Modified: status = "[M]"; statusColor = new Color(1.0f, 0.6f, 0.0f); break;
  159. case LibGit2Sharp.ChangeKind.Renamed: status = "[R]"; statusColor = new Color(0.6f, 0.6f, 1.0f); break;
  160. case LibGit2Sharp.ChangeKind.Conflicted: status = "[C]"; statusColor = Color.magenta; break;
  161. default: status = "[?]"; statusColor = Color.white; break;
  162. }
  163. var filePathDisplay = change.Status == LibGit2Sharp.ChangeKind.Renamed
  164. ? $"{change.OldFilePath} -> {change.FilePath}"
  165. : change.FilePath;
  166. var originalColor = UnityEngine.GUI.color;
  167. UnityEngine.GUI.color = statusColor;
  168. EditorGUILayout.LabelField(new GUIContent(status, change.Status.ToString()), GUILayout.Width(50));
  169. UnityEngine.GUI.color = originalColor;
  170. EditorGUILayout.LabelField(new GUIContent(filePathDisplay, filePathDisplay));
  171. EditorGUILayout.BeginHorizontal(GUILayout.Width(120));
  172. EditorGUI.BeginDisabledGroup(_controller.IsLoading);
  173. if (change.Status == LibGit2Sharp.ChangeKind.Conflicted)
  174. {
  175. if (GUILayout.Button("Resolve", GUILayout.Width(70)))
  176. {
  177. EditorApplication.delayCall += () => _controller.ResolveConflict(change);
  178. }
  179. }
  180. else
  181. {
  182. if (GUILayout.Button("Diff", GUILayout.Width(45)))
  183. {
  184. EditorApplication.delayCall += () => _controller.DiffFile(change);
  185. }
  186. }
  187. if (GUILayout.Button(new GUIContent("Reset", "Revert changes for this file"), GUILayout.Width(55)))
  188. {
  189. EditorApplication.delayCall += () => _controller.ResetFile(change);
  190. }
  191. EditorGUI.EndDisabledGroup();
  192. EditorGUILayout.EndHorizontal();
  193. EditorGUILayout.EndHorizontal();
  194. }
  195. EditorGUILayout.EndScrollView();
  196. }
  197. private void DrawCommitSection()
  198. {
  199. EditorGUILayout.Space(10);
  200. if (_controller.Changes.Any(c => c.Status == LibGit2Sharp.ChangeKind.Conflicted))
  201. {
  202. EditorGUILayout.HelpBox("You must resolve all conflicts before you can commit.", MessageType.Warning);
  203. return;
  204. }
  205. EditorGUILayout.LabelField("Commit & Push", EditorStyles.boldLabel);
  206. _commitMessage = EditorGUILayout.TextArea(_commitMessage, GUILayout.Height(60), GUILayout.ExpandWidth(true));
  207. var isPushDisabled = string.IsNullOrWhiteSpace(_commitMessage) || !_controller.Changes.Any(c => c.IsSelectedForCommit);
  208. EditorGUI.BeginDisabledGroup(isPushDisabled);
  209. if (GUILayout.Button("Commit & Push Selected Files", GUILayout.Height(40)))
  210. {
  211. _controller.CommitAndPush(_commitMessage);
  212. _commitMessage = "";
  213. }
  214. EditorGUI.EndDisabledGroup();
  215. if (isPushDisabled)
  216. {
  217. EditorGUILayout.HelpBox("Please enter a commit message and select at least one file.", MessageType.Warning);
  218. }
  219. }
  220. }
  221. }