2 * Copyright (c) 2014 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(Actor()),
113 mFocusGroupLoopEnabled(false),
114 mIsKeyboardFocusEnabled(false),
115 mIsFocusIndicatorEnabled(false),
116 mIsWaitingKeyboardFocusChangeCommit(false),
119 CreateDefaultFocusIndicatorActor();
121 OnPhysicalKeyboardStatusChanged(PhysicalKeyboard::Get());
123 Toolkit::KeyInputFocusManager::Get().UnhandledKeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
124 Stage::GetCurrent().TouchSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
125 PhysicalKeyboard::Get().StatusChangedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnPhysicalKeyboardStatusChanged);
128 KeyboardFocusManager::~KeyboardFocusManager()
132 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
134 DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
138 return DoSetCurrentFocusActor( actor.GetId() );
144 bool KeyboardFocusManager::DoSetCurrentFocusActor( const unsigned int actorID )
146 Actor rootActor = Stage::GetCurrent().GetRootLayer();
147 Actor actor = rootActor.FindChildById( actorID );
148 bool success = false;
150 // Check whether the actor is in the stage and is keyboard focusable.
151 if( actor && actor.IsKeyboardFocusable() )
153 mIsFocusIndicatorEnabled = true;
154 // Draw the focus indicator upon the focused actor
155 if( mFocusIndicatorActor )
157 actor.Add( mFocusIndicatorActor );
160 // Send notification for the change of focus actor
161 if( !mFocusChangedSignal.Empty() )
163 mFocusChangedSignal.Emit(GetCurrentFocusActor(), actor);
166 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
168 // Save the current focused actor
169 mCurrentFocusActor = actorID;
171 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
176 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
182 Actor KeyboardFocusManager::GetCurrentFocusActor()
184 Actor rootActor = Stage::GetCurrent().GetRootLayer();
185 return rootActor.FindChildById(mCurrentFocusActor);
188 Actor KeyboardFocusManager::GetCurrentFocusGroup()
190 return GetFocusGroup(GetCurrentFocusActor());
193 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
195 Toolkit::Control control = Toolkit::Control::DownCast(actor);
196 return control && GetImplementation( control ).IsKeyboardNavigationSupported();
199 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
201 // Get the actor's parent layout control that supports two dimensional keyboard navigation
202 Actor rootActor = Stage::GetCurrent().GetRootLayer();
206 parent = actor.GetParent();
209 while( parent && !IsLayoutControl(parent) && parent != rootActor )
211 parent = parent.GetParent();
214 return Toolkit::Control::DownCast(parent);
217 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
219 Actor currentFocusActor = GetCurrentFocusActor();
221 bool succeed = false;
223 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
224 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
225 while(parentLayoutControl && !succeed)
227 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
228 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
231 if(!succeed && !mPreFocusChangeSignal.Empty())
233 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
234 mIsWaitingKeyboardFocusChangeCommit = true;
235 Actor nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
236 mIsWaitingKeyboardFocusChangeCommit = false;
238 if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
240 // Whether the next focusable actor is a layout control
241 if(IsLayoutControl(nextFocusableActor))
243 // If so, move the focus inside it.
244 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
245 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
249 // Otherwise, just set focus to the next focusable actor
250 succeed = SetCurrentFocusActor(nextFocusableActor);
258 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
260 // Ask the control for the next actor to focus
261 Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
262 if(nextFocusableActor)
264 if(!nextFocusableActor.IsKeyboardFocusable())
266 // If the actor is not focusable, ask the same layout control for the next actor to focus
267 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
271 Actor currentFocusActor = GetCurrentFocusActor();
272 Actor committedFocusActor = nextFocusableActor;
274 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
275 // Signal handler can check the proposed actor and return a different actor if it wishes.
276 if( !mPreFocusChangeSignal.Empty() )
278 mIsWaitingKeyboardFocusChangeCommit = true;
279 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
280 mIsWaitingKeyboardFocusChangeCommit = false;
283 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
285 // Whether the commited focusable actor is a layout control
286 if(IsLayoutControl(committedFocusActor))
288 // If so, move the focus inside it.
289 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
290 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
294 // Otherwise, just set focus to the next focusable actor
295 if(committedFocusActor == nextFocusableActor)
297 // If the application hasn't changed our proposed actor, we informs the layout control we will
298 // move the focus to what the control returns. The control might wish to perform some actions
299 // before the focus is actually moved.
300 GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
303 return SetCurrentFocusActor(committedFocusActor);
314 // No more actor can be focused in the given direction within the same layout control.
319 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
321 bool succeed = false;
323 // Get the parent layout control of the current focus group
324 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
326 while(parentLayoutControl && !succeed)
328 // If the current focus group has a parent layout control, we can probably automatically
329 // move the focus to the next focus group in the forward or backward direction.
330 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
331 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
332 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
335 if(!mFocusGroupChangedSignal.Empty())
337 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
338 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
344 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
348 Toolkit::Control control = Toolkit::Control::DownCast( actor );
351 // Notify the control that enter has been pressed on it.
352 GetImplementation( control ).KeyboardEnter();
355 // Send a notification for the actor.
356 if( !mFocusedActorEnterKeySignal.Empty() )
358 mFocusedActorEnterKeySignal.Emit( actor );
363 void KeyboardFocusManager::ClearFocus()
365 Actor actor = GetCurrentFocusActor();
368 if(mFocusIndicatorActor)
370 actor.Remove(mFocusIndicatorActor);
373 // Send notification for the change of focus actor
374 if( !mFocusChangedSignal.Empty() )
376 mFocusChangedSignal.Emit(actor, Actor());
380 mCurrentFocusActor = 0;
381 mIsFocusIndicatorEnabled = false;
384 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
386 mFocusGroupLoopEnabled = enabled;
389 bool KeyboardFocusManager::GetFocusGroupLoop() const
391 return mFocusGroupLoopEnabled;
394 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
398 // Create/Set focus group property.
399 actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
403 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
405 // Check whether the actor is a focus group
406 bool isFocusGroup = false;
410 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
411 if(propertyIsFocusGroup != Property::INVALID_INDEX)
413 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
420 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
422 // Go through the actor's hierarchy to check which focus group the actor belongs to
423 while (actor && !IsFocusGroup(actor))
425 actor = actor.GetParent();
431 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
433 if(mFocusIndicatorActor != indicator)
435 Actor currentFocusActor = GetCurrentFocusActor();
436 if(currentFocusActor)
438 // The new focus indicator should be added to the current focused actor immediately
439 if(mFocusIndicatorActor)
441 currentFocusActor.Remove(mFocusIndicatorActor);
446 currentFocusActor.Add(indicator);
450 mFocusIndicatorActor = indicator;
454 Actor KeyboardFocusManager::GetFocusIndicatorActor()
456 return mFocusIndicatorActor;
459 void KeyboardFocusManager::CreateDefaultFocusIndicatorActor()
461 // Create a focus indicator actor shared by all the keyboard focusable actors
462 Toolkit::ImageView focusIndicator = Toolkit::ImageView::New(FOCUS_BORDER_IMAGE_PATH);
463 focusIndicator.SetParentOrigin( ParentOrigin::CENTER );
465 // Apply size constraint to the focus indicator
466 focusIndicator.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
468 SetFocusIndicatorActor(focusIndicator);
471 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
473 mIsKeyboardFocusEnabled = keyboard.IsAttached();
475 if(mIsKeyboardFocusEnabled)
477 // Show indicator when keyboard focus turned on if there is focused actor.
478 Actor actor = GetCurrentFocusActor();
481 if(mFocusIndicatorActor)
483 actor.Add(mFocusIndicatorActor);
486 mIsFocusIndicatorEnabled = true;
490 // Hide indicator when keyboard focus turned off
491 Actor actor = GetCurrentFocusActor();
494 actor.Remove(mFocusIndicatorActor);
496 mIsFocusIndicatorEnabled = false;
500 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
502 if(!mIsKeyboardFocusEnabled)
507 AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
508 bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
510 Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
512 std::string keyName = event.keyPressedName;
514 bool isFocusStartableKey = false;
516 if(event.state == KeyEvent::Down)
518 if (keyName == "Left")
520 if(!isAccessibilityEnabled)
522 if(!mIsFocusIndicatorEnabled)
524 // Show focus indicator
525 mIsFocusIndicatorEnabled = true;
529 // Move the focus towards left
530 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
533 isFocusStartableKey = true;
537 // Move the accessibility focus backward
538 accessibilityManager.MoveFocusBackward();
541 else if (keyName == "Right")
543 if(!isAccessibilityEnabled)
545 if(!mIsFocusIndicatorEnabled)
547 // Show focus indicator
548 mIsFocusIndicatorEnabled = true;
552 // Move the focus towards right
553 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
558 // Move the accessibility focus forward
559 accessibilityManager.MoveFocusForward();
562 isFocusStartableKey = true;
564 else if (keyName == "Up" && !isAccessibilityEnabled)
566 if(!mIsFocusIndicatorEnabled)
568 // Show focus indicator
569 mIsFocusIndicatorEnabled = true;
573 // Move the focus towards up
574 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
577 isFocusStartableKey = true;
579 else if (keyName == "Down" && !isAccessibilityEnabled)
581 if(!mIsFocusIndicatorEnabled)
583 // Show focus indicator
584 mIsFocusIndicatorEnabled = true;
588 // Move the focus towards down
589 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
592 isFocusStartableKey = true;
594 else if (keyName == "Tab" && !isAccessibilityEnabled)
596 if(!mIsFocusIndicatorEnabled)
598 // Show focus indicator
599 mIsFocusIndicatorEnabled = true;
603 // "Tab" key changes the focus group in the forward direction and
604 // "Shift-Tab" key changes it in the backward direction.
605 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
608 isFocusStartableKey = true;
610 else if (keyName == "space" && !isAccessibilityEnabled)
612 if(!mIsFocusIndicatorEnabled)
614 // Show focus indicator
615 mIsFocusIndicatorEnabled = true;
618 isFocusStartableKey = true;
620 else if (keyName == "" && !isAccessibilityEnabled)
622 // Check the fake key event for evas-plugin case
623 if(!mIsFocusIndicatorEnabled)
625 // Show focus indicator
626 mIsFocusIndicatorEnabled = true;
629 isFocusStartableKey = true;
631 else if (keyName == "Backspace" && !isAccessibilityEnabled)
633 // Emit signal to go back to the previous view???
636 else if(event.state == KeyEvent::Up)
638 if (keyName == "Return")
640 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
642 // Show focus indicator
643 mIsFocusIndicatorEnabled = true;
647 // The focused actor has enter pressed on it
649 if( !isAccessibilityEnabled )
651 actor = GetCurrentFocusActor();
655 actor = accessibilityManager.GetCurrentFocusActor();
660 DoKeyboardEnter( actor );
664 isFocusStartableKey = true;
668 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
670 Actor actor = GetCurrentFocusActor();
673 // No actor is focused but keyboard focus is activated by the key press
674 // Let's try to move the initial focus
675 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
677 else if(mFocusIndicatorActor)
679 // Make sure the focused actor is highlighted
680 actor.Add(mFocusIndicatorActor);
685 void KeyboardFocusManager::OnTouch(const TouchData& touch)
687 // Clear the focus when user touch the screen.
688 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
689 if( ( touch.GetPointCount() < 1 ) || ( touch.GetState( 0 ) == PointState::DOWN ) )
695 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
697 return mPreFocusChangeSignal;
700 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
702 return mFocusChangedSignal;
705 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
707 return mFocusGroupChangedSignal;
710 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
712 return mFocusedActorEnterKeySignal;
715 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
717 Dali::BaseHandle handle( object );
719 bool connected( true );
720 KeyboardFocusManager* manager = dynamic_cast<KeyboardFocusManager*>( object );
722 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
724 manager->PreFocusChangeSignal().Connect( tracker, functor );
726 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
728 manager->FocusChangedSignal().Connect( tracker, functor );
730 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
732 manager->FocusGroupChangedSignal().Connect( tracker, functor );
734 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
736 manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
740 // signalName does not match any signal
747 } // namespace Internal
749 } // namespace Toolkit