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>
31 #include <dali-toolkit/public-api/controls/control.h>
32 #include <dali-toolkit/public-api/controls/control-impl.h>
33 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
34 #include <dali-toolkit/public-api/focus-manager/keyinput-focus-manager.h>
35 #include <dali/integration-api/debug.h>
46 namespace // unnamed namespace
49 #if defined(DEBUG_ENABLED)
50 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
53 const std::string IS_FOCUS_GROUP_PROPERTY_NAME("is-keyboard-focus-group"); // This property will be replaced by a flag in Control.
55 const char* FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "keyboard_focus.png";
56 const Vector4 FOCUS_BORDER_IMAGE_BORDER = Vector4(7.0f, 7.0f, 7.0f, 7.0f);
60 BaseHandle handle = KeyboardFocusManager::Get();
64 SingletonService singletonService( SingletonService::Get() );
65 if ( singletonService )
67 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
68 singletonService.Register( typeid( manager ), manager );
75 TypeRegistration KEYBOARD_FOCUS_MANAGER_TYPE( typeid(Dali::Toolkit::KeyboardFocusManager), typeid(Dali::BaseHandle), Create, true /* Create instance at startup */ );
77 } // unnamed namespace
79 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
81 Toolkit::KeyboardFocusManager manager;
83 SingletonService singletonService( SingletonService::Get() );
84 if ( singletonService )
86 // Check whether the keyboard focus manager is already created
87 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
90 // If so, downcast the handle of singleton to keyboard focus manager
91 manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
98 KeyboardFocusManager::KeyboardFocusManager()
99 : mCurrentFocusActor(0),
100 mFocusIndicatorActor(Actor()),
101 mFocusGroupLoopEnabled(false),
102 mIsKeyboardFocusEnabled(false),
103 mIsFocusIndicatorEnabled(false),
104 mIsWaitingKeyboardFocusChangeCommit(false),
107 CreateDefaultFocusIndicatorActor();
109 OnPhysicalKeyboardStatusChanged(PhysicalKeyboard::Get());
111 Toolkit::KeyInputFocusManager::Get().UnhandledKeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
112 Stage::GetCurrent().TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouched);
113 PhysicalKeyboard::Get().StatusChangedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnPhysicalKeyboardStatusChanged);
116 KeyboardFocusManager::~KeyboardFocusManager()
120 bool KeyboardFocusManager::SetCurrentFocusActor(Actor actor)
122 DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
126 return DoSetCurrentFocusActor(actor.GetId());
132 bool KeyboardFocusManager::DoSetCurrentFocusActor(const unsigned int actorID)
134 Actor rootActor = Stage::GetCurrent().GetRootLayer();
135 Actor actor = rootActor.FindChildById(actorID);
137 // Check whether the actor is in the stage
140 // Set the focus only when the actor is keyboard focusable
141 if(actor.IsKeyboardFocusable())
143 // Draw the focus indicator upon the focused actor
144 if(mIsFocusIndicatorEnabled && mFocusIndicatorActor)
146 actor.Add(mFocusIndicatorActor);
149 // Send notification for the change of focus actor
150 if( !mFocusChangedSignalV2.Empty() )
152 mFocusChangedSignalV2.Emit(GetCurrentFocusActor(), actor);
155 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
157 // Save the current focused actor
158 mCurrentFocusActor = actorID;
160 // Move the accessibility focus to the same actor
161 // Toolkit::FocusManager focusManager = Toolkit::FocusManager::Get();
162 // focusManager.SetCurrentFocusActor(actor);
164 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
169 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
173 Actor KeyboardFocusManager::GetCurrentFocusActor()
175 Actor rootActor = Stage::GetCurrent().GetRootLayer();
176 return rootActor.FindChildById(mCurrentFocusActor);
179 Actor KeyboardFocusManager::GetCurrentFocusGroup()
181 return GetFocusGroup(GetCurrentFocusActor());
184 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
186 Toolkit::Control control = Toolkit::Control::DownCast(actor);
187 return control && control.GetImplementation().IsKeyboardNavigationSupported();
190 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
192 // Get the actor's parent layout control that supports two dimensional keyboard navigation
193 Actor rootActor = Stage::GetCurrent().GetRootLayer();
197 parent = actor.GetParent();
200 while( parent && !IsLayoutControl(parent) && parent != rootActor )
202 parent = parent.GetParent();
205 return Toolkit::Control::DownCast(parent);
208 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocusNavigationDirection direction)
210 Actor currentFocusActor = GetCurrentFocusActor();
212 bool succeed = false;
214 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
215 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
216 while(parentLayoutControl && !succeed)
218 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
219 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
222 if(!succeed && !mPreFocusChangeSignalV2.Empty())
224 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
225 mIsWaitingKeyboardFocusChangeCommit = true;
226 Actor nextFocusableActor = mPreFocusChangeSignalV2.Emit(currentFocusActor, Actor(), direction);
227 mIsWaitingKeyboardFocusChangeCommit = false;
229 if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
231 // Whether the next focusable actor is a layout control
232 if(IsLayoutControl(nextFocusableActor))
234 // If so, move the focus inside it.
235 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
236 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
240 // Otherwise, just set focus to the next focusable actor
241 succeed = SetCurrentFocusActor(nextFocusableActor);
249 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction)
251 // Ask the control for the next actor to focus
252 Actor nextFocusableActor = control.GetImplementation().GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
253 if(nextFocusableActor)
255 if(!nextFocusableActor.IsKeyboardFocusable())
257 // If the actor is not focusable, ask the same layout control for the next actor to focus
258 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
262 Actor currentFocusActor = GetCurrentFocusActor();
263 Actor committedFocusActor = nextFocusableActor;
265 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
266 // Signal handler can check the proposed actor and return a different actor if it wishes.
267 if( !mPreFocusChangeSignalV2.Empty() )
269 mIsWaitingKeyboardFocusChangeCommit = true;
270 committedFocusActor = mPreFocusChangeSignalV2.Emit(currentFocusActor, nextFocusableActor, direction);
271 mIsWaitingKeyboardFocusChangeCommit = false;
274 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
276 // Whether the commited focusable actor is a layout control
277 if(IsLayoutControl(committedFocusActor))
279 // If so, move the focus inside it.
280 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
281 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
285 // Otherwise, just set focus to the next focusable actor
286 if(committedFocusActor == nextFocusableActor)
288 // If the application hasn't changed our proposed actor, we informs the layout control we will
289 // move the focus to what the control returns. The control might wish to perform some actions
290 // before the focus is actually moved.
291 control.GetImplementation().OnKeyboardFocusChangeCommitted(committedFocusActor);
294 return SetCurrentFocusActor(committedFocusActor);
305 // No more actor can be focused in the given direction within the same layout control.
310 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
312 bool succeed = false;
314 // Get the parent layout control of the current focus group
315 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
317 while(parentLayoutControl && !succeed)
319 // If the current focus group has a parent layout control, we can probably automatically
320 // move the focus to the next focus group in the forward or backward direction.
321 Toolkit::Control::KeyboardFocusNavigationDirection direction = forward ? Toolkit::Control::Right : Toolkit::Control::Left;
322 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
323 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
326 if(!mFocusGroupChangedSignalV2.Empty())
328 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
329 mFocusGroupChangedSignalV2.Emit(GetCurrentFocusActor(), forward);
335 void KeyboardFocusManager::DoActivate(Actor actor)
339 Toolkit::Control control = Toolkit::Control::DownCast(actor);
342 // Notify the control that it is activated
343 control.GetImplementation().Activate();
346 // Send notification for the activation of focused actor
347 if( !mFocusedActorActivatedSignalV2.Empty() )
349 mFocusedActorActivatedSignalV2.Emit(actor);
354 void KeyboardFocusManager::ClearFocus()
356 Actor actor = GetCurrentFocusActor();
359 if(mFocusIndicatorActor)
361 actor.Remove(mFocusIndicatorActor);
364 // Send notification for the change of focus actor
365 if( !mFocusChangedSignalV2.Empty() )
367 mFocusChangedSignalV2.Emit(actor, Actor());
371 mCurrentFocusActor = 0;
372 mIsFocusIndicatorEnabled = false;
375 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
377 mFocusGroupLoopEnabled = enabled;
380 bool KeyboardFocusManager::GetFocusGroupLoop() const
382 return mFocusGroupLoopEnabled;
385 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
389 // Create focus group property if not already created.
390 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
391 if(propertyIsFocusGroup == Property::INVALID_INDEX)
393 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup);
397 actor.SetProperty(propertyIsFocusGroup, isFocusGroup);
402 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
404 // Check whether the actor is a focus group
405 bool isFocusGroup = false;
409 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
410 if(propertyIsFocusGroup != Property::INVALID_INDEX)
412 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
419 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
421 // Go through the actor's hierarchy to check which focus group the actor belongs to
422 while (actor && !IsFocusGroup(actor))
424 actor = actor.GetParent();
430 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
432 if(mFocusIndicatorActor != indicator)
434 Actor currentFocusActor = GetCurrentFocusActor();
435 if(currentFocusActor)
437 // The new focus indicator should be added to the current focused actor immediately
438 if(mFocusIndicatorActor)
440 currentFocusActor.Remove(mFocusIndicatorActor);
445 currentFocusActor.Add(indicator);
449 mFocusIndicatorActor = indicator;
453 Actor KeyboardFocusManager::GetFocusIndicatorActor()
455 return mFocusIndicatorActor;
458 void KeyboardFocusManager::CreateDefaultFocusIndicatorActor()
460 // Create a focus indicator actor shared by all the keyboard focusable actors
461 Image borderImage = Image::New(FOCUS_BORDER_IMAGE_PATH);
463 ImageActor focusIndicator = ImageActor::New(borderImage);
464 focusIndicator.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
465 focusIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
466 focusIndicator.SetNinePatchBorder(FOCUS_BORDER_IMAGE_BORDER);
467 focusIndicator.SetPosition(Vector3(0.0f, 0.0f, 1.0f));
469 // Apply size constraint to the focus indicator
470 Constraint constraint = Constraint::New<Vector3>(Actor::SIZE,
471 ParentSource(Actor::SIZE),
472 EqualToConstraint());
473 focusIndicator.ApplyConstraint(constraint);
475 SetFocusIndicatorActor(focusIndicator);
478 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
480 mIsKeyboardFocusEnabled = keyboard.IsAttached();
482 if(mIsKeyboardFocusEnabled)
484 // Show indicator when keyboard focus turned on if there is focused actor.
485 Actor actor = GetCurrentFocusActor();
488 if(mFocusIndicatorActor)
490 actor.Add(mFocusIndicatorActor);
493 mIsFocusIndicatorEnabled = true;
497 // Hide indicator when keyboard focus turned off
498 Actor actor = GetCurrentFocusActor();
501 actor.Remove(mFocusIndicatorActor);
503 mIsFocusIndicatorEnabled = false;
507 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
509 if(!mIsKeyboardFocusEnabled)
514 AccessibilityManager accessibilityManager = AccessibilityManager::Get();
515 bool isAccessibilityEnabled = accessibilityManager.IsEnabled();
517 Toolkit::FocusManager accessibilityFocusManager = Toolkit::FocusManager::Get();
519 std::string keyName = event.keyPressedName;
521 bool isFocusStartableKey = false;
523 if(event.state == KeyEvent::Down)
525 if (keyName == "Left")
527 if(!isAccessibilityEnabled)
529 if(!mIsFocusIndicatorEnabled)
531 // Show focus indicator
532 mIsFocusIndicatorEnabled = true;
536 // Move the focus towards left
537 MoveFocus(Toolkit::Control::Left);
540 isFocusStartableKey = true;
544 // Move the accessibility focus backward
545 accessibilityFocusManager.MoveFocusBackward();
548 else if (keyName == "Right")
550 if(!isAccessibilityEnabled)
552 if(!mIsFocusIndicatorEnabled)
554 // Show focus indicator
555 mIsFocusIndicatorEnabled = true;
559 // Move the focus towards right
560 MoveFocus(Toolkit::Control::Right);
563 isFocusStartableKey = true;
567 // Move the accessibility focus forward
568 accessibilityFocusManager.MoveFocusForward();
571 isFocusStartableKey = true;
573 else if (keyName == "Up" && !isAccessibilityEnabled)
575 if(!mIsFocusIndicatorEnabled)
577 // Show focus indicator
578 mIsFocusIndicatorEnabled = true;
582 // Move the focus towards up
583 MoveFocus(Toolkit::Control::Up);
586 isFocusStartableKey = true;
588 else if (keyName == "Down" && !isAccessibilityEnabled)
590 if(!mIsFocusIndicatorEnabled)
592 // Show focus indicator
593 mIsFocusIndicatorEnabled = true;
597 // Move the focus towards down
598 MoveFocus(Toolkit::Control::Down);
601 isFocusStartableKey = true;
603 else if (keyName == "Tab" && !isAccessibilityEnabled)
605 if(!mIsFocusIndicatorEnabled)
607 // Show focus indicator
608 mIsFocusIndicatorEnabled = true;
612 // "Tab" key changes the focus group in the forward direction and
613 // "Shift-Tab" key changes it in the backward direction.
614 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
617 isFocusStartableKey = true;
619 else if (keyName == "space" && !isAccessibilityEnabled)
621 if(!mIsFocusIndicatorEnabled)
623 // Show focus indicator
624 mIsFocusIndicatorEnabled = true;
627 isFocusStartableKey = true;
629 else if (keyName == "" && !isAccessibilityEnabled)
631 // Check the fake key event for evas-plugin case
632 if(!mIsFocusIndicatorEnabled)
634 // Show focus indicator
635 mIsFocusIndicatorEnabled = true;
638 isFocusStartableKey = true;
640 else if (keyName == "Backspace" && !isAccessibilityEnabled)
642 // Emit signal to go back to the previous view???
645 else if(event.state == KeyEvent::Up)
647 if (keyName == "Return")
649 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
651 // Show focus indicator
652 mIsFocusIndicatorEnabled = true;
656 // Activate the focused actor
658 if(!isAccessibilityEnabled)
660 actor = GetCurrentFocusActor();
664 actor = accessibilityFocusManager.GetCurrentFocusActor();
673 isFocusStartableKey = true;
677 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
679 Actor actor = GetCurrentFocusActor();
682 // No actor is focused but keyboard focus is activated by the key press
683 // Let's try to move the initial focus
684 MoveFocus(Toolkit::Control::Right);
686 else if(mFocusIndicatorActor)
688 // Make sure the focused actor is highlighted
689 actor.Add(mFocusIndicatorActor);
694 void KeyboardFocusManager::OnTouched(const TouchEvent& touchEvent)
696 // Clear the focus when user touch the screen
700 Toolkit::KeyboardFocusManager::PreFocusChangeSignalV2& KeyboardFocusManager::PreFocusChangeSignal()
702 return mPreFocusChangeSignalV2;
705 Toolkit::KeyboardFocusManager::FocusChangedSignalV2& KeyboardFocusManager::FocusChangedSignal()
707 return mFocusChangedSignalV2;
710 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalV2& KeyboardFocusManager::FocusGroupChangedSignal()
712 return mFocusGroupChangedSignalV2;
715 Toolkit::KeyboardFocusManager::FocusedActorActivatedSignalV2& KeyboardFocusManager::FocusedActorActivatedSignal()
717 return mFocusedActorActivatedSignalV2;
720 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
722 Dali::BaseHandle handle( object );
724 bool connected( true );
725 KeyboardFocusManager* manager = dynamic_cast<KeyboardFocusManager*>(object);
727 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_PRE_FOCUS_CHANGE == signalName )
729 manager->PreFocusChangeSignal().Connect( tracker, functor );
731 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUS_CHANGED == signalName )
733 manager->FocusChangedSignal().Connect( tracker, functor );
735 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUS_GROUP_CHANGED == signalName )
737 manager->FocusGroupChangedSignal().Connect( tracker, functor );
739 else if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUSED_ACTOR_ACTIVATED== signalName )
741 manager->FocusedActorActivatedSignal().Connect( tracker, functor );
745 // signalName does not match any signal
752 } // namespace Internal
754 } // namespace Toolkit