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/public-api/adaptor-framework/accessibility-manager.h>
25 #include <dali/public-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/public-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/focus-manager/focus-manager.h>
38 #include <dali-toolkit/public-api/focus-manager/keyinput-focus-manager.h>
49 namespace // Unnamed namespace
52 #if defined(DEBUG_ENABLED)
53 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
56 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "is-keyboard-focus-group"; // This property will be replaced by a flag in Control.
58 const char* const FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "keyboard_focus.png";
59 const Vector4 FOCUS_BORDER_IMAGE_BORDER = Vector4(7.0f, 7.0f, 7.0f, 7.0f);
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( KeyboardFocusManager, "keyboard-pre-focus-change", SIGNAL_PRE_FOCUS_CHANGE )
82 DALI_SIGNAL_REGISTRATION( KeyboardFocusManager, "keyboard-focus-changed", SIGNAL_FOCUS_CHANGED )
83 DALI_SIGNAL_REGISTRATION( KeyboardFocusManager, "keyboard-focus-group-changed", SIGNAL_FOCUS_GROUP_CHANGED )
84 DALI_SIGNAL_REGISTRATION( KeyboardFocusManager, "keyboard-focused-actor-activated", SIGNAL_FOCUSED_ACTOR_ACTIVATED )
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);
148 // Check whether the actor is in the stage
151 // Set the focus only when the actor is keyboard focusable
152 if(actor.IsKeyboardFocusable())
154 // Draw the focus indicator upon the focused actor
155 if(mIsFocusIndicatorEnabled && 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 // Move the accessibility focus to the same actor
172 // Toolkit::FocusManager focusManager = Toolkit::FocusManager::Get();
173 // focusManager.SetCurrentFocusActor(actor);
175 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
180 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
184 Actor KeyboardFocusManager::GetCurrentFocusActor()
186 Actor rootActor = Stage::GetCurrent().GetRootLayer();
187 return rootActor.FindChildById(mCurrentFocusActor);
190 Actor KeyboardFocusManager::GetCurrentFocusGroup()
192 return GetFocusGroup(GetCurrentFocusActor());
195 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
197 Toolkit::Control control = Toolkit::Control::DownCast(actor);
198 return control && control.GetImplementation().IsKeyboardNavigationSupported();
201 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
203 // Get the actor's parent layout control that supports two dimensional keyboard navigation
204 Actor rootActor = Stage::GetCurrent().GetRootLayer();
208 parent = actor.GetParent();
211 while( parent && !IsLayoutControl(parent) && parent != rootActor )
213 parent = parent.GetParent();
216 return Toolkit::Control::DownCast(parent);
219 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocusNavigationDirection direction)
221 Actor currentFocusActor = GetCurrentFocusActor();
223 bool succeed = false;
225 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
226 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
227 while(parentLayoutControl && !succeed)
229 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
230 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
233 if(!succeed && !mPreFocusChangeSignal.Empty())
235 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
236 mIsWaitingKeyboardFocusChangeCommit = true;
237 Actor nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
238 mIsWaitingKeyboardFocusChangeCommit = false;
240 if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
242 // Whether the next focusable actor is a layout control
243 if(IsLayoutControl(nextFocusableActor))
245 // If so, move the focus inside it.
246 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
247 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
251 // Otherwise, just set focus to the next focusable actor
252 succeed = SetCurrentFocusActor(nextFocusableActor);
260 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction)
262 // Ask the control for the next actor to focus
263 Actor nextFocusableActor = control.GetImplementation().GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
264 if(nextFocusableActor)
266 if(!nextFocusableActor.IsKeyboardFocusable())
268 // If the actor is not focusable, ask the same layout control for the next actor to focus
269 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
273 Actor currentFocusActor = GetCurrentFocusActor();
274 Actor committedFocusActor = nextFocusableActor;
276 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
277 // Signal handler can check the proposed actor and return a different actor if it wishes.
278 if( !mPreFocusChangeSignal.Empty() )
280 mIsWaitingKeyboardFocusChangeCommit = true;
281 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
282 mIsWaitingKeyboardFocusChangeCommit = false;
285 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
287 // Whether the commited focusable actor is a layout control
288 if(IsLayoutControl(committedFocusActor))
290 // If so, move the focus inside it.
291 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
292 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
296 // Otherwise, just set focus to the next focusable actor
297 if(committedFocusActor == nextFocusableActor)
299 // If the application hasn't changed our proposed actor, we informs the layout control we will
300 // move the focus to what the control returns. The control might wish to perform some actions
301 // before the focus is actually moved.
302 control.GetImplementation().OnKeyboardFocusChangeCommitted(committedFocusActor);
305 return SetCurrentFocusActor(committedFocusActor);
316 // No more actor can be focused in the given direction within the same layout control.
321 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
323 bool succeed = false;
325 // Get the parent layout control of the current focus group
326 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
328 while(parentLayoutControl && !succeed)
330 // If the current focus group has a parent layout control, we can probably automatically
331 // move the focus to the next focus group in the forward or backward direction.
332 Toolkit::Control::KeyboardFocusNavigationDirection direction = forward ? Toolkit::Control::Right : Toolkit::Control::Left;
333 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
334 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
337 if(!mFocusGroupChangedSignal.Empty())
339 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
340 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
346 void KeyboardFocusManager::DoActivate(Actor actor)
350 Toolkit::Control control = Toolkit::Control::DownCast(actor);
353 // Notify the control that it is activated
354 control.GetImplementation().Activate();
357 // Send notification for the activation of focused actor
358 if( !mFocusedActorActivatedSignal.Empty() )
360 mFocusedActorActivatedSignal.Emit(actor);
365 void KeyboardFocusManager::ClearFocus()
367 Actor actor = GetCurrentFocusActor();
370 if(mFocusIndicatorActor)
372 actor.Remove(mFocusIndicatorActor);
375 // Send notification for the change of focus actor
376 if( !mFocusChangedSignal.Empty() )
378 mFocusChangedSignal.Emit(actor, Actor());
382 mCurrentFocusActor = 0;
383 mIsFocusIndicatorEnabled = false;
386 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
388 mFocusGroupLoopEnabled = enabled;
391 bool KeyboardFocusManager::GetFocusGroupLoop() const
393 return mFocusGroupLoopEnabled;
396 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
400 // Create focus group property if not already created.
401 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
402 if(propertyIsFocusGroup == Property::INVALID_INDEX)
404 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup);
408 actor.SetProperty(propertyIsFocusGroup, isFocusGroup);
413 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
415 // Check whether the actor is a focus group
416 bool isFocusGroup = false;
420 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
421 if(propertyIsFocusGroup != Property::INVALID_INDEX)
423 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
430 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
432 // Go through the actor's hierarchy to check which focus group the actor belongs to
433 while (actor && !IsFocusGroup(actor))
435 actor = actor.GetParent();
441 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
443 if(mFocusIndicatorActor != indicator)
445 Actor currentFocusActor = GetCurrentFocusActor();
446 if(currentFocusActor)
448 // The new focus indicator should be added to the current focused actor immediately
449 if(mFocusIndicatorActor)
451 currentFocusActor.Remove(mFocusIndicatorActor);
456 currentFocusActor.Add(indicator);
460 mFocusIndicatorActor = indicator;
464 Actor KeyboardFocusManager::GetFocusIndicatorActor()
466 return mFocusIndicatorActor;
469 void KeyboardFocusManager::CreateDefaultFocusIndicatorActor()
471 // Create a focus indicator actor shared by all the keyboard focusable actors
472 Image borderImage = ResourceImage::New(FOCUS_BORDER_IMAGE_PATH);
474 ImageActor focusIndicator = ImageActor::New(borderImage);
475 focusIndicator.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
476 focusIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
477 focusIndicator.SetNinePatchBorder(FOCUS_BORDER_IMAGE_BORDER);
478 focusIndicator.SetPosition(Vector3(0.0f, 0.0f, 1.0f));
480 // Apply size constraint to the focus indicator
481 focusIndicator.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
483 SetFocusIndicatorActor(focusIndicator);
486 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
488 mIsKeyboardFocusEnabled = keyboard.IsAttached();
490 if(mIsKeyboardFocusEnabled)
492 // Show indicator when keyboard focus turned on if there is focused actor.
493 Actor actor = GetCurrentFocusActor();
496 if(mFocusIndicatorActor)
498 actor.Add(mFocusIndicatorActor);
501 mIsFocusIndicatorEnabled = true;
505 // Hide indicator when keyboard focus turned off
506 Actor actor = GetCurrentFocusActor();
509 actor.Remove(mFocusIndicatorActor);
511 mIsFocusIndicatorEnabled = false;
515 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
517 if(!mIsKeyboardFocusEnabled)
522 AccessibilityManager accessibilityManager = AccessibilityManager::Get();
523 bool isAccessibilityEnabled = accessibilityManager.IsEnabled();
525 Toolkit::FocusManager accessibilityFocusManager = Toolkit::FocusManager::Get();
527 std::string keyName = event.keyPressedName;
529 bool isFocusStartableKey = false;
531 if(event.state == KeyEvent::Down)
533 if (keyName == "Left")
535 if(!isAccessibilityEnabled)
537 if(!mIsFocusIndicatorEnabled)
539 // Show focus indicator
540 mIsFocusIndicatorEnabled = true;
544 // Move the focus towards left
545 MoveFocus(Toolkit::Control::Left);
548 isFocusStartableKey = true;
552 // Move the accessibility focus backward
553 accessibilityFocusManager.MoveFocusBackward();
556 else if (keyName == "Right")
558 if(!isAccessibilityEnabled)
560 if(!mIsFocusIndicatorEnabled)
562 // Show focus indicator
563 mIsFocusIndicatorEnabled = true;
567 // Move the focus towards right
568 MoveFocus(Toolkit::Control::Right);
573 // Move the accessibility focus forward
574 accessibilityFocusManager.MoveFocusForward();
577 isFocusStartableKey = true;
579 else if (keyName == "Up" && !isAccessibilityEnabled)
581 if(!mIsFocusIndicatorEnabled)
583 // Show focus indicator
584 mIsFocusIndicatorEnabled = true;
588 // Move the focus towards up
589 MoveFocus(Toolkit::Control::Up);
592 isFocusStartableKey = true;
594 else if (keyName == "Down" && !isAccessibilityEnabled)
596 if(!mIsFocusIndicatorEnabled)
598 // Show focus indicator
599 mIsFocusIndicatorEnabled = true;
603 // Move the focus towards down
604 MoveFocus(Toolkit::Control::Down);
607 isFocusStartableKey = true;
609 else if (keyName == "Tab" && !isAccessibilityEnabled)
611 if(!mIsFocusIndicatorEnabled)
613 // Show focus indicator
614 mIsFocusIndicatorEnabled = true;
618 // "Tab" key changes the focus group in the forward direction and
619 // "Shift-Tab" key changes it in the backward direction.
620 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
623 isFocusStartableKey = true;
625 else if (keyName == "space" && !isAccessibilityEnabled)
627 if(!mIsFocusIndicatorEnabled)
629 // Show focus indicator
630 mIsFocusIndicatorEnabled = true;
633 isFocusStartableKey = true;
635 else if (keyName == "" && !isAccessibilityEnabled)
637 // Check the fake key event for evas-plugin case
638 if(!mIsFocusIndicatorEnabled)
640 // Show focus indicator
641 mIsFocusIndicatorEnabled = true;
644 isFocusStartableKey = true;
646 else if (keyName == "Backspace" && !isAccessibilityEnabled)
648 // Emit signal to go back to the previous view???
651 else if(event.state == KeyEvent::Up)
653 if (keyName == "Return")
655 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
657 // Show focus indicator
658 mIsFocusIndicatorEnabled = true;
662 // Activate the focused actor
664 if(!isAccessibilityEnabled)
666 actor = GetCurrentFocusActor();
670 actor = accessibilityFocusManager.GetCurrentFocusActor();
679 isFocusStartableKey = true;
683 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
685 Actor actor = GetCurrentFocusActor();
688 // No actor is focused but keyboard focus is activated by the key press
689 // Let's try to move the initial focus
690 MoveFocus(Toolkit::Control::Right);
692 else if(mFocusIndicatorActor)
694 // Make sure the focused actor is highlighted
695 actor.Add(mFocusIndicatorActor);
700 void KeyboardFocusManager::OnTouched(const TouchEvent& touchEvent)
702 // Clear the focus when user touch the screen
706 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
708 return mPreFocusChangeSignal;
711 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
713 return mFocusChangedSignal;
716 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
718 return mFocusGroupChangedSignal;
721 Toolkit::KeyboardFocusManager::FocusedActorActivatedSignalType& KeyboardFocusManager::FocusedActorActivatedSignal()
723 return mFocusedActorActivatedSignal;
726 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
728 Dali::BaseHandle handle( object );
730 bool connected( true );
731 KeyboardFocusManager* manager = dynamic_cast<KeyboardFocusManager*>( object );
733 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
735 manager->PreFocusChangeSignal().Connect( tracker, functor );
737 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
739 manager->FocusChangedSignal().Connect( tracker, functor );
741 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
743 manager->FocusGroupChangedSignal().Connect( tracker, functor );
745 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ACTIVATED ) )
747 manager->FocusedActorActivatedSignal().Connect( tracker, functor );
751 // signalName does not match any signal
758 } // namespace Internal
760 } // namespace Toolkit