(KeyboardFocusManager) Use weak handle instead of calling FindChildById repeatedly
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / focus-manager / keyboard-focus-manager-impl.cpp
index 57fe076..e6aae87 100644 (file)
@@ -114,7 +114,7 @@ KeyboardFocusManager::KeyboardFocusManager()
   mFocusChangedSignal(),
   mFocusGroupChangedSignal(),
   mFocusedActorEnterKeySignal(),
-  mCurrentFocusActor( 0 ),
+  mCurrentFocusActor(),
   mFocusIndicatorActor(),
   mFocusGroupLoopEnabled( false ),
   mIsFocusIndicatorEnabled( false ),
@@ -144,7 +144,7 @@ bool KeyboardFocusManager::DoSetCurrentFocusActor( Actor actor )
   bool success = false;
 
   // Check whether the actor is in the stage and is keyboard focusable.
-  if( actor && actor.IsKeyboardFocusable() )
+  if( actor && actor.IsKeyboardFocusable() && actor.OnStage() )
   {
     if( mIsFocusIndicatorEnabled )
     {
@@ -169,7 +169,7 @@ bool KeyboardFocusManager::DoSetCurrentFocusActor( Actor actor )
     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
 
     // Save the current focused actor
-    mCurrentFocusActor = actor.GetId();
+    mCurrentFocusActor = actor;
 
     Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
     if( newlyFocusedControl )
@@ -201,8 +201,15 @@ bool KeyboardFocusManager::DoSetCurrentFocusActor( Actor actor )
 
 Actor KeyboardFocusManager::GetCurrentFocusActor()
 {
-  Actor rootActor = Stage::GetCurrent().GetRootLayer();
-  return rootActor.FindChildById(mCurrentFocusActor);
+  Actor actor = mCurrentFocusActor.GetHandle();
+  if( actor && ! actor.OnStage() )
+  {
+    // If the actor has been removed from the stage, then it should not be focused
+
+    actor.Reset();
+    mCurrentFocusActor.Reset();
+  }
+  return actor;
 }
 
 Actor KeyboardFocusManager::GetCurrentFocusGroup()
@@ -504,7 +511,7 @@ void KeyboardFocusManager::ClearFocus()
     }
   }
 
-  mCurrentFocusActor = 0;
+  mCurrentFocusActor.Reset();
   mIsFocusIndicatorEnabled = false;
 }