2 * Copyright (c) 2016 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/public-api/animation/constraints.h>
27 #include <dali/public-api/common/stage.h>
28 #include <dali/public-api/events/key-event.h>
29 #include <dali/public-api/events/touch-data.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/images/resource-image.h>
33 #include <dali/integration-api/debug.h>
36 #include <dali-toolkit/public-api/controls/control.h>
37 #include <dali-toolkit/public-api/controls/control-impl.h>
38 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
39 #include <dali-toolkit/public-api/accessibility-manager/accessibility-manager.h>
40 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
51 namespace // Unnamed namespace
54 #if defined(DEBUG_ENABLED)
55 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
58 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
60 const char* const FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "keyboard_focus.9.png";
64 BaseHandle handle = KeyboardFocusManager::Get();
68 SingletonService singletonService( SingletonService::Get() );
69 if ( singletonService )
71 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
72 singletonService.Register( typeid( manager ), manager );
80 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true )
82 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardPreFocusChange", SIGNAL_PRE_FOCUS_CHANGE )
83 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusChanged", SIGNAL_FOCUS_CHANGED )
84 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged", SIGNAL_FOCUS_GROUP_CHANGED )
85 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey", SIGNAL_FOCUSED_ACTOR_ENTER_KEY )
87 DALI_TYPE_REGISTRATION_END()
89 } // unnamed namespace
91 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
93 Toolkit::KeyboardFocusManager manager;
95 SingletonService singletonService( SingletonService::Get() );
96 if ( singletonService )
98 // Check whether the keyboard focus manager is already created
99 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
102 // If so, downcast the handle of singleton to keyboard focus manager
103 manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
110 KeyboardFocusManager::KeyboardFocusManager()
111 : mCurrentFocusActor(0),
112 mFocusIndicatorActor(),
113 mFocusGroupLoopEnabled(false),
114 mIsKeyboardFocusEnabled(false),
115 mIsFocusIndicatorEnabled(false),
116 mIsWaitingKeyboardFocusChangeCommit(false),
119 OnPhysicalKeyboardStatusChanged(PhysicalKeyboard::Get());
121 Toolkit::KeyInputFocusManager::Get().UnhandledKeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
122 Stage::GetCurrent().TouchSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
123 PhysicalKeyboard::Get().StatusChangedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnPhysicalKeyboardStatusChanged);
126 KeyboardFocusManager::~KeyboardFocusManager()
130 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
132 DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
136 return DoSetCurrentFocusActor( actor.GetId() );
142 bool KeyboardFocusManager::DoSetCurrentFocusActor( const unsigned int actorID )
144 Actor rootActor = Stage::GetCurrent().GetRootLayer();
145 Actor actor = rootActor.FindChildById( actorID );
146 bool success = false;
148 // Check whether the actor is in the stage and is keyboard focusable.
149 if( actor && actor.IsKeyboardFocusable() )
151 mIsFocusIndicatorEnabled = true;
152 // Draw the focus indicator upon the focused actor when PhysicalKeyboard is attached
153 if( mIsKeyboardFocusEnabled )
155 actor.Add( GetFocusIndicatorActor() );
157 // Send notification for the change of focus actor
158 if( !mFocusChangedSignal.Empty() )
160 mFocusChangedSignal.Emit(GetCurrentFocusActor(), actor);
163 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
165 // Save the current focused actor
166 mCurrentFocusActor = actorID;
168 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
173 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
179 Actor KeyboardFocusManager::GetCurrentFocusActor()
181 Actor rootActor = Stage::GetCurrent().GetRootLayer();
182 return rootActor.FindChildById(mCurrentFocusActor);
185 Actor KeyboardFocusManager::GetCurrentFocusGroup()
187 return GetFocusGroup(GetCurrentFocusActor());
190 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
192 Toolkit::Control control = Toolkit::Control::DownCast(actor);
193 return control && GetImplementation( control ).IsKeyboardNavigationSupported();
196 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
198 // Get the actor's parent layout control that supports two dimensional keyboard navigation
199 Actor rootActor = Stage::GetCurrent().GetRootLayer();
203 parent = actor.GetParent();
206 while( parent && !IsLayoutControl(parent) && parent != rootActor )
208 parent = parent.GetParent();
211 return Toolkit::Control::DownCast(parent);
214 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
216 Actor currentFocusActor = GetCurrentFocusActor();
218 bool succeed = false;
220 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
221 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
222 while(parentLayoutControl && !succeed)
224 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
225 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
228 if(!succeed && !mPreFocusChangeSignal.Empty())
230 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
231 mIsWaitingKeyboardFocusChangeCommit = true;
232 Actor nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
233 mIsWaitingKeyboardFocusChangeCommit = false;
235 if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
237 // Whether the next focusable actor is a layout control
238 if(IsLayoutControl(nextFocusableActor))
240 // If so, move the focus inside it.
241 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
242 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
246 // Otherwise, just set focus to the next focusable actor
247 succeed = SetCurrentFocusActor(nextFocusableActor);
255 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
257 // Ask the control for the next actor to focus
258 Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
259 if(nextFocusableActor)
261 if(!nextFocusableActor.IsKeyboardFocusable())
263 // If the actor is not focusable, ask the same layout control for the next actor to focus
264 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
268 Actor currentFocusActor = GetCurrentFocusActor();
269 Actor committedFocusActor = nextFocusableActor;
271 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
272 // Signal handler can check the proposed actor and return a different actor if it wishes.
273 if( !mPreFocusChangeSignal.Empty() )
275 mIsWaitingKeyboardFocusChangeCommit = true;
276 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
277 mIsWaitingKeyboardFocusChangeCommit = false;
280 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
282 // Whether the commited focusable actor is a layout control
283 if(IsLayoutControl(committedFocusActor))
285 // If so, move the focus inside it.
286 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
287 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
291 // Otherwise, just set focus to the next focusable actor
292 if(committedFocusActor == nextFocusableActor)
294 // If the application hasn't changed our proposed actor, we informs the layout control we will
295 // move the focus to what the control returns. The control might wish to perform some actions
296 // before the focus is actually moved.
297 GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
300 return SetCurrentFocusActor(committedFocusActor);
311 // No more actor can be focused in the given direction within the same layout control.
316 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
318 bool succeed = false;
320 // Get the parent layout control of the current focus group
321 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
323 while(parentLayoutControl && !succeed)
325 // If the current focus group has a parent layout control, we can probably automatically
326 // move the focus to the next focus group in the forward or backward direction.
327 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
328 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
329 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
332 if(!mFocusGroupChangedSignal.Empty())
334 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
335 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
341 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
345 Toolkit::Control control = Toolkit::Control::DownCast( actor );
348 // Notify the control that enter has been pressed on it.
349 GetImplementation( control ).KeyboardEnter();
352 // Send a notification for the actor.
353 if( !mFocusedActorEnterKeySignal.Empty() )
355 mFocusedActorEnterKeySignal.Emit( actor );
360 void KeyboardFocusManager::ClearFocus()
362 Actor actor = GetCurrentFocusActor();
365 if(mFocusIndicatorActor)
367 actor.Remove(mFocusIndicatorActor);
370 // Send notification for the change of focus actor
371 if( !mFocusChangedSignal.Empty() )
373 mFocusChangedSignal.Emit(actor, Actor());
377 mCurrentFocusActor = 0;
378 mIsFocusIndicatorEnabled = false;
381 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
383 mFocusGroupLoopEnabled = enabled;
386 bool KeyboardFocusManager::GetFocusGroupLoop() const
388 return mFocusGroupLoopEnabled;
391 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
395 // Create/Set focus group property.
396 actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
400 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
402 // Check whether the actor is a focus group
403 bool isFocusGroup = false;
407 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
408 if(propertyIsFocusGroup != Property::INVALID_INDEX)
410 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
417 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
419 // Go through the actor's hierarchy to check which focus group the actor belongs to
420 while (actor && !IsFocusGroup(actor))
422 actor = actor.GetParent();
428 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
430 if(mFocusIndicatorActor != indicator)
432 Actor currentFocusActor = GetCurrentFocusActor();
433 if(currentFocusActor)
435 // The new focus indicator should be added to the current focused actor immediately
436 if(mFocusIndicatorActor)
438 currentFocusActor.Remove(mFocusIndicatorActor);
443 currentFocusActor.Add(indicator);
447 mFocusIndicatorActor = indicator;
451 Actor KeyboardFocusManager::GetFocusIndicatorActor()
453 if( ! mFocusIndicatorActor )
455 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
456 mFocusIndicatorActor = Toolkit::ImageView::New( FOCUS_BORDER_IMAGE_PATH );
457 mFocusIndicatorActor.SetParentOrigin( ParentOrigin::CENTER );
459 // Apply size constraint to the focus indicator
460 mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
463 return mFocusIndicatorActor;
466 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
468 mIsKeyboardFocusEnabled = keyboard.IsAttached();
470 if(mIsKeyboardFocusEnabled)
472 // Show indicator when keyboard focus turned on if there is focused actor.
473 Actor actor = GetCurrentFocusActor();
476 actor.Add( GetFocusIndicatorActor() );
478 mIsFocusIndicatorEnabled = true;
482 // Hide indicator when keyboard focus turned off
483 Actor actor = GetCurrentFocusActor();
484 if( actor && mFocusIndicatorActor )
486 actor.Remove( mFocusIndicatorActor );
488 mIsFocusIndicatorEnabled = false;
492 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
494 if(!mIsKeyboardFocusEnabled)
499 AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
500 bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
502 Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
504 std::string keyName = event.keyPressedName;
506 bool isFocusStartableKey = false;
508 if(event.state == KeyEvent::Down)
510 if (keyName == "Left")
512 if(!isAccessibilityEnabled)
514 if(!mIsFocusIndicatorEnabled)
516 // Show focus indicator
517 mIsFocusIndicatorEnabled = true;
521 // Move the focus towards left
522 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
525 isFocusStartableKey = true;
529 // Move the accessibility focus backward
530 accessibilityManager.MoveFocusBackward();
533 else if (keyName == "Right")
535 if(!isAccessibilityEnabled)
537 if(!mIsFocusIndicatorEnabled)
539 // Show focus indicator
540 mIsFocusIndicatorEnabled = true;
544 // Move the focus towards right
545 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
550 // Move the accessibility focus forward
551 accessibilityManager.MoveFocusForward();
554 isFocusStartableKey = true;
556 else if (keyName == "Up" && !isAccessibilityEnabled)
558 if(!mIsFocusIndicatorEnabled)
560 // Show focus indicator
561 mIsFocusIndicatorEnabled = true;
565 // Move the focus towards up
566 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
569 isFocusStartableKey = true;
571 else if (keyName == "Down" && !isAccessibilityEnabled)
573 if(!mIsFocusIndicatorEnabled)
575 // Show focus indicator
576 mIsFocusIndicatorEnabled = true;
580 // Move the focus towards down
581 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
584 isFocusStartableKey = true;
586 else if (keyName == "Tab" && !isAccessibilityEnabled)
588 if(!mIsFocusIndicatorEnabled)
590 // Show focus indicator
591 mIsFocusIndicatorEnabled = true;
595 // "Tab" key changes the focus group in the forward direction and
596 // "Shift-Tab" key changes it in the backward direction.
597 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
600 isFocusStartableKey = true;
602 else if (keyName == "space" && !isAccessibilityEnabled)
604 if(!mIsFocusIndicatorEnabled)
606 // Show focus indicator
607 mIsFocusIndicatorEnabled = true;
610 isFocusStartableKey = true;
612 else if (keyName == "" && !isAccessibilityEnabled)
614 // Check the fake key event for evas-plugin case
615 if(!mIsFocusIndicatorEnabled)
617 // Show focus indicator
618 mIsFocusIndicatorEnabled = true;
621 isFocusStartableKey = true;
623 else if (keyName == "Backspace" && !isAccessibilityEnabled)
625 // Emit signal to go back to the previous view???
628 else if(event.state == KeyEvent::Up)
630 if (keyName == "Return")
632 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
634 // Show focus indicator
635 mIsFocusIndicatorEnabled = true;
639 // The focused actor has enter pressed on it
641 if( !isAccessibilityEnabled )
643 actor = GetCurrentFocusActor();
647 actor = accessibilityManager.GetCurrentFocusActor();
652 DoKeyboardEnter( actor );
656 isFocusStartableKey = true;
660 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
662 Actor actor = GetCurrentFocusActor();
665 // Make sure the focused actor is highlighted
666 actor.Add( GetFocusIndicatorActor() );
670 // No actor is focused but keyboard focus is activated by the key press
671 // Let's try to move the initial focus
672 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
677 void KeyboardFocusManager::OnTouch(const TouchData& touch)
679 // Clear the focus when user touch the screen.
680 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
681 if( ( touch.GetPointCount() < 1 ) || ( touch.GetState( 0 ) == PointState::DOWN ) )
687 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
689 return mPreFocusChangeSignal;
692 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
694 return mFocusChangedSignal;
697 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
699 return mFocusGroupChangedSignal;
702 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
704 return mFocusedActorEnterKeySignal;
707 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
709 Dali::BaseHandle handle( object );
711 bool connected( true );
712 KeyboardFocusManager* manager = static_cast< KeyboardFocusManager* >( object ); // TypeRegistry guarantees that this is the correct type.
714 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
716 manager->PreFocusChangeSignal().Connect( tracker, functor );
718 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
720 manager->FocusChangedSignal().Connect( tracker, functor );
722 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
724 manager->FocusGroupChangedSignal().Connect( tracker, functor );
726 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
728 manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
732 // signalName does not match any signal
739 } // namespace Internal
741 } // namespace Toolkit