Rename OnStage signals and related internal changes
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / focus-manager / keyinput-focus-manager-impl.cpp
index 8fa9500..2549e52 100644 (file)
@@ -1,25 +1,30 @@
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
 // CLASS HEADER
 #include "keyinput-focus-manager-impl.h"
 
-// INTERNAL INCLUDES
+// EXTERNAL INCLUDES
+#include <cstring> // for strcmp
+#include <dali/public-api/actors/layer.h>
 #include <dali-toolkit/public-api/controls/control-impl.h>
 #include <dali/integration-api/debug.h>
+#include <dali/integration-api/adaptor-framework/adaptor.h>
+#include <dali/integration-api/adaptor-framework/scene-holder.h>
 
 namespace Dali
 {
@@ -30,198 +35,157 @@ namespace Toolkit
 namespace Internal
 {
 
+namespace
+{
+
+// Signals
+
+const char* const SIGNAL_KEY_INPUT_FOCUS_CHANGED = "keyInputFocusChanged";
+
+}
+
 KeyInputFocusManager::KeyInputFocusManager()
-: mSlotDelegate( this )
+: mSlotDelegate( this ),
+  mCurrentFocusControl()
 {
-  Stage::GetCurrent().KeyEventSignal().Connect(mSlotDelegate, &KeyInputFocusManager::OnKeyEvent);
+  // Retrieve all the existing widnows
+  Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
+  for( auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter )
+  {
+    ( *iter ).KeyEventGeneratedSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnKeyEvent );
+  }
+
+  // Get notified when any new scene holder is created afterwards
+  Adaptor::Get().WindowCreatedSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnSceneHolderCreated );
 }
 
 KeyInputFocusManager::~KeyInputFocusManager()
 {
 }
 
-void KeyInputFocusManager::SetFocus(Toolkit::Control control)
+void KeyInputFocusManager::OnSceneHolderCreated( Dali::Integration::SceneHolder& sceneHolder )
 {
-  if(!control)
+  sceneHolder.KeyEventGeneratedSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnKeyEvent );
+}
+
+void KeyInputFocusManager::SetFocus( Toolkit::Control control )
+{
+  if( !control )
   {
-   //No-op
+    // No-op
     return;
   }
 
-  unsigned int actorID = control.GetId();
-
-  ActorQueueIterator pos = std::find( mFocusActorsQueue.begin(), mFocusActorsQueue.end(), actorID);
-
-  if((!mFocusActorsQueue.empty()) && (pos == mFocusActorsQueue.begin()))
+  if( control == mCurrentFocusControl )
   {
-    //Actor allready in front, so No-op
+    // Control already has focus
     return;
   }
 
-  if(pos != mFocusActorsQueue.end())
-  {
-    //A previously focused actor wants to regain focus
-    mFocusActorsQueue.erase(pos);
-  }
-  else
-  {
-    control.OffStageSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnFocusActorStageDisconnection );
-  }
+  control.OffSceneSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlSceneDisconnection );
 
-  Dali::Toolkit::Control previousFocusControl;
-  if(!mFocusActorsQueue.empty())
+  Dali::Toolkit::Control previousFocusControl = GetCurrentFocusControl();
+  if( previousFocusControl )
   {
-    previousFocusControl = Dali::Toolkit::Control::DownCast(Stage::GetCurrent().GetRootLayer().FindChildById(mFocusActorsQueue.front()));
-    if(previousFocusControl)
-    {
-      // Notify the control that it has lost key input focus
-      previousFocusControl.GetImplementation().OnKeyInputFocusLost();
-    }
+    // Notify the control that it has lost key input focus
+    GetImplementation( previousFocusControl ).OnKeyInputFocusLost();
   }
 
-  mFocusActorsQueue.push_front(actorID);
+  // Set control to currentFocusControl
+  mCurrentFocusControl = control;
 
   // Tell the new actor that it has gained focus.
-  control.GetImplementation().OnKeyInputFocusGained();
+  GetImplementation( control ).OnKeyInputFocusGained();
 
   // Emit the signal to inform focus change to the application.
-  if ( !mKeyInputFocusChangedSignalV2.Empty() )
-  {
-    mKeyInputFocusChangedSignalV2.Emit( control, previousFocusControl );
-  }
-}
-
-Control KeyInputFocusManager::GetCurrentFocusControl() const
-{
-  Control currentFocusControl;
-
-  if(!mFocusActorsQueue.empty())
+  if ( !mKeyInputFocusChangedSignal.Empty() )
   {
-    currentFocusControl = Dali::Toolkit::Control::DownCast(Stage::GetCurrent().GetRootLayer().FindChildById(mFocusActorsQueue.front()));
+    mKeyInputFocusChangedSignal.Emit( control, previousFocusControl );
   }
-
-  return currentFocusControl;
 }
 
-void KeyInputFocusManager::RemoveFocus(Toolkit::Control control)
+void KeyInputFocusManager::RemoveFocus( Toolkit::Control control )
 {
-  if(control)
+  if( control == mCurrentFocusControl )
   {
-    unsigned int actorId = control.GetId();
-    ActorQueueIterator pos = std::find( mFocusActorsQueue.begin(), mFocusActorsQueue.end(), actorId);
-
-    if(pos != mFocusActorsQueue.end())
-    {
-      control.OffStageSignal().Disconnect( mSlotDelegate, &KeyInputFocusManager::OnFocusActorStageDisconnection );
+    control.OffSceneSignal().Disconnect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlSceneDisconnection );
 
-      // Notify the control that it has lost key input focus
-      control.GetImplementation().OnKeyInputFocusLost();
+    // Notify the control that it has lost key input focus
+    GetImplementation( control ).OnKeyInputFocusLost();
 
-      if(pos == mFocusActorsQueue.begin())
-      {
-        Actor previousFocusActor;
-
-        mFocusActorsQueue.erase(pos);
-        if(!mFocusActorsQueue.empty())
-        {
-          previousFocusActor = Stage::GetCurrent().GetRootLayer().FindChildById(mFocusActorsQueue.front());
-        }
-
-        Dali::Toolkit::Control previouscontrol = Dali::Toolkit::Control::DownCast(previousFocusActor);
-        if(previouscontrol)
-        {
-          // Tell the new actor that it has gained focus.
-          previouscontrol.GetImplementation().OnKeyInputFocusGained();
-        }
-      }
-      else
-      {
-        //If the removed actor is not currently focused, then no need to emit signal.
-        mFocusActorsQueue.erase(pos);
-      }
-
-    }
+    mCurrentFocusControl.Reset();
   }
 }
 
-bool KeyInputFocusManager::IsKeyboardListener(Toolkit::Control control) const
+Toolkit::Control KeyInputFocusManager::GetCurrentFocusControl() const
 {
-  bool result = false;
-
-  if(!mFocusActorsQueue.empty())
-  {
-    unsigned int actorId = control.GetId();
-    ActorQueueConstIterator pos = std::find(mFocusActorsQueue.begin(), mFocusActorsQueue.end(), actorId);
-
-    if(pos != mFocusActorsQueue.end())
-    {
-      result = true;
-    }
-  }
-
-  return result;
+  return mCurrentFocusControl;
 }
 
-Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalV2& KeyInputFocusManager::KeyInputFocusChangedSignal()
+Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalType& KeyInputFocusManager::KeyInputFocusChangedSignal()
 {
-  return mKeyInputFocusChangedSignalV2;
+  return mKeyInputFocusChangedSignal;
 }
 
-Toolkit::KeyInputFocusManager::UnhandledKeyEventSignalV2& KeyInputFocusManager::UnhandledKeyEventSignal()
+bool KeyInputFocusManager::OnKeyEvent( const KeyEvent& event )
 {
-  return mUnhandledKeyEventSignalV2;
+  bool consumed = false;
+
+  Toolkit::Control control = GetCurrentFocusControl();
+  if( control )
+  {
+    // Notify the control about the key event
+    consumed = EmitKeyEventSignal( control, event );
+  }
+
+  return consumed;
 }
 
-void KeyInputFocusManager::OnKeyEvent(const KeyEvent& event)
+bool KeyInputFocusManager::EmitKeyEventSignal( Toolkit::Control control, const KeyEvent& event )
 {
   bool consumed = false;
 
-  ActorQueueIterator iter = mFocusActorsQueue.begin();
-
-  Layer rootLayer = Stage::GetCurrent().GetRootLayer();
-
-  while(!mFocusActorsQueue.empty() && !consumed && (iter != mFocusActorsQueue.end()))
+  if( control )
   {
-    Actor actor = rootLayer.FindChildById(*iter);
-    Dali::Toolkit::Control control = Dali::Toolkit::Control::DownCast(actor);
-    if(control)
-    {
-      // Notify the control about the key event
-      consumed = control.GetImplementation().EmitKeyEventSignal(event);
-    }
-    iter++;
-  }
+    consumed = GetImplementation( control ).EmitKeyEventSignal( event );
 
-  if(!consumed)
-  {
-    // Emit signal to inform that a key event is not consumed.
-    if( !mUnhandledKeyEventSignalV2.Empty() )
+    // if control doesn't consume KeyEvent, give KeyEvent to its parent.
+    if( !consumed )
     {
-      mUnhandledKeyEventSignalV2.Emit(event);
+      Toolkit::Control parent = Toolkit::Control::DownCast( control.GetParent() );
+
+      if( parent )
+      {
+        consumed = EmitKeyEventSignal( parent, event );
+      }
     }
   }
+
+  return consumed;
 }
 
-void KeyInputFocusManager::OnFocusActorStageDisconnection( Dali::Actor actor )
+void KeyInputFocusManager::OnFocusControlSceneDisconnection( Dali::Actor actor )
 {
-  RemoveFocus(Dali::Toolkit::Control::DownCast(actor));
+  RemoveFocus( Dali::Toolkit::Control::DownCast( actor ) );
 }
 
+
 bool KeyInputFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
 {
-  Dali::BaseHandle handle( object );
-
   bool connected( true );
-  KeyInputFocusManager* manager = dynamic_cast<KeyInputFocusManager*>(object);
+  KeyInputFocusManager* manager = dynamic_cast<KeyInputFocusManager*>( object );
 
-  if( Dali::Toolkit::KeyInputFocusManager::SIGNAL_KEY_INPUT_FOCUS_CHANGED == signalName )
-  {
-    manager->KeyInputFocusChangedSignal().Connect( tracker, functor );
-  }
-  else
+  if( manager )
   {
-    // signalName does not match any signal
-    connected = false;
+    if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_CHANGED ) )
+    {
+      manager->KeyInputFocusChangedSignal().Connect( tracker, functor );
+    }
+    else
+    {
+      // signalName does not match any signal
+      connected = false;
+    }
   }
 
   return connected;