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/styling/style-manager-devel.h>
40 #include <dali-toolkit/public-api/controls/control-impl.h>
41 #include <dali-toolkit/public-api/controls/control.h>
42 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
43 #include <dali-toolkit/public-api/styling/style-manager.h>
44 #include <dali/devel-api/adaptor-framework/accessibility.h>
52 namespace // Unnamed namespace
54 #if defined(DEBUG_ENABLED)
55 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
58 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
60 const char* const FOCUS_BORDER_IMAGE_FILE_NAME = "keyboard_focus.9.png";
64 BaseHandle handle = KeyboardFocusManager::Get();
68 SingletonService singletonService(SingletonService::Get());
71 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager(new Internal::KeyboardFocusManager());
72 singletonService.Register(typeid(manager), manager);
80 DALI_TYPE_REGISTRATION_BEGIN_CREATE(Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true)
82 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardPreFocusChange", SIGNAL_PRE_FOCUS_CHANGE)
83 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusChanged", SIGNAL_FOCUS_CHANGED)
84 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged", SIGNAL_FOCUS_GROUP_CHANGED)
85 DALI_SIGNAL_REGISTRATION(Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey", SIGNAL_FOCUSED_ACTOR_ENTER_KEY)
87 DALI_TYPE_REGISTRATION_END()
89 const unsigned int MAX_HISTORY_AMOUNT = 30; ///< Max length of focus history stack
91 } // unnamed namespace
93 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
95 Toolkit::KeyboardFocusManager manager;
97 SingletonService singletonService(SingletonService::Get());
100 // Check whether the keyboard focus manager is already created
101 Dali::BaseHandle handle = singletonService.GetSingleton(typeid(Toolkit::KeyboardFocusManager));
104 // If so, downcast the handle of singleton to keyboard focus manager
105 manager = Toolkit::KeyboardFocusManager(dynamic_cast<KeyboardFocusManager*>(handle.GetObjectPtr()));
112 KeyboardFocusManager::KeyboardFocusManager()
113 : mPreFocusChangeSignal(),
114 mFocusChangedSignal(),
115 mFocusGroupChangedSignal(),
116 mFocusedActorEnterKeySignal(),
117 mCurrentFocusActor(),
118 mFocusIndicatorActor(),
121 mCustomAlgorithmInterface(NULL),
122 mCurrentFocusedWindow(),
123 mIsFocusIndicatorShown(UNKNOWN),
124 mEnableFocusIndicator(ENABLE),
125 mAlwaysShowIndicator(ALWAYS_SHOW),
126 mFocusGroupLoopEnabled(false),
127 mIsWaitingKeyboardFocusChangeCommit(false),
128 mClearFocusOnTouch(true)
130 // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorShown.
132 LifecycleController::Get().InitSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnAdaptorInit);
135 void KeyboardFocusManager::OnAdaptorInit()
137 if(Adaptor::IsAvailable())
139 // Retrieve all the existing scene holders
140 Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
141 for(auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter)
143 (*iter).KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
144 (*iter).TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
145 Dali::Window window = DevelWindow::DownCast(*iter);
148 window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
152 // Get notified when any new scene holder is created afterwards
153 Adaptor::Get().WindowCreatedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnSceneHolderCreated);
157 void KeyboardFocusManager::OnSceneHolderCreated(Dali::Integration::SceneHolder& sceneHolder)
159 sceneHolder.KeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
160 sceneHolder.TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouch);
161 Dali::Window window = DevelWindow::DownCast(sceneHolder);
164 window.FocusChangeSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
168 KeyboardFocusManager::~KeyboardFocusManager()
172 void KeyboardFocusManager::GetConfigurationFromStyleManger()
174 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
177 Property::Map config = Toolkit::DevelStyleManager::GetConfigurations(styleManager);
178 mAlwaysShowIndicator = config["alwaysShowFocus"].Get<bool>() ? ALWAYS_SHOW : NONE;
179 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
180 mClearFocusOnTouch = (mIsFocusIndicatorShown == SHOW) ? false : true;
184 bool KeyboardFocusManager::SetCurrentFocusActor(Actor actor)
186 DALI_ASSERT_DEBUG(!mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?");
188 if(mIsFocusIndicatorShown == UNKNOWN)
190 GetConfigurationFromStyleManger();
193 return DoSetCurrentFocusActor(actor);
196 bool KeyboardFocusManager::DoSetCurrentFocusActor(Actor actor)
198 bool success = false;
199 if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
201 Integration::SceneHolder currentWindow = Integration::SceneHolder::Get(actor);
203 if(currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
205 Layer rootLayer = currentWindow.GetRootLayer();
206 mCurrentFocusedWindow = rootLayer;
210 Actor currentFocusedActor = GetCurrentFocusActor();
212 // If developer set focus on same actor, doing nothing
213 if(actor == currentFocusedActor)
222 // Check whether the actor is in the stage and is keyboard focusable.
223 if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
225 if((mIsFocusIndicatorShown == SHOW) && (mEnableFocusIndicator == ENABLE))
227 actor.Add(GetFocusIndicatorActor());
230 // Send notification for the change of focus actor
231 if(!mFocusChangedSignal.Empty())
233 mFocusChangedSignal.Emit(currentFocusedActor, actor);
236 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
237 if(currentlyFocusedControl)
239 // Do we need it to remember if it was previously DISABLED?
240 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
241 currentlyFocusedControl.ClearKeyInputFocus();
244 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
246 // Save the current focused actor
247 mCurrentFocusActor = actor;
249 bool focusedWindowFound = false;
250 for(unsigned int i = 0; i < mCurrentFocusActors.size(); i++)
252 if(mCurrentFocusActors[i].first == mCurrentFocusedWindow)
254 mCurrentFocusActors[i].second = actor;
255 focusedWindowFound = true;
259 if(!focusedWindowFound)
261 // A new window gains the focus, so store the focused actor in that window.
262 mCurrentFocusActors.push_back(std::pair<WeakHandle<Layer>, WeakHandle<Actor> >(mCurrentFocusedWindow, actor));
265 Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
266 if(newlyFocusedControl)
268 newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED);
269 newlyFocusedControl.SetKeyInputFocus();
272 // Push Current Focused Actor to FocusHistory
273 mFocusHistory.push_back(actor);
275 // Delete first element before add new element when Stack is full.
276 if(mFocusHistory.size() > MAX_HISTORY_AMOUNT)
278 FocusStackIterator beginPos = mFocusHistory.begin();
279 mFocusHistory.erase(beginPos);
282 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
287 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
293 Actor KeyboardFocusManager::GetCurrentFocusActor()
295 Actor actor = mCurrentFocusActor.GetHandle();
297 if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
299 // If the actor has been removed from the stage, then it should not be focused
301 mCurrentFocusActor.Reset();
306 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
310 for(index = 0; index < mCurrentFocusActors.size(); index++)
312 if(mCurrentFocusActors[index].first == mCurrentFocusedWindow)
314 actor = mCurrentFocusActors[index].second.GetHandle();
319 if(actor && !actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
321 // If the actor has been removed from the window, then the window doesn't have any focused actor
323 mCurrentFocusActors.erase(mCurrentFocusActors.begin() + index);
329 Actor KeyboardFocusManager::GetCurrentFocusGroup()
331 return GetFocusGroup(GetCurrentFocusActor());
334 void KeyboardFocusManager::MoveFocusBackward()
336 // Find Pre Focused Actor when the list size is more than 1
337 if(mFocusHistory.size() > 1)
339 // Delete current focused actor in history
340 mFocusHistory.pop_back();
342 // If pre-focused actors are not on stage or deleted, remove them in stack
343 while(mFocusHistory.size() > 0)
345 // Get pre focused actor
346 Actor target = mFocusHistory[mFocusHistory.size() - 1].GetHandle();
348 // Impl of Actor is not null
349 if(target && target.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
351 // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
352 mFocusHistory.pop_back();
353 SetCurrentFocusActor(target);
358 // Target is empty handle or off stage. Erase from queue
359 mFocusHistory.pop_back();
363 // if there is no actor which can get focus, then push current focus actor in stack again
364 if(mFocusHistory.size() == 0)
366 Actor currentFocusedActor = GetCurrentFocusActor();
367 mFocusHistory.push_back(currentFocusedActor);
372 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
374 Toolkit::Control control = Toolkit::Control::DownCast(actor);
375 return control && GetImplementation(control).IsKeyboardNavigationSupported();
378 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
380 // Get the actor's parent layout control that supports two dimensional keyboard navigation
385 Integration::SceneHolder window = Integration::SceneHolder::Get(actor);
388 rootActor = window.GetRootLayer();
391 parent = actor.GetParent();
394 while(parent && !IsLayoutControl(parent) && parent != rootActor)
396 parent = parent.GetParent();
399 return Toolkit::Control::DownCast(parent);
402 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
404 Actor currentFocusActor = GetCurrentFocusActor();
406 bool succeed = false;
408 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
409 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
410 while(parentLayoutControl && !succeed)
412 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
413 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
418 Actor nextFocusableActor;
420 Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
422 // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
423 if(currentFocusControl)
426 Property::Index index = Property::INVALID_INDEX;
427 Property::Value value;
429 // Find property index based upon focus direction
432 case Toolkit::Control::KeyboardFocus::LEFT:
434 index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
437 case Toolkit::Control::KeyboardFocus::RIGHT:
439 index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
442 case Toolkit::Control::KeyboardFocus::UP:
444 index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
447 case Toolkit::Control::KeyboardFocus::DOWN:
449 index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
456 // If the focusable property is set then determine next focusable actor
457 if(index != Property::INVALID_INDEX)
459 value = currentFocusActor.GetProperty(index);
460 actorId = value.Get<int>();
462 // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
465 if(currentFocusActor.GetParent())
467 nextFocusableActor = currentFocusActor.GetParent().FindChildById(actorId);
470 if(!nextFocusableActor)
472 Integration::SceneHolder window = Integration::SceneHolder::Get(currentFocusActor);
475 nextFocusableActor = window.GetRootLayer().FindChildById(actorId);
482 if(!nextFocusableActor)
484 // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
485 if(mCustomAlgorithmInterface)
487 mIsWaitingKeyboardFocusChangeCommit = true;
488 nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor(currentFocusActor, Actor(), direction);
489 mIsWaitingKeyboardFocusChangeCommit = false;
491 else if(!mPreFocusChangeSignal.Empty())
493 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
494 mIsWaitingKeyboardFocusChangeCommit = true;
495 nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
496 mIsWaitingKeyboardFocusChangeCommit = false;
500 if(nextFocusableActor && nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
502 // Whether the next focusable actor is a layout control
503 if(IsLayoutControl(nextFocusableActor))
505 // If so, move the focus inside it.
506 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
507 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
511 // Otherwise, just set focus to the next focusable actor
512 succeed = SetCurrentFocusActor(nextFocusableActor);
520 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
522 // Ask the control for the next actor to focus
523 Actor nextFocusableActor = GetImplementation(control).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
524 if(nextFocusableActor)
526 if(!nextFocusableActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
528 // If the actor is not focusable, ask the same layout control for the next actor to focus
529 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
533 Actor currentFocusActor = GetCurrentFocusActor();
534 Actor committedFocusActor = nextFocusableActor;
536 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
537 // Signal handler can check the proposed actor and return a different actor if it wishes.
538 if(!mPreFocusChangeSignal.Empty())
540 mIsWaitingKeyboardFocusChangeCommit = true;
541 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
542 mIsWaitingKeyboardFocusChangeCommit = false;
545 if(committedFocusActor && committedFocusActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE))
547 // Whether the commited focusable actor is a layout control
548 if(IsLayoutControl(committedFocusActor))
550 // If so, move the focus inside it.
551 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
552 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
556 // Otherwise, just set focus to the next focusable actor
557 if(committedFocusActor == nextFocusableActor)
559 // If the application hasn't changed our proposed actor, we informs the layout control we will
560 // move the focus to what the control returns. The control might wish to perform some actions
561 // before the focus is actually moved.
562 GetImplementation(control).OnKeyboardFocusChangeCommitted(committedFocusActor);
565 return SetCurrentFocusActor(committedFocusActor);
576 // No more actor can be focused in the given direction within the same layout control.
581 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
583 bool succeed = false;
585 // Get the parent layout control of the current focus group
586 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
588 while(parentLayoutControl && !succeed)
590 // If the current focus group has a parent layout control, we can probably automatically
591 // move the focus to the next focus group in the forward or backward direction.
592 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
593 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
594 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
597 if(!mFocusGroupChangedSignal.Empty())
599 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
600 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
606 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
610 Toolkit::Control control = Toolkit::Control::DownCast(actor);
613 // Notify the control that enter has been pressed on it.
614 GetImplementation(control).KeyboardEnter();
617 // Send a notification for the actor.
618 if(!mFocusedActorEnterKeySignal.Empty())
620 mFocusedActorEnterKeySignal.Emit(actor);
625 void KeyboardFocusManager::ClearFocus()
627 Actor actor = GetCurrentFocusActor();
630 if(mFocusIndicatorActor)
632 actor.Remove(mFocusIndicatorActor);
635 // Send notification for the change of focus actor
636 if(!mFocusChangedSignal.Empty())
638 mFocusChangedSignal.Emit(actor, Actor());
641 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(actor);
642 if(currentlyFocusedControl)
644 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
645 currentlyFocusedControl.ClearKeyInputFocus();
649 mCurrentFocusActor.Reset();
650 mIsFocusIndicatorShown = (mAlwaysShowIndicator == ALWAYS_SHOW) ? SHOW : HIDE;
653 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
655 mFocusGroupLoopEnabled = enabled;
658 bool KeyboardFocusManager::GetFocusGroupLoop() const
660 return mFocusGroupLoopEnabled;
663 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
667 // Create/Set focus group property.
668 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE);
672 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
674 // Check whether the actor is a focus group
675 bool isFocusGroup = false;
679 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
680 if(propertyIsFocusGroup != Property::INVALID_INDEX)
682 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
689 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
691 // Go through the actor's hierarchy to check which focus group the actor belongs to
692 while(actor && !IsFocusGroup(actor))
694 actor = actor.GetParent();
700 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
702 if(mFocusIndicatorActor != indicator)
704 Actor currentFocusActor = GetCurrentFocusActor();
705 if(currentFocusActor)
707 // The new focus indicator should be added to the current focused actor immediately
708 if(mFocusIndicatorActor)
710 currentFocusActor.Remove(mFocusIndicatorActor);
715 currentFocusActor.Add(indicator);
719 mFocusIndicatorActor = indicator;
723 Actor KeyboardFocusManager::GetFocusIndicatorActor()
725 if(!mFocusIndicatorActor)
727 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
728 const std::string imageDirPath = AssetManager::GetDaliImagePath();
729 mFocusIndicatorActor = Toolkit::ImageView::New(imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME);
731 // Apply size constraint to the focus indicator
732 mFocusIndicatorActor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
735 mFocusIndicatorActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
736 mFocusIndicatorActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
737 mFocusIndicatorActor.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
739 return mFocusIndicatorActor;
742 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
744 std::string keyName = event.GetKeyName();
746 if(mIsFocusIndicatorShown == UNKNOWN)
748 GetConfigurationFromStyleManger();
751 bool isFocusStartableKey = false;
753 if(event.GetState() == KeyEvent::DOWN)
755 if(keyName == "Left")
757 if(!mIsFocusIndicatorShown)
759 if(mIsFocusIndicatorShown == HIDE)
761 // Show focus indicator
762 mIsFocusIndicatorShown = SHOW;
766 // Move the focus towards left
767 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
770 isFocusStartableKey = true;
774 // Move the focus towards left
775 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
778 isFocusStartableKey = true;
780 else if(keyName == "Right")
782 if(!mIsFocusIndicatorShown)
784 if(mIsFocusIndicatorShown == HIDE)
786 // Show focus indicator
787 mIsFocusIndicatorShown = SHOW;
791 // Move the focus towards right
792 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
797 // Move the focus towards right
798 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
801 isFocusStartableKey = true;
803 else if(keyName == "Up")
805 if(mIsFocusIndicatorShown == HIDE)
807 // Show focus indicator
808 mIsFocusIndicatorShown = SHOW;
812 // Move the focus towards up
813 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
816 isFocusStartableKey = true;
818 else if(keyName == "Down")
820 if(mIsFocusIndicatorShown == HIDE)
822 // Show focus indicator
823 mIsFocusIndicatorShown = SHOW;
827 // Move the focus towards down
828 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
831 isFocusStartableKey = true;
833 else if(keyName == "Prior")
835 if(mIsFocusIndicatorShown == HIDE)
837 // Show focus indicator
838 mIsFocusIndicatorShown = SHOW;
842 // Move the focus towards the previous page
843 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
846 isFocusStartableKey = true;
848 else if(keyName == "Next")
850 if(mIsFocusIndicatorShown == HIDE)
852 // Show focus indicator
853 mIsFocusIndicatorShown = SHOW;
857 // Move the focus towards the next page
858 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
861 isFocusStartableKey = true;
863 else if(keyName == "Tab")
865 if(mIsFocusIndicatorShown == HIDE)
867 // Show focus indicator
868 mIsFocusIndicatorShown = SHOW;
872 // "Tab" key changes the focus group in the forward direction and
873 // "Shift-Tab" key changes it in the backward direction.
874 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
877 isFocusStartableKey = true;
879 else if(keyName == "space")
881 if(mIsFocusIndicatorShown == HIDE)
883 // Show focus indicator
884 mIsFocusIndicatorShown = SHOW;
887 isFocusStartableKey = true;
889 else if(keyName == "")
891 // Check the fake key event for evas-plugin case
892 if(mIsFocusIndicatorShown == HIDE)
894 // Show focus indicator
895 mIsFocusIndicatorShown = SHOW;
898 isFocusStartableKey = true;
900 else if(keyName == "Backspace")
902 // Emit signal to go back to the previous view???
904 else if(keyName == "Escape")
908 else if(event.GetState() == KeyEvent::UP)
910 if(keyName == "Return")
912 if(mIsFocusIndicatorShown == HIDE)
914 // Show focus indicator
915 mIsFocusIndicatorShown = SHOW;
919 // The focused actor has enter pressed on it
920 Actor actor = GetCurrentFocusActor();
923 DoKeyboardEnter(actor);
927 isFocusStartableKey = true;
931 if(isFocusStartableKey && mIsFocusIndicatorShown == SHOW)
933 Actor actor = GetCurrentFocusActor();
936 if(mEnableFocusIndicator == ENABLE)
938 // Make sure the focused actor is highlighted
939 actor.Add(GetFocusIndicatorActor());
944 // No actor is focused but keyboard focus is activated by the key press
945 // Let's try to move the initial focus
946 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
951 void KeyboardFocusManager::OnTouch(const TouchEvent& touch)
953 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
954 // Try to load configuration.
955 if(mIsFocusIndicatorShown == UNKNOWN)
957 GetConfigurationFromStyleManger();
960 // Clear the focus when user touch the screen.
961 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
962 // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
963 if(((touch.GetPointCount() < 1) || (touch.GetState(0) == PointState::DOWN)) && mClearFocusOnTouch)
969 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn)
971 if(focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer())
973 // Change Current Focused Window
974 Layer rootLayer = window.GetRootLayer();
975 mCurrentFocusedWindow = rootLayer;
977 // Get Current Focused Actor from window
978 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
979 SetCurrentFocusActor(currentFocusedActor);
981 if(currentFocusedActor && (mEnableFocusIndicator == ENABLE))
983 // Make sure the focused actor is highlighted
984 currentFocusedActor.Add(GetFocusIndicatorActor());
985 mIsFocusIndicatorShown = SHOW;
990 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
992 return mPreFocusChangeSignal;
995 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
997 return mFocusChangedSignal;
1000 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1002 return mFocusGroupChangedSignal;
1005 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1007 return mFocusedActorEnterKeySignal;
1010 bool KeyboardFocusManager::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
1012 Dali::BaseHandle handle(object);
1014 bool connected(true);
1015 KeyboardFocusManager* manager = static_cast<KeyboardFocusManager*>(object); // TypeRegistry guarantees that this is the correct type.
1017 if(0 == strcmp(signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE))
1019 manager->PreFocusChangeSignal().Connect(tracker, functor);
1021 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_CHANGED))
1023 manager->FocusChangedSignal().Connect(tracker, functor);
1025 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED))
1027 manager->FocusGroupChangedSignal().Connect(tracker, functor);
1029 else if(0 == strcmp(signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY))
1031 manager->FocusedActorEnterKeySignal().Connect(tracker, functor);
1035 // signalName does not match any signal
1042 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1044 mCustomAlgorithmInterface = &interface;
1047 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1049 if(!enable && mFocusIndicatorActor)
1051 mFocusIndicatorActor.Unparent();
1054 mEnableFocusIndicator = enable ? ENABLE : DISABLE;
1057 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1059 return (mEnableFocusIndicator == ENABLE);
1062 } // namespace Internal
1064 } // namespace Toolkit