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_DIALOG_DIALOG_LAYOUT_ALGORITHM_H
16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_DIALOG_DIALOG_LAYOUT_ALGORITHM_H
17 
18 #include <string>
19 
20 #include "base/geometry/ng/offset_t.h"
21 #include "base/geometry/ng/rect_t.h"
22 #include "base/geometry/ng/size_t.h"
23 #include "core/components/common/layout/grid_layout_info.h"
24 #include "core/components/common/layout/grid_system_manager.h"
25 #include "core/components/common/properties/border.h"
26 #include "core/components/common/properties/edge.h"
27 #include "core/components/common/properties/placement.h"
28 #include "core/components/dialog/dialog_theme.h"
29 #include "core/components_ng/layout/layout_algorithm.h"
30 #include "core/components_ng/pattern/dialog/dialog_layout_property.h"
31 #include "core/components_ng/pattern/overlay/overlay_manager.h"
32 
33 namespace OHOS::Ace::NG {
34 enum class TouchingBoundaryType {
35     NotTouchBoundary = 0,
36     TouchBottomBoundary,
37     TouchRightBoundary,
38 };
39 // DialogLayoutAlgorithm uses for Dialog Node.
40 class ACE_EXPORT DialogLayoutAlgorithm : public LayoutAlgorithm {
41     DECLARE_ACE_TYPE(DialogLayoutAlgorithm, LayoutAlgorithm);
42 
43 public:
44     DialogLayoutAlgorithm() = default;
45     ~DialogLayoutAlgorithm() override = default;
46 
47     void Measure(LayoutWrapper* layoutWrapper) override;
48 
49     void Layout(LayoutWrapper* layoutWrapper) override;
50 
GetTouchRegion()51     RectF GetTouchRegion() const
52     {
53         return touchRegion_;
54     }
55 
56 private:
57     LayoutConstraintF CreateDialogChildConstraint(LayoutWrapper* layoutWrapper, float height, float width);
58     void Distribute(float& scrollHeight, float& listHeight, float restHeight);
59     void AnalysisHeightOfChild(LayoutWrapper* layoutWrapper);
60     void AnalysisLayoutOfContent(LayoutWrapper* layoutWrapper, const RefPtr<LayoutWrapper>& scroll);
61 
62     bool ComputeInnerLayoutSizeParam(LayoutConstraintF& innerLayout, const RefPtr<DialogLayoutProperty>& dialogProp);
63     bool IsGetExpandDisplayValidHeight();
64     RefPtr<PipelineContext> GetCurrentPipelineContext();
65     void ComputeInnerLayoutParam(LayoutConstraintF& innerLayout, const RefPtr<DialogLayoutProperty>& dialogProp);
66     double GetMaxWidthBasedOnGridType(const RefPtr<GridColumnInfo>& info, GridSizeType type, DeviceType deviceType);
67     int32_t GetDeviceColumns(GridSizeType type, DeviceType deviceType);
68     OffsetF ComputeChildPosition(
69         const SizeF& childSize, const RefPtr<DialogLayoutProperty>& prop, const SizeF& slefSize);
70     bool SetAlignmentSwitch(SizeF& maxSize, const SizeF& childSize, OffsetF& topLeftPoint);
71     bool SetAlignmentSwitchLessThanAPITwelve(const SizeF& maxSize, const SizeF& childSize, OffsetF& topLeftPoint);
72     bool IsAlignmentByWholeScreen();
73     void CaculateMaxSize(SizeF& maxSize);
74     bool IsDialogTouchingBoundary(OffsetF topLeftPoint, SizeF childSize, SizeF selfSize);
75     void MultipleDialog(const RefPtr<DialogLayoutProperty>& dialogProp, const SizeF& childSize, const SizeF& selfSize,
76         const RefPtr<OverlayManager> subOverlayManager);
77     void ProcessMaskRect(std::optional<DimensionRect> maskRect, const RefPtr<FrameNode>& dialog, bool isMask = false);
78     void SetSubWindowHotarea(
79         const RefPtr<DialogLayoutProperty>& dialogProp, SizeF childSize, SizeF selfSize, int32_t frameNodeId);
80     std::optional<DimensionRect> GetMaskRect(const RefPtr<FrameNode>& dialog);
81 
82     void UpdateTouchRegion();
83 
84     double GetPaddingBottom() const;
85 
86     OffsetF AdjustChildPosition(
87         OffsetF& topLeftPoint, const OffsetF& dialogOffset, const SizeF& childSize, bool needAvoidKeyboard);
88 
89     SizeF UpdateHeightWithSafeArea(SizeF size);
90     void UpdateSafeArea();
91     void UpdateChildLayoutConstraint(const RefPtr<DialogLayoutProperty>& dialogProp,
92         LayoutConstraintF& childLayoutConstraint, RefPtr<LayoutWrapper>& childLayoutWrapper);
93     void ClipUIExtensionSubWindowContent(const RefPtr<FrameNode>& dialog, bool isClip);
94     void AdjustHeightForKeyboard(LayoutWrapper* layoutWrapper, const RefPtr<LayoutWrapper>& child);
95     void UpdateIsScrollHeightNegative(LayoutWrapper* layoutWrapper, float height);
96     void UpdateChildMaxSizeHeight(SizeT<float>& maxSize);
97 
98     RectF touchRegion_;
99     OffsetF topLeftPoint_;
100     bool customSize_ = false;
101     SafeAreaInsets safeAreaInsets_;
102     bool isModal_ = true;
103     bool isShowInSubWindow_ = false;
104     bool isSuitableForElderly_ = false;
105     bool isSuitOldMeasure_ = false;
106     float dialogMaxHeight_ = 0.0f;
107     int32_t gridCount_ = -1;
108     int32_t subWindowId_ = -1;
109     DimensionOffset dialogOffset_;
110     DialogAlignment alignment_ = DialogAlignment::DEFAULT;
111     TouchingBoundaryType touchingBoundaryFlag_ = TouchingBoundaryType::NotTouchBoundary;
112 
113     bool expandDisplay_ = false;
114     double expandDisplayValidHeight_ = 0.0;
115     bool isUIExtensionSubWindow_ = false;
116     RectF hostWindowRect_;
117 
118     SizeF dialogChildSize_;
119     bool resizeFlag_ = false;
120     bool isHoverMode_ = false;
121     bool isKeyBoardShow_ = false;
122     bool alignBottomScreen_ = false;
123     Rect foldCreaseRect = Rect(0.0, 0.0, 0.0, 0.0);
124     HoverModeAreaType hoverModeArea_ = HoverModeAreaType::BOTTOM_SCREEN;
125 
126     KeyboardAvoidMode keyboardAvoidMode_ = KeyboardAvoidMode::DEFAULT;
127 
128     ACE_DISALLOW_COPY_AND_MOVE(DialogLayoutAlgorithm);
129 };
130 } // namespace OHOS::Ace::NG
131 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_DIALOG_DIALOG_LAYOUT_ALGORITHM_H
132