1 /*
2  * Copyright (c) 2023 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_PRESENTATION_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_OVERLAY_SHEET_PRESENTATION_LAYOUT_ALGORITHM_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <optional>
22 #include <unordered_map>
23 
24 #include "base/geometry/axis.h"
25 #include "base/geometry/dimension.h"
26 #include "base/geometry/ng/size_t.h"
27 #include "base/memory/referenced.h"
28 #include "core/components/common/properties/placement.h"
29 #include "core/components_ng/layout/layout_algorithm.h"
30 #include "core/components_ng/layout/layout_wrapper.h"
31 #include "core/components_ng/pattern/linear_layout/linear_layout_algorithm.h"
32 #include "core/components_ng/pattern/overlay/sheet_presentation_property.h"
33 #include "core/components_ng/pattern/overlay/sheet_style.h"
34 
35 namespace OHOS::Ace::NG {
36 class ACE_EXPORT SheetPresentationLayoutAlgorithm : public LinearLayoutAlgorithm {
37     DECLARE_ACE_TYPE(SheetPresentationLayoutAlgorithm, LayoutAlgorithm);
38 
39 public:
40     SheetPresentationLayoutAlgorithm() = default;
SheetPresentationLayoutAlgorithm(int32_t id,const std::string & tag,SheetType sheetType)41     SheetPresentationLayoutAlgorithm(int32_t id, const std::string& tag, SheetType sheetType)
42         : targetNodeId_(id), targetTag_(tag), sheetType_(sheetType)
43     {
44         directionCheckFunc_[Placement::BOTTOM] = &SheetPresentationLayoutAlgorithm::CheckDirectionBottom;
45         placementCheckFunc_[Placement::BOTTOM] = &SheetPresentationLayoutAlgorithm::CheckPlacementBottom;
46         placementCheckFunc_[Placement::BOTTOM_LEFT] = &SheetPresentationLayoutAlgorithm::CheckPlacementBottomLeft;
47         placementCheckFunc_[Placement::BOTTOM_RIGHT] = &SheetPresentationLayoutAlgorithm::CheckPlacementBottomRight;
48         getOffsetFunc_[Placement::BOTTOM] = &SheetPresentationLayoutAlgorithm::GetOffsetWithBottom;
49         getOffsetFunc_[Placement::BOTTOM_LEFT] = &SheetPresentationLayoutAlgorithm::GetOffsetWithBottomLeft;
50         getOffsetFunc_[Placement::BOTTOM_RIGHT] = &SheetPresentationLayoutAlgorithm::GetOffsetWithBottomRight;
51     }
52     ~SheetPresentationLayoutAlgorithm() override = default;
53 
54     void InitParameter();
OnReset()55     void OnReset() override {}
56     void Measure(LayoutWrapper* layoutWrapper) override;
57     void Layout(LayoutWrapper* layoutWrapper) override;
GetSheetMaxHeight()58     float GetSheetMaxHeight()
59     {
60         return sheetMaxHeight_;
61     }
62 
GetSheetMaxWidth()63     float GetSheetMaxWidth()
64     {
65         return sheetMaxWidth_;
66     }
67 
GetCenterHeight()68     float GetCenterHeight()
69     {
70         return sheetHeight_;
71     }
72 
GetSheetOffsetX()73     float GetSheetOffsetX()
74     {
75         return sheetOffsetX_;
76     }
77 
GetSheetOffsetY()78     float GetSheetOffsetY()
79     {
80         return sheetOffsetY_;
81     }
82 
GetArrowOffsetX()83     float GetArrowOffsetX() const
84     {
85         return arrowOffsetX_;
86     }
87 
88     void CalculateSheetHeightInOtherScenes(LayoutWrapper* layoutWrapper);
89     void CalculateSheetOffsetInOtherScenes(LayoutWrapper* layoutWrapper);
90 private:
91     int32_t targetNodeId_ = -1;
92     std::string targetTag_;
93     OffsetF GetPopupStyleSheetOffset();
94     OffsetF GetOffsetInAvoidanceRule(const SizeF& targetSize, const OffsetF& targetOffset);
95     Placement AvoidanceRuleOfPlacement(
96         const Placement& currentPlacement, const SizeF& targetSize, const OffsetF& targetOffset);
97     bool CheckDirectionBottom(const SizeF&, const OffsetF&);
98     bool CheckPlacementBottom(const SizeF&, const OffsetF&);
99     bool CheckPlacementBottomLeft(const SizeF&, const OffsetF&);
100     bool CheckPlacementBottomRight(const SizeF&, const OffsetF&);
101     OffsetF GetOffsetWithBottom(const SizeF&, const OffsetF&);
102     OffsetF GetOffsetWithBottomLeft(const SizeF&, const OffsetF&);
103     OffsetF GetOffsetWithBottomRight(const SizeF&, const OffsetF&);
104 
105     float GetWidthByScreenSizeType(const SizeF& maxSize, LayoutWrapper* layoutWrapper) const;
106     float GetHeightByScreenSizeType(const SizeF& maxSize) const;
107     float GetHeightBySheetStyle() const;
108     bool SheetInSplitWindow() const;
109     LayoutConstraintF CreateSheetChildConstraint(RefPtr<SheetPresentationProperty> layoutprop);
110     float arrowOffsetX_ = 0.0f; // reletive to SheetOffsetX
111     float sheetHeight_ = 0.0f;
112     float sheetWidth_ = 0.0f;
113     float sheetMaxHeight_ = 0.0f;
114     float sheetMaxWidth_ = 0.0f;
115     float sheetOffsetX_ = 0.0f;
116     float sheetOffsetY_ = 0.0f;
117     float sheetRadius_ = 0.0f;
118     SheetType sheetType_ = SheetType::SHEET_BOTTOM;
119     SheetStyle sheetStyle_;
120     bool isRightAngleArrow_ = false;
121     bool isKeyBoardShow_ = false;
122     bool isHoverMode_ = false;
123     HoverModeAreaType hoverModeArea_ = HoverModeAreaType::BOTTOM_SCREEN;
124     using DirectionCheckFunc = bool (SheetPresentationLayoutAlgorithm::*)(const SizeF&, const OffsetF&);
125     std::unordered_map<Placement, DirectionCheckFunc> directionCheckFunc_;
126     using PlacementCheckFunc = bool (SheetPresentationLayoutAlgorithm::*)(const SizeF&, const OffsetF&);
127     std::unordered_map<Placement, PlacementCheckFunc> placementCheckFunc_;
128     using OffsetGetFunc = OffsetF (SheetPresentationLayoutAlgorithm::*)(const SizeF&, const OffsetF&);
129     std::unordered_map<Placement, OffsetGetFunc> getOffsetFunc_;
130     ACE_DISALLOW_COPY_AND_MOVE(SheetPresentationLayoutAlgorithm);
131 };
132 } // namespace OHOS::Ace::NG
133 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_OVERLAY_SHEET_PRESENTATION_LAYOUT_ALGORITHM_H