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 if(mFocusIndicatorActor)
346 actor.Remove(mFocusIndicatorActor);
349 // Send notification for the change of focus actor
350 if( !mFocusChangedSignalV2.Empty() )
352 mFocusChangedSignalV2.Emit(actor, Actor());
356 mCurrentFocusActor = 0;
357 mIsFocusIndicatorEnabled = false;
360 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
362 mFocusGroupLoopEnabled = enabled;
365 bool KeyboardFocusManager::GetFocusGroupLoop() const
367 return mFocusGroupLoopEnabled;
370 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
374 // Create focus group property if not already created.
375 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
376 if(propertyIsFocusGroup == Property::INVALID_INDEX)
378 propertyIsFocusGroup = actor.RegisterProperty(IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup);
382 actor.SetProperty(propertyIsFocusGroup, isFocusGroup);
387 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
389 // Check whether the actor is a focus group
390 bool isFocusGroup = false;
394 Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
395 if(propertyIsFocusGroup != Property::INVALID_INDEX)
397 isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
404 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
406 // Go through the actor's hierarchy to check which focus group the actor belongs to
407 while (actor && !IsFocusGroup(actor))
409 actor = actor.GetParent();
415 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
417 if(mFocusIndicatorActor != indicator)
419 Actor currentFocusActor = GetCurrentFocusActor();
420 if(currentFocusActor)
422 // The new focus indicator should be added to the current focused actor immediately
423 if(mFocusIndicatorActor)
425 currentFocusActor.Remove(mFocusIndicatorActor);
430 currentFocusActor.Add(indicator);
434 mFocusIndicatorActor = indicator;
438 Actor KeyboardFocusManager::GetFocusIndicatorActor()
440 return mFocusIndicatorActor;
443 void KeyboardFocusManager::CreateDefaultFocusIndicatorActor()
445 // Create a focus indicator actor shared by all the keyboard focusable actors
446 Image borderImage = Image::New(FOCUS_BORDER_IMAGE_PATH);
448 ImageActor focusIndicator = ImageActor::New(borderImage);
449 focusIndicator.SetPositionInheritanceMode( Dali::USE_PARENT_POSITION_PLUS_LOCAL_POSITION );
450 focusIndicator.SetStyle( ImageActor::STYLE_NINE_PATCH );
451 focusIndicator.SetNinePatchBorder(FOCUS_BORDER_IMAGE_BORDER);
452 focusIndicator.SetPosition(Vector3(0.0f, 0.0f, 1.0f));
454 // Apply size constraint to the focus indicator
455 Constraint constraint = Constraint::New<Vector3>(Actor::SIZE,
456 ParentSource(Actor::SIZE),
457 EqualToConstraint());
458 focusIndicator.ApplyConstraint(constraint);
460 SetFocusIndicatorActor(focusIndicator);
463 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
465 mIsKeyboardFocusEnabled = keyboard.IsAttached();
467 if(mIsKeyboardFocusEnabled)
469 // Show indicator when keyboard focus turned on if there is focused actor.
470 Actor actor = GetCurrentFocusActor();
473 if(mFocusIndicatorActor)
475 actor.Add(mFocusIndicatorActor);
478 mIsFocusIndicatorEnabled = true;
482 // Hide indicator when keyboard focus turned off
483 Actor actor = GetCurrentFocusActor();
486 actor.Remove(mFocusIndicatorActor);
488 mIsFocusIndicatorEnabled = false;
492 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
494 if(!mIsKeyboardFocusEnabled)
499 AccessibilityManager accessibilityManager = AccessibilityManager::Get();
500 bool isAccessibilityEnabled = accessibilityManager.IsEnabled();
502 Toolkit::FocusManager accessibilityFocusManager = Toolkit::FocusManager::Get();
504 std::string keyName = event.keyPressedName;
506 bool isFocusStartableKey = false;
508 if(event.state == KeyEvent::Down)
510 if (keyName == "Left")
512 if(!isAccessibilityEnabled)
514 if(!mIsFocusIndicatorEnabled)
516 // Show focus indicator
517 mIsFocusIndicatorEnabled = true;
521 // Move the focus towards left
522 MoveFocus(Toolkit::Control::Left);
525 isFocusStartableKey = true;
529 // Move the accessibility focus backward
530 accessibilityFocusManager.MoveFocusBackward();
533 else if (keyName == "Right")
535 if(!isAccessibilityEnabled)
537 if(!mIsFocusIndicatorEnabled)
539 // Show focus indicator
540 mIsFocusIndicatorEnabled = true;
544 // Move the focus towards right
545 MoveFocus(Toolkit::Control::Right);
548 isFocusStartableKey = true;
552 // Move the accessibility focus forward
553 accessibilityFocusManager.MoveFocusForward();
556 isFocusStartableKey = true;
558 else if (keyName == "Up" && !isAccessibilityEnabled)
560 if(!mIsFocusIndicatorEnabled)
562 // Show focus indicator
563 mIsFocusIndicatorEnabled = true;
567 // Move the focus towards up
568 MoveFocus(Toolkit::Control::Up);
571 isFocusStartableKey = true;
573 else if (keyName == "Down" && !isAccessibilityEnabled)
575 if(!mIsFocusIndicatorEnabled)
577 // Show focus indicator
578 mIsFocusIndicatorEnabled = true;
582 // Move the focus towards down
583 MoveFocus(Toolkit::Control::Down);
586 isFocusStartableKey = true;
588 else if (keyName == "Tab" && !isAccessibilityEnabled)
590 if(!mIsFocusIndicatorEnabled)
592 // Show focus indicator
593 mIsFocusIndicatorEnabled = true;
597 // "Tab" key changes the focus group in the forward direction and
598 // "Shift-Tab" key changes it in the backward direction.
599 DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
602 isFocusStartableKey = true;
604 else if (keyName == "space" && !isAccessibilityEnabled)
606 if(!mIsFocusIndicatorEnabled)
608 // Show focus indicator
609 mIsFocusIndicatorEnabled = true;
612 isFocusStartableKey = true;
614 else if (keyName == "" && !isAccessibilityEnabled)
616 // Check the fake key event for evas-plugin case
617 if(!mIsFocusIndicatorEnabled)
619 // Show focus indicator
620 mIsFocusIndicatorEnabled = true;
623 isFocusStartableKey = true;
625 else if (keyName == "Backspace" && !isAccessibilityEnabled)
627 // Emit signal to go back to the previous view???
630 else if(event.state == KeyEvent::Up)
632 if (keyName == "Return")
634 if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
636 // Show focus indicator
637 mIsFocusIndicatorEnabled = true;
641 // Activate the focused actor
643 if(!isAccessibilityEnabled)
645 actor = GetCurrentFocusActor();
649 actor = accessibilityFocusManager.GetCurrentFocusActor();
658 isFocusStartableKey = true;
662 if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
664 Actor actor = GetCurrentFocusActor();
667 // No actor is focused but keyboard focus is activated by the key press
668 // Let's try to move the initial focus
669 MoveFocus(Toolkit::Control::Right);
671 else if(mFocusIndicatorActor)
673 // Make sure the focused actor is highlighted
674 actor.Add(mFocusIndicatorActor);
679 void KeyboardFocusManager::OnTouched(const TouchEvent& touchEvent)
681 // Clear the focus when user touch the screen
685 Toolkit::KeyboardFocusManager::PreFocusChangeSignalV2& KeyboardFocusManager::PreFocusChangeSignal()
687 return mPreFocusChangeSignalV2;
690 Toolkit::KeyboardFocusManager::FocusChangedSignalV2& KeyboardFocusManager::FocusChangedSignal()
692 return mFocusChangedSignalV2;
695 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalV2& KeyboardFocusManager::FocusGroupChangedSignal()
697 return mFocusGroupChangedSignalV2;
700 Toolkit::KeyboardFocusManager::FocusedActorActivatedSignalV2& KeyboardFocusManager::FocusedActorActivatedSignal()
702 return mFocusedActorActivatedSignalV2;
705 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
707 Dali::BaseHandle handle( object );
709 bool connected( true );
710 KeyboardFocusManager* manager = dynamic_cast<KeyboardFocusManager*>(object);
712 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_PRE_FOCUS_CHANGE == signalName )
714 manager->PreFocusChangeSignal().Connect( tracker, functor );
716 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUS_CHANGED == signalName )
718 manager->FocusChangedSignal().Connect( tracker, functor );
720 if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUS_GROUP_CHANGED == signalName )
722 manager->FocusGroupChangedSignal().Connect( tracker, functor );
724 else if( Dali::Toolkit::KeyboardFocusManager::SIGNAL_FOCUSED_ACTOR_ACTIVATED== signalName )
726 manager->FocusedActorActivatedSignal().Connect( tracker, functor );
730 // signalName does not match any signal
737 } // namespace Internal
739 } // namespace Toolkit