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/object/type-registry-helper.h>
30 #include <dali/public-api/images/resource-image.h>
31 #include <dali/integration-api/debug.h>
34 #include <dali-toolkit/public-api/controls/control.h>
35 #include <dali-toolkit/public-api/controls/control-impl.h>
36 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
37 #include <dali-toolkit/public-api/focus-manager/keyinput-focus-manager.h>
48 namespace // Unnamed namespace
51 #if defined(DEBUG_ENABLED)
52 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
55 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "is-keyboard-focus-group"; // This property will be replaced by a flag in Control.
57 const char* const FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "keyboard_focus.png";
58 const Vector4 FOCUS_BORDER_IMAGE_BORDER = Vector4(7.0f, 7.0f, 7.0f, 7.0f);
62 BaseHandle handle = KeyboardFocusManager::Get();
66 SingletonService singletonService( SingletonService::Get() );
67 if ( singletonService )
69 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
70 singletonService.Register( typeid( manager ), manager );
78 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true )
80 DALI_SIGNAL_REGISTRATION( KeyboardFocusManager, "keyboard-pre-focus-change", SIGNAL_PRE_FOCUS_CHANGE )
81 DALI_SIGNAL_REGISTRATION( KeyboardFocusManager, "keyboard-focus-changed", SIGNAL_FOCUS_CHANGED )
82 DALI_SIGNAL_REGISTRATION( KeyboardFocusManager, "keyboard-focus-group-changed", SIGNAL_FOCUS_GROUP_CHANGED )
83 DALI_SIGNAL_REGISTRATION( KeyboardFocusManager, "keyboard-focused-actor-activated", SIGNAL_FOCUSED_ACTOR_ACTIVATED )
85 DALI_TYPE_REGISTRATION_END()
87 } // unnamed namespace
89 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
91 Toolkit::KeyboardFocusManager manager;
93 SingletonService singletonService( SingletonService::Get() );
94 if ( singletonService )
96 // Check whether the keyboard focus manager is already created
97 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
100 // If so, downcast the handle of singleton to keyboard focus manager
101 manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
108 KeyboardFocusManager::KeyboardFocusManager()
109 : mCurrentFocusActor(0),
110 mFocusIndicatorActor(Actor()),
111 mFocusGroupLoopEnabled(false),
112 mIsKeyboardFocusEnabled(false),
113 mIsFocusIndicatorEnabled(false),
114 mIsWaitingKeyboardFocusChangeCommit(false),
117 CreateDefaultFocusIndicatorActor();
119 OnPhysicalKeyboardStatusChanged(PhysicalKeyboard::Get());
121 Toolkit::KeyInputFocusManager::Get().UnhandledKeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
122 Stage::GetCurrent().TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouched);
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);
147 // Check whether the actor is in the stage
150 // Set the focus only when the actor is keyboard focusable
151 if(actor.IsKeyboardFocusable())
153 // Draw the focus indicator upon the focused actor
154 if(mIsFocusIndicatorEnabled && 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 // Move the accessibility focus to the same actor
171 // Toolkit::FocusManager focusManager = Toolkit::FocusManager::Get();
172 // focusManager.SetCurrentFocusActor(actor);
174 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
179 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
183 Actor KeyboardFocusManager::GetCurrentFocusActor()
185 Actor rootActor = Stage::GetCurrent().GetRootLayer();
186 return rootActor.FindChildById(mCurrentFocusActor);
189 Actor KeyboardFocusManager::GetCurrentFocusGroup()
191 return GetFocusGroup(GetCurrentFocusActor());
194 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
196 Toolkit::Control control = Toolkit::Control::DownCast(actor);
197 return control && control.GetImplementation().IsKeyboardNavigationSupported();
200 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
202 // Get the actor's parent layout control that supports two dimensional keyboard navigation
203 Actor rootActor = Stage::GetCurrent().GetRootLayer();
207 parent = actor.GetParent();
210 while( parent && !IsLayoutControl(parent) && parent != rootActor )
212 parent = parent.GetParent();
215 return Toolkit::Control::DownCast(parent);
218 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocusNavigationDirection direction)
220 Actor currentFocusActor = GetCurrentFocusActor();
222 bool succeed = false;
224 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
225 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
226 while(parentLayoutControl && !succeed)
228 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
229 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
232 if(!succeed && !mPreFocusChangeSignal.Empty())
234 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
235 mIsWaitingKeyboardFocusChangeCommit = true;
236 Actor nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
237 mIsWaitingKeyboardFocusChangeCommit = false;
239 if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
241 // Whether the next focusable actor is a layout control
242 if(IsLayoutControl(nextFocusableActor))
244 // If so, move the focus inside it.
245 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
246 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
250 // Otherwise, just set focus to the next focusable actor
251 succeed = SetCurrentFocusActor(nextFocusableActor);
259 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction)
261 // Ask the control for the next actor to focus
262 Actor nextFocusableActor = control.GetImplementation().GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
263 if(nextFocusableActor)
265 if(!nextFocusableActor.IsKeyboardFocusable())
267 // If the actor is not focusable, ask the same layout control for the next actor to focus
268 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
272 Actor currentFocusActor = GetCurrentFocusActor();
273 Actor committedFocusActor = nextFocusableActor;
275 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
276 // Signal handler can check the proposed actor and return a different actor if it wishes.
277 if( !mPreFocusChangeSignal.Empty() )
279 mIsWaitingKeyboardFocusChangeCommit = true;
280 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
281 mIsWaitingKeyboardFocusChangeCommit = false;
284 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
286 // Whether the commited focusable actor is a layout control
287 if(IsLayoutControl(committedFocusActor))
289 // If so, move the focus inside it.
290 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
291 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
295 // Otherwise, just set focus to the next focusable actor
296 if(committedFocusActor == nextFocusableActor)
298 // If the application hasn't changed our proposed actor, we informs the layout control we will
299 // move the focus to what the control returns. The control might wish to perform some actions
300 // before the focus is actually moved.
301 control.GetImplementation().OnKeyboardFocusChangeCommitted(committedFocusActor);
304 return SetCurrentFocusActor(committedFocusActor);
315 // No more actor can be focused in the given direction within the same layout control.
320 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
322 bool succeed = false;
324 // Get the parent layout control of the current focus group
325 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
327 while(parentLayoutControl && !succeed)
329 // If the current focus group has a parent layout control, we can probably automatically
330 // move the focus to the next focus group in the forward or backward direction.
331 Toolkit::Control::KeyboardFocusNavigationDirection direction = forward ? Toolkit::Control::Right : Toolkit::Control::Left;
332 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
333 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
336 if(!mFocusGroupChangedSignal.Empty())
338 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
339 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
345 void KeyboardFocusManager::DoActivate(Actor actor)
349 Toolkit::Control control = Toolkit::Control::DownCast(actor);
352 // Notify the control that it is activated
353 control.GetImplementation().Activate();
356 // Send notification for the activation of focused actor
357 if( !mFocusedActorActivatedSignal.Empty() )
359 mFocusedActorActivatedSignal.Emit(actor);
364 void KeyboardFocusManager::ClearFocus()
366 Actor actor = GetCurrentFocusActor();
369 if(mFocusIndicatorActor)
371 actor.Remove(mFocusIndicatorActor);
374 // Send notification for the change of focus actor
375 if( !mFocusChangedSignal.Empty() )
377 mFocusChangedSignal.Emit(actor, Actor());
381 mCurrentFocusActor = 0;
382 mIsFocusIndicatorEnabled = false;
385 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
387 mFocusGroupLoopEnabled = enabled;
390 bool KeyboardFocusManager::GetFocusGroupLoop() const
392 return mFocusGroupLoopEnabled;
395 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
399 // Create focus group property if not already created.
400 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
401 if(propertyIsFocusGroup == Property::INVALID_INDEX)
403 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup);
407 actor.SetProperty(propertyIsFocusGroup, isFocusGroup);
412 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
414 // Check whether the actor is a focus group
415 bool isFocusGroup = false;
419 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
420 if(propertyIsFocusGroup != Property::INVALID_INDEX)
422 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
429 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
431 // Go through the actor's hierarchy to check which focus group the actor belongs to
432 while (actor && !IsFocusGroup(actor))
434 actor = actor.GetParent();
440 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
442 if(mFocusIndicatorActor != indicator)
444 Actor currentFocusActor = GetCurrentFocusActor();
445 if(currentFocusActor)
447 // The new focus indicator should be added to the current focused actor immediately
448 if(mFocusIndicatorActor)
450 currentFocusActor.Remove(mFocusIndicatorActor);
455 currentFocusActor.Add(indicator);
459 mFocusIndicatorActor = indicator;
463 Actor KeyboardFocusManager::GetFocusIndicatorActor()
465 return mFocusIndicatorActor;
468 void KeyboardFocusManager::CreateDefaultFocusIndicatorActor()
470 // Create a focus indicator actor shared by all the keyboard focusable actors
471 Image borderImage = ResourceImage::New(FOCUS_BORDER_IMAGE_PATH);
473 ImageActor focusIndicator = ImageActor::New(borderImage);
474 focusIndicator.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
475 focusIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
476 focusIndicator.SetNinePatchBorder(FOCUS_BORDER_IMAGE_BORDER);
477 focusIndicator.SetPosition(Vector3(0.0f, 0.0f, 1.0f));
479 // Apply size constraint to the focus indicator
480 focusIndicator.SetSizeMode( SIZE_EQUAL_TO_PARENT );
482 SetFocusIndicatorActor(focusIndicator);
485 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
487 mIsKeyboardFocusEnabled = keyboard.IsAttached();
489 if(mIsKeyboardFocusEnabled)
491 // Show indicator when keyboard focus turned on if there is focused actor.
492 Actor actor = GetCurrentFocusActor();
495 if(mFocusIndicatorActor)
497 actor.Add(mFocusIndicatorActor);
500 mIsFocusIndicatorEnabled = true;
504 // Hide indicator when keyboard focus turned off
505 Actor actor = GetCurrentFocusActor();
508 actor.Remove(mFocusIndicatorActor);
510 mIsFocusIndicatorEnabled = false;
514 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
516 if(!mIsKeyboardFocusEnabled)
521 AccessibilityManager accessibilityManager = AccessibilityManager::Get();
522 bool isAccessibilityEnabled = accessibilityManager.IsEnabled();
524 Toolkit::FocusManager accessibilityFocusManager = Toolkit::FocusManager::Get();
526 std::string keyName = event.keyPressedName;
528 bool isFocusStartableKey = false;
530 if(event.state == KeyEvent::Down)
532 if (keyName == "Left")
534 if(!isAccessibilityEnabled)
536 if(!mIsFocusIndicatorEnabled)
538 // Show focus indicator
539 mIsFocusIndicatorEnabled = true;
543 // Move the focus towards left
544 MoveFocus(Toolkit::Control::Left);
547 isFocusStartableKey = true;
551 // Move the accessibility focus backward
552 accessibilityFocusManager.MoveFocusBackward();
555 else if (keyName == "Right")
557 if(!isAccessibilityEnabled)
559 if(!mIsFocusIndicatorEnabled)
561 // Show focus indicator
562 mIsFocusIndicatorEnabled = true;
566 // Move the focus towards right
567 MoveFocus(Toolkit::Control::Right);
572 // Move the accessibility focus forward
573 accessibilityFocusManager.MoveFocusForward();
576 isFocusStartableKey = true;
578 else if (keyName == "Up" && !isAccessibilityEnabled)
580 if(!mIsFocusIndicatorEnabled)
582 // Show focus indicator
583 mIsFocusIndicatorEnabled = true;
587 // Move the focus towards up
588 MoveFocus(Toolkit::Control::Up);
591 isFocusStartableKey = true;
593 else if (keyName == "Down" && !isAccessibilityEnabled)
595 if(!mIsFocusIndicatorEnabled)
597 // Show focus indicator
598 mIsFocusIndicatorEnabled = true;
602 // Move the focus towards down
603 MoveFocus(Toolkit::Control::Down);
606 isFocusStartableKey = true;
608 else if (keyName == "Tab" && !isAccessibilityEnabled)
610 if(!mIsFocusIndicatorEnabled)
612 // Show focus indicator
613 mIsFocusIndicatorEnabled = true;
617 // "Tab" key changes the focus group in the forward direction and
618 // "Shift-Tab" key changes it in the backward direction.
619 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
622 isFocusStartableKey = true;
624 else if (keyName == "space" && !isAccessibilityEnabled)
626 if(!mIsFocusIndicatorEnabled)
628 // Show focus indicator
629 mIsFocusIndicatorEnabled = true;
632 isFocusStartableKey = true;
634 else if (keyName == "" && !isAccessibilityEnabled)
636 // Check the fake key event for evas-plugin case
637 if(!mIsFocusIndicatorEnabled)
639 // Show focus indicator
640 mIsFocusIndicatorEnabled = true;
643 isFocusStartableKey = true;
645 else if (keyName == "Backspace" && !isAccessibilityEnabled)
647 // Emit signal to go back to the previous view???
650 else if(event.state == KeyEvent::Up)
652 if (keyName == "Return")
654 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
656 // Show focus indicator
657 mIsFocusIndicatorEnabled = true;
661 // Activate the focused actor
663 if(!isAccessibilityEnabled)
665 actor = GetCurrentFocusActor();
669 actor = accessibilityFocusManager.GetCurrentFocusActor();
678 isFocusStartableKey = true;
682 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
684 Actor actor = GetCurrentFocusActor();
687 // No actor is focused but keyboard focus is activated by the key press
688 // Let's try to move the initial focus
689 MoveFocus(Toolkit::Control::Right);
691 else if(mFocusIndicatorActor)
693 // Make sure the focused actor is highlighted
694 actor.Add(mFocusIndicatorActor);
699 void KeyboardFocusManager::OnTouched(const TouchEvent& touchEvent)
701 // Clear the focus when user touch the screen
705 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
707 return mPreFocusChangeSignal;
710 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
712 return mFocusChangedSignal;
715 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
717 return mFocusGroupChangedSignal;
720 Toolkit::KeyboardFocusManager::FocusedActorActivatedSignalType& KeyboardFocusManager::FocusedActorActivatedSignal()
722 return mFocusedActorActivatedSignal;
725 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
727 Dali::BaseHandle handle( object );
729 bool connected( true );
730 KeyboardFocusManager* manager = dynamic_cast<KeyboardFocusManager*>( object );
732 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
734 manager->PreFocusChangeSignal().Connect( tracker, functor );
736 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
738 manager->FocusChangedSignal().Connect( tracker, functor );
740 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
742 manager->FocusGroupChangedSignal().Connect( tracker, functor );
744 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ACTIVATED ) )
746 manager->FocusedActorActivatedSignal().Connect( tracker, functor );
750 // signalName does not match any signal
757 } // namespace Internal
759 } // namespace Toolkit