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/events/wheel-event.h>
33 #include <dali/public-api/object/property-map.h>
34 #include <dali/public-api/object/type-registry-helper.h>
35 #include <dali/public-api/object/type-registry.h>
36 #include <cstring> // for strcmp
39 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
40 #include <dali-toolkit/devel-api/controls/control-devel.h>
41 #include <dali-toolkit/devel-api/focus-manager/focus-finder.h>
42 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
43 #include <dali-toolkit/public-api/controls/control-impl.h>
44 #include <dali-toolkit/public-api/controls/control.h>
45 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
46 #include <dali-toolkit/public-api/styling/style-manager.h>
47 #include <dali/devel-api/adaptor-framework/accessibility.h>
55 namespace // Unnamed namespace
57 #if defined(DEBUG_ENABLED)
58 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
61 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
63 const char* const FOCUS_BORDER_IMAGE_FILE_NAME = "keyboard_focus.9.png";
67 BaseHandle handle = KeyboardFocusManager::Get();
71 SingletonService singletonService(SingletonService::Get());
74 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager(new Internal::KeyboardFocusManager());
75 singletonService.Register(typeid(manager), manager);
83 DALI_TYPE_REGISTRATION_BEGIN_CREATE(Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true)
85 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardPreFocusChange", SIGNAL_PRE_FOCUS_CHANGE)
86 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusChanged", SIGNAL_FOCUS_CHANGED)
87 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged", SIGNAL_FOCUS_GROUP_CHANGED)
88 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey", SIGNAL_FOCUSED_ACTOR_ENTER_KEY)
90 DALI_TYPE_REGISTRATION_END()
92 const unsigned int MAX_HISTORY_AMOUNT = 30; ///< Max length of focus history stack
94 } // unnamed namespace
96 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
98 Toolkit::KeyboardFocusManager manager;
100 SingletonService singletonService(SingletonService::Get());
103 // Check whether the keyboard focus manager is already created
104 Dali::BaseHandle handle = singletonService.GetSingleton(typeid(Toolkit::KeyboardFocusManager));
107 // If so, downcast the handle of singleton to keyboard focus manager
108 manager = Toolkit::KeyboardFocusManager(dynamic_cast<KeyboardFocusManager*>(handle.GetObjectPtr()));
115 KeyboardFocusManager::KeyboardFocusManager()
116 : mPreFocusChangeSignal(),
117 mFocusChangedSignal(),
118 mFocusGroupChangedSignal(),
119 mFocusedActorEnterKeySignal(),
120 mCurrentFocusActor(),
121 mFocusIndicatorActor(),
124 mCustomAlgorithmInterface(NULL),
125 mCurrentFocusedWindow(),
126 mIsFocusIndicatorShown(UNKNOWN),
127 mEnableFocusIndicator(ENABLE),
128 mAlwaysShowIndicator(ALWAYS_SHOW),
129 mFocusGroupLoopEnabled(false),
130 mIsWaitingKeyboardFocusChangeCommit(false),
131 mClearFocusOnTouch(true),
132 mEnableDefaultAlgorithm(false)
134 // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorShown.
136 LifecycleController::Get().InitSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnAdaptorInit);
139 void KeyboardFocusManager::OnAdaptorInit()
141 if(Adaptor::IsAvailable())
143 // Retrieve all the existing scene holders
144 Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
145 for(auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter)
147 (*iter).KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
148 (*iter).TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
149 (*iter).WheelEventGeneratedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnCustomWheelEvent);
150 (*iter).WheelEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWheelEvent);
151 Dali::Window window = DevelWindow::DownCast(*iter);
154 window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
158 // Get notified when any new scene holder is created afterwards
159 Adaptor::Get().WindowCreatedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnSceneHolderCreated);
163 void KeyboardFocusManager::OnSceneHolderCreated(Dali::Integration::SceneHolder& sceneHolder)
165 sceneHolder.KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
166 sceneHolder.TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
167 sceneHolder.WheelEventGeneratedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnCustomWheelEvent);
168 sceneHolder.WheelEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWheelEvent);
169 Dali::Window window = DevelWindow::DownCast(sceneHolder);
172 window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
176 KeyboardFocusManager::~KeyboardFocusManager()
180 void KeyboardFocusManager::GetConfigurationFromStyleManger()
182 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
185 Property::Map config = Toolkit::DevelStyleManager::GetConfigurations(styleManager);
186 mAlwaysShowIndicator = config["alwaysShowFocus"].Get<bool>() ? ALWAYS_SHOW : NONE;
187 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
188 mClearFocusOnTouch = (mIsFocusIndicatorShown == SHOW) ? false : true;
192 bool KeyboardFocusManager::SetCurrentFocusActor(Actor actor)
194 DALI_ASSERT_DEBUG(!mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?");
196 if(mIsFocusIndicatorShown == UNKNOWN)
198 GetConfigurationFromStyleManger();
201 return DoSetCurrentFocusActor(actor);
204 bool KeyboardFocusManager::DoSetCurrentFocusActor(Actor actor)
206 bool success = false;
208 // If the parent's KEYBOARD_FOCUSABLE_CHILDREN is false, it cannot have focus.
211 Actor parent = actor.GetParent();
214 if(!parent.GetProperty<bool>(DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN))
216 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] Parent Actor has KEYBOARD_FOCUSABLE_CHILDREN false,\n", __FUNCTION__, __LINE__);
219 parent = parent.GetParent();
223 if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(DevelActor::Property::USER_INTERACTION_ENABLED) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
225 Integration::SceneHolder currentWindow = Integration::SceneHolder::Get(actor);
227 if(currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
229 Layer rootLayer = currentWindow.GetRootLayer();
230 mCurrentFocusedWindow = rootLayer;
234 Actor currentFocusedActor = GetCurrentFocusActor();
236 // If developer set focus on same actor, doing nothing
237 if(actor == currentFocusedActor)
246 // Check whether the actor is in the stage and is keyboard focusable.
247 if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(DevelActor::Property::USER_INTERACTION_ENABLED) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
249 if((mIsFocusIndicatorShown == SHOW) && (mEnableFocusIndicator == ENABLE))
251 actor.Add(GetFocusIndicatorActor());
255 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
256 if(currentlyFocusedControl)
258 // Do we need it to remember if it was previously DISABLED?
259 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
260 currentlyFocusedControl.ClearKeyInputFocus();
263 // Save the current focused actor
264 mCurrentFocusActor = actor;
266 bool focusedWindowFound = false;
267 for(unsigned int i = 0; i < mCurrentFocusActors.size(); i++)
269 if(mCurrentFocusActors[i].first == mCurrentFocusedWindow)
271 mCurrentFocusActors[i].second = actor;
272 focusedWindowFound = true;
276 if(!focusedWindowFound)
278 // A new window gains the focus, so store the focused actor in that window.
279 mCurrentFocusActors.push_back(std::pair<WeakHandle<Layer>, WeakHandle<Actor> >(mCurrentFocusedWindow, actor));
282 Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
283 if(newlyFocusedControl)
285 newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED);
286 newlyFocusedControl.SetKeyInputFocus();
289 // Push Current Focused Actor to FocusHistory
290 mFocusHistory.push_back(actor);
292 // Delete first element before add new element when Stack is full.
293 if(mFocusHistory.size() > MAX_HISTORY_AMOUNT)
295 FocusStackIterator beginPos = mFocusHistory.begin();
296 mFocusHistory.erase(beginPos);
299 // Send notification for the change of focus actor
300 if(!mFocusChangedSignal.Empty())
302 mFocusChangedSignal.Emit(currentFocusedActor, actor);
304 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
309 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
315 Actor KeyboardFocusManager::GetCurrentFocusActor()
317 Actor actor = mCurrentFocusActor.GetHandle();
319 if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
321 // If the actor has been removed from the stage, then it should not be focused
323 mCurrentFocusActor.Reset();
328 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
332 for(index = 0; index < mCurrentFocusActors.size(); index++)
334 if(mCurrentFocusActors[index].first == mCurrentFocusedWindow)
336 actor = mCurrentFocusActors[index].second.GetHandle();
341 if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
343 // If the actor has been removed from the window, then the window doesn't have any focused actor
345 mCurrentFocusActors.erase(mCurrentFocusActors.begin() + index);
351 Actor KeyboardFocusManager::GetCurrentFocusGroup()
353 return GetFocusGroup(GetCurrentFocusActor());
356 void KeyboardFocusManager::MoveFocusBackward()
358 // Find Pre Focused Actor when the list size is more than 1
359 if(mFocusHistory.size() > 1)
361 // Delete current focused actor in history
362 mFocusHistory.pop_back();
364 // If pre-focused actors are not on stage or deleted, remove them in stack
365 while(mFocusHistory.size() > 0)
367 // Get pre focused actor
368 Actor target = mFocusHistory[mFocusHistory.size() - 1].GetHandle();
370 // Impl of Actor is not null
371 if(target && target.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
373 // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
374 mFocusHistory.pop_back();
375 SetCurrentFocusActor(target);
380 // Target is empty handle or off stage. Erase from queue
381 mFocusHistory.pop_back();
385 // if there is no actor which can get focus, then push current focus actor in stack again
386 if(mFocusHistory.size() == 0)
388 Actor currentFocusedActor = GetCurrentFocusActor();
389 mFocusHistory.push_back(currentFocusedActor);
394 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
396 Toolkit::Control control = Toolkit::Control::DownCast(actor);
397 return control && GetImplementation(control).IsKeyboardNavigationSupported();
400 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
402 // Get the actor's parent layout control that supports two dimensional keyboard navigation
407 Integration::SceneHolder window = Integration::SceneHolder::Get(actor);
410 rootActor = window.GetRootLayer();
413 parent = actor.GetParent();
416 while(parent && !IsLayoutControl(parent) && parent != rootActor)
418 parent = parent.GetParent();
421 return Toolkit::Control::DownCast(parent);
424 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction, const std::string& deviceName)
426 Actor currentFocusActor = GetCurrentFocusActor();
428 bool succeed = false;
430 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
431 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
432 while(parentLayoutControl && !succeed)
434 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
435 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
440 Actor nextFocusableActor;
442 Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
444 // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
445 if(currentFocusControl)
448 Property::Index index = Property::INVALID_INDEX;
449 Property::Value value;
451 // Find property index based upon focus direction
454 case Toolkit::Control::KeyboardFocus::LEFT:
456 index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
459 case Toolkit::Control::KeyboardFocus::RIGHT:
461 index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
464 case Toolkit::Control::KeyboardFocus::UP:
466 index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
469 case Toolkit::Control::KeyboardFocus::DOWN:
471 index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
474 case Toolkit::Control::KeyboardFocus::CLOCKWISE:
476 index = Toolkit::DevelControl::Property::CLOCKWISE_FOCUSABLE_ACTOR_ID;
479 case Toolkit::Control::KeyboardFocus::COUNTER_CLOCKWISE:
481 index = Toolkit::DevelControl::Property::COUNTER_CLOCKWISE_FOCUSABLE_ACTOR_ID;
488 // If the focusable property is set then determine next focusable actor
489 if(index != Property::INVALID_INDEX)
491 value = currentFocusActor.GetProperty(index);
492 actorId = value.Get<int>();
494 // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
497 if(currentFocusActor.GetParent())
499 nextFocusableActor = currentFocusActor.GetParent().FindChildById(actorId);
502 if(!nextFocusableActor)
504 Integration::SceneHolder window = Integration::SceneHolder::Get(currentFocusActor);
507 nextFocusableActor = window.GetRootLayer().FindChildById(actorId);
514 if(!nextFocusableActor)
516 // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
517 if(mCustomAlgorithmInterface)
519 mIsWaitingKeyboardFocusChangeCommit = true;
520 nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor(currentFocusActor, Actor(), direction, deviceName);
521 mIsWaitingKeyboardFocusChangeCommit = false;
523 else if(!mPreFocusChangeSignal.Empty())
525 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
526 mIsWaitingKeyboardFocusChangeCommit = true;
527 nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
528 mIsWaitingKeyboardFocusChangeCommit = false;
530 else if (mEnableDefaultAlgorithm)
533 if (currentFocusActor)
535 // Find the window of the focused actor.
536 Integration::SceneHolder window = Integration::SceneHolder::Get(currentFocusActor);
539 rootLayer = window.GetRootLayer();
544 // Searches from the currently focused window.
545 rootLayer = mCurrentFocusedWindow.GetHandle();
549 // We should find it among the actors nearby.
550 nextFocusableActor = Toolkit::FocusFinder::GetNearestFocusableActor(rootLayer, currentFocusActor, direction);
555 if(nextFocusableActor && nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && nextFocusableActor.GetProperty<bool>(DevelActor::Property::USER_INTERACTION_ENABLED))
557 // Whether the next focusable actor is a layout control
558 if(IsLayoutControl(nextFocusableActor))
560 // If so, move the focus inside it.
561 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
562 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
566 // Otherwise, just set focus to the next focusable actor
567 succeed = SetCurrentFocusActor(nextFocusableActor);
575 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
577 // Ask the control for the next actor to focus
578 Actor nextFocusableActor = GetImplementation(control).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
579 if(nextFocusableActor)
581 if(!(nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) || nextFocusableActor.GetProperty<bool>(DevelActor::Property::USER_INTERACTION_ENABLED)))
583 // If the actor is not focusable, ask the same layout control for the next actor to focus
584 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
588 Actor currentFocusActor = GetCurrentFocusActor();
589 Actor committedFocusActor = nextFocusableActor;
591 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
592 // Signal handler can check the proposed actor and return a different actor if it wishes.
593 if(!mPreFocusChangeSignal.Empty())
595 mIsWaitingKeyboardFocusChangeCommit = true;
596 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
597 mIsWaitingKeyboardFocusChangeCommit = false;
600 if(committedFocusActor && committedFocusActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && committedFocusActor.GetProperty<bool>(DevelActor::Property::USER_INTERACTION_ENABLED))
602 // Whether the commited focusable actor is a layout control
603 if(IsLayoutControl(committedFocusActor))
605 // If so, move the focus inside it.
606 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
607 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
611 // Otherwise, just set focus to the next focusable actor
612 if(committedFocusActor == nextFocusableActor)
614 // If the application hasn't changed our proposed actor, we informs the layout control we will
615 // move the focus to what the control returns. The control might wish to perform some actions
616 // before the focus is actually moved.
617 GetImplementation(control).OnKeyboardFocusChangeCommitted(committedFocusActor);
620 return SetCurrentFocusActor(committedFocusActor);
631 // No more actor can be focused in the given direction within the same layout control.
636 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
638 bool succeed = false;
640 // Get the parent layout control of the current focus group
641 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
643 while(parentLayoutControl && !succeed)
645 // If the current focus group has a parent layout control, we can probably automatically
646 // move the focus to the next focus group in the forward or backward direction.
647 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
648 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
649 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
652 if(!mFocusGroupChangedSignal.Empty())
654 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
655 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
661 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
665 Toolkit::Control control = Toolkit::Control::DownCast(actor);
668 // Notify the control that enter has been pressed on it.
669 GetImplementation(control).KeyboardEnter();
672 // Send a notification for the actor.
673 if(!mFocusedActorEnterKeySignal.Empty())
675 mFocusedActorEnterKeySignal.Emit(actor);
680 void KeyboardFocusManager::ClearFocus()
682 ClearFocusIndicator();
683 Actor actor = GetCurrentFocusActor();
686 // Send notification for the change of focus actor
687 if(!mFocusChangedSignal.Empty())
689 mFocusChangedSignal.Emit(actor, Actor());
692 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(actor);
693 if(currentlyFocusedControl)
695 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
696 currentlyFocusedControl.ClearKeyInputFocus();
699 mCurrentFocusActor.Reset();
702 void KeyboardFocusManager::ClearFocusIndicator()
704 Actor actor = GetCurrentFocusActor();
707 if(mFocusIndicatorActor)
709 actor.Remove(mFocusIndicatorActor);
712 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
715 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
717 mFocusGroupLoopEnabled = enabled;
720 bool KeyboardFocusManager::GetFocusGroupLoop() const
722 return mFocusGroupLoopEnabled;
725 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
729 // Create/Set focus group property.
730 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE);
734 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
736 // Check whether the actor is a focus group
737 bool isFocusGroup = false;
741 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
742 if(propertyIsFocusGroup != Property::INVALID_INDEX)
744 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
751 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
753 // Go through the actor's hierarchy to check which focus group the actor belongs to
754 while(actor && !IsFocusGroup(actor))
756 actor = actor.GetParent();
762 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
764 if(mFocusIndicatorActor != indicator)
766 Actor currentFocusActor = GetCurrentFocusActor();
767 if(currentFocusActor)
769 // The new focus indicator should be added to the current focused actor immediately
770 if(mFocusIndicatorActor)
772 currentFocusActor.Remove(mFocusIndicatorActor);
777 currentFocusActor.Add(indicator);
781 mFocusIndicatorActor = indicator;
785 Actor KeyboardFocusManager::GetFocusIndicatorActor()
787 if(!mFocusIndicatorActor)
789 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
790 const std::string imageDirPath = AssetManager::GetDaliImagePath();
791 mFocusIndicatorActor = Toolkit::ImageView::New(imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME);
793 // Apply size constraint to the focus indicator
794 mFocusIndicatorActor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
797 mFocusIndicatorActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
798 mFocusIndicatorActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
799 mFocusIndicatorActor.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
801 return mFocusIndicatorActor;
804 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
806 const std::string& keyName = event.GetKeyName();
807 const std::string& deviceName = event.GetDeviceName();
809 if(mIsFocusIndicatorShown == UNKNOWN)
811 GetConfigurationFromStyleManger();
814 bool isFocusStartableKey = false;
816 if(event.GetState() == KeyEvent::DOWN)
818 if(keyName == "Left")
820 if(mIsFocusIndicatorShown == HIDE)
822 // Show focus indicator
823 mIsFocusIndicatorShown = SHOW;
827 // Move the focus towards left
828 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT, deviceName);
831 isFocusStartableKey = true;
833 else if(keyName == "Right")
835 if(mIsFocusIndicatorShown == HIDE)
837 // Show focus indicator
838 mIsFocusIndicatorShown = SHOW;
842 // Move the focus towards right
843 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT, deviceName);
846 isFocusStartableKey = true;
848 else if(keyName == "Up")
850 if(mIsFocusIndicatorShown == HIDE)
852 // Show focus indicator
853 mIsFocusIndicatorShown = SHOW;
857 // Move the focus towards up
858 MoveFocus(Toolkit::Control::KeyboardFocus::UP, deviceName);
861 isFocusStartableKey = true;
863 else if(keyName == "Down")
865 if(mIsFocusIndicatorShown == HIDE)
867 // Show focus indicator
868 mIsFocusIndicatorShown = SHOW;
872 // Move the focus towards down
873 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN, deviceName);
876 isFocusStartableKey = true;
878 else if(keyName == "Prior")
880 if(mIsFocusIndicatorShown == HIDE)
882 // Show focus indicator
883 mIsFocusIndicatorShown = SHOW;
887 // Move the focus towards the previous page
888 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP, deviceName);
891 isFocusStartableKey = true;
893 else if(keyName == "Next")
895 if(mIsFocusIndicatorShown == HIDE)
897 // Show focus indicator
898 mIsFocusIndicatorShown = SHOW;
902 // Move the focus towards the next page
903 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN, deviceName);
906 isFocusStartableKey = true;
908 else if(keyName == "Tab")
910 if(mIsFocusIndicatorShown == HIDE)
912 // Show focus indicator
913 mIsFocusIndicatorShown = SHOW;
917 // "Tab" key changes the focus group in the forward direction and
918 // "Shift-Tab" key changes it in the backward direction.
919 if(!DoMoveFocusToNextFocusGroup(!event.IsShiftModifier()))
921 // If the focus group is not changed, Move the focus towards forward, "Shift-Tap" key moves the focus towards backward.
922 MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::BACKWARD : Toolkit::Control::KeyboardFocus::FORWARD, deviceName);
926 isFocusStartableKey = true;
928 else if(keyName == "space")
930 if(mIsFocusIndicatorShown == HIDE)
932 // Show focus indicator
933 mIsFocusIndicatorShown = SHOW;
936 isFocusStartableKey = true;
938 else if(keyName == "")
940 // Check the fake key event for evas-plugin case
941 if(mIsFocusIndicatorShown == HIDE)
943 // Show focus indicator
944 mIsFocusIndicatorShown = SHOW;
947 isFocusStartableKey = true;
949 else if(keyName == "Backspace")
951 // Emit signal to go back to the previous view???
953 else if(keyName == "Escape")
957 else if(event.GetState() == KeyEvent::UP)
959 if(keyName == "Return")
961 if(mIsFocusIndicatorShown == HIDE)
963 // Show focus indicator
964 mIsFocusIndicatorShown = SHOW;
968 // The focused actor has enter pressed on it
969 Actor actor = GetCurrentFocusActor();
972 DoKeyboardEnter(actor);
976 isFocusStartableKey = true;
980 if(isFocusStartableKey && mIsFocusIndicatorShown == SHOW)
982 Actor actor = GetCurrentFocusActor();
985 if(mEnableFocusIndicator == ENABLE)
987 // Make sure the focused actor is highlighted
988 actor.Add(GetFocusIndicatorActor());
993 // No actor is focused but keyboard focus is activated by the key press
994 // Let's try to move the initial focus
995 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT, deviceName);
1000 void KeyboardFocusManager::OnTouch(const TouchEvent& touch)
1002 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
1003 // Try to load configuration.
1004 if(mIsFocusIndicatorShown == UNKNOWN)
1006 GetConfigurationFromStyleManger();
1009 // Clear the focus when user touch the screen.
1010 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
1011 if(((touch.GetPointCount() < 1) || (touch.GetState(0) == PointState::DOWN)))
1013 // If you touch the currently focused actor again, you don't need to do SetCurrentFocusActor again.
1014 Actor hitActor = touch.GetHitActor(0);
1015 if(hitActor && hitActor == GetCurrentFocusActor())
1019 // If KEYBOARD_FOCUSABLE and TOUCH_FOCUSABLE is true, set focus actor
1020 if(hitActor && hitActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && hitActor.GetProperty<bool>(DevelActor::Property::TOUCH_FOCUSABLE))
1022 // If mClearFocusOnTouch is false, do not clear the focus
1023 if(mClearFocusOnTouch)
1027 SetCurrentFocusActor(hitActor);
1031 // If mClearFocusOnTouch is false, do not clear the focus indicator even if user touch the screen.
1032 if(mClearFocusOnTouch)
1034 ClearFocusIndicator();
1040 void KeyboardFocusManager::OnWheelEvent(const WheelEvent& event)
1042 if(event.GetType() == Dali::WheelEvent::CUSTOM_WHEEL)
1044 Toolkit::Control::KeyboardFocus::Direction direction = (event.GetDelta() > 0) ? Toolkit::Control::KeyboardFocus::CLOCKWISE : Toolkit::Control::KeyboardFocus::COUNTER_CLOCKWISE;
1046 MoveFocus(direction);
1050 bool KeyboardFocusManager::OnCustomWheelEvent(const WheelEvent& event)
1052 bool consumed = false;
1053 Actor actor = GetCurrentFocusActor();
1056 // Notify the actor about the wheel event
1057 consumed = EmitCustomWheelSignals(actor, event);
1062 bool KeyboardFocusManager::EmitCustomWheelSignals(Actor actor, const WheelEvent& event)
1064 bool consumed = false;
1068 Dali::Actor oldParent(actor.GetParent());
1070 // Only do the conversion and emit the signal if the actor's wheel signal has connections.
1071 if(!actor.WheelEventSignal().Empty())
1073 // Emit the signal to the parent
1074 consumed = actor.WheelEventSignal().Emit(actor, event);
1076 // if actor doesn't consume WheelEvent, give WheelEvent to its parent.
1079 // The actor may have been removed/reparented during the signal callbacks.
1080 Dali::Actor parent = actor.GetParent();
1083 (parent == oldParent))
1085 consumed = EmitCustomWheelSignals(parent, event);
1093 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn)
1095 if(focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer())
1097 // Change Current Focused Window
1098 Layer rootLayer = window.GetRootLayer();
1099 mCurrentFocusedWindow = rootLayer;
1101 // Get Current Focused Actor from window
1102 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
1103 SetCurrentFocusActor(currentFocusedActor);
1105 if(currentFocusedActor && (mEnableFocusIndicator == ENABLE))
1107 // Make sure the focused actor is highlighted
1108 currentFocusedActor.Add(GetFocusIndicatorActor());
1109 mIsFocusIndicatorShown = SHOW;
1114 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
1116 return mPreFocusChangeSignal;
1119 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1121 return mFocusChangedSignal;
1124 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1126 return mFocusGroupChangedSignal;
1129 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1131 return mFocusedActorEnterKeySignal;
1134 bool KeyboardFocusManager::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
1136 Dali::BaseHandle handle(object);
1138 bool connected(true);
1139 KeyboardFocusManager* manager = static_cast<KeyboardFocusManager*>(object); // TypeRegistry guarantees that this is the correct type.
1141 if(0 == strcmp(signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE))
1143 manager->PreFocusChangeSignal().Connect(tracker, functor);
1145 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_CHANGED))
1147 manager->FocusChangedSignal().Connect(tracker, functor);
1149 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED))
1151 manager->FocusGroupChangedSignal().Connect(tracker, functor);
1153 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY))
1155 manager->FocusedActorEnterKeySignal().Connect(tracker, functor);
1159 // signalName does not match any signal
1166 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1168 mCustomAlgorithmInterface = &interface;
1171 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1173 if(!enable && mFocusIndicatorActor)
1175 mFocusIndicatorActor.Unparent();
1178 mEnableFocusIndicator = enable ? ENABLE : DISABLE;
1181 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1183 return (mEnableFocusIndicator == ENABLE);
1186 void KeyboardFocusManager::EnableDefaultAlgorithm(bool enable)
1188 mEnableDefaultAlgorithm = enable;
1191 bool KeyboardFocusManager::IsDefaultAlgorithmEnabled() const
1193 return mEnableDefaultAlgorithm;
1196 } // namespace Internal
1198 } // namespace Toolkit