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;
208 // If the parent's KEYBOARD_FOCUSABLE_CHILDREN is false, it cannot have focus.
211 Actor parent = actor.GetParent();
214 if(!parent.GetProperty<bool>(DevelActor::Property::KEYBOARD_FOCUSABLE_CHILDREN))
216 DALI_LOG_INFO(gLogFilter, Debug::General, "[%s:%d] Parent Actor has KEYBOARD_FOCUSABLE_CHILDREN false,\n", __FUNCTION__, __LINE__);
219 parent = parent.GetParent();
223 if(actor && actor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE))
225 Integration::SceneHolder currentWindow = Integration::SceneHolder::Get( actor );
227 if( currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
229 Layer rootLayer = currentWindow.GetRootLayer();
230 mCurrentFocusedWindow = rootLayer;
234 Actor currentFocusedActor = GetCurrentFocusActor();
236 // If developer set focus on same actor, doing nothing
237 if( actor == currentFocusedActor )
246 // Check whether the actor is in the stage and is keyboard focusable.
247 if( actor && actor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ) && actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
249 if( ( mIsFocusIndicatorShown == SHOW ) && ( mEnableFocusIndicator == ENABLE ) )
251 actor.Add( GetFocusIndicatorActor() );
254 // Send notification for the change of focus actor
255 if( !mFocusChangedSignal.Empty() )
257 mFocusChangedSignal.Emit(currentFocusedActor, actor);
260 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
261 if( currentlyFocusedControl )
263 // Do we need it to remember if it was previously DISABLED?
264 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL );
265 currentlyFocusedControl.ClearKeyInputFocus();
268 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
270 // Save the current focused actor
271 mCurrentFocusActor = actor;
273 bool focusedWindowFound = false;
274 for( unsigned int i = 0; i < mCurrentFocusActors.size(); i++ )
276 if( mCurrentFocusActors[i].first == mCurrentFocusedWindow )
278 mCurrentFocusActors[i].second = actor;
279 focusedWindowFound = true;
283 if( !focusedWindowFound)
285 // A new window gains the focus, so store the focused actor in that window.
286 mCurrentFocusActors.push_back( std::pair< WeakHandle< Layer>, WeakHandle< Actor > >( mCurrentFocusedWindow , actor ));
289 Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
290 if( newlyFocusedControl )
292 newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED );
293 newlyFocusedControl.SetKeyInputFocus();
296 // Push Current Focused Actor to FocusHistory
297 mFocusHistory.push_back( actor );
299 // Delete first element before add new element when Stack is full.
300 if( mFocusHistory.size() > MAX_HISTORY_AMOUNT )
302 FocusStackIterator beginPos = mFocusHistory.begin();
303 mFocusHistory.erase( beginPos );
306 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
311 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
317 Actor KeyboardFocusManager::GetCurrentFocusActor()
319 Actor actor = mCurrentFocusActor.GetHandle();
321 if( actor && ! actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
323 // If the actor has been removed from the stage, then it should not be focused
325 mCurrentFocusActor.Reset();
330 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
334 for( index = 0; index < mCurrentFocusActors.size(); index++ )
336 if( mCurrentFocusActors[index].first == mCurrentFocusedWindow )
338 actor = mCurrentFocusActors[index].second.GetHandle();
343 if( actor && ! actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
345 // If the actor has been removed from the window, then the window doesn't have any focused actor
347 mCurrentFocusActors.erase( mCurrentFocusActors.begin() + index );
353 Actor KeyboardFocusManager::GetCurrentFocusGroup()
355 return GetFocusGroup(GetCurrentFocusActor());
358 void KeyboardFocusManager::MoveFocusBackward()
360 // Find Pre Focused Actor when the list size is more than 1
361 if( mFocusHistory.size() > 1 )
363 // Delete current focused actor in history
364 mFocusHistory.pop_back();
366 // If pre-focused actors are not on stage or deleted, remove them in stack
367 while( mFocusHistory.size() > 0 )
369 // Get pre focused actor
370 Actor target = mFocusHistory[ mFocusHistory.size() -1 ].GetHandle();
372 // Impl of Actor is not null
373 if( target && target.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
375 // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
376 mFocusHistory.pop_back();
377 SetCurrentFocusActor( target );
382 // Target is empty handle or off stage. Erase from queue
383 mFocusHistory.pop_back();
387 // if there is no actor which can get focus, then push current focus actor in stack again
388 if( mFocusHistory.size() == 0 )
390 Actor currentFocusedActor = GetCurrentFocusActor();
391 mFocusHistory.push_back( currentFocusedActor );
396 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
398 Toolkit::Control control = Toolkit::Control::DownCast(actor);
399 return control && GetImplementation( control ).IsKeyboardNavigationSupported();
402 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
404 // Get the actor's parent layout control that supports two dimensional keyboard navigation
409 Integration::SceneHolder window = Integration::SceneHolder::Get( actor );
412 rootActor = window.GetRootLayer();
415 parent = actor.GetParent();
418 while( parent && !IsLayoutControl(parent) && parent != rootActor )
420 parent = parent.GetParent();
423 return Toolkit::Control::DownCast(parent);
426 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
428 Actor currentFocusActor = GetCurrentFocusActor();
430 bool succeed = false;
432 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
433 Toolkit::Control parentLayoutControl = GetParentLayoutControl( currentFocusActor );
434 while( parentLayoutControl && !succeed )
436 succeed = DoMoveFocusWithinLayoutControl( parentLayoutControl, currentFocusActor, direction );
437 parentLayoutControl = GetParentLayoutControl( parentLayoutControl );
442 Actor nextFocusableActor;
444 Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
446 // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
447 if( currentFocusControl )
450 Property::Index index = Property::INVALID_INDEX;
451 Property::Value value;
453 // Find property index based upon focus direction
456 case Toolkit::Control::KeyboardFocus::LEFT:
458 index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
461 case Toolkit::Control::KeyboardFocus::RIGHT:
463 index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
466 case Toolkit::Control::KeyboardFocus::UP:
468 index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
471 case Toolkit::Control::KeyboardFocus::DOWN:
473 index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
480 // If the focusable property is set then determine next focusable actor
481 if( index != Property::INVALID_INDEX)
483 value = currentFocusActor.GetProperty( index );
484 actorId = value.Get<int>();
486 // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
489 if( currentFocusActor.GetParent() )
491 nextFocusableActor = currentFocusActor.GetParent().FindChildById( actorId );
494 if( !nextFocusableActor )
496 Integration::SceneHolder window = Integration::SceneHolder::Get( currentFocusActor );
499 nextFocusableActor = window.GetRootLayer().FindChildById( actorId );
506 if( !nextFocusableActor )
508 // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
509 if( mCustomAlgorithmInterface )
511 mIsWaitingKeyboardFocusChangeCommit = true;
512 nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor( currentFocusActor, Actor(), direction );
513 mIsWaitingKeyboardFocusChangeCommit = false;
515 else if( !mPreFocusChangeSignal.Empty() )
517 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
518 mIsWaitingKeyboardFocusChangeCommit = true;
519 nextFocusableActor = mPreFocusChangeSignal.Emit( currentFocusActor, Actor(), direction );
520 mIsWaitingKeyboardFocusChangeCommit = false;
522 else if(mEnableDefaultAlgorithm)
524 // We should find it among the actors nearby.
525 nextFocusableActor = Toolkit::FocusFinder::GetNearestFocusableActor(currentFocusActor, direction);
529 if( nextFocusableActor && nextFocusableActor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ) )
531 // Whether the next focusable actor is a layout control
532 if( IsLayoutControl( nextFocusableActor ) )
534 // If so, move the focus inside it.
535 Toolkit::Control layoutControl = Toolkit::Control::DownCast( nextFocusableActor) ;
536 succeed = DoMoveFocusWithinLayoutControl( layoutControl, currentFocusActor, direction );
540 // Otherwise, just set focus to the next focusable actor
541 succeed = SetCurrentFocusActor( nextFocusableActor );
549 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
551 // Ask the control for the next actor to focus
552 Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
553 if(nextFocusableActor)
555 if(!nextFocusableActor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ))
557 // If the actor is not focusable, ask the same layout control for the next actor to focus
558 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
562 Actor currentFocusActor = GetCurrentFocusActor();
563 Actor committedFocusActor = nextFocusableActor;
565 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
566 // Signal handler can check the proposed actor and return a different actor if it wishes.
567 if( !mPreFocusChangeSignal.Empty() )
569 mIsWaitingKeyboardFocusChangeCommit = true;
570 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
571 mIsWaitingKeyboardFocusChangeCommit = false;
574 if (committedFocusActor && committedFocusActor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ))
576 // Whether the commited focusable actor is a layout control
577 if(IsLayoutControl(committedFocusActor))
579 // If so, move the focus inside it.
580 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
581 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
585 // Otherwise, just set focus to the next focusable actor
586 if(committedFocusActor == nextFocusableActor)
588 // If the application hasn't changed our proposed actor, we informs the layout control we will
589 // move the focus to what the control returns. The control might wish to perform some actions
590 // before the focus is actually moved.
591 GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
594 return SetCurrentFocusActor(committedFocusActor);
605 // No more actor can be focused in the given direction within the same layout control.
610 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
612 bool succeed = false;
614 // Get the parent layout control of the current focus group
615 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
617 while(parentLayoutControl && !succeed)
619 // If the current focus group has a parent layout control, we can probably automatically
620 // move the focus to the next focus group in the forward or backward direction.
621 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
622 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
623 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
626 if(!mFocusGroupChangedSignal.Empty())
628 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
629 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
635 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
639 Toolkit::Control control = Toolkit::Control::DownCast( actor );
642 // Notify the control that enter has been pressed on it.
643 GetImplementation( control ).KeyboardEnter();
646 // Send a notification for the actor.
647 if( !mFocusedActorEnterKeySignal.Empty() )
649 mFocusedActorEnterKeySignal.Emit( actor );
654 void KeyboardFocusManager::ClearFocus()
656 Actor actor = GetCurrentFocusActor();
659 if( mFocusIndicatorActor )
661 actor.Remove( mFocusIndicatorActor );
664 // Send notification for the change of focus actor
665 if( !mFocusChangedSignal.Empty() )
667 mFocusChangedSignal.Emit( actor, Actor() );
670 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast( actor );
671 if( currentlyFocusedControl )
673 currentlyFocusedControl.SetProperty( DevelControl::Property::STATE, DevelControl::NORMAL );
674 currentlyFocusedControl.ClearKeyInputFocus();
678 mCurrentFocusActor.Reset();
679 mIsFocusIndicatorShown = ( mAlwaysShowIndicator == ALWAYS_SHOW ) ? SHOW : HIDE;
682 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
684 mFocusGroupLoopEnabled = enabled;
687 bool KeyboardFocusManager::GetFocusGroupLoop() const
689 return mFocusGroupLoopEnabled;
692 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
696 // Create/Set focus group property.
697 actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
701 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
703 // Check whether the actor is a focus group
704 bool isFocusGroup = false;
708 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
709 if(propertyIsFocusGroup != Property::INVALID_INDEX)
711 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
718 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
720 // Go through the actor's hierarchy to check which focus group the actor belongs to
721 while (actor && !IsFocusGroup(actor))
723 actor = actor.GetParent();
729 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
731 if(mFocusIndicatorActor != indicator)
733 Actor currentFocusActor = GetCurrentFocusActor();
734 if(currentFocusActor)
736 // The new focus indicator should be added to the current focused actor immediately
737 if(mFocusIndicatorActor)
739 currentFocusActor.Remove(mFocusIndicatorActor);
744 currentFocusActor.Add(indicator);
748 mFocusIndicatorActor = indicator;
752 Actor KeyboardFocusManager::GetFocusIndicatorActor()
754 if( ! mFocusIndicatorActor )
756 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
757 const std::string imageDirPath = AssetManager::GetDaliImagePath();
758 mFocusIndicatorActor = Toolkit::ImageView::New( imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME );
760 // Apply size constraint to the focus indicator
761 mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
764 mFocusIndicatorActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
765 mFocusIndicatorActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
766 mFocusIndicatorActor.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 0.0f));
768 return mFocusIndicatorActor;
771 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
773 AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
774 bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
776 Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
778 std::string keyName = event.GetKeyName();
780 if( mIsFocusIndicatorShown == UNKNOWN )
782 GetConfigurationFromStyleManger();
785 bool isFocusStartableKey = false;
787 if(event.GetState() == KeyEvent::DOWN)
789 if (keyName == "Left")
791 if(!isAccessibilityEnabled)
793 if(mIsFocusIndicatorShown == HIDE)
795 // Show focus indicator
796 mIsFocusIndicatorShown = SHOW;
800 // Move the focus towards left
801 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
804 isFocusStartableKey = true;
808 // Move the accessibility focus backward
809 accessibilityManager.MoveFocusBackward();
812 else if (keyName == "Right")
814 if(!isAccessibilityEnabled)
816 if( mIsFocusIndicatorShown == HIDE )
818 // Show focus indicator
819 mIsFocusIndicatorShown = SHOW;
823 // Move the focus towards right
824 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
829 // Move the accessibility focus forward
830 accessibilityManager.MoveFocusForward();
833 isFocusStartableKey = true;
835 else if (keyName == "Up" && !isAccessibilityEnabled)
837 if( mIsFocusIndicatorShown == HIDE )
839 // Show focus indicator
840 mIsFocusIndicatorShown = SHOW;
844 // Move the focus towards up
845 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
848 isFocusStartableKey = true;
850 else if (keyName == "Down" && !isAccessibilityEnabled)
852 if( mIsFocusIndicatorShown == HIDE )
854 // Show focus indicator
855 mIsFocusIndicatorShown = SHOW;
859 // Move the focus towards down
860 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
863 isFocusStartableKey = true;
865 else if (keyName == "Prior" && !isAccessibilityEnabled)
867 if( mIsFocusIndicatorShown == HIDE )
869 // Show focus indicator
870 mIsFocusIndicatorShown = SHOW;
874 // Move the focus towards the previous page
875 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
878 isFocusStartableKey = true;
880 else if (keyName == "Next" && !isAccessibilityEnabled)
882 if( mIsFocusIndicatorShown == HIDE )
884 // Show focus indicator
885 mIsFocusIndicatorShown = SHOW;
889 // Move the focus towards the next page
890 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
893 isFocusStartableKey = true;
895 else if (keyName == "Tab" && !isAccessibilityEnabled)
897 if( mIsFocusIndicatorShown == HIDE )
899 // Show focus indicator
900 mIsFocusIndicatorShown = SHOW;
904 // "Tab" key changes the focus group in the forward direction and
905 // "Shift-Tab" key changes it in the backward direction.
906 if(!DoMoveFocusToNextFocusGroup(!event.IsShiftModifier()))
908 // If the focus group is not changed, Move the focus towards right, "Shift-Tap" key moves the focus towards left.
909 if(!MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::LEFT : Toolkit::Control::KeyboardFocus::RIGHT))
911 // If the focus is not moved, Move the focus towards down, "Shift-Tap" key moves the focus towards up.
912 MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::UP : Toolkit::Control::KeyboardFocus::DOWN);
917 isFocusStartableKey = true;
919 else if (keyName == "space" && !isAccessibilityEnabled)
921 if( mIsFocusIndicatorShown == HIDE )
923 // Show focus indicator
924 mIsFocusIndicatorShown = SHOW;
927 isFocusStartableKey = true;
929 else if (keyName == "" && !isAccessibilityEnabled)
931 // Check the fake key event for evas-plugin case
932 if( mIsFocusIndicatorShown == HIDE )
934 // Show focus indicator
935 mIsFocusIndicatorShown = SHOW;
938 isFocusStartableKey = true;
940 else if (keyName == "Backspace" && !isAccessibilityEnabled)
942 // Emit signal to go back to the previous view???
944 else if (keyName == "Escape" && !isAccessibilityEnabled)
948 else if(event.GetState() == KeyEvent::UP)
950 if (keyName == "Return")
952 if((mIsFocusIndicatorShown == HIDE) && !isAccessibilityEnabled)
954 // Show focus indicator
955 mIsFocusIndicatorShown = SHOW;
959 // The focused actor has enter pressed on it
961 if( !isAccessibilityEnabled )
963 actor = GetCurrentFocusActor();
967 actor = accessibilityManager.GetCurrentFocusActor();
972 DoKeyboardEnter( actor );
976 isFocusStartableKey = true;
980 if(isFocusStartableKey && ( mIsFocusIndicatorShown == SHOW ) && !isAccessibilityEnabled)
982 Actor actor = GetCurrentFocusActor();
985 if( mEnableFocusIndicator == ENABLE )
987 // Make sure the focused actor is highlighted
988 actor.Add( GetFocusIndicatorActor() );
993 // No actor is focused but keyboard focus is activated by the key press
994 // Let's try to move the initial focus
995 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
1000 void KeyboardFocusManager::OnTouch(const TouchEvent& touch)
1002 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
1003 // Try to load configuration.
1004 if( mIsFocusIndicatorShown == UNKNOWN )
1006 GetConfigurationFromStyleManger();
1009 // Clear the focus when user touch the screen.
1010 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
1011 if(((touch.GetPointCount() < 1) || (touch.GetState(0) == PointState::DOWN)))
1013 // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
1014 if(mClearFocusOnTouch)
1019 // If KEYBOARD_FOCUSABLE and TOUCH_FOCUSABLE is true, set focus actor
1020 Actor hitActor = touch.GetHitActor(0);
1021 if(hitActor && hitActor.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE) && hitActor.GetProperty<bool>(DevelActor::Property::TOUCH_FOCUSABLE))
1023 SetCurrentFocusActor(hitActor);
1028 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn )
1030 if( focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer() )
1032 // Change Current Focused Window
1033 Layer rootLayer = window.GetRootLayer();
1034 mCurrentFocusedWindow = rootLayer;
1036 // Get Current Focused Actor from window
1037 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
1038 SetCurrentFocusActor( currentFocusedActor );
1040 if( currentFocusedActor && ( mEnableFocusIndicator == ENABLE ) )
1042 // Make sure the focused actor is highlighted
1043 currentFocusedActor.Add( GetFocusIndicatorActor() );
1044 mIsFocusIndicatorShown = SHOW;
1049 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
1051 return mPreFocusChangeSignal;
1054 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1056 return mFocusChangedSignal;
1059 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1061 return mFocusGroupChangedSignal;
1064 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1066 return mFocusedActorEnterKeySignal;
1069 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1071 Dali::BaseHandle handle( object );
1073 bool connected( true );
1074 KeyboardFocusManager* manager = static_cast< KeyboardFocusManager* >( object ); // TypeRegistry guarantees that this is the correct type.
1076 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
1078 manager->PreFocusChangeSignal().Connect( tracker, functor );
1080 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
1082 manager->FocusChangedSignal().Connect( tracker, functor );
1084 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
1086 manager->FocusGroupChangedSignal().Connect( tracker, functor );
1088 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
1090 manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
1094 // signalName does not match any signal
1101 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1103 mCustomAlgorithmInterface = &interface;
1106 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1108 if( !enable && mFocusIndicatorActor )
1110 mFocusIndicatorActor.Unparent();
1113 mEnableFocusIndicator = enable? ENABLE : DISABLE;
1117 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1119 return ( mEnableFocusIndicator == ENABLE );
1122 void KeyboardFocusManager::EnableDefaultAlgorithm(bool enable)
1124 mEnableDefaultAlgorithm = enable;
1127 bool KeyboardFocusManager::IsDefaultAlgorithmEnabled() const
1129 return mEnableDefaultAlgorithm;
1132 } // namespace Internal
1134 } // namespace Toolkit