(TextController) Reformatted to reduce LOC
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / focus-manager / keyinput-focus-manager-impl.cpp
index 8fa9500..ebdb214 100644 (file)
-//
-// 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) 2021 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 <dali-toolkit/public-api/controls/control-impl.h>
+#include <dali/integration-api/adaptor-framework/adaptor.h>
+#include <dali/integration-api/adaptor-framework/scene-holder.h>
 #include <dali/integration-api/debug.h>
+#include <dali/public-api/actors/layer.h>
+#include <cstring> // for strcmp
+
+// INTERNAL INCLUDES
+#include <dali-toolkit/devel-api/controls/control-devel.h>
 
 namespace Dali
 {
-
 namespace Toolkit
 {
-
 namespace Internal
 {
+namespace
+{
+// Signals
+
+const char* const SIGNAL_KEY_INPUT_FOCUS_CHANGED = "keyInputFocusChanged";
+
+} // namespace
 
 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::OnSceneHolderCreated(Dali::Integration::SceneHolder& sceneHolder)
+{
+  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)
 {
-  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 );
-
-      // Notify the control that it has lost key input focus
-      control.GetImplementation().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);
-      }
+    control.OffSceneSignal().Disconnect(mSlotDelegate, &KeyInputFocusManager::OnFocusControlSceneDisconnection);
 
-    }
-  }
-}
-
-bool KeyInputFocusManager::IsKeyboardListener(Toolkit::Control control) const
-{
-  bool result = false;
-
-  if(!mFocusActorsQueue.empty())
-  {
-    unsigned int actorId = control.GetId();
-    ActorQueueConstIterator pos = std::find(mFocusActorsQueue.begin(), mFocusActorsQueue.end(), actorId);
+    // Notify the control that it has lost key input focus
+    GetImplementation(control).OnKeyInputFocusLost();
 
-    if(pos != mFocusActorsQueue.end())
-    {
-      result = true;
-    }
+    mCurrentFocusControl.Reset();
   }
-
-  return result;
 }
 
-Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalV2& KeyInputFocusManager::KeyInputFocusChangedSignal()
+Toolkit::Control KeyInputFocusManager::GetCurrentFocusControl() const
 {
-  return mKeyInputFocusChangedSignalV2;
+  return mCurrentFocusControl;
 }
 
-Toolkit::KeyInputFocusManager::UnhandledKeyEventSignalV2& KeyInputFocusManager::UnhandledKeyEventSignal()
+Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalType& KeyInputFocusManager::KeyInputFocusChangedSignal()
 {
-  return mUnhandledKeyEventSignalV2;
+  return mKeyInputFocusChangedSignal;
 }
 
-void KeyInputFocusManager::OnKeyEvent(const KeyEvent& event)
+bool KeyInputFocusManager::OnKeyEvent(const KeyEvent& event)
 {
   bool consumed = false;
 
-  ActorQueueIterator iter = mFocusActorsQueue.begin();
-
-  Layer rootLayer = Stage::GetCurrent().GetRootLayer();
-
-  while(!mFocusActorsQueue.empty() && !consumed && (iter != mFocusActorsQueue.end()))
+  Toolkit::Control control = GetCurrentFocusControl();
+  if(control)
   {
-    Actor actor = rootLayer.FindChildById(*iter);
-    Dali::Toolkit::Control control = Dali::Toolkit::Control::DownCast(actor);
-    if(control)
+    Dali::Actor dispatch = control;
+    while(dispatch)
     {
-      // Notify the control about the key event
-      consumed = control.GetImplementation().EmitKeyEventSignal(event);
+      // If the DISPATCH_KEY_EVENTS is false, it cannot emit key event.
+      Toolkit::Control dispatchControl = Toolkit::Control::DownCast(dispatch);
+      if(dispatchControl && !dispatchControl.GetProperty<bool>(Toolkit::DevelControl::Property::DISPATCH_KEY_EVENTS))
+      {
+        return true;
+      }
+      dispatch = dispatch.GetParent();
     }
-    iter++;
+
+    // Notify the control about the key event
+    consumed = EmitKeyEventSignal(control, event);
   }
 
-  if(!consumed)
+  return consumed;
+}
+
+bool KeyInputFocusManager::EmitKeyEventSignal(Toolkit::Control control, const KeyEvent& event)
+{
+  bool consumed = false;
+
+  if(control)
   {
-    // Emit signal to inform that a key event is not consumed.
-    if( !mUnhandledKeyEventSignalV2.Empty() )
+    consumed = GetImplementation(control).EmitKeyEventSignal(event);
+
+    // 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));
 }
 
-bool KeyInputFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
+bool KeyInputFocusManager::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
 {
-  Dali::BaseHandle handle( object );
-
-  bool connected( true );
+  bool                  connected(true);
   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;