[dali_2.3.19] 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) 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/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   const 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   const 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   struct AccessibleImpl : public DevelControl::AccessibleImpl
245   {
246     using DevelControl::AccessibleImpl::AccessibleImpl;
247
248     std::string                 GetNameRaw() override;
249     Dali::Accessibility::States CalculateStates() override;
250   };
251
252   /**
253    * Construct a new Popup.
254    */
255   Popup();
256
257   /**
258    * A reference counted object may only be deleted by calling Unreference()
259    */
260   virtual ~Popup();
261
262 private:
263   /**
264    * @brief Creates the layout of the popup, to be done just before showing for the first time.
265    * Also calls OnLayoutSetup() to allow derived classes to perform layout at this stage.
266    */
267   void LayoutPopup();
268
269   /**
270    * @brief Creates or destroys the popup tail based on the current TAIL_DISPLAYED property.
271    * Also uses the TAIL_POSITION property to position it.
272    */
273   void LayoutTail();
274
275   /**
276    * @brief Performs any relative positioning required based on the current contextual mode, if set.
277    * If contextual mode is not enabled, this method has no effect.
278    * @param[in] size The Popups current size (can be accessed from within the OnRelayout() method).
279    */
280   void LayoutContext(const Vector2& size);
281
282   /**
283    * @brief All transition-in animation setup and layout is done here.
284    * Different types of animation mode require different layouts to work,
285    * this function encapsulates anything animation-mode specific.
286    * This is called once for multiple displays/hides of the pops.
287    * It is only re-called when the layout becomes dirty.
288    */
289   void LayoutAnimation();
290
291   /**
292    * @brief Initiates a transition-in or transition-out animation based
293    * on the current animation settings.
294    * @param[in] transitionIn True to perform a transition-in, false for transition out.
295    * @param[in] instantaneous Optional - If set to true will override the duration to provide an instant animation.
296    */
297   void StartTransitionAnimation(bool transitionIn, bool instantaneous = false);
298
299   /**
300    * @brief Invoked once a display state change has completed.
301    */
302   void DisplayStateChangeComplete();
303
304   /**
305    * @brief This is called when the auto-hide timer finishes.
306    * It performs a display-state change to HIDDEN.
307    * @return True as signal is consumed.
308    */
309   bool OnAutoHideTimeReached();
310
311   /**
312    * @brief Create Dimmed Backing (covers all content behind the dialog).
313    *
314    * @return The backing control.
315    */
316   Toolkit::Control CreateBacking();
317
318   /**
319    * @brief Creates the lower area within the popup.
320    */
321   void CreateFooter();
322
323   /**
324    * @brief Sets if the popup allows touch events to pass through or not.
325    *
326    * @param[in] enabled Set to true to make the popup touch-transparent.
327    */
328   void SetTouchTransparent(bool enabled);
329
330   /**
331    * @brief Returns if the popup allows touch events to pass through or not.
332    *
333    * @return True if the popup is touch-transparent.
334    */
335   const bool IsTouchTransparent() const;
336
337   /**
338    * @brief Allows the popup entry animation to be setup from a Property::Map that could
339    * originate, for example, from a JSON file.
340    *
341    * @param[in] map A Property::Map containing a description of an animation
342    */
343   void SetEntryAnimationData(const Property::Map& map);
344
345   /**
346    * @brief Allows the popup exit 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 SetExitAnimationData(const Property::Map& map);
352
353   /**
354    * @briefs Updates the popup background's position and size.
355    */
356   void UpdateBackgroundPositionAndSize();
357
358 public: // Signals
359   /**
360    * @copydoc Dali::Toolkit::Popup::OutsideTouchedSignal()
361    */
362   Toolkit::Popup::TouchedOutsideSignalType& OutsideTouchedSignal();
363
364   /**
365    * @copydoc Dali::Toolkit::Popup::ShowingSignal()
366    */
367   Toolkit::Popup::DisplayStateChangeSignalType& ShowingSignal();
368
369   /**
370    * @copydoc Dali::Toolkit::Popup::ShownSignal()
371    */
372   Toolkit::Popup::DisplayStateChangeSignalType& ShownSignal();
373
374   /**
375    * @copydoc Dali::Toolkit::Popup::HidingSignal()
376    */
377   Toolkit::Popup::DisplayStateChangeSignalType& HidingSignal();
378
379   /**
380    * @copydoc Dali::Toolkit::Popup::HiddenSignal()
381    */
382   Toolkit::Popup::DisplayStateChangeSignalType& HiddenSignal();
383
384   /**
385    * Connects a callback function with the object's signals.
386    * @param[in] object The object providing the signal.
387    * @param[in] tracker Used to disconnect the signal.
388    * @param[in] signalName The signal to connect to.
389    * @param[in] functor A newly allocated FunctorDelegate.
390    * @return True if the signal was connected.
391    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
392    */
393   static bool DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor);
394
395 private:
396   /**
397    * Signal occurs when the State animation (transition from hide <-> show) finishes.
398    * @param[in] source The animation that just finished.
399    */
400   void OnDisplayChangeAnimationFinished(Animation& source);
401
402   /**
403    * Signal occurs when the dimmed backing for the Popup is touched.
404    * @param[in] actor The Actor Touched
405    * @param[in] touch The Touch Data.
406    * @return Whether to consume event or not.
407    */
408   bool OnBackingTouched(Actor actor, const TouchEvent& touch);
409
410   /**
411    * Signal occurs when a mouse wheel event occurs on the dimmed backing.
412    * @param[in] actor The Actor that got the wheel event.
413    * @param[in] event The Wheel Event.
414    * @return Whether to consume event or not.
415    */
416   bool OnBackingWheelEvent(Actor actor, const WheelEvent& event);
417
418   /**
419    * Signal occurs when the dialog has been touched.
420    * @param[in] actor The Actor Touched
421    * @param[in] touch The Touch Data.
422    * @return Whether to consume event or not.
423    */
424   bool OnDialogTouched(Actor actor, const TouchEvent& touch);
425
426   /**
427    * @copydoc Toolkit::Control::OnInitialize()
428    */
429   void OnInitialize() override;
430
431   /**
432    * Called whenever the popup layout is re-set up.
433    * Normally due to a change in contents.
434    * Note: This is only done when the popup is shown.
435    */
436   virtual void OnLayoutSetup()
437   {
438   }
439
440   /**
441    * Called when the popup is directly or indirectly parented to the stage.
442    */
443   void OnSceneConnection(int depth) override;
444
445   /**
446    * From Control; called after a child has been added to the owning actor.
447    * @param[in] child The child which has been added.
448    */
449   void OnChildAdd(Actor& child) override;
450
451   /**
452    * @copydoc Control::OnRelayOut()
453    */
454   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
455
456   /**
457    * @copydoc Control::OnSetResizePolicy()
458    */
459   void OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension) override;
460
461   /**
462    * @copydoc Control::GetNaturalSize()
463    */
464   Vector3 GetNaturalSize() override;
465
466   /**
467    * @copydoc Control::GetHeightForWidth()
468    */
469   float GetHeightForWidth(float width) override;
470
471   /**
472    * @copydoc Control::GetWidthForHeight()
473    */
474   float GetWidthForHeight(float height) override;
475
476   /**
477    * @copydoc Control::OnKeyEvent()
478    */
479   bool OnKeyEvent(const KeyEvent& event) override;
480
481   /**
482    * @copydoc Control::GetNextKeyboardFocusableActor()
483    */
484   Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled);
485
486 private:
487   /**
488    * Recursively add any focusable actors or layout containers to the provided vector.
489    * Include the top level actor if it is a layout container.
490    *
491    * @param[in]     parent          The actor to start from
492    * @param[in/out] focusableActors The vector to add focusable actors to
493    */
494   void AddFocusableChildren(Actor parent, std::vector<Actor>& focusableActors);
495
496   /**
497    * Recursively add any focusable actors or layout containers to the provided vector.
498    *
499    * @param[in]     parent          The actor to start from
500    * @param[in/out] focusableActors The vector to add focusable actors to
501    */
502   void AddFocusableChildrenRecursive(Actor parent, std::vector<Actor>& focusableActors);
503
504   /**
505    * Sets up the touch signals connections as required.
506    * @note This must be called after all the members have been created.
507    */
508   void SetupTouch();
509
510 private:
511   // Undefined.
512   Popup(const Popup&);
513
514   // Undefined.
515   Popup& operator=(const Popup& rhs);
516
517 private:
518   Toolkit::Popup::TouchedOutsideSignalType     mTouchedOutsideSignal;
519   Toolkit::Popup::DisplayStateChangeSignalType mShowingSignal;
520   Toolkit::Popup::DisplayStateChangeSignalType mShownSignal;
521   Toolkit::Popup::DisplayStateChangeSignalType mHidingSignal;
522   Toolkit::Popup::DisplayStateChangeSignalType mHiddenSignal;
523
524   Layer              mLayer;                ///< Popup Layer (i.e. Dim backing and PopupBg reside in this).
525   Toolkit::TableView mPopupLayout;          ///< Popup Background (i.e. dialog reside in this).
526   Toolkit::Control   mBacking;              ///< Backing actor (dim effect).
527   Actor              mPreviousFocusedActor; ///< Store the previous focused actor to restore the focus when popup hide.
528   Actor              mTailImage;            ///< Stores the tail image.
529   Actor              mPopupContainer;       ///< This actor is used to house the background image and the main popup layout.
530   Animation          mAnimation;            ///< The current animation in use used to manage display state changing.
531   bool               mAlterAddedChild;      ///< Flag used to control whether children are reparented or not.
532   bool               mLayoutDirty;          ///< Set to true whenever any property that would require a layout update is modified.
533   Timer              mAutoHideTimer;        ///< Used to perform an auto-hide of the popup if desired.
534   bool               mTouchTransparent;     ///< Allows all events to pass through the popup.
535
536   // Main Content related properties:
537   Actor mTitle;   ///< Stores the text title.
538   Actor mContent; ///< Stores the unselected content.
539   Actor mFooter;  ///< Stores the footer content (typically controls).
540
541   // Display related properties.
542   Toolkit::Popup::DisplayState   mDisplayState;       ///< The current display state of the popup.
543   bool                           mTailVisible;        ///< True if the popup tail should be visible.
544   Vector3                        mTailPosition;       ///< The position of the tail.
545   Toolkit::Popup::ContextualMode mContextualMode;     ///< Allows the popup to be layed out adjacent to its parent in different directions.
546   float                          mAnimationDuration;  ///< The duration of the transition in and out animations.
547   Toolkit::Popup::AnimationMode  mAnimationMode;      ///< The animation to use to transition in and out.
548   Dali::AnimationData            mEntryAnimationData; ///< Stores description data that can be used for generating a custom entry animation.
549   Dali::AnimationData            mExitAnimationData;  ///< Stores description data that can be used for generating a custom exit animation.
550   unsigned int                   mAutoHideDelay;      ///< If set, will auto-hide the popup after a specified amount of time.
551
552   // Style related properties:
553   bool        mBackingEnabled;       ///< True if a dimmed backing will be used.
554   Vector4     mBackingColor;         ///< The color of the backing.
555   Actor       mPopupBackgroundImage; ///< Stores the background image.
556   Rect<int>   mBackgroundBorder;     ///< Background border.
557   float       mMargin;               ///< Internal margin for popup contents.
558   std::string mTailUpImage;          ///< Image used for the tail for the up direction.
559   std::string mTailDownImage;        ///< Image used for the tail for the down direction.
560   std::string mTailLeftImage;        ///< Image used for the tail for the left direction.
561   std::string mTailRightImage;       ///< Image used for the tail for the right direction.
562 };
563
564 } // namespace Internal
565
566 // Helpers for public-api forwarding methods
567
568 inline Toolkit::Internal::Popup& GetImpl(Toolkit::Popup& publicObject)
569 {
570   DALI_ASSERT_ALWAYS(publicObject);
571
572   Dali::RefObject& handle = publicObject.GetImplementation();
573
574   return static_cast<Toolkit::Internal::Popup&>(handle);
575 }
576
577 inline const Toolkit::Internal::Popup& GetImpl(const Toolkit::Popup& publicObject)
578 {
579   DALI_ASSERT_ALWAYS(publicObject);
580
581   const Dali::RefObject& handle = publicObject.GetImplementation();
582
583   return static_cast<const Toolkit::Internal::Popup&>(handle);
584 }
585
586 } // namespace Toolkit
587
588 } // namespace Dali
589
590 #endif // DALI_TOOLKIT_INTERNAL_POPUP_H