[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / popup / popup-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_POPUP_H
2 #define DALI_TOOLKIT_INTERNAL_POPUP_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/devel-api/animation/animation-data.h>
23 #include <dali/public-api/actors/layer.h>
24 #include <dali/public-api/adaptor-framework/timer.h>
25 #include <dali/public-api/animation/animation.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/devel-api/controls/control-devel.h>
29 #include <dali-toolkit/devel-api/controls/popup/popup.h>
30 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
31 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
32 #include <dali-toolkit/public-api/controls/control-impl.h>
33
34 namespace Dali
35 {
36 namespace Toolkit
37 {
38 namespace Internal
39 {
40 class Popup;
41
42 typedef IntrusivePtr<Popup> PopupPtr;
43
44 /**
45  * @copydoc Toolkit::Popup
46  */
47 class Popup : public Control
48 {
49 public:
50   /**
51    * Create a new Popup.
52    * @return A public handle to the newly allocated Popup.
53    */
54   static Dali::Toolkit::Popup New();
55
56 public:
57   /**
58    * @copydoc Toolkit::Popup::SetPopupBackgroundImage
59    */
60   void SetPopupBackgroundImage(Actor image);
61
62   /**
63    * @copydoc Toolkit::Popup::GetPopupBackgroundImage
64    */
65   Actor GetPopupBackgroundImage() const;
66
67   /**
68    * @copydoc Toolkit::Popup::SetTitle( Actor titleActor )
69    */
70   void SetTitle(Actor titleActor);
71
72   /**
73    * @copydoc Toolkit::Popup::GetTitle
74    */
75   Actor GetTitle() const;
76
77   /**
78    * @copydoc Toolkit::Popup::SetContent
79    */
80   void SetContent(Actor content);
81
82   /**
83    * @copydoc Toolkit::Popup::GetContent
84    */
85   Actor GetContent() const;
86
87   /**
88    * @copydoc Toolkit::Popup::SetFooter
89    */
90   void SetFooter(Actor control);
91
92   /**
93    * @copydoc Toolkit::Popup::GetFooter
94    */
95   Actor GetFooter() const;
96
97   /**
98    * @copydoc Toolkit::Popup::SetDisplayState
99    */
100   void SetDisplayState(Toolkit::Popup::DisplayState displayState);
101
102   /**
103    * @copydoc Toolkit::Popup::GetDisplayState
104    */
105   Toolkit::Popup::DisplayState GetDisplayState() const;
106
107   /**
108    * @copydoc Toolkit::Popup::SetTailVisibility
109    */
110   void SetTailVisibility(bool visible);
111
112   /**
113    * @copydoc Toolkit::Popup::IsTailVisible
114    */
115   bool IsTailVisible() const;
116
117   /**
118    * @copydoc Toolkit::Popup::SetTailPosition
119    */
120   void SetTailPosition(Vector3 position);
121
122   /**
123    * @copydoc Toolkit::Popup::GetTailPosition
124    */
125   const Vector3& GetTailPosition() const;
126
127   /**
128    * @copydoc Toolkit::Popup::SetContextualMode
129    */
130   void SetContextualMode(Toolkit::Popup::ContextualMode mode);
131
132   /**
133    * @copydoc Toolkit::Popup::GetContextualMode
134    */
135   Toolkit::Popup::ContextualMode GetContextualMode() const;
136
137   /**
138    * @copydoc Toolkit::Popup::SetAnimationDuration
139    */
140   void SetAnimationDuration(float duration);
141
142   /**
143    * @copydoc Toolkit::Popup::GetAnimationDuration
144    */
145   float GetAnimationDuration() const;
146
147   /**
148    * @copydoc Toolkit::Popup::SetAnimationMode
149    */
150   void SetAnimationMode(Toolkit::Popup::AnimationMode animationMode);
151
152   /**
153    * @copydoc Toolkit::Popup::GetAnimationMode
154    */
155   Toolkit::Popup::AnimationMode GetAnimationMode() const;
156
157   /**
158    * @copydoc Toolkit::Popup::SetAutoHideDelay
159    */
160   void SetAutoHideDelay(int delay);
161
162   /**
163    * @copydoc Toolkit::Popup::GetAutoHideDelay
164    */
165   int GetAutoHideDelay() const;
166
167   /**
168    * @copydoc Toolkit::Popup::SetBackingEnabled
169    */
170   void SetBackingEnabled(bool enabled);
171
172   /**
173    * @copydoc Toolkit::Popup::IsBackingEnabled
174    */
175   bool IsBackingEnabled() const;
176
177   /**
178    * @copydoc Toolkit::Popup::SetBackingColor
179    */
180   void SetBackingColor(Vector4 color);
181
182   /**
183    * @copydoc Toolkit::Popup::GetBackingColor
184    */
185   const Vector4& GetBackingColor() const;
186
187   /**
188    * @copydoc Toolkit::Popup::SetTailUpImage
189    */
190   void SetTailUpImage(std::string image);
191
192   /**
193    * @copydoc Toolkit::Popup::GetTailUpImage
194    */
195   const std::string& GetTailUpImage() const;
196
197   /**
198    * @copydoc Toolkit::Popup::SetTailDownImage
199    */
200   void SetTailDownImage(std::string image);
201
202   /**
203    * @copydoc Toolkit::Popup::GetTailDownImage
204    */
205   const std::string& GetTailDownImage() const;
206
207   /**
208    * @copydoc Toolkit::Popup::SetTailLeftImage
209    */
210   void SetTailLeftImage(std::string image);
211
212   /**
213    * @copydoc Toolkit::Popup::GetTailLeftImage
214    */
215   const std::string& GetTailLeftImage() const;
216
217   /**
218    * @copydoc Toolkit::Popup::SetTailRightImage
219    */
220   void SetTailRightImage(std::string image);
221
222   /**
223    * @copydoc Toolkit::Popup::GetTailRightImage
224    */
225   const std::string& GetTailRightImage() const;
226
227   /**
228    * Called when a property of an object of this type is set.
229    * @param[in] object The object whose property is set.
230    * @param[in] propertyIndex The property index.
231    * @param[in] value The new property value.
232    */
233   static void SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value);
234
235   /**
236    * Called to retrieve a property of an object of this type.
237    * @param[in] object The object whose property is to be retrieved.
238    * @param[in] propertyIndex The property index.
239    * @return The current value of the property.
240    */
241   static Property::Value GetProperty(BaseObject* object, Property::Index propertyIndex);
242
243 protected:
244   class PopupAccessible : public DevelControl::ControlAccessible
245   {
246   public:
247     using DevelControl::ControlAccessible::ControlAccessible;
248
249     /**
250      * @copydoc Dali::Toolkit::DevelControl::ControlAccessible::GetNameRaw()
251      */
252     std::string GetNameRaw() const override;
253
254     /**
255      * @copydoc Dali::Toolkit::DevelControl::ControlAccessible::CalculateStates()
256      */
257     Dali::Accessibility::States CalculateStates() override;
258   };
259
260   /**
261    * Construct a new Popup.
262    */
263   Popup();
264
265   /**
266    * A reference counted object may only be deleted by calling Unreference()
267    */
268   virtual ~Popup();
269
270 private:
271   /**
272    * @brief Creates the layout of the popup, to be done just before showing for the first time.
273    * Also calls OnLayoutSetup() to allow derived classes to perform layout at this stage.
274    */
275   void LayoutPopup();
276
277   /**
278    * @brief Creates or destroys the popup tail based on the current TAIL_DISPLAYED property.
279    * Also uses the TAIL_POSITION property to position it.
280    */
281   void LayoutTail();
282
283   /**
284    * @brief Performs any relative positioning required based on the current contextual mode, if set.
285    * If contextual mode is not enabled, this method has no effect.
286    * @param[in] size The Popups current size (can be accessed from within the OnRelayout() method).
287    */
288   void LayoutContext(const Vector2& size);
289
290   /**
291    * @brief All transition-in animation setup and layout is done here.
292    * Different types of animation mode require different layouts to work,
293    * this function encapsulates anything animation-mode specific.
294    * This is called once for multiple displays/hides of the pops.
295    * It is only re-called when the layout becomes dirty.
296    */
297   void LayoutAnimation();
298
299   /**
300    * @brief Initiates a transition-in or transition-out animation based
301    * on the current animation settings.
302    * @param[in] transitionIn True to perform a transition-in, false for transition out.
303    * @param[in] instantaneous Optional - If set to true will override the duration to provide an instant animation.
304    */
305   void StartTransitionAnimation(bool transitionIn, bool instantaneous = false);
306
307   /**
308    * @brief Invoked once a display state change has completed.
309    */
310   void DisplayStateChangeComplete();
311
312   /**
313    * @brief This is called when the auto-hide timer finishes.
314    * It performs a display-state change to HIDDEN.
315    * @return True as signal is consumed.
316    */
317   bool OnAutoHideTimeReached();
318
319   /**
320    * @brief Create Dimmed Backing (covers all content behind the dialog).
321    *
322    * @return The backing control.
323    */
324   Toolkit::Control CreateBacking();
325
326   /**
327    * @brief Creates the lower area within the popup.
328    */
329   void CreateFooter();
330
331   /**
332    * @brief Sets if the popup allows touch events to pass through or not.
333    *
334    * @param[in] enabled Set to true to make the popup touch-transparent.
335    */
336   void SetTouchTransparent(bool enabled);
337
338   /**
339    * @brief Returns if the popup allows touch events to pass through or not.
340    *
341    * @return True if the popup is touch-transparent.
342    */
343   bool IsTouchTransparent() const;
344
345   /**
346    * @brief Allows the popup entry animation to be setup from a Property::Map that could
347    * originate, for example, from a JSON file.
348    *
349    * @param[in] map A Property::Map containing a description of an animation
350    */
351   void SetEntryAnimationData(const Property::Map& map);
352
353   /**
354    * @brief Allows the popup exit animation to be setup from a Property::Map that could
355    * originate, for example, from a JSON file.
356    *
357    * @param[in] map A Property::Map containing a description of an animation
358    */
359   void SetExitAnimationData(const Property::Map& map);
360
361   /**
362    * @briefs Updates the popup background's position and size.
363    */
364   void UpdateBackgroundPositionAndSize();
365
366 public: // Signals
367   /**
368    * @copydoc Dali::Toolkit::Popup::OutsideTouchedSignal()
369    */
370   Toolkit::Popup::TouchedOutsideSignalType& OutsideTouchedSignal();
371
372   /**
373    * @copydoc Dali::Toolkit::Popup::ShowingSignal()
374    */
375   Toolkit::Popup::DisplayStateChangeSignalType& ShowingSignal();
376
377   /**
378    * @copydoc Dali::Toolkit::Popup::ShownSignal()
379    */
380   Toolkit::Popup::DisplayStateChangeSignalType& ShownSignal();
381
382   /**
383    * @copydoc Dali::Toolkit::Popup::HidingSignal()
384    */
385   Toolkit::Popup::DisplayStateChangeSignalType& HidingSignal();
386
387   /**
388    * @copydoc Dali::Toolkit::Popup::HiddenSignal()
389    */
390   Toolkit::Popup::DisplayStateChangeSignalType& HiddenSignal();
391
392   /**
393    * Connects a callback function with the object's signals.
394    * @param[in] object The object providing the signal.
395    * @param[in] tracker Used to disconnect the signal.
396    * @param[in] signalName The signal to connect to.
397    * @param[in] functor A newly allocated FunctorDelegate.
398    * @return True if the signal was connected.
399    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
400    */
401   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
402
403 private:
404   /**
405    * Signal occurs when the State animation (transition from hide <-> show) finishes.
406    * @param[in] source The animation that just finished.
407    */
408   void OnDisplayChangeAnimationFinished(Animation& source);
409
410   /**
411    * Signal occurs when the dimmed backing for the Popup is touched.
412    * @param[in] actor The Actor Touched
413    * @param[in] touch The Touch Data.
414    * @return Whether to consume event or not.
415    */
416   bool OnBackingTouched(Actor actor, const TouchEvent& touch);
417
418   /**
419    * Signal occurs when a mouse wheel event occurs on the dimmed backing.
420    * @param[in] actor The Actor that got the wheel event.
421    * @param[in] event The Wheel Event.
422    * @return Whether to consume event or not.
423    */
424   bool OnBackingWheelEvent(Actor actor, const WheelEvent& event);
425
426   /**
427    * Signal occurs when the dialog has been touched.
428    * @param[in] actor The Actor Touched
429    * @param[in] touch The Touch Data.
430    * @return Whether to consume event or not.
431    */
432   bool OnDialogTouched(Actor actor, const TouchEvent& touch);
433
434   /**
435    * @copydoc Toolkit::Control::OnInitialize()
436    */
437   void OnInitialize() override;
438
439   /**
440    * @copydoc Toolkit::Internal::Control::CreateAccessibleObject()
441    */
442   DevelControl::ControlAccessible* CreateAccessibleObject() override;
443
444   /**
445    * Called whenever the popup layout is re-set up.
446    * Normally due to a change in contents.
447    * Note: This is only done when the popup is shown.
448    */
449   virtual void OnLayoutSetup()
450   {
451   }
452
453   /**
454    * Called when the popup is directly or indirectly parented to the stage.
455    */
456   void OnSceneConnection(int depth) override;
457
458   /**
459    * From Control; called after a child has been added to the owning actor.
460    * @param[in] child The child which has been added.
461    */
462   void OnChildAdd(Actor& child) override;
463
464   /**
465    * @copydoc Control::OnRelayOut()
466    */
467   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
468
469   /**
470    * @copydoc Control::OnSetResizePolicy()
471    */
472   void OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension) override;
473
474   /**
475    * @copydoc Control::GetNaturalSize()
476    */
477   Vector3 GetNaturalSize() override;
478
479   /**
480    * @copydoc Control::GetHeightForWidth()
481    */
482   float GetHeightForWidth(float width) override;
483
484   /**
485    * @copydoc Control::GetWidthForHeight()
486    */
487   float GetWidthForHeight(float height) override;
488
489   /**
490    * @copydoc Control::OnKeyEvent()
491    */
492   bool OnKeyEvent(const KeyEvent& event) override;
493
494   /**
495    * @copydoc Control::GetNextKeyboardFocusableActor()
496    */
497   Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled);
498
499 private:
500   /**
501    * Recursively add any focusable actors or layout containers to the provided vector.
502    * Include the top level actor if it is a layout container.
503    *
504    * @param[in]     parent          The actor to start from
505    * @param[in/out] focusableActors The vector to add focusable actors to
506    */
507   void AddFocusableChildren(Actor parent, std::vector<Actor>& focusableActors);
508
509   /**
510    * Recursively add any focusable actors or layout containers to the provided vector.
511    *
512    * @param[in]     parent          The actor to start from
513    * @param[in/out] focusableActors The vector to add focusable actors to
514    */
515   void AddFocusableChildrenRecursive(Actor parent, std::vector<Actor>& focusableActors);
516
517   /**
518    * Sets up the touch signals connections as required.
519    * @note This must be called after all the members have been created.
520    */
521   void SetupTouch();
522
523 private:
524   // Undefined.
525   Popup(const Popup&);
526
527   // Undefined.
528   Popup& operator=(const Popup& rhs);
529
530 private:
531   Toolkit::Popup::TouchedOutsideSignalType     mTouchedOutsideSignal;
532   Toolkit::Popup::DisplayStateChangeSignalType mShowingSignal;
533   Toolkit::Popup::DisplayStateChangeSignalType mShownSignal;
534   Toolkit::Popup::DisplayStateChangeSignalType mHidingSignal;
535   Toolkit::Popup::DisplayStateChangeSignalType mHiddenSignal;
536
537   Layer              mLayer;                ///< Popup Layer (i.e. Dim backing and PopupBg reside in this).
538   Toolkit::TableView mPopupLayout;          ///< Popup Background (i.e. dialog reside in this).
539   Toolkit::Control   mBacking;              ///< Backing actor (dim effect).
540   Actor              mPreviousFocusedActor; ///< Store the previous focused actor to restore the focus when popup hide.
541   Actor              mTailImage;            ///< Stores the tail image.
542   Actor              mPopupContainer;       ///< This actor is used to house the background image and the main popup layout.
543   Animation          mAnimation;            ///< The current animation in use used to manage display state changing.
544   bool               mAlterAddedChild;      ///< Flag used to control whether children are reparented or not.
545   bool               mLayoutDirty;          ///< Set to true whenever any property that would require a layout update is modified.
546   Timer              mAutoHideTimer;        ///< Used to perform an auto-hide of the popup if desired.
547   bool               mTouchTransparent;     ///< Allows all events to pass through the popup.
548
549   // Main Content related properties:
550   Actor mTitle;   ///< Stores the text title.
551   Actor mContent; ///< Stores the unselected content.
552   Actor mFooter;  ///< Stores the footer content (typically controls).
553
554   // Display related properties.
555   Toolkit::Popup::DisplayState   mDisplayState;       ///< The current display state of the popup.
556   bool                           mTailVisible;        ///< True if the popup tail should be visible.
557   Vector3                        mTailPosition;       ///< The position of the tail.
558   Toolkit::Popup::ContextualMode mContextualMode;     ///< Allows the popup to be layed out adjacent to its parent in different directions.
559   float                          mAnimationDuration;  ///< The duration of the transition in and out animations.
560   Toolkit::Popup::AnimationMode  mAnimationMode;      ///< The animation to use to transition in and out.
561   Dali::AnimationData            mEntryAnimationData; ///< Stores description data that can be used for generating a custom entry animation.
562   Dali::AnimationData            mExitAnimationData;  ///< Stores description data that can be used for generating a custom exit animation.
563   unsigned int                   mAutoHideDelay;      ///< If set, will auto-hide the popup after a specified amount of time.
564
565   // Style related properties:
566   bool        mBackingEnabled;       ///< True if a dimmed backing will be used.
567   Vector4     mBackingColor;         ///< The color of the backing.
568   Actor       mPopupBackgroundImage; ///< Stores the background image.
569   Rect<int>   mBackgroundBorder;     ///< Background border.
570   float       mMargin;               ///< Internal margin for popup contents.
571   std::string mTailUpImage;          ///< Image used for the tail for the up direction.
572   std::string mTailDownImage;        ///< Image used for the tail for the down direction.
573   std::string mTailLeftImage;        ///< Image used for the tail for the left direction.
574   std::string mTailRightImage;       ///< Image used for the tail for the right direction.
575 };
576
577 } // namespace Internal
578
579 // Helpers for public-api forwarding methods
580
581 inline Toolkit::Internal::Popup& GetImpl(Toolkit::Popup& publicObject)
582 {
583   DALI_ASSERT_ALWAYS(publicObject);
584
585   Dali::RefObject& handle = publicObject.GetImplementation();
586
587   return static_cast<Toolkit::Internal::Popup&>(handle);
588 }
589
590 inline const Toolkit::Internal::Popup& GetImpl(const Toolkit::Popup& publicObject)
591 {
592   DALI_ASSERT_ALWAYS(publicObject);
593
594   const Dali::RefObject& handle = publicObject.GetImplementation();
595
596   return static_cast<const Toolkit::Internal::Popup&>(handle);
597 }
598
599 } // namespace Toolkit
600
601 } // namespace Dali
602
603 #endif // DALI_TOOLKIT_INTERNAL_POPUP_H