chore(editor): Move stateful toggle to NestedArtboard (#12490) 9f0dc79e3f
Moving isStateful toggle to NestedArtboard (Component) from Artboard. This allows marking components as stateful on a per instance basis rather than on the Artboard itself.

Co-authored-by: Philip Chung <philterdesign@gmail.com>
diff --git a/.rive_head b/.rive_head
index baa9714..2387d84 100644
--- a/.rive_head
+++ b/.rive_head
@@ -1 +1 @@
-f1c2f2c7765ce4cb8a3901d2e7810aa6c69ee210
+9f0dc79e3f9018f28337c715d58232096ad0a317
diff --git a/dev/defs/artboard.json b/dev/defs/artboard.json
index faf4a22..b703708 100644
--- a/dev/defs/artboard.json
+++ b/dev/defs/artboard.json
@@ -134,15 +134,6 @@
       "runtime": false,
       "coop": false,
       "journal": false
-    },
-    "isStateful": {
-      "type": "bool",
-      "initialValue": "false",
-      "key": {
-        "int": 951,
-        "string": "isstateful"
-      },
-      "description": "If true, the Artboard will have a Viewmodel instance created at runtime seperate from the VM tree."
     }
   }
 }
\ No newline at end of file
diff --git a/dev/defs/nested_artboard.json b/dev/defs/nested_artboard.json
index 4910497..7e9a1c7 100644
--- a/dev/defs/nested_artboard.json
+++ b/dev/defs/nested_artboard.json
@@ -71,6 +71,15 @@
       },
       "description": "FPS at which the nested state machine will be quantized. -1 means no quantizaztion.",
       "bindable": true
+    },
+    "isStateful": {
+      "type": "bool",
+      "initialValue": "false",
+      "key": {
+        "int": 1014,
+        "string": "isstateful"
+      },
+      "description": "If true, the Component will have a Viewmodel instance created at runtime separate from the VM tree."
     }
   }
 }
\ No newline at end of file
diff --git a/include/rive/artboard_component_list.hpp b/include/rive/artboard_component_list.hpp
index e5eb424..bc90480 100644
--- a/include/rive/artboard_component_list.hpp
+++ b/include/rive/artboard_component_list.hpp
@@ -114,6 +114,8 @@
         invalidateOrderedListIndicesCache();
     }
     void shouldResetInstances(bool value) { m_shouldResetInstances = value; }
+    bool useStatefulInstances() const { return m_useStatefulInstances; }
+    void useStatefulInstances(bool value) { m_useStatefulInstances = value; }
     void setVirtualizablePosition(int index, Vec2D position) override;
     void createArtboardAt(int index, bool forceLayoutSync = true);
     void addArtboardAt(std::unique_ptr<ArtboardInstance> artboard,
@@ -210,6 +212,7 @@
                                 rcp<ViewModelInstanceListItem>);
     void clearArtboardOverride(ArtboardInstance*);
     bool m_shouldResetInstances = false;
+    bool m_useStatefulInstances = false;
     bool listsAreEqual(std::vector<rcp<ViewModelInstanceListItem>>* list,
                        std::vector<rcp<ViewModelInstanceListItem>>* compared);
 
diff --git a/include/rive/generated/artboard_base.hpp b/include/rive/generated/artboard_base.hpp
index 341853c..70cbaee 100644
--- a/include/rive/generated/artboard_base.hpp
+++ b/include/rive/generated/artboard_base.hpp
@@ -1,6 +1,5 @@
 #ifndef _RIVE_ARTBOARD_BASE_HPP_
 #define _RIVE_ARTBOARD_BASE_HPP_
-#include "rive/core/field_types/core_bool_type.hpp"
 #include "rive/core/field_types/core_double_type.hpp"
 #include "rive/core/field_types/core_uint_type.hpp"
 #include "rive/layout_component.hpp"
@@ -40,14 +39,12 @@
     static const uint16_t originYPropertyKey = 12;
     static const uint16_t defaultStateMachineIdPropertyKey = 236;
     static const uint16_t viewModelIdPropertyKey = 583;
-    static const uint16_t isStatefulPropertyKey = 951;
 
 protected:
     float m_OriginX = 0.0f;
     float m_OriginY = 0.0f;
     uint32_t m_DefaultStateMachineId = -1;
     uint32_t m_ViewModelId = -1;
-    bool m_IsStateful = false;
 
 public:
     inline float originX() const { return m_OriginX; }
@@ -97,17 +94,6 @@
         viewModelIdChanged();
     }
 
-    inline bool isStateful() const { return m_IsStateful; }
-    void isStateful(bool value)
-    {
-        if (m_IsStateful == value)
-        {
-            return;
-        }
-        m_IsStateful = value;
-        isStatefulChanged();
-    }
-
     Core* clone() const override;
     void copy(const ArtboardBase& object)
     {
@@ -115,7 +101,6 @@
         m_OriginY = object.m_OriginY;
         m_DefaultStateMachineId = object.m_DefaultStateMachineId;
         m_ViewModelId = object.m_ViewModelId;
-        m_IsStateful = object.m_IsStateful;
         LayoutComponent::copy(object);
     }
 
@@ -135,9 +120,6 @@
             case viewModelIdPropertyKey:
                 m_ViewModelId = CoreUintType::deserialize(reader);
                 return true;
-            case isStatefulPropertyKey:
-                m_IsStateful = CoreBoolType::deserialize(reader);
-                return true;
         }
         return LayoutComponent::deserialize(propertyKey, reader);
     }
@@ -147,7 +129,6 @@
     virtual void originYChanged() {}
     virtual void defaultStateMachineIdChanged() {}
     virtual void viewModelIdChanged() {}
-    virtual void isStatefulChanged() {}
 };
 } // namespace rive
 
diff --git a/include/rive/generated/core_registry.hpp b/include/rive/generated/core_registry.hpp
index a621672..c7bb3cb 100644
--- a/include/rive/generated/core_registry.hpp
+++ b/include/rive/generated/core_registry.hpp
@@ -384,18 +384,12 @@
                 return new DataEnumSystem();
             case ViewModelPropertyViewModelBase::typeKey:
                 return new ViewModelPropertyViewModel();
-            case DataEnumValueBase::typeKey:
-                return new DataEnumValue();
-            case ViewModelPropertyTriggerBase::typeKey:
-                return new ViewModelPropertyTrigger();
-            case ViewModelPropertyStringBase::typeKey:
-                return new ViewModelPropertyString();
-            case ViewModelPropertyColorBase::typeKey:
-                return new ViewModelPropertyColor();
-            case ViewModelPropertyBooleanBase::typeKey:
-                return new ViewModelPropertyBoolean();
             case ViewModelInstanceBase::typeKey:
                 return new ViewModelInstance();
+            case ViewModelPropertyBooleanBase::typeKey:
+                return new ViewModelPropertyBoolean();
+            case ViewModelPropertyColorBase::typeKey:
+                return new ViewModelPropertyColor();
             case ViewModelPropertyAssetImageBase::typeKey:
                 return new ViewModelPropertyAssetImage();
             case ViewModelInstanceBooleanBase::typeKey:
@@ -408,12 +402,18 @@
                 return new ViewModelInstanceTrigger();
             case ViewModelInstanceSymbolListIndexBase::typeKey:
                 return new ViewModelInstanceSymbolListIndex();
+            case ViewModelPropertyStringBase::typeKey:
+                return new ViewModelPropertyString();
             case ViewModelInstanceViewModelBase::typeKey:
                 return new ViewModelInstanceViewModel();
+            case ViewModelPropertyTriggerBase::typeKey:
+                return new ViewModelPropertyTrigger();
             case ViewModelInstanceAssetBase::typeKey:
                 return new ViewModelInstanceAsset();
             case ViewModelInstanceAssetImageBase::typeKey:
                 return new ViewModelInstanceAssetImage();
+            case DataEnumValueBase::typeKey:
+                return new DataEnumValue();
             case CustomPropertyTriggerBase::typeKey:
                 return new CustomPropertyTrigger();
             case ScriptInputTriggerBase::typeKey:
@@ -1839,6 +1839,9 @@
             case NestedArtboardBase::isPausedPropertyKey:
                 object->as<NestedArtboardBase>()->isPaused(value);
                 break;
+            case NestedArtboardBase::isStatefulPropertyKey:
+                object->as<NestedArtboardBase>()->isStateful(value);
+                break;
             case AxisBase::normalizedPropertyKey:
                 object->as<AxisBase>()->normalized(value);
                 break;
@@ -1916,9 +1919,6 @@
             case LayoutComponentBase::clipPropertyKey:
                 object->as<LayoutComponentBase>()->clip(value);
                 break;
-            case ArtboardBase::isStatefulPropertyKey:
-                object->as<ArtboardBase>()->isStateful(value);
-                break;
             case SemanticDataBase::isExpandablePropertyKey:
             {
                 auto* _o = object->as<SemanticDataBase>();
@@ -3678,6 +3678,8 @@
                 return object->as<ScrollBarConstraintBase>()->autoSize();
             case NestedArtboardBase::isPausedPropertyKey:
                 return object->as<NestedArtboardBase>()->isPaused();
+            case NestedArtboardBase::isStatefulPropertyKey:
+                return object->as<NestedArtboardBase>()->isStateful();
             case AxisBase::normalizedPropertyKey:
                 return object->as<AxisBase>()->normalized();
             case LayoutComponentStyleBase::intrinsicallySizedValuePropertyKey:
@@ -3731,8 +3733,6 @@
                 return object->as<CustomPropertyBooleanBase>()->propertyValue();
             case LayoutComponentBase::clipPropertyKey:
                 return object->as<LayoutComponentBase>()->clip();
-            case ArtboardBase::isStatefulPropertyKey:
-                return object->as<ArtboardBase>()->isStateful();
             case DataBindPathBase::isRelativePropertyKey:
                 return object->as<DataBindPathBase>()->isRelative();
             case BindablePropertyBooleanBase::propertyValuePropertyKey:
@@ -4515,6 +4515,7 @@
             case ScrollConstraintBase::interactivePropertyKey:
             case ScrollBarConstraintBase::autoSizePropertyKey:
             case NestedArtboardBase::isPausedPropertyKey:
+            case NestedArtboardBase::isStatefulPropertyKey:
             case AxisBase::normalizedPropertyKey:
             case LayoutComponentStyleBase::intrinsicallySizedValuePropertyKey:
             case LayoutComponentStyleBase::linkCornerRadiusPropertyKey:
@@ -4540,7 +4541,6 @@
             case FocusDataBase::canTraversePropertyKey:
             case CustomPropertyBooleanBase::propertyValuePropertyKey:
             case LayoutComponentBase::clipPropertyKey:
-            case ArtboardBase::isStatefulPropertyKey:
             case DataBindPathBase::isRelativePropertyKey:
             case BindablePropertyBooleanBase::propertyValuePropertyKey:
             case TextModifierRangeBase::clampPropertyKey:
@@ -5382,6 +5382,8 @@
                 return object->is<ScrollBarConstraintBase>();
             case NestedArtboardBase::isPausedPropertyKey:
                 return object->is<NestedArtboardBase>();
+            case NestedArtboardBase::isStatefulPropertyKey:
+                return object->is<NestedArtboardBase>();
             case AxisBase::normalizedPropertyKey:
                 return object->is<AxisBase>();
             case LayoutComponentStyleBase::intrinsicallySizedValuePropertyKey:
@@ -5432,8 +5434,6 @@
                 return object->is<CustomPropertyBooleanBase>();
             case LayoutComponentBase::clipPropertyKey:
                 return object->is<LayoutComponentBase>();
-            case ArtboardBase::isStatefulPropertyKey:
-                return object->is<ArtboardBase>();
             case SemanticDataBase::isExpandablePropertyKey:
                 return object->is<SemanticDataBase>();
             case SemanticDataBase::isSelectablePropertyKey:
diff --git a/include/rive/generated/nested_artboard_base.hpp b/include/rive/generated/nested_artboard_base.hpp
index eb9fb8c..26f0110 100644
--- a/include/rive/generated/nested_artboard_base.hpp
+++ b/include/rive/generated/nested_artboard_base.hpp
@@ -42,12 +42,14 @@
     static const uint16_t isPausedPropertyKey = 895;
     static const uint16_t speedPropertyKey = 907;
     static const uint16_t quantizePropertyKey = 908;
+    static const uint16_t isStatefulPropertyKey = 1014;
 
 protected:
     uint32_t m_ArtboardId = -1;
     bool m_IsPaused = false;
     float m_Speed = 1.0f;
     float m_Quantize = -1.0f;
+    bool m_IsStateful = false;
 
 public:
     inline uint32_t artboardId() const { return m_ArtboardId; }
@@ -97,6 +99,17 @@
         quantizeChanged();
     }
 
+    inline bool isStateful() const { return m_IsStateful; }
+    void isStateful(bool value)
+    {
+        if (m_IsStateful == value)
+        {
+            return;
+        }
+        m_IsStateful = value;
+        isStatefulChanged();
+    }
+
     Core* clone() const override;
     void copy(const NestedArtboardBase& object)
     {
@@ -105,6 +118,7 @@
         m_IsPaused = object.m_IsPaused;
         m_Speed = object.m_Speed;
         m_Quantize = object.m_Quantize;
+        m_IsStateful = object.m_IsStateful;
         Drawable::copy(object);
     }
 
@@ -127,6 +141,9 @@
             case quantizePropertyKey:
                 m_Quantize = CoreDoubleType::deserialize(reader);
                 return true;
+            case isStatefulPropertyKey:
+                m_IsStateful = CoreBoolType::deserialize(reader);
+                return true;
         }
         return Drawable::deserialize(propertyKey, reader);
     }
@@ -137,6 +154,7 @@
     virtual void isPausedChanged() {}
     virtual void speedChanged() {}
     virtual void quantizeChanged() {}
+    virtual void isStatefulChanged() {}
 };
 } // namespace rive
 
diff --git a/src/artboard_component_list.cpp b/src/artboard_component_list.cpp
index f225817..4a7bc0e 100644
--- a/src/artboard_component_list.cpp
+++ b/src/artboard_component_list.cpp
@@ -1265,14 +1265,10 @@
         auto dataContext = mainArtboard->dataContext();
         rcp<ViewModelInstance> viewModelInstance = nullptr;
 
-        // Check if the source artboard is stateful - if so, create a new
-        // instance for it (takes priority over any existing list item
-        // instance). Clone the list item's instance when available so we
-        // pick up its property values; otherwise fall back to the default.
         if (m_file != nullptr)
         {
             auto source = artboardInstance->artboardSource();
-            if (source != nullptr && source->isStateful())
+            if (m_useStatefulInstances && source != nullptr)
             {
                 auto listItemInstance = listItem->viewModelInstance();
                 if (listItemInstance != nullptr)
diff --git a/src/nested_artboard.cpp b/src/nested_artboard.cpp
index 42a0f94..0073e3f 100644
--- a/src/nested_artboard.cpp
+++ b/src/nested_artboard.cpp
@@ -161,7 +161,7 @@
         }
         referencedArtboard(artboardInstance.release());
 
-        if (artboard->isStateful())
+        if (isStateful())
         {
             const bool cacheStale =
                 m_statefulViewModelInstance == nullptr ||
@@ -530,7 +530,7 @@
 {
     m_viewModelInstance = viewModelInstance;
     auto instance = artboardInstance(0);
-    if (instance && !instance->isStateful())
+    if (instance && !isStateful())
     {
         auto dataContext = instance->dataContext();
         if (dataContext != nullptr)
diff --git a/tests/unit_tests/assets/databind_viewmodel.riv b/tests/unit_tests/assets/databind_viewmodel.riv
index 7799faf..c91e4ee 100644
--- a/tests/unit_tests/assets/databind_viewmodel.riv
+++ b/tests/unit_tests/assets/databind_viewmodel.riv
Binary files differ
diff --git a/tests/unit_tests/assets/stateful_artboard_swap.riv b/tests/unit_tests/assets/stateful_artboard_swap.riv
index 124633e..aea16ca 100644
--- a/tests/unit_tests/assets/stateful_artboard_swap.riv
+++ b/tests/unit_tests/assets/stateful_artboard_swap.riv
Binary files differ
diff --git a/tests/unit_tests/assets/stateful_list_props.riv b/tests/unit_tests/assets/stateful_list_props.riv
index 4a2fc29..291fb45 100644
--- a/tests/unit_tests/assets/stateful_list_props.riv
+++ b/tests/unit_tests/assets/stateful_list_props.riv
Binary files differ
diff --git a/tests/unit_tests/assets/stateful_multi_property.riv b/tests/unit_tests/assets/stateful_multi_property.riv
index 5e4e74b..0ba5979 100644
--- a/tests/unit_tests/assets/stateful_multi_property.riv
+++ b/tests/unit_tests/assets/stateful_multi_property.riv
Binary files differ
diff --git a/tests/unit_tests/assets/stateful_nested.riv b/tests/unit_tests/assets/stateful_nested.riv
index 5ccd4b2..b110607 100644
--- a/tests/unit_tests/assets/stateful_nested.riv
+++ b/tests/unit_tests/assets/stateful_nested.riv
Binary files differ
diff --git a/tests/unit_tests/assets/unbound_stateful_component.riv b/tests/unit_tests/assets/unbound_stateful_component.riv
index b211e93..9ad1d8e 100644
--- a/tests/unit_tests/assets/unbound_stateful_component.riv
+++ b/tests/unit_tests/assets/unbound_stateful_component.riv
Binary files differ
diff --git a/tests/unit_tests/runtime/component_test.cpp b/tests/unit_tests/runtime/component_test.cpp
index 2e0b440..108055f 100644
--- a/tests/unit_tests/runtime/component_test.cpp
+++ b/tests/unit_tests/runtime/component_test.cpp
@@ -280,7 +280,10 @@
 
     CHECK(silver.matches("stateful_nested"));
 }
-
+/*
+// Disable this test for now. Initially we decided not to create stateful
+// VM instances for each list item to reduce overhead. This may change in
+// the future and we can revisit this test at that point.
 TEST_CASE("Stateful Component list with input/output bridge binds", "[silver]")
 {
     rive::SerializingFactory silver;
@@ -474,7 +477,7 @@
 
     CHECK(silver.matches("stateful_list_props"));
 }
-
+*/
 TEST_CASE("Stateful Component dynamic artboard swap via VMI artboard "
           "property",
           "[silver]")