123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- // Copyright (c) 2025 TerraByte Inc.
- //
- // A new, non-dockable editor window for viewing and managing changes
- // stored in the 'Better Git Stash'.
- using System;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.Scripting;
- using Terra.Arbitrator.Services;
- using System.Collections.Generic;
- namespace Terra.Arbitrator.GUI
- {
- [Preserve]
- public class StashedChangesWindow : EditorWindow
- {
- private ArbitratorController _controller;
- private List<GitChange> _stashedFiles;
- private Vector2 _scrollPosition;
- private Action _onClose;
- public static void ShowWindow(ArbitratorController controller, List<GitChange> stashedFiles, Action onClose = null)
- {
- var window = GetWindow<StashedChangesWindow>(true, "Stashed Changes", true);
- window.minSize = new Vector2(500, 350);
- window._controller = controller;
- window._stashedFiles = stashedFiles;
- window._onClose = onClose;
- window.ShowUtility();
- }
- private void OnGUI()
- {
- if (_controller == null || _stashedFiles == null)
- {
- EditorGUILayout.LabelField("No data available.");
- return;
- }
-
- DrawHeader();
- DrawFileList();
- DrawFooter();
- }
- private void DrawHeader()
- {
- EditorGUILayout.LabelField("Files in 'Better Git Stash'", EditorStyles.boldLabel);
- EditorGUILayout.BeginHorizontal();
- if (GUILayout.Button("Apply Stash", GUILayout.Height(30)))
- {
- _controller.ApplyStash();
- Close();
- }
- if (GUILayout.Button("Discard Stash", GUILayout.Height(30)))
- {
- if (EditorUtility.DisplayDialog("Discard Stash?", "Are you sure you want to discard the 'Better Git Stash'? This action cannot be undone.", "Yes, Discard", "Cancel"))
- {
- _controller.DiscardStash();
- Close();
- }
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.Space();
- }
- private void DrawFileList()
- {
- _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, EditorStyles.helpBox);
- foreach (var change in _stashedFiles)
- {
- EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
-
- string status;
- Color statusColor;
- switch (change.Status)
- {
- case LibGit2Sharp.ChangeKind.Added:
- status = "[+]";
- statusColor = Color.green;
- break;
- case LibGit2Sharp.ChangeKind.Deleted:
- status = "[-]";
- statusColor = Color.red;
- break;
- case LibGit2Sharp.ChangeKind.Modified:
- status = "[M]";
- statusColor = new Color(1.0f, 0.6f, 0.0f);
- break;
- case LibGit2Sharp.ChangeKind.Renamed:
- status = "[R]";
- statusColor = new Color(0.6f, 0.6f, 1.0f);
- break;
- default:
- status = "[?]";
- statusColor = Color.white;
- break;
- }
- var originalColor = UnityEngine.GUI.color;
- UnityEngine.GUI.color = statusColor;
- EditorGUILayout.LabelField(new GUIContent(status, change.Status.ToString()), GUILayout.Width(25));
- UnityEngine.GUI.color = originalColor;
-
- var filePathDisplay = change.Status == LibGit2Sharp.ChangeKind.Renamed
- ? $"{change.OldFilePath} -> {change.FilePath}"
- : change.FilePath;
- EditorGUILayout.LabelField(new GUIContent(filePathDisplay, filePathDisplay));
- if (GUILayout.Button("Diff", GUILayout.Width(60)))
- {
- _controller.DiffStashedFile(change);
- }
-
- EditorGUILayout.EndHorizontal();
- }
- EditorGUILayout.EndScrollView();
- }
- private void DrawFooter()
- {
- GUILayout.FlexibleSpace();
- EditorGUILayout.BeginHorizontal();
- GUILayout.FlexibleSpace();
- if (GUILayout.Button("Close", GUILayout.Width(100)))
- {
- Close();
- }
- EditorGUILayout.EndHorizontal();
- EditorGUILayout.Space();
- }
-
- private void OnDestroy()
- {
- _onClose?.Invoke();
- }
- }
- }
|