[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / focus-manager / keyboard-focus-manager-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_KEYBOARD_FOCUS_MANAGER_H
2 #define DALI_TOOLKIT_INTERNAL_KEYBOARD_FOCUS_MANAGER_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/vector-wrapper.h>
23 #include <dali/public-api/object/base-object.h>
24 #include <dali/public-api/object/weak-handle.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/devel-api/focus-manager/keyboard-focus-manager-devel.h>
28 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
29 #include <dali/devel-api/adaptor-framework/window-devel.h>
30
31 namespace Dali
32 {
33 namespace Integration
34 {
35 class SceneHolder;
36
37 } // namespace Integration
38
39 namespace Toolkit
40 {
41 namespace Internal
42 {
43 /**
44  * @copydoc Toolkit::KeyboardFocusManager
45  */
46 class KeyboardFocusManager : public Dali::BaseObject, public ConnectionTracker
47 {
48 public:
49   typedef Toolkit::DevelKeyboardFocusManager::CustomAlgorithmInterface CustomAlgorithmInterface;
50
51   enum FocusIndicatorState
52   {
53     UNKNOWN = -1, ///< Unknown state
54     HIDE    = 0,  ///< FocusIndicator is hidden
55     SHOW    = 1,  ///< FocusIndicator is shown
56   };
57
58   enum EnableFocusedIndicatorState
59   {
60     DISABLE = 0, ///< FocusIndicator is disable
61     ENABLE  = 1, ///< FocusIndicator is enable
62   };
63
64   enum FocusedIndicatorModeState
65   {
66     NONE        = 0, ///< Set nothing
67     ALWAYS_SHOW = 1, ///< FocusIndicator is always shown
68   };
69
70   /**
71    * @copydoc Toolkit::KeyboardFocusManager::Get
72    */
73   static Toolkit::KeyboardFocusManager Get();
74
75   /**
76    * Construct a new KeyboardFocusManager.
77    */
78   KeyboardFocusManager();
79
80   /**
81    * @copydoc Toolkit::KeyboardFocusManager::SetCurrentFocusActor
82    */
83   bool SetCurrentFocusActor(Actor actor);
84
85   /**
86    * @copydoc Toolkit::KeyboardFocusManager::GetCurrentFocusActor
87    */
88   Actor GetCurrentFocusActor();
89
90   /**
91    * @copydoc Toolkit::KeyboardFocusManager::MoveFocus
92    */
93   bool MoveFocus(Toolkit::Control::KeyboardFocus::Direction direction, const std::string& deviceName = "");
94
95   /**
96    * @copydoc Toolkit::KeyboardFocusManager::ClearFocus
97    */
98   void ClearFocus();
99
100   /**
101    * @copydoc Toolkit::KeyboardFocusManager::SetAsFocusGroup
102    */
103   void SetAsFocusGroup(Actor actor, bool isFocusGroup);
104
105   /**
106    * @copydoc Toolkit::KeyboardFocusManager::IsFocusGroup
107    */
108   bool IsFocusGroup(Actor actor) const;
109
110   /**
111    * @copydoc Toolkit::KeyboardFocusManager::GetFocusGroup
112    */
113   Actor GetFocusGroup(Actor actor);
114
115   /**
116    * @copydoc Toolkit::KeyboardFocusManager::SetFocusGroupLoop
117    */
118   void SetFocusGroupLoop(bool enabled);
119
120   /**
121    * @copydoc Toolkit::KeyboardFocusManager::GetFocusGroupLoop
122    */
123   bool GetFocusGroupLoop() const;
124
125   /**
126    * @copydoc Toolkit::KeyboardFocusManager::SetFocusIndicatorActor
127    */
128   void SetFocusIndicatorActor(Actor indicator);
129
130   /**
131    * @copydoc Toolkit::KeyboardFocusManager::GetFocusIndicatorActor
132    */
133   Actor GetFocusIndicatorActor();
134
135   /**
136    * Move current focus to backward
137    */
138   void MoveFocusBackward();
139
140   /**
141    * @copydoc Toolkit::DevelKeyboardFocusManager::SetCustomAlgorithm
142    */
143   void SetCustomAlgorithm(CustomAlgorithmInterface& interface);
144
145   /**
146    * @copydoc Toolkit::DevelKeyboardFocusManager::UseFocusIndicator
147    */
148   void EnableFocusIndicator(bool enable);
149
150   /**
151    * @copydoc Toolkit::DevelKeyboardFocusManager::UseFocusIndicator
152    */
153   bool IsFocusIndicatorEnabled() const;
154
155   /**
156    * @copydoc Toolkit::DevelKeyboardFocusManager::EnableDefaultAlgorithm
157    */
158   void EnableDefaultAlgorithm(bool enable);
159
160   /**
161    * @copydoc Toolkit::DevelKeyboardFocusManager::IsDefaultAlgorithmEnabled
162    */
163   bool IsDefaultAlgorithmEnabled() const;
164
165   /**
166    * @copydoc Toolkit::DevelKeyboardFocusManager::SetFocusFinderRootActor
167    */
168   void SetFocusFinderRootActor(Actor actor);
169
170   /**
171    * @copydoc Toolkit::DevelKeyboardFocusManager::ResetFocusFinderRootActor
172    */
173   void ResetFocusFinderRootActor();
174
175 public:
176   /**
177    * @copydoc Toolkit::KeyboardFocusManager::PreFocusChangeSignal()
178    */
179   Toolkit::KeyboardFocusManager::PreFocusChangeSignalType& PreFocusChangeSignal();
180
181   /**
182    * @copydoc Toolkit::KeyboardFocusManager::FocusChangedSignal()
183    */
184   Toolkit::KeyboardFocusManager::FocusChangedSignalType& FocusChangedSignal();
185
186   /**
187    * @copydoc Toolkit::KeyboardFocusManager::FocusGroupChangedSignal()
188    */
189   Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType& FocusGroupChangedSignal();
190
191   /**
192    * @copydoc Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignal()
193    */
194   Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType& FocusedActorEnterKeySignal();
195
196   /**
197    * Connects a callback function with the object's signals.
198    * @param[in] object The object providing the signal.
199    * @param[in] tracker Used to disconnect the signal.
200    * @param[in] signalName The signal to connect to.
201    * @param[in] functor A newly allocated FunctorDelegate.
202    * @return True if the signal was connected.
203    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
204    */
205   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
206
207 protected:
208   /**
209    * Destructor
210    */
211   virtual ~KeyboardFocusManager();
212
213 private:
214   typedef std::vector<WeakHandle<Actor> > FocusStack;         ///< Define Dali::Vector< Dali::BaseObject* > as FocusStack to contain focus history
215   typedef FocusStack::iterator            FocusStackIterator; ///< Define FocusStack::Iterator as FocusStackIterator to navigate FocusStack
216
217   /**
218    * This will be called when the adaptor is initialized
219    */
220   void OnAdaptorInit();
221
222   /**
223    * This will be called when a new scene holder is created
224    * @param sceneHolder The new scene holder
225    */
226   void OnSceneHolderCreated(Dali::Integration::SceneHolder& sceneHolder);
227
228   /**
229    * Get configuration from StyleManager.
230    */
231   void GetConfigurationFromStyleManger();
232
233   /**
234    * Get the focus group of current focused actor.
235    * @pre The FocusManager has been initialized.
236    * @return A handle to the parent of the current focused actor which is a focus group,
237    * or an empty handle if no actor is focused.
238    */
239   Actor GetCurrentFocusGroup();
240
241   /**
242    * Move the focus to the specified actor and send notification for the focus change.
243    * @param actor The actor to be queried
244    * @return Whether the focus is successful or not
245    */
246   bool DoSetCurrentFocusActor(Actor actor);
247
248   /**
249    * Move the focus to the next actor towards the specified direction within the layout control
250    * @param control The layout control to move the focus in
251    * @param actor The current focused actor
252    * @param direction The direction of focus movement
253    * @return Whether the focus is successful or not
254    */
255   bool DoMoveFocusWithinLayoutControl(Toolkit::Control control, Actor actor, Toolkit::Control::KeyboardFocus::Direction direction);
256
257   /**
258    * Move the focus to the first focusable actor in the next focus group in the forward
259    * or backward direction. The "Tab" key changes the focus group in the forward direction
260    * and the "Shift-Tab" key changes it in the backward direction.
261    * @param forward Whether the direction of focus group change is forward or backward
262    * @return Whether the focus group change is successful or not
263    */
264   bool DoMoveFocusToNextFocusGroup(bool forward);
265
266   /**
267    * Enter has been pressed on the actor. If the actor is control, call the OnKeybaordEnter virtual function.
268    * This function will emit FocusedActorEnterKeySignal.
269    * @param actor The actor to notify
270    */
271   void DoKeyboardEnter(Actor actor);
272
273   /**
274    * Check whether the actor is a layout control that supports two dimensional keyboard navigation.
275    * The layout control needs to internally set the focus order for the child actor and be able to
276    * tell KeyboardFocusmanager the next focusable actor in the given direction.
277    * @pre The KeyboardFocusManager has been initialized.
278    * @pre The Actor has been initialized.
279    * @param actor The actor to be checked.
280    * @return Whether the actor is a layout control or not.
281    */
282   bool IsLayoutControl(Actor actor) const;
283
284   /**
285    * Returns the closest ancestor of the given actor that is a layout control.
286    * @param actor The actor to be checked for its parent layout control
287    * @return The parent layout control the given actor belongs to or an empty handle if the given actor doesn't belong to a layout control
288    */
289   Toolkit::Control GetParentLayoutControl(Actor actor) const;
290
291   /**
292    * Callback for the key event when no actor in the stage has gained the key input focus
293    * @param[in] event The KeyEvent event.
294    */
295   void OnKeyEvent(const KeyEvent& event);
296
297   /**
298    * Callback for the touch event when the screen is touched and when the touch ends
299    * (i.e. the down & up touch events only).
300    * @param[in] touch The touch information
301    */
302   void OnTouch(const TouchEvent& touch);
303
304   /**
305    * Callback for the wheel event when the custom wheel event occurs.
306    * @param[in] wheel The WheelEvent information
307    */
308   bool OnCustomWheelEvent(const WheelEvent& wheel);
309
310   /**
311    * Callback for the wheel event when the wheel event occurs.
312    * @param[in] wheel The WheelEvent information
313    */
314   void OnWheelEvent(const WheelEvent& wheel);
315
316   /**
317    * Called when the window focus is changed.
318    * @param[in] window The window whose focus is changed
319    * @param[in] focusIn Whether the focus is in/out
320    */
321   void OnWindowFocusChanged(Window window, bool focusIn);
322
323   /**
324    * Get the focus Actor from current window
325    */
326   Actor GetFocusActorFromCurrentWindow();
327
328   /**
329    * Recursively deliver events to the control and its parents, until the event is consumed or the stage is reached.
330    * @param[in]  actor  The actor got WheelEvent.
331    * @param[in]  event  The WheelEvent.
332    * @return True if WheelEvent is consumed.
333    */
334   bool EmitCustomWheelSignals(Actor actor, const WheelEvent& event);
335
336   /**
337    * Clear the focus indicator actor.
338    */
339   void ClearFocusIndicator();
340
341 private:
342   // Undefined
343   KeyboardFocusManager(const KeyboardFocusManager&);
344
345   KeyboardFocusManager& operator=(const KeyboardFocusManager& rhs);
346
347 private:
348   Toolkit::KeyboardFocusManager::PreFocusChangeSignalType       mPreFocusChangeSignal;       ///< The signal to notify the focus will be changed
349   Toolkit::KeyboardFocusManager::FocusChangedSignalType         mFocusChangedSignal;         ///< The signal to notify the focus change
350   Toolkit::KeyboardFocusManager::FocusGroupChangedSignalType    mFocusGroupChangedSignal;    ///< The signal to notify the focus group change
351   Toolkit::KeyboardFocusManager::FocusedActorEnterKeySignalType mFocusedActorEnterKeySignal; ///< The signal to notify that enter has been pressed on the focused actor
352
353   WeakHandle<Actor> mCurrentFocusActor; ///< A weak handle to the current focused actor
354
355   Actor mFocusIndicatorActor; ///< The focus indicator actor shared by all the keyboard focusable actors for highlight
356
357   WeakHandle<Actor> mFocusFinderRootActor; ///<The root actor from which the focus finder is started.
358
359   FocusStack mFocusHistory; ///< Stack to contain pre-focused actor's BaseObject*
360
361   SlotDelegate<KeyboardFocusManager> mSlotDelegate;
362
363   CustomAlgorithmInterface* mCustomAlgorithmInterface; ///< The user's (application / toolkit) implementation of CustomAlgorithmInterface
364
365   typedef std::vector<std::pair<WeakHandle<Layer>, WeakHandle<Actor> > > FocusActorContainer;
366
367   FocusActorContainer mCurrentFocusActors; ///< A container of focused actors
368
369   WeakHandle<Layer> mCurrentFocusedWindow; ///< A weak handle to the current focused window's root layer
370
371   FocusIndicatorState mIsFocusIndicatorShown; ///< Whether indicator should be shown / hidden when getting focus. It could be enabled when keyboard focus feature is enabled and navigation keys or 'Tab' key are pressed.
372
373   EnableFocusedIndicatorState mEnableFocusIndicator; ///< Whether use focus indicator
374
375   FocusedIndicatorModeState mAlwaysShowIndicator; ///< Whether always show indicator. If true, the indicator would be directly shown when focused
376
377   bool mFocusGroupLoopEnabled : 1; ///< Whether the focus movement is looped within the same focus group
378
379   bool mIsWaitingKeyboardFocusChangeCommit : 1; /// A flag to indicate PreFocusChangeSignal emitted but the proposed focus actor is not commited by the application yet.
380
381   bool mClearFocusOnTouch : 1; ///< Whether clear focus on touch.
382
383   bool mEnableDefaultAlgorithm : 1; ///< Whether use default algorithm focus
384
385 };
386
387 } // namespace Internal
388
389 inline Internal::KeyboardFocusManager& GetImpl(Dali::Toolkit::KeyboardFocusManager& obj)
390 {
391   DALI_ASSERT_ALWAYS(obj);
392
393   Dali::BaseObject& handle = obj.GetBaseObject();
394
395   return static_cast<Internal::KeyboardFocusManager&>(handle);
396 }
397
398 inline const Internal::KeyboardFocusManager& GetImpl(const Dali::Toolkit::KeyboardFocusManager& obj)
399 {
400   DALI_ASSERT_ALWAYS(obj);
401
402   const Dali::BaseObject& handle = obj.GetBaseObject();
403
404   return static_cast<const Internal::KeyboardFocusManager&>(handle);
405 }
406
407 } // namespace Toolkit
408
409 } // namespace Dali
410
411 #endif // DALI_TOOLKIT_INTERNAL_KEYBOARD_FOCUS_MANAGER_H