Merge "Fix TableView::GetNextKeyboardFocusableActor function by returning a valid...
[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 : mCurrentFocusActor(0),
114   mFocusIndicatorActor(),
115   mFocusGroupLoopEnabled(false),
116   mIsKeyboardFocusEnabled(false),
117   mIsFocusIndicatorEnabled(false),
118   mIsWaitingKeyboardFocusChangeCommit(false),
119   mSlotDelegate(this)
120 {
121   OnPhysicalKeyboardStatusChanged(PhysicalKeyboard::Get());
122
123   Toolkit::KeyInputFocusManager::Get().UnhandledKeyEventSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnKeyEvent);
124   Stage::GetCurrent().TouchSignal().Connect( mSlotDelegate, &KeyboardFocusManager::OnTouch );
125   PhysicalKeyboard::Get().StatusChangedSignal().Connect(mSlotDelegate, &KeyboardFocusManager::OnPhysicalKeyboardStatusChanged);
126 }
127
128 KeyboardFocusManager::~KeyboardFocusManager()
129 {
130 }
131
132 bool KeyboardFocusManager::SetCurrentFocusActor( Actor actor )
133 {
134   DALI_ASSERT_DEBUG( !mIsWaitingKeyboardFocusChangeCommit && "Calling this function in the PreFocusChangeSignal callback?" );
135
136   if( actor )
137   {
138     return DoSetCurrentFocusActor( actor.GetId() );
139   }
140
141   return false;
142 }
143
144 bool KeyboardFocusManager::DoSetCurrentFocusActor( const unsigned int actorID )
145 {
146   Actor rootActor = Stage::GetCurrent().GetRootLayer();
147   Actor actor = rootActor.FindChildById( actorID );
148   bool success = false;
149
150   // Check whether the actor is in the stage and is keyboard focusable.
151   if( actor && actor.IsKeyboardFocusable() )
152   {
153     mIsFocusIndicatorEnabled = true;
154     // Draw the focus indicator upon the focused actor when PhysicalKeyboard is attached
155     if( mIsKeyboardFocusEnabled )
156     {
157       actor.Add( GetFocusIndicatorActor() );
158     }
159     // Send notification for the change of focus actor
160     if( !mFocusChangedSignal.Empty() )
161     {
162       mFocusChangedSignal.Emit(GetCurrentFocusActor(), actor);
163     }
164
165     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] Focus Changed\n", __FUNCTION__, __LINE__);
166
167     // Save the current focused actor
168     mCurrentFocusActor = actorID;
169
170     // Push Current Focused Actor to FocusHistory
171     mFocusHistory.PushBack( &actor.GetBaseObject() );
172
173     // Delete first element before add new element when Stack is full.
174     if( mFocusHistory.Count() > MAX_HISTORY_AMOUNT )
175     {
176        FocusStackIterator beginPos = mFocusHistory.Begin();
177        mFocusHistory.Erase( beginPos );
178     }
179
180     DALI_LOG_INFO( gLogFilter, Debug::General, "[%s:%d] SUCCEED\n", __FUNCTION__, __LINE__);
181     success = true;
182   }
183   else
184   {
185     DALI_LOG_WARNING("[%s:%d] FAILED\n", __FUNCTION__, __LINE__);
186   }
187
188   return success;
189 }
190
191 Actor KeyboardFocusManager::GetCurrentFocusActor()
192 {
193   Actor rootActor = Stage::GetCurrent().GetRootLayer();
194   return rootActor.FindChildById(mCurrentFocusActor);
195 }
196
197 Actor KeyboardFocusManager::GetCurrentFocusGroup()
198 {
199   return GetFocusGroup(GetCurrentFocusActor());
200 }
201
202 void KeyboardFocusManager::MoveFocusBackward()
203 {
204   // Find Pre Focused Actor when the list size is more than 1
205   if( mFocusHistory.Count() > 1 )
206   {
207     // Delete current focused actor in history
208     FocusStackIterator endPos = mFocusHistory.End();
209     endPos = mFocusHistory.Erase( --endPos );
210
211     // If pre-focused actors are not on stage, remove them in stack
212     while( !Dali::Actor::DownCast(BaseHandle(mFocusHistory[ mFocusHistory.Count() - 1 ])).OnStage() )
213     {
214       endPos = mFocusHistory.Erase( --endPos );
215     }
216
217     // Get pre focused actor
218     BaseObject* object = mFocusHistory[ mFocusHistory.Count() - 1 ];
219     BaseHandle handle( object );
220     Actor preFocusedActor = Dali::Actor::DownCast( handle );
221
222     // Delete pre focused actor in history because it will pushed again by SetCurrentFocusActor()
223     mFocusHistory.Erase( --endPos );
224
225     SetCurrentFocusActor( preFocusedActor );
226  }
227 }
228
229 bool KeyboardFocusManager::IsLayoutControl(Actor actor) const
230 {
231   Toolkit::Control control = Toolkit::Control::DownCast(actor);
232   return control && GetImplementation( control ).IsKeyboardNavigationSupported();
233 }
234
235 Toolkit::Control KeyboardFocusManager::GetParentLayoutControl(Actor actor) const
236 {
237   // Get the actor's parent layout control that supports two dimensional keyboard navigation
238   Actor rootActor = Stage::GetCurrent().GetRootLayer();
239   Actor parent;
240   if(actor)
241   {
242     parent = actor.GetParent();
243   }
244
245   while( parent && !IsLayoutControl(parent) && parent != rootActor )
246   {
247     parent = parent.GetParent();
248   }
249
250   return Toolkit::Control::DownCast(parent);
251 }
252
253 bool KeyboardFocusManager::MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction)
254 {
255   Actor currentFocusActor = GetCurrentFocusActor();
256
257   bool succeed = false;
258
259   // Go through the actor's hierarchy until we find a layout control that knows how to move the focus
260   Toolkit::Control parentLayoutControl = GetParentLayoutControl(currentFocusActor);
261   while(parentLayoutControl && !succeed)
262   {
263     succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, currentFocusActor, direction);
264     parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
265   }
266
267   if(!succeed && !mPreFocusChangeSignal.Empty())
268   {
269     // Don't know how to move the focus further. The application needs to tell us which actor to move the focus to
270     mIsWaitingKeyboardFocusChangeCommit = true;
271     Actor nextFocusableActor = mPreFocusChangeSignal.Emit(currentFocusActor, Actor(), direction);
272     mIsWaitingKeyboardFocusChangeCommit = false;
273
274     if ( nextFocusableActor && nextFocusableActor.IsKeyboardFocusable() )
275     {
276       // Whether the next focusable actor is a layout control
277       if(IsLayoutControl(nextFocusableActor))
278       {
279         // If so, move the focus inside it.
280         Toolkit::Control layoutControl = Toolkit::Control::DownCast(nextFocusableActor);
281         succeed = DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
282       }
283       else
284       {
285         // Otherwise, just set focus to the next focusable actor
286         succeed = SetCurrentFocusActor(nextFocusableActor);
287       }
288     }
289   }
290
291   return succeed;
292 }
293
294 bool KeyboardFocusManager::DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction)
295 {
296   // Ask the control for the next actor to focus
297   Actor nextFocusableActor = GetImplementation( control ).GetNextKeyboardFocusableActor(actor, direction, mFocusGroupLoopEnabled);
298   if(nextFocusableActor)
299   {
300     if(!nextFocusableActor.IsKeyboardFocusable())
301     {
302       // If the actor is not focusable, ask the same layout control for the next actor to focus
303       return DoMoveFocusWithinLayoutControl(control, nextFocusableActor, direction);
304     }
305     else
306     {
307       Actor currentFocusActor = GetCurrentFocusActor();
308       Actor committedFocusActor = nextFocusableActor;
309
310       // We will try to move the focus to the actor. Emit a signal to notify the proposed actor to focus
311       // Signal handler can check the proposed actor and return a different actor if it wishes.
312       if( !mPreFocusChangeSignal.Empty() )
313       {
314         mIsWaitingKeyboardFocusChangeCommit = true;
315         committedFocusActor = mPreFocusChangeSignal.Emit(currentFocusActor, nextFocusableActor, direction);
316         mIsWaitingKeyboardFocusChangeCommit = false;
317       }
318
319       if (committedFocusActor && committedFocusActor.IsKeyboardFocusable())
320       {
321         // Whether the commited focusable actor is a layout control
322         if(IsLayoutControl(committedFocusActor))
323         {
324           // If so, move the focus inside it.
325           Toolkit::Control layoutControl = Toolkit::Control::DownCast(committedFocusActor);
326           return DoMoveFocusWithinLayoutControl(layoutControl, currentFocusActor, direction);
327         }
328         else
329         {
330           // Otherwise, just set focus to the next focusable actor
331           if(committedFocusActor == nextFocusableActor)
332           {
333             // If the application hasn't changed our proposed actor, we informs the layout control we will
334             // move the focus to what the control returns. The control might wish to perform some actions
335             // before the focus is actually moved.
336             GetImplementation( control ).OnKeyboardFocusChangeCommitted( committedFocusActor );
337           }
338
339           return SetCurrentFocusActor(committedFocusActor);
340         }
341       }
342       else
343       {
344         return false;
345       }
346     }
347   }
348   else
349   {
350     // No more actor can be focused in the given direction within the same layout control.
351     return false;
352   }
353 }
354
355 bool KeyboardFocusManager::DoMoveFocusToNextFocusGroup(bool forward)
356 {
357   bool succeed = false;
358
359   // Get the parent layout control of the current focus group
360   Toolkit::Control parentLayoutControl = GetParentLayoutControl(GetCurrentFocusGroup());
361
362   while(parentLayoutControl && !succeed)
363   {
364     // If the current focus group has a parent layout control, we can probably automatically
365     // move the focus to the next focus group in the forward or backward direction.
366     Toolkit::Control::KeyboardFocus::Direction direction = forward ? Toolkit::Control::KeyboardFocus::RIGHT : Toolkit::Control::KeyboardFocus::LEFT;
367     succeed = DoMoveFocusWithinLayoutControl(parentLayoutControl, GetCurrentFocusActor(), direction);
368     parentLayoutControl = GetParentLayoutControl(parentLayoutControl);
369   }
370
371   if(!mFocusGroupChangedSignal.Empty())
372   {
373     // Emit a focus group changed signal. The applicaton can move the focus to a new focus group
374     mFocusGroupChangedSignal.Emit(GetCurrentFocusActor(), forward);
375   }
376
377   return succeed;
378 }
379
380 void KeyboardFocusManager::DoKeyboardEnter(Actor actor)
381 {
382   if( actor )
383   {
384     Toolkit::Control control = Toolkit::Control::DownCast( actor );
385     if( control )
386     {
387       // Notify the control that enter has been pressed on it.
388       GetImplementation( control ).KeyboardEnter();
389     }
390
391     // Send a notification for the actor.
392     if( !mFocusedActorEnterKeySignal.Empty() )
393     {
394       mFocusedActorEnterKeySignal.Emit( actor );
395     }
396   }
397 }
398
399 void KeyboardFocusManager::ClearFocus()
400 {
401   Actor actor = GetCurrentFocusActor();
402   if(actor)
403   {
404     if(mFocusIndicatorActor)
405     {
406       actor.Remove(mFocusIndicatorActor);
407     }
408
409     // Send notification for the change of focus actor
410     if( !mFocusChangedSignal.Empty() )
411     {
412       mFocusChangedSignal.Emit(actor, Actor());
413     }
414   }
415
416   mCurrentFocusActor = 0;
417   mIsFocusIndicatorEnabled = false;
418 }
419
420 void KeyboardFocusManager::SetFocusGroupLoop(bool enabled)
421 {
422   mFocusGroupLoopEnabled = enabled;
423 }
424
425 bool KeyboardFocusManager::GetFocusGroupLoop() const
426 {
427   return mFocusGroupLoopEnabled;
428 }
429
430 void KeyboardFocusManager::SetAsFocusGroup(Actor actor, bool isFocusGroup)
431 {
432   if(actor)
433   {
434     // Create/Set focus group property.
435     actor.RegisterProperty( IS_FOCUS_GROUP_PROPERTY_NAME, isFocusGroup, Property::READ_WRITE );
436   }
437 }
438
439 bool KeyboardFocusManager::IsFocusGroup(Actor actor) const
440 {
441   // Check whether the actor is a focus group
442   bool isFocusGroup = false;
443
444   if(actor)
445   {
446     Property::Index propertyIsFocusGroup = actor.GetPropertyIndex(IS_FOCUS_GROUP_PROPERTY_NAME);
447     if(propertyIsFocusGroup != Property::INVALID_INDEX)
448     {
449       isFocusGroup = actor.GetProperty<bool>(propertyIsFocusGroup);
450     }
451   }
452
453   return isFocusGroup;
454 }
455
456 Actor KeyboardFocusManager::GetFocusGroup(Actor actor)
457 {
458   // Go through the actor's hierarchy to check which focus group the actor belongs to
459   while (actor && !IsFocusGroup(actor))
460   {
461     actor = actor.GetParent();
462   }
463
464   return actor;
465 }
466
467 void KeyboardFocusManager::SetFocusIndicatorActor(Actor indicator)
468 {
469   if(mFocusIndicatorActor != indicator)
470   {
471     Actor currentFocusActor = GetCurrentFocusActor();
472     if(currentFocusActor)
473     {
474       // The new focus indicator should be added to the current focused actor immediately
475       if(mFocusIndicatorActor)
476       {
477         currentFocusActor.Remove(mFocusIndicatorActor);
478       }
479
480       if(indicator)
481       {
482         currentFocusActor.Add(indicator);
483       }
484     }
485
486     mFocusIndicatorActor = indicator;
487   }
488 }
489
490 Actor KeyboardFocusManager::GetFocusIndicatorActor()
491 {
492   if( ! mFocusIndicatorActor )
493   {
494     // Create the default if it hasn't been set and one that's shared by all the keyboard focusable actors
495     mFocusIndicatorActor = Toolkit::ImageView::New( FOCUS_BORDER_IMAGE_PATH );
496     mFocusIndicatorActor.SetParentOrigin( ParentOrigin::CENTER );
497
498     // Apply size constraint to the focus indicator
499     mFocusIndicatorActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
500   }
501
502   return mFocusIndicatorActor;
503 }
504
505 void KeyboardFocusManager::OnPhysicalKeyboardStatusChanged(PhysicalKeyboard keyboard)
506 {
507   mIsKeyboardFocusEnabled = keyboard.IsAttached();
508
509   if(mIsKeyboardFocusEnabled)
510   {
511     // Show indicator when keyboard focus turned on if there is focused actor.
512     Actor actor = GetCurrentFocusActor();
513     if(actor)
514     {
515       actor.Add( GetFocusIndicatorActor() );
516     }
517     mIsFocusIndicatorEnabled = true;
518   }
519   else
520   {
521     // Hide indicator when keyboard focus turned off
522     Actor actor = GetCurrentFocusActor();
523     if( actor && mFocusIndicatorActor )
524     {
525       actor.Remove( mFocusIndicatorActor );
526     }
527     mIsFocusIndicatorEnabled = false;
528   }
529 }
530
531 void KeyboardFocusManager::OnKeyEvent(const KeyEvent& event)
532 {
533   if(!mIsKeyboardFocusEnabled)
534   {
535     return;
536   }
537
538   AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
539   bool isAccessibilityEnabled = accessibilityAdaptor.IsEnabled();
540
541   Toolkit::AccessibilityManager accessibilityManager = Toolkit::AccessibilityManager::Get();
542
543   std::string keyName = event.keyPressedName;
544
545   bool isFocusStartableKey = false;
546
547   if(event.state == KeyEvent::Down)
548   {
549     if (keyName == "Left")
550     {
551       if(!isAccessibilityEnabled)
552       {
553         if(!mIsFocusIndicatorEnabled)
554         {
555           // Show focus indicator
556           mIsFocusIndicatorEnabled = true;
557         }
558         else
559         {
560           // Move the focus towards left
561           MoveFocus(Toolkit::Control::KeyboardFocus::LEFT);
562         }
563
564         isFocusStartableKey = true;
565       }
566       else
567       {
568         // Move the accessibility focus backward
569         accessibilityManager.MoveFocusBackward();
570       }
571     }
572     else if (keyName == "Right")
573     {
574       if(!isAccessibilityEnabled)
575       {
576         if(!mIsFocusIndicatorEnabled)
577         {
578           // Show focus indicator
579           mIsFocusIndicatorEnabled = true;
580         }
581         else
582         {
583           // Move the focus towards right
584           MoveFocus(Toolkit::Control::KeyboardFocus::RIGHT);
585         }
586       }
587       else
588       {
589         // Move the accessibility focus forward
590         accessibilityManager.MoveFocusForward();
591       }
592
593       isFocusStartableKey = true;
594     }
595     else if (keyName == "Up" && !isAccessibilityEnabled)
596     {
597       if(!mIsFocusIndicatorEnabled)
598       {
599         // Show focus indicator
600         mIsFocusIndicatorEnabled = true;
601       }
602       else
603       {
604         // Move the focus towards up
605         MoveFocus(Toolkit::Control::KeyboardFocus::UP);
606       }
607
608       isFocusStartableKey = true;
609     }
610     else if (keyName == "Down" && !isAccessibilityEnabled)
611     {
612       if(!mIsFocusIndicatorEnabled)
613       {
614         // Show focus indicator
615         mIsFocusIndicatorEnabled = true;
616       }
617       else
618       {
619         // Move the focus towards down
620         MoveFocus(Toolkit::Control::KeyboardFocus::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