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/devel-api/adaptor-framework/accessibility-adaptor.h>
25 #include <dali/devel-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/devel-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/accessibility-manager/accessibility-manager.h>
38 #include <dali-toolkit/devel-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( Toolkit, KeyboardFocusManager, "keyboard-pre-focus-change", SIGNAL_PRE_FOCUS_CHANGE )
82 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboard-focus-changed", SIGNAL_FOCUS_CHANGED )
83 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboard-focus-group-changed", SIGNAL_FOCUS_GROUP_CHANGED )
84 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboard-focused-actor-enter-key", SIGNAL_FOCUSED_ACTOR_ENTER_KEY )
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 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
176 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
180 Actor KeyboardFocusManager::GetCurrentFocusActor()
182 Actor rootActor = Stage::GetCurrent().GetRootLayer();
183 return rootActor.FindChildById(mCurrentFocusActor);
186 Actor KeyboardFocusManager::GetCurrentFocusGroup()
188 return GetFocusGroup(GetCurrentFocusActor());
191 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
193 Toolkit::Control control = Toolkit::Control::DownCast(actor);
194 return control && GetImplementation( control ).IsKeyboardNavigationSupported();
197 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
199 // Get the actor's parent layout control that supports two dimensional keyboard navigation
200 Actor rootActor = Stage::GetCurrent().GetRootLayer();
204 parent = actor.GetParent();
207 while( parent && !IsLayoutControl(parent) && parent != rootActor )
209 parent = parent.GetParent();
212 return Toolkit::Control::DownCast(parent);
215 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
217 Actor currentFocusActor = GetCurrentFocusActor();
219 bool succeed = false;
221 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
222 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
223 while(parentLayoutControl && !succeed)
225 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
226 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
229 if(!succeed && !mPreFocusChangeSignal.Empty())
231 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
232 mIsWaitingKeyboardFocusChangeCommit = true;
233 Actor nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
234 mIsWaitingKeyboardFocusChangeCommit = false;
236 if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
238 // Whether the next focusable actor is a layout control
239 if(IsLayoutControl(nextFocusableActor))
241 // If so, move the focus inside it.
242 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
243 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
247 // Otherwise, just set focus to the next focusable actor
248 succeed = SetCurrentFocusActor(nextFocusableActor);
256 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
258 // Ask the control for the next actor to focus
259 Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
260 if(nextFocusableActor)
262 if(!nextFocusableActor.IsKeyboardFocusable())
264 // If the actor is not focusable, ask the same layout control for the next actor to focus
265 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
269 Actor currentFocusActor = GetCurrentFocusActor();
270 Actor committedFocusActor = nextFocusableActor;
272 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
273 // Signal handler can check the proposed actor and return a different actor if it wishes.
274 if( !mPreFocusChangeSignal.Empty() )
276 mIsWaitingKeyboardFocusChangeCommit = true;
277 committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
278 mIsWaitingKeyboardFocusChangeCommit = false;
281 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
283 // Whether the commited focusable actor is a layout control
284 if(IsLayoutControl(committedFocusActor))
286 // If so, move the focus inside it.
287 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
288 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
292 // Otherwise, just set focus to the next focusable actor
293 if(committedFocusActor == nextFocusableActor)
295 // If the application hasn't changed our proposed actor, we informs the layout control we will
296 // move the focus to what the control returns. The control might wish to perform some actions
297 // before the focus is actually moved.
298 GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
301 return SetCurrentFocusActor(committedFocusActor);
312 // No more actor can be focused in the given direction within the same layout control.
317 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
319 bool succeed = false;
321 // Get the parent layout control of the current focus group
322 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
324 while(parentLayoutControl && !succeed)
326 // If the current focus group has a parent layout control, we can probably automatically
327 // move the focus to the next focus group in the forward or backward direction.
328 Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
329 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
330 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
333 if(!mFocusGroupChangedSignal.Empty())
335 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
336 mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
342 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
346 Toolkit::Control control = Toolkit::Control::DownCast( actor );
349 // Notify the control that enter has been pressed on it.
350 GetImplementation( control ).KeyboardEnter();
353 // Send a notification for the actor.
354 if( !mFocusedActorEnterKeySignal.Empty() )
356 mFocusedActorEnterKeySignal.Emit( actor );
361 void KeyboardFocusManager::ClearFocus()
363 Actor actor = GetCurrentFocusActor();
366 if(mFocusIndicatorActor)
368 actor.Remove(mFocusIndicatorActor);
371 // Send notification for the change of focus actor
372 if( !mFocusChangedSignal.Empty() )
374 mFocusChangedSignal.Emit(actor, Actor());
378 mCurrentFocusActor = 0;
379 mIsFocusIndicatorEnabled = false;
382 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
384 mFocusGroupLoopEnabled = enabled;
387 bool KeyboardFocusManager::GetFocusGroupLoop() const
389 return mFocusGroupLoopEnabled;
392 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
396 // Create focus group property if not already created.
397 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
398 if(propertyIsFocusGroup == Property::INVALID_INDEX)
400 actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
404 actor.SetProperty(propertyIsFocusGroup, isFocusGroup);
409 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
411 // Check whether the actor is a focus group
412 bool isFocusGroup = false;
416 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
417 if(propertyIsFocusGroup != Property::INVALID_INDEX)
419 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
426 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
428 // Go through the actor's hierarchy to check which focus group the actor belongs to
429 while (actor && !IsFocusGroup(actor))
431 actor = actor.GetParent();
437 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
439 if(mFocusIndicatorActor != indicator)
441 Actor currentFocusActor = GetCurrentFocusActor();
442 if(currentFocusActor)
444 // The new focus indicator should be added to the current focused actor immediately
445 if(mFocusIndicatorActor)
447 currentFocusActor.Remove(mFocusIndicatorActor);
452 currentFocusActor.Add(indicator);
456 mFocusIndicatorActor = indicator;
460 Actor KeyboardFocusManager::GetFocusIndicatorActor()
462 return mFocusIndicatorActor;
465 void KeyboardFocusManager::CreateDefaultFocusIndicatorActor()
467 // Create a focus indicator actor shared by all the keyboard focusable actors
468 Image borderImage = ResourceImage::New(FOCUS_BORDER_IMAGE_PATH);
470 ImageActor focusIndicator = ImageActor::New(borderImage);
471 focusIndicator.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
472 focusIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
473 focusIndicator.SetNinePatchBorder(FOCUS_BORDER_IMAGE_BORDER);
474 focusIndicator.SetPosition(Vector3(0.0f, 0.0f, 1.0f));
476 // Apply size constraint to the focus indicator
477 focusIndicator.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
479 SetFocusIndicatorActor(focusIndicator);
482 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
484 mIsKeyboardFocusEnabled = keyboard.IsAttached();
486 if(mIsKeyboardFocusEnabled)
488 // Show indicator when keyboard focus turned on if there is focused actor.
489 Actor actor = GetCurrentFocusActor();
492 if(mFocusIndicatorActor)
494 actor.Add(mFocusIndicatorActor);
497 mIsFocusIndicatorEnabled = true;
501 // Hide indicator when keyboard focus turned off
502 Actor actor = GetCurrentFocusActor();
505 actor.Remove(mFocusIndicatorActor);
507 mIsFocusIndicatorEnabled = false;
511 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
513 if(!mIsKeyboardFocusEnabled)
518 AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
519 bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
521 Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
523 std::string keyName = event.keyPressedName;
525 bool isFocusStartableKey = false;
527 if(event.state == KeyEvent::Down)
529 if (keyName == "Left")
531 if(!isAccessibilityEnabled)
533 if(!mIsFocusIndicatorEnabled)
535 // Show focus indicator
536 mIsFocusIndicatorEnabled = true;
540 // Move the focus towards left
541 MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
544 isFocusStartableKey = true;
548 // Move the accessibility focus backward
549 accessibilityManager.MoveFocusBackward();
552 else if (keyName == "Right")
554 if(!isAccessibilityEnabled)
556 if(!mIsFocusIndicatorEnabled)
558 // Show focus indicator
559 mIsFocusIndicatorEnabled = true;
563 // Move the focus towards right
564 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
569 // Move the accessibility focus forward
570 accessibilityManager.MoveFocusForward();
573 isFocusStartableKey = true;
575 else if (keyName == "Up" && !isAccessibilityEnabled)
577 if(!mIsFocusIndicatorEnabled)
579 // Show focus indicator
580 mIsFocusIndicatorEnabled = true;
584 // Move the focus towards up
585 MoveFocus(Toolkit::Control::KeyboardFocus::UP);
588 isFocusStartableKey = true;
590 else if (keyName == "Down" && !isAccessibilityEnabled)
592 if(!mIsFocusIndicatorEnabled)
594 // Show focus indicator
595 mIsFocusIndicatorEnabled = true;
599 // Move the focus towards down
600 MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
603 isFocusStartableKey = true;
605 else if (keyName == "Tab" && !isAccessibilityEnabled)
607 if(!mIsFocusIndicatorEnabled)
609 // Show focus indicator
610 mIsFocusIndicatorEnabled = true;
614 // "Tab" key changes the focus group in the forward direction and
615 // "Shift-Tab" key changes it in the backward direction.
616 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
619 isFocusStartableKey = true;
621 else if (keyName == "space" && !isAccessibilityEnabled)
623 if(!mIsFocusIndicatorEnabled)
625 // Show focus indicator
626 mIsFocusIndicatorEnabled = true;
629 isFocusStartableKey = true;
631 else if (keyName == "" && !isAccessibilityEnabled)
633 // Check the fake key event for evas-plugin case
634 if(!mIsFocusIndicatorEnabled)
636 // Show focus indicator
637 mIsFocusIndicatorEnabled = true;
640 isFocusStartableKey = true;
642 else if (keyName == "Backspace" && !isAccessibilityEnabled)
644 // Emit signal to go back to the previous view???
647 else if(event.state == KeyEvent::Up)
649 if (keyName == "Return")
651 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
653 // Show focus indicator
654 mIsFocusIndicatorEnabled = true;
658 // The focused actor has enter pressed on it
660 if( !isAccessibilityEnabled )
662 actor = GetCurrentFocusActor();
666 actor = accessibilityManager.GetCurrentFocusActor();
671 DoKeyboardEnter( actor );
675 isFocusStartableKey = true;
679 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
681 Actor actor = GetCurrentFocusActor();
684 // No actor is focused but keyboard focus is activated by the key press
685 // Let's try to move the initial focus
686 MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
688 else if(mFocusIndicatorActor)
690 // Make sure the focused actor is highlighted
691 actor.Add(mFocusIndicatorActor);
696 void KeyboardFocusManager::OnTouched(const TouchEvent& touchEvent)
698 // Clear the focus when user touch the screen
702 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
704 return mPreFocusChangeSignal;
707 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
709 return mFocusChangedSignal;
712 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
714 return mFocusGroupChangedSignal;
717 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
719 return mFocusedActorEnterKeySignal;
722 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
724 Dali::BaseHandle handle( object );
726 bool connected( true );
727 KeyboardFocusManager* manager = dynamic_cast<KeyboardFocusManager*>( object );
729 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
731 manager->PreFocusChangeSignal().Connect( tracker, functor );
733 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
735 manager->FocusChangedSignal().Connect( tracker, functor );
737 if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
739 manager->FocusGroupChangedSignal().Connect( tracker, functor );
741 else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
743 manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
747 // signalName does not match any signal
754 } // namespace Internal
756 } // namespace Toolkit