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/adaptor-framework/singleton-service.h>
26 #include <dali/devel-api/adaptor-framework/window-devel.h>
27 #include <dali/devel-api/adaptor-framework/lifecycle-controller.h>
28 #include <dali/public-api/animation/constraints.h>
29 #include <dali/public-api/common/stage.h>
30 #include <dali/public-api/events/key-event.h>
31 #include <dali/public-api/events/touch-data.h>
32 #include <dali/public-api/object/type-registry.h>
33 #include <dali/public-api/object/type-registry-helper.h>
34 #include <dali/public-api/object/property-map.h>
35 #include <dali/public-api/images/resource-image.h>
36 #include <dali/integration-api/adaptors/adaptor.h>
37 #include <dali/integration-api/adaptors/scene-holder.h>
38 #include <dali/integration-api/debug.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_PATH = DALI_IMAGE_DIR "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 widnows
147 Dali::WindowContainer windows = Adaptor::Get().GetWindows();
148 for ( auto iter = windows.begin(); iter != windows.end(); ++iter )
150 DevelWindow::KeyEventSignal( *iter ).Connect( mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
151 DevelWindow::TouchSignal( *iter ).Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch);
154 // Get notified when any new window is created afterwards
155 Adaptor::Get().WindowCreatedSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnWindowCreated);
159 void KeyboardFocusManager::OnWindowCreated( Dali::Window& window )
161 DevelWindow::KeyEventSignal( window ).Connect( mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
162 DevelWindow::TouchSignal( window ).Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch);
165 KeyboardFocusManager::~KeyboardFocusManager()
169 void KeyboardFocusManager::GetConfigurationFromStyleManger()
171 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
174 Property::Map config = Toolkit::DevelStyleManager::GetConfigurations( styleManager );
175 mAlwaysShowIndicator = config["alwaysShowFocus"].Get<bool>() ? ALWAYS_SHOW : NONE;
176 mIsFocusIndicatorShown = ( mAlwaysShowIndicator == ALWAYS_SHOW )? SHOW : HIDE;
177 mClearFocusOnTouch = ( mIsFocusIndicatorShown == SHOW ) ? false : true;
181 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
183 DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
185 if( mIsFocusIndicatorShown == UNKNOWN )
187 GetConfigurationFromStyleManger();
190 return DoSetCurrentFocusActor( actor );
193 bool KeyboardFocusManager::DoSetCurrentFocusActor( Actor actor )
195 bool success = false;
196 if( actor && actor.IsKeyboardFocusable() && actor.OnStage() )
198 Integration::SceneHolder currentWindow = Integration::SceneHolder::Get( actor );
200 if( currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
202 Layer rootLayer = currentWindow.GetRootLayer();
203 mCurrentFocusedWindow = rootLayer;
207 Actor currentFocusedActor = GetCurrentFocusActor();
209 // If developer set focus on same actor, doing nothing
210 if( actor == currentFocusedActor )
219 // Check whether the actor is in the stage and is keyboard focusable.
220 if( actor && actor.IsKeyboardFocusable() && actor.OnStage() )
222 if( ( mIsFocusIndicatorShown == SHOW ) && ( mEnableFocusIndicator == ENABLE ) )
224 actor.Add( GetFocusIndicatorActor() );
227 // Send notification for the change of focus actor
228 if( !mFocusChangedSignal.Empty() )
230 mFocusChangedSignal.Emit(currentFocusedActor, actor);
233 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
234 if( currentlyFocusedControl )
236 // Do we need it to remember if it was previously DISABLED?
237 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL );
238 currentlyFocusedControl.ClearKeyInputFocus();
241 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
243 // Save the current focused actor
244 mCurrentFocusActor = actor;
246 bool focusedWindowFound = false;
247 for( unsigned int i = 0; i < mCurrentFocusActors.size(); i++ )
249 if( mCurrentFocusActors[i].first == mCurrentFocusedWindow )
251 mCurrentFocusActors[i].second = actor;
252 focusedWindowFound = true;
256 if( !focusedWindowFound)
258 // A new window gains the focus, so store the focused actor in that window.
259 mCurrentFocusActors.push_back( std::pair< WeakHandle< Layer>, WeakHandle< Actor > >( mCurrentFocusedWindow , actor ));
262 Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
263 if( newlyFocusedControl )
265 newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED );
266 newlyFocusedControl.SetKeyInputFocus();
269 // Push Current Focused Actor to FocusHistory
270 mFocusHistory.push_back( actor );
272 // Delete first element before add new element when Stack is full.
273 if( mFocusHistory.size() > MAX_HISTORY_AMOUNT )
275 FocusStackIterator beginPos = mFocusHistory.begin();
276 mFocusHistory.erase( beginPos );
279 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
284 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
290 Actor KeyboardFocusManager::GetCurrentFocusActor()
292 Actor actor = mCurrentFocusActor.GetHandle();
294 if( actor && ! actor.OnStage() )
296 // If the actor has been removed from the stage, then it should not be focused
298 mCurrentFocusActor.Reset();
303 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
307 for( index = 0; index < mCurrentFocusActors.size(); index++ )
309 if( mCurrentFocusActors[index].first == mCurrentFocusedWindow )
311 actor = mCurrentFocusActors[index].second.GetHandle();
316 if( actor && ! actor.OnStage() )
318 // If the actor has been removed from the window, then the window doesn't have any focused actor
320 mCurrentFocusActors.erase( mCurrentFocusActors.begin() + index );
326 Actor KeyboardFocusManager::GetCurrentFocusGroup()
328 return GetFocusGroup(GetCurrentFocusActor());
331 void KeyboardFocusManager::MoveFocusBackward()
333 // Find Pre Focused Actor when the list size is more than 1
334 if( mFocusHistory.size() > 1 )
336 // Delete current focused actor in history
337 mFocusHistory.pop_back();
339 // If pre-focused actors are not on stage or deleted, remove them in stack
340 while( mFocusHistory.size() > 0 )
342 // Get pre focused actor
343 Actor target = mFocusHistory[ mFocusHistory.size() -1 ].GetHandle();
345 // Impl of Actor is not null
346 if( target && target.OnStage() )
348 // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
349 mFocusHistory.pop_back();
350 SetCurrentFocusActor( target );
355 // Target is empty handle or off stage. Erase from queue
356 mFocusHistory.pop_back();
360 // if there is no actor which can get focus, then push current focus actor in stack again
361 if( mFocusHistory.size() == 0 )
363 Actor currentFocusedActor = GetCurrentFocusActor();
364 mFocusHistory.push_back( currentFocusedActor );
369 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
371 Toolkit::Control control = Toolkit::Control::DownCast(actor);
372 return control && GetImplementation( control ).IsKeyboardNavigationSupported();
375 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
377 // Get the actor's parent layout control that supports two dimensional keyboard navigation
382 Integration::SceneHolder window = Integration::SceneHolder::Get( actor );
385 rootActor = window.GetRootLayer();
388 parent = actor.GetParent();
391 while( parent && !IsLayoutControl(parent) && parent != rootActor )
393 parent = parent.GetParent();
396 return Toolkit::Control::DownCast(parent);
399 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
401 Actor currentFocusActor = GetCurrentFocusActor();
403 bool succeed = false;
405 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
406 Toolkit::Control parentLayoutControl = GetParentLayoutControl( currentFocusActor );
407 while( parentLayoutControl && !succeed )
409 succeed = DoMoveFocusWithinLayoutControl( parentLayoutControl, currentFocusActor, direction );
410 parentLayoutControl = GetParentLayoutControl( parentLayoutControl );
415 Actor nextFocusableActor;
417 Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
419 // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
420 if( currentFocusControl )
423 Property::Index index = Property::INVALID_INDEX;
424 Property::Value value;
426 // Find property index based upon focus direction
429 case Toolkit::Control::KeyboardFocus::LEFT:
431 index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
434 case Toolkit::Control::KeyboardFocus::RIGHT:
436 index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
439 case Toolkit::Control::KeyboardFocus::UP:
441 index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
444 case Toolkit::Control::KeyboardFocus::DOWN:
446 index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
453 // If the focusable property is set then determine next focusable actor
454 if( index != Property::INVALID_INDEX)
456 value = currentFocusActor.GetProperty( index );
457 actorId = value.Get<int>();
459 // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
462 if( currentFocusActor.GetParent() )
464 nextFocusableActor = currentFocusActor.GetParent().FindChildById( actorId );
467 if( !nextFocusableActor )
469 Integration::SceneHolder window = Integration::SceneHolder::Get( currentFocusActor );
472 nextFocusableActor = window.GetRootLayer().FindChildById( actorId );
479 if( !nextFocusableActor )
481 // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
482 if( mCustomAlgorithmInterface )
484 mIsWaitingKeyboardFocusChangeCommit = true;
485 nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor( currentFocusActor, Actor(), direction );
486 mIsWaitingKeyboardFocusChangeCommit = false;
488 else if( !mPreFocusChangeSignal.Empty() )
490 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
491 mIsWaitingKeyboardFocusChangeCommit = true;
492 nextFocusableActor = mPreFocusChangeSignal.Emit( currentFocusActor, Actor(), direction );
493 mIsWaitingKeyboardFocusChangeCommit = false;
497 if( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
499 // Whether the next focusable actor is a layout control
500 if( IsLayoutControl( nextFocusableActor ) )
502 // If so, move the focus inside it.
503 Toolkit::Control layoutControl = Toolkit::Control::DownCast( nextFocusableActor) ;
504 succeed = DoMoveFocusWithinLayoutControl( layoutControl, currentFocusActor, direction );
508 // Otherwise, just set focus to the next focusable actor
509 succeed = SetCurrentFocusActor( nextFocusableActor );
517 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
519 // Ask the control for the next actor to focus
520 Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
521 if(nextFocusableActor)
523 if(!nextFocusableActor.IsKeyboardFocusable())
525 // If the actor is not focusable, ask the same layout control for the next actor to focus
526 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
530 Actor currentFocusActor = GetCurrentFocusActor();
531 Actor committedFocusActor = nextFocusableActor;
533 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
534 // Signal handler can check the proposed actor and return a different actor if it wishes.
535 if( !mPreFocusChangeSignal.Empty() )
537 mIsWaitingKeyboardFocusChangeCommit = true;
538 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
539 mIsWaitingKeyboardFocusChangeCommit = false;
542 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
544 // Whether the commited focusable actor is a layout control
545 if(IsLayoutControl(committedFocusActor))
547 // If so, move the focus inside it.
548 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
549 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
553 // Otherwise, just set focus to the next focusable actor
554 if(committedFocusActor == nextFocusableActor)
556 // If the application hasn't changed our proposed actor, we informs the layout control we will
557 // move the focus to what the control returns. The control might wish to perform some actions
558 // before the focus is actually moved.
559 GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
562 return SetCurrentFocusActor(committedFocusActor);
573 // No more actor can be focused in the given direction within the same layout control.
578 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
580 bool succeed = false;
582 // Get the parent layout control of the current focus group
583 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
585 while(parentLayoutControl && !succeed)
587 // If the current focus group has a parent layout control, we can probably automatically
588 // move the focus to the next focus group in the forward or backward direction.
589 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
590 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
591 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
594 if(!mFocusGroupChangedSignal.Empty())
596 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
597 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
603 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
607 Toolkit::Control control = Toolkit::Control::DownCast( actor );
610 // Notify the control that enter has been pressed on it.
611 GetImplementation( control ).KeyboardEnter();
614 // Send a notification for the actor.
615 if( !mFocusedActorEnterKeySignal.Empty() )
617 mFocusedActorEnterKeySignal.Emit( actor );
622 void KeyboardFocusManager::ClearFocus()
624 Actor actor = GetCurrentFocusActor();
627 if( mFocusIndicatorActor )
629 actor.Remove( mFocusIndicatorActor );
632 // Send notification for the change of focus actor
633 if( !mFocusChangedSignal.Empty() )
635 mFocusChangedSignal.Emit( actor, Actor() );
638 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast( actor );
639 if( currentlyFocusedControl )
641 currentlyFocusedControl.SetProperty( DevelControl::Property::STATE, DevelControl::NORMAL );
642 currentlyFocusedControl.ClearKeyInputFocus();
646 mCurrentFocusActor.Reset();
647 mIsFocusIndicatorShown = ( mAlwaysShowIndicator == ALWAYS_SHOW ) ? SHOW : HIDE;
650 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
652 mFocusGroupLoopEnabled = enabled;
655 bool KeyboardFocusManager::GetFocusGroupLoop() const
657 return mFocusGroupLoopEnabled;
660 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
664 // Create/Set focus group property.
665 actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
669 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
671 // Check whether the actor is a focus group
672 bool isFocusGroup = false;
676 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
677 if(propertyIsFocusGroup != Property::INVALID_INDEX)
679 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
686 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
688 // Go through the actor's hierarchy to check which focus group the actor belongs to
689 while (actor && !IsFocusGroup(actor))
691 actor = actor.GetParent();
697 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
699 if(mFocusIndicatorActor != indicator)
701 Actor currentFocusActor = GetCurrentFocusActor();
702 if(currentFocusActor)
704 // The new focus indicator should be added to the current focused actor immediately
705 if(mFocusIndicatorActor)
707 currentFocusActor.Remove(mFocusIndicatorActor);
712 currentFocusActor.Add(indicator);
716 mFocusIndicatorActor = indicator;
720 Actor KeyboardFocusManager::GetFocusIndicatorActor()
722 if( ! mFocusIndicatorActor )
724 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
725 mFocusIndicatorActor = Toolkit::ImageView::New( FOCUS_BORDER_IMAGE_PATH );
727 // Apply size constraint to the focus indicator
728 mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
731 mFocusIndicatorActor.SetParentOrigin( ParentOrigin::CENTER );
732 mFocusIndicatorActor.SetAnchorPoint( AnchorPoint::CENTER );
733 mFocusIndicatorActor.SetPosition(0.0f, 0.0f);
735 return mFocusIndicatorActor;
738 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
740 AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
741 bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
743 Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
745 std::string keyName = event.keyPressedName;
747 if( mIsFocusIndicatorShown == UNKNOWN )
749 GetConfigurationFromStyleManger();
752 bool isFocusStartableKey = false;
754 if(event.state == KeyEvent::Down)
756 if (keyName == "Left")
758 if(!isAccessibilityEnabled)
760 if(mIsFocusIndicatorShown == HIDE)
762 // Show focus indicator
763 mIsFocusIndicatorShown = SHOW;
767 // Move the focus towards left
768 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
771 isFocusStartableKey = true;
775 // Move the accessibility focus backward
776 accessibilityManager.MoveFocusBackward();
779 else if (keyName == "Right")
781 if(!isAccessibilityEnabled)
783 if( mIsFocusIndicatorShown == HIDE )
785 // Show focus indicator
786 mIsFocusIndicatorShown = SHOW;
790 // Move the focus towards right
791 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
796 // Move the accessibility focus forward
797 accessibilityManager.MoveFocusForward();
800 isFocusStartableKey = true;
802 else if (keyName == "Up" && !isAccessibilityEnabled)
804 if( mIsFocusIndicatorShown == HIDE )
806 // Show focus indicator
807 mIsFocusIndicatorShown = SHOW;
811 // Move the focus towards up
812 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
815 isFocusStartableKey = true;
817 else if (keyName == "Down" && !isAccessibilityEnabled)
819 if( mIsFocusIndicatorShown == HIDE )
821 // Show focus indicator
822 mIsFocusIndicatorShown = SHOW;
826 // Move the focus towards down
827 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
830 isFocusStartableKey = true;
832 else if (keyName == "Prior" && !isAccessibilityEnabled)
834 if( mIsFocusIndicatorShown == HIDE )
836 // Show focus indicator
837 mIsFocusIndicatorShown = SHOW;
841 // Move the focus towards the previous page
842 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
845 isFocusStartableKey = true;
847 else if (keyName == "Next" && !isAccessibilityEnabled)
849 if( mIsFocusIndicatorShown == HIDE )
851 // Show focus indicator
852 mIsFocusIndicatorShown = SHOW;
856 // Move the focus towards the next page
857 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
860 isFocusStartableKey = true;
862 else if (keyName == "Tab" && !isAccessibilityEnabled)
864 if( mIsFocusIndicatorShown == HIDE )
866 // Show focus indicator
867 mIsFocusIndicatorShown = SHOW;
871 // "Tab" key changes the focus group in the forward direction and
872 // "Shift-Tab" key changes it in the backward direction.
873 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
876 isFocusStartableKey = true;
878 else if (keyName == "space" && !isAccessibilityEnabled)
880 if( mIsFocusIndicatorShown == HIDE )
882 // Show focus indicator
883 mIsFocusIndicatorShown = SHOW;
886 isFocusStartableKey = true;
888 else if (keyName == "" && !isAccessibilityEnabled)
890 // Check the fake key event for evas-plugin case
891 if( mIsFocusIndicatorShown == HIDE )
893 // Show focus indicator
894 mIsFocusIndicatorShown = SHOW;
897 isFocusStartableKey = true;
899 else if (keyName == "Backspace" && !isAccessibilityEnabled)
901 // Emit signal to go back to the previous view???
903 else if (keyName == "Escape" && !isAccessibilityEnabled)
907 else if(event.state == KeyEvent::Up)
909 if (keyName == "Return")
911 if((mIsFocusIndicatorShown == HIDE) && !isAccessibilityEnabled)
913 // Show focus indicator
914 mIsFocusIndicatorShown = SHOW;
918 // The focused actor has enter pressed on it
920 if( !isAccessibilityEnabled )
922 actor = GetCurrentFocusActor();
926 actor = accessibilityManager.GetCurrentFocusActor();
931 DoKeyboardEnter( actor );
935 isFocusStartableKey = true;
939 if(isFocusStartableKey && ( mIsFocusIndicatorShown == SHOW ) && !isAccessibilityEnabled)
941 Actor actor = GetCurrentFocusActor();
944 if( mEnableFocusIndicator == ENABLE )
946 // Make sure the focused actor is highlighted
947 actor.Add( GetFocusIndicatorActor() );
952 // No actor is focused but keyboard focus is activated by the key press
953 // Let's try to move the initial focus
954 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
959 void KeyboardFocusManager::OnTouch(const TouchData& touch)
961 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
962 // Try to load configuration.
963 if( mIsFocusIndicatorShown == UNKNOWN )
965 GetConfigurationFromStyleManger();
968 // Clear the focus when user touch the screen.
969 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
970 // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
971 if( (( touch.GetPointCount() < 1 ) || ( touch.GetState( 0 ) == PointState::DOWN )) && mClearFocusOnTouch )
977 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn )
979 if( focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer() )
981 // Change Current Focused Window
982 Layer rootLayer = window.GetRootLayer();
983 mCurrentFocusedWindow = rootLayer;
985 // Get Current Focused Actor from window
986 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
987 SetCurrentFocusActor( currentFocusedActor );
989 if( currentFocusedActor && ( mEnableFocusIndicator == ENABLE ) )
991 // Make sure the focused actor is highlighted
992 currentFocusedActor.Add( GetFocusIndicatorActor() );
993 mIsFocusIndicatorShown = SHOW;
998 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
1000 return mPreFocusChangeSignal;
1003 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
1005 return mFocusChangedSignal;
1008 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
1010 return mFocusGroupChangedSignal;
1013 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1015 return mFocusedActorEnterKeySignal;
1018 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1020 Dali::BaseHandle handle( object );
1022 bool connected( true );
1023 KeyboardFocusManager* manager = static_cast< KeyboardFocusManager* >( object ); // TypeRegistry guarantees that this is the correct type.
1025 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
1027 manager->PreFocusChangeSignal().Connect( tracker, functor );
1029 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
1031 manager->FocusChangedSignal().Connect( tracker, functor );
1033 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
1035 manager->FocusGroupChangedSignal().Connect( tracker, functor );
1037 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
1039 manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
1043 // signalName does not match any signal
1050 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1052 mCustomAlgorithmInterface = &interface;
1055 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1057 if( !enable && mFocusIndicatorActor )
1059 mFocusIndicatorActor.Unparent();
1062 mEnableFocusIndicator = enable? ENABLE : DISABLE;
1066 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1068 return ( mEnableFocusIndicator == ENABLE );
1071 } // namespace Internal
1073 } // namespace Toolkit