DiffWindow.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) 2025 TerraByte Inc.
  2. //
  3. // A modal editor window for displaying text differences (diffs) and
  4. // confirming an action with the user.
  5. using System;
  6. using System.IO;
  7. using UnityEditor;
  8. using UnityEngine;
  9. namespace Terra.Arbitrator.GUI
  10. {
  11. public class DiffWindow : EditorWindow
  12. {
  13. private string _filePath;
  14. private string _diffContent;
  15. private Action<bool> _onCloseCallback;
  16. private Vector2 _scrollPosition;
  17. private bool _callbackInvoked;
  18. private GUIStyle _diffStyle;
  19. /// <summary>
  20. /// Shows a modal window to display the diff and get user confirmation.
  21. /// </summary>
  22. /// <param name="filePath">The path of the file being diffed.</param>
  23. /// <param name="diffContent">The diff text to display.</param>
  24. /// <param name="onCloseCallback">A callback that returns true if the user confirmed, otherwise false.</param>
  25. public static void ShowWindow(string filePath, string diffContent, Action<bool> onCloseCallback)
  26. {
  27. var window = GetWindow<DiffWindow>(true, "Diff Viewer", true);
  28. window.titleContent = new GUIContent($"Diff: {Path.GetFileName(filePath)}");
  29. window.minSize = new Vector2(700, 500);
  30. window._filePath = filePath;
  31. window._diffContent = diffContent;
  32. window._onCloseCallback = onCloseCallback;
  33. window._callbackInvoked = false;
  34. window.ShowModalUtility(); // Show as a blocking modal window
  35. }
  36. private void OnEnable()
  37. {
  38. // Initialize the style here to avoid doing it every OnGUI call.
  39. _diffStyle = new GUIStyle(EditorStyles.label)
  40. {
  41. richText = true,
  42. wordWrap = false,
  43. alignment = TextAnchor.UpperLeft
  44. };
  45. }
  46. private void OnGUI()
  47. {
  48. EditorGUILayout.LabelField(_filePath, EditorStyles.boldLabel);
  49. _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, EditorStyles.helpBox);
  50. var coloredText = GetColoredDiff(_diffContent);
  51. var content = new GUIContent(coloredText);
  52. // Calculate the required height based on the window's width, accounting for scrollbar and padding.
  53. var viewWidth = EditorGUIUtility.currentViewWidth - 30; // Approximate width inside scroll view
  54. var requiredHeight = _diffStyle.CalcHeight(content, viewWidth);
  55. // Use GUILayout.Label with an explicit height to ensure the ScrollView's content area is sized correctly.
  56. EditorGUILayout.SelectableLabel(coloredText, _diffStyle, GUILayout.MinHeight(requiredHeight));
  57. EditorGUILayout.EndScrollView();
  58. EditorGUILayout.Space();
  59. EditorGUILayout.HelpBox("Review the changes above. Green lines are additions, red lines are deletions. Reverting will permanently discard these changes.", MessageType.Warning);
  60. EditorGUILayout.BeginHorizontal();
  61. GUILayout.FlexibleSpace();
  62. if (GUILayout.Button("Revert Changes", GUILayout.Height(30), GUILayout.Width(150)))
  63. {
  64. _callbackInvoked = true;
  65. _onCloseCallback?.Invoke(true);
  66. Close();
  67. }
  68. if (GUILayout.Button("Cancel", GUILayout.Height(30), GUILayout.Width(120)))
  69. {
  70. Close();
  71. }
  72. EditorGUILayout.EndHorizontal();
  73. }
  74. private static string GetColoredDiff(string rawDiff)
  75. {
  76. var coloredDiff = "";
  77. var lines = rawDiff.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  78. foreach (var line in lines)
  79. {
  80. if (line.StartsWith("+"))
  81. {
  82. coloredDiff += $"<color=green>{line}</color>\n";
  83. }
  84. else if (line.StartsWith("-"))
  85. {
  86. coloredDiff += $"<color=red>{line}</color>\n";
  87. }
  88. else if (line.StartsWith("@@"))
  89. {
  90. coloredDiff += $"<color=cyan>{line}</color>\n";
  91. }
  92. else
  93. {
  94. coloredDiff += $"{line}\n";
  95. }
  96. }
  97. return coloredDiff + "\n";
  98. }
  99. private void OnDestroy()
  100. {
  101. // This is called when the window is closed by any means.
  102. // If the callback hasn't been invoked yet (i.e., user clicked 'X' or 'Cancel'),
  103. // we invoke it with 'false' to signify cancellation.
  104. if (!_callbackInvoked)
  105. {
  106. _onCloseCallback?.Invoke(false);
  107. }
  108. }
  109. }
  110. }