• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_PATTERN_BUBBLE_BUBBLE_PAINT_METHOD_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_BUBBLE_BUBBLE_PAINT_METHOD_H
18 
19 #include "base/geometry/dimension.h"
20 #include "base/geometry/ng/offset_t.h"
21 #include "base/geometry/ng/size_t.h"
22 #include "core/components/common/properties/alignment.h"
23 #include "core/components/common/properties/border.h"
24 #include "core/components/common/properties/edge.h"
25 #include "core/components/common/properties/placement.h"
26 #include "core/components/common/properties/popup_param.h"
27 #include "core/components/common/properties/shadow.h"
28 #include "core/components/common/properties/shadow_config.h"
29 #include "core/components/popup/popup_theme.h"
30 #include "core/components_ng/render/canvas_image.h"
31 #include "core/components_ng/render/drawing.h"
32 #include "core/components_ng/render/node_paint_method.h"
33 #include "core/components_ng/render/paint_wrapper.h"
34 
35 namespace OHOS::Ace::NG {
36 
37 class ACE_EXPORT BubblePaintMethod : public NodePaintMethod {
38     DECLARE_ACE_TYPE(BubblePaintMethod, NodePaintMethod);
39 public:
40     BubblePaintMethod() = default;
41     ~BubblePaintMethod() override = default;
42 
GetContentDrawFunction(PaintWrapper * paintWrapper)43     CanvasDrawFunction GetContentDrawFunction(PaintWrapper* paintWrapper) override
44     {
45         return [weak = WeakClaim(this), paintWrapper](RSCanvas& canvas) {
46             auto bubble = weak.Upgrade();
47             if (bubble) {
48                 bubble->PaintMask(canvas, paintWrapper);
49                 bubble->ClipBubble(paintWrapper);
50                 bubble->PaintBorder(canvas, paintWrapper);
51             }
52         };
53     }
54 
GetOverlayDrawFunction(PaintWrapper * paintWrapper)55     CanvasDrawFunction GetOverlayDrawFunction(PaintWrapper* paintWrapper) override
56     {
57         return [weak = WeakClaim(this), paintWrapper](RSCanvas& canvas) {
58             auto bubble = weak.Upgrade();
59             if (bubble) {
60                 bubble->PaintSingleBorder(canvas, paintWrapper);
61                 bubble->PaintInnerBorder(canvas, paintWrapper);
62                 bubble->PaintOuterBorder(canvas, paintWrapper);
63             }
64         };
65     }
66 
SetShowArrow(bool flag)67     void SetShowArrow(bool flag)
68     {
69         showArrow_ = flag;
70     }
71 
SetChildOffset(const OffsetF & offset)72     void SetChildOffset(const OffsetF& offset)
73     {
74         childOffset_ = offset;
75     }
76 
SetChildSize(const SizeF & size)77     void SetChildSize(const SizeF& size)
78     {
79         childSize_ = size;
80     }
81 
SetArrowPosition(const OffsetF & offset)82     void SetArrowPosition(const OffsetF& offset)
83     {
84         arrowPosition_ = offset;
85     }
86 
SetClipPath(const std::string & clipPath)87     void SetClipPath(const std::string& clipPath)
88     {
89         clipPath_ = clipPath;
90     }
91 
SetBorder(const Border & border)92     void SetBorder(const Border& border)
93     {
94         border_ = border;
95     }
96 
SetClipFrameNode(RefPtr<FrameNode> & clipFrameNode)97     void SetClipFrameNode(RefPtr<FrameNode>& clipFrameNode)
98     {
99         clipFrameNode_ = clipFrameNode;
100     }
101 
SetArrowOffsetsFromClip(const std::vector<std::vector<float>> & arrowOffsetsFromClip)102     void SetArrowOffsetsFromClip(const std::vector<std::vector<float>>& arrowOffsetsFromClip)
103     {
104         arrowOffsetsFromClip_ = arrowOffsetsFromClip;
105     }
106 
SetArrowWidth(const float arrowWidth)107     void SetArrowWidth(const float arrowWidth)
108     {
109         arrowWidth_ = arrowWidth;
110     }
111 
SetArrowHeight(const float arrowHeight)112     void SetArrowHeight(const float arrowHeight)
113     {
114         arrowHeight_ = arrowHeight;
115     }
116 
SetOuterBorderWidth(const Dimension outerBorderWidth)117     void SetOuterBorderWidth(const Dimension outerBorderWidth)
118     {
119         outerBorderWidth_ = outerBorderWidth.ConvertToPx();
120     }
121 
SetInnerBorderWidth(const Dimension innerBorderWidth)122     void SetInnerBorderWidth(const Dimension innerBorderWidth)
123     {
124         innerBorderWidth_ = innerBorderWidth.ConvertToPx();
125     }
126 
SetArrowBuildPlacement(const Placement arrowBuildPlacement)127     void SetArrowBuildPlacement(const Placement arrowBuildPlacement)
128     {
129         arrowBuildPlacement_ = arrowBuildPlacement;
130     }
131 
SetOutlineLinearGradient(const PopupLinearGradientProperties & outlineLinearGradient)132     void SetOutlineLinearGradient(const PopupLinearGradientProperties& outlineLinearGradient)
133     {
134         outlineLinearGradient_ = outlineLinearGradient;
135     }
136 
GetOutlineLinearGradient()137     const PopupLinearGradientProperties& GetOutlineLinearGradient() const
138     {
139         return outlineLinearGradient_;
140     }
141 
SetInnerBorderLinearGradient(const PopupLinearGradientProperties & innerBorderLinearGradient)142     void SetInnerBorderLinearGradient(const PopupLinearGradientProperties& innerBorderLinearGradient)
143     {
144         innerBorderLinearGradient_ = innerBorderLinearGradient;
145     }
146 
GetInnerBorderLinearGradient()147     const PopupLinearGradientProperties& GetInnerBorderLinearGradient() const
148     {
149         return innerBorderLinearGradient_;
150     }
151 
SetOuterBorderWidthByUser(const Dimension outerBorderWidthByUser)152     void SetOuterBorderWidthByUser(const Dimension outerBorderWidthByUser)
153     {
154         outerBorderWidthByUser_ = outerBorderWidthByUser.ConvertToPx();
155     }
156 
SetInnerBorderWidthByUser(const Dimension innerBorderWidthByUser)157     void SetInnerBorderWidthByUser(const Dimension innerBorderWidthByUser)
158     {
159         innerBorderWidthByUser_ = innerBorderWidthByUser.ConvertToPx();
160     }
161 
162     void PaintBubble(RSCanvas& canvas, PaintWrapper* paintWrapper);
163     void PaintMask(RSCanvas& canvas, PaintWrapper* paintWrapper);
164     void PaintBorder(RSCanvas& canvas, PaintWrapper* paintWrapper);
165     void ClipBubble(PaintWrapper* paintWrapper);
166     void PaintSingleBorder(RSCanvas& canvas, PaintWrapper* paintWrapper);
167     void PaintDoubleBorder(RSCanvas& canvas, PaintWrapper* paintWrapper);
168     void PaintOuterBorder(RSCanvas& canvas, PaintWrapper* paintWrapper);
169     void PaintInnerBorder(RSCanvas& canvas, PaintWrapper* paintWrapper);
170     bool IsPaintDoubleBorder(PaintWrapper* paintWrapper);
171     void DrawDashedBorder(RSCanvas& canvas, RSPen& paint);
172     std::vector<RSPoint> BorderLinearGradientPoint(int popupInnerBorderDirectionInt);
173     std::pair<std::vector<uint32_t>, std::vector<float>> BorderLinearGradientColors(
174         std::vector<PopupGradientColor> popupBorderGradientColor);
175     void PaintOuterBorderGradient(RSPen& paint);
176     void PaintInnerBorderGradient(RSPen& paint);
177 
178 private:
179     void PaintBubbleWithArrow(RSCanvas& canvas, PaintWrapper* paintWrapper);
180     void PaintDoubleBorderWithArrow(RSCanvas& canvas, PaintWrapper* paintWrapper);
181     void PaintNonCustomPopup(RSCanvas& canvas, PaintWrapper* wrapper);
182 
183     void PaintTopBubble(RSCanvas& canvas);
184     void PaintBottomBubble(RSCanvas& canvas);
185     void PaintDefaultBubble(RSCanvas& canvas);
186 
187     void UpdateArrowOffset(const std::optional<Dimension>& offset, const Placement& placement);
188 
189     RSRoundRect MakeRRect();
190     float GetArrowOffset(const Placement& placement);
191     void InitEdgeSize(Edge& edge);
192 
193     void BuildCompletePath(RSPath& path, const RefPtr<PopupTheme>& popupTheme);
194     void BuildCornerPath(RSPath& path, const Placement& placement, float radius, const RefPtr<PopupTheme>& popupTheme);
195     void BuildTopLinePath(RSPath& path, float arrowOffset, float radius, const RefPtr<PopupTheme>& popupTheme);
196     void BuildRightLinePath(RSPath& path, float arrowOffset, float radius, const RefPtr<PopupTheme>& popupTheme);
197     void BuildBottomLinePath(RSPath& path, float arrowOffset, float radius, const RefPtr<PopupTheme>& popupTheme);
198     void BuildLeftLinePath(RSPath& path, float arrowOffset, float radius, const RefPtr<PopupTheme>& popupTheme);
199     void PaintShadow(const RSPath& path, const Shadow& shadow, RSCanvas& canvas);
200     void ClipBubbleWithPath(const RefPtr<FrameNode>& frameNode);
201 
202     void BuildDoubleBorderPath(RSPath& path, const RefPtr<PopupTheme>& popupTheme);
203     void BuildTopDoubleBorderPath(RSPath& path, float radius, const RefPtr<PopupTheme>& popupTheme);
204     void BuildRightDoubleBorderPath(RSPath& path, float radius, const RefPtr<PopupTheme>& popupTheme);
205     void BuildBottomDoubleBorderPath(RSPath& path, float radius, const RefPtr<PopupTheme>& popupTheme);
206     void BuildLeftDoubleBorderPath(RSPath& path, float radius, const RefPtr<PopupTheme>& popupTheme);
207 
208     float GetInnerBorderOffset(const RefPtr<PopupTheme>& popupTheme);
209     float GetBorderOffset(const RefPtr<PopupTheme>& popupTheme);
210     float outerBorderWidth_ = Dimension(0.8_vp).ConvertToPx();
211     float innerBorderWidth_ = Dimension(0.6_vp).ConvertToPx();
212     bool needPaintOuterBorder_ = false;
213     bool isPaintBubble_ = false;
214     std::vector<std::vector<float>> arrowOffsetsFromClip_
215         = { {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f} };
216     float arrowWidth_ = Dimension(16.0_vp).ConvertToPx();
217     float arrowHeight_ = Dimension(8.0_vp).ConvertToPx();
218 
219     // Get from RenderProp
220     bool isTips_ = false;
221     bool useCustom_ = false;
222     Placement arrowPlacement_ = Placement::BOTTOM;
223     bool enableArrow_ = false;
224     Dimension arrowOffset_;
225     Color maskColor_;
226     Color backgroundColor_;
227 
228     // Get from pattern
229     OffsetF childOffset_;
230     OffsetF arrowPosition_;
231     SizeF childSize_;
232     bool showArrow_ = false;
233     std::string clipPath_;
234     RefPtr<FrameNode> clipFrameNode_;
235     Placement arrowBuildPlacement_ = Placement::BOTTOM;
236     // Get from theme
237     Border border_;
238     Edge padding_;
239     // top right bottom left
240     std::vector<float> arrowOffsetByClips_ = { 0.0f, 0.0f, 0.0f, 0.0f };
241     PopupLinearGradientProperties outlineLinearGradient_;
242     PopupLinearGradientProperties innerBorderLinearGradient_;
243     float outerBorderWidthByUser_;
244     float innerBorderWidthByUser_;
245 
246 #ifndef USE_ROSEN_DRAWING
247     RSPath path_;
248 #else
249     RSRecordingPath path_;
250 #endif
251 
252     ACE_DISALLOW_COPY_AND_MOVE(BubblePaintMethod);
253 };
254 } // namespace OHOS::Ace::NG
255 
256 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_BUBBLE_BUBBLE_PAINT_METHOD_H