Visual replacements logic updated
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / control / control-data-impl.cpp
index 38c9a57..2469beb 100644 (file)
 // EXTERNAL INCLUDES
 #include <dali/public-api/common/dali-common.h>
 #include <dali/integration-api/debug.h>
+#include <dali/devel-api/object/handle-devel.h>
 #include <dali/devel-api/scripting/enum-helper.h>
 #include <dali/devel-api/scripting/scripting.h>
 #include <dali/integration-api/debug.h>
 #include <dali/public-api/object/type-registry-helper.h>
 #include <cstring>
+#include <limits>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
@@ -35,6 +37,7 @@
 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
 #include <dali-toolkit/devel-api/controls/control-devel.h>
+#include <dali-toolkit/devel-api/controls/control-wrapper-impl.h>
 
 namespace Dali
 {
@@ -93,6 +96,21 @@ Toolkit::DevelVisual::Type GetVisualTypeFromMap( const Property::Map& map )
   return type;
 }
 
+/**
+ *  Finds visual in given array, returning true if found along with the iterator for that visual as a out parameter
+ */
+bool FindVisual( Property::Index targetIndex, const RegisteredVisualContainer& visuals, RegisteredVisualContainer::Iterator& iter )
+{
+  for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
+  {
+    if ( (*iter)->index ==  targetIndex )
+    {
+      return true;
+    }
+  }
+  return false;
+}
+
 void FindChangableVisuals( Dictionary<Property::Map>& stateVisualsToAdd,
                            Dictionary<Property::Map>& stateVisualsToChange,
                            DictionaryKeys& stateVisualsToRemove)
@@ -113,6 +131,38 @@ void FindChangableVisuals( Dictionary<Property::Map>& stateVisualsToAdd,
   }
 }
 
+Toolkit::Visual::Base GetVisualByName(
+  const RegisteredVisualContainer& visuals,
+  const std::string& visualName )
+{
+  Toolkit::Visual::Base visualHandle;
+
+  RegisteredVisualContainer::Iterator iter;
+  for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
+  {
+    Toolkit::Visual::Base visual = (*iter)->visual;
+    if( visual && visual.GetName() == visualName )
+    {
+      visualHandle = visual;
+      break;
+    }
+  }
+  return visualHandle;
+}
+
+/**
+ * Move visual from source to destination container
+ */
+void MoveVisual( RegisteredVisualContainer::Iterator sourceIter, RegisteredVisualContainer& source, RegisteredVisualContainer& destination )
+{
+   Toolkit::Visual::Base visual = (*sourceIter)->visual;
+   if( visual )
+   {
+     RegisteredVisual* rv = source.Release( sourceIter );
+     destination.PushBack( rv );
+   }
+}
+
 /**
  * Performs actions as requested using the action name.
  * @param[in] object The object on which to perform the action.
@@ -225,7 +275,23 @@ TypeAction registerAction( typeRegistration, ACTION_ACCESSIBILITY_ACTIVATED, &Do
 
 DALI_TYPE_REGISTRATION_END()
 
-
+/**
+ * @brief Iterate through given container and setOffStage any visual found
+ *
+ * @param[in] container Container of visuals
+ * @param[in] parent Parent actor to remove visuals from
+ */
+void SetVisualsOffStage( const RegisteredVisualContainer& container, Actor parent )
+{
+  for( auto iter = container.Begin(), end = container.End() ; iter!= end; iter++)
+  {
+    if( (*iter)->visual )
+    {
+      DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::SetOffStage Setting visual(%d) off stage\n", (*iter)->index );
+      Toolkit::GetImplementation((*iter)->visual).SetOffStage( parent );
+    }
+  }
+}
 
 } // unnamed namespace
 
@@ -285,28 +351,6 @@ const Control::Impl& Control::Impl::Get( const Internal::Control& internalContro
   return *internalControl.mImpl;
 }
 
-
-
-
-Toolkit::Visual::Base Control::Impl::GetVisualByName(
-   RegisteredVisualContainer& visuals,
-  const std::string& visualName )
-{
-  Toolkit::Visual::Base visualHandle;
-
-  RegisteredVisualContainer::Iterator iter;
-  for ( iter = visuals.Begin(); iter != visuals.End(); iter++ )
-  {
-    Toolkit::Visual::Base visual = (*iter)->visual;
-    if( visual && visual.GetName() == visualName )
-    {
-      visualHandle = visual;
-      break;
-    }
-  }
-  return visualHandle;
-}
-
 // Gesture Detection Methods
 void Control::Impl::PinchDetected(Actor actor, const PinchGesture& pinch)
 {
@@ -328,47 +372,234 @@ void Control::Impl::LongPressDetected(Actor actor, const LongPressGesture& longP
   mControlImpl.OnLongPress(longPress);
 }
 
-// Called by a Visual when it's resource is ready
-void Control::Impl::ResourceReady( Visual::Base& object)
+void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual )
+{
+  RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::NOT_SET );
+}
+
+void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, int depthIndex )
 {
+  RegisterVisual( index, visual, VisualState::ENABLED, DepthIndexValue::SET, depthIndex );
+}
+
+void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled )
+{
+  RegisterVisual( index, visual, ( enabled ? VisualState::ENABLED : VisualState::DISABLED ), DepthIndexValue::NOT_SET );
+}
 
-  // go through and check if all the visuals are ready, if they are emit a signal
-  for ( RegisteredVisualContainer::ConstIterator visualIter = mVisuals.Begin();
-        visualIter != mVisuals.End(); ++visualIter )
+void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, bool enabled, int depthIndex )
+{
+  RegisterVisual( index, visual, ( enabled ? VisualState::ENABLED : VisualState::DISABLED ), DepthIndexValue::SET, depthIndex );
+}
+
+void Control::Impl::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visual, VisualState::Type enabled, DepthIndexValue::Type depthIndexValueSet, int depthIndex )
+{
+  DALI_LOG_INFO( gLogFilter, Debug::Concise, "RegisterVisual:%d \n", index );
+
+  bool visualReplaced ( false );
+  Actor self = mControlImpl.Self();
+
+  // Set the depth index, if not set by caller this will be either the current visual depth, max depth of all visuals
+  // or zero.
+  int requiredDepthIndex = visual.GetDepthIndex();
+
+  if( depthIndexValueSet == DepthIndexValue::SET )
   {
-    const Toolkit::Visual::Base visual = (*visualIter)->visual;
-    const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
+    requiredDepthIndex = depthIndex;
+  }
 
-    // one of the visuals is not ready
-    if( !visualImpl.IsResourceReady() )
+  // Visual replacement, existing visual should only be removed from stage when replacement ready.
+  if( !mVisuals.Empty() )
+  {
+    RegisteredVisualContainer::Iterator registeredVisualsiter;
+    // Check if visual (index) is already registered, this is the current visual.
+    if( FindVisual( index, mVisuals, registeredVisualsiter ) )
     {
-      return;
+      Toolkit::Visual::Base& currentRegisteredVisual = (*registeredVisualsiter)->visual;
+      if( currentRegisteredVisual )
+      {
+        // Store current visual depth index as may need to set the replacement visual to same depth
+        const int currentDepthIndex = (*registeredVisualsiter)->visual.GetDepthIndex();
+
+        // No longer required to know if the replaced visual's resources are ready
+        StopObservingVisual( currentRegisteredVisual );
+
+        // If control staged and visual enabled then visuals will be swapped once ready
+        if(  self.OnStage() && enabled )
+        {
+          // Check if visual is currently in the process of being replaced ( is in removal container )
+          RegisteredVisualContainer::Iterator visualQueuedForRemoval;
+          if ( FindVisual( index, mRemoveVisuals, visualQueuedForRemoval ) )
+          {
+            // Visual with same index is already in removal container so current visual pending
+            // Only the the last requested visual will be displayed so remove current visual which is staged but not ready.
+            Toolkit::GetImplementation( currentRegisteredVisual ).SetOffStage( self );
+            mVisuals.Erase( registeredVisualsiter );
+          }
+          else
+          {
+            // current visual not already in removal container so add now.
+            DALI_LOG_INFO( gLogFilter, Debug::Verbose, "RegisterVisual Move current registered visual to removal Queue: %d \n", index );
+            MoveVisual( registeredVisualsiter, mVisuals, mRemoveVisuals );
+          }
+        }
+        else
+        {
+          // Control not staged or visual disabled so can just erase from registered visuals and new visual will be added later.
+          mVisuals.Erase( registeredVisualsiter );
+        }
+
+        // If we've not set the depth-index value and the new visual does not have a depth index applied to it, then use the previously set depth-index for this index
+        if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
+            ( visual.GetDepthIndex() == 0 ) )
+        {
+          requiredDepthIndex = currentDepthIndex;
+        }
+      }
+
+      visualReplaced = true;
+    }
+  }
+
+  // If not set, set the name of the visual to the same name as the control's property.
+  // ( If the control has been type registered )
+  if( visual.GetName().empty() )
+  {
+    try
+    {
+      std::string visualName = self.GetPropertyName( index );
+      if( !visualName.empty() )
+      {
+        DALI_LOG_INFO( gLogFilter, Debug::Concise, "Setting visual name for property %d to %s\n",
+                       index, visualName.c_str() );
+        visual.SetName( visualName );
+      }
+    }
+    catch( Dali::DaliException e )
+    {
+      DALI_LOG_WARNING( "Attempting to register visual without a registered property, index: %d\n", index );
+    }
+  }
+
+  if( !visualReplaced ) // New registration entry
+  {
+    // monitor when the visual resources are ready
+    StartObservingVisual( visual );
+
+    // If we've not set the depth-index value, we have more than one visual and the visual does not have a depth index, then set it to be the highest
+    if( ( depthIndexValueSet == DepthIndexValue::NOT_SET ) &&
+        ( mVisuals.Size() > 0 ) &&
+        ( visual.GetDepthIndex() == 0 ) )
+    {
+      int maxDepthIndex = std::numeric_limits< int >::min();
+
+      RegisteredVisualContainer::ConstIterator iter;
+      const RegisteredVisualContainer::ConstIterator endIter = mVisuals.End();
+      for ( iter = mVisuals.Begin(); iter != endIter; iter++ )
+      {
+        const int visualDepthIndex = (*iter)->visual.GetDepthIndex();
+        if ( visualDepthIndex > maxDepthIndex )
+        {
+          maxDepthIndex = visualDepthIndex;
+        }
+      }
+      ++maxDepthIndex; // Add one to the current maximum depth index so that our added visual appears on top
+      requiredDepthIndex = std::max( 0, maxDepthIndex ); // Start at zero if maxDepth index belongs to a background
     }
   }
 
-  // all the visuals are ready
-  Dali::Toolkit::Control handle( mControlImpl.GetOwner() );
-  mResourceReadySignal.Emit( handle );
+  if( visual )
+  {
+    // Set determined depth index
+    visual.SetDepthIndex( requiredDepthIndex );
+
+    // Monitor when the visual resources are ready
+    StartObservingVisual( visual );
+
+    DALI_LOG_INFO( gLogFilter, Debug::Concise, "New Visual registration index[%d] depth[%d]\n", index, requiredDepthIndex );
+    RegisteredVisual* newRegisteredVisual  = new RegisteredVisual( index, visual,
+                                             ( enabled == VisualState::ENABLED ? true : false ),
+                                             ( visualReplaced && enabled ) ) ;
+    mVisuals.PushBack( newRegisteredVisual );
+
+    // Put on stage if enabled and the control is already on the stage
+    // Visual must be set on stage for the renderer to be created and the ResourceReady triggered.
+    if( ( enabled == VisualState::ENABLED ) && self.OnStage() )
+    {
+      Toolkit::GetImplementation(visual).SetOnStage( self );
+    }
+  }
 
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::RegisterVisual() Registered %s(%d), enabled:%s\n",  visual.GetName().c_str(), index, enabled?"true":"false" );
 }
 
-bool Control::Impl::IsResourceReady() const
+void Control::Impl::UnregisterVisual( Property::Index index )
 {
-  // go through and check all the visuals are ready
-  for ( RegisteredVisualContainer::ConstIterator visualIter = mVisuals.Begin();
-         visualIter != mVisuals.End(); ++visualIter )
+   RegisteredVisualContainer::Iterator iter;
+   if ( FindVisual( index, mVisuals, iter ) )
    {
-     const Toolkit::Visual::Base visual = (*visualIter)->visual;
-     const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
-
-     // one of the visuals is not ready
-     if( !visualImpl.IsResourceReady()  )
-     {
-       return false;
-     }
+     // stop observing visual
+     StopObservingVisual( (*iter)->visual );
+
+     Actor self( mControlImpl.Self() );
+     Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
+     (*iter)->visual.Reset();
+     mVisuals.Erase( iter );
    }
-  return true;
 }
+
+Toolkit::Visual::Base Control::Impl::GetVisual( Property::Index index ) const
+{
+  RegisteredVisualContainer::Iterator iter;
+  if ( FindVisual( index, mVisuals, iter ) )
+  {
+    return (*iter)->visual;
+  }
+
+  return Toolkit::Visual::Base();
+}
+
+void Control::Impl::EnableVisual( Property::Index index, bool enable )
+{
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual (%d)\n", index);
+
+  RegisteredVisualContainer::Iterator iter;
+  if ( FindVisual( index, mVisuals, iter ) )
+  {
+    if (  (*iter)->enabled == enable )
+    {
+      DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual %s(%d) already %s\n", (*iter)->visual.GetName().c_str(), index, enable?"enabled":"disabled");
+      return;
+    }
+
+    (*iter)->enabled = enable;
+    Actor parentActor = mControlImpl.Self();
+    if ( mControlImpl.Self().OnStage() ) // If control not on Stage then Visual will be added when StageConnection is called.
+    {
+      if ( enable )
+      {
+        DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) on stage \n", (*iter)->visual.GetName().c_str(), index );
+        Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
+      }
+      else
+      {
+        DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting %s(%d) off stage \n", (*iter)->visual.GetName().c_str(), index );
+        Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor );  // No need to call if control not staged.
+      }
+    }
+  }
+}
+
+bool Control::Impl::IsVisualEnabled( Property::Index index ) const
+{
+  RegisteredVisualContainer::Iterator iter;
+  if ( FindVisual( index, mVisuals, iter ) )
+  {
+    return (*iter)->enabled;
+  }
+  return false;
+}
+
 void Control::Impl::StopObservingVisual( Toolkit::Visual::Base& visual )
 {
   Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
@@ -385,14 +616,141 @@ void Control::Impl::StartObservingVisual( Toolkit::Visual::Base& visual)
   visualImpl.AddResourceObserver( *this );
 }
 
-// Properties
+// Called by a Visual when it's resource is ready
+void Control::Impl::ResourceReady( Visual::Base& object)
+{
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "ResourceReady replacements pending[%d]\n", mRemoveVisuals.Count() );
+
+  Actor self = mControlImpl.Self();
+
+  // A resource is ready, find resource in the registered visuals container and get its index
+  for( auto registeredIter = mVisuals.Begin(),  end = mVisuals.End(); registeredIter != end; ++registeredIter )
+  {
+    Internal::Visual::Base& registeredVisualImpl = Toolkit::GetImplementation( (*registeredIter)->visual );
+
+    if( &object == &registeredVisualImpl )
+    {
+      RegisteredVisualContainer::Iterator visualToRemoveIter;
+      // Find visual with the same index in the removal container
+      // Set if off stage as it's replacement is now ready.
+      // Remove if from removal list as now removed from stage.
+      // Set Pending flag on the ready visual to false as now ready.
+      if( FindVisual( (*registeredIter)->index, mRemoveVisuals, visualToRemoveIter ) )
+      {
+        (*registeredIter)->pending = false;
+        Toolkit::GetImplementation( (*visualToRemoveIter)->visual ).SetOffStage( self );
+        mRemoveVisuals.Erase( visualToRemoveIter );
+      }
+      break;
+    }
+  }
+
+  // A visual is ready so control may need relayouting
+  mControlImpl.RelayoutRequest();
+
+  // Emit signal if all enabled visuals registered by the control are ready.
+  if( IsResourceReady() )
+  {
+    Dali::Toolkit::Control handle( mControlImpl.GetOwner() );
+    mResourceReadySignal.Emit( handle );
+  }
+}
+
+bool Control::Impl::IsResourceReady() const
+{
+  // Iterate through and check all the enabled visuals are ready
+  for( auto visualIter = mVisuals.Begin();
+         visualIter != mVisuals.End(); ++visualIter )
+  {
+    const Toolkit::Visual::Base visual = (*visualIter)->visual;
+    const Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
+
+    // one of the enabled visuals is not ready
+    if( !visualImpl.IsResourceReady() && (*visualIter)->enabled )
+    {
+      return false;
+    }
+  }
+  return true;
+}
+
+
+Dali::Animation Control::Impl::CreateTransition( const Toolkit::TransitionData& handle )
+{
+  Dali::Animation transition;
+  const Internal::TransitionData& transitionData = Toolkit::GetImplementation( handle );
+
+  if( transitionData.Count() > 0 )
+  {
+    // Setup a Transition from TransitionData.
+    TransitionData::Iterator end = transitionData.End();
+    for( TransitionData::Iterator iter = transitionData.Begin() ;
+         iter != end; ++iter )
+    {
+      TransitionData::Animator* animator = (*iter);
+
+      Toolkit::Visual::Base visual = GetVisualByName( mVisuals, animator->objectName );
+
+      if( visual )
+      {
+#if defined(DEBUG_ENABLED)
+        Dali::TypeInfo typeInfo;
+        ControlWrapper* controlWrapperImpl = dynamic_cast<ControlWrapper*>(&mControlImpl);
+        if( controlWrapperImpl )
+        {
+          typeInfo = controlWrapperImpl->GetTypeInfo();
+        }
+
+        DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Found %s visual for %s\n",
+                       visual.GetName().c_str(), typeInfo?typeInfo.GetName().c_str():"Unknown" );
+#endif
+        Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
+        visualImpl.AnimateProperty( transition, *animator );
+      }
+      else
+      {
+        DALI_LOG_INFO( gLogFilter, Debug::Concise, "CreateTransition: Could not find visual. Trying actors");
+        // Otherwise, try any actor children of control (Including the control)
+        Actor child = mControlImpl.Self().FindChildByName( animator->objectName );
+        if( child )
+        {
+          Property::Index propertyIndex = DevelHandle::GetPropertyIndex( child, animator->propertyKey );
+          if( propertyIndex != Property::INVALID_INDEX )
+          {
+            if( animator->animate == false )
+            {
+              if( animator->targetValue.GetType() != Property::NONE )
+              {
+                child.SetProperty( propertyIndex, animator->targetValue );
+              }
+            }
+            else // animate the property
+            {
+              if( animator->initialValue.GetType() != Property::NONE )
+              {
+                child.SetProperty( propertyIndex, animator->initialValue );
+              }
+
+              if( ! transition )
+              {
+                transition = Dali::Animation::New( 0.1f );
+              }
+
+              transition.AnimateTo( Property( child, propertyIndex ),
+                                    animator->targetValue,
+                                    animator->alphaFunction,
+                                    TimePeriod( animator->timePeriodDelay,
+                                                animator->timePeriodDuration ) );
+            }
+          }
+        }
+      }
+    }
+  }
+
+  return transition;
+}
 
-/**
- * Called when a property of an object of this type is set.
- * @param[in] object The object whose property is set.
- * @param[in] index The property index.
- * @param[in] value The new property value.
- */
 void Control::Impl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
 {
   Toolkit::Control control = Toolkit::Control::DownCast( BaseHandle( object ) );
@@ -537,8 +895,7 @@ void Control::Impl::SetProperty( BaseObject* object, Property::Index index, cons
           Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( url, ImageDimensions() );
           if( visual )
           {
-            controlImpl.RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
-            visual.SetDepthIndex( DepthIndex::BACKGROUND );
+            controlImpl.mImpl->RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND );
           }
         }
         else if( value.Get( color ) )
@@ -566,12 +923,6 @@ void Control::Impl::SetProperty( BaseObject* object, Property::Index index, cons
   }
 }
 
-/**
- * Called to retrieve a property of an object of this type.
- * @param[in] object The object whose property is to be retrieved.
- * @param[in] index The property index.
- * @return The current value of the property.
- */
 Property::Value Control::Impl::GetProperty( BaseObject* object, Property::Index index )
 {
   Property::Value value;
@@ -637,7 +988,7 @@ Property::Value Control::Impl::GetProperty( BaseObject* object, Property::Index
       {
         DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
         Property::Map map;
-        Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
+        Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
         if( visual )
         {
           visual.CreatePropertyMap( map );
@@ -655,7 +1006,7 @@ Property::Value Control::Impl::GetProperty( BaseObject* object, Property::Index
       case Toolkit::Control::Property::BACKGROUND:
       {
         Property::Map map;
-        Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
+        Toolkit::Visual::Base visual = controlImpl.mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
         if( visual )
         {
           visual.CreatePropertyMap( map );
@@ -725,12 +1076,6 @@ void Control::Impl::RemoveVisuals( RegisteredVisualContainer& visuals, Dictionar
   }
 }
 
-
-/**
- * Go through the list of visuals that are common to both states.
- * If they are different types, or are both image types with different
- * URLs, then the existing visual needs moving and the new visual creating
- */
 void Control::Impl::RecreateChangedVisuals( Dictionary<Property::Map>& stateVisualsToChange,
                              Dictionary<Property::Map>& instancedProperties )
 {
@@ -927,6 +1272,32 @@ void Control::Impl::SetSubState( const std::string& subStateName, bool withTrans
   }
 }
 
+void Control::Impl::OnStageDisconnection()
+{
+  Actor self = mControlImpl.Self();
+
+  // Any visuals set for replacement but not yet ready should still be registered.
+  // Reason: If a request was made to register a new visual but the control removed from stage before visual was ready
+  // then when this control appears back on stage it should use that new visual.
+
+  // Iterate through all registered visuals and set off stage
+  SetVisualsOffStage( mVisuals, self );
+
+  // Visuals pending replacement can now be taken out of the removal list and set off stage
+  // Iterate through all replacement visuals and add to a move queue then set off stage
+  for( auto removalIter = mRemoveVisuals.Begin(), end = mRemoveVisuals.End(); removalIter != end; removalIter++ )
+  {
+    Toolkit::GetImplementation((*removalIter)->visual).SetOffStage( self );
+  }
+
+  for( auto replacedIter = mVisuals.Begin(), end = mVisuals.End(); replacedIter != end; replacedIter++ )
+  {
+    (*replacedIter)->pending = false;
+  }
+
+  mRemoveVisuals.Clear();
+}
+
 } // namespace Internal
 
 } // namespace Toolkit