2 * Copyright (c) 2023 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(),
122 mFocusFinderRootActor(),
125 mCustomAlgorithmInterface(NULL),
126 mCurrentFocusedWindow(),
127 mIsFocusIndicatorShown(UNKNOWN),
128 mEnableFocusIndicator(ENABLE),
129 mAlwaysShowIndicator(ALWAYS_SHOW),
130 mFocusGroupLoopEnabled(false),
131 mIsWaitingKeyboardFocusChangeCommit(false),
132 mClearFocusOnTouch(true),
133 mEnableDefaultAlgorithm(false)
135 // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorShown.
137 LifecycleController::Get().InitSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnAdaptorInit);
140 void KeyboardFocusManager::OnAdaptorInit()
142 if(Adaptor::IsAvailable())
144 // Retrieve all the existing scene holders
145 Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
146 for(auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter)
148 (*iter).KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
149 (*iter).TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
150 (*iter).WheelEventGeneratedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnCustomWheelEvent);
151 (*iter).WheelEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWheelEvent);
152 Window window = Window::DownCast(*iter);
155 window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
159 // Get notified when any new scene holder is created afterwards
160 Adaptor::Get().WindowCreatedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnSceneHolderCreated);
164 void KeyboardFocusManager::OnSceneHolderCreated(Dali::Integration::SceneHolder& sceneHolder)
166 sceneHolder.KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
167 sceneHolder.TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
168 sceneHolder.WheelEventGeneratedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnCustomWheelEvent);
169 sceneHolder.WheelEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWheelEvent);
170 Window window = Window::DownCast(sceneHolder);
173 window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
177 KeyboardFocusManager::~KeyboardFocusManager()
181 void KeyboardFocusManager::GetConfigurationFromStyleManger()
183 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
186 const auto alwaysShowFocusValue = Toolkit::DevelStyleManager::GetConfigurations(styleManager).Find("alwaysShowFocus", Property::Type::BOOLEAN);
188 mAlwaysShowIndicator = (alwaysShowFocusValue && alwaysShowFocusValue->Get<bool>()) ? ALWAYS_SHOW : NONE;
189 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
190 mClearFocusOnTouch = (mIsFocusIndicatorShown == SHOW) ? false : true;
194 bool KeyboardFocusManager::SetCurrentFocusActor(Actor actor)
196 DALI_ASSERT_DEBUG(!mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?");
198 if(mIsFocusIndicatorShown == UNKNOWN)
200 GetConfigurationFromStyleManger();
203 return DoSetCurrentFocusActor(actor);
206 bool KeyboardFocusManager::DoSetCurrentFocusActor(Actor actor)
208 bool success = false;
210 // Check whether the actor is in the stage and is keyboard focusable.
212 actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) &&
213 actor.GetProperty<bool>(DevelActor::Property::USER_INTERACTION_ENABLED) &&
214 actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
216 // If the parent's KEYBOARD_FOCUSABLE_CHILDREN is false, it cannot have focus.
217 Actor parent = actor.GetParent();
220 if(!parent.GetProperty<bool>(DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN))
222 DALI_LOG_DEBUG_INFO("Parent Actor has KEYBOARD_FOCUSABLE_CHILDREN false\n");
225 parent = parent.GetParent();
228 // If developer set focus on same actor, doing nothing
229 Actor currentFocusedActor = GetCurrentFocusActor();
230 DALI_LOG_DEBUG_INFO("current focused actor : [%p] new focused actor : [%p]\n", currentFocusedActor.GetObjectPtr(), actor.GetObjectPtr());
231 if(actor == currentFocusedActor)
236 Integration::SceneHolder currentWindow = Integration::SceneHolder::Get(actor);
237 if(currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
239 Layer rootLayer = currentWindow.GetRootLayer();
240 mCurrentFocusedWindow = rootLayer;
243 if((mIsFocusIndicatorShown == SHOW) && (mEnableFocusIndicator == ENABLE))
245 actor.Add(GetFocusIndicatorActor());
248 // Save the current focused actor
249 mCurrentFocusActor = actor;
251 bool focusedWindowFound = false;
252 for(unsigned int i = 0; i < mCurrentFocusActors.size(); i++)
254 if(mCurrentFocusActors[i].first == mCurrentFocusedWindow)
256 mCurrentFocusActors[i].second = actor;
257 focusedWindowFound = true;
261 if(!focusedWindowFound)
263 // A new window gains the focus, so store the focused actor in that window.
264 mCurrentFocusActors.push_back(std::pair<WeakHandle<Layer>, WeakHandle<Actor> >(mCurrentFocusedWindow, actor));
267 // Send notification for the change of focus actor
268 if(!mFocusChangedSignal.Empty())
270 mFocusChangedSignal.Emit(currentFocusedActor, actor);
273 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
274 if(currentlyFocusedControl)
276 // Do we need it to remember if it was previously DISABLED?
277 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
278 currentlyFocusedControl.ClearKeyInputFocus();
281 Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
282 if(newlyFocusedControl)
284 newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED);
285 newlyFocusedControl.SetKeyInputFocus();
288 // Push Current Focused Actor to FocusHistory
289 mFocusHistory.push_back(actor);
291 // Delete first element before add new element when Stack is full.
292 if(mFocusHistory.size() > MAX_HISTORY_AMOUNT)
294 FocusStackIterator beginPos = mFocusHistory.begin();
295 mFocusHistory.erase(beginPos);
298 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
303 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
309 Actor KeyboardFocusManager::GetCurrentFocusActor()
311 Actor actor = mCurrentFocusActor.GetHandle();
313 if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
315 // If the actor has been removed from the stage, then it should not be focused
317 mCurrentFocusActor.Reset();
322 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
326 for(index = 0; index < mCurrentFocusActors.size(); index++)
328 if(mCurrentFocusActors[index].first == mCurrentFocusedWindow)
330 actor = mCurrentFocusActors[index].second.GetHandle();
335 if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
337 // If the actor has been removed from the window, then the window doesn't have any focused actor
339 mCurrentFocusActors.erase(mCurrentFocusActors.begin() + index);
345 Actor KeyboardFocusManager::GetCurrentFocusGroup()
347 return GetFocusGroup(GetCurrentFocusActor());
350 void KeyboardFocusManager::MoveFocusBackward()
352 // Find Pre Focused Actor when the list size is more than 1
353 if(mFocusHistory.size() > 1)
355 // Delete current focused actor in history
356 mFocusHistory.pop_back();
358 // If pre-focused actors are not on stage or deleted, remove them in stack
359 while(mFocusHistory.size() > 0)
361 // Get pre focused actor
362 Actor target = mFocusHistory[mFocusHistory.size() - 1].GetHandle();
364 // Impl of Actor is not null
365 if(target && target.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
367 // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
368 mFocusHistory.pop_back();
369 SetCurrentFocusActor(target);
374 // Target is empty handle or off stage. Erase from queue
375 mFocusHistory.pop_back();
379 // if there is no actor which can get focus, then push current focus actor in stack again
380 if(mFocusHistory.size() == 0)
382 Actor currentFocusedActor = GetCurrentFocusActor();
383 mFocusHistory.push_back(currentFocusedActor);
388 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
390 Toolkit::Control control = Toolkit::Control::DownCast(actor);
391 return control && GetImplementation(control).IsKeyboardNavigationSupported();
394 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
396 // Get the actor's parent layout control that supports two dimensional keyboard navigation
401 Integration::SceneHolder window = Integration::SceneHolder::Get(actor);
404 rootActor = window.GetRootLayer();
407 parent = actor.GetParent();
410 while(parent && !IsLayoutControl(parent) && parent != rootActor)
412 parent = parent.GetParent();
415 return Toolkit::Control::DownCast(parent);
418 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction, const std::string& deviceName)
420 Actor currentFocusActor = GetCurrentFocusActor();
422 bool succeed = false;
424 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
425 Toolkit::Control layoutControl = IsLayoutControl(currentFocusActor) ? Toolkit::Control::DownCast(currentFocusActor) : GetParentLayoutControl(currentFocusActor);
426 while(layoutControl && !succeed)
428 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
429 layoutControl = GetParentLayoutControl(layoutControl);
434 Actor nextFocusableActor;
436 Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
438 // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
439 if(currentFocusControl)
442 Property::Index index = Property::INVALID_INDEX;
443 Property::Value value;
445 // Find property index based upon focus direction
448 case Toolkit::Control::KeyboardFocus::LEFT:
450 index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
453 case Toolkit::Control::KeyboardFocus::RIGHT:
455 index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
458 case Toolkit::Control::KeyboardFocus::UP:
460 index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
463 case Toolkit::Control::KeyboardFocus::DOWN:
465 index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
468 case Toolkit::Control::KeyboardFocus::CLOCKWISE:
470 index = Toolkit::DevelControl::Property::CLOCKWISE_FOCUSABLE_ACTOR_ID;
473 case Toolkit::Control::KeyboardFocus::COUNTER_CLOCKWISE:
475 index = Toolkit::DevelControl::Property::COUNTER_CLOCKWISE_FOCUSABLE_ACTOR_ID;
482 // If the focusable property is set then determine next focusable actor
483 if(index != Property::INVALID_INDEX)
485 value = currentFocusActor.GetProperty(index);
486 actorId = value.Get<int>();
488 // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
491 if(currentFocusActor.GetParent())
493 nextFocusableActor = currentFocusActor.GetParent().FindChildById(actorId);
496 if(!nextFocusableActor)
498 Integration::SceneHolder window = Integration::SceneHolder::Get(currentFocusActor);
501 nextFocusableActor = window.GetRootLayer().FindChildById(actorId);
508 if(!nextFocusableActor)
510 // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
511 if(mCustomAlgorithmInterface)
513 mIsWaitingKeyboardFocusChangeCommit = true;
514 nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor(currentFocusActor, Actor(), direction, deviceName);
515 mIsWaitingKeyboardFocusChangeCommit = false;
517 else if(!mPreFocusChangeSignal.Empty())
519 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
520 mIsWaitingKeyboardFocusChangeCommit = true;
521 nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
522 mIsWaitingKeyboardFocusChangeCommit = false;
524 else if(mEnableDefaultAlgorithm)
526 Actor rootActor = mFocusFinderRootActor.GetHandle();
529 if(currentFocusActor)
531 // Find the window of the focused actor.
532 Integration::SceneHolder window = Integration::SceneHolder::Get(currentFocusActor);
535 rootActor = window.GetRootLayer();
540 // Searches from the currently focused window.
541 rootActor = mCurrentFocusedWindow.GetHandle();
546 // We should find it among the actors nearby.
547 nextFocusableActor = Toolkit::FocusFinder::GetNearestFocusableActor(rootActor, currentFocusActor, direction);
552 if(nextFocusableActor && nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && nextFocusableActor.GetProperty<bool>(DevelActor::Property::USER_INTERACTION_ENABLED))
554 // Whether the next focusable actor is a layout control
555 if(IsLayoutControl(nextFocusableActor))
557 // If so, move the focus inside it.
558 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
559 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
563 // Just set focus to the next focusable actor
564 succeed = SetCurrentFocusActor(nextFocusableActor);
572 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
574 // Ask the control for the next actor to focus
575 Actor nextFocusableActor = GetImplementation(control).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
576 if(nextFocusableActor)
578 if(!(nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) || nextFocusableActor.GetProperty<bool>(DevelActor::Property::USER_INTERACTION_ENABLED)))
580 // If the actor is not focusable, ask the same layout control for the next actor to focus
581 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
585 Actor currentFocusActor = GetCurrentFocusActor();
586 Actor committedFocusActor = nextFocusableActor;
588 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
589 // Signal handler can check the proposed actor and return a different actor if it wishes.
590 if(!mPreFocusChangeSignal.Empty())
592 mIsWaitingKeyboardFocusChangeCommit = true;
593 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
594 mIsWaitingKeyboardFocusChangeCommit = false;
597 if(committedFocusActor && committedFocusActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && committedFocusActor.GetProperty<bool>(DevelActor::Property::USER_INTERACTION_ENABLED))
599 // Whether the commited focusable actor is a layout control
600 if(IsLayoutControl(committedFocusActor) && committedFocusActor != control)
602 // If so, move the focus inside it.
603 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
604 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
608 // Otherwise, just set focus to the next focusable actor
609 if(committedFocusActor == nextFocusableActor)
611 // If the application hasn't changed our proposed actor, we informs the layout control we will
612 // move the focus to what the control returns. The control might wish to perform some actions
613 // before the focus is actually moved.
614 GetImplementation(control).OnKeyboardFocusChangeCommitted(committedFocusActor);
617 return SetCurrentFocusActor(committedFocusActor);
628 // No more actor can be focused in the given direction within the same layout control.
633 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
635 bool succeed = false;
637 // Get the parent layout control of the current focus group
638 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
640 while(parentLayoutControl && !succeed)
642 // If the current focus group has a parent layout control, we can probably automatically
643 // move the focus to the next focus group in the forward or backward direction.
644 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
645 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
646 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
649 if(!mFocusGroupChangedSignal.Empty())
651 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
652 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
658 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
662 Toolkit::Control control = Toolkit::Control::DownCast(actor);
665 // Notify the control that enter has been pressed on it.
666 GetImplementation(control).KeyboardEnter();
669 // Send a notification for the actor.
670 if(!mFocusedActorEnterKeySignal.Empty())
672 mFocusedActorEnterKeySignal.Emit(actor);
677 void KeyboardFocusManager::ClearFocus()
679 ClearFocusIndicator();
680 Actor actor = GetCurrentFocusActor();
683 DALI_LOG_RELEASE_INFO("ClearFocus id:(%d)\n", actor.GetProperty<int32_t>(Dali::Actor::Property::ID));
684 // Send notification for the change of focus actor
685 if(!mFocusChangedSignal.Empty())
687 mFocusChangedSignal.Emit(actor, Actor());
690 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(actor);
691 if(currentlyFocusedControl)
693 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
694 currentlyFocusedControl.ClearKeyInputFocus();
697 mCurrentFocusActor.Reset();
700 void KeyboardFocusManager::ClearFocusIndicator()
702 Actor actor = GetCurrentFocusActor();
705 if(mFocusIndicatorActor)
707 actor.Remove(mFocusIndicatorActor);
710 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
713 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
715 mFocusGroupLoopEnabled = enabled;
718 bool KeyboardFocusManager::GetFocusGroupLoop() const
720 return mFocusGroupLoopEnabled;
723 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
727 // Create/Set focus group property.
728 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE);
732 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
734 // Check whether the actor is a focus group
735 bool isFocusGroup = false;
739 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
740 if(propertyIsFocusGroup != Property::INVALID_INDEX)
742 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
749 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
751 // Go through the actor's hierarchy to check which focus group the actor belongs to
752 while(actor && !IsFocusGroup(actor))
754 actor = actor.GetParent();
760 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
762 if(mFocusIndicatorActor != indicator)
764 Actor currentFocusActor = GetCurrentFocusActor();
765 if(currentFocusActor)
767 // The new focus indicator should be added to the current focused actor immediately
768 if(mFocusIndicatorActor)
770 currentFocusActor.Remove(mFocusIndicatorActor);
775 currentFocusActor.Add(indicator);
779 mFocusIndicatorActor = indicator;
783 Actor KeyboardFocusManager::GetFocusIndicatorActor()
785 if(!mFocusIndicatorActor)
787 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
788 const std::string imageDirPath = AssetManager::GetDaliImagePath();
789 mFocusIndicatorActor = Toolkit::ImageView::New(imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME);
791 // Apply size constraint to the focus indicator
792 mFocusIndicatorActor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
795 mFocusIndicatorActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
796 mFocusIndicatorActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
797 mFocusIndicatorActor.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
799 return mFocusIndicatorActor;
802 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
804 const std::string& keyName = event.GetKeyName();
805 const std::string& deviceName = event.GetDeviceName();
807 if(mIsFocusIndicatorShown == UNKNOWN)
809 GetConfigurationFromStyleManger();
812 bool isFocusStartableKey = false;
814 if(event.GetState() == KeyEvent::DOWN)
816 if(keyName == "Left")
818 if(mIsFocusIndicatorShown == HIDE)
820 // Show focus indicator
821 mIsFocusIndicatorShown = SHOW;
825 // Move the focus towards left
826 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT, deviceName);
829 isFocusStartableKey = true;
831 else if(keyName == "Right")
833 if(mIsFocusIndicatorShown == HIDE)
835 // Show focus indicator
836 mIsFocusIndicatorShown = SHOW;
840 // Move the focus towards right
841 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT, deviceName);
844 isFocusStartableKey = true;
846 else if(keyName == "Up")
848 if(mIsFocusIndicatorShown == HIDE)
850 // Show focus indicator
851 mIsFocusIndicatorShown = SHOW;
855 // Move the focus towards up
856 MoveFocus(Toolkit::Control::KeyboardFocus::UP, deviceName);
859 isFocusStartableKey = true;
861 else if(keyName == "Down")
863 if(mIsFocusIndicatorShown == HIDE)
865 // Show focus indicator
866 mIsFocusIndicatorShown = SHOW;
870 // Move the focus towards down
871 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN, deviceName);
874 isFocusStartableKey = true;
876 else if(keyName == "Prior")
878 if(mIsFocusIndicatorShown == HIDE)
880 // Show focus indicator
881 mIsFocusIndicatorShown = SHOW;
885 // Move the focus towards the previous page
886 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP, deviceName);
889 isFocusStartableKey = true;
891 else if(keyName == "Next")
893 if(mIsFocusIndicatorShown == HIDE)
895 // Show focus indicator
896 mIsFocusIndicatorShown = SHOW;
900 // Move the focus towards the next page
901 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN, deviceName);
904 isFocusStartableKey = true;
906 else if(keyName == "Tab")
908 if(mIsFocusIndicatorShown == HIDE)
910 // Show focus indicator
911 mIsFocusIndicatorShown = SHOW;
915 // "Tab" key changes the focus group in the forward direction and
916 // "Shift-Tab" key changes it in the backward direction.
917 if(!DoMoveFocusToNextFocusGroup(!event.IsShiftModifier()))
919 // If the focus group is not changed, Move the focus towards forward, "Shift-Tap" key moves the focus towards backward.
920 MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::BACKWARD : Toolkit::Control::KeyboardFocus::FORWARD, deviceName);
924 isFocusStartableKey = true;
926 else if(keyName == "space")
928 if(mIsFocusIndicatorShown == HIDE)
930 // Show focus indicator
931 mIsFocusIndicatorShown = SHOW;
934 isFocusStartableKey = true;
936 else if(keyName == "")
938 // Check the fake key event for evas-plugin case
939 if(mIsFocusIndicatorShown == HIDE)
941 // Show focus indicator
942 mIsFocusIndicatorShown = SHOW;
945 isFocusStartableKey = true;
947 else if(keyName == "Backspace")
949 // Emit signal to go back to the previous view???
951 else if(keyName == "Escape")
955 else if(event.GetState() == KeyEvent::UP)
957 if(keyName == "Return")
959 if(mIsFocusIndicatorShown == HIDE)
961 // Show focus indicator
962 mIsFocusIndicatorShown = SHOW;
966 // The focused actor has enter pressed on it
967 Actor actor = GetCurrentFocusActor();
970 DoKeyboardEnter(actor);
974 isFocusStartableKey = true;
978 if(isFocusStartableKey && mIsFocusIndicatorShown == SHOW)
980 Actor actor = GetCurrentFocusActor();
983 if(mEnableFocusIndicator == ENABLE)
985 // Make sure the focused actor is highlighted
986 actor.Add(GetFocusIndicatorActor());
989 else if(!mEnableDefaultAlgorithm)
991 // No actor is focused but keyboard focus is activated by the key press
992 // Let's try to move the initial focus
993 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT, deviceName);
998 void KeyboardFocusManager::OnTouch(const TouchEvent& touch)
1000 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
1001 // Try to load configuration.
1002 if(mIsFocusIndicatorShown == UNKNOWN)
1004 GetConfigurationFromStyleManger();
1007 // Clear the focus when user touch the screen.
1008 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
1009 if(((touch.GetPointCount() < 1) || (touch.GetState(0) == PointState::DOWN)))
1011 // If you touch the currently focused actor again, you don't need to do SetCurrentFocusActor again.
1012 Actor hitActor = touch.GetHitActor(0);
1013 if(hitActor && hitActor == GetCurrentFocusActor())
1017 // If mClearFocusOnTouch is false, do not clear the focus indicator even if user touch the screen.
1018 if(mClearFocusOnTouch)
1020 ClearFocusIndicator();
1023 // If KEYBOARD_FOCUSABLE and TOUCH_FOCUSABLE is true, set focus actor
1024 if(hitActor && hitActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && hitActor.GetProperty<bool>(DevelActor::Property::TOUCH_FOCUSABLE))
1026 SetCurrentFocusActor(hitActor);
1031 void KeyboardFocusManager::OnWheelEvent(const WheelEvent& event)
1033 if(event.GetType() == Dali::WheelEvent::CUSTOM_WHEEL)
1035 Toolkit::Control::KeyboardFocus::Direction direction = (event.GetDelta() > 0) ? Toolkit::Control::KeyboardFocus::CLOCKWISE : Toolkit::Control::KeyboardFocus::COUNTER_CLOCKWISE;
1037 MoveFocus(direction);
1041 bool KeyboardFocusManager::OnCustomWheelEvent(const WheelEvent& event)
1043 bool consumed = false;
1044 Actor actor = GetCurrentFocusActor();
1047 // Notify the actor about the wheel event
1048 consumed = EmitCustomWheelSignals(actor, event);
1053 bool KeyboardFocusManager::EmitCustomWheelSignals(Actor actor, const WheelEvent& event)
1055 bool consumed = false;
1059 Dali::Actor oldParent(actor.GetParent());
1061 // Only do the conversion and emit the signal if the actor's wheel signal has connections.
1062 if(!actor.WheelEventSignal().Empty())
1064 // Emit the signal to the parent
1065 consumed = actor.WheelEventSignal().Emit(actor, event);
1067 // if actor doesn't consume WheelEvent, give WheelEvent to its parent.
1070 // The actor may have been removed/reparented during the signal callbacks.
1071 Dali::Actor parent = actor.GetParent();
1074 (parent == oldParent))
1076 consumed = EmitCustomWheelSignals(parent, event);
1084 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn)
1086 if(focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer())
1088 // Change Current Focused Window
1089 Layer rootLayer = window.GetRootLayer();
1090 mCurrentFocusedWindow = rootLayer;
1092 // Get Current Focused Actor from window
1093 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
1094 if(currentFocusedActor)
1096 SetCurrentFocusActor(currentFocusedActor);
1098 if(mEnableFocusIndicator == ENABLE)
1100 // Make sure the focused actor is highlighted
1101 currentFocusedActor.Add(GetFocusIndicatorActor());
1102 mIsFocusIndicatorShown = SHOW;
1108 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
1110 return mPreFocusChangeSignal;
1113 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1115 return mFocusChangedSignal;
1118 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1120 return mFocusGroupChangedSignal;
1123 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1125 return mFocusedActorEnterKeySignal;
1128 bool KeyboardFocusManager::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
1130 Dali::BaseHandle handle(object);
1132 bool connected(true);
1133 KeyboardFocusManager* manager = static_cast<KeyboardFocusManager*>(object); // TypeRegistry guarantees that this is the correct type.
1135 if(0 == strcmp(signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE))
1137 manager->PreFocusChangeSignal().Connect(tracker, functor);
1139 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_CHANGED))
1141 manager->FocusChangedSignal().Connect(tracker, functor);
1143 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED))
1145 manager->FocusGroupChangedSignal().Connect(tracker, functor);
1147 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY))
1149 manager->FocusedActorEnterKeySignal().Connect(tracker, functor);
1153 // signalName does not match any signal
1160 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1162 mCustomAlgorithmInterface = &interface;
1165 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1167 if(!enable && mFocusIndicatorActor)
1169 mFocusIndicatorActor.Unparent();
1172 mEnableFocusIndicator = enable ? ENABLE : DISABLE;
1175 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1177 return (mEnableFocusIndicator == ENABLE);
1180 void KeyboardFocusManager::EnableDefaultAlgorithm(bool enable)
1182 mEnableDefaultAlgorithm = enable;
1185 bool KeyboardFocusManager::IsDefaultAlgorithmEnabled() const
1187 return mEnableDefaultAlgorithm;
1190 void KeyboardFocusManager::SetFocusFinderRootActor(Actor actor)
1192 mFocusFinderRootActor = actor;
1195 void KeyboardFocusManager::ResetFocusFinderRootActor()
1197 mFocusFinderRootActor.Reset();
1200 } // namespace Internal
1202 } // namespace Toolkit