StashedChangesWindow.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (c) 2025 TerraByte Inc.
  2. //
  3. // A new, non-dockable editor window for viewing and managing changes
  4. // stored in the 'Better Git Stash'.
  5. using System;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using UnityEngine.Scripting;
  9. using Terra.Arbitrator.Services;
  10. using System.Collections.Generic;
  11. namespace Terra.Arbitrator.GUI
  12. {
  13. [Preserve]
  14. public class StashedChangesWindow : EditorWindow
  15. {
  16. private ArbitratorController _controller;
  17. private List<GitChange> _stashedFiles;
  18. private Vector2 _scrollPosition;
  19. private Action _onClose;
  20. public static void ShowWindow(ArbitratorController controller, List<GitChange> stashedFiles, Action onClose = null)
  21. {
  22. var window = GetWindow<StashedChangesWindow>(true, "Stashed Changes", true);
  23. window.minSize = new Vector2(500, 350);
  24. window._controller = controller;
  25. window._stashedFiles = stashedFiles;
  26. window._onClose = onClose;
  27. window.ShowUtility();
  28. }
  29. private void OnGUI()
  30. {
  31. if (_controller == null || _stashedFiles == null)
  32. {
  33. EditorGUILayout.LabelField("No data available.");
  34. return;
  35. }
  36. DrawHeader();
  37. DrawFileList();
  38. DrawFooter();
  39. }
  40. private void DrawHeader()
  41. {
  42. EditorGUILayout.LabelField("Files in 'Better Git Stash'", EditorStyles.boldLabel);
  43. EditorGUILayout.BeginHorizontal();
  44. if (GUILayout.Button("Apply Stash", GUILayout.Height(30)))
  45. {
  46. _controller.ApplyStash();
  47. Close();
  48. }
  49. if (GUILayout.Button("Discard Stash", GUILayout.Height(30)))
  50. {
  51. if (EditorUtility.DisplayDialog("Discard Stash?", "Are you sure you want to discard the 'Better Git Stash'? This action cannot be undone.", "Yes, Discard", "Cancel"))
  52. {
  53. _controller.DiscardStash();
  54. Close();
  55. }
  56. }
  57. EditorGUILayout.EndHorizontal();
  58. EditorGUILayout.Space();
  59. }
  60. private void DrawFileList()
  61. {
  62. _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, EditorStyles.helpBox);
  63. foreach (var change in _stashedFiles)
  64. {
  65. EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
  66. string status;
  67. Color statusColor;
  68. switch (change.Status)
  69. {
  70. case LibGit2Sharp.ChangeKind.Added:
  71. status = "[+]";
  72. statusColor = Color.green;
  73. break;
  74. case LibGit2Sharp.ChangeKind.Deleted:
  75. status = "[-]";
  76. statusColor = Color.red;
  77. break;
  78. case LibGit2Sharp.ChangeKind.Modified:
  79. status = "[M]";
  80. statusColor = new Color(1.0f, 0.6f, 0.0f);
  81. break;
  82. case LibGit2Sharp.ChangeKind.Renamed:
  83. status = "[R]";
  84. statusColor = new Color(0.6f, 0.6f, 1.0f);
  85. break;
  86. default:
  87. status = "[?]";
  88. statusColor = Color.white;
  89. break;
  90. }
  91. var originalColor = UnityEngine.GUI.color;
  92. UnityEngine.GUI.color = statusColor;
  93. EditorGUILayout.LabelField(new GUIContent(status, change.Status.ToString()), GUILayout.Width(25));
  94. UnityEngine.GUI.color = originalColor;
  95. var filePathDisplay = change.Status == LibGit2Sharp.ChangeKind.Renamed
  96. ? $"{change.OldFilePath} -> {change.FilePath}"
  97. : change.FilePath;
  98. EditorGUILayout.LabelField(new GUIContent(filePathDisplay, filePathDisplay));
  99. if (GUILayout.Button("Diff", GUILayout.Width(60)))
  100. {
  101. _controller.DiffStashedFile(change);
  102. }
  103. EditorGUILayout.EndHorizontal();
  104. }
  105. EditorGUILayout.EndScrollView();
  106. }
  107. private void DrawFooter()
  108. {
  109. GUILayout.FlexibleSpace();
  110. EditorGUILayout.BeginHorizontal();
  111. GUILayout.FlexibleSpace();
  112. if (GUILayout.Button("Close", GUILayout.Width(100)))
  113. {
  114. Close();
  115. }
  116. EditorGUILayout.EndHorizontal();
  117. EditorGUILayout.Space();
  118. }
  119. private void OnDestroy()
  120. {
  121. _onClose?.Invoke();
  122. }
  123. }
  124. }