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>
32 #include <dali-toolkit/public-api/controls/control.h>
33 #include <dali-toolkit/public-api/controls/control-impl.h>
34 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
35 #include <dali-toolkit/public-api/focus-manager/keyinput-focus-manager.h>
36 #include <dali/integration-api/debug.h>
47 namespace // unnamed namespace
50 #if defined(DEBUG_ENABLED)
51 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
54 const std::string IS_FOCUS_GROUP_PROPERTY_NAME("is-keyboard-focus-group"); // This property will be replaced by a flag in Control.
56 const char* FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "keyboard_focus.png";
57 const Vector4 FOCUS_BORDER_IMAGE_BORDER = Vector4(7.0f, 7.0f, 7.0f, 7.0f);
61 BaseHandle handle = KeyboardFocusManager::Get();
65 SingletonService singletonService( SingletonService::Get() );
66 if ( singletonService )
68 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
69 singletonService.Register( typeid( manager ), manager );
76 TypeRegistration KEYBOARD_FOCUS_MANAGER_TYPE( typeid(Dali::Toolkit::KeyboardFocusManager), typeid(Dali::BaseHandle), Create, true /* Create instance at startup */ );
78 } // unnamed namespace
80 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
82 Toolkit::KeyboardFocusManager manager;
84 SingletonService singletonService( SingletonService::Get() );
85 if ( singletonService )
87 // Check whether the keyboard focus manager is already created
88 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
91 // If so, downcast the handle of singleton to keyboard focus manager
92 manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
99 KeyboardFocusManager::KeyboardFocusManager()
100 : mCurrentFocusActor(0),
101 mFocusIndicatorActor(Actor()),
102 mFocusGroupLoopEnabled(false),
103 mIsKeyboardFocusEnabled(false),
104 mIsFocusIndicatorEnabled(false),
105 mIsWaitingKeyboardFocusChangeCommit(false),
108 CreateDefaultFocusIndicatorActor();
110 OnPhysicalKeyboardStatusChanged(PhysicalKeyboard::Get());
112 Toolkit::KeyInputFocusManager::Get().UnhandledKeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
113 Stage::GetCurrent().TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouched);
114 PhysicalKeyboard::Get().StatusChangedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnPhysicalKeyboardStatusChanged);
117 KeyboardFocusManager::~KeyboardFocusManager()
121 bool KeyboardFocusManager::SetCurrentFocusActor(Actor actor)
123 DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
127 return DoSetCurrentFocusActor(actor.GetId());
133 bool KeyboardFocusManager::DoSetCurrentFocusActor(const unsigned int actorID)
135 Actor rootActor = Stage::GetCurrent().GetRootLayer();
136 Actor actor = rootActor.FindChildById(actorID);
138 // Check whether the actor is in the stage
141 // Set the focus only when the actor is keyboard focusable
142 if(actor.IsKeyboardFocusable())
144 // Draw the focus indicator upon the focused actor
145 if(mIsFocusIndicatorEnabled && mFocusIndicatorActor)
147 actor.Add(mFocusIndicatorActor);
150 // Send notification for the change of focus actor
151 if( !mFocusChangedSignal.Empty() )
153 mFocusChangedSignal.Emit(GetCurrentFocusActor(), actor);
156 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
158 // Save the current focused actor
159 mCurrentFocusActor = actorID;
161 // Move the accessibility focus to the same actor
162 // Toolkit::FocusManager focusManager = Toolkit::FocusManager::Get();
163 // focusManager.SetCurrentFocusActor(actor);
165 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
170 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
174 Actor KeyboardFocusManager::GetCurrentFocusActor()
176 Actor rootActor = Stage::GetCurrent().GetRootLayer();
177 return rootActor.FindChildById(mCurrentFocusActor);
180 Actor KeyboardFocusManager::GetCurrentFocusGroup()
182 return GetFocusGroup(GetCurrentFocusActor());
185 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
187 Toolkit::Control control = Toolkit::Control::DownCast(actor);
188 return control && control.GetImplementation().IsKeyboardNavigationSupported();
191 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
193 // Get the actor's parent layout control that supports two dimensional keyboard navigation
194 Actor rootActor = Stage::GetCurrent().GetRootLayer();
198 parent = actor.GetParent();
201 while( parent && !IsLayoutControl(parent) && parent != rootActor )
203 parent = parent.GetParent();
206 return Toolkit::Control::DownCast(parent);
209 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocusNavigationDirection direction)
211 Actor currentFocusActor = GetCurrentFocusActor();
213 bool succeed = false;
215 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
216 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
217 while(parentLayoutControl && !succeed)
219 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
220 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
223 if(!succeed && !mPreFocusChangeSignal.Empty())
225 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
226 mIsWaitingKeyboardFocusChangeCommit = true;
227 Actor nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
228 mIsWaitingKeyboardFocusChangeCommit = false;
230 if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
232 // Whether the next focusable actor is a layout control
233 if(IsLayoutControl(nextFocusableActor))
235 // If so, move the focus inside it.
236 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
237 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
241 // Otherwise, just set focus to the next focusable actor
242 succeed = SetCurrentFocusActor(nextFocusableActor);
250 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction)
252 // Ask the control for the next actor to focus
253 Actor nextFocusableActor = control.GetImplementation().GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
254 if(nextFocusableActor)
256 if(!nextFocusableActor.IsKeyboardFocusable())
258 // If the actor is not focusable, ask the same layout control for the next actor to focus
259 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
263 Actor currentFocusActor = GetCurrentFocusActor();
264 Actor committedFocusActor = nextFocusableActor;
266 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
267 // Signal handler can check the proposed actor and return a different actor if it wishes.
268 if( !mPreFocusChangeSignal.Empty() )
270 mIsWaitingKeyboardFocusChangeCommit = true;
271 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
272 mIsWaitingKeyboardFocusChangeCommit = false;
275 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
277 // Whether the commited focusable actor is a layout control
278 if(IsLayoutControl(committedFocusActor))
280 // If so, move the focus inside it.
281 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
282 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
286 // Otherwise, just set focus to the next focusable actor
287 if(committedFocusActor == nextFocusableActor)
289 // If the application hasn't changed our proposed actor, we informs the layout control we will
290 // move the focus to what the control returns. The control might wish to perform some actions
291 // before the focus is actually moved.
292 control.GetImplementation().OnKeyboardFocusChangeCommitted(committedFocusActor);
295 return SetCurrentFocusActor(committedFocusActor);
306 // No more actor can be focused in the given direction within the same layout control.
311 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
313 bool succeed = false;
315 // Get the parent layout control of the current focus group
316 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
318 while(parentLayoutControl && !succeed)
320 // If the current focus group has a parent layout control, we can probably automatically
321 // move the focus to the next focus group in the forward or backward direction.
322 Toolkit::Control::KeyboardFocusNavigationDirection direction = forward ? Toolkit::Control::Right : Toolkit::Control::Left;
323 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
324 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
327 if(!mFocusGroupChangedSignal.Empty())
329 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
330 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
336 void KeyboardFocusManager::DoActivate(Actor actor)
340 Toolkit::Control control = Toolkit::Control::DownCast(actor);
343 // Notify the control that it is activated
344 control.GetImplementation().Activate();
347 // Send notification for the activation of focused actor
348 if( !mFocusedActorActivatedSignal.Empty() )
350 mFocusedActorActivatedSignal.Emit(actor);
355 void KeyboardFocusManager::ClearFocus()
357 Actor actor = GetCurrentFocusActor();
360 if(mFocusIndicatorActor)
362 actor.Remove(mFocusIndicatorActor);
365 // Send notification for the change of focus actor
366 if( !mFocusChangedSignal.Empty() )
368 mFocusChangedSignal.Emit(actor, Actor());
372 mCurrentFocusActor = 0;
373 mIsFocusIndicatorEnabled = false;
376 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
378 mFocusGroupLoopEnabled = enabled;
381 bool KeyboardFocusManager::GetFocusGroupLoop() const
383 return mFocusGroupLoopEnabled;
386 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
390 // Create focus group property if not already created.
391 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
392 if(propertyIsFocusGroup == Property::INVALID_INDEX)
394 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup);
398 actor.SetProperty(propertyIsFocusGroup, isFocusGroup);
403 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
405 // Check whether the actor is a focus group
406 bool isFocusGroup = false;
410 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
411 if(propertyIsFocusGroup != Property::INVALID_INDEX)
413 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
420 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
422 // Go through the actor's hierarchy to check which focus group the actor belongs to
423 while (actor && !IsFocusGroup(actor))
425 actor = actor.GetParent();
431 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
433 if(mFocusIndicatorActor != indicator)
435 Actor currentFocusActor = GetCurrentFocusActor();
436 if(currentFocusActor)
438 // The new focus indicator should be added to the current focused actor immediately
439 if(mFocusIndicatorActor)
441 currentFocusActor.Remove(mFocusIndicatorActor);
446 currentFocusActor.Add(indicator);
450 mFocusIndicatorActor = indicator;
454 Actor KeyboardFocusManager::GetFocusIndicatorActor()
456 return mFocusIndicatorActor;
459 void KeyboardFocusManager::CreateDefaultFocusIndicatorActor()
461 // Create a focus indicator actor shared by all the keyboard focusable actors
462 Image borderImage = ResourceImage::New(FOCUS_BORDER_IMAGE_PATH);
464 ImageActor focusIndicator = ImageActor::New(borderImage);
465 focusIndicator.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
466 focusIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
467 focusIndicator.SetNinePatchBorder(FOCUS_BORDER_IMAGE_BORDER);
468 focusIndicator.SetPosition(Vector3(0.0f, 0.0f, 1.0f));
470 // Apply size constraint to the focus indicator
471 Constraint constraint = Constraint::New<Vector3>(Actor::SIZE,
472 ParentSource(Actor::SIZE),
473 EqualToConstraint());
474 focusIndicator.ApplyConstraint(constraint);
476 SetFocusIndicatorActor(focusIndicator);
479 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
481 mIsKeyboardFocusEnabled = keyboard.IsAttached();
483 if(mIsKeyboardFocusEnabled)
485 // Show indicator when keyboard focus turned on if there is focused actor.
486 Actor actor = GetCurrentFocusActor();
489 if(mFocusIndicatorActor)
491 actor.Add(mFocusIndicatorActor);
494 mIsFocusIndicatorEnabled = true;
498 // Hide indicator when keyboard focus turned off
499 Actor actor = GetCurrentFocusActor();
502 actor.Remove(mFocusIndicatorActor);
504 mIsFocusIndicatorEnabled = false;
508 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
510 if(!mIsKeyboardFocusEnabled)
515 AccessibilityManager accessibilityManager = AccessibilityManager::Get();
516 bool isAccessibilityEnabled = accessibilityManager.IsEnabled();
518 Toolkit::FocusManager accessibilityFocusManager = Toolkit::FocusManager::Get();
520 std::string keyName = event.keyPressedName;
522 bool isFocusStartableKey = false;
524 if(event.state == KeyEvent::Down)
526 if (keyName == "Left")
528 if(!isAccessibilityEnabled)
530 if(!mIsFocusIndicatorEnabled)
532 // Show focus indicator
533 mIsFocusIndicatorEnabled = true;
537 // Move the focus towards left
538 MoveFocus(Toolkit::Control::Left);
541 isFocusStartableKey = true;
545 // Move the accessibility focus backward
546 accessibilityFocusManager.MoveFocusBackward();
549 else if (keyName == "Right")
551 if(!isAccessibilityEnabled)
553 if(!mIsFocusIndicatorEnabled)
555 // Show focus indicator
556 mIsFocusIndicatorEnabled = true;
560 // Move the focus towards right
561 MoveFocus(Toolkit::Control::Right);
566 // Move the accessibility focus forward
567 accessibilityFocusManager.MoveFocusForward();
570 isFocusStartableKey = true;
572 else if (keyName == "Up" && !isAccessibilityEnabled)
574 if(!mIsFocusIndicatorEnabled)
576 // Show focus indicator
577 mIsFocusIndicatorEnabled = true;
581 // Move the focus towards up
582 MoveFocus(Toolkit::Control::Up);
585 isFocusStartableKey = true;
587 else if (keyName == "Down" && !isAccessibilityEnabled)
589 if(!mIsFocusIndicatorEnabled)
591 // Show focus indicator
592 mIsFocusIndicatorEnabled = true;
596 // Move the focus towards down
597 MoveFocus(Toolkit::Control::Down);
600 isFocusStartableKey = true;
602 else if (keyName == "Tab" && !isAccessibilityEnabled)
604 if(!mIsFocusIndicatorEnabled)
606 // Show focus indicator
607 mIsFocusIndicatorEnabled = true;
611 // "Tab" key changes the focus group in the forward direction and
612 // "Shift-Tab" key changes it in the backward direction.
613 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
616 isFocusStartableKey = true;
618 else if (keyName == "space" && !isAccessibilityEnabled)
620 if(!mIsFocusIndicatorEnabled)
622 // Show focus indicator
623 mIsFocusIndicatorEnabled = true;
626 isFocusStartableKey = true;
628 else if (keyName == "" && !isAccessibilityEnabled)
630 // Check the fake key event for evas-plugin case
631 if(!mIsFocusIndicatorEnabled)
633 // Show focus indicator
634 mIsFocusIndicatorEnabled = true;
637 isFocusStartableKey = true;
639 else if (keyName == "Backspace" && !isAccessibilityEnabled)
641 // Emit signal to go back to the previous view???
644 else if(event.state == KeyEvent::Up)
646 if (keyName == "Return")
648 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
650 // Show focus indicator
651 mIsFocusIndicatorEnabled = true;
655 // Activate the focused actor
657 if(!isAccessibilityEnabled)
659 actor = GetCurrentFocusActor();
663 actor = accessibilityFocusManager.GetCurrentFocusActor();
672 isFocusStartableKey = true;
676 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
678 Actor actor = GetCurrentFocusActor();
681 // No actor is focused but keyboard focus is activated by the key press
682 // Let's try to move the initial focus
683 MoveFocus(Toolkit::Control::Right);
685 else if(mFocusIndicatorActor)
687 // Make sure the focused actor is highlighted
688 actor.Add(mFocusIndicatorActor);
693 void KeyboardFocusManager::OnTouched(const TouchEvent& touchEvent)
695 // Clear the focus when user touch the screen
699 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
701 return mPreFocusChangeSignal;
704 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
706 return mFocusChangedSignal;
709 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
711 return mFocusGroupChangedSignal;
714 Toolkit::KeyboardFocusManager::FocusedActorActivatedSignalType& KeyboardFocusManager::FocusedActorActivatedSignal()
716 return mFocusedActorActivatedSignal;
719 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
721 Dali::BaseHandle handle( object );
723 bool connected( true );
724 KeyboardFocusManager* manager = dynamic_cast<KeyboardFocusManager*>(object);
726 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_PRE_FOCUS_CHANGE == signalName )
728 manager->PreFocusChangeSignal().Connect( tracker, functor );
730 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUS_CHANGED == signalName )
732 manager->FocusChangedSignal().Connect( tracker, functor );
734 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUS_GROUP_CHANGED == signalName )
736 manager->FocusGroupChangedSignal().Connect( tracker, functor );
738 else if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUSED_ACTOR_ACTIVATED== signalName )
740 manager->FocusedActorActivatedSignal().Connect( tracker, functor );
744 // signalName does not match any signal
751 } // namespace Internal
753 } // namespace Toolkit