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/actors/actor-devel.h>
25 #include <dali/devel-api/adaptor-framework/accessibility-adaptor.h>
26 #include <dali/devel-api/common/singleton-service.h>
27 #include <dali/devel-api/adaptor-framework/lifecycle-controller.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/type-registry.h>
32 #include <dali/public-api/object/type-registry-helper.h>
33 #include <dali/public-api/object/property-map.h>
34 #include <dali/integration-api/debug.h>
35 #include <dali/integration-api/adaptor-framework/adaptor.h>
36 #include <dali/integration-api/adaptor-framework/scene-holder.h>
39 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
40 #include <dali-toolkit/public-api/controls/control.h>
41 #include <dali-toolkit/public-api/controls/control-impl.h>
42 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
43 #include <dali-toolkit/devel-api/accessibility-manager/accessibility-manager.h>
44 #include <dali-toolkit/devel-api/controls/control-devel.h>
45 #include <dali-toolkit/devel-api/focus-manager/focus-finder.h>
46 #include <dali-toolkit/public-api/styling/style-manager.h>
47 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
58 namespace // Unnamed namespace
61 #if defined(DEBUG_ENABLED)
62 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
65 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
67 const char* const FOCUS_BORDER_IMAGE_FILE_NAME = "keyboard_focus.9.png";
71 BaseHandle handle = KeyboardFocusManager::Get();
75 SingletonService singletonService( SingletonService::Get() );
76 if ( singletonService )
78 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
79 singletonService.Register( typeid( manager ), manager );
87 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true )
89 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardPreFocusChange", SIGNAL_PRE_FOCUS_CHANGE )
90 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusChanged", SIGNAL_FOCUS_CHANGED )
91 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged", SIGNAL_FOCUS_GROUP_CHANGED )
92 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey", SIGNAL_FOCUSED_ACTOR_ENTER_KEY )
94 DALI_TYPE_REGISTRATION_END()
96 const unsigned int MAX_HISTORY_AMOUNT = 30; ///< Max length of focus history stack
98 } // unnamed namespace
100 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
102 Toolkit::KeyboardFocusManager manager;
104 SingletonService singletonService( SingletonService::Get() );
105 if ( singletonService )
107 // Check whether the keyboard focus manager is already created
108 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
111 // If so, downcast the handle of singleton to keyboard focus manager
112 manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
119 KeyboardFocusManager::KeyboardFocusManager()
120 : mPreFocusChangeSignal(),
121 mFocusChangedSignal(),
122 mFocusGroupChangedSignal(),
123 mFocusedActorEnterKeySignal(),
124 mCurrentFocusActor(),
125 mFocusIndicatorActor(),
127 mSlotDelegate( this ),
128 mCustomAlgorithmInterface(NULL),
129 mCurrentFocusedWindow(),
130 mIsFocusIndicatorShown( UNKNOWN ),
131 mEnableFocusIndicator( ENABLE ),
132 mAlwaysShowIndicator( ALWAYS_SHOW ),
133 mFocusGroupLoopEnabled( false ),
134 mIsWaitingKeyboardFocusChangeCommit( false ),
135 mClearFocusOnTouch( true ),
136 mEnableDefaultAlgorithm(false)
138 // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorShown.
140 LifecycleController::Get().InitSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnAdaptorInit );
143 void KeyboardFocusManager::OnAdaptorInit()
145 if( Adaptor::IsAvailable() )
147 // Retrieve all the existing scene holders
148 Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
149 for( auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter )
151 ( *iter ).KeyEventSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnKeyEvent );
152 ( *iter ).TouchedSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
153 Dali::Window window = DevelWindow::DownCast( *iter );
156 window.FocusChangeSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
160 // Get notified when any new scene holder is created afterwards
161 Adaptor::Get().WindowCreatedSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnSceneHolderCreated );
165 void KeyboardFocusManager::OnSceneHolderCreated( Dali::Integration::SceneHolder& sceneHolder )
167 sceneHolder.KeyEventSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnKeyEvent );
168 sceneHolder.TouchedSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
169 Dali::Window window = DevelWindow::DownCast( sceneHolder );
172 window.FocusChangeSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
176 KeyboardFocusManager::~KeyboardFocusManager()
180 void KeyboardFocusManager::GetConfigurationFromStyleManger()
182 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
185 Property::Map config = Toolkit::DevelStyleManager::GetConfigurations( styleManager );
186 mAlwaysShowIndicator = config["alwaysShowFocus"].Get<bool>() ? ALWAYS_SHOW : NONE;
187 mIsFocusIndicatorShown = ( mAlwaysShowIndicator == ALWAYS_SHOW )? SHOW : HIDE;
188 mClearFocusOnTouch = ( mIsFocusIndicatorShown == SHOW ) ? false : true;
192 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
194 DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
196 if( mIsFocusIndicatorShown == UNKNOWN )
198 GetConfigurationFromStyleManger();
201 return DoSetCurrentFocusActor( actor );
204 bool KeyboardFocusManager::DoSetCurrentFocusActor( Actor actor )
206 bool success = false;
207 if( actor && actor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ) && actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
209 Integration::SceneHolder currentWindow = Integration::SceneHolder::Get( actor );
211 if( currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
213 Layer rootLayer = currentWindow.GetRootLayer();
214 mCurrentFocusedWindow = rootLayer;
218 Actor currentFocusedActor = GetCurrentFocusActor();
220 // If developer set focus on same actor, doing nothing
221 if( actor == currentFocusedActor )
230 // Check whether the actor is in the stage and is keyboard focusable.
231 if( actor && actor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ) && actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
233 if( ( mIsFocusIndicatorShown == SHOW ) && ( mEnableFocusIndicator == ENABLE ) )
235 actor.Add( GetFocusIndicatorActor() );
238 // Send notification for the change of focus actor
239 if( !mFocusChangedSignal.Empty() )
241 mFocusChangedSignal.Emit(currentFocusedActor, actor);
244 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
245 if( currentlyFocusedControl )
247 // Do we need it to remember if it was previously DISABLED?
248 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL );
249 currentlyFocusedControl.ClearKeyInputFocus();
252 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
254 // Save the current focused actor
255 mCurrentFocusActor = actor;
257 bool focusedWindowFound = false;
258 for( unsigned int i = 0; i < mCurrentFocusActors.size(); i++ )
260 if( mCurrentFocusActors[i].first == mCurrentFocusedWindow )
262 mCurrentFocusActors[i].second = actor;
263 focusedWindowFound = true;
267 if( !focusedWindowFound)
269 // A new window gains the focus, so store the focused actor in that window.
270 mCurrentFocusActors.push_back( std::pair< WeakHandle< Layer>, WeakHandle< Actor > >( mCurrentFocusedWindow , actor ));
273 Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
274 if( newlyFocusedControl )
276 newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED );
277 newlyFocusedControl.SetKeyInputFocus();
280 // Push Current Focused Actor to FocusHistory
281 mFocusHistory.push_back( actor );
283 // Delete first element before add new element when Stack is full.
284 if( mFocusHistory.size() > MAX_HISTORY_AMOUNT )
286 FocusStackIterator beginPos = mFocusHistory.begin();
287 mFocusHistory.erase( beginPos );
290 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
295 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
301 Actor KeyboardFocusManager::GetCurrentFocusActor()
303 Actor actor = mCurrentFocusActor.GetHandle();
305 if( actor && ! actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
307 // If the actor has been removed from the stage, then it should not be focused
309 mCurrentFocusActor.Reset();
314 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
318 for( index = 0; index < mCurrentFocusActors.size(); index++ )
320 if( mCurrentFocusActors[index].first == mCurrentFocusedWindow )
322 actor = mCurrentFocusActors[index].second.GetHandle();
327 if( actor && ! actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
329 // If the actor has been removed from the window, then the window doesn't have any focused actor
331 mCurrentFocusActors.erase( mCurrentFocusActors.begin() + index );
337 Actor KeyboardFocusManager::GetCurrentFocusGroup()
339 return GetFocusGroup(GetCurrentFocusActor());
342 void KeyboardFocusManager::MoveFocusBackward()
344 // Find Pre Focused Actor when the list size is more than 1
345 if( mFocusHistory.size() > 1 )
347 // Delete current focused actor in history
348 mFocusHistory.pop_back();
350 // If pre-focused actors are not on stage or deleted, remove them in stack
351 while( mFocusHistory.size() > 0 )
353 // Get pre focused actor
354 Actor target = mFocusHistory[ mFocusHistory.size() -1 ].GetHandle();
356 // Impl of Actor is not null
357 if( target && target.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
359 // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
360 mFocusHistory.pop_back();
361 SetCurrentFocusActor( target );
366 // Target is empty handle or off stage. Erase from queue
367 mFocusHistory.pop_back();
371 // if there is no actor which can get focus, then push current focus actor in stack again
372 if( mFocusHistory.size() == 0 )
374 Actor currentFocusedActor = GetCurrentFocusActor();
375 mFocusHistory.push_back( currentFocusedActor );
380 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
382 Toolkit::Control control = Toolkit::Control::DownCast(actor);
383 return control && GetImplementation( control ).IsKeyboardNavigationSupported();
386 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
388 // Get the actor's parent layout control that supports two dimensional keyboard navigation
393 Integration::SceneHolder window = Integration::SceneHolder::Get( actor );
396 rootActor = window.GetRootLayer();
399 parent = actor.GetParent();
402 while( parent && !IsLayoutControl(parent) && parent != rootActor )
404 parent = parent.GetParent();
407 return Toolkit::Control::DownCast(parent);
410 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
412 Actor currentFocusActor = GetCurrentFocusActor();
414 bool succeed = false;
416 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
417 Toolkit::Control parentLayoutControl = GetParentLayoutControl( currentFocusActor );
418 while( parentLayoutControl && !succeed )
420 succeed = DoMoveFocusWithinLayoutControl( parentLayoutControl, currentFocusActor, direction );
421 parentLayoutControl = GetParentLayoutControl( parentLayoutControl );
426 Actor nextFocusableActor;
428 Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
430 // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
431 if( currentFocusControl )
434 Property::Index index = Property::INVALID_INDEX;
435 Property::Value value;
437 // Find property index based upon focus direction
440 case Toolkit::Control::KeyboardFocus::LEFT:
442 index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
445 case Toolkit::Control::KeyboardFocus::RIGHT:
447 index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
450 case Toolkit::Control::KeyboardFocus::UP:
452 index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
455 case Toolkit::Control::KeyboardFocus::DOWN:
457 index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
464 // If the focusable property is set then determine next focusable actor
465 if( index != Property::INVALID_INDEX)
467 value = currentFocusActor.GetProperty( index );
468 actorId = value.Get<int>();
470 // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
473 if( currentFocusActor.GetParent() )
475 nextFocusableActor = currentFocusActor.GetParent().FindChildById( actorId );
478 if( !nextFocusableActor )
480 Integration::SceneHolder window = Integration::SceneHolder::Get( currentFocusActor );
483 nextFocusableActor = window.GetRootLayer().FindChildById( actorId );
490 if( !nextFocusableActor )
492 // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
493 if( mCustomAlgorithmInterface )
495 mIsWaitingKeyboardFocusChangeCommit = true;
496 nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor( currentFocusActor, Actor(), direction );
497 mIsWaitingKeyboardFocusChangeCommit = false;
499 else if( !mPreFocusChangeSignal.Empty() )
501 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
502 mIsWaitingKeyboardFocusChangeCommit = true;
503 nextFocusableActor = mPreFocusChangeSignal.Emit( currentFocusActor, Actor(), direction );
504 mIsWaitingKeyboardFocusChangeCommit = false;
506 else if(mEnableDefaultAlgorithm)
508 // We should find it among the actors nearby.
509 nextFocusableActor = Toolkit::FocusFinder::GetNearestFocusableActor(currentFocusActor, direction);
513 if( nextFocusableActor && nextFocusableActor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ) )
515 // Whether the next focusable actor is a layout control
516 if( IsLayoutControl( nextFocusableActor ) )
518 // If so, move the focus inside it.
519 Toolkit::Control layoutControl = Toolkit::Control::DownCast( nextFocusableActor) ;
520 succeed = DoMoveFocusWithinLayoutControl( layoutControl, currentFocusActor, direction );
524 // Otherwise, just set focus to the next focusable actor
525 succeed = SetCurrentFocusActor( nextFocusableActor );
533 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
535 // Ask the control for the next actor to focus
536 Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
537 if(nextFocusableActor)
539 if(!nextFocusableActor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ))
541 // If the actor is not focusable, ask the same layout control for the next actor to focus
542 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
546 Actor currentFocusActor = GetCurrentFocusActor();
547 Actor committedFocusActor = nextFocusableActor;
549 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
550 // Signal handler can check the proposed actor and return a different actor if it wishes.
551 if( !mPreFocusChangeSignal.Empty() )
553 mIsWaitingKeyboardFocusChangeCommit = true;
554 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
555 mIsWaitingKeyboardFocusChangeCommit = false;
558 if (committedFocusActor && committedFocusActor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ))
560 // Whether the commited focusable actor is a layout control
561 if(IsLayoutControl(committedFocusActor))
563 // If so, move the focus inside it.
564 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
565 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
569 // Otherwise, just set focus to the next focusable actor
570 if(committedFocusActor == nextFocusableActor)
572 // If the application hasn't changed our proposed actor, we informs the layout control we will
573 // move the focus to what the control returns. The control might wish to perform some actions
574 // before the focus is actually moved.
575 GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
578 return SetCurrentFocusActor(committedFocusActor);
589 // No more actor can be focused in the given direction within the same layout control.
594 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
596 bool succeed = false;
598 // Get the parent layout control of the current focus group
599 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
601 while(parentLayoutControl && !succeed)
603 // If the current focus group has a parent layout control, we can probably automatically
604 // move the focus to the next focus group in the forward or backward direction.
605 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
606 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
607 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
610 if(!mFocusGroupChangedSignal.Empty())
612 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
613 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
619 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
623 Toolkit::Control control = Toolkit::Control::DownCast( actor );
626 // Notify the control that enter has been pressed on it.
627 GetImplementation( control ).KeyboardEnter();
630 // Send a notification for the actor.
631 if( !mFocusedActorEnterKeySignal.Empty() )
633 mFocusedActorEnterKeySignal.Emit( actor );
638 void KeyboardFocusManager::ClearFocus()
640 Actor actor = GetCurrentFocusActor();
643 if( mFocusIndicatorActor )
645 actor.Remove( mFocusIndicatorActor );
648 // Send notification for the change of focus actor
649 if( !mFocusChangedSignal.Empty() )
651 mFocusChangedSignal.Emit( actor, Actor() );
654 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast( actor );
655 if( currentlyFocusedControl )
657 currentlyFocusedControl.SetProperty( DevelControl::Property::STATE, DevelControl::NORMAL );
658 currentlyFocusedControl.ClearKeyInputFocus();
662 mCurrentFocusActor.Reset();
663 mIsFocusIndicatorShown = ( mAlwaysShowIndicator == ALWAYS_SHOW ) ? SHOW : HIDE;
666 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
668 mFocusGroupLoopEnabled = enabled;
671 bool KeyboardFocusManager::GetFocusGroupLoop() const
673 return mFocusGroupLoopEnabled;
676 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
680 // Create/Set focus group property.
681 actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
685 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
687 // Check whether the actor is a focus group
688 bool isFocusGroup = false;
692 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
693 if(propertyIsFocusGroup != Property::INVALID_INDEX)
695 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
702 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
704 // Go through the actor's hierarchy to check which focus group the actor belongs to
705 while (actor && !IsFocusGroup(actor))
707 actor = actor.GetParent();
713 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
715 if(mFocusIndicatorActor != indicator)
717 Actor currentFocusActor = GetCurrentFocusActor();
718 if(currentFocusActor)
720 // The new focus indicator should be added to the current focused actor immediately
721 if(mFocusIndicatorActor)
723 currentFocusActor.Remove(mFocusIndicatorActor);
728 currentFocusActor.Add(indicator);
732 mFocusIndicatorActor = indicator;
736 Actor KeyboardFocusManager::GetFocusIndicatorActor()
738 if( ! mFocusIndicatorActor )
740 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
741 const std::string imageDirPath = AssetManager::GetDaliImagePath();
742 mFocusIndicatorActor = Toolkit::ImageView::New( imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME );
744 // Apply size constraint to the focus indicator
745 mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
748 mFocusIndicatorActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
749 mFocusIndicatorActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
750 mFocusIndicatorActor.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 0.0f));
752 return mFocusIndicatorActor;
755 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
757 AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
758 bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
760 Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
762 std::string keyName = event.GetKeyName();
764 if( mIsFocusIndicatorShown == UNKNOWN )
766 GetConfigurationFromStyleManger();
769 bool isFocusStartableKey = false;
771 if(event.GetState() == KeyEvent::DOWN)
773 if (keyName == "Left")
775 if(!isAccessibilityEnabled)
777 if(mIsFocusIndicatorShown == HIDE)
779 // Show focus indicator
780 mIsFocusIndicatorShown = SHOW;
784 // Move the focus towards left
785 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
788 isFocusStartableKey = true;
792 // Move the accessibility focus backward
793 accessibilityManager.MoveFocusBackward();
796 else if (keyName == "Right")
798 if(!isAccessibilityEnabled)
800 if( mIsFocusIndicatorShown == HIDE )
802 // Show focus indicator
803 mIsFocusIndicatorShown = SHOW;
807 // Move the focus towards right
808 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
813 // Move the accessibility focus forward
814 accessibilityManager.MoveFocusForward();
817 isFocusStartableKey = true;
819 else if (keyName == "Up" && !isAccessibilityEnabled)
821 if( mIsFocusIndicatorShown == HIDE )
823 // Show focus indicator
824 mIsFocusIndicatorShown = SHOW;
828 // Move the focus towards up
829 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
832 isFocusStartableKey = true;
834 else if (keyName == "Down" && !isAccessibilityEnabled)
836 if( mIsFocusIndicatorShown == HIDE )
838 // Show focus indicator
839 mIsFocusIndicatorShown = SHOW;
843 // Move the focus towards down
844 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
847 isFocusStartableKey = true;
849 else if (keyName == "Prior" && !isAccessibilityEnabled)
851 if( mIsFocusIndicatorShown == HIDE )
853 // Show focus indicator
854 mIsFocusIndicatorShown = SHOW;
858 // Move the focus towards the previous page
859 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
862 isFocusStartableKey = true;
864 else if (keyName == "Next" && !isAccessibilityEnabled)
866 if( mIsFocusIndicatorShown == HIDE )
868 // Show focus indicator
869 mIsFocusIndicatorShown = SHOW;
873 // Move the focus towards the next page
874 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
877 isFocusStartableKey = true;
879 else if (keyName == "Tab" && !isAccessibilityEnabled)
881 if( mIsFocusIndicatorShown == HIDE )
883 // Show focus indicator
884 mIsFocusIndicatorShown = SHOW;
888 // "Tab" key changes the focus group in the forward direction and
889 // "Shift-Tab" key changes it in the backward direction.
890 if(!DoMoveFocusToNextFocusGroup(!event.IsShiftModifier()))
892 // If the focus group is not changed, Move the focus towards right, "Shift-Tap" key moves the focus towards left.
893 if(!MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::LEFT : Toolkit::Control::KeyboardFocus::RIGHT))
895 // If the focus is not moved, Move the focus towards down, "Shift-Tap" key moves the focus towards up.
896 MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::UP : Toolkit::Control::KeyboardFocus::DOWN);
901 isFocusStartableKey = true;
903 else if (keyName == "space" && !isAccessibilityEnabled)
905 if( mIsFocusIndicatorShown == HIDE )
907 // Show focus indicator
908 mIsFocusIndicatorShown = SHOW;
911 isFocusStartableKey = true;
913 else if (keyName == "" && !isAccessibilityEnabled)
915 // Check the fake key event for evas-plugin case
916 if( mIsFocusIndicatorShown == HIDE )
918 // Show focus indicator
919 mIsFocusIndicatorShown = SHOW;
922 isFocusStartableKey = true;
924 else if (keyName == "Backspace" && !isAccessibilityEnabled)
926 // Emit signal to go back to the previous view???
928 else if (keyName == "Escape" && !isAccessibilityEnabled)
932 else if(event.GetState() == KeyEvent::UP)
934 if (keyName == "Return")
936 if((mIsFocusIndicatorShown == HIDE) && !isAccessibilityEnabled)
938 // Show focus indicator
939 mIsFocusIndicatorShown = SHOW;
943 // The focused actor has enter pressed on it
945 if( !isAccessibilityEnabled )
947 actor = GetCurrentFocusActor();
951 actor = accessibilityManager.GetCurrentFocusActor();
956 DoKeyboardEnter( actor );
960 isFocusStartableKey = true;
964 if(isFocusStartableKey && ( mIsFocusIndicatorShown == SHOW ) && !isAccessibilityEnabled)
966 Actor actor = GetCurrentFocusActor();
969 if( mEnableFocusIndicator == ENABLE )
971 // Make sure the focused actor is highlighted
972 actor.Add( GetFocusIndicatorActor() );
977 // No actor is focused but keyboard focus is activated by the key press
978 // Let's try to move the initial focus
979 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
984 void KeyboardFocusManager::OnTouch(const TouchEvent& touch)
986 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
987 // Try to load configuration.
988 if( mIsFocusIndicatorShown == UNKNOWN )
990 GetConfigurationFromStyleManger();
993 // Clear the focus when user touch the screen.
994 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
995 if(((touch.GetPointCount() < 1) || (touch.GetState(0) == PointState::DOWN)))
997 // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
998 if(mClearFocusOnTouch)
1003 // If KEYBOARD_FOCUSABLE and TOUCH_FOCUSABLE is true, set focus actor
1004 Actor hitActor = touch.GetHitActor(0);
1005 if(hitActor && hitActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && hitActor.GetProperty<bool>(DevelActor::Property::TOUCH_FOCUSABLE))
1007 SetCurrentFocusActor(hitActor);
1012 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn )
1014 if( focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer() )
1016 // Change Current Focused Window
1017 Layer rootLayer = window.GetRootLayer();
1018 mCurrentFocusedWindow = rootLayer;
1020 // Get Current Focused Actor from window
1021 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
1022 SetCurrentFocusActor( currentFocusedActor );
1024 if( currentFocusedActor && ( mEnableFocusIndicator == ENABLE ) )
1026 // Make sure the focused actor is highlighted
1027 currentFocusedActor.Add( GetFocusIndicatorActor() );
1028 mIsFocusIndicatorShown = SHOW;
1033 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
1035 return mPreFocusChangeSignal;
1038 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1040 return mFocusChangedSignal;
1043 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1045 return mFocusGroupChangedSignal;
1048 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1050 return mFocusedActorEnterKeySignal;
1053 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1055 Dali::BaseHandle handle( object );
1057 bool connected( true );
1058 KeyboardFocusManager* manager = static_cast< KeyboardFocusManager* >( object ); // TypeRegistry guarantees that this is the correct type.
1060 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
1062 manager->PreFocusChangeSignal().Connect( tracker, functor );
1064 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
1066 manager->FocusChangedSignal().Connect( tracker, functor );
1068 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
1070 manager->FocusGroupChangedSignal().Connect( tracker, functor );
1072 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
1074 manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
1078 // signalName does not match any signal
1085 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1087 mCustomAlgorithmInterface = &interface;
1090 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1092 if( !enable && mFocusIndicatorActor )
1094 mFocusIndicatorActor.Unparent();
1097 mEnableFocusIndicator = enable? ENABLE : DISABLE;
1101 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1103 return ( mEnableFocusIndicator == ENABLE );
1106 void KeyboardFocusManager::EnableDefaultAlgorithm(bool enable)
1108 mEnableDefaultAlgorithm = enable;
1111 bool KeyboardFocusManager::IsDefaultAlgorithmEnabled() const
1113 return mEnableDefaultAlgorithm;
1116 } // namespace Internal
1118 } // namespace Toolkit