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/singleton-service.h>
25 #include <dali/devel-api/adaptor-framework/window-devel.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/adaptors/adaptor.h>
36 #include <dali/integration-api/adaptors/scene-holder.h>
37 #include <dali/integration-api/debug.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/controls/control-devel.h>
44 #include <dali-toolkit/public-api/styling/style-manager.h>
45 #include <dali-toolkit/devel-api/styling/style-manager-devel.h>
46 #include <dali/devel-api/adaptor-framework/accessibility.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_PATH = DALI_IMAGE_DIR "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 )
136 // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorShown.
138 LifecycleController::Get().InitSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnAdaptorInit );
141 void KeyboardFocusManager::OnAdaptorInit()
143 if( Adaptor::IsAvailable() )
145 // Retrieve all the existing widnows
146 Dali::WindowContainer windows = Adaptor::Get().GetWindows();
147 for ( auto iter = windows.begin(); iter != windows.end(); ++iter )
149 DevelWindow::KeyEventSignal( *iter ).Connect( mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
150 DevelWindow::TouchSignal( *iter ).Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch);
153 // Get notified when any new window is created afterwards
154 Adaptor::Get().WindowCreatedSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnWindowCreated);
158 void KeyboardFocusManager::OnWindowCreated( Dali::Window& window )
160 DevelWindow::KeyEventSignal( window ).Connect( mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
161 DevelWindow::TouchSignal( window ).Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch);
164 KeyboardFocusManager::~KeyboardFocusManager()
168 void KeyboardFocusManager::GetConfigurationFromStyleManger()
170 Toolkit::StyleManager styleManager = Toolkit::StyleManager::Get();
173 Property::Map config = Toolkit::DevelStyleManager::GetConfigurations( styleManager );
174 mAlwaysShowIndicator = config["alwaysShowFocus"].Get<bool>() ? ALWAYS_SHOW : NONE;
175 mIsFocusIndicatorShown = ( mAlwaysShowIndicator == ALWAYS_SHOW )? SHOW : HIDE;
176 mClearFocusOnTouch = ( mIsFocusIndicatorShown == SHOW ) ? false : true;
180 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
182 DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
184 if( mIsFocusIndicatorShown == UNKNOWN )
186 GetConfigurationFromStyleManger();
189 return DoSetCurrentFocusActor( actor );
192 bool KeyboardFocusManager::DoSetCurrentFocusActor( Actor actor )
194 bool success = false;
195 if( actor && actor.IsKeyboardFocusable() && actor.OnStage() )
197 Integration::SceneHolder currentWindow = Integration::SceneHolder::Get( actor );
199 if( currentWindow.GetRootLayer() != mCurrentFocusedWindow.GetHandle())
201 Layer rootLayer = currentWindow.GetRootLayer();
202 mCurrentFocusedWindow = rootLayer;
206 Actor currentFocusedActor = GetCurrentFocusActor();
208 // If developer set focus on same actor, doing nothing
209 if( actor == currentFocusedActor )
218 // Check whether the actor is in the stage and is keyboard focusable.
219 if( actor && actor.IsKeyboardFocusable() && actor.OnStage() )
221 if( ( mIsFocusIndicatorShown == SHOW ) && ( mEnableFocusIndicator == ENABLE ) )
223 actor.Add( GetFocusIndicatorActor() );
226 // Send notification for the change of focus actor
227 if( !mFocusChangedSignal.Empty() )
229 mFocusChangedSignal.Emit(currentFocusedActor, actor);
232 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast(currentFocusedActor);
233 if( currentlyFocusedControl )
235 // Do we need it to remember if it was previously DISABLED?
236 currentlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL );
237 currentlyFocusedControl.ClearKeyInputFocus();
240 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
242 // Save the current focused actor
243 mCurrentFocusActor = actor;
245 bool focusedWindowFound = false;
246 for( unsigned int i = 0; i < mCurrentFocusActors.size(); i++ )
248 if( mCurrentFocusActors[i].first == mCurrentFocusedWindow )
250 mCurrentFocusActors[i].second = actor;
251 focusedWindowFound = true;
255 if( !focusedWindowFound)
257 // A new window gains the focus, so store the focused actor in that window.
258 mCurrentFocusActors.push_back( std::pair< WeakHandle< Layer>, WeakHandle< Actor > >( mCurrentFocusedWindow , actor ));
261 Toolkit::Control newlyFocusedControl = Toolkit::Control::DownCast(actor);
262 if( newlyFocusedControl )
264 newlyFocusedControl.SetProperty(DevelControl::Property::STATE, DevelControl::FOCUSED );
265 newlyFocusedControl.SetKeyInputFocus();
268 // Push Current Focused Actor to FocusHistory
269 mFocusHistory.push_back( actor );
271 // Delete first element before add new element when Stack is full.
272 if( mFocusHistory.size() > MAX_HISTORY_AMOUNT )
274 FocusStackIterator beginPos = mFocusHistory.begin();
275 mFocusHistory.erase( beginPos );
278 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
283 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
289 Actor KeyboardFocusManager::GetCurrentFocusActor()
291 Actor actor = mCurrentFocusActor.GetHandle();
293 if( actor && ! actor.OnStage() )
295 // If the actor has been removed from the stage, then it should not be focused
297 mCurrentFocusActor.Reset();
302 Actor KeyboardFocusManager::GetFocusActorFromCurrentWindow()
306 for( index = 0; index < mCurrentFocusActors.size(); index++ )
308 if( mCurrentFocusActors[index].first == mCurrentFocusedWindow )
310 actor = mCurrentFocusActors[index].second.GetHandle();
315 if( actor && ! actor.OnStage() )
317 // If the actor has been removed from the window, then the window doesn't have any focused actor
319 mCurrentFocusActors.erase( mCurrentFocusActors.begin() + index );
325 Actor KeyboardFocusManager::GetCurrentFocusGroup()
327 return GetFocusGroup(GetCurrentFocusActor());
330 void KeyboardFocusManager::MoveFocusBackward()
332 // Find Pre Focused Actor when the list size is more than 1
333 if( mFocusHistory.size() > 1 )
335 // Delete current focused actor in history
336 mFocusHistory.pop_back();
338 // If pre-focused actors are not on stage or deleted, remove them in stack
339 while( mFocusHistory.size() > 0 )
341 // Get pre focused actor
342 Actor target = mFocusHistory[ mFocusHistory.size() -1 ].GetHandle();
344 // Impl of Actor is not null
345 if( target && target.OnStage() )
347 // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
348 mFocusHistory.pop_back();
349 SetCurrentFocusActor( target );
354 // Target is empty handle or off stage. Erase from queue
355 mFocusHistory.pop_back();
359 // if there is no actor which can get focus, then push current focus actor in stack again
360 if( mFocusHistory.size() == 0 )
362 Actor currentFocusedActor = GetCurrentFocusActor();
363 mFocusHistory.push_back( currentFocusedActor );
368 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
370 Toolkit::Control control = Toolkit::Control::DownCast(actor);
371 return control && GetImplementation( control ).IsKeyboardNavigationSupported();
374 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
376 // Get the actor's parent layout control that supports two dimensional keyboard navigation
381 Integration::SceneHolder window = Integration::SceneHolder::Get( actor );
384 rootActor = window.GetRootLayer();
387 parent = actor.GetParent();
390 while( parent && !IsLayoutControl(parent) && parent != rootActor )
392 parent = parent.GetParent();
395 return Toolkit::Control::DownCast(parent);
398 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
400 Actor currentFocusActor = GetCurrentFocusActor();
402 bool succeed = false;
404 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
405 Toolkit::Control parentLayoutControl = GetParentLayoutControl( currentFocusActor );
406 while( parentLayoutControl && !succeed )
408 succeed = DoMoveFocusWithinLayoutControl( parentLayoutControl, currentFocusActor, direction );
409 parentLayoutControl = GetParentLayoutControl( parentLayoutControl );
414 Actor nextFocusableActor;
416 Toolkit::Control currentFocusControl = Toolkit::Control::DownCast(currentFocusActor);
418 // If the current focused actor is a control, then find the next focusable actor via the focusable properties.
419 if( currentFocusControl )
422 Property::Index index = Property::INVALID_INDEX;
423 Property::Value value;
425 // Find property index based upon focus direction
428 case Toolkit::Control::KeyboardFocus::LEFT:
430 index = Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID;
433 case Toolkit::Control::KeyboardFocus::RIGHT:
435 index = Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID;
438 case Toolkit::Control::KeyboardFocus::UP:
440 index = Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID;
443 case Toolkit::Control::KeyboardFocus::DOWN:
445 index = Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID;
452 // If the focusable property is set then determine next focusable actor
453 if( index != Property::INVALID_INDEX)
455 value = currentFocusActor.GetProperty( index );
456 actorId = value.Get<int>();
458 // If actor's id is valid then find actor form actor's id. The actor should be on the stage.
461 if( currentFocusActor.GetParent() )
463 nextFocusableActor = currentFocusActor.GetParent().FindChildById( actorId );
466 if( !nextFocusableActor )
468 Integration::SceneHolder window = Integration::SceneHolder::Get( currentFocusActor );
471 nextFocusableActor = window.GetRootLayer().FindChildById( actorId );
478 if( !nextFocusableActor )
480 // If the implementation of CustomAlgorithmInterface is provided then the PreFocusChangeSignal is no longer emitted.
481 if( mCustomAlgorithmInterface )
483 mIsWaitingKeyboardFocusChangeCommit = true;
484 nextFocusableActor = mCustomAlgorithmInterface->GetNextFocusableActor( currentFocusActor, Actor(), direction );
485 mIsWaitingKeyboardFocusChangeCommit = false;
487 else if( !mPreFocusChangeSignal.Empty() )
489 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
490 mIsWaitingKeyboardFocusChangeCommit = true;
491 nextFocusableActor = mPreFocusChangeSignal.Emit( currentFocusActor, Actor(), direction );
492 mIsWaitingKeyboardFocusChangeCommit = false;
496 if( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
498 // Whether the next focusable actor is a layout control
499 if( IsLayoutControl( nextFocusableActor ) )
501 // If so, move the focus inside it.
502 Toolkit::Control layoutControl = Toolkit::Control::DownCast( nextFocusableActor) ;
503 succeed = DoMoveFocusWithinLayoutControl( layoutControl, currentFocusActor, direction );
507 // Otherwise, just set focus to the next focusable actor
508 succeed = SetCurrentFocusActor( nextFocusableActor );
516 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
518 // Ask the control for the next actor to focus
519 Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
520 if(nextFocusableActor)
522 if(!nextFocusableActor.IsKeyboardFocusable())
524 // If the actor is not focusable, ask the same layout control for the next actor to focus
525 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
529 Actor currentFocusActor = GetCurrentFocusActor();
530 Actor committedFocusActor = nextFocusableActor;
532 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
533 // Signal handler can check the proposed actor and return a different actor if it wishes.
534 if( !mPreFocusChangeSignal.Empty() )
536 mIsWaitingKeyboardFocusChangeCommit = true;
537 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
538 mIsWaitingKeyboardFocusChangeCommit = false;
541 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
543 // Whether the commited focusable actor is a layout control
544 if(IsLayoutControl(committedFocusActor))
546 // If so, move the focus inside it.
547 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
548 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
552 // Otherwise, just set focus to the next focusable actor
553 if(committedFocusActor == nextFocusableActor)
555 // If the application hasn't changed our proposed actor, we informs the layout control we will
556 // move the focus to what the control returns. The control might wish to perform some actions
557 // before the focus is actually moved.
558 GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
561 return SetCurrentFocusActor(committedFocusActor);
572 // No more actor can be focused in the given direction within the same layout control.
577 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
579 bool succeed = false;
581 // Get the parent layout control of the current focus group
582 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
584 while(parentLayoutControl && !succeed)
586 // If the current focus group has a parent layout control, we can probably automatically
587 // move the focus to the next focus group in the forward or backward direction.
588 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
589 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
590 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
593 if(!mFocusGroupChangedSignal.Empty())
595 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
596 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
602 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
606 Toolkit::Control control = Toolkit::Control::DownCast( actor );
609 // Notify the control that enter has been pressed on it.
610 GetImplementation( control ).KeyboardEnter();
613 // Send a notification for the actor.
614 if( !mFocusedActorEnterKeySignal.Empty() )
616 mFocusedActorEnterKeySignal.Emit( actor );
621 void KeyboardFocusManager::ClearFocus()
623 Actor actor = GetCurrentFocusActor();
626 if( mFocusIndicatorActor )
628 actor.Remove( mFocusIndicatorActor );
631 // Send notification for the change of focus actor
632 if( !mFocusChangedSignal.Empty() )
634 mFocusChangedSignal.Emit( actor, Actor() );
637 Toolkit::Control currentlyFocusedControl = Toolkit::Control::DownCast( actor );
638 if( currentlyFocusedControl )
640 currentlyFocusedControl.SetProperty( DevelControl::Property::STATE, DevelControl::NORMAL );
641 currentlyFocusedControl.ClearKeyInputFocus();
645 mCurrentFocusActor.Reset();
646 mIsFocusIndicatorShown = ( mAlwaysShowIndicator == ALWAYS_SHOW ) ? SHOW : HIDE;
649 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
651 mFocusGroupLoopEnabled = enabled;
654 bool KeyboardFocusManager::GetFocusGroupLoop() const
656 return mFocusGroupLoopEnabled;
659 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
663 // Create/Set focus group property.
664 actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
668 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
670 // Check whether the actor is a focus group
671 bool isFocusGroup = false;
675 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
676 if(propertyIsFocusGroup != Property::INVALID_INDEX)
678 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
685 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
687 // Go through the actor's hierarchy to check which focus group the actor belongs to
688 while (actor && !IsFocusGroup(actor))
690 actor = actor.GetParent();
696 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
698 if(mFocusIndicatorActor != indicator)
700 Actor currentFocusActor = GetCurrentFocusActor();
701 if(currentFocusActor)
703 // The new focus indicator should be added to the current focused actor immediately
704 if(mFocusIndicatorActor)
706 currentFocusActor.Remove(mFocusIndicatorActor);
711 currentFocusActor.Add(indicator);
715 mFocusIndicatorActor = indicator;
719 Actor KeyboardFocusManager::GetFocusIndicatorActor()
721 if( ! mFocusIndicatorActor )
723 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
724 mFocusIndicatorActor = Toolkit::ImageView::New( FOCUS_BORDER_IMAGE_PATH );
726 // Apply size constraint to the focus indicator
727 mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
730 mFocusIndicatorActor.SetParentOrigin( ParentOrigin::CENTER );
731 mFocusIndicatorActor.SetAnchorPoint( AnchorPoint::CENTER );
732 mFocusIndicatorActor.SetPosition(0.0f, 0.0f);
734 return mFocusIndicatorActor;
737 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
739 std::string keyName = event.keyPressedName;
741 if( mIsFocusIndicatorShown == UNKNOWN )
743 GetConfigurationFromStyleManger();
746 bool isFocusStartableKey = false;
748 if(event.state == KeyEvent::Down)
750 if (keyName == "Left")
752 if(!mIsFocusIndicatorShown)
754 if(mIsFocusIndicatorShown == HIDE)
756 // Show focus indicator
757 mIsFocusIndicatorShown = SHOW;
761 // Move the focus towards left
762 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
765 isFocusStartableKey = true;
769 // Move the focus towards left
770 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
773 isFocusStartableKey = true;
775 else if (keyName == "Right")
777 if(!mIsFocusIndicatorShown)
779 if( mIsFocusIndicatorShown == HIDE )
781 // Show focus indicator
782 mIsFocusIndicatorShown = SHOW;
786 // Move the focus towards right
787 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
792 // Move the focus towards right
793 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
796 isFocusStartableKey = true;
798 else if (keyName == "Up")
800 if( mIsFocusIndicatorShown == HIDE )
802 // Show focus indicator
803 mIsFocusIndicatorShown = SHOW;
807 // Move the focus towards up
808 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
811 isFocusStartableKey = true;
813 else if (keyName == "Down")
815 if( mIsFocusIndicatorShown == HIDE )
817 // Show focus indicator
818 mIsFocusIndicatorShown = SHOW;
822 // Move the focus towards down
823 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
826 isFocusStartableKey = true;
828 else if (keyName == "Prior")
830 if( mIsFocusIndicatorShown == HIDE )
832 // Show focus indicator
833 mIsFocusIndicatorShown = SHOW;
837 // Move the focus towards the previous page
838 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
841 isFocusStartableKey = true;
843 else if (keyName == "Next")
845 if( mIsFocusIndicatorShown == HIDE )
847 // Show focus indicator
848 mIsFocusIndicatorShown = SHOW;
852 // Move the focus towards the next page
853 MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
856 isFocusStartableKey = true;
858 else if (keyName == "Tab")
860 if( mIsFocusIndicatorShown == HIDE )
862 // Show focus indicator
863 mIsFocusIndicatorShown = SHOW;
867 // "Tab" key changes the focus group in the forward direction and
868 // "Shift-Tab" key changes it in the backward direction.
869 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
872 isFocusStartableKey = true;
874 else if (keyName == "space")
876 if( mIsFocusIndicatorShown == HIDE )
878 // Show focus indicator
879 mIsFocusIndicatorShown = SHOW;
882 isFocusStartableKey = true;
884 else if (keyName == "")
886 // Check the fake key event for evas-plugin case
887 if( mIsFocusIndicatorShown == HIDE )
889 // Show focus indicator
890 mIsFocusIndicatorShown = SHOW;
893 isFocusStartableKey = true;
895 else if (keyName == "Backspace")
897 // Emit signal to go back to the previous view???
899 else if (keyName == "Escape")
903 else if(event.state == KeyEvent::Up)
905 if (keyName == "Return")
907 if( mIsFocusIndicatorShown == HIDE )
909 // Show focus indicator
910 mIsFocusIndicatorShown = SHOW;
914 // The focused actor has enter pressed on it
915 Actor actor = GetCurrentFocusActor();
918 DoKeyboardEnter( actor );
922 isFocusStartableKey = true;
926 if( isFocusStartableKey && mIsFocusIndicatorShown == SHOW )
928 Actor actor = GetCurrentFocusActor();
931 if( mEnableFocusIndicator == ENABLE )
933 // Make sure the focused actor is highlighted
934 actor.Add( GetFocusIndicatorActor() );
939 // No actor is focused but keyboard focus is activated by the key press
940 // Let's try to move the initial focus
941 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
947 void KeyboardFocusManager::OnTouch(const TouchData& touch)
949 // if mIsFocusIndicatorShown is UNKNOWN, it means Configuration is not loaded.
950 // Try to load configuration.
951 if( mIsFocusIndicatorShown == UNKNOWN )
953 GetConfigurationFromStyleManger();
956 // Clear the focus when user touch the screen.
957 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
958 // If mClearFocusOnTouch is false, do not clear the focus even if user touch the screen.
959 if( (( touch.GetPointCount() < 1 ) || ( touch.GetState( 0 ) == PointState::DOWN )) && mClearFocusOnTouch )
965 void KeyboardFocusManager::OnWindowFocusChanged(Window window, bool focusIn )
967 if( focusIn && mCurrentFocusedWindow.GetHandle() != window.GetRootLayer() )
969 // Change Current Focused Window
970 Layer rootLayer = window.GetRootLayer();
971 mCurrentFocusedWindow = rootLayer;
973 // Get Current Focused Actor from window
974 Actor currentFocusedActor = GetFocusActorFromCurrentWindow();
975 SetCurrentFocusActor( currentFocusedActor );
977 if( currentFocusedActor && ( mEnableFocusIndicator == ENABLE ) )
979 // Make sure the focused actor is highlighted
980 currentFocusedActor.Add( GetFocusIndicatorActor() );
981 mIsFocusIndicatorShown = SHOW;
986 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
988 return mPreFocusChangeSignal;
991 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
993 return mFocusChangedSignal;
996 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
998 return mFocusGroupChangedSignal;
1001 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
1003 return mFocusedActorEnterKeySignal;
1006 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1008 Dali::BaseHandle handle( object );
1010 bool connected( true );
1011 KeyboardFocusManager* manager = static_cast< KeyboardFocusManager* >( object ); // TypeRegistry guarantees that this is the correct type.
1013 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
1015 manager->PreFocusChangeSignal().Connect( tracker, functor );
1017 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
1019 manager->FocusChangedSignal().Connect( tracker, functor );
1021 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
1023 manager->FocusGroupChangedSignal().Connect( tracker, functor );
1025 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
1027 manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
1031 // signalName does not match any signal
1038 void KeyboardFocusManager::SetCustomAlgorithm(CustomAlgorithmInterface& interface)
1040 mCustomAlgorithmInterface = &interface;
1043 void KeyboardFocusManager::EnableFocusIndicator(bool enable)
1045 if( !enable && mFocusIndicatorActor )
1047 mFocusIndicatorActor.Unparent();
1050 mEnableFocusIndicator = enable? ENABLE : DISABLE;
1054 bool KeyboardFocusManager::IsFocusIndicatorEnabled() const
1056 return ( mEnableFocusIndicator == ENABLE );
1059 } // namespace Internal
1061 } // namespace Toolkit