2 * Copyright (c) 2020 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 <cstring> // for strcmp
23 #include <dali/public-api/actors/layer.h>
24 #include <dali/devel-api/adaptor-framework/accessibility-adaptor.h>
25 #include <dali/devel-api/common/singleton-service.h>
26 #include <dali/devel-api/adaptor-framework/lifecycle-controller.h>
27 #include <dali/public-api/animation/constraints.h>
28 #include <dali/public-api/events/key-event.h>
29 #include <dali/public-api/events/touch-data.h>
30 #include <dali/public-api/object/type-registry.h>
31 #include <dali/public-api/object/type-registry-helper.h>
32 #include <dali/public-api/object/property-map.h>
33 #include <dali/integration-api/debug.h>
34 #include <dali/integration-api/adaptor-framework/adaptor.h>
35 #include <dali/integration-api/adaptor-framework/scene-holder.h>
38 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
39 #include <dali-toolkit/public-api/controls/control.h>
40 #include <dali-toolkit/public-api/controls/control-impl.h>
41 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
42 #include <dali-toolkit/devel-api/accessibility-manager/accessibility-manager.h>
43 #include <dali-toolkit/devel-api/controls/control-devel.h>
44 #include <dali-toolkit/public-api/styling/style-manager.h>
45 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
56 namespace // Unnamed namespace
59 #if defined(DEBUG_ENABLED)
60 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
63 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
65 const char* const FOCUS_BORDER_IMAGE_FILE_NAME = "keyboard_focus.9.png";
69 BaseHandle handle = KeyboardFocusManager::Get();
73 SingletonService singletonService( SingletonService::Get() );
74 if ( singletonService )
76 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
77 singletonService.Register( typeid( manager ), manager );
85 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true )
87 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardPreFocusChange", SIGNAL_PRE_FOCUS_CHANGE )
88 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusChanged", SIGNAL_FOCUS_CHANGED )
89 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged", SIGNAL_FOCUS_GROUP_CHANGED )
90 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey", SIGNAL_FOCUSED_ACTOR_ENTER_KEY )
92 DALI_TYPE_REGISTRATION_END()
94 const unsigned int MAX_HISTORY_AMOUNT = 30; ///< Max length of focus history stack
96 } // unnamed namespace
98 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
100 Toolkit::KeyboardFocusManager manager;
102 SingletonService singletonService( SingletonService::Get() );
103 if ( singletonService )
105 // Check whether the keyboard focus manager is already created
106 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
109 // If so, downcast the handle of singleton to keyboard focus manager
110 manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
117 KeyboardFocusManager::KeyboardFocusManager()
118 : mPreFocusChangeSignal(),
119 mFocusChangedSignal(),
120 mFocusGroupChangedSignal(),
121 mFocusedActorEnterKeySignal(),
122 mCurrentFocusActor(),
123 mFocusIndicatorActor(),
125 mSlotDelegate( this ),
126 mCustomAlgorithmInterface(NULL),
127 mCurrentFocusedWindow(),
128 mIsFocusIndicatorShown( UNKNOWN ),
129 mEnableFocusIndicator( ENABLE ),
130 mAlwaysShowIndicator( ALWAYS_SHOW ),
131 mFocusGroupLoopEnabled( false ),
132 mIsWaitingKeyboardFocusChangeCommit( false ),
133 mClearFocusOnTouch( true )
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 ).TouchSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
150 Dali::Window window = DevelWindow::DownCast( *iter );
153 window.FocusChangeSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
157 // Get notified when any new scene holder is created afterwards
158 Adaptor::Get().WindowCreatedSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnSceneHolderCreated );
162 void KeyboardFocusManager::OnSceneHolderCreated( Dali::Integration::SceneHolder& sceneHolder )
164 sceneHolder.KeyEventSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnKeyEvent );
165 sceneHolder.TouchSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
166 Dali::Window window = DevelWindow::DownCast( sceneHolder );
169 window.FocusChangeSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
173 KeyboardFocusManager::~KeyboardFocusManager()
177 void KeyboardFocusManager::GetConfigurationFromStyleManger()
179 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
182 Property::Map config = Toolkit::DevelStyleManager::GetConfigurations( styleManager );
183 mAlwaysShowIndicator = config["alwaysShowFocus"].Get<bool>() ? ALWAYS_SHOW : NONE;
184 mIsFocusIndicatorShown = ( mAlwaysShowIndicator == ALWAYS_SHOW )? SHOW : HIDE;
185 mClearFocusOnTouch = ( mIsFocusIndicatorShown == SHOW ) ? false : true;
189 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
191 DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
193 if( mIsFocusIndicatorShown == UNKNOWN )
195 GetConfigurationFromStyleManger();
198 return DoSetCurrentFocusActor( actor );
201 bool KeyboardFocusManager::DoSetCurrentFocusActor( Actor actor )
203 bool success = false;
204 if( actor && actor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ) && actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
206 Integration::SceneHolder currentWindow = Integration::SceneHolder::Get( actor );
208 if( currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
210 Layer rootLayer = currentWindow.GetRootLayer();
211 mCurrentFocusedWindow = rootLayer;
215 Actor currentFocusedActor = GetCurrentFocusActor();
217 // If developer set focus on same actor, doing nothing
218 if( actor == currentFocusedActor )
227 // Check whether the actor is in the stage and is keyboard focusable.
228 if( actor && actor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ) && actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
230 if( ( mIsFocusIndicatorShown == SHOW ) && ( mEnableFocusIndicator == ENABLE ) )
232 actor.Add( GetFocusIndicatorActor() );
235 // Send notification for the change of focus actor
236 if( !mFocusChangedSignal.Empty() )
238 mFocusChangedSignal.Emit(currentFocusedActor, actor);
241 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
242 if( currentlyFocusedControl )
244 // Do we need it to remember if it was previously DISABLED?
245 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL );
246 currentlyFocusedControl.ClearKeyInputFocus();
249 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
251 // Save the current focused actor
252 mCurrentFocusActor = actor;
254 bool focusedWindowFound = false;
255 for( unsigned int i = 0; i < mCurrentFocusActors.size(); i++ )
257 if( mCurrentFocusActors[i].first == mCurrentFocusedWindow )
259 mCurrentFocusActors[i].second = actor;
260 focusedWindowFound = true;
264 if( !focusedWindowFound)
266 // A new window gains the focus, so store the focused actor in that window.
267 mCurrentFocusActors.push_back( std::pair< WeakHandle< Layer>, WeakHandle< Actor > >( mCurrentFocusedWindow , actor ));
270 Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
271 if( newlyFocusedControl )
273 newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED );
274 newlyFocusedControl.SetKeyInputFocus();
277 // Push Current Focused Actor to FocusHistory
278 mFocusHistory.push_back( actor );
280 // Delete first element before add new element when Stack is full.
281 if( mFocusHistory.size() > MAX_HISTORY_AMOUNT )
283 FocusStackIterator beginPos = mFocusHistory.begin();
284 mFocusHistory.erase( beginPos );
287 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
292 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
298 Actor KeyboardFocusManager::GetCurrentFocusActor()
300 Actor actor = mCurrentFocusActor.GetHandle();
302 if( actor && ! actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
304 // If the actor has been removed from the stage, then it should not be focused
306 mCurrentFocusActor.Reset();
311 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
315 for( index = 0; index < mCurrentFocusActors.size(); index++ )
317 if( mCurrentFocusActors[index].first == mCurrentFocusedWindow )
319 actor = mCurrentFocusActors[index].second.GetHandle();
324 if( actor && ! actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
326 // If the actor has been removed from the window, then the window doesn't have any focused actor
328 mCurrentFocusActors.erase( mCurrentFocusActors.begin() + index );
334 Actor KeyboardFocusManager::GetCurrentFocusGroup()
336 return GetFocusGroup(GetCurrentFocusActor());
339 void KeyboardFocusManager::MoveFocusBackward()
341 // Find Pre Focused Actor when the list size is more than 1
342 if( mFocusHistory.size() > 1 )
344 // Delete current focused actor in history
345 mFocusHistory.pop_back();
347 // If pre-focused actors are not on stage or deleted, remove them in stack
348 while( mFocusHistory.size() > 0 )
350 // Get pre focused actor
351 Actor target = mFocusHistory[ mFocusHistory.size() -1 ].GetHandle();
353 // Impl of Actor is not null
354 if( target && target.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
356 // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
357 mFocusHistory.pop_back();
358 SetCurrentFocusActor( target );
363 // Target is empty handle or off stage. Erase from queue
364 mFocusHistory.pop_back();
368 // if there is no actor which can get focus, then push current focus actor in stack again
369 if( mFocusHistory.size() == 0 )
371 Actor currentFocusedActor = GetCurrentFocusActor();
372 mFocusHistory.push_back( currentFocusedActor );
377 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
379 Toolkit::Control control = Toolkit::Control::DownCast(actor);
380 return control && GetImplementation( control ).IsKeyboardNavigationSupported();
383 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
385 // Get the actor's parent layout control that supports two dimensional keyboard navigation
390 Integration::SceneHolder window = Integration::SceneHolder::Get( actor );
393 rootActor = window.GetRootLayer();
396 parent = actor.GetParent();
399 while( parent && !IsLayoutControl(parent) && parent != rootActor )
401 parent = parent.GetParent();
404 return Toolkit::Control::DownCast(parent);
407 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
409 Actor currentFocusActor = GetCurrentFocusActor();
411 bool succeed = false;
413 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
414 Toolkit::Control parentLayoutControl = GetParentLayoutControl( currentFocusActor );
415 while( parentLayoutControl && !succeed )
417 succeed = DoMoveFocusWithinLayoutControl( parentLayoutControl, currentFocusActor, direction );
418 parentLayoutControl = GetParentLayoutControl( parentLayoutControl );
423 Actor nextFocusableActor;
425 Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
427 // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
428 if( currentFocusControl )
431 Property::Index index = Property::INVALID_INDEX;
432 Property::Value value;
434 // Find property index based upon focus direction
437 case Toolkit::Control::KeyboardFocus::LEFT:
439 index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
442 case Toolkit::Control::KeyboardFocus::RIGHT:
444 index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
447 case Toolkit::Control::KeyboardFocus::UP:
449 index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
452 case Toolkit::Control::KeyboardFocus::DOWN:
454 index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
461 // If the focusable property is set then determine next focusable actor
462 if( index != Property::INVALID_INDEX)
464 value = currentFocusActor.GetProperty( index );
465 actorId = value.Get<int>();
467 // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
470 if( currentFocusActor.GetParent() )
472 nextFocusableActor = currentFocusActor.GetParent().FindChildById( actorId );
475 if( !nextFocusableActor )
477 Integration::SceneHolder window = Integration::SceneHolder::Get( currentFocusActor );
480 nextFocusableActor = window.GetRootLayer().FindChildById( actorId );
487 if( !nextFocusableActor )
489 // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
490 if( mCustomAlgorithmInterface )
492 mIsWaitingKeyboardFocusChangeCommit = true;
493 nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor( currentFocusActor, Actor(), direction );
494 mIsWaitingKeyboardFocusChangeCommit = false;
496 else if( !mPreFocusChangeSignal.Empty() )
498 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
499 mIsWaitingKeyboardFocusChangeCommit = true;
500 nextFocusableActor = mPreFocusChangeSignal.Emit( currentFocusActor, Actor(), direction );
501 mIsWaitingKeyboardFocusChangeCommit = false;
505 if( nextFocusableActor && nextFocusableActor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ) )
507 // Whether the next focusable actor is a layout control
508 if( IsLayoutControl( nextFocusableActor ) )
510 // If so, move the focus inside it.
511 Toolkit::Control layoutControl = Toolkit::Control::DownCast( nextFocusableActor) ;
512 succeed = DoMoveFocusWithinLayoutControl( layoutControl, currentFocusActor, direction );
516 // Otherwise, just set focus to the next focusable actor
517 succeed = SetCurrentFocusActor( nextFocusableActor );
525 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
527 // Ask the control for the next actor to focus
528 Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
529 if(nextFocusableActor)
531 if(!nextFocusableActor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ))
533 // If the actor is not focusable, ask the same layout control for the next actor to focus
534 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
538 Actor currentFocusActor = GetCurrentFocusActor();
539 Actor committedFocusActor = nextFocusableActor;
541 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
542 // Signal handler can check the proposed actor and return a different actor if it wishes.
543 if( !mPreFocusChangeSignal.Empty() )
545 mIsWaitingKeyboardFocusChangeCommit = true;
546 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
547 mIsWaitingKeyboardFocusChangeCommit = false;
550 if (committedFocusActor && committedFocusActor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ))
552 // Whether the commited focusable actor is a layout control
553 if(IsLayoutControl(committedFocusActor))
555 // If so, move the focus inside it.
556 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
557 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
561 // Otherwise, just set focus to the next focusable actor
562 if(committedFocusActor == nextFocusableActor)
564 // If the application hasn't changed our proposed actor, we informs the layout control we will
565 // move the focus to what the control returns. The control might wish to perform some actions
566 // before the focus is actually moved.
567 GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
570 return SetCurrentFocusActor(committedFocusActor);
581 // No more actor can be focused in the given direction within the same layout control.
586 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
588 bool succeed = false;
590 // Get the parent layout control of the current focus group
591 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
593 while(parentLayoutControl && !succeed)
595 // If the current focus group has a parent layout control, we can probably automatically
596 // move the focus to the next focus group in the forward or backward direction.
597 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
598 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
599 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
602 if(!mFocusGroupChangedSignal.Empty())
604 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
605 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
611 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
615 Toolkit::Control control = Toolkit::Control::DownCast( actor );
618 // Notify the control that enter has been pressed on it.
619 GetImplementation( control ).KeyboardEnter();
622 // Send a notification for the actor.
623 if( !mFocusedActorEnterKeySignal.Empty() )
625 mFocusedActorEnterKeySignal.Emit( actor );
630 void KeyboardFocusManager::ClearFocus()
632 Actor actor = GetCurrentFocusActor();
635 if( mFocusIndicatorActor )
637 actor.Remove( mFocusIndicatorActor );
640 // Send notification for the change of focus actor
641 if( !mFocusChangedSignal.Empty() )
643 mFocusChangedSignal.Emit( actor, Actor() );
646 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast( actor );
647 if( currentlyFocusedControl )
649 currentlyFocusedControl.SetProperty( DevelControl::Property::STATE, DevelControl::NORMAL );
650 currentlyFocusedControl.ClearKeyInputFocus();
654 mCurrentFocusActor.Reset();
655 mIsFocusIndicatorShown = ( mAlwaysShowIndicator == ALWAYS_SHOW ) ? SHOW : HIDE;
658 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
660 mFocusGroupLoopEnabled = enabled;
663 bool KeyboardFocusManager::GetFocusGroupLoop() const
665 return mFocusGroupLoopEnabled;
668 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
672 // Create/Set focus group property.
673 actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
677 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
679 // Check whether the actor is a focus group
680 bool isFocusGroup = false;
684 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
685 if(propertyIsFocusGroup != Property::INVALID_INDEX)
687 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
694 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
696 // Go through the actor's hierarchy to check which focus group the actor belongs to
697 while (actor && !IsFocusGroup(actor))
699 actor = actor.GetParent();
705 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
707 if(mFocusIndicatorActor != indicator)
709 Actor currentFocusActor = GetCurrentFocusActor();
710 if(currentFocusActor)
712 // The new focus indicator should be added to the current focused actor immediately
713 if(mFocusIndicatorActor)
715 currentFocusActor.Remove(mFocusIndicatorActor);
720 currentFocusActor.Add(indicator);
724 mFocusIndicatorActor = indicator;
728 Actor KeyboardFocusManager::GetFocusIndicatorActor()
730 if( ! mFocusIndicatorActor )
732 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
733 const std::string imageDirPath = AssetManager::GetDaliImagePath();
734 mFocusIndicatorActor = Toolkit::ImageView::New( imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME );
736 // Apply size constraint to the focus indicator
737 mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
740 mFocusIndicatorActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
741 mFocusIndicatorActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
742 mFocusIndicatorActor.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 0.0f));
744 return mFocusIndicatorActor;
747 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
749 AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
750 bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
752 Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
754 std::string keyName = event.keyPressedName;
756 if( mIsFocusIndicatorShown == UNKNOWN )
758 GetConfigurationFromStyleManger();
761 bool isFocusStartableKey = false;
763 if(event.state == KeyEvent::Down)
765 if (keyName == "Left")
767 if(!isAccessibilityEnabled)
769 if(mIsFocusIndicatorShown == HIDE)
771 // Show focus indicator
772 mIsFocusIndicatorShown = SHOW;
776 // Move the focus towards left
777 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
780 isFocusStartableKey = true;
784 // Move the accessibility focus backward
785 accessibilityManager.MoveFocusBackward();
788 else if (keyName == "Right")
790 if(!isAccessibilityEnabled)
792 if( mIsFocusIndicatorShown == HIDE )
794 // Show focus indicator
795 mIsFocusIndicatorShown = SHOW;
799 // Move the focus towards right
800 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
805 // Move the accessibility focus forward
806 accessibilityManager.MoveFocusForward();
809 isFocusStartableKey = true;
811 else if (keyName == "Up" && !isAccessibilityEnabled)
813 if( mIsFocusIndicatorShown == HIDE )
815 // Show focus indicator
816 mIsFocusIndicatorShown = SHOW;
820 // Move the focus towards up
821 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
824 isFocusStartableKey = true;
826 else if (keyName == "Down" && !isAccessibilityEnabled)
828 if( mIsFocusIndicatorShown == HIDE )
830 // Show focus indicator
831 mIsFocusIndicatorShown = SHOW;
835 // Move the focus towards down
836 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
839 isFocusStartableKey = true;
841 else if (keyName == "Prior" && !isAccessibilityEnabled)
843 if( mIsFocusIndicatorShown == HIDE )
845 // Show focus indicator
846 mIsFocusIndicatorShown = SHOW;
850 // Move the focus towards the previous page
851 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
854 isFocusStartableKey = true;
856 else if (keyName == "Next" && !isAccessibilityEnabled)
858 if( mIsFocusIndicatorShown == HIDE )
860 // Show focus indicator
861 mIsFocusIndicatorShown = SHOW;
865 // Move the focus towards the next page
866 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
869 isFocusStartableKey = true;
871 else if (keyName == "Tab" && !isAccessibilityEnabled)
873 if( mIsFocusIndicatorShown == HIDE )
875 // Show focus indicator
876 mIsFocusIndicatorShown = SHOW;
880 // "Tab" key changes the focus group in the forward direction and
881 // "Shift-Tab" key changes it in the backward direction.
882 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
885 isFocusStartableKey = true;
887 else if (keyName == "space" && !isAccessibilityEnabled)
889 if( mIsFocusIndicatorShown == HIDE )
891 // Show focus indicator
892 mIsFocusIndicatorShown = SHOW;
895 isFocusStartableKey = true;
897 else if (keyName == "" && !isAccessibilityEnabled)
899 // Check the fake key event for evas-plugin case
900 if( mIsFocusIndicatorShown == HIDE )
902 // Show focus indicator
903 mIsFocusIndicatorShown = SHOW;
906 isFocusStartableKey = true;
908 else if (keyName == "Backspace" && !isAccessibilityEnabled)
910 // Emit signal to go back to the previous view???
912 else if (keyName == "Escape" && !isAccessibilityEnabled)
916 else if(event.state == KeyEvent::Up)
918 if (keyName == "Return")
920 if((mIsFocusIndicatorShown == HIDE) && !isAccessibilityEnabled)
922 // Show focus indicator
923 mIsFocusIndicatorShown = SHOW;
927 // The focused actor has enter pressed on it
929 if( !isAccessibilityEnabled )
931 actor = GetCurrentFocusActor();
935 actor = accessibilityManager.GetCurrentFocusActor();
940 DoKeyboardEnter( actor );
944 isFocusStartableKey = true;
948 if(isFocusStartableKey && ( mIsFocusIndicatorShown == SHOW ) && !isAccessibilityEnabled)
950 Actor actor = GetCurrentFocusActor();
953 if( mEnableFocusIndicator == ENABLE )
955 // Make sure the focused actor is highlighted
956 actor.Add( GetFocusIndicatorActor() );
961 // No actor is focused but keyboard focus is activated by the key press
962 // Let's try to move the initial focus
963 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
968 void KeyboardFocusManager::OnTouch(const TouchData& touch)
970 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
971 // Try to load configuration.
972 if( mIsFocusIndicatorShown == UNKNOWN )
974 GetConfigurationFromStyleManger();
977 // Clear the focus when user touch the screen.
978 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
979 // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
980 if( (( touch.GetPointCount() < 1 ) || ( touch.GetState( 0 ) == PointState::DOWN )) && mClearFocusOnTouch )
986 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn )
988 if( focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer() )
990 // Change Current Focused Window
991 Layer rootLayer = window.GetRootLayer();
992 mCurrentFocusedWindow = rootLayer;
994 // Get Current Focused Actor from window
995 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
996 SetCurrentFocusActor( currentFocusedActor );
998 if( currentFocusedActor && ( mEnableFocusIndicator == ENABLE ) )
1000 // Make sure the focused actor is highlighted
1001 currentFocusedActor.Add( GetFocusIndicatorActor() );
1002 mIsFocusIndicatorShown = SHOW;
1007 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
1009 return mPreFocusChangeSignal;
1012 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1014 return mFocusChangedSignal;
1017 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1019 return mFocusGroupChangedSignal;
1022 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1024 return mFocusedActorEnterKeySignal;
1027 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1029 Dali::BaseHandle handle( object );
1031 bool connected( true );
1032 KeyboardFocusManager* manager = static_cast< KeyboardFocusManager* >( object ); // TypeRegistry guarantees that this is the correct type.
1034 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
1036 manager->PreFocusChangeSignal().Connect( tracker, functor );
1038 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
1040 manager->FocusChangedSignal().Connect( tracker, functor );
1042 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
1044 manager->FocusGroupChangedSignal().Connect( tracker, functor );
1046 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
1048 manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
1052 // signalName does not match any signal
1059 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1061 mCustomAlgorithmInterface = &interface;
1064 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1066 if( !enable && mFocusIndicatorActor )
1068 mFocusIndicatorActor.Unparent();
1071 mEnableFocusIndicator = enable? ENABLE : DISABLE;
1075 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1077 return ( mEnableFocusIndicator == ENABLE );
1080 } // namespace Internal
1082 } // namespace Toolkit