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
153 actor.Add( GetFocusIndicatorActor() );
155 // Send notification for the change of focus actor
156 if( !mFocusChangedSignal.Empty() )
158 mFocusChangedSignal.Emit(GetCurrentFocusActor(), actor);
161 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
163 // Save the current focused actor
164 mCurrentFocusActor = actorID;
166 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
171 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
177 Actor KeyboardFocusManager::GetCurrentFocusActor()
179 Actor rootActor = Stage::GetCurrent().GetRootLayer();
180 return rootActor.FindChildById(mCurrentFocusActor);
183 Actor KeyboardFocusManager::GetCurrentFocusGroup()
185 return GetFocusGroup(GetCurrentFocusActor());
188 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
190 Toolkit::Control control = Toolkit::Control::DownCast(actor);
191 return control && GetImplementation( control ).IsKeyboardNavigationSupported();
194 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
196 // Get the actor's parent layout control that supports two dimensional keyboard navigation
197 Actor rootActor = Stage::GetCurrent().GetRootLayer();
201 parent = actor.GetParent();
204 while( parent && !IsLayoutControl(parent) && parent != rootActor )
206 parent = parent.GetParent();
209 return Toolkit::Control::DownCast(parent);
212 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
214 Actor currentFocusActor = GetCurrentFocusActor();
216 bool succeed = false;
218 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
219 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
220 while(parentLayoutControl && !succeed)
222 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
223 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
226 if(!succeed && !mPreFocusChangeSignal.Empty())
228 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
229 mIsWaitingKeyboardFocusChangeCommit = true;
230 Actor nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
231 mIsWaitingKeyboardFocusChangeCommit = false;
233 if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
235 // Whether the next focusable actor is a layout control
236 if(IsLayoutControl(nextFocusableActor))
238 // If so, move the focus inside it.
239 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
240 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
244 // Otherwise, just set focus to the next focusable actor
245 succeed = SetCurrentFocusActor(nextFocusableActor);
253 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
255 // Ask the control for the next actor to focus
256 Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
257 if(nextFocusableActor)
259 if(!nextFocusableActor.IsKeyboardFocusable())
261 // If the actor is not focusable, ask the same layout control for the next actor to focus
262 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
266 Actor currentFocusActor = GetCurrentFocusActor();
267 Actor committedFocusActor = nextFocusableActor;
269 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
270 // Signal handler can check the proposed actor and return a different actor if it wishes.
271 if( !mPreFocusChangeSignal.Empty() )
273 mIsWaitingKeyboardFocusChangeCommit = true;
274 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
275 mIsWaitingKeyboardFocusChangeCommit = false;
278 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
280 // Whether the commited focusable actor is a layout control
281 if(IsLayoutControl(committedFocusActor))
283 // If so, move the focus inside it.
284 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
285 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
289 // Otherwise, just set focus to the next focusable actor
290 if(committedFocusActor == nextFocusableActor)
292 // If the application hasn't changed our proposed actor, we informs the layout control we will
293 // move the focus to what the control returns. The control might wish to perform some actions
294 // before the focus is actually moved.
295 GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
298 return SetCurrentFocusActor(committedFocusActor);
309 // No more actor can be focused in the given direction within the same layout control.
314 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
316 bool succeed = false;
318 // Get the parent layout control of the current focus group
319 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
321 while(parentLayoutControl && !succeed)
323 // If the current focus group has a parent layout control, we can probably automatically
324 // move the focus to the next focus group in the forward or backward direction.
325 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
326 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
327 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
330 if(!mFocusGroupChangedSignal.Empty())
332 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
333 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
339 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
343 Toolkit::Control control = Toolkit::Control::DownCast( actor );
346 // Notify the control that enter has been pressed on it.
347 GetImplementation( control ).KeyboardEnter();
350 // Send a notification for the actor.
351 if( !mFocusedActorEnterKeySignal.Empty() )
353 mFocusedActorEnterKeySignal.Emit( actor );
358 void KeyboardFocusManager::ClearFocus()
360 Actor actor = GetCurrentFocusActor();
363 if(mFocusIndicatorActor)
365 actor.Remove(mFocusIndicatorActor);
368 // Send notification for the change of focus actor
369 if( !mFocusChangedSignal.Empty() )
371 mFocusChangedSignal.Emit(actor, Actor());
375 mCurrentFocusActor = 0;
376 mIsFocusIndicatorEnabled = false;
379 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
381 mFocusGroupLoopEnabled = enabled;
384 bool KeyboardFocusManager::GetFocusGroupLoop() const
386 return mFocusGroupLoopEnabled;
389 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
393 // Create/Set focus group property.
394 actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
398 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
400 // Check whether the actor is a focus group
401 bool isFocusGroup = false;
405 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
406 if(propertyIsFocusGroup != Property::INVALID_INDEX)
408 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
415 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
417 // Go through the actor's hierarchy to check which focus group the actor belongs to
418 while (actor && !IsFocusGroup(actor))
420 actor = actor.GetParent();
426 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
428 if(mFocusIndicatorActor != indicator)
430 Actor currentFocusActor = GetCurrentFocusActor();
431 if(currentFocusActor)
433 // The new focus indicator should be added to the current focused actor immediately
434 if(mFocusIndicatorActor)
436 currentFocusActor.Remove(mFocusIndicatorActor);
441 currentFocusActor.Add(indicator);
445 mFocusIndicatorActor = indicator;
449 Actor KeyboardFocusManager::GetFocusIndicatorActor()
451 if( ! mFocusIndicatorActor )
453 // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
454 mFocusIndicatorActor = Toolkit::ImageView::New( FOCUS_BORDER_IMAGE_PATH );
455 mFocusIndicatorActor.SetParentOrigin( ParentOrigin::CENTER );
457 // Apply size constraint to the focus indicator
458 mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
461 return mFocusIndicatorActor;
464 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
466 mIsKeyboardFocusEnabled = keyboard.IsAttached();
468 if(mIsKeyboardFocusEnabled)
470 // Show indicator when keyboard focus turned on if there is focused actor.
471 Actor actor = GetCurrentFocusActor();
474 actor.Add( GetFocusIndicatorActor() );
476 mIsFocusIndicatorEnabled = true;
480 // Hide indicator when keyboard focus turned off
481 Actor actor = GetCurrentFocusActor();
482 if( actor && mFocusIndicatorActor )
484 actor.Remove( mFocusIndicatorActor );
486 mIsFocusIndicatorEnabled = false;
490 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
492 if(!mIsKeyboardFocusEnabled)
497 AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
498 bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
500 Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
502 std::string keyName = event.keyPressedName;
504 bool isFocusStartableKey = false;
506 if(event.state == KeyEvent::Down)
508 if (keyName == "Left")
510 if(!isAccessibilityEnabled)
512 if(!mIsFocusIndicatorEnabled)
514 // Show focus indicator
515 mIsFocusIndicatorEnabled = true;
519 // Move the focus towards left
520 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
523 isFocusStartableKey = true;
527 // Move the accessibility focus backward
528 accessibilityManager.MoveFocusBackward();
531 else if (keyName == "Right")
533 if(!isAccessibilityEnabled)
535 if(!mIsFocusIndicatorEnabled)
537 // Show focus indicator
538 mIsFocusIndicatorEnabled = true;
542 // Move the focus towards right
543 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
548 // Move the accessibility focus forward
549 accessibilityManager.MoveFocusForward();
552 isFocusStartableKey = true;
554 else if (keyName == "Up" && !isAccessibilityEnabled)
556 if(!mIsFocusIndicatorEnabled)
558 // Show focus indicator
559 mIsFocusIndicatorEnabled = true;
563 // Move the focus towards up
564 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
567 isFocusStartableKey = true;
569 else if (keyName == "Down" && !isAccessibilityEnabled)
571 if(!mIsFocusIndicatorEnabled)
573 // Show focus indicator
574 mIsFocusIndicatorEnabled = true;
578 // Move the focus towards down
579 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
582 isFocusStartableKey = true;
584 else if (keyName == "Tab" && !isAccessibilityEnabled)
586 if(!mIsFocusIndicatorEnabled)
588 // Show focus indicator
589 mIsFocusIndicatorEnabled = true;
593 // "Tab" key changes the focus group in the forward direction and
594 // "Shift-Tab" key changes it in the backward direction.
595 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
598 isFocusStartableKey = true;
600 else if (keyName == "space" && !isAccessibilityEnabled)
602 if(!mIsFocusIndicatorEnabled)
604 // Show focus indicator
605 mIsFocusIndicatorEnabled = true;
608 isFocusStartableKey = true;
610 else if (keyName == "" && !isAccessibilityEnabled)
612 // Check the fake key event for evas-plugin case
613 if(!mIsFocusIndicatorEnabled)
615 // Show focus indicator
616 mIsFocusIndicatorEnabled = true;
619 isFocusStartableKey = true;
621 else if (keyName == "Backspace" && !isAccessibilityEnabled)
623 // Emit signal to go back to the previous view???
626 else if(event.state == KeyEvent::Up)
628 if (keyName == "Return")
630 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
632 // Show focus indicator
633 mIsFocusIndicatorEnabled = true;
637 // The focused actor has enter pressed on it
639 if( !isAccessibilityEnabled )
641 actor = GetCurrentFocusActor();
645 actor = accessibilityManager.GetCurrentFocusActor();
650 DoKeyboardEnter( actor );
654 isFocusStartableKey = true;
658 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
660 Actor actor = GetCurrentFocusActor();
663 // Make sure the focused actor is highlighted
664 actor.Add( GetFocusIndicatorActor() );
668 // No actor is focused but keyboard focus is activated by the key press
669 // Let's try to move the initial focus
670 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
675 void KeyboardFocusManager::OnTouch(const TouchData& touch)
677 // Clear the focus when user touch the screen.
678 // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
679 if( ( touch.GetPointCount() < 1 ) || ( touch.GetState( 0 ) == PointState::DOWN ) )
685 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
687 return mPreFocusChangeSignal;
690 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
692 return mFocusChangedSignal;
695 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
697 return mFocusGroupChangedSignal;
700 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
702 return mFocusedActorEnterKeySignal;
705 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
707 Dali::BaseHandle handle( object );
709 bool connected( true );
710 KeyboardFocusManager* manager = dynamic_cast<KeyboardFocusManager*>( object );
712 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
714 manager->PreFocusChangeSignal().Connect( tracker, functor );
716 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
718 manager->FocusChangedSignal().Connect( tracker, functor );
720 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
722 manager->FocusGroupChangedSignal().Connect( tracker, functor );
724 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
726 manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
730 // signalName does not match any signal
737 } // namespace Internal
739 } // namespace Toolkit