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 && currentFocusActor)
521 // We should find it among the actors nearby.
522 Integration::SceneHolder window = Integration::SceneHolder::Get(currentFocusActor);
525 nextFocusableActor = Toolkit::FocusFinder::GetNearestFocusableActor(window.GetRootLayer(), currentFocusActor, direction);
530 if(nextFocusableActor && nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
532 // Whether the next focusable actor is a layout control
533 if(IsLayoutControl(nextFocusableActor))
535 // If so, move the focus inside it.
536 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
537 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
541 // Otherwise, just set focus to the next focusable actor
542 succeed = SetCurrentFocusActor(nextFocusableActor);
550 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
552 // Ask the control for the next actor to focus
553 Actor nextFocusableActor = GetImplementation(control).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
554 if(nextFocusableActor)
556 if(!nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
558 // If the actor is not focusable, ask the same layout control for the next actor to focus
559 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
563 Actor currentFocusActor = GetCurrentFocusActor();
564 Actor committedFocusActor = nextFocusableActor;
566 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
567 // Signal handler can check the proposed actor and return a different actor if it wishes.
568 if(!mPreFocusChangeSignal.Empty())
570 mIsWaitingKeyboardFocusChangeCommit = true;
571 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
572 mIsWaitingKeyboardFocusChangeCommit = false;
575 if(committedFocusActor && committedFocusActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
577 // Whether the commited focusable actor is a layout control
578 if(IsLayoutControl(committedFocusActor))
580 // If so, move the focus inside it.
581 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
582 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
586 // Otherwise, just set focus to the next focusable actor
587 if(committedFocusActor == nextFocusableActor)
589 // If the application hasn't changed our proposed actor, we informs the layout control we will
590 // move the focus to what the control returns. The control might wish to perform some actions
591 // before the focus is actually moved.
592 GetImplementation(control).OnKeyboardFocusChangeCommitted(committedFocusActor);
595 return SetCurrentFocusActor(committedFocusActor);
606 // No more actor can be focused in the given direction within the same layout control.
611 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
613 bool succeed = false;
615 // Get the parent layout control of the current focus group
616 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
618 while(parentLayoutControl && !succeed)
620 // If the current focus group has a parent layout control, we can probably automatically
621 // move the focus to the next focus group in the forward or backward direction.
622 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
623 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
624 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
627 if(!mFocusGroupChangedSignal.Empty())
629 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
630 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
636 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
640 Toolkit::Control control = Toolkit::Control::DownCast(actor);
643 // Notify the control that enter has been pressed on it.
644 GetImplementation(control).KeyboardEnter();
647 // Send a notification for the actor.
648 if(!mFocusedActorEnterKeySignal.Empty())
650 mFocusedActorEnterKeySignal.Emit(actor);
655 void KeyboardFocusManager::ClearFocus()
657 Actor actor = GetCurrentFocusActor();
660 if(mFocusIndicatorActor)
662 actor.Remove(mFocusIndicatorActor);
665 // Send notification for the change of focus actor
666 if(!mFocusChangedSignal.Empty())
668 mFocusChangedSignal.Emit(actor, Actor());
671 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(actor);
672 if(currentlyFocusedControl)
674 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
675 currentlyFocusedControl.ClearKeyInputFocus();
679 mCurrentFocusActor.Reset();
680 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
683 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
685 mFocusGroupLoopEnabled = enabled;
688 bool KeyboardFocusManager::GetFocusGroupLoop() const
690 return mFocusGroupLoopEnabled;
693 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
697 // Create/Set focus group property.
698 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE);
702 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
704 // Check whether the actor is a focus group
705 bool isFocusGroup = false;
709 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
710 if(propertyIsFocusGroup != Property::INVALID_INDEX)
712 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
719 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
721 // Go through the actor's hierarchy to check which focus group the actor belongs to
722 while(actor && !IsFocusGroup(actor))
724 actor = actor.GetParent();
730 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
732 if(mFocusIndicatorActor != indicator)
734 Actor currentFocusActor = GetCurrentFocusActor();
735 if(currentFocusActor)
737 // The new focus indicator should be added to the current focused actor immediately
738 if(mFocusIndicatorActor)
740 currentFocusActor.Remove(mFocusIndicatorActor);
745 currentFocusActor.Add(indicator);
749 mFocusIndicatorActor = indicator;
753 Actor KeyboardFocusManager::GetFocusIndicatorActor()
755 if(!mFocusIndicatorActor)
757 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
758 const std::string imageDirPath = AssetManager::GetDaliImagePath();
759 mFocusIndicatorActor = Toolkit::ImageView::New(imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME);
761 // Apply size constraint to the focus indicator
762 mFocusIndicatorActor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
765 mFocusIndicatorActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
766 mFocusIndicatorActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
767 mFocusIndicatorActor.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
769 return mFocusIndicatorActor;
772 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
774 std::string keyName = event.GetKeyName();
776 if(mIsFocusIndicatorShown == UNKNOWN)
778 GetConfigurationFromStyleManger();
781 bool isFocusStartableKey = false;
783 if(event.GetState() == KeyEvent::DOWN)
785 if(keyName == "Left")
787 if(mIsFocusIndicatorShown == HIDE)
789 // Show focus indicator
790 mIsFocusIndicatorShown = SHOW;
794 // Move the focus towards left
795 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
798 isFocusStartableKey = true;
800 else if(keyName == "Right")
802 if(mIsFocusIndicatorShown == HIDE)
804 // Show focus indicator
805 mIsFocusIndicatorShown = SHOW;
809 // Move the focus towards right
810 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
813 isFocusStartableKey = true;
815 else if(keyName == "Up")
817 if(mIsFocusIndicatorShown == HIDE)
819 // Show focus indicator
820 mIsFocusIndicatorShown = SHOW;
824 // Move the focus towards up
825 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
828 isFocusStartableKey = true;
830 else if(keyName == "Down")
832 if(mIsFocusIndicatorShown == HIDE)
834 // Show focus indicator
835 mIsFocusIndicatorShown = SHOW;
839 // Move the focus towards down
840 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
843 isFocusStartableKey = true;
845 else if(keyName == "Prior")
847 if(mIsFocusIndicatorShown == HIDE)
849 // Show focus indicator
850 mIsFocusIndicatorShown = SHOW;
854 // Move the focus towards the previous page
855 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
858 isFocusStartableKey = true;
860 else if(keyName == "Next")
862 if(mIsFocusIndicatorShown == HIDE)
864 // Show focus indicator
865 mIsFocusIndicatorShown = SHOW;
869 // Move the focus towards the next page
870 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
873 isFocusStartableKey = true;
875 else if(keyName == "Tab")
877 if(mIsFocusIndicatorShown == HIDE)
879 // Show focus indicator
880 mIsFocusIndicatorShown = SHOW;
884 // "Tab" key changes the focus group in the forward direction and
885 // "Shift-Tab" key changes it in the backward direction.
886 if(!DoMoveFocusToNextFocusGroup(!event.IsShiftModifier()))
888 // If the focus group is not changed, Move the focus towards forward, "Shift-Tap" key moves the focus towards backward.
889 MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::BACKWARD : Toolkit::Control::KeyboardFocus::FORWARD);
893 isFocusStartableKey = true;
895 else if(keyName == "space")
897 if(mIsFocusIndicatorShown == HIDE)
899 // Show focus indicator
900 mIsFocusIndicatorShown = SHOW;
903 isFocusStartableKey = true;
905 else if(keyName == "")
907 // Check the fake key event for evas-plugin case
908 if(mIsFocusIndicatorShown == HIDE)
910 // Show focus indicator
911 mIsFocusIndicatorShown = SHOW;
914 isFocusStartableKey = true;
916 else if(keyName == "Backspace")
918 // Emit signal to go back to the previous view???
920 else if(keyName == "Escape")
924 else if(event.GetState() == KeyEvent::UP)
926 if(keyName == "Return")
928 if(mIsFocusIndicatorShown == HIDE)
930 // Show focus indicator
931 mIsFocusIndicatorShown = SHOW;
935 // The focused actor has enter pressed on it
936 Actor actor = GetCurrentFocusActor();
939 DoKeyboardEnter(actor);
943 isFocusStartableKey = true;
947 if(isFocusStartableKey && mIsFocusIndicatorShown == SHOW)
949 Actor actor = GetCurrentFocusActor();
952 if(mEnableFocusIndicator == ENABLE)
954 // Make sure the focused actor is highlighted
955 actor.Add(GetFocusIndicatorActor());
960 // No actor is focused but keyboard focus is activated by the key press
961 // Let's try to move the initial focus
962 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
967 void KeyboardFocusManager::OnTouch(const TouchEvent& touch)
969 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
970 // Try to load configuration.
971 if(mIsFocusIndicatorShown == UNKNOWN)
973 GetConfigurationFromStyleManger();
976 // Clear the focus when user touch the screen.
977 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
978 if(((touch.GetPointCount() < 1) || (touch.GetState(0) == PointState::DOWN)))
980 // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
981 if(mClearFocusOnTouch)
986 // If KEYBOARD_FOCUSABLE and TOUCH_FOCUSABLE is true, set focus actor
987 Actor hitActor = touch.GetHitActor(0);
988 if(hitActor && hitActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && hitActor.GetProperty<bool>(DevelActor::Property::TOUCH_FOCUSABLE))
990 SetCurrentFocusActor(hitActor);
995 bool KeyboardFocusManager::OnWheelEvent(const WheelEvent& event)
997 bool consumed = false;
998 Actor actor = GetCurrentFocusActor();
1001 // Notify the actor about the wheel event
1002 consumed = EmitWheelSignals(actor, event);
1007 bool KeyboardFocusManager::EmitWheelSignals(Actor actor, const WheelEvent& event)
1009 bool consumed = false;
1013 Dali::Actor oldParent(actor.GetParent());
1015 // Only do the conversion and emit the signal if the actor's wheel signal has connections.
1016 if(!actor.WheelEventSignal().Empty())
1018 // Emit the signal to the parent
1019 consumed = actor.WheelEventSignal().Emit(actor, event);
1021 // if actor doesn't consume WheelEvent, give WheelEvent to its parent.
1024 // The actor may have been removed/reparented during the signal callbacks.
1025 Dali::Actor parent = actor.GetParent();
1028 (parent == oldParent))
1030 consumed = EmitWheelSignals(parent, event);
1038 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn)
1040 if(focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer())
1042 // Change Current Focused Window
1043 Layer rootLayer = window.GetRootLayer();
1044 mCurrentFocusedWindow = rootLayer;
1046 // Get Current Focused Actor from window
1047 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
1048 SetCurrentFocusActor(currentFocusedActor);
1050 if(currentFocusedActor && (mEnableFocusIndicator == ENABLE))
1052 // Make sure the focused actor is highlighted
1053 currentFocusedActor.Add(GetFocusIndicatorActor());
1054 mIsFocusIndicatorShown = SHOW;
1059 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
1061 return mPreFocusChangeSignal;
1064 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1066 return mFocusChangedSignal;
1069 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1071 return mFocusGroupChangedSignal;
1074 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1076 return mFocusedActorEnterKeySignal;
1079 bool KeyboardFocusManager::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
1081 Dali::BaseHandle handle(object);
1083 bool connected(true);
1084 KeyboardFocusManager* manager = static_cast<KeyboardFocusManager*>(object); // TypeRegistry guarantees that this is the correct type.
1086 if(0 == strcmp(signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE))
1088 manager->PreFocusChangeSignal().Connect(tracker, functor);
1090 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_CHANGED))
1092 manager->FocusChangedSignal().Connect(tracker, functor);
1094 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED))
1096 manager->FocusGroupChangedSignal().Connect(tracker, functor);
1098 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY))
1100 manager->FocusedActorEnterKeySignal().Connect(tracker, functor);
1104 // signalName does not match any signal
1111 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1113 mCustomAlgorithmInterface = &interface;
1116 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1118 if(!enable && mFocusIndicatorActor)
1120 mFocusIndicatorActor.Unparent();
1123 mEnableFocusIndicator = enable ? ENABLE : DISABLE;
1126 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1128 return (mEnableFocusIndicator == ENABLE);
1131 void KeyboardFocusManager::EnableDefaultAlgorithm(bool enable)
1133 mEnableDefaultAlgorithm = enable;
1136 bool KeyboardFocusManager::IsDefaultAlgorithmEnabled() const
1138 return mEnableDefaultAlgorithm;
1141 } // namespace Internal
1143 } // namespace Toolkit