Add ResourceReady for Control
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / control / control-data-impl.h
1 #ifndef DALI_TOOLKIT_CONTROL_DATA_IMPL_H
2 #define DALI_TOOLKIT_CONTROL_DATA_IMPL_H
3
4 /*
5  * Copyright (c) 2022 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-toolkit/devel-api/controls/control-devel.h>
23 #include <dali/devel-api/adaptor-framework/accessibility.h>
24 #include <dali/public-api/object/property-notification.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <string>
27
28 // INTERNAL INCLUDES
29 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
30 #include <dali-toolkit/internal/builder/dictionary.h>
31 #include <dali-toolkit/internal/builder/style.h>
32 #include <dali-toolkit/internal/controls/tooltip/tooltip.h>
33 #include <dali-toolkit/internal/visuals/visual-event-observer.h>
34 #include <dali-toolkit/public-api/controls/control-impl.h>
35 #include <dali-toolkit/public-api/visuals/visual-properties.h>
36 #include <dali/devel-api/common/owner-container.h>
37 #include <dali/integration-api/debug.h>
38 #include <map>
39 #include <memory>
40 #include <set>
41
42 namespace Dali
43 {
44 namespace Toolkit
45 {
46 namespace Internal
47 {
48 /**
49   * Struct used to store Visual within the control, index is a unique key for each visual.
50   */
51 struct RegisteredVisual
52 {
53   Property::Index       index;
54   Toolkit::Visual::Base visual;
55   bool                  enabled : 1;
56   bool                  pending : 1;
57
58   RegisteredVisual(Property::Index aIndex, Toolkit::Visual::Base& aVisual, bool aEnabled, bool aPendingReplacement)
59   : index(aIndex),
60     visual(aVisual),
61     enabled(aEnabled),
62     pending(aPendingReplacement)
63   {
64   }
65 };
66
67 typedef Dali::OwnerContainer<RegisteredVisual*> RegisteredVisualContainer;
68
69 /**
70  * @brief Holds the Implementation for the internal control class
71  */
72 class Control::Impl : public ConnectionTracker, public Visual::EventObserver
73 {
74   friend class Toolkit::DevelControl::ControlAccessible;
75
76 public:
77   /**
78    * @brief Retrieves the implementation of the internal control class.
79    * @param[in] internalControl A ref to the control whose internal implementation is required
80    * @return The internal implementation
81    */
82   static Control::Impl& Get(Internal::Control& internalControl);
83
84   /**
85    * @copydoc Get( Internal::Control& )
86    */
87   static const Control::Impl& Get(const Internal::Control& internalControl);
88
89   /**
90    * @brief Constructor.
91    * @param[in] controlImpl The control which owns this implementation
92    */
93   Impl(Control& controlImpl);
94
95   /**
96    * @brief Destructor.
97    */
98   ~Impl();
99
100   /**
101    * @brief Called when a pinch is detected.
102    * @param[in] actor The actor the pinch occurred on
103    * @param[in] pinch The pinch gesture details
104    */
105   void PinchDetected(Actor actor, const PinchGesture& pinch);
106
107   /**
108    * @brief Called when a pan is detected.
109    * @param[in] actor The actor the pan occurred on
110    * @param[in] pinch The pan gesture details
111    */
112   void PanDetected(Actor actor, const PanGesture& pan);
113
114   /**
115    * @brief Called when a tap is detected.
116    * @param[in] actor The actor the tap occurred on
117    * @param[in] pinch The tap gesture details
118    */
119   void TapDetected(Actor actor, const TapGesture& tap);
120
121   /**
122    * @brief Called when a long-press is detected.
123    * @param[in] actor The actor the long-press occurred on
124    * @param[in] pinch The long-press gesture details
125    */
126   void LongPressDetected(Actor actor, const LongPressGesture& longPress);
127
128   /**
129    * @brief Called when resources of control are ready.
130    * @param[in] relayoutRequest True to request relayout
131    */
132   void ResourceReady(bool relayoutRequest);
133
134   /**
135    * @brief Called when a resource is ready.
136    * @param[in] object The visual whose resources are ready
137    * @note Overriding method in Visual::EventObserver.
138    */
139   void ResourceReady(Visual::Base& object) override;
140
141   /**
142    * @brief Called when an event occurs.
143    * @param[in] object The visual whose events occur
144    * @param[in] signalId The signal to emit. See Visual to find supported signals
145    * @note Overriding method in Visual::EventObserver.
146    */
147   void NotifyVisualEvent(Visual::Base& object, Property::Index signalId) override;
148
149   /**
150    * @brief Called when the visual needs relayout request.
151    * @param[in] object The visual who requests relayout
152    */
153   void RelayoutRequest(Visual::Base& object) override;
154
155   /**
156    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
157    */
158   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual);
159
160   /**
161    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
162    */
163   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual, int depthIndex);
164
165   /**
166    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
167    */
168   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual, bool enabled);
169
170   /**
171    * @copydoc Dali::Toolkit::DevelControl::RegisterVisual()
172    */
173   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual, bool enabled, int depthIndex);
174
175   /**
176    * @copydoc Dali::Toolkit::DevelControl::UnregisterVisual()
177    */
178   void UnregisterVisual(Property::Index index);
179
180   /**
181    * @copydoc Dali::Toolkit::DevelControl::GetVisual()
182    */
183   Toolkit::Visual::Base GetVisual(Property::Index index) const;
184
185   /**
186    * @copydoc Dali::Toolkit::DevelControl::EnableVisual()
187    */
188   void EnableVisual(Property::Index index, bool enable);
189
190   /**
191    * @copydoc Dali::Toolkit::DevelControl::IsVisualEnabled()
192    */
193   bool IsVisualEnabled(Property::Index index) const;
194
195   /**
196    * @brief Stops observing the given visual.
197    * @param[in] visual The visual to stop observing
198    */
199   void StopObservingVisual(Toolkit::Visual::Base& visual);
200
201   /**
202    * @brief Starts observing the given visual.
203    * @param[in] visual The visual to start observing
204    */
205   void StartObservingVisual(Toolkit::Visual::Base& visual);
206
207   /**
208    * @copydoc Dali::Toolkit::DevelControl::GetVisualResourceStatus()
209    */
210   Toolkit::Visual::ResourceStatus GetVisualResourceStatus(Property::Index index) const;
211
212   /**
213    * @param[in,out] animation Handle to existing animation, or an empty handle that
214    * can be set to a New animation if createAnimation is true
215    * @param[in] transitionData The transition data describing the animation
216    * @param[in] createAnimation True if the animation should be created
217    */
218   void AddTransitions(Dali::Animation&               animation,
219                       const Toolkit::TransitionData& transitionData,
220                       bool                           createAnimation = false);
221
222   /**
223    * @copydoc Dali::Toolkit::DevelControl::CreateTransition()
224    */
225   Dali::Animation CreateTransition(const Toolkit::TransitionData& transitionData);
226
227   /**
228    * @copydoc Dali::Toolkit::DevelControl::DoAction()
229    */
230   void DoAction(Dali::Property::Index visualIndex, Dali::Property::Index actionId, const Dali::Property::Value attributes);
231
232   /**
233    * @copydoc Dali::Toolkit::DevelControl::DoActionExtension()
234    */
235   void DoActionExtension(Dali::Property::Index visualIndex, Dali::Property::Index actionId, Dali::Any attributes);
236
237   /**
238    * @brief Function used to set control properties.
239    * @param[in] object The object whose property to set
240    * @param[in] index The index of the property to set
241    * @param[in] value The value of the property to set
242    */
243   static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
244
245   /**
246    * @brief Function used to retrieve the value of control properties.
247    * @param[in] object The object whose property to get
248    * @param[in] index The index of the property to get
249    * @return The value of the property
250    */
251   static Property::Value GetProperty(BaseObject* object, Property::Index index);
252
253   /**
254    * @brief Sets the state of the control.
255    * @param[in] newState The state to set
256    * @param[in] withTransitions Whether to show a transition when changing to the new state
257    */
258   void SetState(DevelControl::State newState, bool withTransitions = true);
259
260   /**
261    * @brief Sets the sub-state of the control.
262    * @param[in] newState The sub-state to set
263    * @param[in] withTransitions Whether to show a transition when changing to the new sub-state
264    */
265   void SetSubState(const std::string& subStateName, bool withTransitions = true);
266
267   /**
268    * @brief Replaces visuals and properties from the old state to the new state.
269    * @param[in] oldState The old state
270    * @param[in] newState The new state
271    * @param[in] subState The current sub state
272    */
273   void ReplaceStateVisualsAndProperties(const StylePtr oldState, const StylePtr newState, const std::string& subState);
274
275   /**
276    * @brief Removes a visual from the control's container.
277    * @param[in] visuals The container of visuals
278    * @param[in] visualName The name of the visual to remove
279    */
280   void RemoveVisual(RegisteredVisualContainer& visuals, const std::string& visualName);
281
282   /**
283    * @brief Removes several visuals from the control's container.
284    * @param[in] visuals The container of visuals
285    * @param[in] removeVisuals The visuals to remove
286    */
287   void RemoveVisuals(RegisteredVisualContainer& visuals, DictionaryKeys& removeVisuals);
288
289   /**
290    * @brief Copies the visual properties that are specific to the control instance into the instancedProperties container.
291    * @param[in] visuals The control's visual container
292    * @param[out] instancedProperties The instanced properties are added to this container
293    */
294   void CopyInstancedProperties(RegisteredVisualContainer& visuals, Dictionary<Property::Map>& instancedProperties);
295
296   /**
297    * @brief On state change, ensures visuals are moved or created appropriately.
298    *
299    * Go through the list of visuals that are common to both states.
300    * If they are different types, or are both image types with different
301    * URLs, then the existing visual needs moving and the new visual needs creating
302    *
303    * @param[in] stateVisualsToChange The visuals to change
304    * @param[in] instancedProperties The instanced properties @see CopyInstancedProperties
305    */
306   void RecreateChangedVisuals(Dictionary<Property::Map>& stateVisualsToChange, Dictionary<Property::Map>& instancedProperties);
307
308   /**
309    * @brief Whether the resource is ready
310    * @return True if the resource is read.
311    */
312   bool IsResourceReady() const;
313
314   /**
315    * @copydoc CustomActorImpl::OnSceneDisconnection()
316    */
317   void OnSceneDisconnection();
318
319   /**
320    * @brief Sets the margin.
321    * @param[in] margin Margin is a collections of extent ( start, end, top, bottom )
322    */
323   void SetMargin(Extents margin);
324
325   /**
326    * @brief Returns the value of margin
327    * @return The value of margin
328    */
329   Extents GetMargin() const;
330
331   /**
332    * @brief Sets the padding.
333    * @param[in] padding Padding is a collections of extent ( start, end, top, bottom ).
334    */
335   void SetPadding(Extents padding);
336
337   /**
338    * @brief Returns the value of padding
339    * @return The value of padding
340    */
341   Extents GetPadding() const;
342
343   /**
344    * @brief Set the input method context.
345    * @param[in] inputMethodContext The input method context.
346    */
347   void SetInputMethodContext(InputMethodContext& inputMethodContext);
348
349   /**
350    * @brief Filter an key event.
351    * @param[in] event The key to be filtered.
352    * @return True if the key handled, otherwise false.
353    */
354   bool FilterKeyEvent(const KeyEvent& event);
355
356   /**
357    * @brief Adds accessibility attribute
358    * @param[in] key Attribute name to set
359    * @param[in] value Attribute value to set
360    *
361    * Attribute is added if not existed previously or updated
362    * if existed.
363    */
364   void AppendAccessibilityAttribute(const std::string& key, const std::string value);
365
366   /**
367    * @brief Removes accessibility attribute
368    * @param[in] key Attribute name to remove
369    *
370    * Function does nothing if attribute doesn't exist.
371    */
372   void RemoveAccessibilityAttribute(const std::string& key);
373
374   /**
375    * @brief Removes all accessibility attributes
376    */
377   void ClearAccessibilityAttributes();
378
379   /**
380    * @brief Sets reading info type attributes
381    * @param[in] types info type attributes to set
382    *
383    * This function sets, which part of object will be read out
384    * by screen-reader.
385    */
386   void SetAccessibilityReadingInfoType(const Dali::Accessibility::ReadingInfoTypes types);
387
388   /**
389    * @brief Gets currently active reading info type attributes
390    */
391   Dali::Accessibility::ReadingInfoTypes GetAccessibilityReadingInfoType() const;
392
393   /**
394    * @copydoc DevelControl::VisualEventSignal()
395    */
396   DevelControl::VisualEventSignalType& VisualEventSignal();
397
398   /**
399    * @brief Sets the shadow with a property map.
400    * @param[in] map The shadow property map
401    */
402   void SetShadow(const Property::Map& map);
403
404   /**
405    * @brief Clear the shadow.
406    */
407   void ClearShadow();
408
409   /**
410    * @copydoc DevelControl::GetVisualProperty()
411    */
412   Dali::Property GetVisualProperty(Dali::Property::Index index, Dali::Property::Key visualPropertyKey);
413
414   /**
415    * @brief Retrieves source and destination visual properties for the Transition of this Control.
416    * The properties of this Control will be transitioned from the propeties of source Control to that of destination control.
417    * If a property value is different between source and destination Control,
418    * the property information of each Control will be included in sourceProperties and destinationProperties.
419    *
420    * @param[out] sourceProperties Source property list to be applied on this Control.
421    * @param[out] destinationProperties Destination property list to be applied on this Control.
422    * @param[in] source Source control of the animation.
423    * @param[in] destination Destination control of the animation.
424    *
425    * @note This method do not handle Actor properties.
426    * And the size and order of the sourceProperties and destinationProperties must be synchronized.
427    *
428    * This method triggers Control::OnCreateTransition().
429    */
430   void CreateTransitions(std::vector<std::pair<Dali::Property::Index, Dali::Property::Map>>& sourceProperties,
431                          std::vector<std::pair<Dali::Property::Index, Dali::Property::Map>>& destinationProperties,
432                          Dali::Toolkit::Control                                              source,
433                          Dali::Toolkit::Control                                              destination);
434
435   /**
436    * @brief Update visual properties.
437    * @param[in] properties Property list to be used to update visual properties of this Control.
438    *
439    * @note This method triggers Control::OnUpdateVisualProperties().
440    */
441   void UpdateVisualProperties(const std::vector<std::pair<Dali::Property::Index, Dali::Property::Map>>& properties);
442
443   /**
444    * @copydoc Dali::Toolkit::Internal::Control::GetAccessibleObject()
445    */
446   Toolkit::DevelControl::ControlAccessible* GetAccessibleObject();
447
448 private:
449   /**
450    * Used as an alternative to boolean so that it is obvious whether a visual is enabled/disabled.
451    */
452   struct VisualState
453   {
454     enum Type
455     {
456       DISABLED = 0, ///< Visual disabled.
457       ENABLED  = 1  ///< Visual enabled.
458     };
459   };
460
461   /**
462    * Used as an alternative to boolean so that it is obvious whether a visual's depth value has been set or not by the caller.
463    */
464   struct DepthIndexValue
465   {
466     enum Type
467     {
468       NOT_SET = 0, ///< Visual depth value not set by caller.
469       SET     = 1  ///< Visual depth value set by caller.
470     };
471   };
472
473   /**
474    * @brief Adds the visual to the list of registered visuals.
475    * @param[in] index The Property index of the visual, used to reference visual
476    * @param[in,out] visual The visual to register, which can be altered in this function
477    * @param[in] enabled false if derived class wants to control when visual is set on stage
478    * @param[in] depthIndexValueSet Set to true if the depthIndex has actually been set manually
479    * @param[in] depthIndex The visual's depth-index is set to this
480    *
481    * @note Registering a visual with an index that already has a registered visual will replace it. The replacement will
482    *       occur once the replacement visual is ready (loaded).
483    */
484   void RegisterVisual(Property::Index index, Toolkit::Visual::Base& visual, VisualState::Type enabled, DepthIndexValue::Type depthIndexValueSet, int depthIndex = 0);
485
486   /**
487    * @brief Emits the resource ready signal.
488    */
489   void EmitResourceReadySignal();
490
491   /**
492    * @brief Callbacks called on idle.
493    */
494   void OnIdleCallback();
495
496   /**
497    * @brief Checks highlighted object geometry if it is showing or not
498    */
499   void CheckHighlightedObjectGeometry();
500
501   /**
502    * @brief Register property notification to check highlighted object position
503    */
504   void RegisterAccessibilityPositionPropertyNotification();
505
506   /**
507    * @brief Remove property notification added by RegisterPropertyNotification
508    */
509   void UnregisterAccessibilityPositionPropertyNotification();
510
511 public:
512   Control&            mControlImpl;
513   DevelControl::State mState;
514   std::string         mSubStateName;
515   Property::Map       mAccessibilityAttributes;
516
517   int mLeftFocusableActorId;             ///< Actor ID of Left focusable control.
518   int mRightFocusableActorId;            ///< Actor ID of Right focusable control.
519   int mUpFocusableActorId;               ///< Actor ID of Up focusable control.
520   int mDownFocusableActorId;             ///< Actor ID of Down focusable control.
521   int mClockwiseFocusableActorId;        ///< Actor ID of Clockwise focusable control.
522   int mCounterClockwiseFocusableActorId; ///< Actor ID of Counter clockwise focusable control.
523
524   RegisteredVisualContainer                 mVisuals; ///< Stores visuals needed by the control, non trivial type so std::vector used.
525   std::string                               mStyleName;
526   Vector4                                   mBackgroundColor;    ///< The color of the background visual
527   Vector3*                                  mStartingPinchScale; ///< The scale when a pinch gesture starts, TODO: consider removing this
528   Extents                                   mMargin;             ///< The margin values
529   Extents                                   mPadding;            ///< The padding values
530   Toolkit::Control::KeyEventSignalType      mKeyEventSignal;
531   Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusGainedSignal;
532   Toolkit::Control::KeyInputFocusSignalType mKeyInputFocusLostSignal;
533   Toolkit::Control::ResourceReadySignalType mResourceReadySignal;
534   DevelControl::VisualEventSignalType       mVisualEventSignal;
535
536   // Accessibility
537   Toolkit::DevelControl::AccessibilityActivateSignalType         mAccessibilityActivateSignal;
538   Toolkit::DevelControl::AccessibilityReadingSkippedSignalType   mAccessibilityReadingSkippedSignal;
539   Toolkit::DevelControl::AccessibilityReadingPausedSignalType    mAccessibilityReadingPausedSignal;
540   Toolkit::DevelControl::AccessibilityReadingResumedSignalType   mAccessibilityReadingResumedSignal;
541   Toolkit::DevelControl::AccessibilityReadingCancelledSignalType mAccessibilityReadingCancelledSignal;
542   Toolkit::DevelControl::AccessibilityReadingStoppedSignalType   mAccessibilityReadingStoppedSignal;
543
544   Toolkit::DevelControl::AccessibilityGetNameSignalType        mAccessibilityGetNameSignal;
545   Toolkit::DevelControl::AccessibilityGetDescriptionSignalType mAccessibilityGetDescriptionSignal;
546   Toolkit::DevelControl::AccessibilityDoGestureSignalType      mAccessibilityDoGestureSignal;
547
548   std::string mAccessibilityName;
549   std::string mAccessibilityDescription;
550   std::string mAccessibilityTranslationDomain;
551   std::string mAutomationId;
552
553   bool mAccessibilityHighlightable = false;
554   bool mAccessibilityHidden        = false;
555
556   Dali::Accessibility::Role mAccessibilityRole = Dali::Accessibility::Role::UNKNOWN;
557
558   std::map<Dali::Accessibility::RelationType, std::set<Accessibility::Accessible*>> mAccessibilityRelations;
559   std::unique_ptr<Toolkit::DevelControl::ControlAccessible>                         mAccessibleObject;
560
561   // Gesture Detection
562   PinchGestureDetector     mPinchGestureDetector;
563   PanGestureDetector       mPanGestureDetector;
564   TapGestureDetector       mTapGestureDetector;
565   LongPressGestureDetector mLongPressGestureDetector;
566
567   // Tooltip
568   TooltipPtr mTooltip;
569
570   InputMethodContext mInputMethodContext;
571   CallbackBase*      mIdleCallback; ///< The idle callback to emit the resource ready signal.
572
573   ControlBehaviour mFlags : CONTROL_BEHAVIOUR_FLAG_COUNT; ///< Flags passed in from constructor.
574   bool             mIsKeyboardNavigationSupported : 1;    ///< Stores whether keyboard navigation is supported by the control.
575   bool             mIsKeyboardFocusGroup : 1;             ///< Stores whether the control is a focus group.
576   bool             mIsEmittingResourceReadySignal : 1;    ///< True during ResourceReady().
577   bool             mNeedToEmitResourceReady : 1;          ///< True if need to emit the resource ready signal again.
578   bool             mDispatchKeyEvents : 1;                ///< Whether the actor emits key event signals
579
580   RegisteredVisualContainer mRemoveVisuals; ///< List of visuals that are being replaced by another visual once ready
581
582   // Properties - these need to be members of Internal::Control::Impl as they access private methods/data of Internal::Control and Internal::Control::Impl.
583   static const PropertyRegistration PROPERTY_1;
584   static const PropertyRegistration PROPERTY_2;
585   static const PropertyRegistration PROPERTY_3;
586   static const PropertyRegistration PROPERTY_4;
587   static const PropertyRegistration PROPERTY_5;
588   static const PropertyRegistration PROPERTY_6;
589   static const PropertyRegistration PROPERTY_7;
590   static const PropertyRegistration PROPERTY_8;
591   static const PropertyRegistration PROPERTY_9;
592   static const PropertyRegistration PROPERTY_10;
593   static const PropertyRegistration PROPERTY_11;
594   static const PropertyRegistration PROPERTY_12;
595   static const PropertyRegistration PROPERTY_13;
596   static const PropertyRegistration PROPERTY_14;
597   static const PropertyRegistration PROPERTY_15;
598   static const PropertyRegistration PROPERTY_16;
599   static const PropertyRegistration PROPERTY_17;
600   static const PropertyRegistration PROPERTY_18;
601   static const PropertyRegistration PROPERTY_19;
602   static const PropertyRegistration PROPERTY_20;
603   static const PropertyRegistration PROPERTY_21;
604   static const PropertyRegistration PROPERTY_22;
605   static const PropertyRegistration PROPERTY_23;
606   static const PropertyRegistration PROPERTY_24;
607   static const PropertyRegistration PROPERTY_25;
608   static const PropertyRegistration PROPERTY_26;
609
610 private:
611   // Accessibility - notification for highlighted object to check if it is showing.
612   bool                                        mIsAccessibilityPositionPropertyNotificationSet{false};
613   Dali::PropertyNotification                  mAccessibilityPositionNotification;
614   Dali::Accessibility::ScreenRelativeMoveType mAccessibilityLastScreenRelativeMoveType{Accessibility::ScreenRelativeMoveType::OUTSIDE};
615 };
616
617 } // namespace Internal
618
619 } // namespace Toolkit
620
621 } // namespace Dali
622
623 #endif // DALI_TOOLKIT_CONTROL_DATA_IMPL_H