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_PATTERNS_OVERLAY_SHEET_STYLE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_OVERLAY_SHEET_STYLE_H
18 
19 #include <optional>
20 
21 #include "base/geometry/dimension.h"
22 #include "core/components/common/layout/constants.h"
23 #include "core/components/common/properties/color.h"
24 #include "core/components/common/properties/decoration.h"
25 #include "core/components_ng/pattern/overlay/sheet_theme.h"
26 
27 namespace OHOS::Ace::NG {
28 constexpr float SHEET_VELOCITY_THRESHOLD = 1000.0f;
29 constexpr float CURVE_MASS = 1.0f;
30 constexpr float CURVE_STIFFNESS = 328.0f;
31 constexpr float CURVE_DAMPING = 36.0f;
32 constexpr float MEDIUM_SIZE = 0.6f;
33 constexpr float MEDIUM_SIZE_PRE = 0.5f;
34 constexpr float POPUP_LARGE_SIZE = 0.9f;
35 enum SheetMode {
36     MEDIUM,
37     LARGE,
38     AUTO,
39 };
40 
41 enum SheetType {
42     SHEET_BOTTOM,
43     SHEET_CENTER,
44     SHEET_POPUP,
45     SHEET_BOTTOMLANDSPACE,
46     SHEET_BOTTOM_FREE_WINDOW,
47 };
48 
49 struct SheetKey {
SheetKeySheetKey50     SheetKey() {}
SheetKeySheetKey51     explicit SheetKey(int32_t inputTargetId) : targetId(inputTargetId) {}
SheetKeySheetKey52     SheetKey(bool hasValidTarget, int32_t inputContentId, int32_t inputTargetId)
53         : hasValidTargetNode(hasValidTarget), contentId(inputContentId), targetId(inputTargetId)
54     {
55         isStartUpByUIContext = true;
56     }
57 
58     bool operator==(const SheetKey& other) const
59     {
60         return isStartUpByUIContext == other.isStartUpByUIContext &&
61             hasValidTargetNode == other.hasValidTargetNode &&
62             contentId == other.contentId && targetId == other.targetId;
63     }
64 
65     bool isStartUpByUIContext = false;  // Indicates whether the sheet is started by UIContext
66     bool hasValidTargetNode = true;     // If sheet was start-up by UIContext and without targetId, this flag is FALSE
67     int32_t contentId = -1;             // Indicates the uniqueID of componentContent when isStartUpByUIContext is TRUE
68     int32_t targetId = -1;
69 };
70 
71 struct SheetKeyHash {
operatorSheetKeyHash72     size_t operator()(const SheetKey& sheetKey) const
73     {
74         return sheetKey.isStartUpByUIContext ? sheetKey.contentId : sheetKey.targetId;
75     }
76 };
77 
78 enum SheetLevel {
79     OVERLAY,
80     EMBEDDED,
81 };
82 
83 enum ScrollSizeMode {
84     FOLLOW_DETENT,
85     CONTINUOUS,
86 };
87 
88 struct SheetHeight {
89     std::optional<Dimension> height;
90     std::optional<SheetMode> sheetMode;
91 
92     bool operator==(const SheetHeight& sheetHeight) const
93     {
94         return (height == sheetHeight.height && sheetMode == sheetHeight.sheetMode);
95     }
96 
97     bool operator!=(const SheetHeight& sheetHeight) const
98     {
99         return !(*this == sheetHeight);
100     }
101 };
102 
103 enum class SheetKeyboardAvoidMode {
104     NONE,
105     TRANSLATE_AND_RESIZE,
106     RESIZE_ONLY,
107     TRANSLATE_AND_SCROLL,
108 };
109 
110 struct SheetStyle {
111     std::optional<Dimension> height;
112     std::optional<SheetMode> sheetMode;
113     std::optional<bool> showDragBar;
114     std::optional<bool> showCloseIcon;
115     std::optional<bool> isTitleBuilder;
116     std::optional<SheetType> sheetType;
117     std::optional<Color> backgroundColor;
118     std::optional<Color> maskColor;
119     std::optional<BlurStyleOption> backgroundBlurStyle;
120     std::optional<std::string> sheetTitle;
121     std::optional<std::string> sheetSubtitle;
122     std::vector<SheetHeight> detents;
123     std::optional<bool> interactive;
124     std::optional<bool> showInPage;
125     std::optional<ScrollSizeMode> scrollSizeMode;
126     std::optional<SheetKeyboardAvoidMode> sheetKeyboardAvoidMode;
127     std::optional<NG::BorderWidthProperty> borderWidth; // border width
128     std::optional<NG::BorderColorProperty> borderColor; // border color
129     std::optional<NG::BorderStyleProperty> borderStyle;  // border style
130     std::optional<Shadow> shadow;
131     std::optional<Dimension> width;
132     std::optional<int32_t> instanceId; // uiContext instanceId
133     std::optional<bool> enableHoverMode;
134     std::optional<HoverModeAreaType> hoverModeArea;
135 
136     bool operator==(const SheetStyle& sheetStyle) const
137     {
138         return (height == sheetStyle.height && sheetMode == sheetStyle.sheetMode &&
139                 showDragBar == sheetStyle.showDragBar && showCloseIcon == sheetStyle.showCloseIcon &&
140                 isTitleBuilder == sheetStyle.isTitleBuilder && sheetType == sheetStyle.sheetType &&
141                 backgroundColor == sheetStyle.backgroundColor && maskColor == sheetStyle.maskColor &&
142                 detents == sheetStyle.detents && backgroundBlurStyle == sheetStyle.backgroundBlurStyle &&
143                 sheetTitle == sheetStyle.sheetTitle && sheetSubtitle == sheetStyle.sheetSubtitle &&
144                 interactive == sheetStyle.interactive && showInPage == sheetStyle.showInPage &&
145                 borderWidth == sheetStyle.borderWidth && borderColor == sheetStyle.borderColor &&
146                 borderStyle == sheetStyle.borderStyle && shadow == sheetStyle.shadow && width == sheetStyle.width &&
147                 instanceId == sheetStyle.instanceId && scrollSizeMode == sheetStyle.scrollSizeMode &&
148                 sheetKeyboardAvoidMode == sheetStyle.sheetKeyboardAvoidMode &&
149                 enableHoverMode == sheetStyle.enableHoverMode &&
150                 hoverModeArea == sheetStyle.hoverModeArea);
151     }
152 
PartialUpdateSheetStyle153     void PartialUpdate(const SheetStyle& sheetStyle)
154     {
155         if (sheetStyle.height.has_value() && !sheetStyle.sheetMode.has_value()) {
156             height = sheetStyle.height;
157             sheetMode.reset();
158         } else if (!sheetStyle.height.has_value() && sheetStyle.sheetMode.has_value()) {
159             sheetMode = sheetStyle.sheetMode;
160             height.reset();
161         } else {
162             sheetMode = sheetStyle.sheetMode.has_value() ? sheetStyle.sheetMode : sheetMode;
163         }
164         showDragBar = sheetStyle.showDragBar.has_value() ? sheetStyle.showDragBar : showDragBar;
165         showCloseIcon = sheetStyle.showCloseIcon.has_value() ? sheetStyle.showCloseIcon : showCloseIcon;
166         isTitleBuilder = sheetStyle.isTitleBuilder.has_value() ? sheetStyle.isTitleBuilder : isTitleBuilder;
167         sheetType = sheetStyle.sheetType.has_value() ? sheetStyle.sheetType : sheetType;
168         backgroundColor = sheetStyle.backgroundColor.has_value() ? sheetStyle.backgroundColor : backgroundColor;
169         maskColor = sheetStyle.maskColor.has_value() ? sheetStyle.maskColor : maskColor;
170         backgroundBlurStyle = sheetStyle.backgroundBlurStyle.has_value() ?
171             sheetStyle.backgroundBlurStyle : backgroundBlurStyle;
172         sheetTitle = sheetStyle.sheetTitle.has_value() ? sheetStyle.sheetTitle : sheetTitle;
173         sheetSubtitle = sheetStyle.sheetSubtitle.has_value() ? sheetStyle.sheetSubtitle : sheetSubtitle;
174         detents = !sheetStyle.detents.empty() ? sheetStyle.detents : detents;
175         interactive = sheetStyle.interactive.has_value() ? sheetStyle.interactive : interactive;
176         scrollSizeMode = sheetStyle.scrollSizeMode.has_value() ? sheetStyle.scrollSizeMode : scrollSizeMode;
177         sheetKeyboardAvoidMode =
178             sheetStyle.sheetKeyboardAvoidMode.has_value() ? sheetStyle.sheetKeyboardAvoidMode : sheetKeyboardAvoidMode;
179         borderWidth = sheetStyle.borderWidth.has_value() ? sheetStyle.borderWidth : borderWidth;
180         borderColor = sheetStyle.borderColor.has_value() ? sheetStyle.borderColor : borderColor;
181         borderStyle = sheetStyle.borderStyle.has_value() ? sheetStyle.borderStyle : borderStyle;
182         shadow = sheetStyle.shadow.has_value() ? sheetStyle.shadow : shadow;
183         width = sheetStyle.width.has_value() ? sheetStyle.width : width;
184         enableHoverMode = sheetStyle.enableHoverMode.has_value() ? sheetStyle.enableHoverMode : enableHoverMode;
185         hoverModeArea = sheetStyle.hoverModeArea.has_value() ? sheetStyle.hoverModeArea : hoverModeArea;
186     }
187 };
188 } // namespace OHOS::Ace::NG
189 
190 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_OVERLAY_SHEET_STYLE_H
191