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 focusIndicator.SetSizeMode( SIZE_EQUAL_TO_PARENT );
473 SetFocusIndicatorActor(focusIndicator);
476 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
478 mIsKeyboardFocusEnabled = keyboard.IsAttached();
480 if(mIsKeyboardFocusEnabled)
482 // Show indicator when keyboard focus turned on if there is focused actor.
483 Actor actor = GetCurrentFocusActor();
486 if(mFocusIndicatorActor)
488 actor.Add(mFocusIndicatorActor);
491 mIsFocusIndicatorEnabled = true;
495 // Hide indicator when keyboard focus turned off
496 Actor actor = GetCurrentFocusActor();
499 actor.Remove(mFocusIndicatorActor);
501 mIsFocusIndicatorEnabled = false;
505 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
507 if(!mIsKeyboardFocusEnabled)
512 AccessibilityManager accessibilityManager = AccessibilityManager::Get();
513 bool isAccessibilityEnabled = accessibilityManager.IsEnabled();
515 Toolkit::FocusManager accessibilityFocusManager = Toolkit::FocusManager::Get();
517 std::string keyName = event.keyPressedName;
519 bool isFocusStartableKey = false;
521 if(event.state == KeyEvent::Down)
523 if (keyName == "Left")
525 if(!isAccessibilityEnabled)
527 if(!mIsFocusIndicatorEnabled)
529 // Show focus indicator
530 mIsFocusIndicatorEnabled = true;
534 // Move the focus towards left
535 MoveFocus(Toolkit::Control::Left);
538 isFocusStartableKey = true;
542 // Move the accessibility focus backward
543 accessibilityFocusManager.MoveFocusBackward();
546 else if (keyName == "Right")
548 if(!isAccessibilityEnabled)
550 if(!mIsFocusIndicatorEnabled)
552 // Show focus indicator
553 mIsFocusIndicatorEnabled = true;
557 // Move the focus towards right
558 MoveFocus(Toolkit::Control::Right);
563 // Move the accessibility focus forward
564 accessibilityFocusManager.MoveFocusForward();
567 isFocusStartableKey = true;
569 else if (keyName == "Up" && !isAccessibilityEnabled)
571 if(!mIsFocusIndicatorEnabled)
573 // Show focus indicator
574 mIsFocusIndicatorEnabled = true;
578 // Move the focus towards up
579 MoveFocus(Toolkit::Control::Up);
582 isFocusStartableKey = true;
584 else if (keyName == "Down" && !isAccessibilityEnabled)
586 if(!mIsFocusIndicatorEnabled)
588 // Show focus indicator
589 mIsFocusIndicatorEnabled = true;
593 // Move the focus towards down
594 MoveFocus(Toolkit::Control::Down);
597 isFocusStartableKey = true;
599 else if (keyName == "Tab" && !isAccessibilityEnabled)
601 if(!mIsFocusIndicatorEnabled)
603 // Show focus indicator
604 mIsFocusIndicatorEnabled = true;
608 // "Tab" key changes the focus group in the forward direction and
609 // "Shift-Tab" key changes it in the backward direction.
610 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
613 isFocusStartableKey = true;
615 else if (keyName == "space" && !isAccessibilityEnabled)
617 if(!mIsFocusIndicatorEnabled)
619 // Show focus indicator
620 mIsFocusIndicatorEnabled = true;
623 isFocusStartableKey = true;
625 else if (keyName == "" && !isAccessibilityEnabled)
627 // Check the fake key event for evas-plugin case
628 if(!mIsFocusIndicatorEnabled)
630 // Show focus indicator
631 mIsFocusIndicatorEnabled = true;
634 isFocusStartableKey = true;
636 else if (keyName == "Backspace" && !isAccessibilityEnabled)
638 // Emit signal to go back to the previous view???
641 else if(event.state == KeyEvent::Up)
643 if (keyName == "Return")
645 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
647 // Show focus indicator
648 mIsFocusIndicatorEnabled = true;
652 // Activate the focused actor
654 if(!isAccessibilityEnabled)
656 actor = GetCurrentFocusActor();
660 actor = accessibilityFocusManager.GetCurrentFocusActor();
669 isFocusStartableKey = true;
673 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
675 Actor actor = GetCurrentFocusActor();
678 // No actor is focused but keyboard focus is activated by the key press
679 // Let's try to move the initial focus
680 MoveFocus(Toolkit::Control::Right);
682 else if(mFocusIndicatorActor)
684 // Make sure the focused actor is highlighted
685 actor.Add(mFocusIndicatorActor);
690 void KeyboardFocusManager::OnTouched(const TouchEvent& touchEvent)
692 // Clear the focus when user touch the screen
696 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
698 return mPreFocusChangeSignal;
701 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
703 return mFocusChangedSignal;
706 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
708 return mFocusGroupChangedSignal;
711 Toolkit::KeyboardFocusManager::FocusedActorActivatedSignalType& KeyboardFocusManager::FocusedActorActivatedSignal()
713 return mFocusedActorActivatedSignal;
716 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
718 Dali::BaseHandle handle( object );
720 bool connected( true );
721 KeyboardFocusManager* manager = dynamic_cast<KeyboardFocusManager*>(object);
723 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_PRE_FOCUS_CHANGE == signalName )
725 manager->PreFocusChangeSignal().Connect( tracker, functor );
727 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUS_CHANGED == signalName )
729 manager->FocusChangedSignal().Connect( tracker, functor );
731 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUS_GROUP_CHANGED == signalName )
733 manager->FocusGroupChangedSignal().Connect( tracker, functor );
735 else if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUSED_ACTOR_ACTIVATED== signalName )
737 manager->FocusedActorActivatedSignal().Connect( tracker, functor );
741 // signalName does not match any signal
748 } // namespace Internal
750 } // namespace Toolkit