2 * Copyright (c) 2019 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/common/stage.h>
29 #include <dali/public-api/events/key-event.h>
30 #include <dali/public-api/events/touch-data.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/public-api/images/resource-image.h>
35 #include <dali/integration-api/debug.h>
36 #include <dali/integration-api/adaptor-framework/adaptor.h>
37 #include <dali/integration-api/adaptor-framework/scene-holder.h>
40 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
41 #include <dali-toolkit/public-api/controls/control.h>
42 #include <dali-toolkit/public-api/controls/control-impl.h>
43 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
44 #include <dali-toolkit/public-api/accessibility-manager/accessibility-manager.h>
45 #include <dali-toolkit/devel-api/controls/control-devel.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 )
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 ).TouchSignal().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.TouchSignal().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.IsKeyboardFocusable() && actor.OnStage() )
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.IsKeyboardFocusable() && actor.OnStage() )
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.OnStage() )
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.OnStage() )
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.OnStage() )
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;
507 if( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
509 // Whether the next focusable actor is a layout control
510 if( IsLayoutControl( nextFocusableActor ) )
512 // If so, move the focus inside it.
513 Toolkit::Control layoutControl = Toolkit::Control::DownCast( nextFocusableActor) ;
514 succeed = DoMoveFocusWithinLayoutControl( layoutControl, currentFocusActor, direction );
518 // Otherwise, just set focus to the next focusable actor
519 succeed = SetCurrentFocusActor( nextFocusableActor );
527 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
529 // Ask the control for the next actor to focus
530 Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
531 if(nextFocusableActor)
533 if(!nextFocusableActor.IsKeyboardFocusable())
535 // If the actor is not focusable, ask the same layout control for the next actor to focus
536 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
540 Actor currentFocusActor = GetCurrentFocusActor();
541 Actor committedFocusActor = nextFocusableActor;
543 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
544 // Signal handler can check the proposed actor and return a different actor if it wishes.
545 if( !mPreFocusChangeSignal.Empty() )
547 mIsWaitingKeyboardFocusChangeCommit = true;
548 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
549 mIsWaitingKeyboardFocusChangeCommit = false;
552 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
554 // Whether the commited focusable actor is a layout control
555 if(IsLayoutControl(committedFocusActor))
557 // If so, move the focus inside it.
558 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
559 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
563 // Otherwise, just set focus to the next focusable actor
564 if(committedFocusActor == nextFocusableActor)
566 // If the application hasn't changed our proposed actor, we informs the layout control we will
567 // move the focus to what the control returns. The control might wish to perform some actions
568 // before the focus is actually moved.
569 GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
572 return SetCurrentFocusActor(committedFocusActor);
583 // No more actor can be focused in the given direction within the same layout control.
588 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
590 bool succeed = false;
592 // Get the parent layout control of the current focus group
593 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
595 while(parentLayoutControl && !succeed)
597 // If the current focus group has a parent layout control, we can probably automatically
598 // move the focus to the next focus group in the forward or backward direction.
599 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
600 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
601 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
604 if(!mFocusGroupChangedSignal.Empty())
606 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
607 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
613 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
617 Toolkit::Control control = Toolkit::Control::DownCast( actor );
620 // Notify the control that enter has been pressed on it.
621 GetImplementation( control ).KeyboardEnter();
624 // Send a notification for the actor.
625 if( !mFocusedActorEnterKeySignal.Empty() )
627 mFocusedActorEnterKeySignal.Emit( actor );
632 void KeyboardFocusManager::ClearFocus()
634 Actor actor = GetCurrentFocusActor();
637 if( mFocusIndicatorActor )
639 actor.Remove( mFocusIndicatorActor );
642 // Send notification for the change of focus actor
643 if( !mFocusChangedSignal.Empty() )
645 mFocusChangedSignal.Emit( actor, Actor() );
648 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast( actor );
649 if( currentlyFocusedControl )
651 currentlyFocusedControl.SetProperty( DevelControl::Property::STATE, DevelControl::NORMAL );
652 currentlyFocusedControl.ClearKeyInputFocus();
656 mCurrentFocusActor.Reset();
657 mIsFocusIndicatorShown = ( mAlwaysShowIndicator == ALWAYS_SHOW ) ? SHOW : HIDE;
660 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
662 mFocusGroupLoopEnabled = enabled;
665 bool KeyboardFocusManager::GetFocusGroupLoop() const
667 return mFocusGroupLoopEnabled;
670 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
674 // Create/Set focus group property.
675 actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
679 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
681 // Check whether the actor is a focus group
682 bool isFocusGroup = false;
686 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
687 if(propertyIsFocusGroup != Property::INVALID_INDEX)
689 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
696 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
698 // Go through the actor's hierarchy to check which focus group the actor belongs to
699 while (actor && !IsFocusGroup(actor))
701 actor = actor.GetParent();
707 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
709 if(mFocusIndicatorActor != indicator)
711 Actor currentFocusActor = GetCurrentFocusActor();
712 if(currentFocusActor)
714 // The new focus indicator should be added to the current focused actor immediately
715 if(mFocusIndicatorActor)
717 currentFocusActor.Remove(mFocusIndicatorActor);
722 currentFocusActor.Add(indicator);
726 mFocusIndicatorActor = indicator;
730 Actor KeyboardFocusManager::GetFocusIndicatorActor()
732 if( ! mFocusIndicatorActor )
734 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
735 const std::string imageDirPath = AssetManager::GetDaliImagePath();
736 mFocusIndicatorActor = Toolkit::ImageView::New( imageDirPath + FOCUS_BORDER_IMAGE_FILE_NAME );
738 // Apply size constraint to the focus indicator
739 mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
742 mFocusIndicatorActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
743 mFocusIndicatorActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
744 mFocusIndicatorActor.SetPosition(0.0f, 0.0f);
746 return mFocusIndicatorActor;
749 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
751 AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
752 bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
754 Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
756 std::string keyName = event.keyPressedName;
758 if( mIsFocusIndicatorShown == UNKNOWN )
760 GetConfigurationFromStyleManger();
763 bool isFocusStartableKey = false;
765 if(event.state == KeyEvent::Down)
767 if (keyName == "Left")
769 if(!isAccessibilityEnabled)
771 if(mIsFocusIndicatorShown == HIDE)
773 // Show focus indicator
774 mIsFocusIndicatorShown = SHOW;
778 // Move the focus towards left
779 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
782 isFocusStartableKey = true;
786 // Move the accessibility focus backward
787 accessibilityManager.MoveFocusBackward();
790 else if (keyName == "Right")
792 if(!isAccessibilityEnabled)
794 if( mIsFocusIndicatorShown == HIDE )
796 // Show focus indicator
797 mIsFocusIndicatorShown = SHOW;
801 // Move the focus towards right
802 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
807 // Move the accessibility focus forward
808 accessibilityManager.MoveFocusForward();
811 isFocusStartableKey = true;
813 else if (keyName == "Up" && !isAccessibilityEnabled)
815 if( mIsFocusIndicatorShown == HIDE )
817 // Show focus indicator
818 mIsFocusIndicatorShown = SHOW;
822 // Move the focus towards up
823 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
826 isFocusStartableKey = true;
828 else if (keyName == "Down" && !isAccessibilityEnabled)
830 if( mIsFocusIndicatorShown == HIDE )
832 // Show focus indicator
833 mIsFocusIndicatorShown = SHOW;
837 // Move the focus towards down
838 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
841 isFocusStartableKey = true;
843 else if (keyName == "Prior" && !isAccessibilityEnabled)
845 if( mIsFocusIndicatorShown == HIDE )
847 // Show focus indicator
848 mIsFocusIndicatorShown = SHOW;
852 // Move the focus towards the previous page
853 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
856 isFocusStartableKey = true;
858 else if (keyName == "Next" && !isAccessibilityEnabled)
860 if( mIsFocusIndicatorShown == HIDE )
862 // Show focus indicator
863 mIsFocusIndicatorShown = SHOW;
867 // Move the focus towards the next page
868 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
871 isFocusStartableKey = true;
873 else if (keyName == "Tab" && !isAccessibilityEnabled)
875 if( mIsFocusIndicatorShown == HIDE )
877 // Show focus indicator
878 mIsFocusIndicatorShown = SHOW;
882 // "Tab" key changes the focus group in the forward direction and
883 // "Shift-Tab" key changes it in the backward direction.
884 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
887 isFocusStartableKey = true;
889 else if (keyName == "space" && !isAccessibilityEnabled)
891 if( mIsFocusIndicatorShown == HIDE )
893 // Show focus indicator
894 mIsFocusIndicatorShown = SHOW;
897 isFocusStartableKey = true;
899 else if (keyName == "" && !isAccessibilityEnabled)
901 // Check the fake key event for evas-plugin case
902 if( mIsFocusIndicatorShown == HIDE )
904 // Show focus indicator
905 mIsFocusIndicatorShown = SHOW;
908 isFocusStartableKey = true;
910 else if (keyName == "Backspace" && !isAccessibilityEnabled)
912 // Emit signal to go back to the previous view???
914 else if (keyName == "Escape" && !isAccessibilityEnabled)
918 else if(event.state == KeyEvent::Up)
920 if (keyName == "Return")
922 if((mIsFocusIndicatorShown == HIDE) && !isAccessibilityEnabled)
924 // Show focus indicator
925 mIsFocusIndicatorShown = SHOW;
929 // The focused actor has enter pressed on it
931 if( !isAccessibilityEnabled )
933 actor = GetCurrentFocusActor();
937 actor = accessibilityManager.GetCurrentFocusActor();
942 DoKeyboardEnter( actor );
946 isFocusStartableKey = true;
950 if(isFocusStartableKey && ( mIsFocusIndicatorShown == SHOW ) && !isAccessibilityEnabled)
952 Actor actor = GetCurrentFocusActor();
955 if( mEnableFocusIndicator == ENABLE )
957 // Make sure the focused actor is highlighted
958 actor.Add( GetFocusIndicatorActor() );
963 // No actor is focused but keyboard focus is activated by the key press
964 // Let's try to move the initial focus
965 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
970 void KeyboardFocusManager::OnTouch(const TouchData& touch)
972 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
973 // Try to load configuration.
974 if( mIsFocusIndicatorShown == UNKNOWN )
976 GetConfigurationFromStyleManger();
979 // Clear the focus when user touch the screen.
980 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
981 // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
982 if( (( touch.GetPointCount() < 1 ) || ( touch.GetState( 0 ) == PointState::DOWN )) && mClearFocusOnTouch )
988 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn )
990 if( focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer() )
992 // Change Current Focused Window
993 Layer rootLayer = window.GetRootLayer();
994 mCurrentFocusedWindow = rootLayer;
996 // Get Current Focused Actor from window
997 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
998 SetCurrentFocusActor( currentFocusedActor );
1000 if( currentFocusedActor && ( mEnableFocusIndicator == ENABLE ) )
1002 // Make sure the focused actor is highlighted
1003 currentFocusedActor.Add( GetFocusIndicatorActor() );
1004 mIsFocusIndicatorShown = SHOW;
1009 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
1011 return mPreFocusChangeSignal;
1014 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1016 return mFocusChangedSignal;
1019 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1021 return mFocusGroupChangedSignal;
1024 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1026 return mFocusedActorEnterKeySignal;
1029 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1031 Dali::BaseHandle handle( object );
1033 bool connected( true );
1034 KeyboardFocusManager* manager = static_cast< KeyboardFocusManager* >( object ); // TypeRegistry guarantees that this is the correct type.
1036 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
1038 manager->PreFocusChangeSignal().Connect( tracker, functor );
1040 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
1042 manager->FocusChangedSignal().Connect( tracker, functor );
1044 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
1046 manager->FocusGroupChangedSignal().Connect( tracker, functor );
1048 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
1050 manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
1054 // signalName does not match any signal
1061 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1063 mCustomAlgorithmInterface = &interface;
1066 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1068 if( !enable && mFocusIndicatorActor )
1070 mFocusIndicatorActor.Unparent();
1073 mEnableFocusIndicator = enable? ENABLE : DISABLE;
1077 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1079 return ( mEnableFocusIndicator == ENABLE );
1082 } // namespace Internal
1084 } // namespace Toolkit