From e541b3f672669bfed540a5656bb151856e127513 Mon Sep 17 00:00:00 2001
From: Victor <VicChelaru@gmail.com>
Date: Fri, 20 Dec 2024 14:49:37 -0700
Subject: [PATCH] Fixed another case of elements not being selected properly.

---
 .../VariableGrid/MainVariableGridPlugin.cs    |  6 +++
 Gum/ToolStates/SelectedState.cs               | 42 +++++++++++--------
 2 files changed, 30 insertions(+), 18 deletions(-)

diff --git a/Gum/Plugins/InternalPlugins/VariableGrid/MainVariableGridPlugin.cs b/Gum/Plugins/InternalPlugins/VariableGrid/MainVariableGridPlugin.cs
index 5e1ace09..1de3f68b 100644
--- a/Gum/Plugins/InternalPlugins/VariableGrid/MainVariableGridPlugin.cs
+++ b/Gum/Plugins/InternalPlugins/VariableGrid/MainVariableGridPlugin.cs
@@ -21,9 +21,15 @@ public override void StartUp()
             this.ReactToStateSaveCategorySelected += MainVariableGridPlugin_ReactToStateSaveCategorySelected;
             this.StateMovedToCategory += HandleStateMovedToCategory;
             this.InstanceSelected += HandleInstanceSelected;
+            this.ElementSelected += HandleElementSelected;
             this.BehaviorSelected += HandleBehaviorSelected;
         }
 
+        private void HandleElementSelected(ElementSave save)
+        {
+            PropertyGridManager.Self.RefreshUI(force: true);
+        }
+
         private void HandleBehaviorSelected(BehaviorSave save)
         {
             PropertyGridManager.Self.RefreshUI(force: true);
diff --git a/Gum/ToolStates/SelectedState.cs b/Gum/ToolStates/SelectedState.cs
index 47534e78..508a4082 100644
--- a/Gum/ToolStates/SelectedState.cs
+++ b/Gum/ToolStates/SelectedState.cs
@@ -95,8 +95,12 @@ private void HandleElementSelected(ElementSave value)
             snapshot.SelectedBehavior = null;
         }
         UpdateToSelectedElement(value);
-    }
 
+        if(value != null)
+        {
+            PluginManager.Self.ElementSelected(SelectedElement);
+        }
+    }
 
     private void UpdateToSelectedElement(ElementSave element)
     {
@@ -130,10 +134,7 @@ private void UpdateToSelectedElement(ElementSave element)
             SelectionManager.Self.Refresh();
 
             _menuStripManager.RefreshUI();
-
-            PluginManager.Self.ElementSelected(SelectedElement);
         }
-
     }
 
     #endregion
@@ -409,6 +410,25 @@ public VariableSave SelectedBehaviorVariable
 
     #endregion
 
+    #region Instance
+
+    private void HandleSelectedInstances(List<InstanceSave> value)
+    {
+        var instance = value?.FirstOrDefault();
+        if(instance != null)
+        {
+            var elementAfter = ObjectFinder.Self.GetElementContainerOf(instance);
+            var behaviorAfter = ObjectFinder.Self.GetBehaviorContainerOf(instance);
+
+            snapshot.SelectedElement = elementAfter;
+            snapshot.SelectedBehavior = behaviorAfter;
+        }
+
+        UpdateToSelectedInstances(value);
+    }
+
+    #endregion
+
     private void UpdateToSetSelectedStackingMode(StateStackingMode value)
     {
         var isSame = snapshot.StateStackingMode == value;
@@ -429,20 +449,6 @@ private void UpdateToSetSelectedStateSaveCategory(StateSaveCategory selectedStat
         }
     }
 
-    private void HandleSelectedInstances(List<InstanceSave> value)
-    {
-        var instance = value?.FirstOrDefault();
-        if(instance != null)
-        {
-            var elementAfter = ObjectFinder.Self.GetElementContainerOf(instance);
-            var behaviorAfter = ObjectFinder.Self.GetBehaviorContainerOf(instance);
-
-            snapshot.SelectedElement = elementAfter;
-            snapshot.SelectedBehavior = behaviorAfter;
-        }
-
-        UpdateToSelectedInstances(value);
-    }
 
     private void HandleStateSaveSelected(StateSave stateSave)
     {