• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUBBLE_BUBBLE_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUBBLE_BUBBLE_PATTERN_H
18 
19 #include <optional>
20 
21 #include "base/geometry/ng/offset_t.h"
22 #include "base/geometry/ng/size_t.h"
23 #include "base/memory/referenced.h"
24 #include "core/components/popup/popup_theme.h"
25 #include "core/components_ng/base/frame_node.h"
26 #include "core/components_ng/event/focus_hub.h"
27 #include "core/components_ng/manager/focus/focus_view.h"
28 #include "core/components_ng/pattern/bubble/bubble_accessibility_property.h"
29 #include "core/components_ng/pattern/bubble/bubble_event_hub.h"
30 #include "core/components_ng/pattern/bubble/bubble_layout_algorithm.h"
31 #include "core/components_ng/pattern/bubble/bubble_layout_property.h"
32 #include "core/components_ng/pattern/bubble/bubble_paint_method.h"
33 #include "core/components_ng/pattern/bubble/bubble_render_property.h"
34 #include "core/components_ng/pattern/overlay/popup_base_pattern.h"
35 
36 namespace OHOS::Ace::NG {
37 
38 enum class TransitionStatus {
39     INVISIABLE,
40     ENTERING,
41     NORMAL,
42     EXITING,
43 };
44 
45 enum class DismissReason {
46     BACK_PRESSED = 0,
47     TOUCH_OUTSIDE,
48     CLOSE_BUTTON,
49 };
50 class BubblePattern : public PopupBasePattern, public FocusView {
51     DECLARE_ACE_TYPE(BubblePattern, PopupBasePattern, FocusView);
52 
53 public:
54     BubblePattern() = default;
BubblePattern(int32_t id,const std::string & tag)55     BubblePattern(int32_t id, const std::string& tag) : targetNodeId_(id), targetTag_(tag) {}
56     ~BubblePattern() override = default;
57 
IsAtomicNode()58     bool IsAtomicNode() const override
59     {
60         return false;
61     }
62 
CreateNodePaintMethod()63     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
64     {
65         auto bubbleMethod = AceType::MakeRefPtr<BubblePaintMethod>();
66         bubbleMethod->SetArrowPosition(arrowPosition_);
67         bubbleMethod->SetChildOffset(childOffset_);
68         bubbleMethod->SetChildSize(childSize_);
69         bubbleMethod->SetShowArrow(showArrow_);
70         bubbleMethod->SetClipPath(clipPath_);
71         bubbleMethod->SetClipFrameNode(clipFrameNode_);
72         bubbleMethod->SetArrowOffsetsFromClip(arrowOffsetsFromClip_);
73         bubbleMethod->SetArrowWidth(arrowWidth_);
74         bubbleMethod->SetArrowHeight(arrowHeight_);
75         bubbleMethod->SetBorder(border_);
76         return bubbleMethod;
77     }
78 
CreateLayoutProperty()79     RefPtr<LayoutProperty> CreateLayoutProperty() override
80     {
81         auto bubbleProp = AceType::MakeRefPtr<BubbleLayoutProperty>();
82         bubbleProp->UpdatePropertyChangeFlag(PROPERTY_UPDATE_MEASURE);
83         return bubbleProp;
84     }
85 
CreateLayoutAlgorithm()86     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
87     {
88         return MakeRefPtr<BubbleLayoutAlgorithm>(targetNodeId_, targetTag_, targetOffset_, targetSize_);
89     }
90 
CreatePaintProperty()91     RefPtr<PaintProperty> CreatePaintProperty() override
92     {
93         return MakeRefPtr<BubbleRenderProperty>();
94     }
95 
CreateEventHub()96     RefPtr<EventHub> CreateEventHub() override
97     {
98         return MakeRefPtr<BubbleEventHub>();
99     }
100 
CreateAccessibilityProperty()101     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
102     {
103         return MakeRefPtr<BubbleAccessibilityProperty>();
104     }
105 
GetChildOffset()106     OffsetF GetChildOffset()
107     {
108         return childOffset_;
109     }
110 
GetChildSize()111     SizeF GetChildSize()
112     {
113         return childSize_;
114     }
115 
GetFocusPattern()116     FocusPattern GetFocusPattern() const override
117     {
118         return { FocusType::SCOPE, true };
119     }
120 
GetRouteOfFirstScope()121     std::list<int32_t> GetRouteOfFirstScope() override
122     {
123         return { 0, 0, 0 };
124     }
125 
126     void OnWindowHide() override;
127     void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override;
128     void StartEnteringTransitionEffects(const RefPtr<FrameNode>& popupNode, const std::function<void()>& finish);
129     void StartExitingTransitionEffects(const RefPtr<FrameNode>& popupNode, const std::function<void()>& finish);
130     void StartEnteringAnimation(std::function<void()> finish);
131     void StartExitingAnimation(std::function<void()> finish);
132     bool IsOnShow();
133     bool IsExiting();
134     void OnColorConfigurationUpdate() override;
135     void UpdateBubbleText();
136     void UpdateText(const RefPtr<UINode>& node, const RefPtr<PopupTheme>& popupTheme);
137     void AddPipelineCallBack();
138     void UpdateAgingTextSize();
139     void DumpInfo() override;
140 
SetMessageColor(bool isSetMessageColor)141     void SetMessageColor(bool isSetMessageColor)
142     {
143         isSetMessageColor_ = isSetMessageColor;
144     }
145 
SetMessageNode(RefPtr<FrameNode> messageNode)146     void SetMessageNode(RefPtr<FrameNode> messageNode)
147     {
148         messageNode_ = messageNode;
149     }
150 
SetCustomPopupTag(bool isCustomPopup)151     void SetCustomPopupTag(bool isCustomPopup)
152     {
153         isCustomPopup_ = isCustomPopup;
154     }
155 
SetTransitionStatus(TransitionStatus transitionStatus)156     void SetTransitionStatus(TransitionStatus transitionStatus)
157     {
158         transitionStatus_ = transitionStatus;
159     }
160 
GetTransitionStatus()161     TransitionStatus GetTransitionStatus() const
162     {
163         return transitionStatus_;
164     }
165 
SetInteractiveDismiss(bool interactiveDismiss)166     void SetInteractiveDismiss(bool interactiveDismiss)
167     {
168         interactiveDismiss_ = interactiveDismiss;
169     }
170 
GetInteractiveDismiss()171     bool GetInteractiveDismiss()
172     {
173         if (interactiveDismiss_) {
174             return true;
175         }
176         return false;
177     }
178 
UpdateOnWillDismiss(const std::function<void (int32_t)> && onWillDismiss)179     void UpdateOnWillDismiss(const std::function<void(int32_t)>&& onWillDismiss)
180     {
181         onWillDismiss_ = std::move(onWillDismiss);
182     }
183 
HasOnWillDismiss()184     bool HasOnWillDismiss()
185     {
186         if (onWillDismiss_) {
187             return true;
188         }
189         return false;
190     }
191 
CallOnWillDismiss(int32_t reason)192     void CallOnWillDismiss(int32_t reason)
193     {
194         if (onWillDismiss_) {
195             onWillDismiss_(reason);
196         }
197     }
198 
SetHasTransition(bool hasTransition)199     void SetHasTransition(bool hasTransition)
200     {
201         hasTransition_ = hasTransition;
202     }
203 
SetAvoidKeyboard(bool avoidKeyboard)204     void SetAvoidKeyboard(bool avoidKeyboard)
205     {
206         avoidKeyboard_ = avoidKeyboard;
207     }
208 
GetAvoidKeyboard()209     bool GetAvoidKeyboard()
210     {
211         return avoidKeyboard_;
212     }
213 
GetHasTransition()214     bool GetHasTransition() const
215     {
216         return hasTransition_;
217     }
218 
ResetFocusState()219     void ResetFocusState()
220     {
221         FocusViewDidShow(nullptr);
222         SetIsViewRootScopeFocused(true);
223         SetIsViewHasFocused(false);
224     }
225 
GetHostWindowRect()226     Rect GetHostWindowRect() const
227     {
228         return hostWindowRect_;
229     }
230 
231 protected:
232     void OnDetachFromFrameNode(FrameNode* frameNode) override;
233 
AvoidKeyboard()234     bool AvoidKeyboard() const override
235     {
236         return false;
237     }
238 
AvoidBottom()239     bool AvoidBottom() const override
240     {
241         return false;
242     }
243 
244 private:
245     void OnModifyDone() override;
246     void OnAttachToFrameNode() override;
247     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override;
248 
249     RefPtr<FrameNode> GetButtonRowNode();
250     RefPtr<PopupTheme> GetPopupTheme();
251     void InitTouchEvent();
252     void HandleTouchEvent(const TouchEventInfo& info);
253     void HandleTouchDown(const Offset& clickPosition);
254     void RegisterButtonOnHover();
255     void RegisterButtonOnTouch();
256     void ButtonOnHover(bool isHover, const RefPtr<NG::FrameNode>& buttonNode);
257     void ButtonOnPress(const TouchEventInfo& info, const RefPtr<NG::FrameNode>& buttonNode);
258     void PopBubble();
259     void Animation(
260         RefPtr<RenderContext>& renderContext, const Color& endColor, int32_t duration, const RefPtr<Curve>& curve);
261 
262     OffsetT<Dimension> GetInvisibleOffset();
263     RefPtr<RenderContext> GetRenderContext();
264     void ResetToInvisible();
265     bool PostTask(const TaskExecutor::Task& task, const std::string& name);
266     void StartOffsetEnteringAnimation();
267     void StartAlphaEnteringAnimation(std::function<void()> finish);
268     void StartOffsetExitingAnimation();
269     void StartAlphaExitingAnimation(std::function<void()> finish);
270 
271     int32_t targetNodeId_ = -1;
272     std::string targetTag_;
273 
274     RefPtr<TouchEventImpl> touchEvent_;
275     bool mouseEventInitFlag_ = false;
276     bool touchEventInitFlag_ = false;
277     bool isHover_ = false;
278     bool interactiveDismiss_ = true;
279     std::function<void(int32_t)> onWillDismiss_;
280     OffsetF childOffset_;
281     OffsetF arrowPosition_;
282     SizeF childSize_;
283     RectF touchRegion_;
284     Rect hostWindowRect_;
285     BubbleDumpInfo dumpInfo_;
286     // top right bottom left
287     std::vector<float> arrowOffsetByClips_ = { 0.0f, 0.0f, 0.0f, 0.0f };
288     std::optional<Placement> arrowPlacement_;
289     std::vector<std::vector<float>> arrowOffsetsFromClip_
290         = { {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f} };
291     float arrowWidth_ = Dimension(16.0_vp).ConvertToPx();
292     float arrowHeight_ = Dimension(8.0_vp).ConvertToPx();
293 
294     bool showArrow_ = false;
295     ColorMode colorMode_ = ColorMode::COLOR_MODE_UNDEFINED;
296     bool isSetMessageColor_ = false;
297     Border border_;
298     bool avoidKeyboard_ = false;
299 
300     TransitionStatus transitionStatus_ = TransitionStatus::INVISIABLE;
301 
302     bool delayShow_ = false;
303     std::function<void()> finish_;
304 
305     std::optional<OffsetF> targetOffset_;
306     std::optional<SizeF> targetSize_;
307 
308     bool isCustomPopup_ = false;
309     RefPtr<FrameNode> messageNode_;
310 
311     std::string clipPath_;
312     RefPtr<FrameNode> clipFrameNode_;
313     ACE_DISALLOW_COPY_AND_MOVE(BubblePattern);
314 
315     bool hasTransition_ = false;
316     bool hasOnAreaChange_ = false;
317 };
318 } // namespace OHOS::Ace::NG
319 
320 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUBBLE_BUBBLE_PATTERN_H
321