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 GetBorder()84 const Border& GetBorder() const 85 { 86 return border_; 87 } 88 GetArrowPlacement()89 Placement GetArrowPlacement() const 90 { 91 return arrowPlacement_; 92 } 93 GetArrowOffsetByClips()94 std::vector<float>& GetArrowOffsetByClips() 95 { 96 return arrowOffsetByClips_; 97 } 98 GetClipPath()99 std::string GetClipPath() const 100 { 101 return clipPath_; 102 } GetClipFrameNode()103 RefPtr<FrameNode> GetClipFrameNode() 104 { 105 return clipFrameNode_; 106 } 107 GetArrowOffsetsFromClip()108 const std::vector<std::vector<float>>& GetArrowOffsetsFromClip() const 109 { 110 return arrowOffsetsFromClip_; 111 } 112 GetArrowWidth()113 const float& GetArrowWidth() const 114 { 115 return realArrowWidth_; 116 } 117 GetArrowHeight()118 const float& GetArrowHeight() const 119 { 120 return realArrowHeight_; 121 } 122 123 void UpdateMarginByWidth(); 124 125 protected: 126 OffsetF positionOffset_; 127 SizeF wrapperSize_; 128 129 private: 130 enum class ErrorPositionType { 131 NORMAL = 0, 132 TOP_LEFT_ERROR, 133 BOTTOM_RIGHT_ERROR, 134 }; 135 bool CheckPosition(const OffsetF& position, const SizeF& childSize, size_t step, size_t& i); 136 OffsetF GetPositionWithPlacementTop(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 137 OffsetF GetPositionWithPlacementTopLeft(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 138 OffsetF GetPositionWithPlacementTopRight(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 139 OffsetF GetPositionWithPlacementBottom(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 140 OffsetF GetPositionWithPlacementBottomLeft(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 141 OffsetF GetPositionWithPlacementBottomRight(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 142 OffsetF GetPositionWithPlacementLeft(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 143 OffsetF GetPositionWithPlacementLeftTop(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 144 OffsetF GetPositionWithPlacementLeftBottom(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 145 OffsetF GetPositionWithPlacementRight(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 146 OffsetF GetPositionWithPlacementRightTop(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 147 OffsetF GetPositionWithPlacementRightBottom(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 148 OffsetF AddTargetSpace(const OffsetF& position); 149 OffsetF AddOffset(const OffsetF& position); 150 bool CheckPositionInPlacementRect(const Rect& rect, const OffsetF& position, const SizeF& childSize); 151 OffsetF AdjustPosition(const OffsetF& position, float width, float height, float space); 152 OffsetF GetAdjustPosition(std::vector<Placement>& currentPlacementStates, size_t step, const SizeF& childSize, 153 const OffsetF& topPosition, const OffsetF& bottomPosition, OffsetF& arrowPosition); 154 void InitTargetSizeAndPosition(bool showInSubWindow); 155 void InitCaretTargetSizeAndPosition(); 156 void InitProps(const RefPtr<BubbleLayoutProperty>& layoutProp, bool showInSubWindow); 157 void InitArrowState(const RefPtr<BubbleLayoutProperty>& layoutProp); 158 OffsetF GetPositionWithPlacementNew( 159 const SizeF& childSize, const OffsetF& topPosition, const OffsetF& bottomPosition, OffsetF& arrowPosition); 160 OffsetF GetChildPositionNew(const SizeF& childSize, const RefPtr<BubbleLayoutProperty>& bubbleProp); 161 OffsetF FitToScreenNew( 162 const OffsetF& position, size_t step, size_t& i, const SizeF& childSize, bool didNeedArrow = false); 163 bool GetIfNeedArrow(const RefPtr<BubbleLayoutProperty>& bubbleProp, const SizeF& childSize); 164 void UpdateChildPosition(OffsetF& childOffset); 165 void UpdateTouchRegion(); 166 167 std::string MoveTo(double x, double y); 168 std::string LineTo(double x, double y); 169 std::string ArcTo(double rx, double ry, double rotation, int32_t arc_flag, double x, double y); 170 void UpdateClipOffset(const RefPtr<FrameNode>& frameNode); 171 172 std::string ClipBubbleWithPath(); 173 float GetArrowOffset(const Placement& placement); 174 void InitEdgeSize(Edge& edge); 175 float ModifyBorderRadius(float borderRadius, float halfChildHeight); 176 void GetArrowBuildPlacement(Placement& arrowBuildplacement); 177 std::string BuildTopLinePath(float arrowOffset, float radius, Placement& arrowBuildplacement); 178 std::string BuildRightLinePath(float arrowOffset, float radius, Placement& arrowBuildplacement); 179 std::string BuildBottomLinePath(float arrowOffset, float radius, Placement& arrowBuildplacement); 180 std::string BuildLeftLinePath(float arrowOffset, float radius, Placement& arrowBuildplacement); 181 std::string ReplaceArrowTopLeft(const float arrowOffset, const float childOffset); 182 std::string ReplaceArrowTopRight(const float arrowOffset, const float childOffset); 183 std::string ReplaceArrowRightTop(const float arrowOffset, const float childOffset); 184 std::string ReplaceArrowRightBottom(const float arrowOffset, const float childOffset); 185 std::string ReplaceArrowBottomLeft(const float arrowOffset, const float childOffset); 186 std::string ReplaceArrowBottomRight(const float arrowOffset, const float childOffset); 187 std::string ReplaceArrowLeftTop(const float arrowOffset, const float childOffset); 188 std::string ReplaceArrowLeftBottom(const float arrowOffset, const float childOffset); 189 std::string BuildCornerPath(const Placement& placement, float radius); 190 void UpdateArrowOffset(const std::optional<Dimension>& offset, const Placement& placement); 191 void BubbleAvoidanceRule(RefPtr<LayoutWrapper> child, RefPtr<BubbleLayoutProperty> bubbleProp, 192 RefPtr<FrameNode> bubbleNode, bool showInSubWindow); 193 void SetArrowOffsetsFromClip(const int16_t index, const float offsetX, const float offsetY); 194 void SetHotAreas(bool showInSubWindow, bool isBlock, RefPtr<FrameNode> frameNode, int32_t containerId); 195 void SetBubbleRadius(); 196 197 OffsetF GetChildPosition( 198 const SizeF& childSize, const RefPtr<BubbleLayoutProperty>& layoutProp, bool UseArrowOffset); 199 void InitArrowTopAndBottomPosition(OffsetF& topArrowPosition, OffsetF& bottomArrowPosition, OffsetF& topPosition, 200 OffsetF& bottomPosition, const SizeF& childSize); 201 void GetPositionWithPlacement( 202 OffsetF& childPosition, OffsetF& arrowPosition, const SizeF& childSize, Placement placement); 203 ErrorPositionType GetErrorPositionType(const OffsetF& childOffset, const SizeF& childSize); 204 OffsetF FitToScreen(const OffsetF& fitPosition, const SizeF& childSize); 205 SizeF GetPopupMaxWidthAndHeight(bool showInSubWindow, const float& width); 206 ArrowOfTargetOffset arrowOfTargetOffset_ = ArrowOfTargetOffset::NONE; 207 Dimension arrowOffset_; 208 209 int32_t targetNodeId_ = -1; 210 std::string targetTag_; 211 bool bCaretMode_ = false; 212 bool useCustom_ = false; 213 214 SizeF targetSize_; 215 OffsetF targetOffset_; 216 OffsetF targetOffsetForPaint_; 217 SizeF childSize_; 218 OffsetF childOffset_; 219 // Offset from upper left corner of the screen 220 OffsetF childOffsetForPaint_; 221 OffsetF arrowPosition_; 222 OffsetF arrowPositionForPaint_; 223 // top right bottom left 224 std::vector<float> arrowOffsetByClips_ = { 0.0f, 0.0f, 0.0f, 0.0f }; 225 SizeF selfSize_; 226 RectF touchRegion_; 227 SizeF buttonRowSize_; 228 OffsetF buttonRowOffset_; 229 230 Edge padding_; 231 Edge margin_; 232 Border border_; 233 Placement arrowPlacement_ = Placement::BOTTOM; 234 Placement placement_ = Placement::BOTTOM; 235 Dimension targetSpace_; 236 Dimension borderRadius_; 237 Dimension userSetTargetSpace_; 238 bool showArrow_ = false; 239 bool enableArrow_ = false; 240 bool isCaretMode_ = true; 241 bool followTransformOfTarget_ = false; 242 float scaledBubbleSpacing_ = 0.0f; 243 float arrowHeight_ = 0.0f; 244 float realArrowWidth_ = 20.0f; 245 float realArrowHeight_ = 10.0f; 246 247 float marginStart_ = 0.0f; 248 float marginEnd_ = 0.0f; 249 float marginTop_ = 0.0f; 250 float marginBottom_ = 0.0f; 251 float top_ = 0.0f; 252 float bottom_ = 0.0f; 253 bool bHorizontal_ = false; 254 bool bVertical_ = false; 255 std::unordered_set<Placement> setHorizontal_; 256 std::unordered_set<Placement> setVertical_; 257 float targetSecurity_ = 0.0f; 258 using PlacementFunc = OffsetF (BubbleLayoutAlgorithm::*)(const SizeF&, const OffsetF&, const OffsetF&, OffsetF&); 259 std::map<Placement, PlacementFunc> placementFuncMap_; 260 std::string clipPath_; 261 RefPtr<FrameNode> clipFrameNode_; 262 SizeF layoutChildSize_; 263 SizeF measureChildSizeLast_; 264 SizeF measureChildSizeAfter_; 265 SizeF measureChildSizeBefore_; 266 ACE_DISALLOW_COPY_AND_MOVE(BubbleLayoutAlgorithm); 267 std::vector<std::vector<float>> arrowOffsetsFromClip_ 268 = { {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f} }; 269 bool isGreatWrapperWidth_ = false; 270 }; 271 } // namespace OHOS::Ace::NG 272 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_BUBBLE_BUBBLE_LAYOUT_ALGORITHM_H 273