2 * Copyright (c) 2014 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 <dali/public-api/actors/layer.h>
23 #include <dali/public-api/common/stage.h>
26 #include <dali-toolkit/public-api/controls/control-impl.h>
27 #include <dali/integration-api/debug.h>
38 KeyInputFocusManager::KeyInputFocusManager()
39 : mSlotDelegate( this )
41 Stage::GetCurrent().KeyEventSignal().Connect(mSlotDelegate, &KeyInputFocusManager::OnKeyEvent);
44 KeyInputFocusManager::~KeyInputFocusManager()
48 void KeyInputFocusManager::SetFocus(Toolkit::Control control)
56 unsigned int actorID = control.GetId();
58 ActorQueueIterator pos = std::find( mFocusActorsQueue.begin(), mFocusActorsQueue.end(), actorID);
60 if((!mFocusActorsQueue.empty()) && (pos == mFocusActorsQueue.begin()))
62 //Actor allready in front, so No-op
66 if(pos != mFocusActorsQueue.end())
68 //A previously focused actor wants to regain focus
69 mFocusActorsQueue.erase(pos);
73 control.OffStageSignal().Connect( mSlotDelegate, &KeyInputFocusManager::OnFocusActorStageDisconnection );
76 Dali::Toolkit::Control previousFocusControl;
77 if(!mFocusActorsQueue.empty())
79 previousFocusControl = Dali::Toolkit::Control::DownCast(Stage::GetCurrent().GetRootLayer().FindChildById(mFocusActorsQueue.front()));
80 if(previousFocusControl)
82 // Notify the control that it has lost key input focus
83 previousFocusControl.GetImplementation().OnKeyInputFocusLost();
87 mFocusActorsQueue.push_front(actorID);
89 // Tell the new actor that it has gained focus.
90 control.GetImplementation().OnKeyInputFocusGained();
92 // Emit the signal to inform focus change to the application.
93 if ( !mKeyInputFocusChangedSignalV2.Empty() )
95 mKeyInputFocusChangedSignalV2.Emit( control, previousFocusControl );
99 Toolkit::Control KeyInputFocusManager::GetCurrentFocusControl() const
101 Toolkit::Control currentFocusControl;
103 if(!mFocusActorsQueue.empty())
105 currentFocusControl = Dali::Toolkit::Control::DownCast(Stage::GetCurrent().GetRootLayer().FindChildById(mFocusActorsQueue.front()));
108 return currentFocusControl;
111 void KeyInputFocusManager::RemoveFocus(Toolkit::Control control)
115 unsigned int actorId = control.GetId();
116 ActorQueueIterator pos = std::find( mFocusActorsQueue.begin(), mFocusActorsQueue.end(), actorId);
118 if(pos != mFocusActorsQueue.end())
120 control.OffStageSignal().Disconnect( mSlotDelegate, &KeyInputFocusManager::OnFocusActorStageDisconnection );
122 // Notify the control that it has lost key input focus
123 control.GetImplementation().OnKeyInputFocusLost();
125 if(pos == mFocusActorsQueue.begin())
127 Actor previousFocusActor;
129 mFocusActorsQueue.erase(pos);
130 if(!mFocusActorsQueue.empty())
132 previousFocusActor = Stage::GetCurrent().GetRootLayer().FindChildById(mFocusActorsQueue.front());
135 Dali::Toolkit::Control previouscontrol = Dali::Toolkit::Control::DownCast(previousFocusActor);
138 // Tell the new actor that it has gained focus.
139 previouscontrol.GetImplementation().OnKeyInputFocusGained();
144 //If the removed actor is not currently focused, then no need to emit signal.
145 mFocusActorsQueue.erase(pos);
152 bool KeyInputFocusManager::IsKeyboardListener(Toolkit::Control control) const
156 if(!mFocusActorsQueue.empty())
158 unsigned int actorId = control.GetId();
159 ActorQueueConstIterator pos = std::find(mFocusActorsQueue.begin(), mFocusActorsQueue.end(), actorId);
161 if(pos != mFocusActorsQueue.end())
170 Toolkit::KeyInputFocusManager::KeyInputFocusChangedSignalV2& KeyInputFocusManager::KeyInputFocusChangedSignal()
172 return mKeyInputFocusChangedSignalV2;
175 Toolkit::KeyInputFocusManager::UnhandledKeyEventSignalV2& KeyInputFocusManager::UnhandledKeyEventSignal()
177 return mUnhandledKeyEventSignalV2;
180 void KeyInputFocusManager::OnKeyEvent(const KeyEvent& event)
182 bool consumed = false;
184 ActorQueueIterator iter = mFocusActorsQueue.begin();
186 Layer rootLayer = Stage::GetCurrent().GetRootLayer();
188 while(!mFocusActorsQueue.empty() && !consumed && (iter != mFocusActorsQueue.end()))
190 Actor actor = rootLayer.FindChildById(*iter);
191 Dali::Toolkit::Control control = Dali::Toolkit::Control::DownCast(actor);
194 // Notify the control about the key event
195 consumed = control.GetImplementation().EmitKeyEventSignal(event);
202 // Emit signal to inform that a key event is not consumed.
203 if( !mUnhandledKeyEventSignalV2.Empty() )
205 mUnhandledKeyEventSignalV2.Emit(event);
210 void KeyInputFocusManager::OnFocusActorStageDisconnection( Dali::Actor actor )
212 RemoveFocus(Dali::Toolkit::Control::DownCast(actor));
215 bool KeyInputFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
217 Dali::BaseHandle handle( object );
219 bool connected( true );
220 KeyInputFocusManager* manager = dynamic_cast<KeyInputFocusManager*>(object);
222 if( Dali::Toolkit::KeyInputFocusManager::SIGNAL_KEY_INPUT_FOCUS_CHANGED == signalName )
224 manager->KeyInputFocusChangedSignal().Connect( tracker, functor );
228 // signalName does not match any signal
235 } // namespace Internal
237 } // namespace Toolkit