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