• 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_BUBBLE_BUBBLE_LAYOUT_ALGORITHM_H
16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_BUBBLE_BUBBLE_LAYOUT_ALGORITHM_H
17 
18 #include <optional>
19 #include <string>
20 
21 #include "base/geometry/ng/offset_t.h"
22 #include "base/geometry/ng/size_t.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_ng/layout/layout_algorithm.h"
27 #include "core/components_ng/pattern/bubble/bubble_layout_property.h"
28 
29 namespace OHOS::Ace::NG {
30 // BubbleLayoutAlgorithm uses for Popup Node.
31 class ACE_EXPORT BubbleLayoutAlgorithm : public LayoutAlgorithm {
32     DECLARE_ACE_TYPE(BubbleLayoutAlgorithm, LayoutAlgorithm);
33 
34 public:
35     BubbleLayoutAlgorithm() = default;
36     BubbleLayoutAlgorithm(int32_t id, const std::string& tag, const std::optional<OffsetF>& targetOffset = std::nullopt,
37         const std::optional<SizeF>& targetSize = std::nullopt)
targetNodeId_(id)38         : targetNodeId_(id), targetTag_(tag)
39     {
40         if (targetOffset.has_value()) {
41             targetOffset_ = targetOffset.value();
42         }
43         if (targetSize.has_value()) {
44             targetSize_ = targetSize.value();
45         }
46     }
47     ~BubbleLayoutAlgorithm() override = default;
48 
49     void Measure(LayoutWrapper* layoutWrapper) override;
50 
51     void Layout(LayoutWrapper* layoutWrapper) override;
52 
GetTargetSize()53     SizeF GetTargetSize() const
54     {
55         return targetSize_;
56     }
57 
GetTargetOffset()58     OffsetF GetTargetOffset() const
59     {
60         return targetOffset_;
61     }
62 
GetChildSize()63     SizeF GetChildSize() const
64     {
65         return childSize_;
66     }
67 
GetChildOffset()68     OffsetF GetChildOffset() const
69     {
70         return childOffset_;
71     }
72 
ShowArrow()73     bool ShowArrow() const
74     {
75         return showArrow_;
76     }
77 
GetArrowPosition()78     OffsetF GetArrowPosition() const
79     {
80         return arrowPosition_;
81     }
82 
GetTouchRegion()83     RectF GetTouchRegion() const
84     {
85         return touchRegion_;
86     }
87 
GetArrowPlacement()88     Placement GetArrowPlacement() const
89     {
90         return arrowPlacement_;
91     }
92 
93     OffsetT<Dimension> GetChildOffsetAfterLayout(const RefPtr<LayoutWrapper>& layoutWrapper);
94 
95 protected:
96     OffsetF positionOffset_;
97 
98 private:
99     enum class ErrorPositionType {
100         NORMAL = 0,
101         TOP_LEFT_ERROR,
102         BOTTOM_RIGHT_ERROR,
103     };
104 
105     void InitTargetSizeAndPosition(bool showInSubWindow);
106     void InitProps(const RefPtr<BubbleLayoutProperty>& layoutProp);
107     void InitArrowState(const RefPtr<BubbleLayoutProperty>& layoutProp);
108     void InitArrowTopAndBottomPosition(OffsetF& topArrowPosition, OffsetF& bottomArrowPosition, OffsetF& topPosition,
109         OffsetF& bottomPosition, const SizeF& childSize);
110     void GetPositionWithPlacement(
111         OffsetF& childPosition, OffsetF& arrowPosition, const SizeF& childSize, Placement placement);
112     OffsetF GetChildPosition(
113         const SizeF& childSize, const RefPtr<BubbleLayoutProperty>& layoutProp, bool UseArrowOffset);
114     OffsetF FitToScreen(const OffsetF& fitPosition, const SizeF& childSize);
115     ErrorPositionType GetErrorPositionType(const OffsetF& childOffset, const SizeF& childSize);
116 
117     void UpdateChildPosition(const RefPtr<BubbleLayoutProperty>& layoutProp);
118     void UpdateTouchRegion();
119 
120     int32_t targetNodeId_ = -1;
121     std::string targetTag_;
122 
123     SizeF targetSize_;
124     OffsetF targetOffset_;
125     SizeF childSize_;
126     OffsetF childOffset_;
127     OffsetF arrowPosition_;
128     SizeF selfSize_;
129     RectF touchRegion_;
130     SizeF buttonRowSize_;
131     OffsetF buttonRowOffset_;
132 
133     Edge padding_;
134     Edge margin_;
135     Border border_;
136     Placement arrowPlacement_ = Placement::BOTTOM;
137     Placement placement_ = Placement::BOTTOM;
138     Dimension targetSpace_;
139     Dimension borderRadius_;
140 
141     bool showArrow_ = false;
142     float scaledBubbleSpacing_ = 0.0f;
143     float arrowHeight_ = 0.0f;
144 
145     ACE_DISALLOW_COPY_AND_MOVE(BubbleLayoutAlgorithm);
146 };
147 } // namespace OHOS::Ace::NG
148 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_BUBBLE_BUBBLE_LAYOUT_ALGORITHM_H