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 namespace OHOS::Ace::NG { 29 enum class ArrowOfTargetOffset { 30 START, 31 CENTER, 32 END, 33 NONE, 34 }; 35 // BubbleLayoutAlgorithm uses for Popup Node. 36 class ACE_EXPORT BubbleLayoutAlgorithm : public LayoutAlgorithm { 37 DECLARE_ACE_TYPE(BubbleLayoutAlgorithm, LayoutAlgorithm); 38 39 public: 40 BubbleLayoutAlgorithm() = default; 41 BubbleLayoutAlgorithm(int32_t id, const std::string& tag, const std::optional<OffsetF>& targetOffset = std::nullopt, 42 const std::optional<SizeF>& targetSize = std::nullopt); 43 ~BubbleLayoutAlgorithm() override = default; 44 45 void Measure(LayoutWrapper* layoutWrapper) override; 46 47 void Layout(LayoutWrapper* layoutWrapper) override; 48 GetTargetSize()49 SizeF GetTargetSize() const 50 { 51 return targetSize_; 52 } 53 GetTargetOffset()54 OffsetF GetTargetOffset() const 55 { 56 return targetOffsetForPaint_; 57 } 58 GetChildSize()59 SizeF GetChildSize() const 60 { 61 return childSize_; 62 } 63 GetChildOffset()64 OffsetF GetChildOffset() const 65 { 66 return childOffsetForPaint_; 67 } 68 ShowArrow()69 bool ShowArrow() const 70 { 71 return showArrow_; 72 } 73 GetArrowPosition()74 OffsetF GetArrowPosition() const 75 { 76 return arrowPositionForPaint_; 77 } 78 GetTouchRegion()79 RectF GetTouchRegion() const 80 { 81 return touchRegion_; 82 } 83 GetArrowPlacement()84 Placement GetArrowPlacement() const 85 { 86 return arrowPlacement_; 87 } GetClipPath()88 std::string GetClipPath() const 89 { 90 return clipPath_; 91 } GetClipFrameNode()92 RefPtr<FrameNode> GetClipFrameNode() 93 { 94 return clipFrameNode_; 95 } 96 GetArrowOffsetsFromClip()97 const std::vector<std::vector<float>>& GetArrowOffsetsFromClip() const 98 { 99 return arrowOffsetsFromClip_; 100 } 101 GetArrowWidth()102 const float& GetArrowWidth() const 103 { 104 return realArrowWidth_; 105 } 106 GetArrowHeight()107 const float& GetArrowHeight() const 108 { 109 return realArrowHeight_; 110 } 111 112 protected: 113 OffsetF positionOffset_; 114 SizeF wrapperSize_; 115 116 private: 117 enum class ErrorPositionType { 118 NORMAL = 0, 119 TOP_LEFT_ERROR, 120 BOTTOM_RIGHT_ERROR, 121 }; 122 bool CheckPosition(const OffsetF& position, const SizeF& childSize, size_t step, size_t& i); 123 OffsetF GetPositionWithPlacementTop(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 124 OffsetF GetPositionWithPlacementTopLeft(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 125 OffsetF GetPositionWithPlacementTopRight(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 126 OffsetF GetPositionWithPlacementBottom(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 127 OffsetF GetPositionWithPlacementBottomLeft(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 128 OffsetF GetPositionWithPlacementBottomRight(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 129 OffsetF GetPositionWithPlacementLeft(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 130 OffsetF GetPositionWithPlacementLeftTop(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 131 OffsetF GetPositionWithPlacementLeftBottom(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 132 OffsetF GetPositionWithPlacementRight(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 133 OffsetF GetPositionWithPlacementRightTop(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 134 OffsetF GetPositionWithPlacementRightBottom(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 135 OffsetF AddTargetSpace(const OffsetF& position); 136 OffsetF AddOffset(const OffsetF& position); 137 bool CheckPositionInPlacementRect(const Rect& rect, const OffsetF& position, const SizeF& childSize); 138 OffsetF AdjustPosition(const OffsetF& position, float width, float height, float space); 139 OffsetF GetAdjustPosition(std::vector<Placement>& currentPlacementStates, size_t step, const SizeF& childSize, 140 const OffsetF& topPosition, const OffsetF& bottomPosition, OffsetF& arrowPosition); 141 void InitTargetSizeAndPosition(bool showInSubWindow); 142 void InitCaretTargetSizeAndPosition(); 143 void InitProps(const RefPtr<BubbleLayoutProperty>& layoutProp, bool showInSubWindow); 144 void InitArrowState(const RefPtr<BubbleLayoutProperty>& layoutProp); 145 OffsetF GetPositionWithPlacementNew( 146 const SizeF& childSize, const OffsetF& topPosition, const OffsetF& bottomPosition, OffsetF& arrowPosition); 147 OffsetF GetChildPositionNew(const SizeF& childSize, const RefPtr<BubbleLayoutProperty>& bubbleProp); 148 OffsetF FitToScreenNew( 149 const OffsetF& position, size_t step, size_t& i, const SizeF& childSize, bool didNeedArrow = false); 150 bool GetIfNeedArrow(const RefPtr<BubbleLayoutProperty>& bubbleProp, const SizeF& childSize); 151 void UpdateChildPosition(OffsetF& childOffset); 152 void UpdateTouchRegion(); 153 154 std::string MoveTo(double x, double y); 155 std::string LineTo(double x, double y); 156 std::string ArcTo(double rx, double ry, double rotation, int32_t arc_flag, double x, double y); 157 void UpdateClipOffset(const RefPtr<FrameNode>& frameNode); 158 159 std::string ClipBubbleWithPath(); 160 float GetArrowOffset(const Placement& placement); 161 void InitEdgeSize(Edge& edge); 162 float ModifyBorderRadius(float borderRadius, float halfChildHeight); 163 void GetArrowBuildPlacement(Placement& arrowBuildplacement); 164 std::string BuildTopLinePath(float arrowOffset, float radius, Placement& arrowBuildplacement); 165 std::string BuildRightLinePath(float arrowOffset, float radius, Placement& arrowBuildplacement); 166 std::string BuildBottomLinePath(float arrowOffset, float radius, Placement& arrowBuildplacement); 167 std::string BuildLeftLinePath(float arrowOffset, float radius, Placement& arrowBuildplacement); 168 std::string ReplaceArrowTopLeft(const float arrowOffset, const float childOffset); 169 std::string ReplaceArrowTopRight(const float arrowOffset, const float childOffset); 170 std::string ReplaceArrowRightTop(const float arrowOffset, const float childOffset); 171 std::string ReplaceArrowRightBottom(const float arrowOffset, const float childOffset); 172 std::string ReplaceArrowBottomLeft(const float arrowOffset, const float childOffset); 173 std::string ReplaceArrowBottomRight(const float arrowOffset, const float childOffset); 174 std::string ReplaceArrowLeftTop(const float arrowOffset, const float childOffset); 175 std::string ReplaceArrowLeftBottom(const float arrowOffset, const float childOffset); 176 std::string BuildCornerPath(const Placement& placement, float radius); 177 void UpdateArrowOffset(const std::optional<Dimension>& offset, const Placement& placement); 178 void BubbleAvoidanceRule(RefPtr<LayoutWrapper> child, RefPtr<BubbleLayoutProperty> bubbleProp, 179 RefPtr<FrameNode> bubbleNode, bool showInSubWindow); 180 void SetArrowOffsetsFromClip(const int16_t index, const float offsetX, const float offsetY); 181 182 OffsetF GetChildPosition( 183 const SizeF& childSize, const RefPtr<BubbleLayoutProperty>& layoutProp, bool UseArrowOffset); 184 void InitArrowTopAndBottomPosition(OffsetF& topArrowPosition, OffsetF& bottomArrowPosition, OffsetF& topPosition, 185 OffsetF& bottomPosition, const SizeF& childSize); 186 void GetPositionWithPlacement( 187 OffsetF& childPosition, OffsetF& arrowPosition, const SizeF& childSize, Placement placement); 188 ErrorPositionType GetErrorPositionType(const OffsetF& childOffset, const SizeF& childSize); 189 OffsetF FitToScreen(const OffsetF& fitPosition, const SizeF& childSize); 190 SizeF GetPopupMaxWidthAndHeight(bool showInSubWindow, const float& width); 191 ArrowOfTargetOffset arrowOfTargetOffset_ = ArrowOfTargetOffset::NONE; 192 Dimension arrowOffset_; 193 194 int32_t targetNodeId_ = -1; 195 std::string targetTag_; 196 bool bCaretMode_ = false; 197 bool useCustom_ = false; 198 199 SizeF targetSize_; 200 OffsetF targetOffset_; 201 OffsetF targetOffsetForPaint_; 202 SizeF childSize_; 203 OffsetF childOffset_; 204 OffsetF childOffsetForPaint_; 205 OffsetF arrowPosition_; 206 OffsetF arrowPositionForPaint_; 207 SizeF selfSize_; 208 RectF touchRegion_; 209 SizeF buttonRowSize_; 210 OffsetF buttonRowOffset_; 211 212 Edge padding_; 213 Edge margin_; 214 Border border_; 215 Placement arrowPlacement_ = Placement::BOTTOM; 216 Placement placement_ = Placement::BOTTOM; 217 Dimension targetSpace_; 218 Dimension borderRadius_; 219 Dimension userSetTargetSpace_; 220 bool showArrow_ = false; 221 bool enableArrow_ = false; 222 float scaledBubbleSpacing_ = 0.0f; 223 float arrowHeight_ = 0.0f; 224 float realArrowWidth_ = 20.0f; 225 float realArrowHeight_ = 10.0f; 226 227 float paddingStart_ = 0.0f; 228 float paddingEnd_ = 0.0f; 229 float paddingTop_ = 0.0f; 230 float paddingBottom_ = 0.0f; 231 float top_ = 0.0f; 232 float bottom_ = 0.0f; 233 bool bHorizontal_ = false; 234 bool bVertical_ = false; 235 std::unordered_set<Placement> setHorizontal_; 236 std::unordered_set<Placement> setVertical_; 237 float targetSecurity_ = 0.0f; 238 using PlacementFunc = OffsetF (BubbleLayoutAlgorithm::*)(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 239 std::map<Placement, PlacementFunc> placementFuncMap_; 240 std::string clipPath_; 241 RefPtr<FrameNode> clipFrameNode_; 242 SizeF layoutChildSize_; 243 SizeF measureChildSizeLast_; 244 SizeF measureChildSizeAfter_; 245 SizeF measureChildSizeBefore_; 246 ACE_DISALLOW_COPY_AND_MOVE(BubbleLayoutAlgorithm); 247 std::vector<std::vector<float>> arrowOffsetsFromClip_ 248 = { {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f} }; 249 }; 250 } // namespace OHOS::Ace::NG 251 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_BUBBLE_BUBBLE_LAYOUT_ALGORITHM_H 252