1 /*
2  * Copyright (c) 2021-2023 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 USE_ROSEN_DRAWING
17 #include "include/core/SkPath.h"
18 #endif
19 
20 #include "base/geometry/dimension.h"
21 #include "base/subwindow/subwindow_manager.h"
22 #include "core/components/bubble/bubble_component.h"
23 #include "core/components/common/properties/edge.h"
24 #include "core/components/slider/render_slider.h"
25 #include "core/gestures/raw_recognizer.h"
26 #include "core/pipeline/base/render_node.h"
27 
28 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUBBLE_RENDER_BUBBLE_H
29 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUBBLE_RENDER_BUBBLE_H
30 
31 namespace OHOS::Ace {
32 namespace {
33 
34 constexpr Dimension BEZIER_WIDTH_HALF = 16.0_vp;
35 constexpr Dimension BEZIER_HORIZON_OFFSET_FIRST = 1.3_vp;
36 constexpr Dimension BEZIER_HORIZON_OFFSET_SECOND = 3.2_vp;
37 constexpr Dimension BEZIER_HORIZON_OFFSET_THIRD = 6.6_vp;
38 constexpr Dimension BEZIER_HORIZON_OFFSET_FOURTH = 16.0_vp;
39 constexpr Dimension BEZIER_VERTICAL_OFFSET_FIRST = 0.1_vp;
40 constexpr Dimension BEZIER_VERTICAL_OFFSET_SECOND = 3.0_vp;
41 constexpr Dimension BEZIER_VERTICAL_OFFSET_THIRD = 8.0_vp;
42 
43 } // namespace
44 
45 class RenderBubble : public RenderNode {
46     DECLARE_ACE_TYPE(RenderBubble, RenderNode);
47 
48 public:
49     ~RenderBubble() override = default;
50 
51     static RefPtr<RenderNode> Create();
52     void Update(const RefPtr<Component>& component) override;
53     void PerformLayout() override;
54     bool PopBubble();
55     void FirePopEvent();
56     bool HandleMouseEvent(const MouseEvent& event) override;
57     double GetArrowOffset(const Placement& placement);
58     void OnPaintFinish() override;
59 
60 protected:
61     enum class ErrorPositionType {
62         NORMAL = 0,
63         TOP_LEFT_ERROR,
64         BOTTOM_RIGHT_ERROR,
65     };
66 
67     RenderBubble();
68 
69     void OnHiddenChanged(bool hidden) override;
70     void OnTouchTestHit(
71         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
72 
73     void HandleTouch(const Offset& clickPosition);
74     Offset GetChildPosition(const Size& childSize);
75     Offset GetPositionWithPlacement(const Size& childSize, const Offset& topPosition, const Offset& bottomPosition,
76         const Offset& topArrowPosition, const Offset& bottomArrowPosition);
77     Offset FitToScreen(const Offset& fitPosition, const Size& childSize);
78     ErrorPositionType GetErrorPositionType(const Offset& childOffset, const Size& childSize);
79     void UpdateAccessibilityInfo(Size size, Offset offset);
80     void InitAccessibilityEventListener();
81 #ifndef USE_ROSEN_DRAWING
82     void BuildCornerPath(SkPath& path, Placement placement, double radius);
83     void BuildTopLinePath(SkPath& path, double arrowOffset, double radius);
84     void BuildRightLinePath(SkPath& path, double arrowOffset, double radius);
85     void BuildBottomLinePath(SkPath& path, double arrowOffset, double radius);
86     void BuildLeftLinePath(SkPath& path, double arrowOffset, double radius);
87     void BuildCompletePath(SkPath& path);
88 #else
89     void BuildCornerPath(RSPath& path, Placement placement, double radius);
90     void BuildTopLinePath(RSPath& path, double arrowOffset, double radius);
91     void BuildRightLinePath(RSPath& path, double arrowOffset, double radius);
92     void BuildBottomLinePath(RSPath& path, double arrowOffset, double radius);
93     void BuildLeftLinePath(RSPath& path, double arrowOffset, double radius);
94     void BuildCompletePath(RSPath& path);
95 #endif
96 
97     static const Dimension BUBBLE_SPACING;
98 
99     bool isShow_ = true;
100     bool enableArrow_ = true;
101     // Is there has enough space for showing arrow.
102     bool showTopArrow_ = true;
103     bool showBottomArrow_ = true;
104     bool showCustomArrow_ = false;
105     bool useCustom_ = false;
106     bool isShowInSubWindow_ = false;
107     Edge padding_;
108     Edge margin_;
109     Border border_;
110     Size childSize_;
111     Size targetSize_;
112     Offset childOffset_;
113     Offset targetOffset_;
114     Offset arrowPosition_;
115     Dimension arrowOffset_;
116     Color maskColor_;
117     Color backgroundColor_;
118     Placement placement_ = Placement::BOTTOM;
119     Placement arrowPlacement_ = Placement::TOP;
120     ComposeId targetId_;
121     std::function<void(const std::string&)> onVisibilityChange_;
122     RefPtr<RawRecognizer> rawDetector_;
123     RefPtr<BubbleComponent> bubbleComponent_;
124     WeakPtr<StackElement> weakStack_;
125     Dimension targetSpace_;
126 
127 private:
128     void InitArrowState();
129     // Get size and position of target by targetId.
130     void InitTargetSizeAndPosition();
131     void UpdateCustomChildPosition();
132     void GenerateChildPosition(const Size& childSize);
133     void UpdateTouchRegion();
134     void InitEdgeSize(Edge& edge);
135     void InitArrowTopAndBottomPosition(Offset& topArrowPosition, Offset& bottomArrowPosition, Offset& topPosition,
136         Offset& bottomPosition, const Size& childSize);
137     void UpdateArrowOffset(const RefPtr<BubbleComponent>& bubble, const Placement& placement);
138     TouchRegion touchRegion_;
139 };
140 
141 } // namespace OHOS::Ace
142 
143 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUBBLE_RENDER_BUBBLE_H
144