using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using MCPForUnity.Editor.Constants; using MCPForUnity.Editor.Helpers; using MCPForUnity.Editor.Services; using MCPForUnity.Editor.Windows.Components.Advanced; using MCPForUnity.Editor.Windows.Components.ClientConfig; using MCPForUnity.Editor.Windows.Components.Connection; using MCPForUnity.Editor.Windows.Components.Resources; using MCPForUnity.Editor.Windows.Components.Tools; using MCPForUnity.Editor.Windows.Components.Validation; using UnityEditor; using UnityEditor.UIElements; using UnityEngine; using UnityEngine.UIElements; namespace MCPForUnity.Editor.Windows { public class MCPForUnityEditorWindow : EditorWindow { // Section controllers private McpConnectionSection connectionSection; private McpClientConfigSection clientConfigSection; private McpValidationSection validationSection; private McpAdvancedSection advancedSection; private McpToolsSection toolsSection; private McpResourcesSection resourcesSection; // UI Elements private Label versionLabel; private VisualElement updateNotification; private Label updateNotificationText; private ToolbarToggle clientsTabToggle; private ToolbarToggle validationTabToggle; private ToolbarToggle advancedTabToggle; private ToolbarToggle toolsTabToggle; private ToolbarToggle resourcesTabToggle; private VisualElement clientsPanel; private VisualElement validationPanel; private VisualElement advancedPanel; private VisualElement toolsPanel; private VisualElement resourcesPanel; private static readonly HashSet OpenWindows = new(); private bool guiCreated = false; private bool toolsLoaded = false; private bool resourcesLoaded = false; private double lastRefreshTime = 0; private const double RefreshDebounceSeconds = 0.5; private enum ActivePanel { Clients, Validation, Advanced, Tools, Resources } internal static void CloseAllWindows() { var windows = OpenWindows.Where(window => window != null).ToArray(); foreach (var window in windows) { window.Close(); } } public static void ShowWindow() { var window = GetWindow("MCP For Unity"); window.minSize = new Vector2(500, 340); } // Helper to check and manage open windows from other classes public static bool HasAnyOpenWindow() { return OpenWindows.Count > 0; } public static void CloseAllOpenWindows() { if (OpenWindows.Count == 0) return; // Copy to array to avoid modifying the collection while iterating var arr = new MCPForUnityEditorWindow[OpenWindows.Count]; OpenWindows.CopyTo(arr); foreach (var window in arr) { try { window?.Close(); } catch (Exception ex) { McpLog.Warn($"Error closing MCP window: {ex.Message}"); } } } public void CreateGUI() { // Guard against repeated CreateGUI calls (e.g., domain reloads) if (guiCreated) return; string basePath = AssetPathUtility.GetMcpPackageRootPath(); // Load main window UXML var visualTree = AssetDatabase.LoadAssetAtPath( $"{basePath}/Editor/Windows/MCPForUnityEditorWindow.uxml" ); if (visualTree == null) { McpLog.Error( $"Failed to load UXML at: {basePath}/Editor/Windows/MCPForUnityEditorWindow.uxml" ); return; } visualTree.CloneTree(rootVisualElement); // Load main window USS var mainStyleSheet = AssetDatabase.LoadAssetAtPath( $"{basePath}/Editor/Windows/MCPForUnityEditorWindow.uss" ); if (mainStyleSheet != null) { rootVisualElement.styleSheets.Add(mainStyleSheet); } // Load common USS var commonStyleSheet = AssetDatabase.LoadAssetAtPath( $"{basePath}/Editor/Windows/Components/Common.uss" ); if (commonStyleSheet != null) { rootVisualElement.styleSheets.Add(commonStyleSheet); } // Cache UI elements versionLabel = rootVisualElement.Q