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/adaptor-framework/lifecycle-controller.h>
23 #include <dali/devel-api/common/singleton-service.h>
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/adaptor-framework/scene-holder.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/actors/layer.h>
28 #include <dali/public-api/animation/constraints.h>
29 #include <dali/public-api/events/key-event.h>
30 #include <dali/public-api/events/touch-event.h>
31 #include <dali/public-api/object/property-map.h>
32 #include <dali/public-api/object/type-registry-helper.h>
33 #include <dali/public-api/object/type-registry.h>
34 #include <cstring> // for strcmp
37 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
38 #include <dali-toolkit/devel-api/controls/control-devel.h>
39 #include <dali-toolkit/devel-api/focus-manager/focus-finder.h>
40 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
41 #include <dali-toolkit/public-api/controls/control-impl.h>
42 #include <dali-toolkit/public-api/controls/control.h>
43 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
44 #include <dali-toolkit/public-api/styling/style-manager.h>
45 #include <dali/devel-api/adaptor-framework/accessibility.h>
53 namespace // Unnamed namespace
55 #if defined(DEBUG_ENABLED)
56 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
59 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
61 const char* const FOCUS_BORDER_IMAGE_FILE_NAME = "keyboard_focus.9.png";
65 BaseHandle handle = KeyboardFocusManager::Get();
69 SingletonService singletonService(SingletonService::Get());
72 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager(new Internal::KeyboardFocusManager());
73 singletonService.Register(typeid(manager), manager);
81 DALI_TYPE_REGISTRATION_BEGIN_CREATE(Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true)
83 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardPreFocusChange", SIGNAL_PRE_FOCUS_CHANGE)
84 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusChanged", SIGNAL_FOCUS_CHANGED)
85 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged", SIGNAL_FOCUS_GROUP_CHANGED)
86 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey", SIGNAL_FOCUSED_ACTOR_ENTER_KEY)
88 DALI_TYPE_REGISTRATION_END()
90 const unsigned int MAX_HISTORY_AMOUNT = 30; ///< Max length of focus history stack
92 } // unnamed namespace
94 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
96 Toolkit::KeyboardFocusManager manager;
98 SingletonService singletonService(SingletonService::Get());
101 // Check whether the keyboard focus manager is already created
102 Dali::BaseHandle handle = singletonService.GetSingleton(typeid(Toolkit::KeyboardFocusManager));
105 // If so, downcast the handle of singleton to keyboard focus manager
106 manager = Toolkit::KeyboardFocusManager(dynamic_cast<KeyboardFocusManager*>(handle.GetObjectPtr()));
113 KeyboardFocusManager::KeyboardFocusManager()
114 : mPreFocusChangeSignal(),
115 mFocusChangedSignal(),
116 mFocusGroupChangedSignal(),
117 mFocusedActorEnterKeySignal(),
118 mCurrentFocusActor(),
119 mFocusIndicatorActor(),
122 mCustomAlgorithmInterface(NULL),
123 mCurrentFocusedWindow(),
124 mIsFocusIndicatorShown(UNKNOWN),
125 mEnableFocusIndicator(ENABLE),
126 mAlwaysShowIndicator(ALWAYS_SHOW),
127 mFocusGroupLoopEnabled(false),
128 mIsWaitingKeyboardFocusChangeCommit(false),
129 mClearFocusOnTouch(true)
131 // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorShown.
133 LifecycleController::Get().InitSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnAdaptorInit);
136 void KeyboardFocusManager::OnAdaptorInit()
138 if(Adaptor::IsAvailable())
140 // Retrieve all the existing scene holders
141 Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
142 for(auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter)
144 (*iter).KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
145 (*iter).TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
146 Dali::Window window = DevelWindow::DownCast(*iter);
149 window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
153 // Get notified when any new scene holder is created afterwards
154 Adaptor::Get().WindowCreatedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnSceneHolderCreated);
158 void KeyboardFocusManager::OnSceneHolderCreated(Dali::Integration::SceneHolder& sceneHolder)
160 sceneHolder.KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
161 sceneHolder.TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
162 Dali::Window window = DevelWindow::DownCast(sceneHolder);
165 window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
169 KeyboardFocusManager::~KeyboardFocusManager()
173 void KeyboardFocusManager::GetConfigurationFromStyleManger()
175 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
178 Property::Map config = Toolkit::DevelStyleManager::GetConfigurations(styleManager);
179 mAlwaysShowIndicator = config["alwaysShowFocus"].Get<bool>() ? ALWAYS_SHOW : NONE;
180 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
181 mClearFocusOnTouch = (mIsFocusIndicatorShown == SHOW) ? false : true;
185 bool KeyboardFocusManager::SetCurrentFocusActor(Actor actor)
187 DALI_ASSERT_DEBUG(!mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?");
189 if(mIsFocusIndicatorShown == UNKNOWN)
191 GetConfigurationFromStyleManger();
194 return DoSetCurrentFocusActor(actor);
197 bool KeyboardFocusManager::DoSetCurrentFocusActor(Actor actor)
199 bool success = false;
200 if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
202 Integration::SceneHolder currentWindow = Integration::SceneHolder::Get(actor);
204 if(currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
206 Layer rootLayer = currentWindow.GetRootLayer();
207 mCurrentFocusedWindow = rootLayer;
211 Actor currentFocusedActor = GetCurrentFocusActor();
213 // If developer set focus on same actor, doing nothing
214 if(actor == currentFocusedActor)
223 // Check whether the actor is in the stage and is keyboard focusable.
224 if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
226 if((mIsFocusIndicatorShown == SHOW) && (mEnableFocusIndicator == ENABLE))
228 actor.Add(GetFocusIndicatorActor());
231 // Send notification for the change of focus actor
232 if(!mFocusChangedSignal.Empty())
234 mFocusChangedSignal.Emit(currentFocusedActor, actor);
237 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
238 if(currentlyFocusedControl)
240 // Do we need it to remember if it was previously DISABLED?
241 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
242 currentlyFocusedControl.ClearKeyInputFocus();
245 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
247 // Save the current focused actor
248 mCurrentFocusActor = actor;
250 bool focusedWindowFound = false;
251 for(unsigned int i = 0; i < mCurrentFocusActors.size(); i++)
253 if(mCurrentFocusActors[i].first == mCurrentFocusedWindow)
255 mCurrentFocusActors[i].second = actor;
256 focusedWindowFound = true;
260 if(!focusedWindowFound)
262 // A new window gains the focus, so store the focused actor in that window.
263 mCurrentFocusActors.push_back(std::pair<WeakHandle<Layer>, WeakHandle<Actor> >(mCurrentFocusedWindow, actor));
266 Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
267 if(newlyFocusedControl)
269 newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED);
270 newlyFocusedControl.SetKeyInputFocus();
273 // Push Current Focused Actor to FocusHistory
274 mFocusHistory.push_back(actor);
276 // Delete first element before add new element when Stack is full.
277 if(mFocusHistory.size() > MAX_HISTORY_AMOUNT)
279 FocusStackIterator beginPos = mFocusHistory.begin();
280 mFocusHistory.erase(beginPos);
283 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
288 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
294 Actor KeyboardFocusManager::GetCurrentFocusActor()
296 Actor actor = mCurrentFocusActor.GetHandle();
298 if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
300 // If the actor has been removed from the stage, then it should not be focused
302 mCurrentFocusActor.Reset();
307 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
311 for(index = 0; index < mCurrentFocusActors.size(); index++)
313 if(mCurrentFocusActors[index].first == mCurrentFocusedWindow)
315 actor = mCurrentFocusActors[index].second.GetHandle();
320 if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
322 // If the actor has been removed from the window, then the window doesn't have any focused actor
324 mCurrentFocusActors.erase(mCurrentFocusActors.begin() + index);
330 Actor KeyboardFocusManager::GetCurrentFocusGroup()
332 return GetFocusGroup(GetCurrentFocusActor());
335 void KeyboardFocusManager::MoveFocusBackward()
337 // Find Pre Focused Actor when the list size is more than 1
338 if(mFocusHistory.size() > 1)
340 // Delete current focused actor in history
341 mFocusHistory.pop_back();
343 // If pre-focused actors are not on stage or deleted, remove them in stack
344 while(mFocusHistory.size() > 0)
346 // Get pre focused actor
347 Actor target = mFocusHistory[mFocusHistory.size() - 1].GetHandle();
349 // Impl of Actor is not null
350 if(target && target.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
352 // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
353 mFocusHistory.pop_back();
354 SetCurrentFocusActor(target);
359 // Target is empty handle or off stage. Erase from queue
360 mFocusHistory.pop_back();
364 // if there is no actor which can get focus, then push current focus actor in stack again
365 if(mFocusHistory.size() == 0)
367 Actor currentFocusedActor = GetCurrentFocusActor();
368 mFocusHistory.push_back(currentFocusedActor);
373 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
375 Toolkit::Control control = Toolkit::Control::DownCast(actor);
376 return control && GetImplementation(control).IsKeyboardNavigationSupported();
379 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
381 // Get the actor's parent layout control that supports two dimensional keyboard navigation
386 Integration::SceneHolder window = Integration::SceneHolder::Get(actor);
389 rootActor = window.GetRootLayer();
392 parent = actor.GetParent();
395 while(parent && !IsLayoutControl(parent) && parent != rootActor)
397 parent = parent.GetParent();
400 return Toolkit::Control::DownCast(parent);
403 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
405 Actor currentFocusActor = GetCurrentFocusActor();
407 bool succeed = false;
409 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
410 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
411 while(parentLayoutControl && !succeed)
413 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
414 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
419 Actor nextFocusableActor;
421 Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
423 // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
424 if(currentFocusControl)
427 Property::Index index = Property::INVALID_INDEX;
428 Property::Value value;
430 // Find property index based upon focus direction
433 case Toolkit::Control::KeyboardFocus::LEFT:
435 index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
438 case Toolkit::Control::KeyboardFocus::RIGHT:
440 index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
443 case Toolkit::Control::KeyboardFocus::UP:
445 index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
448 case Toolkit::Control::KeyboardFocus::DOWN:
450 index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
457 // If the focusable property is set then determine next focusable actor
458 if(index != Property::INVALID_INDEX)
460 value = currentFocusActor.GetProperty(index);
461 actorId = value.Get<int>();
463 // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
466 if(currentFocusActor.GetParent())
468 nextFocusableActor = currentFocusActor.GetParent().FindChildById(actorId);
471 if(!nextFocusableActor)
473 Integration::SceneHolder window = Integration::SceneHolder::Get(currentFocusActor);
476 nextFocusableActor = window.GetRootLayer().FindChildById(actorId);
483 if(!nextFocusableActor)
485 // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
486 if(mCustomAlgorithmInterface)
488 mIsWaitingKeyboardFocusChangeCommit = true;
489 nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor(currentFocusActor, Actor(), direction);
490 mIsWaitingKeyboardFocusChangeCommit = false;
492 else if(!mPreFocusChangeSignal.Empty())
494 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
495 mIsWaitingKeyboardFocusChangeCommit = true;
496 nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
497 mIsWaitingKeyboardFocusChangeCommit = false;
501 // We should find it among the actors nearby.
502 nextFocusableActor = Toolkit::FocusFinder::Get().GetNearestFocusableActor(currentFocusActor, direction);
506 if(nextFocusableActor && nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
508 // Whether the next focusable actor is a layout control
509 if(IsLayoutControl(nextFocusableActor))
511 // If so, move the focus inside it.
512 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
513 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
517 // Otherwise, just set focus to the next focusable actor
518 succeed = SetCurrentFocusActor(nextFocusableActor);
526 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
528 // Ask the control for the next actor to focus
529 Actor nextFocusableActor = GetImplementation(control).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
530 if(nextFocusableActor)
532 if(!nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
534 // If the actor is not focusable, ask the same layout control for the next actor to focus
535 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
539 Actor currentFocusActor = GetCurrentFocusActor();
540 Actor committedFocusActor = nextFocusableActor;
542 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
543 // Signal handler can check the proposed actor and return a different actor if it wishes.
544 if(!mPreFocusChangeSignal.Empty())
546 mIsWaitingKeyboardFocusChangeCommit = true;
547 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
548 mIsWaitingKeyboardFocusChangeCommit = false;
551 if(committedFocusActor && committedFocusActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
553 // Whether the commited focusable actor is a layout control
554 if(IsLayoutControl(committedFocusActor))
556 // If so, move the focus inside it.
557 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
558 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
562 // Otherwise, just set focus to the next focusable actor
563 if(committedFocusActor == nextFocusableActor)
565 // If the application hasn't changed our proposed actor, we informs the layout control we will
566 // move the focus to what the control returns. The control might wish to perform some actions
567 // before the focus is actually moved.
568 GetImplementation(control).OnKeyboardFocusChangeCommitted(committedFocusActor);
571 return SetCurrentFocusActor(committedFocusActor);
582 // No more actor can be focused in the given direction within the same layout control.
587 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
589 bool succeed = false;
591 // Get the parent layout control of the current focus group
592 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
594 while(parentLayoutControl && !succeed)
596 // If the current focus group has a parent layout control, we can probably automatically
597 // move the focus to the next focus group in the forward or backward direction.
598 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
599 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
600 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
603 if(!mFocusGroupChangedSignal.Empty())
605 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
606 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
612 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
616 Toolkit::Control control = Toolkit::Control::DownCast(actor);
619 // Notify the control that enter has been pressed on it.
620 GetImplementation(control).KeyboardEnter();
623 // Send a notification for the actor.
624 if(!mFocusedActorEnterKeySignal.Empty())
626 mFocusedActorEnterKeySignal.Emit(actor);
631 void KeyboardFocusManager::ClearFocus()
633 Actor actor = GetCurrentFocusActor();
636 if(mFocusIndicatorActor)
638 actor.Remove(mFocusIndicatorActor);
641 // Send notification for the change of focus actor
642 if(!mFocusChangedSignal.Empty())
644 mFocusChangedSignal.Emit(actor, Actor());
647 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(actor);
648 if(currentlyFocusedControl)
650 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
651 currentlyFocusedControl.ClearKeyInputFocus();
655 mCurrentFocusActor.Reset();
656 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
659 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
661 mFocusGroupLoopEnabled = enabled;
664 bool KeyboardFocusManager::GetFocusGroupLoop() const
666 return mFocusGroupLoopEnabled;
669 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
673 // Create/Set focus group property.
674 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE);
678 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
680 // Check whether the actor is a focus group
681 bool isFocusGroup = false;
685 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
686 if(propertyIsFocusGroup != Property::INVALID_INDEX)
688 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
695 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
697 // Go through the actor's hierarchy to check which focus group the actor belongs to
698 while(actor && !IsFocusGroup(actor))
700 actor = actor.GetParent();
706 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
708 if(mFocusIndicatorActor != indicator)
710 Actor currentFocusActor = GetCurrentFocusActor();
711 if(currentFocusActor)
713 // The new focus indicator should be added to the current focused actor immediately
714 if(mFocusIndicatorActor)
716 currentFocusActor.Remove(mFocusIndicatorActor);
721 currentFocusActor.Add(indicator);
725 mFocusIndicatorActor = indicator;
729 Actor KeyboardFocusManager::GetFocusIndicatorActor()
731 if(!mFocusIndicatorActor)
733 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
734 const std::string imageDirPath = AssetManager::GetDaliImagePath();
735 mFocusIndicatorActor = Toolkit::ImageView::New(imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME);
737 // Apply size constraint to the focus indicator
738 mFocusIndicatorActor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
741 mFocusIndicatorActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
742 mFocusIndicatorActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
743 mFocusIndicatorActor.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
745 return mFocusIndicatorActor;
748 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
750 std::string keyName = event.GetKeyName();
752 if(mIsFocusIndicatorShown == UNKNOWN)
754 GetConfigurationFromStyleManger();
757 bool isFocusStartableKey = false;
759 if(event.GetState() == KeyEvent::DOWN)
761 if(keyName == "Left")
763 if(!mIsFocusIndicatorShown)
765 if(mIsFocusIndicatorShown == HIDE)
767 // Show focus indicator
768 mIsFocusIndicatorShown = SHOW;
772 // Move the focus towards left
773 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
776 isFocusStartableKey = true;
780 // Move the focus towards left
781 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
784 isFocusStartableKey = true;
786 else if(keyName == "Right")
788 if(!mIsFocusIndicatorShown)
790 if(mIsFocusIndicatorShown == HIDE)
792 // Show focus indicator
793 mIsFocusIndicatorShown = SHOW;
797 // Move the focus towards right
798 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
803 // Move the focus towards right
804 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
807 isFocusStartableKey = true;
809 else if(keyName == "Up")
811 if(mIsFocusIndicatorShown == HIDE)
813 // Show focus indicator
814 mIsFocusIndicatorShown = SHOW;
818 // Move the focus towards up
819 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
822 isFocusStartableKey = true;
824 else if(keyName == "Down")
826 if(mIsFocusIndicatorShown == HIDE)
828 // Show focus indicator
829 mIsFocusIndicatorShown = SHOW;
833 // Move the focus towards down
834 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
837 isFocusStartableKey = true;
839 else if(keyName == "Prior")
841 if(mIsFocusIndicatorShown == HIDE)
843 // Show focus indicator
844 mIsFocusIndicatorShown = SHOW;
848 // Move the focus towards the previous page
849 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
852 isFocusStartableKey = true;
854 else if(keyName == "Next")
856 if(mIsFocusIndicatorShown == HIDE)
858 // Show focus indicator
859 mIsFocusIndicatorShown = SHOW;
863 // Move the focus towards the next page
864 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
867 isFocusStartableKey = true;
869 else if(keyName == "Tab")
871 if(mIsFocusIndicatorShown == HIDE)
873 // Show focus indicator
874 mIsFocusIndicatorShown = SHOW;
878 // "Tab" key changes the focus group in the forward direction and
879 // "Shift-Tab" key changes it in the backward direction.
880 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
883 isFocusStartableKey = true;
885 else if(keyName == "space")
887 if(mIsFocusIndicatorShown == HIDE)
889 // Show focus indicator
890 mIsFocusIndicatorShown = SHOW;
893 isFocusStartableKey = true;
895 else if(keyName == "")
897 // Check the fake key event for evas-plugin case
898 if(mIsFocusIndicatorShown == HIDE)
900 // Show focus indicator
901 mIsFocusIndicatorShown = SHOW;
904 isFocusStartableKey = true;
906 else if(keyName == "Backspace")
908 // Emit signal to go back to the previous view???
910 else if(keyName == "Escape")
914 else if(event.GetState() == KeyEvent::UP)
916 if(keyName == "Return")
918 if(mIsFocusIndicatorShown == HIDE)
920 // Show focus indicator
921 mIsFocusIndicatorShown = SHOW;
925 // The focused actor has enter pressed on it
926 Actor actor = GetCurrentFocusActor();
929 DoKeyboardEnter(actor);
933 isFocusStartableKey = true;
937 if(isFocusStartableKey && mIsFocusIndicatorShown == SHOW)
939 Actor actor = GetCurrentFocusActor();
942 if(mEnableFocusIndicator == ENABLE)
944 // Make sure the focused actor is highlighted
945 actor.Add(GetFocusIndicatorActor());
950 // No actor is focused but keyboard focus is activated by the key press
951 // Let's try to move the initial focus
952 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
957 void KeyboardFocusManager::OnTouch(const TouchEvent& touch)
959 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
960 // Try to load configuration.
961 if(mIsFocusIndicatorShown == UNKNOWN)
963 GetConfigurationFromStyleManger();
966 // Clear the focus when user touch the screen.
967 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
968 // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
969 if(((touch.GetPointCount() < 1) || (touch.GetState(0) == PointState::DOWN)) && mClearFocusOnTouch)
975 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn)
977 if(focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer())
979 // Change Current Focused Window
980 Layer rootLayer = window.GetRootLayer();
981 mCurrentFocusedWindow = rootLayer;
983 // Get Current Focused Actor from window
984 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
985 SetCurrentFocusActor(currentFocusedActor);
987 if(currentFocusedActor && (mEnableFocusIndicator == ENABLE))
989 // Make sure the focused actor is highlighted
990 currentFocusedActor.Add(GetFocusIndicatorActor());
991 mIsFocusIndicatorShown = SHOW;
996 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
998 return mPreFocusChangeSignal;
1001 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1003 return mFocusChangedSignal;
1006 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1008 return mFocusGroupChangedSignal;
1011 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1013 return mFocusedActorEnterKeySignal;
1016 bool KeyboardFocusManager::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
1018 Dali::BaseHandle handle(object);
1020 bool connected(true);
1021 KeyboardFocusManager* manager = static_cast<KeyboardFocusManager*>(object); // TypeRegistry guarantees that this is the correct type.
1023 if(0 == strcmp(signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE))
1025 manager->PreFocusChangeSignal().Connect(tracker, functor);
1027 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_CHANGED))
1029 manager->FocusChangedSignal().Connect(tracker, functor);
1031 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED))
1033 manager->FocusGroupChangedSignal().Connect(tracker, functor);
1035 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY))
1037 manager->FocusedActorEnterKeySignal().Connect(tracker, functor);
1041 // signalName does not match any signal
1048 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1050 mCustomAlgorithmInterface = &interface;
1053 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1055 if(!enable && mFocusIndicatorActor)
1057 mFocusIndicatorActor.Unparent();
1060 mEnableFocusIndicator = enable ? ENABLE : DISABLE;
1063 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1065 return (mEnableFocusIndicator == ENABLE);
1068 } // namespace Internal
1070 } // namespace Toolkit