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-event.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/devel-api/focus-manager/focus-finder.h>
45 #include <dali-toolkit/public-api/styling/style-manager.h>
46 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
57 namespace // Unnamed namespace
60 #if defined(DEBUG_ENABLED)
61 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
64 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
66 const char* const FOCUS_BORDER_IMAGE_FILE_NAME = "keyboard_focus.9.png";
70 BaseHandle handle = KeyboardFocusManager::Get();
74 SingletonService singletonService( SingletonService::Get() );
75 if ( singletonService )
77 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
78 singletonService.Register( typeid( manager ), manager );
86 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true )
88 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardPreFocusChange", SIGNAL_PRE_FOCUS_CHANGE )
89 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusChanged", SIGNAL_FOCUS_CHANGED )
90 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged", SIGNAL_FOCUS_GROUP_CHANGED )
91 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey", SIGNAL_FOCUSED_ACTOR_ENTER_KEY )
93 DALI_TYPE_REGISTRATION_END()
95 const unsigned int MAX_HISTORY_AMOUNT = 30; ///< Max length of focus history stack
97 } // unnamed namespace
99 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
101 Toolkit::KeyboardFocusManager manager;
103 SingletonService singletonService( SingletonService::Get() );
104 if ( singletonService )
106 // Check whether the keyboard focus manager is already created
107 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
110 // If so, downcast the handle of singleton to keyboard focus manager
111 manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
118 KeyboardFocusManager::KeyboardFocusManager()
119 : mPreFocusChangeSignal(),
120 mFocusChangedSignal(),
121 mFocusGroupChangedSignal(),
122 mFocusedActorEnterKeySignal(),
123 mCurrentFocusActor(),
124 mFocusIndicatorActor(),
126 mSlotDelegate( this ),
127 mCustomAlgorithmInterface(NULL),
128 mCurrentFocusedWindow(),
129 mIsFocusIndicatorShown( UNKNOWN ),
130 mEnableFocusIndicator( ENABLE ),
131 mAlwaysShowIndicator( ALWAYS_SHOW ),
132 mFocusGroupLoopEnabled( false ),
133 mIsWaitingKeyboardFocusChangeCommit( false ),
134 mClearFocusOnTouch( true ),
135 mEnableDefaultAlgorithm(false)
137 // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorShown.
139 LifecycleController::Get().InitSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnAdaptorInit );
142 void KeyboardFocusManager::OnAdaptorInit()
144 if( Adaptor::IsAvailable() )
146 // Retrieve all the existing scene holders
147 Dali::SceneHolderList sceneHolders = Adaptor::Get().GetSceneHolders();
148 for( auto iter = sceneHolders.begin(); iter != sceneHolders.end(); ++iter )
150 ( *iter ).KeyEventSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnKeyEvent );
151 ( *iter ).TouchedSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
152 Dali::Window window = DevelWindow::DownCast( *iter );
155 window.FocusChangeSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
159 // Get notified when any new scene holder is created afterwards
160 Adaptor::Get().WindowCreatedSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnSceneHolderCreated );
164 void KeyboardFocusManager::OnSceneHolderCreated( Dali::Integration::SceneHolder& sceneHolder )
166 sceneHolder.KeyEventSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnKeyEvent );
167 sceneHolder.TouchedSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
168 Dali::Window window = DevelWindow::DownCast( sceneHolder );
171 window.FocusChangeSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnWindowFocusChanged);
175 KeyboardFocusManager::~KeyboardFocusManager()
179 void KeyboardFocusManager::GetConfigurationFromStyleManger()
181 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
184 Property::Map config = Toolkit::DevelStyleManager::GetConfigurations( styleManager );
185 mAlwaysShowIndicator = config["alwaysShowFocus"].Get<bool>() ? ALWAYS_SHOW : NONE;
186 mIsFocusIndicatorShown = ( mAlwaysShowIndicator == ALWAYS_SHOW )? SHOW : HIDE;
187 mClearFocusOnTouch = ( mIsFocusIndicatorShown == SHOW ) ? false : true;
191 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
193 DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
195 if( mIsFocusIndicatorShown == UNKNOWN )
197 GetConfigurationFromStyleManger();
200 return DoSetCurrentFocusActor( actor );
203 bool KeyboardFocusManager::DoSetCurrentFocusActor( Actor actor )
205 bool success = false;
206 if( actor && actor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ) && actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
208 Integration::SceneHolder currentWindow = Integration::SceneHolder::Get( actor );
210 if( currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
212 Layer rootLayer = currentWindow.GetRootLayer();
213 mCurrentFocusedWindow = rootLayer;
217 Actor currentFocusedActor = GetCurrentFocusActor();
219 // If developer set focus on same actor, doing nothing
220 if( actor == currentFocusedActor )
229 // Check whether the actor is in the stage and is keyboard focusable.
230 if( actor && actor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ) && actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
232 if( ( mIsFocusIndicatorShown == SHOW ) && ( mEnableFocusIndicator == ENABLE ) )
234 actor.Add( GetFocusIndicatorActor() );
237 // Send notification for the change of focus actor
238 if( !mFocusChangedSignal.Empty() )
240 mFocusChangedSignal.Emit(currentFocusedActor, actor);
243 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
244 if( currentlyFocusedControl )
246 // Do we need it to remember if it was previously DISABLED?
247 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL );
248 currentlyFocusedControl.ClearKeyInputFocus();
251 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
253 // Save the current focused actor
254 mCurrentFocusActor = actor;
256 bool focusedWindowFound = false;
257 for( unsigned int i = 0; i < mCurrentFocusActors.size(); i++ )
259 if( mCurrentFocusActors[i].first == mCurrentFocusedWindow )
261 mCurrentFocusActors[i].second = actor;
262 focusedWindowFound = true;
266 if( !focusedWindowFound)
268 // A new window gains the focus, so store the focused actor in that window.
269 mCurrentFocusActors.push_back( std::pair< WeakHandle< Layer>, WeakHandle< Actor > >( mCurrentFocusedWindow , actor ));
272 Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
273 if( newlyFocusedControl )
275 newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED );
276 newlyFocusedControl.SetKeyInputFocus();
279 // Push Current Focused Actor to FocusHistory
280 mFocusHistory.push_back( actor );
282 // Delete first element before add new element when Stack is full.
283 if( mFocusHistory.size() > MAX_HISTORY_AMOUNT )
285 FocusStackIterator beginPos = mFocusHistory.begin();
286 mFocusHistory.erase( beginPos );
289 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
294 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
300 Actor KeyboardFocusManager::GetCurrentFocusActor()
302 Actor actor = mCurrentFocusActor.GetHandle();
304 if( actor && ! actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
306 // If the actor has been removed from the stage, then it should not be focused
308 mCurrentFocusActor.Reset();
313 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
317 for( index = 0; index < mCurrentFocusActors.size(); index++ )
319 if( mCurrentFocusActors[index].first == mCurrentFocusedWindow )
321 actor = mCurrentFocusActors[index].second.GetHandle();
326 if( actor && ! actor.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
328 // If the actor has been removed from the window, then the window doesn't have any focused actor
330 mCurrentFocusActors.erase( mCurrentFocusActors.begin() + index );
336 Actor KeyboardFocusManager::GetCurrentFocusGroup()
338 return GetFocusGroup(GetCurrentFocusActor());
341 void KeyboardFocusManager::MoveFocusBackward()
343 // Find Pre Focused Actor when the list size is more than 1
344 if( mFocusHistory.size() > 1 )
346 // Delete current focused actor in history
347 mFocusHistory.pop_back();
349 // If pre-focused actors are not on stage or deleted, remove them in stack
350 while( mFocusHistory.size() > 0 )
352 // Get pre focused actor
353 Actor target = mFocusHistory[ mFocusHistory.size() -1 ].GetHandle();
355 // Impl of Actor is not null
356 if( target && target.GetProperty< bool >( Actor::Property::CONNECTED_TO_SCENE ) )
358 // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
359 mFocusHistory.pop_back();
360 SetCurrentFocusActor( target );
365 // Target is empty handle or off stage. Erase from queue
366 mFocusHistory.pop_back();
370 // if there is no actor which can get focus, then push current focus actor in stack again
371 if( mFocusHistory.size() == 0 )
373 Actor currentFocusedActor = GetCurrentFocusActor();
374 mFocusHistory.push_back( currentFocusedActor );
379 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
381 Toolkit::Control control = Toolkit::Control::DownCast(actor);
382 return control && GetImplementation( control ).IsKeyboardNavigationSupported();
385 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
387 // Get the actor's parent layout control that supports two dimensional keyboard navigation
392 Integration::SceneHolder window = Integration::SceneHolder::Get( actor );
395 rootActor = window.GetRootLayer();
398 parent = actor.GetParent();
401 while( parent && !IsLayoutControl(parent) && parent != rootActor )
403 parent = parent.GetParent();
406 return Toolkit::Control::DownCast(parent);
409 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
411 Actor currentFocusActor = GetCurrentFocusActor();
413 bool succeed = false;
415 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
416 Toolkit::Control parentLayoutControl = GetParentLayoutControl( currentFocusActor );
417 while( parentLayoutControl && !succeed )
419 succeed = DoMoveFocusWithinLayoutControl( parentLayoutControl, currentFocusActor, direction );
420 parentLayoutControl = GetParentLayoutControl( parentLayoutControl );
425 Actor nextFocusableActor;
427 Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
429 // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
430 if( currentFocusControl )
433 Property::Index index = Property::INVALID_INDEX;
434 Property::Value value;
436 // Find property index based upon focus direction
439 case Toolkit::Control::KeyboardFocus::LEFT:
441 index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
444 case Toolkit::Control::KeyboardFocus::RIGHT:
446 index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
449 case Toolkit::Control::KeyboardFocus::UP:
451 index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
454 case Toolkit::Control::KeyboardFocus::DOWN:
456 index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
463 // If the focusable property is set then determine next focusable actor
464 if( index != Property::INVALID_INDEX)
466 value = currentFocusActor.GetProperty( index );
467 actorId = value.Get<int>();
469 // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
472 if( currentFocusActor.GetParent() )
474 nextFocusableActor = currentFocusActor.GetParent().FindChildById( actorId );
477 if( !nextFocusableActor )
479 Integration::SceneHolder window = Integration::SceneHolder::Get( currentFocusActor );
482 nextFocusableActor = window.GetRootLayer().FindChildById( actorId );
489 if( !nextFocusableActor )
491 // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
492 if( mCustomAlgorithmInterface )
494 mIsWaitingKeyboardFocusChangeCommit = true;
495 nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor( currentFocusActor, Actor(), direction );
496 mIsWaitingKeyboardFocusChangeCommit = false;
498 else if( !mPreFocusChangeSignal.Empty() )
500 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
501 mIsWaitingKeyboardFocusChangeCommit = true;
502 nextFocusableActor = mPreFocusChangeSignal.Emit( currentFocusActor, Actor(), direction );
503 mIsWaitingKeyboardFocusChangeCommit = false;
505 else if(mEnableDefaultAlgorithm)
507 // We should find it among the actors nearby.
508 nextFocusableActor = Toolkit::FocusFinder::GetNearestFocusableActor(currentFocusActor, direction);
512 if( nextFocusableActor && nextFocusableActor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ) )
514 // Whether the next focusable actor is a layout control
515 if( IsLayoutControl( nextFocusableActor ) )
517 // If so, move the focus inside it.
518 Toolkit::Control layoutControl = Toolkit::Control::DownCast( nextFocusableActor) ;
519 succeed = DoMoveFocusWithinLayoutControl( layoutControl, currentFocusActor, direction );
523 // Otherwise, just set focus to the next focusable actor
524 succeed = SetCurrentFocusActor( nextFocusableActor );
532 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
534 // Ask the control for the next actor to focus
535 Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
536 if(nextFocusableActor)
538 if(!nextFocusableActor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ))
540 // If the actor is not focusable, ask the same layout control for the next actor to focus
541 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
545 Actor currentFocusActor = GetCurrentFocusActor();
546 Actor committedFocusActor = nextFocusableActor;
548 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
549 // Signal handler can check the proposed actor and return a different actor if it wishes.
550 if( !mPreFocusChangeSignal.Empty() )
552 mIsWaitingKeyboardFocusChangeCommit = true;
553 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
554 mIsWaitingKeyboardFocusChangeCommit = false;
557 if (committedFocusActor && committedFocusActor.GetProperty< bool >( Actor::Property::KEYBOARD_FOCUSABLE ))
559 // Whether the commited focusable actor is a layout control
560 if(IsLayoutControl(committedFocusActor))
562 // If so, move the focus inside it.
563 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
564 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
568 // Otherwise, just set focus to the next focusable actor
569 if(committedFocusActor == nextFocusableActor)
571 // If the application hasn't changed our proposed actor, we informs the layout control we will
572 // move the focus to what the control returns. The control might wish to perform some actions
573 // before the focus is actually moved.
574 GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
577 return SetCurrentFocusActor(committedFocusActor);
588 // No more actor can be focused in the given direction within the same layout control.
593 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
595 bool succeed = false;
597 // Get the parent layout control of the current focus group
598 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
600 while(parentLayoutControl && !succeed)
602 // If the current focus group has a parent layout control, we can probably automatically
603 // move the focus to the next focus group in the forward or backward direction.
604 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
605 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
606 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
609 if(!mFocusGroupChangedSignal.Empty())
611 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
612 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
618 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
622 Toolkit::Control control = Toolkit::Control::DownCast( actor );
625 // Notify the control that enter has been pressed on it.
626 GetImplementation( control ).KeyboardEnter();
629 // Send a notification for the actor.
630 if( !mFocusedActorEnterKeySignal.Empty() )
632 mFocusedActorEnterKeySignal.Emit( actor );
637 void KeyboardFocusManager::ClearFocus()
639 Actor actor = GetCurrentFocusActor();
642 if( mFocusIndicatorActor )
644 actor.Remove( mFocusIndicatorActor );
647 // Send notification for the change of focus actor
648 if( !mFocusChangedSignal.Empty() )
650 mFocusChangedSignal.Emit( actor, Actor() );
653 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast( actor );
654 if( currentlyFocusedControl )
656 currentlyFocusedControl.SetProperty( DevelControl::Property::STATE, DevelControl::NORMAL );
657 currentlyFocusedControl.ClearKeyInputFocus();
661 mCurrentFocusActor.Reset();
662 mIsFocusIndicatorShown = ( mAlwaysShowIndicator == ALWAYS_SHOW ) ? SHOW : HIDE;
665 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
667 mFocusGroupLoopEnabled = enabled;
670 bool KeyboardFocusManager::GetFocusGroupLoop() const
672 return mFocusGroupLoopEnabled;
675 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
679 // Create/Set focus group property.
680 actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
684 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
686 // Check whether the actor is a focus group
687 bool isFocusGroup = false;
691 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
692 if(propertyIsFocusGroup != Property::INVALID_INDEX)
694 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
701 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
703 // Go through the actor's hierarchy to check which focus group the actor belongs to
704 while (actor && !IsFocusGroup(actor))
706 actor = actor.GetParent();
712 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
714 if(mFocusIndicatorActor != indicator)
716 Actor currentFocusActor = GetCurrentFocusActor();
717 if(currentFocusActor)
719 // The new focus indicator should be added to the current focused actor immediately
720 if(mFocusIndicatorActor)
722 currentFocusActor.Remove(mFocusIndicatorActor);
727 currentFocusActor.Add(indicator);
731 mFocusIndicatorActor = indicator;
735 Actor KeyboardFocusManager::GetFocusIndicatorActor()
737 if( ! mFocusIndicatorActor )
739 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
740 const std::string imageDirPath = AssetManager::GetDaliImagePath();
741 mFocusIndicatorActor = Toolkit::ImageView::New( imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME );
743 // Apply size constraint to the focus indicator
744 mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
747 mFocusIndicatorActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
748 mFocusIndicatorActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
749 mFocusIndicatorActor.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 0.0f));
751 return mFocusIndicatorActor;
754 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
756 AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
757 bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
759 Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
761 std::string keyName = event.GetKeyName();
763 if( mIsFocusIndicatorShown == UNKNOWN )
765 GetConfigurationFromStyleManger();
768 bool isFocusStartableKey = false;
770 if(event.GetState() == KeyEvent::DOWN)
772 if (keyName == "Left")
774 if(!isAccessibilityEnabled)
776 if(mIsFocusIndicatorShown == HIDE)
778 // Show focus indicator
779 mIsFocusIndicatorShown = SHOW;
783 // Move the focus towards left
784 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
787 isFocusStartableKey = true;
791 // Move the accessibility focus backward
792 accessibilityManager.MoveFocusBackward();
795 else if (keyName == "Right")
797 if(!isAccessibilityEnabled)
799 if( mIsFocusIndicatorShown == HIDE )
801 // Show focus indicator
802 mIsFocusIndicatorShown = SHOW;
806 // Move the focus towards right
807 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
812 // Move the accessibility focus forward
813 accessibilityManager.MoveFocusForward();
816 isFocusStartableKey = true;
818 else if (keyName == "Up" && !isAccessibilityEnabled)
820 if( mIsFocusIndicatorShown == HIDE )
822 // Show focus indicator
823 mIsFocusIndicatorShown = SHOW;
827 // Move the focus towards up
828 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
831 isFocusStartableKey = true;
833 else if (keyName == "Down" && !isAccessibilityEnabled)
835 if( mIsFocusIndicatorShown == HIDE )
837 // Show focus indicator
838 mIsFocusIndicatorShown = SHOW;
842 // Move the focus towards down
843 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
846 isFocusStartableKey = true;
848 else if (keyName == "Prior" && !isAccessibilityEnabled)
850 if( mIsFocusIndicatorShown == HIDE )
852 // Show focus indicator
853 mIsFocusIndicatorShown = SHOW;
857 // Move the focus towards the previous page
858 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
861 isFocusStartableKey = true;
863 else if (keyName == "Next" && !isAccessibilityEnabled)
865 if( mIsFocusIndicatorShown == HIDE )
867 // Show focus indicator
868 mIsFocusIndicatorShown = SHOW;
872 // Move the focus towards the next page
873 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
876 isFocusStartableKey = true;
878 else if (keyName == "Tab" && !isAccessibilityEnabled)
880 if( mIsFocusIndicatorShown == HIDE )
882 // Show focus indicator
883 mIsFocusIndicatorShown = SHOW;
887 // "Tab" key changes the focus group in the forward direction and
888 // "Shift-Tab" key changes it in the backward direction.
889 if(!DoMoveFocusToNextFocusGroup(!event.IsShiftModifier()))
891 // If the focus group is not changed, Move the focus towards right, "Shift-Tap" key moves the focus towards left.
892 if(!MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::LEFT : Toolkit::Control::KeyboardFocus::RIGHT))
894 // If the focus is not moved, Move the focus towards down, "Shift-Tap" key moves the focus towards up.
895 MoveFocus(event.IsShiftModifier() ? Toolkit::Control::KeyboardFocus::UP : Toolkit::Control::KeyboardFocus::DOWN);
900 isFocusStartableKey = true;
902 else if (keyName == "space" && !isAccessibilityEnabled)
904 if( mIsFocusIndicatorShown == HIDE )
906 // Show focus indicator
907 mIsFocusIndicatorShown = SHOW;
910 isFocusStartableKey = true;
912 else if (keyName == "" && !isAccessibilityEnabled)
914 // Check the fake key event for evas-plugin case
915 if( mIsFocusIndicatorShown == HIDE )
917 // Show focus indicator
918 mIsFocusIndicatorShown = SHOW;
921 isFocusStartableKey = true;
923 else if (keyName == "Backspace" && !isAccessibilityEnabled)
925 // Emit signal to go back to the previous view???
927 else if (keyName == "Escape" && !isAccessibilityEnabled)
931 else if(event.GetState() == KeyEvent::UP)
933 if (keyName == "Return")
935 if((mIsFocusIndicatorShown == HIDE) && !isAccessibilityEnabled)
937 // Show focus indicator
938 mIsFocusIndicatorShown = SHOW;
942 // The focused actor has enter pressed on it
944 if( !isAccessibilityEnabled )
946 actor = GetCurrentFocusActor();
950 actor = accessibilityManager.GetCurrentFocusActor();
955 DoKeyboardEnter( actor );
959 isFocusStartableKey = true;
963 if(isFocusStartableKey && ( mIsFocusIndicatorShown == SHOW ) && !isAccessibilityEnabled)
965 Actor actor = GetCurrentFocusActor();
968 if( mEnableFocusIndicator == ENABLE )
970 // Make sure the focused actor is highlighted
971 actor.Add( GetFocusIndicatorActor() );
976 // No actor is focused but keyboard focus is activated by the key press
977 // Let's try to move the initial focus
978 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
983 void KeyboardFocusManager::OnTouch(const TouchEvent& touch)
985 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
986 // Try to load configuration.
987 if( mIsFocusIndicatorShown == UNKNOWN )
989 GetConfigurationFromStyleManger();
992 // Clear the focus when user touch the screen.
993 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
994 // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
995 if( (( touch.GetPointCount() < 1 ) || ( touch.GetState( 0 ) == PointState::DOWN )) && mClearFocusOnTouch )
1001 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn )
1003 if( focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer() )
1005 // Change Current Focused Window
1006 Layer rootLayer = window.GetRootLayer();
1007 mCurrentFocusedWindow = rootLayer;
1009 // Get Current Focused Actor from window
1010 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
1011 SetCurrentFocusActor( currentFocusedActor );
1013 if( currentFocusedActor && ( mEnableFocusIndicator == ENABLE ) )
1015 // Make sure the focused actor is highlighted
1016 currentFocusedActor.Add( GetFocusIndicatorActor() );
1017 mIsFocusIndicatorShown = SHOW;
1022 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
1024 return mPreFocusChangeSignal;
1027 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1029 return mFocusChangedSignal;
1032 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1034 return mFocusGroupChangedSignal;
1037 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1039 return mFocusedActorEnterKeySignal;
1042 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1044 Dali::BaseHandle handle( object );
1046 bool connected( true );
1047 KeyboardFocusManager* manager = static_cast< KeyboardFocusManager* >( object ); // TypeRegistry guarantees that this is the correct type.
1049 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
1051 manager->PreFocusChangeSignal().Connect( tracker, functor );
1053 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
1055 manager->FocusChangedSignal().Connect( tracker, functor );
1057 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
1059 manager->FocusGroupChangedSignal().Connect( tracker, functor );
1061 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
1063 manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
1067 // signalName does not match any signal
1074 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1076 mCustomAlgorithmInterface = &interface;
1079 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1081 if( !enable && mFocusIndicatorActor )
1083 mFocusIndicatorActor.Unparent();
1086 mEnableFocusIndicator = enable? ENABLE : DISABLE;
1090 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1092 return ( mEnableFocusIndicator == ENABLE );
1095 void KeyboardFocusManager::EnableDefaultAlgorithm(bool enable)
1097 mEnableDefaultAlgorithm = enable;
1100 bool KeyboardFocusManager::IsDefaultAlgorithmEnabled() const
1102 return mEnableDefaultAlgorithm;
1105 } // namespace Internal
1107 } // namespace Toolkit