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),
131 mEnableDefaultAlgorithm(false)
133 // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorShown.
135 LifecycleController::Get().InitSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnAdaptorInit);
138 void KeyboardFocusManager::OnAdaptorInit()
140 if(Adaptor::IsAvailable())
142 // Retrieve all the existing scene holders
143 Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
144 for(auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter)
146 (*iter).KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
147 (*iter).TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
148 (*iter).WheelEventGeneratedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWheelEvent);
149 Dali::Window window = DevelWindow::DownCast(*iter);
152 window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
156 // Get notified when any new scene holder is created afterwards
157 Adaptor::Get().WindowCreatedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnSceneHolderCreated);
161 void KeyboardFocusManager::OnSceneHolderCreated(Dali::Integration::SceneHolder& sceneHolder)
163 sceneHolder.KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
164 sceneHolder.TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
165 sceneHolder.WheelEventGeneratedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWheelEvent);
166 Dali::Window window = DevelWindow::DownCast(sceneHolder);
169 window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
173 KeyboardFocusManager::~KeyboardFocusManager()
177 void KeyboardFocusManager::GetConfigurationFromStyleManger()
179 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
182 Property::Map config = Toolkit::DevelStyleManager::GetConfigurations(styleManager);
183 mAlwaysShowIndicator = config["alwaysShowFocus"].Get<bool>() ? ALWAYS_SHOW : NONE;
184 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
185 mClearFocusOnTouch = (mIsFocusIndicatorShown == SHOW) ? false : true;
189 bool KeyboardFocusManager::SetCurrentFocusActor(Actor actor)
191 DALI_ASSERT_DEBUG(!mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?");
193 if(mIsFocusIndicatorShown == UNKNOWN)
195 GetConfigurationFromStyleManger();
198 return DoSetCurrentFocusActor(actor);
201 bool KeyboardFocusManager::DoSetCurrentFocusActor(Actor actor)
203 bool success = false;
205 // If the parent's KEYBOARD_FOCUSABLE_CHILDREN is false, it cannot have focus.
208 Actor parent = actor.GetParent();
211 if(!parent.GetProperty<bool>(DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN))
213 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] Parent Actor has KEYBOARD_FOCUSABLE_CHILDREN false,\n", __FUNCTION__, __LINE__);
216 parent = parent.GetParent();
220 if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
222 Integration::SceneHolder currentWindow = Integration::SceneHolder::Get(actor);
224 if(currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
226 Layer rootLayer = currentWindow.GetRootLayer();
227 mCurrentFocusedWindow = rootLayer;
231 Actor currentFocusedActor = GetCurrentFocusActor();
233 // If developer set focus on same actor, doing nothing
234 if(actor == currentFocusedActor)
243 // Check whether the actor is in the stage and is keyboard focusable.
244 if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
246 if((mIsFocusIndicatorShown == SHOW) && (mEnableFocusIndicator == ENABLE))
248 actor.Add(GetFocusIndicatorActor());
251 // Send notification for the change of focus actor
252 if(!mFocusChangedSignal.Empty())
254 mFocusChangedSignal.Emit(currentFocusedActor, actor);
257 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
258 if(currentlyFocusedControl)
260 // Do we need it to remember if it was previously DISABLED?
261 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
262 currentlyFocusedControl.ClearKeyInputFocus();
265 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
267 // Save the current focused actor
268 mCurrentFocusActor = actor;
270 bool focusedWindowFound = false;
271 for(unsigned int i = 0; i < mCurrentFocusActors.size(); i++)
273 if(mCurrentFocusActors[i].first == mCurrentFocusedWindow)
275 mCurrentFocusActors[i].second = actor;
276 focusedWindowFound = true;
280 if(!focusedWindowFound)
282 // A new window gains the focus, so store the focused actor in that window.
283 mCurrentFocusActors.push_back(std::pair<WeakHandle<Layer>, WeakHandle<Actor> >(mCurrentFocusedWindow, actor));
286 Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
287 if(newlyFocusedControl)
289 newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED);
290 newlyFocusedControl.SetKeyInputFocus();
293 // Push Current Focused Actor to FocusHistory
294 mFocusHistory.push_back(actor);
296 // Delete first element before add new element when Stack is full.
297 if(mFocusHistory.size() > MAX_HISTORY_AMOUNT)
299 FocusStackIterator beginPos = mFocusHistory.begin();
300 mFocusHistory.erase(beginPos);
303 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
308 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
314 Actor KeyboardFocusManager::GetCurrentFocusActor()
316 Actor actor = mCurrentFocusActor.GetHandle();
318 if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
320 // If the actor has been removed from the stage, then it should not be focused
322 mCurrentFocusActor.Reset();
327 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
331 for(index = 0; index < mCurrentFocusActors.size(); index++)
333 if(mCurrentFocusActors[index].first == mCurrentFocusedWindow)
335 actor = mCurrentFocusActors[index].second.GetHandle();
340 if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
342 // If the actor has been removed from the window, then the window doesn't have any focused actor
344 mCurrentFocusActors.erase(mCurrentFocusActors.begin() + index);
350 Actor KeyboardFocusManager::GetCurrentFocusGroup()
352 return GetFocusGroup(GetCurrentFocusActor());
355 void KeyboardFocusManager::MoveFocusBackward()
357 // Find Pre Focused Actor when the list size is more than 1
358 if(mFocusHistory.size() > 1)
360 // Delete current focused actor in history
361 mFocusHistory.pop_back();
363 // If pre-focused actors are not on stage or deleted, remove them in stack
364 while(mFocusHistory.size() > 0)
366 // Get pre focused actor
367 Actor target = mFocusHistory[mFocusHistory.size() - 1].GetHandle();
369 // Impl of Actor is not null
370 if(target && target.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
372 // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
373 mFocusHistory.pop_back();
374 SetCurrentFocusActor(target);
379 // Target is empty handle or off stage. Erase from queue
380 mFocusHistory.pop_back();
384 // if there is no actor which can get focus, then push current focus actor in stack again
385 if(mFocusHistory.size() == 0)
387 Actor currentFocusedActor = GetCurrentFocusActor();
388 mFocusHistory.push_back(currentFocusedActor);
393 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
395 Toolkit::Control control = Toolkit::Control::DownCast(actor);
396 return control && GetImplementation(control).IsKeyboardNavigationSupported();
399 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
401 // Get the actor's parent layout control that supports two dimensional keyboard navigation
406 Integration::SceneHolder window = Integration::SceneHolder::Get(actor);
409 rootActor = window.GetRootLayer();
412 parent = actor.GetParent();
415 while(parent && !IsLayoutControl(parent) && parent != rootActor)
417 parent = parent.GetParent();
420 return Toolkit::Control::DownCast(parent);
423 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
425 Actor currentFocusActor = GetCurrentFocusActor();
427 bool succeed = false;
429 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
430 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
431 while(parentLayoutControl && !succeed)
433 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
434 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
439 Actor nextFocusableActor;
441 Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
443 // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
444 if(currentFocusControl)
447 Property::Index index = Property::INVALID_INDEX;
448 Property::Value value;
450 // Find property index based upon focus direction
453 case Toolkit::Control::KeyboardFocus::LEFT:
455 index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
458 case Toolkit::Control::KeyboardFocus::RIGHT:
460 index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
463 case Toolkit::Control::KeyboardFocus::UP:
465 index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
468 case Toolkit::Control::KeyboardFocus::DOWN:
470 index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
477 // If the focusable property is set then determine next focusable actor
478 if(index != Property::INVALID_INDEX)
480 value = currentFocusActor.GetProperty(index);
481 actorId = value.Get<int>();
483 // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
486 if(currentFocusActor.GetParent())
488 nextFocusableActor = currentFocusActor.GetParent().FindChildById(actorId);
491 if(!nextFocusableActor)
493 Integration::SceneHolder window = Integration::SceneHolder::Get(currentFocusActor);
496 nextFocusableActor = window.GetRootLayer().FindChildById(actorId);
503 if(!nextFocusableActor)
505 // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
506 if(mCustomAlgorithmInterface)
508 mIsWaitingKeyboardFocusChangeCommit = true;
509 nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor(currentFocusActor, Actor(), direction);
510 mIsWaitingKeyboardFocusChangeCommit = false;
512 else if(!mPreFocusChangeSignal.Empty())
514 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
515 mIsWaitingKeyboardFocusChangeCommit = true;
516 nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
517 mIsWaitingKeyboardFocusChangeCommit = false;
519 else if (mEnableDefaultAlgorithm)
522 if (currentFocusActor)
524 // Find the window of the focused actor.
525 Integration::SceneHolder window = Integration::SceneHolder::Get(currentFocusActor);
528 rootLayer = window.GetRootLayer();
533 // Searches from the currently focused window.
534 rootLayer = mCurrentFocusedWindow.GetHandle();
538 // We should find it among the actors nearby.
539 nextFocusableActor = Toolkit::FocusFinder::GetNearestFocusableActor(rootLayer, currentFocusActor, direction);
544 if(nextFocusableActor && nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
546 // Whether the next focusable actor is a layout control
547 if(IsLayoutControl(nextFocusableActor))
549 // If so, move the focus inside it.
550 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
551 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
555 // Otherwise, just set focus to the next focusable actor
556 succeed = SetCurrentFocusActor(nextFocusableActor);
564 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
566 // Ask the control for the next actor to focus
567 Actor nextFocusableActor = GetImplementation(control).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
568 if(nextFocusableActor)
570 if(!nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
572 // If the actor is not focusable, ask the same layout control for the next actor to focus
573 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
577 Actor currentFocusActor = GetCurrentFocusActor();
578 Actor committedFocusActor = nextFocusableActor;
580 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
581 // Signal handler can check the proposed actor and return a different actor if it wishes.
582 if(!mPreFocusChangeSignal.Empty())
584 mIsWaitingKeyboardFocusChangeCommit = true;
585 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
586 mIsWaitingKeyboardFocusChangeCommit = false;
589 if(committedFocusActor && committedFocusActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
591 // Whether the commited focusable actor is a layout control
592 if(IsLayoutControl(committedFocusActor))
594 // If so, move the focus inside it.
595 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
596 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
600 // Otherwise, just set focus to the next focusable actor
601 if(committedFocusActor == nextFocusableActor)
603 // If the application hasn't changed our proposed actor, we informs the layout control we will
604 // move the focus to what the control returns. The control might wish to perform some actions
605 // before the focus is actually moved.
606 GetImplementation(control).OnKeyboardFocusChangeCommitted(committedFocusActor);
609 return SetCurrentFocusActor(committedFocusActor);
620 // No more actor can be focused in the given direction within the same layout control.
625 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
627 bool succeed = false;
629 // Get the parent layout control of the current focus group
630 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
632 while(parentLayoutControl && !succeed)
634 // If the current focus group has a parent layout control, we can probably automatically
635 // move the focus to the next focus group in the forward or backward direction.
636 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
637 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
638 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
641 if(!mFocusGroupChangedSignal.Empty())
643 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
644 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
650 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
654 Toolkit::Control control = Toolkit::Control::DownCast(actor);
657 // Notify the control that enter has been pressed on it.
658 GetImplementation(control).KeyboardEnter();
661 // Send a notification for the actor.
662 if(!mFocusedActorEnterKeySignal.Empty())
664 mFocusedActorEnterKeySignal.Emit(actor);
669 void KeyboardFocusManager::ClearFocus()
671 Actor actor = GetCurrentFocusActor();
674 if(mFocusIndicatorActor)
676 actor.Remove(mFocusIndicatorActor);
679 // Send notification for the change of focus actor
680 if(!mFocusChangedSignal.Empty())
682 mFocusChangedSignal.Emit(actor, Actor());
685 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(actor);
686 if(currentlyFocusedControl)
688 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
689 currentlyFocusedControl.ClearKeyInputFocus();
693 mCurrentFocusActor.Reset();
694 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
697 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
699 mFocusGroupLoopEnabled = enabled;
702 bool KeyboardFocusManager::GetFocusGroupLoop() const
704 return mFocusGroupLoopEnabled;
707 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
711 // Create/Set focus group property.
712 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE);
716 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
718 // Check whether the actor is a focus group
719 bool isFocusGroup = false;
723 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
724 if(propertyIsFocusGroup != Property::INVALID_INDEX)
726 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
733 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
735 // Go through the actor's hierarchy to check which focus group the actor belongs to
736 while(actor && !IsFocusGroup(actor))
738 actor = actor.GetParent();
744 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
746 if(mFocusIndicatorActor != indicator)
748 Actor currentFocusActor = GetCurrentFocusActor();
749 if(currentFocusActor)
751 // The new focus indicator should be added to the current focused actor immediately
752 if(mFocusIndicatorActor)
754 currentFocusActor.Remove(mFocusIndicatorActor);
759 currentFocusActor.Add(indicator);
763 mFocusIndicatorActor = indicator;
767 Actor KeyboardFocusManager::GetFocusIndicatorActor()
769 if(!mFocusIndicatorActor)
771 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
772 const std::string imageDirPath = AssetManager::GetDaliImagePath();
773 mFocusIndicatorActor = Toolkit::ImageView::New(imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME);
775 // Apply size constraint to the focus indicator
776 mFocusIndicatorActor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
779 mFocusIndicatorActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
780 mFocusIndicatorActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
781 mFocusIndicatorActor.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
783 return mFocusIndicatorActor;
786 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
788 std::string keyName = event.GetKeyName();
790 if(mIsFocusIndicatorShown == UNKNOWN)
792 GetConfigurationFromStyleManger();
795 bool isFocusStartableKey = false;
797 if(event.GetState() == KeyEvent::DOWN)
799 if(keyName == "Left")
801 if(mIsFocusIndicatorShown == HIDE)
803 // Show focus indicator
804 mIsFocusIndicatorShown = SHOW;
808 // Move the focus towards left
809 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
812 isFocusStartableKey = true;
814 else if(keyName == "Right")
816 if(mIsFocusIndicatorShown == HIDE)
818 // Show focus indicator
819 mIsFocusIndicatorShown = SHOW;
823 // Move the focus towards right
824 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
827 isFocusStartableKey = true;
829 else if(keyName == "Up")
831 if(mIsFocusIndicatorShown == HIDE)
833 // Show focus indicator
834 mIsFocusIndicatorShown = SHOW;
838 // Move the focus towards up
839 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
842 isFocusStartableKey = true;
844 else if(keyName == "Down")
846 if(mIsFocusIndicatorShown == HIDE)
848 // Show focus indicator
849 mIsFocusIndicatorShown = SHOW;
853 // Move the focus towards down
854 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
857 isFocusStartableKey = true;
859 else if(keyName == "Prior")
861 if(mIsFocusIndicatorShown == HIDE)
863 // Show focus indicator
864 mIsFocusIndicatorShown = SHOW;
868 // Move the focus towards the previous page
869 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
872 isFocusStartableKey = true;
874 else if(keyName == "Next")
876 if(mIsFocusIndicatorShown == HIDE)
878 // Show focus indicator
879 mIsFocusIndicatorShown = SHOW;
883 // Move the focus towards the next page
884 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
887 isFocusStartableKey = true;
889 else if(keyName == "Tab")
891 if(mIsFocusIndicatorShown == HIDE)
893 // Show focus indicator
894 mIsFocusIndicatorShown = SHOW;
898 // "Tab" key changes the focus group in the forward direction and
899 // "Shift-Tab" key changes it in the backward direction.
900 if(!DoMoveFocusToNextFocusGroup(!event.IsShiftModifier()))
902 // If the focus group is not changed, Move the focus towards forward, "Shift-Tap" key moves the focus towards backward.
903 MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::BACKWARD : Toolkit::Control::KeyboardFocus::FORWARD);
907 isFocusStartableKey = true;
909 else if(keyName == "space")
911 if(mIsFocusIndicatorShown == HIDE)
913 // Show focus indicator
914 mIsFocusIndicatorShown = SHOW;
917 isFocusStartableKey = true;
919 else if(keyName == "")
921 // Check the fake key event for evas-plugin case
922 if(mIsFocusIndicatorShown == HIDE)
924 // Show focus indicator
925 mIsFocusIndicatorShown = SHOW;
928 isFocusStartableKey = true;
930 else if(keyName == "Backspace")
932 // Emit signal to go back to the previous view???
934 else if(keyName == "Escape")
938 else if(event.GetState() == KeyEvent::UP)
940 if(keyName == "Return")
942 if(mIsFocusIndicatorShown == HIDE)
944 // Show focus indicator
945 mIsFocusIndicatorShown = SHOW;
949 // The focused actor has enter pressed on it
950 Actor actor = GetCurrentFocusActor();
953 DoKeyboardEnter(actor);
957 isFocusStartableKey = true;
961 if(isFocusStartableKey && mIsFocusIndicatorShown == SHOW)
963 Actor actor = GetCurrentFocusActor();
966 if(mEnableFocusIndicator == ENABLE)
968 // Make sure the focused actor is highlighted
969 actor.Add(GetFocusIndicatorActor());
974 // No actor is focused but keyboard focus is activated by the key press
975 // Let's try to move the initial focus
976 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
981 void KeyboardFocusManager::OnTouch(const TouchEvent& touch)
983 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
984 // Try to load configuration.
985 if(mIsFocusIndicatorShown == UNKNOWN)
987 GetConfigurationFromStyleManger();
990 // Clear the focus when user touch the screen.
991 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
992 if(((touch.GetPointCount() < 1) || (touch.GetState(0) == PointState::DOWN)))
994 // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
995 if(mClearFocusOnTouch)
1000 // If KEYBOARD_FOCUSABLE and TOUCH_FOCUSABLE is true, set focus actor
1001 Actor hitActor = touch.GetHitActor(0);
1002 if(hitActor && hitActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && hitActor.GetProperty<bool>(DevelActor::Property::TOUCH_FOCUSABLE))
1004 SetCurrentFocusActor(hitActor);
1009 bool KeyboardFocusManager::OnWheelEvent(const WheelEvent& event)
1011 bool consumed = false;
1012 Actor actor = GetCurrentFocusActor();
1015 // Notify the actor about the wheel event
1016 consumed = EmitWheelSignals(actor, event);
1021 bool KeyboardFocusManager::EmitWheelSignals(Actor actor, const WheelEvent& event)
1023 bool consumed = false;
1027 Dali::Actor oldParent(actor.GetParent());
1029 // Only do the conversion and emit the signal if the actor's wheel signal has connections.
1030 if(!actor.WheelEventSignal().Empty())
1032 // Emit the signal to the parent
1033 consumed = actor.WheelEventSignal().Emit(actor, event);
1035 // if actor doesn't consume WheelEvent, give WheelEvent to its parent.
1038 // The actor may have been removed/reparented during the signal callbacks.
1039 Dali::Actor parent = actor.GetParent();
1042 (parent == oldParent))
1044 consumed = EmitWheelSignals(parent, event);
1052 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn)
1054 if(focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer())
1056 // Change Current Focused Window
1057 Layer rootLayer = window.GetRootLayer();
1058 mCurrentFocusedWindow = rootLayer;
1060 // Get Current Focused Actor from window
1061 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
1062 SetCurrentFocusActor(currentFocusedActor);
1064 if(currentFocusedActor && (mEnableFocusIndicator == ENABLE))
1066 // Make sure the focused actor is highlighted
1067 currentFocusedActor.Add(GetFocusIndicatorActor());
1068 mIsFocusIndicatorShown = SHOW;
1073 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
1075 return mPreFocusChangeSignal;
1078 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1080 return mFocusChangedSignal;
1083 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1085 return mFocusGroupChangedSignal;
1088 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1090 return mFocusedActorEnterKeySignal;
1093 bool KeyboardFocusManager::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
1095 Dali::BaseHandle handle(object);
1097 bool connected(true);
1098 KeyboardFocusManager* manager = static_cast<KeyboardFocusManager*>(object); // TypeRegistry guarantees that this is the correct type.
1100 if(0 == strcmp(signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE))
1102 manager->PreFocusChangeSignal().Connect(tracker, functor);
1104 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_CHANGED))
1106 manager->FocusChangedSignal().Connect(tracker, functor);
1108 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED))
1110 manager->FocusGroupChangedSignal().Connect(tracker, functor);
1112 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY))
1114 manager->FocusedActorEnterKeySignal().Connect(tracker, functor);
1118 // signalName does not match any signal
1125 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1127 mCustomAlgorithmInterface = &interface;
1130 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1132 if(!enable && mFocusIndicatorActor)
1134 mFocusIndicatorActor.Unparent();
1137 mEnableFocusIndicator = enable ? ENABLE : DISABLE;
1140 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1142 return (mEnableFocusIndicator == ENABLE);
1145 void KeyboardFocusManager::EnableDefaultAlgorithm(bool enable)
1147 mEnableDefaultAlgorithm = enable;
1150 bool KeyboardFocusManager::IsDefaultAlgorithmEnabled() const
1152 return mEnableDefaultAlgorithm;
1155 } // namespace Internal
1157 } // namespace Toolkit