2 * Copyright (c) 2021 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 "keyboard-focus-manager-impl.h"
22 #include <dali/devel-api/actors/actor-devel.h>
23 #include <dali/devel-api/adaptor-framework/lifecycle-controller.h>
24 #include <dali/devel-api/common/singleton-service.h>
25 #include <dali/integration-api/adaptor-framework/adaptor.h>
26 #include <dali/integration-api/adaptor-framework/scene-holder.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/public-api/actors/layer.h>
29 #include <dali/public-api/animation/constraints.h>
30 #include <dali/public-api/events/key-event.h>
31 #include <dali/public-api/events/touch-event.h>
32 #include <dali/public-api/object/property-map.h>
33 #include <dali/public-api/object/type-registry-helper.h>
34 #include <dali/public-api/object/type-registry.h>
35 #include <cstring> // for strcmp
38 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
39 #include <dali-toolkit/devel-api/controls/control-devel.h>
40 #include <dali-toolkit/devel-api/focus-manager/focus-finder.h>
41 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
42 #include <dali-toolkit/public-api/controls/control-impl.h>
43 #include <dali-toolkit/public-api/controls/control.h>
44 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
45 #include <dali-toolkit/public-api/styling/style-manager.h>
46 #include <dali/devel-api/adaptor-framework/accessibility.h>
54 namespace // Unnamed namespace
56 #if defined(DEBUG_ENABLED)
57 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
60 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
62 const char* const FOCUS_BORDER_IMAGE_FILE_NAME = "keyboard_focus.9.png";
66 BaseHandle handle = KeyboardFocusManager::Get();
70 SingletonService singletonService(SingletonService::Get());
73 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager(new Internal::KeyboardFocusManager());
74 singletonService.Register(typeid(manager), manager);
82 DALI_TYPE_REGISTRATION_BEGIN_CREATE(Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true)
84 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardPreFocusChange", SIGNAL_PRE_FOCUS_CHANGE)
85 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusChanged", SIGNAL_FOCUS_CHANGED)
86 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged", SIGNAL_FOCUS_GROUP_CHANGED)
87 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey", SIGNAL_FOCUSED_ACTOR_ENTER_KEY)
89 DALI_TYPE_REGISTRATION_END()
91 const unsigned int MAX_HISTORY_AMOUNT = 30; ///< Max length of focus history stack
93 } // unnamed namespace
95 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
97 Toolkit::KeyboardFocusManager manager;
99 SingletonService singletonService(SingletonService::Get());
102 // Check whether the keyboard focus manager is already created
103 Dali::BaseHandle handle = singletonService.GetSingleton(typeid(Toolkit::KeyboardFocusManager));
106 // If so, downcast the handle of singleton to keyboard focus manager
107 manager = Toolkit::KeyboardFocusManager(dynamic_cast<KeyboardFocusManager*>(handle.GetObjectPtr()));
114 KeyboardFocusManager::KeyboardFocusManager()
115 : mPreFocusChangeSignal(),
116 mFocusChangedSignal(),
117 mFocusGroupChangedSignal(),
118 mFocusedActorEnterKeySignal(),
119 mCurrentFocusActor(),
120 mFocusIndicatorActor(),
123 mCustomAlgorithmInterface(NULL),
124 mCurrentFocusedWindow(),
125 mIsFocusIndicatorShown(UNKNOWN),
126 mEnableFocusIndicator(ENABLE),
127 mAlwaysShowIndicator(ALWAYS_SHOW),
128 mFocusGroupLoopEnabled(false),
129 mIsWaitingKeyboardFocusChangeCommit(false),
130 mClearFocusOnTouch(true)
132 // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorShown.
134 LifecycleController::Get().InitSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnAdaptorInit);
137 void KeyboardFocusManager::OnAdaptorInit()
139 if(Adaptor::IsAvailable())
141 // Retrieve all the existing scene holders
142 Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
143 for(auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter)
145 (*iter).KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
146 (*iter).TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
147 Dali::Window window = DevelWindow::DownCast(*iter);
150 window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
154 // Get notified when any new scene holder is created afterwards
155 Adaptor::Get().WindowCreatedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnSceneHolderCreated);
159 void KeyboardFocusManager::OnSceneHolderCreated(Dali::Integration::SceneHolder& sceneHolder)
161 sceneHolder.KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
162 sceneHolder.TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
163 Dali::Window window = DevelWindow::DownCast(sceneHolder);
166 window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
170 KeyboardFocusManager::~KeyboardFocusManager()
174 void KeyboardFocusManager::GetConfigurationFromStyleManger()
176 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
179 Property::Map config = Toolkit::DevelStyleManager::GetConfigurations(styleManager);
180 mAlwaysShowIndicator = config["alwaysShowFocus"].Get<bool>() ? ALWAYS_SHOW : NONE;
181 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
182 mClearFocusOnTouch = (mIsFocusIndicatorShown == SHOW) ? false : true;
186 bool KeyboardFocusManager::SetCurrentFocusActor(Actor actor)
188 DALI_ASSERT_DEBUG(!mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?");
190 if(mIsFocusIndicatorShown == UNKNOWN)
192 GetConfigurationFromStyleManger();
195 return DoSetCurrentFocusActor(actor);
198 bool KeyboardFocusManager::DoSetCurrentFocusActor(Actor actor)
200 bool success = false;
201 if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
203 Integration::SceneHolder currentWindow = Integration::SceneHolder::Get(actor);
205 if(currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
207 Layer rootLayer = currentWindow.GetRootLayer();
208 mCurrentFocusedWindow = rootLayer;
212 Actor currentFocusedActor = GetCurrentFocusActor();
214 // If developer set focus on same actor, doing nothing
215 if(actor == currentFocusedActor)
224 // Check whether the actor is in the stage and is keyboard focusable.
225 if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
227 if((mIsFocusIndicatorShown == SHOW) && (mEnableFocusIndicator == ENABLE))
229 actor.Add(GetFocusIndicatorActor());
232 // Send notification for the change of focus actor
233 if(!mFocusChangedSignal.Empty())
235 mFocusChangedSignal.Emit(currentFocusedActor, actor);
238 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
239 if(currentlyFocusedControl)
241 // Do we need it to remember if it was previously DISABLED?
242 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
243 currentlyFocusedControl.ClearKeyInputFocus();
246 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
248 // Save the current focused actor
249 mCurrentFocusActor = actor;
251 bool focusedWindowFound = false;
252 for(unsigned int i = 0; i < mCurrentFocusActors.size(); i++)
254 if(mCurrentFocusActors[i].first == mCurrentFocusedWindow)
256 mCurrentFocusActors[i].second = actor;
257 focusedWindowFound = true;
261 if(!focusedWindowFound)
263 // A new window gains the focus, so store the focused actor in that window.
264 mCurrentFocusActors.push_back(std::pair<WeakHandle<Layer>, WeakHandle<Actor> >(mCurrentFocusedWindow, actor));
267 Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
268 if(newlyFocusedControl)
270 newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED);
271 newlyFocusedControl.SetKeyInputFocus();
274 // Push Current Focused Actor to FocusHistory
275 mFocusHistory.push_back(actor);
277 // Delete first element before add new element when Stack is full.
278 if(mFocusHistory.size() > MAX_HISTORY_AMOUNT)
280 FocusStackIterator beginPos = mFocusHistory.begin();
281 mFocusHistory.erase(beginPos);
284 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
289 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
295 Actor KeyboardFocusManager::GetCurrentFocusActor()
297 Actor actor = mCurrentFocusActor.GetHandle();
299 if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
301 // If the actor has been removed from the stage, then it should not be focused
303 mCurrentFocusActor.Reset();
308 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
312 for(index = 0; index < mCurrentFocusActors.size(); index++)
314 if(mCurrentFocusActors[index].first == mCurrentFocusedWindow)
316 actor = mCurrentFocusActors[index].second.GetHandle();
321 if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
323 // If the actor has been removed from the window, then the window doesn't have any focused actor
325 mCurrentFocusActors.erase(mCurrentFocusActors.begin() + index);
331 Actor KeyboardFocusManager::GetCurrentFocusGroup()
333 return GetFocusGroup(GetCurrentFocusActor());
336 void KeyboardFocusManager::MoveFocusBackward()
338 // Find Pre Focused Actor when the list size is more than 1
339 if(mFocusHistory.size() > 1)
341 // Delete current focused actor in history
342 mFocusHistory.pop_back();
344 // If pre-focused actors are not on stage or deleted, remove them in stack
345 while(mFocusHistory.size() > 0)
347 // Get pre focused actor
348 Actor target = mFocusHistory[mFocusHistory.size() - 1].GetHandle();
350 // Impl of Actor is not null
351 if(target && target.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
353 // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
354 mFocusHistory.pop_back();
355 SetCurrentFocusActor(target);
360 // Target is empty handle or off stage. Erase from queue
361 mFocusHistory.pop_back();
365 // if there is no actor which can get focus, then push current focus actor in stack again
366 if(mFocusHistory.size() == 0)
368 Actor currentFocusedActor = GetCurrentFocusActor();
369 mFocusHistory.push_back(currentFocusedActor);
374 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
376 Toolkit::Control control = Toolkit::Control::DownCast(actor);
377 return control && GetImplementation(control).IsKeyboardNavigationSupported();
380 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
382 // Get the actor's parent layout control that supports two dimensional keyboard navigation
387 Integration::SceneHolder window = Integration::SceneHolder::Get(actor);
390 rootActor = window.GetRootLayer();
393 parent = actor.GetParent();
396 while(parent && !IsLayoutControl(parent) && parent != rootActor)
398 parent = parent.GetParent();
401 return Toolkit::Control::DownCast(parent);
404 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
406 Actor currentFocusActor = GetCurrentFocusActor();
408 bool succeed = false;
410 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
411 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
412 while(parentLayoutControl && !succeed)
414 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
415 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
420 Actor nextFocusableActor;
422 Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
424 // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
425 if(currentFocusControl)
428 Property::Index index = Property::INVALID_INDEX;
429 Property::Value value;
431 // Find property index based upon focus direction
434 case Toolkit::Control::KeyboardFocus::LEFT:
436 index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
439 case Toolkit::Control::KeyboardFocus::RIGHT:
441 index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
444 case Toolkit::Control::KeyboardFocus::UP:
446 index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
449 case Toolkit::Control::KeyboardFocus::DOWN:
451 index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
458 // If the focusable property is set then determine next focusable actor
459 if(index != Property::INVALID_INDEX)
461 value = currentFocusActor.GetProperty(index);
462 actorId = value.Get<int>();
464 // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
467 if(currentFocusActor.GetParent())
469 nextFocusableActor = currentFocusActor.GetParent().FindChildById(actorId);
472 if(!nextFocusableActor)
474 Integration::SceneHolder window = Integration::SceneHolder::Get(currentFocusActor);
477 nextFocusableActor = window.GetRootLayer().FindChildById(actorId);
484 if(!nextFocusableActor)
486 // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
487 if(mCustomAlgorithmInterface)
489 mIsWaitingKeyboardFocusChangeCommit = true;
490 nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor(currentFocusActor, Actor(), direction);
491 mIsWaitingKeyboardFocusChangeCommit = false;
493 else if(!mPreFocusChangeSignal.Empty())
495 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
496 mIsWaitingKeyboardFocusChangeCommit = true;
497 nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
498 mIsWaitingKeyboardFocusChangeCommit = false;
502 // We should find it among the actors nearby.
503 nextFocusableActor = Toolkit::FocusFinder::GetNearestFocusableActor(currentFocusActor, direction);
507 if(nextFocusableActor && nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
509 // Whether the next focusable actor is a layout control
510 if(IsLayoutControl(nextFocusableActor))
512 // If so, move the focus inside it.
513 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
514 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
518 // Otherwise, just set focus to the next focusable actor
519 succeed = SetCurrentFocusActor(nextFocusableActor);
527 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
529 // Ask the control for the next actor to focus
530 Actor nextFocusableActor = GetImplementation(control).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
531 if(nextFocusableActor)
533 if(!nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
535 // If the actor is not focusable, ask the same layout control for the next actor to focus
536 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
540 Actor currentFocusActor = GetCurrentFocusActor();
541 Actor committedFocusActor = nextFocusableActor;
543 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
544 // Signal handler can check the proposed actor and return a different actor if it wishes.
545 if(!mPreFocusChangeSignal.Empty())
547 mIsWaitingKeyboardFocusChangeCommit = true;
548 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
549 mIsWaitingKeyboardFocusChangeCommit = false;
552 if(committedFocusActor && committedFocusActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
554 // Whether the commited focusable actor is a layout control
555 if(IsLayoutControl(committedFocusActor))
557 // If so, move the focus inside it.
558 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
559 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
563 // Otherwise, just set focus to the next focusable actor
564 if(committedFocusActor == nextFocusableActor)
566 // If the application hasn't changed our proposed actor, we informs the layout control we will
567 // move the focus to what the control returns. The control might wish to perform some actions
568 // before the focus is actually moved.
569 GetImplementation(control).OnKeyboardFocusChangeCommitted(committedFocusActor);
572 return SetCurrentFocusActor(committedFocusActor);
583 // No more actor can be focused in the given direction within the same layout control.
588 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
590 bool succeed = false;
592 // Get the parent layout control of the current focus group
593 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
595 while(parentLayoutControl && !succeed)
597 // If the current focus group has a parent layout control, we can probably automatically
598 // move the focus to the next focus group in the forward or backward direction.
599 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
600 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
601 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
604 if(!mFocusGroupChangedSignal.Empty())
606 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
607 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
613 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
617 Toolkit::Control control = Toolkit::Control::DownCast(actor);
620 // Notify the control that enter has been pressed on it.
621 GetImplementation(control).KeyboardEnter();
624 // Send a notification for the actor.
625 if(!mFocusedActorEnterKeySignal.Empty())
627 mFocusedActorEnterKeySignal.Emit(actor);
632 void KeyboardFocusManager::ClearFocus()
634 Actor actor = GetCurrentFocusActor();
637 if(mFocusIndicatorActor)
639 actor.Remove(mFocusIndicatorActor);
642 // Send notification for the change of focus actor
643 if(!mFocusChangedSignal.Empty())
645 mFocusChangedSignal.Emit(actor, Actor());
648 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(actor);
649 if(currentlyFocusedControl)
651 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
652 currentlyFocusedControl.ClearKeyInputFocus();
656 mCurrentFocusActor.Reset();
657 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
660 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
662 mFocusGroupLoopEnabled = enabled;
665 bool KeyboardFocusManager::GetFocusGroupLoop() const
667 return mFocusGroupLoopEnabled;
670 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
674 // Create/Set focus group property.
675 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE);
679 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
681 // Check whether the actor is a focus group
682 bool isFocusGroup = false;
686 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
687 if(propertyIsFocusGroup != Property::INVALID_INDEX)
689 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
696 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
698 // Go through the actor's hierarchy to check which focus group the actor belongs to
699 while(actor && !IsFocusGroup(actor))
701 actor = actor.GetParent();
707 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
709 if(mFocusIndicatorActor != indicator)
711 Actor currentFocusActor = GetCurrentFocusActor();
712 if(currentFocusActor)
714 // The new focus indicator should be added to the current focused actor immediately
715 if(mFocusIndicatorActor)
717 currentFocusActor.Remove(mFocusIndicatorActor);
722 currentFocusActor.Add(indicator);
726 mFocusIndicatorActor = indicator;
730 Actor KeyboardFocusManager::GetFocusIndicatorActor()
732 if(!mFocusIndicatorActor)
734 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
735 const std::string imageDirPath = AssetManager::GetDaliImagePath();
736 mFocusIndicatorActor = Toolkit::ImageView::New(imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME);
738 // Apply size constraint to the focus indicator
739 mFocusIndicatorActor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
742 mFocusIndicatorActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
743 mFocusIndicatorActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
744 mFocusIndicatorActor.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
746 return mFocusIndicatorActor;
749 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
751 std::string keyName = event.GetKeyName();
753 if(mIsFocusIndicatorShown == UNKNOWN)
755 GetConfigurationFromStyleManger();
758 bool isFocusStartableKey = false;
760 if(event.GetState() == KeyEvent::DOWN)
762 if(keyName == "Left")
764 if(!mIsFocusIndicatorShown)
766 if(mIsFocusIndicatorShown == HIDE)
768 // Show focus indicator
769 mIsFocusIndicatorShown = SHOW;
773 // Move the focus towards left
774 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
777 isFocusStartableKey = true;
781 // Move the focus towards left
782 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
785 isFocusStartableKey = true;
787 else if(keyName == "Right")
789 if(!mIsFocusIndicatorShown)
791 if(mIsFocusIndicatorShown == HIDE)
793 // Show focus indicator
794 mIsFocusIndicatorShown = SHOW;
798 // Move the focus towards right
799 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
804 // Move the focus towards right
805 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
808 isFocusStartableKey = true;
810 else if(keyName == "Up")
812 if(mIsFocusIndicatorShown == HIDE)
814 // Show focus indicator
815 mIsFocusIndicatorShown = SHOW;
819 // Move the focus towards up
820 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
823 isFocusStartableKey = true;
825 else if(keyName == "Down")
827 if(mIsFocusIndicatorShown == HIDE)
829 // Show focus indicator
830 mIsFocusIndicatorShown = SHOW;
834 // Move the focus towards down
835 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
838 isFocusStartableKey = true;
840 else if(keyName == "Prior")
842 if(mIsFocusIndicatorShown == HIDE)
844 // Show focus indicator
845 mIsFocusIndicatorShown = SHOW;
849 // Move the focus towards the previous page
850 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
853 isFocusStartableKey = true;
855 else if(keyName == "Next")
857 if(mIsFocusIndicatorShown == HIDE)
859 // Show focus indicator
860 mIsFocusIndicatorShown = SHOW;
864 // Move the focus towards the next page
865 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
868 isFocusStartableKey = true;
870 else if(keyName == "Tab")
872 if(mIsFocusIndicatorShown == HIDE)
874 // Show focus indicator
875 mIsFocusIndicatorShown = SHOW;
879 // "Tab" key changes the focus group in the forward direction and
880 // "Shift-Tab" key changes it in the backward direction.
881 if(!DoMoveFocusToNextFocusGroup(!event.IsShiftModifier()))
883 // If the focus group is not changed, Move the focus towards right, "Shift-Tap" key moves the focus towards left.
884 if(!MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::LEFT : Toolkit::Control::KeyboardFocus::RIGHT))
886 // If the focus is not moved, Move the focus towards down, "Shift-Tap" key moves the focus towards up.
887 MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::UP : Toolkit::Control::KeyboardFocus::DOWN);
892 isFocusStartableKey = true;
894 else if(keyName == "space")
896 if(mIsFocusIndicatorShown == HIDE)
898 // Show focus indicator
899 mIsFocusIndicatorShown = SHOW;
902 isFocusStartableKey = true;
904 else if(keyName == "")
906 // Check the fake key event for evas-plugin case
907 if(mIsFocusIndicatorShown == HIDE)
909 // Show focus indicator
910 mIsFocusIndicatorShown = SHOW;
913 isFocusStartableKey = true;
915 else if(keyName == "Backspace")
917 // Emit signal to go back to the previous view???
919 else if(keyName == "Escape")
923 else if(event.GetState() == KeyEvent::UP)
925 if(keyName == "Return")
927 if(mIsFocusIndicatorShown == HIDE)
929 // Show focus indicator
930 mIsFocusIndicatorShown = SHOW;
934 // The focused actor has enter pressed on it
935 Actor actor = GetCurrentFocusActor();
938 DoKeyboardEnter(actor);
942 isFocusStartableKey = true;
946 if(isFocusStartableKey && mIsFocusIndicatorShown == SHOW)
948 Actor actor = GetCurrentFocusActor();
951 if(mEnableFocusIndicator == ENABLE)
953 // Make sure the focused actor is highlighted
954 actor.Add(GetFocusIndicatorActor());
959 // No actor is focused but keyboard focus is activated by the key press
960 // Let's try to move the initial focus
961 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
966 void KeyboardFocusManager::OnTouch(const TouchEvent& touch)
968 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
969 // Try to load configuration.
970 if(mIsFocusIndicatorShown == UNKNOWN)
972 GetConfigurationFromStyleManger();
975 // Clear the focus when user touch the screen.
976 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
977 if(((touch.GetPointCount() < 1) || (touch.GetState(0) == PointState::DOWN)))
979 // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
980 if(mClearFocusOnTouch)
985 // If KEYBOARD_FOCUSABLE and TOUCH_FOCUSABLE is true, set focus actor
986 Actor hitActor = touch.GetHitActor(0);
987 if(hitActor && hitActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && hitActor.GetProperty<bool>(DevelActor::Property::TOUCH_FOCUSABLE))
989 SetCurrentFocusActor(hitActor);
994 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn)
996 if(focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer())
998 // Change Current Focused Window
999 Layer rootLayer = window.GetRootLayer();
1000 mCurrentFocusedWindow = rootLayer;
1002 // Get Current Focused Actor from window
1003 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
1004 SetCurrentFocusActor(currentFocusedActor);
1006 if(currentFocusedActor && (mEnableFocusIndicator == ENABLE))
1008 // Make sure the focused actor is highlighted
1009 currentFocusedActor.Add(GetFocusIndicatorActor());
1010 mIsFocusIndicatorShown = SHOW;
1015 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
1017 return mPreFocusChangeSignal;
1020 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1022 return mFocusChangedSignal;
1025 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1027 return mFocusGroupChangedSignal;
1030 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1032 return mFocusedActorEnterKeySignal;
1035 bool KeyboardFocusManager::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
1037 Dali::BaseHandle handle(object);
1039 bool connected(true);
1040 KeyboardFocusManager* manager = static_cast<KeyboardFocusManager*>(object); // TypeRegistry guarantees that this is the correct type.
1042 if(0 == strcmp(signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE))
1044 manager->PreFocusChangeSignal().Connect(tracker, functor);
1046 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_CHANGED))
1048 manager->FocusChangedSignal().Connect(tracker, functor);
1050 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED))
1052 manager->FocusGroupChangedSignal().Connect(tracker, functor);
1054 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY))
1056 manager->FocusedActorEnterKeySignal().Connect(tracker, functor);
1060 // signalName does not match any signal
1067 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1069 mCustomAlgorithmInterface = &interface;
1072 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1074 if(!enable && mFocusIndicatorActor)
1076 mFocusIndicatorActor.Unparent();
1079 mEnableFocusIndicator = enable ? ENABLE : DISABLE;
1082 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1084 return (mEnableFocusIndicator == ENABLE);
1087 } // namespace Internal
1089 } // namespace Toolkit