1 /* 2 * Copyright (c) 2024 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_OVERLAY_SHEET_WRAPPER_LAYOUT_ALGORITHM_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_OVERLAY_SHEET_WRAPPER_LAYOUT_ALGORITHM_H 18 19 #include "core/common/container.h" 20 #include "core/components/common/properties/placement.h" 21 #include "core/components_ng/layout/box_layout_algorithm.h" 22 #include "core/components_ng/pattern/overlay/sheet_style.h" 23 24 namespace OHOS::Ace::NG { 25 class ACE_EXPORT SheetWrapperLayoutAlgorithm : public BoxLayoutAlgorithm { 26 DECLARE_ACE_TYPE(SheetWrapperLayoutAlgorithm, BoxLayoutAlgorithm); 27 28 public: SheetWrapperLayoutAlgorithm()29 SheetWrapperLayoutAlgorithm() 30 { 31 placementCheckFunc_[Placement::BOTTOM] = &SheetWrapperLayoutAlgorithm::CheckPlacementBottom; 32 placementCheckFunc_[Placement::BOTTOM_LEFT] = &SheetWrapperLayoutAlgorithm::CheckPlacementBottomLeft; 33 placementCheckFunc_[Placement::BOTTOM_RIGHT] = &SheetWrapperLayoutAlgorithm::CheckPlacementBottomRight; 34 35 directionCheckFunc_[Placement::BOTTOM] = &SheetWrapperLayoutAlgorithm::CheckDirectionBottom; 36 directionCheckFunc_[Placement::BOTTOM_LEFT] = &SheetWrapperLayoutAlgorithm::CheckDirectionBottom; 37 directionCheckFunc_[Placement::BOTTOM_RIGHT] = &SheetWrapperLayoutAlgorithm::CheckDirectionBottom; 38 directionCheckFunc_[Placement::TOP] = &SheetWrapperLayoutAlgorithm::CheckDirectionTop; 39 directionCheckFunc_[Placement::TOP_LEFT] = &SheetWrapperLayoutAlgorithm::CheckDirectionTop; 40 directionCheckFunc_[Placement::TOP_RIGHT] = &SheetWrapperLayoutAlgorithm::CheckDirectionTop; 41 directionCheckFunc_[Placement::LEFT] = &SheetWrapperLayoutAlgorithm::CheckDirectionLeft; 42 directionCheckFunc_[Placement::LEFT_TOP] = &SheetWrapperLayoutAlgorithm::CheckDirectionLeft; 43 directionCheckFunc_[Placement::LEFT_BOTTOM] = &SheetWrapperLayoutAlgorithm::CheckDirectionLeft; 44 directionCheckFunc_[Placement::RIGHT] = &SheetWrapperLayoutAlgorithm::CheckDirectionRight; 45 directionCheckFunc_[Placement::RIGHT_TOP] = &SheetWrapperLayoutAlgorithm::CheckDirectionRight; 46 directionCheckFunc_[Placement::RIGHT_BOTTOM] = &SheetWrapperLayoutAlgorithm::CheckDirectionRight; 47 48 getOffsetFunc_[Placement::BOTTOM] = &SheetWrapperLayoutAlgorithm::GetOffsetWithBottom; 49 getOffsetFunc_[Placement::BOTTOM_LEFT] = &SheetWrapperLayoutAlgorithm::GetOffsetWithBottomLeft; 50 getOffsetFunc_[Placement::BOTTOM_RIGHT] = &SheetWrapperLayoutAlgorithm::GetOffsetWithBottomRight; 51 getOffsetFunc_[Placement::TOP] = &SheetWrapperLayoutAlgorithm::GetOffsetWithTop; 52 getOffsetFunc_[Placement::TOP_LEFT] = &SheetWrapperLayoutAlgorithm::GetOffsetWithTopLeft; 53 getOffsetFunc_[Placement::TOP_RIGHT] = &SheetWrapperLayoutAlgorithm::GetOffsetWithTopRight; 54 getOffsetFunc_[Placement::LEFT] = &SheetWrapperLayoutAlgorithm::GetOffsetWithLeft; 55 getOffsetFunc_[Placement::LEFT_TOP] = &SheetWrapperLayoutAlgorithm::GetOffsetWithLeftTop; 56 getOffsetFunc_[Placement::LEFT_BOTTOM] = &SheetWrapperLayoutAlgorithm::GetOffsetWithLeftBottom; 57 getOffsetFunc_[Placement::RIGHT] = &SheetWrapperLayoutAlgorithm::GetOffsetWithRight; 58 getOffsetFunc_[Placement::RIGHT_TOP] = &SheetWrapperLayoutAlgorithm::GetOffsetWithRightTop; 59 getOffsetFunc_[Placement::RIGHT_BOTTOM] = &SheetWrapperLayoutAlgorithm::GetOffsetWithRightBottom; 60 } 61 ~SheetWrapperLayoutAlgorithm() override = default; 62 63 void Measure(LayoutWrapper* layoutWrapper) override; 64 void Layout(LayoutWrapper* layoutWrapper) override; 65 66 void MeasureSheetMask(LayoutWrapper* layoutWrapper); 67 void LayoutMaskNode(LayoutWrapper* layoutWrapper); 68 69 private: 70 void InitParameter(LayoutWrapper* layoutWrapper); 71 void GetSheetPageSize(LayoutWrapper* layoutWrapper); 72 void DecreaseArrowHeightWhenArrowIsShown(const RefPtr<FrameNode>& sheetNode); 73 OffsetF GetPopupStyleSheetOffset(LayoutWrapper* layoutWrapper); 74 OffsetF GetOffsetInAvoidanceRule( 75 LayoutWrapper* layoutWrapper, const SizeF& targetSize, const OffsetF& targetOffset); 76 Placement AvoidanceRuleBottom( 77 const Placement& currentPlacement, const SizeF& targetSize, const OffsetF& targetOffset); 78 Placement AvoidanceRuleOfPlacement( 79 LayoutWrapper* layoutWrapper, const SizeF& targetSize, const OffsetF& targetOffset); 80 Placement RecheckBestPlacementWithInsufficientSpace(const SizeF&, const OffsetF&, SizeF&); 81 SizeF GetLeftSpaceWithPlacement(const Placement& placement, const SizeF& targetSize, const OffsetF& targetOffset); 82 bool CheckDirectionBottom(const SizeF&, const OffsetF&); 83 bool CheckDirectionTop(const SizeF&, const OffsetF&); 84 bool CheckDirectionRight(const SizeF&, const OffsetF&); 85 bool CheckDirectionLeft(const SizeF&, const OffsetF&); 86 87 bool CheckPlacementBottom(const SizeF&, const OffsetF&); 88 bool CheckPlacementBottomLeft(const SizeF&, const OffsetF&); 89 bool CheckPlacementBottomRight(const SizeF&, const OffsetF&); 90 91 OffsetF GetOffsetWithBottom(const SizeF&, const OffsetF&); 92 OffsetF GetOffsetWithBottomLeft(const SizeF&, const OffsetF&); 93 OffsetF GetOffsetWithBottomRight(const SizeF&, const OffsetF&); 94 OffsetF GetOffsetWithTop(const SizeF&, const OffsetF&); 95 OffsetF GetOffsetWithTopLeft(const SizeF&, const OffsetF&); 96 OffsetF GetOffsetWithTopRight(const SizeF&, const OffsetF&); 97 OffsetF GetOffsetWithLeft(const SizeF&, const OffsetF&); 98 OffsetF GetOffsetWithLeftTop(const SizeF&, const OffsetF&); 99 OffsetF GetOffsetWithLeftBottom(const SizeF&, const OffsetF&); 100 OffsetF GetOffsetWithRight(const SizeF&, const OffsetF&); 101 OffsetF GetOffsetWithRightTop(const SizeF&, const OffsetF&); 102 OffsetF GetOffsetWithRightBottom(const SizeF&, const OffsetF&); 103 104 void SetArrowOffsetInBottomOrTop(const SizeF&, const OffsetF&, float); 105 void SetArrowOffsetInRightOrLeft(const SizeF&, const OffsetF&, float); 106 107 void RestrictOffsetInSpaceBottom(float&, float&); 108 void RestrictOffsetInSpaceTop(float&, float&); 109 void RestrictOffsetInSpaceLeft(float&, float&); 110 void RestrictOffsetInSpaceRight(float&, float&); 111 void CheckIsArrowOverlapSheetRadius(); 112 113 void RemeasureForPopup(LayoutWrapper* layoutWrapper); 114 void UpdateSheetNodePopupInfo(LayoutWrapper* layoutWrapper); 115 116 bool IsTargetNodeHideByKeyboard(const SafeAreaInsets::Inset& keyboardInset, const RefPtr<FrameNode>& targetNode); 117 118 using DirectionCheckFunc = bool (SheetWrapperLayoutAlgorithm::*)(const SizeF&, const OffsetF&); 119 std::unordered_map<Placement, DirectionCheckFunc> directionCheckFunc_; 120 using OffsetGetFunc = OffsetF (SheetWrapperLayoutAlgorithm::*)(const SizeF&, const OffsetF&); 121 std::unordered_map<Placement, OffsetGetFunc> getOffsetFunc_; 122 using PlacementCheckFunc = bool (SheetWrapperLayoutAlgorithm::*)(const SizeF&, const OffsetF&); 123 std::unordered_map<Placement, PlacementCheckFunc> placementCheckFunc_; 124 125 SheetPopupInfo sheetPopupInfo_; 126 Placement placement_ = Placement::BOTTOM; 127 Rect windowGlobalRect_; 128 float sheetWidth_ = 0.f; 129 float sheetHeight_ = 0.f; 130 BorderRadiusProperty sheetRadius_; 131 float windowEdgeWidth_ = 0.f; 132 ACE_DISALLOW_COPY_AND_MOVE(SheetWrapperLayoutAlgorithm); 133 }; 134 } // namespace OHOS::Ace::NG 135 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_OVERLAY_SHEET_WRAPPER_LAYOUT_ALGORITHM_H 136