f67ca6fd297585a58d36cabe395fa430d3a7e664
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / focus-manager / keyboard-focus-manager-impl.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "keyboard-focus-manager-impl.h"
20
21 // EXTERNAL INCLUDES
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/events/touch-data.h>
30 #include <dali/public-api/object/type-registry.h>
31 #include <dali/public-api/object/type-registry-helper.h>
32 #include <dali/public-api/images/resource-image.h>
33 #include <dali/integration-api/debug.h>
34
35 // INTERNAL INCLUDES
36 #include <dali-toolkit/public-api/controls/control.h>
37 #include <dali-toolkit/public-api/controls/control-impl.h>
38 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
39 #include <dali-toolkit/public-api/accessibility-manager/accessibility-manager.h>
40 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
41
42 namespace Dali
43 {
44
45 namespace Toolkit
46 {
47
48 namespace Internal
49 {
50
51 namespace // Unnamed namespace
52 {
53
54 #if defined(DEBUG_ENABLED)
55 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_KEYBOARD_FOCUS_MANAGER");
56 #endif
57
58 const char* const IS_FOCUS_GROUP_PROPERTY_NAME = "isKeyboardFocusGroup"; // This property will be replaced by a flag in Control.
59
60 const char* const FOCUS_BORDER_IMAGE_PATH = DALI_IMAGE_DIR "keyboard_focus.9.png";
61
62 BaseHandle Create()
63 {
64   BaseHandle handle = KeyboardFocusManager::Get();
65
66   if ( !handle )
67   {
68     SingletonService singletonService( SingletonService::Get() );
69     if ( singletonService )
70     {
71       Toolkit::KeyboardFocusManager manager = Toolkit::KeyboardFocusManager( new Internal::KeyboardFocusManager() );
72       singletonService.Register( typeid( manager ), manager );
73       handle = manager;
74     }
75   }
76
77   return handle;
78 }
79
80 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::KeyboardFocusManager, Dali::BaseHandle, Create, true )
81
82 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardPreFocusChange",           SIGNAL_PRE_FOCUS_CHANGE )
83 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusChanged",             SIGNAL_FOCUS_CHANGED )
84 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusGroupChanged",        SIGNAL_FOCUS_GROUP_CHANGED )
85 DALI_SIGNAL_REGISTRATION( Toolkit, KeyboardFocusManager, "keyboardFocusedActorEnterKey",     SIGNAL_FOCUSED_ACTOR_ENTER_KEY )
86
87 DALI_TYPE_REGISTRATION_END()
88
89 const unsigned int MAX_HISTORY_AMOUNT = 30; ///< Max length of focus history stack
90
91 } // unnamed namespace
92
93 Toolkit::KeyboardFocusManager KeyboardFocusManager::Get()
94 {
95   Toolkit::KeyboardFocusManager manager;
96
97   SingletonService singletonService( SingletonService::Get() );
98   if ( singletonService )
99   {
100     // Check whether the keyboard focus manager is already created
101     Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::KeyboardFocusManager ) );
102     if(handle)
103     {
104       // If so, downcast the handle of singleton to keyboard focus manager
105       manager = Toolkit::KeyboardFocusManager( dynamic_cast< KeyboardFocusManager* >( handle.GetObjectPtr() ) );
106     }
107   }
108
109   return manager;
110 }
111
112 KeyboardFocusManager::KeyboardFocusManager()
113 : mPreFocusChangeSignal(),
114   mFocusChangedSignal(),
115   mFocusGroupChangedSignal(),
116   mFocusedActorEnterKeySignal(),
117   mCurrentFocusActor( 0 ),
118   mFocusIndicatorActor(),
119   mFocusGroupLoopEnabled( false ),
120   mIsFocusIndicatorEnabled( false ),
121   mIsWaitingKeyboardFocusChangeCommit( false ),
122   mFocusHistory(),
123   mSlotDelegate( this )
124 {
125   // TODO: Get FocusIndicatorEnable constant from stylesheet to set mIsFocusIndicatorEnabled.
126   Toolkit::KeyInputFocusManager::Get().UnhandledKeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
127   Stage::GetCurrent().TouchSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
128 }
129
130 KeyboardFocusManager::~KeyboardFocusManager()
131 {
132 }
133
134 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
135 {
136   DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
137
138   if( actor )
139   {
140     return DoSetCurrentFocusActor( actor.GetId() );
141   }
142
143   return false;
144 }
145
146 bool KeyboardFocusManager::DoSetCurrentFocusActor( const unsigned int actorID )
147 {
148   Actor rootActor = Stage::GetCurrent().GetRootLayer();
149   Actor actor = rootActor.FindChildById( actorID );
150   bool success = false;
151
152   // Check whether the actor is in the stage and is keyboard focusable.
153   if( actor && actor.IsKeyboardFocusable() )
154   {
155     // Draw the focus indicator upon the focused actor when Focus Indicator is enabled
156     if( mIsFocusIndicatorEnabled )
157     {
158       actor.Add( GetFocusIndicatorActor() );
159     }
160     // Send notification for the change of focus actor
161     if( !mFocusChangedSignal.Empty() )
162     {
163       mFocusChangedSignal.Emit(GetCurrentFocusActor(), actor);
164     }
165
166     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
167
168     // Save the current focused actor
169     mCurrentFocusActor = actorID;
170
171     // Push Current Focused Actor to FocusHistory
172     mFocusHistory.PushBack( &actor.GetBaseObject() );
173
174     // Delete first element before add new element when Stack is full.
175     if( mFocusHistory.Count() > MAX_HISTORY_AMOUNT )
176     {
177        FocusStackIterator beginPos = mFocusHistory.Begin();
178        mFocusHistory.Erase( beginPos );
179     }
180
181     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
182     success = true;
183   }
184   else
185   {
186     DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
187   }
188
189   return success;
190 }
191
192 Actor KeyboardFocusManager::GetCurrentFocusActor()
193 {
194   Actor rootActor = Stage::GetCurrent().GetRootLayer();
195   return rootActor.FindChildById(mCurrentFocusActor);
196 }
197
198 Actor KeyboardFocusManager::GetCurrentFocusGroup()
199 {
200   return GetFocusGroup(GetCurrentFocusActor());
201 }
202
203 void KeyboardFocusManager::MoveFocusBackward()
204 {
205   // Find Pre Focused Actor when the list size is more than 1
206   if( mFocusHistory.Count() > 1 )
207   {
208     // Delete current focused actor in history
209     FocusStackIterator endPos = mFocusHistory.End();
210     endPos = mFocusHistory.Erase( --endPos );
211
212     // If pre-focused actors are not on stage, remove them in stack
213     while( !Dali::Actor::DownCast(BaseHandle(mFocusHistory[ mFocusHistory.Count() - 1 ])).OnStage() )
214     {
215       endPos = mFocusHistory.Erase( --endPos );
216     }
217
218     // Get pre focused actor
219     BaseObject* object = mFocusHistory[ mFocusHistory.Count() - 1 ];
220     BaseHandle handle( object );
221     Actor preFocusedActor = Dali::Actor::DownCast( handle );
222
223     // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
224     mFocusHistory.Erase( --endPos );
225
226     SetCurrentFocusActor( preFocusedActor );
227  }
228 }
229
230 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
231 {
232   Toolkit::Control control = Toolkit::Control::DownCast(actor);
233   return control && GetImplementation( control ).IsKeyboardNavigationSupported();
234 }
235
236 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
237 {
238   // Get the actor's parent layout control that supports two dimensional keyboard navigation
239   Actor rootActor = Stage::GetCurrent().GetRootLayer();
240   Actor parent;
241   if(actor)
242   {
243     parent = actor.GetParent();
244   }
245
246   while( parent && !IsLayoutControl(parent) && parent != rootActor )
247   {
248     parent = parent.GetParent();
249   }
250
251   return Toolkit::Control::DownCast(parent);
252 }
253
254 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
255 {
256   Actor currentFocusActor = GetCurrentFocusActor();
257
258   bool succeed = false;
259
260   // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
261   Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
262   while(parentLayoutControl && !succeed)
263   {
264     succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
265     parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
266   }
267
268   if(!succeed && !mPreFocusChangeSignal.Empty())
269   {
270     // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
271     mIsWaitingKeyboardFocusChangeCommit = true;
272     Actor nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
273     mIsWaitingKeyboardFocusChangeCommit = false;
274
275     if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
276     {
277       // Whether the next focusable actor is a layout control
278       if(IsLayoutControl(nextFocusableActor))
279       {
280         // If so, move the focus inside it.
281         Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
282         succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
283       }
284       else
285       {
286         // Otherwise, just set focus to the next focusable actor
287         succeed = SetCurrentFocusActor(nextFocusableActor);
288       }
289     }
290   }
291
292   return succeed;
293 }
294
295 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
296 {
297   // Ask the control for the next actor to focus
298   Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
299   if(nextFocusableActor)
300   {
301     if(!nextFocusableActor.IsKeyboardFocusable())
302     {
303       // If the actor is not focusable, ask the same layout control for the next actor to focus
304       return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
305     }
306     else
307     {
308       Actor currentFocusActor = GetCurrentFocusActor();
309       Actor committedFocusActor = nextFocusableActor;
310
311       // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
312       // Signal handler can check the proposed actor and return a different actor if it wishes.
313       if( !mPreFocusChangeSignal.Empty() )
314       {
315         mIsWaitingKeyboardFocusChangeCommit = true;
316         committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
317         mIsWaitingKeyboardFocusChangeCommit = false;
318       }
319
320       if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
321       {
322         // Whether the commited focusable actor is a layout control
323         if(IsLayoutControl(committedFocusActor))
324         {
325           // If so, move the focus inside it.
326           Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
327           return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
328         }
329         else
330         {
331           // Otherwise, just set focus to the next focusable actor
332           if(committedFocusActor == nextFocusableActor)
333           {
334             // If the application hasn't changed our proposed actor, we informs the layout control we will
335             // move the focus to what the control returns. The control might wish to perform some actions
336             // before the focus is actually moved.
337             GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
338           }
339
340           return SetCurrentFocusActor(committedFocusActor);
341         }
342       }
343       else
344       {
345         return false;
346       }
347     }
348   }
349   else
350   {
351     // No more actor can be focused in the given direction within the same layout control.
352     return false;
353   }
354 }
355
356 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
357 {
358   bool succeed = false;
359
360   // Get the parent layout control of the current focus group
361   Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
362
363   while(parentLayoutControl && !succeed)
364   {
365     // If the current focus group has a parent layout control, we can probably automatically
366     // move the focus to the next focus group in the forward or backward direction.
367     Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
368     succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
369     parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
370   }
371
372   if(!mFocusGroupChangedSignal.Empty())
373   {
374     // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
375     mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
376   }
377
378   return succeed;
379 }
380
381 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
382 {
383   if( actor )
384   {
385     Toolkit::Control control = Toolkit::Control::DownCast( actor );
386     if( control )
387     {
388       // Notify the control that enter has been pressed on it.
389       GetImplementation( control ).KeyboardEnter();
390     }
391
392     // Send a notification for the actor.
393     if( !mFocusedActorEnterKeySignal.Empty() )
394     {
395       mFocusedActorEnterKeySignal.Emit( actor );
396     }
397   }
398 }
399
400 void KeyboardFocusManager::ClearFocus()
401 {
402   Actor actor = GetCurrentFocusActor();
403   if(actor)
404   {
405     if(mFocusIndicatorActor)
406     {
407       actor.Remove(mFocusIndicatorActor);
408     }
409
410     // Send notification for the change of focus actor
411     if( !mFocusChangedSignal.Empty() )
412     {
413       mFocusChangedSignal.Emit(actor, Actor());
414     }
415   }
416
417   mCurrentFocusActor = 0;
418   mIsFocusIndicatorEnabled = false;
419 }
420
421 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
422 {
423   mFocusGroupLoopEnabled = enabled;
424 }
425
426 bool KeyboardFocusManager::GetFocusGroupLoop() const
427 {
428   return mFocusGroupLoopEnabled;
429 }
430
431 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
432 {
433   if(actor)
434   {
435     // Create/Set focus group property.
436     actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
437   }
438 }
439
440 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
441 {
442   // Check whether the actor is a focus group
443   bool isFocusGroup = false;
444
445   if(actor)
446   {
447     Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
448     if(propertyIsFocusGroup != Property::INVALID_INDEX)
449     {
450       isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
451     }
452   }
453
454   return isFocusGroup;
455 }
456
457 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
458 {
459   // Go through the actor's hierarchy to check which focus group the actor belongs to
460   while (actor && !IsFocusGroup(actor))
461   {
462     actor = actor.GetParent();
463   }
464
465   return actor;
466 }
467
468 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
469 {
470   if(mFocusIndicatorActor != indicator)
471   {
472     Actor currentFocusActor = GetCurrentFocusActor();
473     if(currentFocusActor)
474     {
475       // The new focus indicator should be added to the current focused actor immediately
476       if(mFocusIndicatorActor)
477       {
478         currentFocusActor.Remove(mFocusIndicatorActor);
479       }
480
481       if(indicator)
482       {
483         currentFocusActor.Add(indicator);
484       }
485     }
486
487     mFocusIndicatorActor = indicator;
488   }
489 }
490
491 Actor KeyboardFocusManager::GetFocusIndicatorActor()
492 {
493   if( ! mFocusIndicatorActor )
494   {
495     // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
496     mFocusIndicatorActor = Toolkit::ImageView::New( FOCUS_BORDER_IMAGE_PATH );
497     mFocusIndicatorActor.SetParentOrigin( ParentOrigin::CENTER );
498
499     // Apply size constraint to the focus indicator
500     mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
501   }
502
503   return mFocusIndicatorActor;
504 }
505
506 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
507 {
508   AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
509   bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
510
511   Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
512
513   std::string keyName = event.keyPressedName;
514
515   bool isFocusStartableKey = false;
516
517   if(event.state == KeyEvent::Down)
518   {
519     if (keyName == "Left")
520     {
521       if(!isAccessibilityEnabled)
522       {
523         if(!mIsFocusIndicatorEnabled)
524         {
525           // Show focus indicator
526           mIsFocusIndicatorEnabled = true;
527         }
528         else
529         {
530           // Move the focus towards left
531           MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
532         }
533
534         isFocusStartableKey = true;
535       }
536       else
537       {
538         // Move the accessibility focus backward
539         accessibilityManager.MoveFocusBackward();
540       }
541     }
542     else if (keyName == "Right")
543     {
544       if(!isAccessibilityEnabled)
545       {
546         if(!mIsFocusIndicatorEnabled)
547         {
548           // Show focus indicator
549           mIsFocusIndicatorEnabled = true;
550         }
551         else
552         {
553           // Move the focus towards right
554           MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
555         }
556       }
557       else
558       {
559         // Move the accessibility focus forward
560         accessibilityManager.MoveFocusForward();
561       }
562
563       isFocusStartableKey = true;
564     }
565     else if (keyName == "Up" && !isAccessibilityEnabled)
566     {
567       if(!mIsFocusIndicatorEnabled)
568       {
569         // Show focus indicator
570         mIsFocusIndicatorEnabled = true;
571       }
572       else
573       {
574         // Move the focus towards up
575         MoveFocus(Toolkit::Control::KeyboardFocus::UP);
576       }
577
578       isFocusStartableKey = true;
579     }
580     else if (keyName == "Down" && !isAccessibilityEnabled)
581     {
582       if(!mIsFocusIndicatorEnabled)
583       {
584         // Show focus indicator
585         mIsFocusIndicatorEnabled = true;
586       }
587       else
588       {
589         // Move the focus towards down
590         MoveFocus(Toolkit::Control::KeyboardFocus::DOWN);
591       }
592
593       isFocusStartableKey = true;
594     }
595     else if (keyName == "Prior" && !isAccessibilityEnabled)
596     {
597       if(!mIsFocusIndicatorEnabled)
598       {
599         // Show focus indicator
600         mIsFocusIndicatorEnabled = true;
601       }
602       else
603       {
604         // Move the focus towards the previous page
605         MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_UP);
606       }
607
608       isFocusStartableKey = true;
609     }
610     else if (keyName == "Next" && !isAccessibilityEnabled)
611     {
612       if(!mIsFocusIndicatorEnabled)
613       {
614         // Show focus indicator
615         mIsFocusIndicatorEnabled = true;
616       }
617       else
618       {
619         // Move the focus towards the next page
620         MoveFocus(Toolkit::Control::KeyboardFocus::PAGE_DOWN);
621       }
622
623       isFocusStartableKey = true;
624     }
625     else if (keyName == "Tab" && !isAccessibilityEnabled)
626     {
627       if(!mIsFocusIndicatorEnabled)
628       {
629         // Show focus indicator
630         mIsFocusIndicatorEnabled = true;
631       }
632       else
633       {
634         // "Tab" key changes the focus group in the forward direction and
635         // "Shift-Tab" key changes it in the backward direction.
636         DoMoveFocusToNextFocusGroup(!event.IsShiftModifier());
637       }
638
639       isFocusStartableKey = true;
640     }
641     else if (keyName == "space" && !isAccessibilityEnabled)
642     {
643       if(!mIsFocusIndicatorEnabled)
644       {
645         // Show focus indicator
646         mIsFocusIndicatorEnabled = true;
647       }
648
649       isFocusStartableKey = true;
650     }
651     else if (keyName == "" && !isAccessibilityEnabled)
652     {
653       // Check the fake key event for evas-plugin case
654       if(!mIsFocusIndicatorEnabled)
655       {
656         // Show focus indicator
657         mIsFocusIndicatorEnabled = true;
658       }
659
660       isFocusStartableKey = true;
661     }
662     else if (keyName == "Backspace" && !isAccessibilityEnabled)
663     {
664       // Emit signal to go back to the previous view???
665     }
666     else if (keyName == "Escape" && !isAccessibilityEnabled)
667     {
668     }
669   }
670   else if(event.state == KeyEvent::Up)
671   {
672     if (keyName == "Return")
673     {
674       if(!mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
675       {
676         // Show focus indicator
677         mIsFocusIndicatorEnabled = true;
678       }
679       else
680       {
681         // The focused actor has enter pressed on it
682         Actor actor;
683         if( !isAccessibilityEnabled )
684         {
685           actor = GetCurrentFocusActor();
686         }
687         else
688         {
689           actor = accessibilityManager.GetCurrentFocusActor();
690         }
691
692         if( actor )
693         {
694           DoKeyboardEnter( actor );
695         }
696       }
697
698       isFocusStartableKey = true;
699     }
700   }
701
702   if(isFocusStartableKey && mIsFocusIndicatorEnabled && !isAccessibilityEnabled)
703   {
704     Actor actor = GetCurrentFocusActor();
705     if( actor )
706     {
707       // Make sure the focused actor is highlighted
708       actor.Add( GetFocusIndicatorActor() );
709     }
710     else
711     {
712       // No actor is focused but keyboard focus is activated by the key press
713       // Let's try to move the initial focus
714       MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
715     }
716   }
717 }
718
719 void KeyboardFocusManager::OnTouch(const TouchData& touch)
720 {
721   // Clear the focus when user touch the screen.
722   // We only do this on a Down event, otherwise the clear action may override a manually focused actor.
723   if( ( touch.GetPointCount() < 1 ) || ( touch.GetState( 0 ) == PointState::DOWN ) )
724   {
725     ClearFocus();
726   }
727 }
728
729 Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& KeyboardFocusManager::PreFocusChangeSignal()
730 {
731   return mPreFocusChangeSignal;
732 }
733
734 Toolkit::KeyboardFocusManager::FocusChangedSignalType& KeyboardFocusManager::FocusChangedSignal()
735 {
736   return mFocusChangedSignal;
737 }
738
739 Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& KeyboardFocusManager::FocusGroupChangedSignal()
740 {
741   return mFocusGroupChangedSignal;
742 }
743
744 Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& KeyboardFocusManager::FocusedActorEnterKeySignal()
745 {
746   return mFocusedActorEnterKeySignal;
747 }
748
749 bool KeyboardFocusManager::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
750 {
751   Dali::BaseHandle handle( object );
752
753   bool connected( true );
754   KeyboardFocusManager* manager = static_cast< KeyboardFocusManager* >( object ); // TypeRegistry guarantees that this is the correct type.
755
756   if( 0 == strcmp( signalName.c_str(), SIGNAL_PRE_FOCUS_CHANGE ) )
757   {
758     manager->PreFocusChangeSignal().Connect( tracker, functor );
759   }
760   else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_CHANGED ) )
761   {
762     manager->FocusChangedSignal().Connect( tracker, functor );
763   }
764   else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUS_GROUP_CHANGED ) )
765   {
766     manager->FocusGroupChangedSignal().Connect( tracker, functor );
767   }
768   else if( 0 == strcmp( signalName.c_str(), SIGNAL_FOCUSED_ACTOR_ENTER_KEY ) )
769   {
770     manager->FocusedActorEnterKeySignal().Connect( tracker, functor );
771   }
772   else
773   {
774     // signalName does not match any signal
775     connected = false;
776   }
777
778   return connected;
779 }
780
781 } // namespace Internal
782
783 } // namespace Toolkit
784
785 } // namespace Dali