2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include "keyboard-focus-manager-impl.h"
21 #include <dali-toolkit/public-api/controls/control.h>
22 #include <dali-toolkit/public-api/controls/control-impl.h>
23 #include <dali-toolkit/public-api/focus-manager/focus-manager.h>
24 #include <dali-toolkit/public-api/focus-manager/keyinput-focus-manager.h>
25 #include <dali/integration-api/debug.h>
36 namespace // unnamed namespace
39 #if defined(DEBUG_ENABLED)
40 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
43 const std::string IS_FOCUS_GROUP_PROPERTY_NAME("is-keyboard-focus-group"); // This property will be replaced by a flag in ControlImpl.
45 const char* FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "keyboard_focus.png";
46 const Vector4 FOCUS_BORDER_IMAGE_BORDER = Vector4(7.0f, 7.0f, 7.0f, 7.0f);
50 BaseHandle handle = KeyboardFocusManager::Get();
52 if ( !handle && Adaptor::IsAvailable() )
54 Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
55 Adaptor::Get().RegisterSingleton( typeid( manager ), manager );
61 TypeRegistration KEYBOARD_FOCUS_MANAGER_TYPE( typeid(Dali::Toolkit::KeyboardFocusManager), typeid(Dali::BaseHandle), Create, true /* Create instance at startup */ );
63 } // unnamed namespace
65 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
67 Toolkit::KeyboardFocusManager manager;
69 if ( Adaptor::IsAvailable() )
71 // Check whether the keyboard focus manager is already created
72 Dali::BaseHandle handle = Dali::Adaptor::Get().GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
75 // If so, downcast the handle of singleton to keyboard focus manager
76 manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
83 KeyboardFocusManager::KeyboardFocusManager()
84 : mCurrentFocusActor(0),
85 mFocusIndicatorActor(Actor()),
86 mFocusGroupLoopEnabled(false),
87 mIsKeyboardFocusEnabled(false),
88 mIsFocusIndicatorEnabled(false),
89 mIsWaitingKeyboardFocusChangeCommit(false),
92 CreateDefaultFocusIndicatorActor();
94 OnPhysicalKeyboardStatusChanged(PhysicalKeyboard::Get());
96 Toolkit::KeyInputFocusManager::Get().UnhandledKeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
97 Stage::GetCurrent().TouchedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnTouched);
98 PhysicalKeyboard::Get().StatusChangedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnPhysicalKeyboardStatusChanged);
101 KeyboardFocusManager::~KeyboardFocusManager()
105 bool KeyboardFocusManager::SetCurrentFocusActor(Actor actor)
107 DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
111 return DoSetCurrentFocusActor(actor.GetId());
117 bool KeyboardFocusManager::DoSetCurrentFocusActor(const unsigned int actorID)
119 Actor rootActor = Stage::GetCurrent().GetRootLayer();
120 Actor actor = rootActor.FindChildById(actorID);
122 // Check whether the actor is in the stage
125 // Set the focus only when the actor is keyboard focusable
126 if(actor.IsKeyboardFocusable())
128 // Draw the focus indicator upon the focused actor
129 if(mIsFocusIndicatorEnabled && mFocusIndicatorActor)
131 actor.Add(mFocusIndicatorActor);
134 // Send notification for the change of focus actor
135 if( !mFocusChangedSignalV2.Empty() )
137 mFocusChangedSignalV2.Emit(GetCurrentFocusActor(), actor);
140 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
142 // Save the current focused actor
143 mCurrentFocusActor = actorID;
145 // Move the accessibility focus to the same actor
146 // Toolkit::FocusManager focusManager = Toolkit::FocusManager::Get();
147 // focusManager.SetCurrentFocusActor(actor);
149 DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
154 DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
158 Actor KeyboardFocusManager::GetCurrentFocusActor()
160 Actor rootActor = Stage::GetCurrent().GetRootLayer();
161 return rootActor.FindChildById(mCurrentFocusActor);
164 Actor KeyboardFocusManager::GetCurrentFocusGroup()
166 return GetFocusGroup(GetCurrentFocusActor());
169 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
171 Toolkit::Control control = Toolkit::Control::DownCast(actor);
172 return control && control.GetImplementation().IsKeyboardNavigationSupported();
175 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
177 // Get the actor's parent layout control that supports two dimensional keyboard navigation
178 Actor rootActor = Stage::GetCurrent().GetRootLayer();
182 parent = actor.GetParent();
185 while( parent && !IsLayoutControl(parent) && parent != rootActor )
187 parent = parent.GetParent();
190 return Toolkit::Control::DownCast(parent);
193 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocusNavigationDirection direction)
195 Actor currentFocusActor = GetCurrentFocusActor();
197 bool succeed = false;
199 // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
200 Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
201 while(parentLayoutControl && !succeed)
203 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
204 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
207 if(!succeed && !mPreFocusChangeSignalV2.Empty())
209 // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
210 mIsWaitingKeyboardFocusChangeCommit = true;
211 Actor nextFocusableActor = mPreFocusChangeSignalV2.Emit(currentFocusActor, Actor(), direction);
212 mIsWaitingKeyboardFocusChangeCommit = false;
214 if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
216 // Whether the next focusable actor is a layout control
217 if(IsLayoutControl(nextFocusableActor))
219 // If so, move the focus inside it.
220 Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
221 succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
225 // Otherwise, just set focus to the next focusable actor
226 succeed = SetCurrentFocusActor(nextFocusableActor);
234 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction)
236 // Ask the control for the next actor to focus
237 Actor nextFocusableActor = control.GetImplementation().GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
238 if(nextFocusableActor)
240 if(!nextFocusableActor.IsKeyboardFocusable())
242 // If the actor is not focusable, ask the same layout control for the next actor to focus
243 return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
247 Actor currentFocusActor = GetCurrentFocusActor();
248 Actor committedFocusActor = nextFocusableActor;
250 // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
251 // Signal handler can check the proposed actor and return a different actor if it wishes.
252 if( !mPreFocusChangeSignalV2.Empty() )
254 mIsWaitingKeyboardFocusChangeCommit = true;
255 committedFocusActor = mPreFocusChangeSignalV2.Emit(currentFocusActor, nextFocusableActor, direction);
256 mIsWaitingKeyboardFocusChangeCommit = false;
259 if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
261 // Whether the commited focusable actor is a layout control
262 if(IsLayoutControl(committedFocusActor))
264 // If so, move the focus inside it.
265 Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
266 return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
270 // Otherwise, just set focus to the next focusable actor
271 if(committedFocusActor == nextFocusableActor)
273 // If the application hasn't changed our proposed actor, we informs the layout control we will
274 // move the focus to what the control returns. The control might wish to perform some actions
275 // before the focus is actually moved.
276 control.GetImplementation().OnKeyboardFocusChangeCommitted(committedFocusActor);
279 return SetCurrentFocusActor(committedFocusActor);
290 // No more actor can be focused in the given direction within the same layout control.
295 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
297 bool succeed = false;
299 // Get the parent layout control of the current focus group
300 Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
302 while(parentLayoutControl && !succeed)
304 // If the current focus group has a parent layout control, we can probably automatically
305 // move the focus to the next focus group in the forward or backward direction.
306 Toolkit::Control::KeyboardFocusNavigationDirection direction = forward ? Toolkit::Control::Right : Toolkit::Control::Left;
307 succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
308 parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
311 if(!mFocusGroupChangedSignalV2.Empty())
313 // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
314 mFocusGroupChangedSignalV2.Emit(GetCurrentFocusActor(), forward);
320 void KeyboardFocusManager::DoActivate(Actor actor)
324 Toolkit::Control control = Toolkit::Control::DownCast(actor);
327 // Notify the control that it is activated
328 control.GetImplementation().OnActivated();
331 // Send notification for the activation of focused actor
332 if( !mFocusedActorActivatedSignalV2.Empty() )
334 mFocusedActorActivatedSignalV2.Emit(actor);
339 void KeyboardFocusManager::ClearFocus()
341 Actor actor = GetCurrentFocusActor();
344 actor.Remove(mFocusIndicatorActor);
346 // Send notification for the change of focus actor
347 if( !mFocusChangedSignalV2.Empty() )
349 mFocusChangedSignalV2.Emit(actor, Actor());
353 mCurrentFocusActor = 0;
354 mIsFocusIndicatorEnabled = false;
357 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
359 mFocusGroupLoopEnabled = enabled;
362 bool KeyboardFocusManager::GetFocusGroupLoop() const
364 return mFocusGroupLoopEnabled;
367 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
371 // Create focus group property if not already created.
372 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
373 if(propertyIsFocusGroup == Property::INVALID_INDEX)
375 propertyIsFocusGroup = actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup);
379 actor.SetProperty(propertyIsFocusGroup, isFocusGroup);
384 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
386 // Check whether the actor is a focus group
387 bool isFocusGroup = false;
391 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
392 if(propertyIsFocusGroup != Property::INVALID_INDEX)
394 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
401 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
403 // Go through the actor's hierarchy to check which focus group the actor belongs to
404 while (actor && !IsFocusGroup(actor))
406 actor = actor.GetParent();
412 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
414 mFocusIndicatorActor = indicator;
417 Actor KeyboardFocusManager::GetFocusIndicatorActor()
419 return mFocusIndicatorActor;
422 void KeyboardFocusManager::CreateDefaultFocusIndicatorActor()
424 // Create a focus indicator actor shared by all the keyboard focusable actors
425 Image borderImage = Image::New(FOCUS_BORDER_IMAGE_PATH);
427 ImageActor focusIndicator = ImageActor::New(borderImage);
428 focusIndicator.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
429 focusIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
430 focusIndicator.SetNinePatchBorder(FOCUS_BORDER_IMAGE_BORDER);
431 focusIndicator.SetPosition(Vector3(0.0f, 0.0f, 1.0f));
433 // Apply size constraint to the focus indicator
434 Constraint constraint = Constraint::New<Vector3>(Actor::SIZE,
435 ParentSource(Actor::SIZE),
436 EqualToConstraint());
437 focusIndicator.ApplyConstraint(constraint);
439 SetFocusIndicatorActor(focusIndicator);
442 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
444 mIsKeyboardFocusEnabled = keyboard.IsAttached();
446 if(mIsKeyboardFocusEnabled)
448 // Show indicator when keyboard focus turned on if there is focused actor.
449 Actor actor = GetCurrentFocusActor();
452 if(mFocusIndicatorActor)
454 actor.Add(mFocusIndicatorActor);
457 mIsFocusIndicatorEnabled = true;
461 // Hide indicator when keyboard focus turned off
462 Actor actor = GetCurrentFocusActor();
465 actor.Remove(mFocusIndicatorActor);
467 mIsFocusIndicatorEnabled = false;
471 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
473 if(!mIsKeyboardFocusEnabled)
478 AccessibilityManager accessibilityManager = AccessibilityManager::Get();
479 bool isAccessibilityEnabled = accessibilityManager.IsEnabled();
481 Toolkit::FocusManager accessibilityFocusManager = Toolkit::FocusManager::Get();
483 std::string keyName = event.keyPressedName;
485 bool isFocusStartableKey = false;
487 if(event.state == KeyEvent::Down)
489 if (keyName == "Left")
491 if(!isAccessibilityEnabled)
493 if(!mIsFocusIndicatorEnabled)
495 // Show focus indicator
496 mIsFocusIndicatorEnabled = true;
500 // Move the focus towards left
501 MoveFocus(Toolkit::Control::Left);
504 isFocusStartableKey = true;
508 // Move the accessibility focus backward
509 accessibilityFocusManager.MoveFocusBackward();
512 else if (keyName == "Right")
514 if(!isAccessibilityEnabled)
516 if(!mIsFocusIndicatorEnabled)
518 // Show focus indicator
519 mIsFocusIndicatorEnabled = true;
523 // Move the focus towards right
524 MoveFocus(Toolkit::Control::Right);
527 isFocusStartableKey = true;
531 // Move the accessibility focus forward
532 accessibilityFocusManager.MoveFocusForward();
535 isFocusStartableKey = true;
537 else if (keyName == "Up" && !isAccessibilityEnabled)
539 if(!mIsFocusIndicatorEnabled)
541 // Show focus indicator
542 mIsFocusIndicatorEnabled = true;
546 // Move the focus towards up
547 MoveFocus(Toolkit::Control::Up);
550 isFocusStartableKey = true;
552 else if (keyName == "Down" && !isAccessibilityEnabled)
554 if(!mIsFocusIndicatorEnabled)
556 // Show focus indicator
557 mIsFocusIndicatorEnabled = true;
561 // Move the focus towards down
562 MoveFocus(Toolkit::Control::Down);
565 isFocusStartableKey = true;
567 else if (keyName == "Tab" && !isAccessibilityEnabled)
569 if(!mIsFocusIndicatorEnabled)
571 // Show focus indicator
572 mIsFocusIndicatorEnabled = true;
576 // "Tab" key changes the focus group in the forward direction and
577 // "Shift-Tab" key changes it in the backward direction.
578 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
581 isFocusStartableKey = true;
583 else if (keyName == "space" && !isAccessibilityEnabled)
585 if(!mIsFocusIndicatorEnabled)
587 // Show focus indicator
588 mIsFocusIndicatorEnabled = true;
591 isFocusStartableKey = true;
593 else if (keyName == "" && !isAccessibilityEnabled)
595 // Check the fake key event for evas-plugin case
596 if(!mIsFocusIndicatorEnabled)
598 // Show focus indicator
599 mIsFocusIndicatorEnabled = true;
602 isFocusStartableKey = true;
604 else if (keyName == "Backspace" && !isAccessibilityEnabled)
606 // Emit signal to go back to the previous view???
609 else if(event.state == KeyEvent::Up)
611 if (keyName == "Return")
613 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
615 // Show focus indicator
616 mIsFocusIndicatorEnabled = true;
620 // Activate the focused actor
622 if(!isAccessibilityEnabled)
624 actor = GetCurrentFocusActor();
628 actor = accessibilityFocusManager.GetCurrentFocusActor();
637 isFocusStartableKey = true;
641 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
643 Actor actor = GetCurrentFocusActor();
646 // No actor is focused but keyboard focus is activated by the key press
647 // Let's try to move the initial focus
648 MoveFocus(Toolkit::Control::Right);
650 else if(mFocusIndicatorActor)
652 // Make sure the focused actor is highlighted
653 actor.Add(mFocusIndicatorActor);
658 void KeyboardFocusManager::OnTouched(const TouchEvent& touchEvent)
660 // Clear the focus when user touch the screen
664 Toolkit::KeyboardFocusManager::PreFocusChangeSignalV2& KeyboardFocusManager::PreFocusChangeSignal()
666 return mPreFocusChangeSignalV2;
669 Toolkit::KeyboardFocusManager::FocusChangedSignalV2& KeyboardFocusManager::FocusChangedSignal()
671 return mFocusChangedSignalV2;
674 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalV2& KeyboardFocusManager::FocusGroupChangedSignal()
676 return mFocusGroupChangedSignalV2;
679 Toolkit::KeyboardFocusManager::FocusedActorActivatedSignalV2& KeyboardFocusManager::FocusedActorActivatedSignal()
681 return mFocusedActorActivatedSignalV2;
684 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
686 Dali::BaseHandle handle( object );
688 bool connected( true );
689 KeyboardFocusManager* manager = dynamic_cast<KeyboardFocusManager*>(object);
691 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_PRE_FOCUS_CHANGE == signalName )
693 manager->PreFocusChangeSignal().Connect( tracker, functor );
695 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUS_CHANGED == signalName )
697 manager->FocusChangedSignal().Connect( tracker, functor );
699 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUS_GROUP_CHANGED == signalName )
701 manager->FocusGroupChangedSignal().Connect( tracker, functor );
703 else if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUSED_ACTOR_ACTIVATED== signalName )
705 manager->FocusedActorActivatedSignal().Connect( tracker, functor );
709 // signalName does not match any signal
716 } // namespace Internal
718 } // namespace Toolkit