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 <dali/public-api/actors/layer.h>
23 #include <dali/public-api/adaptor-framework/accessibility-manager.h>
24 #include <dali/public-api/adaptor-framework/singleton-service.h>
25 #include <dali/public-api/animation/constraints.h>
26 #include <dali/public-api/common/stage.h>
27 #include <dali/public-api/events/key-event.h>
28 #include <dali/public-api/object/type-registry.h>
29 #include <dali/public-api/images/resource-image.h>
30 #include <dali/integration-api/debug.h>
33 #include <dali-toolkit/public-api/controls/control.h>
34 #include <dali-toolkit/public-api/controls/control-impl.h>
35 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
36 #include <dali-toolkit/public-api/focus-manager/keyinput-focus-manager.h>
47 namespace // unnamed namespace
52 const char* const SIGNAL_PRE_FOCUS_CHANGE = "keyboard-pre-focus-change";
53 const char* const SIGNAL_FOCUS_CHANGED = "keyboard-focus-changed";
54 const char* const SIGNAL_FOCUS_GROUP_CHANGED = "keyboard-focus-group-changed";
55 const char* const SIGNAL_FOCUSED_ACTOR_ACTIVATED = "keyboard-focused-actor-activated";
57 #if defined(DEBUG_ENABLED)
58 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
61 const std::string IS_FOCUS_GROUP_PROPERTY_NAME("is-keyboard-focus-group"); // This property will be replaced by a flag in Control.
63 const char* FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "keyboard_focus.png";
64 const Vector4 FOCUS_BORDER_IMAGE_BORDER = Vector4(7.0f, 7.0f, 7.0f, 7.0f);
68 BaseHandle handle = KeyboardFocusManager::Get();
72 SingletonService singletonService( SingletonService::Get() );
73 if ( singletonService )
75 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
76 singletonService.Register( typeid( manager ), manager );
84 TypeRegistration KEYBOARD_FOCUS_MANAGER_TYPE( typeid(Dali::Toolkit::KeyboardFocusManager), typeid(Dali::BaseHandle), Create, true /* Create instance at startup */ );
86 SignalConnectorType signalConnector1( KEYBOARD_FOCUS_MANAGER_TYPE, SIGNAL_PRE_FOCUS_CHANGE , &KeyboardFocusManager::DoConnectSignal );
87 SignalConnectorType signalConnector2( KEYBOARD_FOCUS_MANAGER_TYPE, SIGNAL_FOCUS_CHANGED , &KeyboardFocusManager::DoConnectSignal );
88 SignalConnectorType signalConnector3( KEYBOARD_FOCUS_MANAGER_TYPE, SIGNAL_FOCUS_GROUP_CHANGED , &KeyboardFocusManager::DoConnectSignal );
89 SignalConnectorType signalConnector4( KEYBOARD_FOCUS_MANAGER_TYPE, SIGNAL_FOCUSED_ACTOR_ACTIVATED , &KeyboardFocusManager::DoConnectSignal );
91 } // unnamed namespace
93 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
95 Toolkit::KeyboardFocusManager manager;
97 SingletonService singletonService( SingletonService::Get() );
98 if ( singletonService )
100 // Check whether the keyboard focus manager is already created
101 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
104 // If so, downcast the handle of singleton to keyboard focus manager
105 manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
112 KeyboardFocusManager::KeyboardFocusManager()
113 : mCurrentFocusActor(0),
114 mFocusIndicatorActor(Actor()),
115 mFocusGroupLoopEnabled(false),
116 mIsKeyboardFocusEnabled(false),
117 mIsFocusIndicatorEnabled(false),
118 mIsWaitingKeyboardFocusChangeCommit(false),
121 CreateDefaultFocusIndicatorActor();
123 OnPhysicalKeyboardStatusChanged(PhysicalKeyboard::Get());
125 Toolkit::KeyInputFocusManager::Get().UnhandledKeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
126 Stage::GetCurrent().TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouched);
127 PhysicalKeyboard::Get().StatusChangedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnPhysicalKeyboardStatusChanged);
130 KeyboardFocusManager::~KeyboardFocusManager()
134 bool KeyboardFocusManager::SetCurrentFocusActor(Actor actor)
136 DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
140 return DoSetCurrentFocusActor(actor.GetId());
146 bool KeyboardFocusManager::DoSetCurrentFocusActor(const unsigned int actorID)
148 Actor rootActor = Stage::GetCurrent().GetRootLayer();
149 Actor actor = rootActor.FindChildById(actorID);
151 // Check whether the actor is in the stage
154 // Set the focus only when the actor is keyboard focusable
155 if(actor.IsKeyboardFocusable())
157 // Draw the focus indicator upon the focused actor
158 if(mIsFocusIndicatorEnabled && mFocusIndicatorActor)
160 actor.Add(mFocusIndicatorActor);
163 // Send notification for the change of focus actor
164 if( !mFocusChangedSignal.Empty() )
166 mFocusChangedSignal.Emit(GetCurrentFocusActor(), actor);
169 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
171 // Save the current focused actor
172 mCurrentFocusActor = actorID;
174 // Move the accessibility focus to the same actor
175 // Toolkit::FocusManager focusManager = Toolkit::FocusManager::Get();
176 // focusManager.SetCurrentFocusActor(actor);
178 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
183 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
187 Actor KeyboardFocusManager::GetCurrentFocusActor()
189 Actor rootActor = Stage::GetCurrent().GetRootLayer();
190 return rootActor.FindChildById(mCurrentFocusActor);
193 Actor KeyboardFocusManager::GetCurrentFocusGroup()
195 return GetFocusGroup(GetCurrentFocusActor());
198 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
200 Toolkit::Control control = Toolkit::Control::DownCast(actor);
201 return control && control.GetImplementation().IsKeyboardNavigationSupported();
204 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
206 // Get the actor's parent layout control that supports two dimensional keyboard navigation
207 Actor rootActor = Stage::GetCurrent().GetRootLayer();
211 parent = actor.GetParent();
214 while( parent && !IsLayoutControl(parent) && parent != rootActor )
216 parent = parent.GetParent();
219 return Toolkit::Control::DownCast(parent);
222 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocusNavigationDirection direction)
224 Actor currentFocusActor = GetCurrentFocusActor();
226 bool succeed = false;
228 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
229 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
230 while(parentLayoutControl && !succeed)
232 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
233 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
236 if(!succeed && !mPreFocusChangeSignal.Empty())
238 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
239 mIsWaitingKeyboardFocusChangeCommit = true;
240 Actor nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
241 mIsWaitingKeyboardFocusChangeCommit = false;
243 if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
245 // Whether the next focusable actor is a layout control
246 if(IsLayoutControl(nextFocusableActor))
248 // If so, move the focus inside it.
249 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
250 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
254 // Otherwise, just set focus to the next focusable actor
255 succeed = SetCurrentFocusActor(nextFocusableActor);
263 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction)
265 // Ask the control for the next actor to focus
266 Actor nextFocusableActor = control.GetImplementation().GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
267 if(nextFocusableActor)
269 if(!nextFocusableActor.IsKeyboardFocusable())
271 // If the actor is not focusable, ask the same layout control for the next actor to focus
272 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
276 Actor currentFocusActor = GetCurrentFocusActor();
277 Actor committedFocusActor = nextFocusableActor;
279 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
280 // Signal handler can check the proposed actor and return a different actor if it wishes.
281 if( !mPreFocusChangeSignal.Empty() )
283 mIsWaitingKeyboardFocusChangeCommit = true;
284 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
285 mIsWaitingKeyboardFocusChangeCommit = false;
288 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
290 // Whether the commited focusable actor is a layout control
291 if(IsLayoutControl(committedFocusActor))
293 // If so, move the focus inside it.
294 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
295 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
299 // Otherwise, just set focus to the next focusable actor
300 if(committedFocusActor == nextFocusableActor)
302 // If the application hasn't changed our proposed actor, we informs the layout control we will
303 // move the focus to what the control returns. The control might wish to perform some actions
304 // before the focus is actually moved.
305 control.GetImplementation().OnKeyboardFocusChangeCommitted(committedFocusActor);
308 return SetCurrentFocusActor(committedFocusActor);
319 // No more actor can be focused in the given direction within the same layout control.
324 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
326 bool succeed = false;
328 // Get the parent layout control of the current focus group
329 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
331 while(parentLayoutControl && !succeed)
333 // If the current focus group has a parent layout control, we can probably automatically
334 // move the focus to the next focus group in the forward or backward direction.
335 Toolkit::Control::KeyboardFocusNavigationDirection direction = forward ? Toolkit::Control::Right : Toolkit::Control::Left;
336 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
337 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
340 if(!mFocusGroupChangedSignal.Empty())
342 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
343 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
349 void KeyboardFocusManager::DoActivate(Actor actor)
353 Toolkit::Control control = Toolkit::Control::DownCast(actor);
356 // Notify the control that it is activated
357 control.GetImplementation().Activate();
360 // Send notification for the activation of focused actor
361 if( !mFocusedActorActivatedSignal.Empty() )
363 mFocusedActorActivatedSignal.Emit(actor);
368 void KeyboardFocusManager::ClearFocus()
370 Actor actor = GetCurrentFocusActor();
373 if(mFocusIndicatorActor)
375 actor.Remove(mFocusIndicatorActor);
378 // Send notification for the change of focus actor
379 if( !mFocusChangedSignal.Empty() )
381 mFocusChangedSignal.Emit(actor, Actor());
385 mCurrentFocusActor = 0;
386 mIsFocusIndicatorEnabled = false;
389 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
391 mFocusGroupLoopEnabled = enabled;
394 bool KeyboardFocusManager::GetFocusGroupLoop() const
396 return mFocusGroupLoopEnabled;
399 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
403 // Create focus group property if not already created.
404 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
405 if(propertyIsFocusGroup == Property::INVALID_INDEX)
407 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup);
411 actor.SetProperty(propertyIsFocusGroup, isFocusGroup);
416 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
418 // Check whether the actor is a focus group
419 bool isFocusGroup = false;
423 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
424 if(propertyIsFocusGroup != Property::INVALID_INDEX)
426 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
433 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
435 // Go through the actor's hierarchy to check which focus group the actor belongs to
436 while (actor && !IsFocusGroup(actor))
438 actor = actor.GetParent();
444 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
446 if(mFocusIndicatorActor != indicator)
448 Actor currentFocusActor = GetCurrentFocusActor();
449 if(currentFocusActor)
451 // The new focus indicator should be added to the current focused actor immediately
452 if(mFocusIndicatorActor)
454 currentFocusActor.Remove(mFocusIndicatorActor);
459 currentFocusActor.Add(indicator);
463 mFocusIndicatorActor = indicator;
467 Actor KeyboardFocusManager::GetFocusIndicatorActor()
469 return mFocusIndicatorActor;
472 void KeyboardFocusManager::CreateDefaultFocusIndicatorActor()
474 // Create a focus indicator actor shared by all the keyboard focusable actors
475 Image borderImage = ResourceImage::New(FOCUS_BORDER_IMAGE_PATH);
477 ImageActor focusIndicator = ImageActor::New(borderImage);
478 focusIndicator.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
479 focusIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
480 focusIndicator.SetNinePatchBorder(FOCUS_BORDER_IMAGE_BORDER);
481 focusIndicator.SetPosition(Vector3(0.0f, 0.0f, 1.0f));
483 // Apply size constraint to the focus indicator
484 focusIndicator.SetSizeMode( SIZE_EQUAL_TO_PARENT );
486 SetFocusIndicatorActor(focusIndicator);
489 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
491 mIsKeyboardFocusEnabled = keyboard.IsAttached();
493 if(mIsKeyboardFocusEnabled)
495 // Show indicator when keyboard focus turned on if there is focused actor.
496 Actor actor = GetCurrentFocusActor();
499 if(mFocusIndicatorActor)
501 actor.Add(mFocusIndicatorActor);
504 mIsFocusIndicatorEnabled = true;
508 // Hide indicator when keyboard focus turned off
509 Actor actor = GetCurrentFocusActor();
512 actor.Remove(mFocusIndicatorActor);
514 mIsFocusIndicatorEnabled = false;
518 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
520 if(!mIsKeyboardFocusEnabled)
525 AccessibilityManager accessibilityManager = AccessibilityManager::Get();
526 bool isAccessibilityEnabled = accessibilityManager.IsEnabled();
528 Toolkit::FocusManager accessibilityFocusManager = Toolkit::FocusManager::Get();
530 std::string keyName = event.keyPressedName;
532 bool isFocusStartableKey = false;
534 if(event.state == KeyEvent::Down)
536 if (keyName == "Left")
538 if(!isAccessibilityEnabled)
540 if(!mIsFocusIndicatorEnabled)
542 // Show focus indicator
543 mIsFocusIndicatorEnabled = true;
547 // Move the focus towards left
548 MoveFocus(Toolkit::Control::Left);
551 isFocusStartableKey = true;
555 // Move the accessibility focus backward
556 accessibilityFocusManager.MoveFocusBackward();
559 else if (keyName == "Right")
561 if(!isAccessibilityEnabled)
563 if(!mIsFocusIndicatorEnabled)
565 // Show focus indicator
566 mIsFocusIndicatorEnabled = true;
570 // Move the focus towards right
571 MoveFocus(Toolkit::Control::Right);
576 // Move the accessibility focus forward
577 accessibilityFocusManager.MoveFocusForward();
580 isFocusStartableKey = true;
582 else if (keyName == "Up" && !isAccessibilityEnabled)
584 if(!mIsFocusIndicatorEnabled)
586 // Show focus indicator
587 mIsFocusIndicatorEnabled = true;
591 // Move the focus towards up
592 MoveFocus(Toolkit::Control::Up);
595 isFocusStartableKey = true;
597 else if (keyName == "Down" && !isAccessibilityEnabled)
599 if(!mIsFocusIndicatorEnabled)
601 // Show focus indicator
602 mIsFocusIndicatorEnabled = true;
606 // Move the focus towards down
607 MoveFocus(Toolkit::Control::Down);
610 isFocusStartableKey = true;
612 else if (keyName == "Tab" && !isAccessibilityEnabled)
614 if(!mIsFocusIndicatorEnabled)
616 // Show focus indicator
617 mIsFocusIndicatorEnabled = true;
621 // "Tab" key changes the focus group in the forward direction and
622 // "Shift-Tab" key changes it in the backward direction.
623 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
626 isFocusStartableKey = true;
628 else if (keyName == "space" && !isAccessibilityEnabled)
630 if(!mIsFocusIndicatorEnabled)
632 // Show focus indicator
633 mIsFocusIndicatorEnabled = true;
636 isFocusStartableKey = true;
638 else if (keyName == "" && !isAccessibilityEnabled)
640 // Check the fake key event for evas-plugin case
641 if(!mIsFocusIndicatorEnabled)
643 // Show focus indicator
644 mIsFocusIndicatorEnabled = true;
647 isFocusStartableKey = true;
649 else if (keyName == "Backspace" && !isAccessibilityEnabled)
651 // Emit signal to go back to the previous view???
654 else if(event.state == KeyEvent::Up)
656 if (keyName == "Return")
658 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
660 // Show focus indicator
661 mIsFocusIndicatorEnabled = true;
665 // Activate the focused actor
667 if(!isAccessibilityEnabled)
669 actor = GetCurrentFocusActor();
673 actor = accessibilityFocusManager.GetCurrentFocusActor();
682 isFocusStartableKey = true;
686 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
688 Actor actor = GetCurrentFocusActor();
691 // No actor is focused but keyboard focus is activated by the key press
692 // Let's try to move the initial focus
693 MoveFocus(Toolkit::Control::Right);
695 else if(mFocusIndicatorActor)
697 // Make sure the focused actor is highlighted
698 actor.Add(mFocusIndicatorActor);
703 void KeyboardFocusManager::OnTouched(const TouchEvent& touchEvent)
705 // Clear the focus when user touch the screen
709 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
711 return mPreFocusChangeSignal;
714 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
716 return mFocusChangedSignal;
719 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
721 return mFocusGroupChangedSignal;
724 Toolkit::KeyboardFocusManager::FocusedActorActivatedSignalType& KeyboardFocusManager::FocusedActorActivatedSignal()
726 return mFocusedActorActivatedSignal;
729 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
731 Dali::BaseHandle handle( object );
733 bool connected( true );
734 KeyboardFocusManager* manager = dynamic_cast<KeyboardFocusManager*>( object );
736 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
738 manager->PreFocusChangeSignal().Connect( tracker, functor );
740 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
742 manager->FocusChangedSignal().Connect( tracker, functor );
744 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
746 manager->FocusGroupChangedSignal().Connect( tracker, functor );
748 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ACTIVATED ) )
750 manager->FocusedActorActivatedSignal().Connect( tracker, functor );
754 // signalName does not match any signal
761 } // namespace Internal
763 } // namespace Toolkit