(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 72f9359..e6aae87 100644 (file)
@@ -37,7 +37,6 @@
 #include <dali-toolkit/public-api/controls/control-impl.h>
 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
 #include <dali-toolkit/public-api/accessibility-manager/accessibility-manager.h>
-#include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
 #include <dali-toolkit/devel-api/controls/control-devel.h>
 
 namespace Dali
@@ -115,7 +114,7 @@ KeyboardFocusManager::KeyboardFocusManager()
   mFocusChangedSignal(),
   mFocusGroupChangedSignal(),
   mFocusedActorEnterKeySignal(),
-  mCurrentFocusActor( 0 ),
+  mCurrentFocusActor(),
   mFocusIndicatorActor(),
   mFocusGroupLoopEnabled( false ),
   mIsFocusIndicatorEnabled( false ),
@@ -125,7 +124,7 @@ KeyboardFocusManager::KeyboardFocusManager()
   mCustomAlgorithmInterface(NULL)
 {
   // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorEnabled.
-  Toolkit::KeyInputFocusManager::Get().UnhandledKeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
+  Stage::GetCurrent().KeyEventSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
   Stage::GetCurrent().TouchSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
 }
 
@@ -145,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 )
     {
@@ -170,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 )
@@ -202,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()
@@ -505,7 +511,7 @@ void KeyboardFocusManager::ClearFocus()
     }
   }
 
-  mCurrentFocusActor = 0;
+  mCurrentFocusActor.Reset();
   mIsFocusIndicatorEnabled = false;
 }