2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "keyinput-focus-manager-impl.h"
22 #include <cstring> // for strcmp
23 #include <dali/public-api/actors/layer.h>
24 #include <dali/public-api/common/stage.h>
25 #include <dali/devel-api/common/stage-devel.h>
26 #include <dali-toolkit/public-api/controls/control-impl.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/integration-api/adaptor-framework/adaptor.h>
29 #include <dali/integration-api/adaptor-framework/scene-holder.h>
45 const char* const SIGNAL_KEY_INPUT_FOCUS_CHANGED = "keyInputFocusChanged";
49 KeyInputFocusManager::KeyInputFocusManager()
50 : mSlotDelegate( this ),
51 mCurrentFocusControl()
53 // Retrieve all the existing widnows
54 Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
55 for( auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter )
57 ( *iter ).KeyEventGeneratedSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnKeyEvent );
60 // Get notified when any new scene holder is created afterwards
61 Adaptor::Get().WindowCreatedSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnSceneHolderCreated );
64 KeyInputFocusManager::~KeyInputFocusManager()
68 void KeyInputFocusManager::OnSceneHolderCreated( Dali::Integration::SceneHolder& sceneHolder )
70 sceneHolder.KeyEventGeneratedSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnKeyEvent );
73 void KeyInputFocusManager::SetFocus( Toolkit::Control control )
81 if( control == mCurrentFocusControl )
83 // Control already has focus
87 control.OffStageSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
89 Dali::Toolkit::Control previousFocusControl = GetCurrentFocusControl();
90 if( previousFocusControl )
92 // Notify the control that it has lost key input focus
93 GetImplementation( previousFocusControl ).OnKeyInputFocusLost();
96 // Set control to currentFocusControl
97 mCurrentFocusControl = control;
99 // Tell the new actor that it has gained focus.
100 GetImplementation( control ).OnKeyInputFocusGained();
102 // Emit the signal to inform focus change to the application.
103 if ( !mKeyInputFocusChangedSignal.Empty() )
105 mKeyInputFocusChangedSignal.Emit( control, previousFocusControl );
109 void KeyInputFocusManager::RemoveFocus( Toolkit::Control control )
111 if( control == mCurrentFocusControl )
113 control.OffStageSignal().Disconnect( mSlotDelegate, &KeyInputFocusManager::OnFocusControlStageDisconnection );
115 // Notify the control that it has lost key input focus
116 GetImplementation( control ).OnKeyInputFocusLost();
118 mCurrentFocusControl.Reset();
122 Toolkit::Control KeyInputFocusManager::GetCurrentFocusControl() const
124 return mCurrentFocusControl;
127 Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalType& KeyInputFocusManager::KeyInputFocusChangedSignal()
129 return mKeyInputFocusChangedSignal;
132 bool KeyInputFocusManager::OnKeyEvent( const KeyEvent& event )
134 bool consumed = false;
136 Toolkit::Control control = GetCurrentFocusControl();
139 // Notify the control about the key event
140 consumed = EmitKeyEventSignal( control, event );
146 bool KeyInputFocusManager::EmitKeyEventSignal( Toolkit::Control control, const KeyEvent& event )
148 bool consumed = false;
152 consumed = GetImplementation( control ).EmitKeyEventSignal( event );
154 // if control doesn't consume KeyEvent, give KeyEvent to its parent.
157 Toolkit::Control parent = Toolkit::Control::DownCast( control.GetParent() );
161 consumed = EmitKeyEventSignal( parent, event );
169 void KeyInputFocusManager::OnFocusControlStageDisconnection( Dali::Actor actor )
171 RemoveFocus( Dali::Toolkit::Control::DownCast( actor ) );
175 bool KeyInputFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
177 bool connected( true );
178 KeyInputFocusManager* manager = dynamic_cast<KeyInputFocusManager*>( object );
182 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_INPUT_FOCUS_CHANGED ) )
184 manager->KeyInputFocusChangedSignal().Connect( tracker, functor );
188 // signalName does not match any signal
196 } // namespace Internal
198 } // namespace Toolkit