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/object/type-registry.h>
30 #include <dali/devel-api/object/type-registry-helper.h>
31 #include <dali/public-api/images/resource-image.h>
32 #include <dali/integration-api/debug.h>
35 #include <dali-toolkit/public-api/controls/control.h>
36 #include <dali-toolkit/public-api/controls/control-impl.h>
37 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
38 #include <dali-toolkit/public-api/accessibility-manager/accessibility-manager.h>
39 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
50 namespace // Unnamed namespace
53 #if defined(DEBUG_ENABLED)
54 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
57 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
59 const char* const FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "keyboard_focus.9.png";
63 BaseHandle handle = KeyboardFocusManager::Get();
67 SingletonService singletonService( SingletonService::Get() );
68 if ( singletonService )
70 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
71 singletonService.Register( typeid( manager ), manager );
79 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true )
81 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardPreFocusChange", SIGNAL_PRE_FOCUS_CHANGE )
82 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusChanged", SIGNAL_FOCUS_CHANGED )
83 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged", SIGNAL_FOCUS_GROUP_CHANGED )
84 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey", SIGNAL_FOCUSED_ACTOR_ENTER_KEY )
86 DALI_TYPE_REGISTRATION_END()
88 } // unnamed namespace
90 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
92 Toolkit::KeyboardFocusManager manager;
94 SingletonService singletonService( SingletonService::Get() );
95 if ( singletonService )
97 // Check whether the keyboard focus manager is already created
98 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
101 // If so, downcast the handle of singleton to keyboard focus manager
102 manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
109 KeyboardFocusManager::KeyboardFocusManager()
110 : mCurrentFocusActor(0),
111 mFocusIndicatorActor(Actor()),
112 mFocusGroupLoopEnabled(false),
113 mIsKeyboardFocusEnabled(false),
114 mIsFocusIndicatorEnabled(false),
115 mIsWaitingKeyboardFocusChangeCommit(false),
118 CreateDefaultFocusIndicatorActor();
120 OnPhysicalKeyboardStatusChanged(PhysicalKeyboard::Get());
122 Toolkit::KeyInputFocusManager::Get().UnhandledKeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
123 Stage::GetCurrent().TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouched);
124 PhysicalKeyboard::Get().StatusChangedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnPhysicalKeyboardStatusChanged);
127 KeyboardFocusManager::~KeyboardFocusManager()
131 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
133 DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
137 return DoSetCurrentFocusActor( actor.GetId() );
143 bool KeyboardFocusManager::DoSetCurrentFocusActor( const unsigned int actorID )
145 Actor rootActor = Stage::GetCurrent().GetRootLayer();
146 Actor actor = rootActor.FindChildById( actorID );
147 bool success = false;
149 // Check whether the actor is in the stage and is keyboard focusable.
150 if( actor && actor.IsKeyboardFocusable() )
152 mIsFocusIndicatorEnabled = true;
153 // Draw the focus indicator upon the focused actor
154 if( mFocusIndicatorActor )
156 actor.Add( mFocusIndicatorActor );
159 // Send notification for the change of focus actor
160 if( !mFocusChangedSignal.Empty() )
162 mFocusChangedSignal.Emit(GetCurrentFocusActor(), actor);
165 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
167 // Save the current focused actor
168 mCurrentFocusActor = actorID;
170 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
175 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
181 Actor KeyboardFocusManager::GetCurrentFocusActor()
183 Actor rootActor = Stage::GetCurrent().GetRootLayer();
184 return rootActor.FindChildById(mCurrentFocusActor);
187 Actor KeyboardFocusManager::GetCurrentFocusGroup()
189 return GetFocusGroup(GetCurrentFocusActor());
192 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
194 Toolkit::Control control = Toolkit::Control::DownCast(actor);
195 return control && GetImplementation( control ).IsKeyboardNavigationSupported();
198 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
200 // Get the actor's parent layout control that supports two dimensional keyboard navigation
201 Actor rootActor = Stage::GetCurrent().GetRootLayer();
205 parent = actor.GetParent();
208 while( parent && !IsLayoutControl(parent) && parent != rootActor )
210 parent = parent.GetParent();
213 return Toolkit::Control::DownCast(parent);
216 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
218 Actor currentFocusActor = GetCurrentFocusActor();
220 bool succeed = false;
222 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
223 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
224 while(parentLayoutControl && !succeed)
226 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
227 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
230 if(!succeed && !mPreFocusChangeSignal.Empty())
232 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
233 mIsWaitingKeyboardFocusChangeCommit = true;
234 Actor nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
235 mIsWaitingKeyboardFocusChangeCommit = false;
237 if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
239 // Whether the next focusable actor is a layout control
240 if(IsLayoutControl(nextFocusableActor))
242 // If so, move the focus inside it.
243 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
244 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
248 // Otherwise, just set focus to the next focusable actor
249 succeed = SetCurrentFocusActor(nextFocusableActor);
257 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
259 // Ask the control for the next actor to focus
260 Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
261 if(nextFocusableActor)
263 if(!nextFocusableActor.IsKeyboardFocusable())
265 // If the actor is not focusable, ask the same layout control for the next actor to focus
266 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
270 Actor currentFocusActor = GetCurrentFocusActor();
271 Actor committedFocusActor = nextFocusableActor;
273 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
274 // Signal handler can check the proposed actor and return a different actor if it wishes.
275 if( !mPreFocusChangeSignal.Empty() )
277 mIsWaitingKeyboardFocusChangeCommit = true;
278 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
279 mIsWaitingKeyboardFocusChangeCommit = false;
282 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
284 // Whether the commited focusable actor is a layout control
285 if(IsLayoutControl(committedFocusActor))
287 // If so, move the focus inside it.
288 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
289 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
293 // Otherwise, just set focus to the next focusable actor
294 if(committedFocusActor == nextFocusableActor)
296 // If the application hasn't changed our proposed actor, we informs the layout control we will
297 // move the focus to what the control returns. The control might wish to perform some actions
298 // before the focus is actually moved.
299 GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
302 return SetCurrentFocusActor(committedFocusActor);
313 // No more actor can be focused in the given direction within the same layout control.
318 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
320 bool succeed = false;
322 // Get the parent layout control of the current focus group
323 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
325 while(parentLayoutControl && !succeed)
327 // If the current focus group has a parent layout control, we can probably automatically
328 // move the focus to the next focus group in the forward or backward direction.
329 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
330 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
331 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
334 if(!mFocusGroupChangedSignal.Empty())
336 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
337 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
343 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
347 Toolkit::Control control = Toolkit::Control::DownCast( actor );
350 // Notify the control that enter has been pressed on it.
351 GetImplementation( control ).KeyboardEnter();
354 // Send a notification for the actor.
355 if( !mFocusedActorEnterKeySignal.Empty() )
357 mFocusedActorEnterKeySignal.Emit( actor );
362 void KeyboardFocusManager::ClearFocus()
364 Actor actor = GetCurrentFocusActor();
367 if(mFocusIndicatorActor)
369 actor.Remove(mFocusIndicatorActor);
372 // Send notification for the change of focus actor
373 if( !mFocusChangedSignal.Empty() )
375 mFocusChangedSignal.Emit(actor, Actor());
379 mCurrentFocusActor = 0;
380 mIsFocusIndicatorEnabled = false;
383 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
385 mFocusGroupLoopEnabled = enabled;
388 bool KeyboardFocusManager::GetFocusGroupLoop() const
390 return mFocusGroupLoopEnabled;
393 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
397 // Create/Set focus group property.
398 actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
402 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
404 // Check whether the actor is a focus group
405 bool isFocusGroup = false;
409 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
410 if(propertyIsFocusGroup != Property::INVALID_INDEX)
412 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
419 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
421 // Go through the actor's hierarchy to check which focus group the actor belongs to
422 while (actor && !IsFocusGroup(actor))
424 actor = actor.GetParent();
430 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
432 if(mFocusIndicatorActor != indicator)
434 Actor currentFocusActor = GetCurrentFocusActor();
435 if(currentFocusActor)
437 // The new focus indicator should be added to the current focused actor immediately
438 if(mFocusIndicatorActor)
440 currentFocusActor.Remove(mFocusIndicatorActor);
445 currentFocusActor.Add(indicator);
449 mFocusIndicatorActor = indicator;
453 Actor KeyboardFocusManager::GetFocusIndicatorActor()
455 return mFocusIndicatorActor;
458 void KeyboardFocusManager::CreateDefaultFocusIndicatorActor()
460 // Create a focus indicator actor shared by all the keyboard focusable actors
461 Toolkit::ImageView focusIndicator = Toolkit::ImageView::New(FOCUS_BORDER_IMAGE_PATH);
462 focusIndicator.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION );
464 // Apply size constraint to the focus indicator
465 focusIndicator.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
467 SetFocusIndicatorActor(focusIndicator);
470 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
472 mIsKeyboardFocusEnabled = keyboard.IsAttached();
474 if(mIsKeyboardFocusEnabled)
476 // Show indicator when keyboard focus turned on if there is focused actor.
477 Actor actor = GetCurrentFocusActor();
480 if(mFocusIndicatorActor)
482 actor.Add(mFocusIndicatorActor);
485 mIsFocusIndicatorEnabled = true;
489 // Hide indicator when keyboard focus turned off
490 Actor actor = GetCurrentFocusActor();
493 actor.Remove(mFocusIndicatorActor);
495 mIsFocusIndicatorEnabled = false;
499 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
501 if(!mIsKeyboardFocusEnabled)
506 AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
507 bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
509 Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
511 std::string keyName = event.keyPressedName;
513 bool isFocusStartableKey = false;
515 if(event.state == KeyEvent::Down)
517 if (keyName == "Left")
519 if(!isAccessibilityEnabled)
521 if(!mIsFocusIndicatorEnabled)
523 // Show focus indicator
524 mIsFocusIndicatorEnabled = true;
528 // Move the focus towards left
529 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
532 isFocusStartableKey = true;
536 // Move the accessibility focus backward
537 accessibilityManager.MoveFocusBackward();
540 else if (keyName == "Right")
542 if(!isAccessibilityEnabled)
544 if(!mIsFocusIndicatorEnabled)
546 // Show focus indicator
547 mIsFocusIndicatorEnabled = true;
551 // Move the focus towards right
552 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
557 // Move the accessibility focus forward
558 accessibilityManager.MoveFocusForward();
561 isFocusStartableKey = true;
563 else if (keyName == "Up" && !isAccessibilityEnabled)
565 if(!mIsFocusIndicatorEnabled)
567 // Show focus indicator
568 mIsFocusIndicatorEnabled = true;
572 // Move the focus towards up
573 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
576 isFocusStartableKey = true;
578 else if (keyName == "Down" && !isAccessibilityEnabled)
580 if(!mIsFocusIndicatorEnabled)
582 // Show focus indicator
583 mIsFocusIndicatorEnabled = true;
587 // Move the focus towards down
588 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
591 isFocusStartableKey = true;
593 else if (keyName == "Tab" && !isAccessibilityEnabled)
595 if(!mIsFocusIndicatorEnabled)
597 // Show focus indicator
598 mIsFocusIndicatorEnabled = true;
602 // "Tab" key changes the focus group in the forward direction and
603 // "Shift-Tab" key changes it in the backward direction.
604 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
607 isFocusStartableKey = true;
609 else if (keyName == "space" && !isAccessibilityEnabled)
611 if(!mIsFocusIndicatorEnabled)
613 // Show focus indicator
614 mIsFocusIndicatorEnabled = true;
617 isFocusStartableKey = true;
619 else if (keyName == "" && !isAccessibilityEnabled)
621 // Check the fake key event for evas-plugin case
622 if(!mIsFocusIndicatorEnabled)
624 // Show focus indicator
625 mIsFocusIndicatorEnabled = true;
628 isFocusStartableKey = true;
630 else if (keyName == "Backspace" && !isAccessibilityEnabled)
632 // Emit signal to go back to the previous view???
635 else if(event.state == KeyEvent::Up)
637 if (keyName == "Return")
639 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
641 // Show focus indicator
642 mIsFocusIndicatorEnabled = true;
646 // The focused actor has enter pressed on it
648 if( !isAccessibilityEnabled )
650 actor = GetCurrentFocusActor();
654 actor = accessibilityManager.GetCurrentFocusActor();
659 DoKeyboardEnter( actor );
663 isFocusStartableKey = true;
667 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
669 Actor actor = GetCurrentFocusActor();
672 // No actor is focused but keyboard focus is activated by the key press
673 // Let's try to move the initial focus
674 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
676 else if(mFocusIndicatorActor)
678 // Make sure the focused actor is highlighted
679 actor.Add(mFocusIndicatorActor);
684 void KeyboardFocusManager::OnTouched(const TouchEvent& touchEvent)
686 // Clear the focus when user touch the screen.
687 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
688 if( ( touchEvent.GetPointCount() < 1 ) || ( touchEvent.GetPoint( 0 ).state == TouchPoint::Down ) )
694 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
696 return mPreFocusChangeSignal;
699 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
701 return mFocusChangedSignal;
704 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
706 return mFocusGroupChangedSignal;
709 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
711 return mFocusedActorEnterKeySignal;
714 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
716 Dali::BaseHandle handle( object );
718 bool connected( true );
719 KeyboardFocusManager* manager = dynamic_cast<KeyboardFocusManager*>( object );
721 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
723 manager->PreFocusChangeSignal().Connect( tracker, functor );
725 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
727 manager->FocusChangedSignal().Connect( tracker, functor );
729 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
731 manager->FocusGroupChangedSignal().Connect( tracker, functor );
733 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
735 manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
739 // signalName does not match any signal
746 } // namespace Internal
748 } // namespace Toolkit