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/common/properties/placement.h" 26 #include "core/components_ng/pattern/overlay/modal_style.h" 27 #include "core/components_ng/pattern/overlay/sheet_theme.h" 28 #include "core/common/resource/resource_object.h" 29 30 #define ACE_SHEET_CREATE_RESOURCE_FUNCTIONS(name) \ 31 public: \ 32 void Set##name##ResObj(RefPtr<ResourceObject>& obj) { prop##name##Obj_ = obj; } \ 33 const RefPtr<ResourceObject>& Get##name##ResObj() const { return prop##name##Obj_; } \ 34 private: \ 35 RefPtr<ResourceObject> prop##name##Obj_ 36 37 namespace OHOS::Ace::NG { 38 constexpr float SHEET_VELOCITY_THRESHOLD = 1000.0f; 39 constexpr float CURVE_MASS = 1.0f; 40 constexpr float CURVE_STIFFNESS = 328.0f; 41 constexpr float CURVE_DAMPING = 36.0f; 42 constexpr float MEDIUM_SIZE = 0.6f; 43 constexpr float MEDIUM_SIZE_PRE = 0.5f; 44 constexpr float POPUP_LARGE_SIZE = 0.9f; 45 constexpr int32_t SHEET_ANIMATION_DURATION = 580; 46 47 enum SheetMode { 48 MEDIUM, 49 LARGE, 50 AUTO, 51 }; 52 53 enum SheetType { 54 SHEET_BOTTOM, 55 SHEET_CENTER, 56 SHEET_POPUP, 57 SHEET_SIDE = 3, 58 SHEET_CONTENT_COVER = 4, 59 SHEET_BOTTOMLANDSPACE, 60 SHEET_BOTTOM_FREE_WINDOW, 61 SHEET_BOTTOM_OFFSET, 62 }; 63 64 enum class SheetAccessibilityDetents { 65 HIGH = 0, 66 MEDIUM, 67 LOW, 68 }; 69 70 enum class SheetArrowPosition { 71 TOP_LEFT, 72 TOP_RIGHT, 73 BOTTOM_LEFT, 74 BOTTOM_RIGHT, 75 LEFT_TOP, 76 LEFT_BOTTOM, 77 RIGHT_TOP, 78 RIGHT_BOTTOM, 79 NONE 80 }; 81 82 enum class SheetEffectEdge { 83 NONE = 0, 84 START = 1, 85 END = 2, 86 ALL = 3, 87 }; 88 89 struct SheetKey { SheetKeySheetKey90 SheetKey() {} SheetKeySheetKey91 explicit SheetKey(int32_t inputTargetId) : targetId(inputTargetId) {} SheetKeySheetKey92 SheetKey(bool hasValidTarget, int32_t inputContentId, int32_t inputTargetId) 93 : hasValidTargetNode(hasValidTarget), contentId(inputContentId), targetId(inputTargetId) 94 { 95 isStartUpByUIContext = true; 96 } 97 98 bool operator==(const SheetKey& other) const 99 { 100 return isStartUpByUIContext == other.isStartUpByUIContext && 101 hasValidTargetNode == other.hasValidTargetNode && 102 contentId == other.contentId && targetId == other.targetId; 103 } 104 105 bool isStartUpByUIContext = false; // Indicates whether the sheet is started by UIContext 106 bool hasValidTargetNode = true; // If sheet was start-up by UIContext and without targetId, this flag is FALSE 107 int32_t contentId = -1; // Indicates the uniqueID of componentContent when isStartUpByUIContext is TRUE 108 int32_t targetId = -1; 109 }; 110 111 struct SheetPopupInfo { 112 Placement finalPlacement = Placement::NONE; 113 bool placementOnTarget = true; 114 bool placementRechecked = false; 115 bool showArrow = true; 116 float arrowOffsetX = 0.f; 117 float arrowOffsetY = 0.f; 118 SheetArrowPosition arrowPosition = SheetArrowPosition::NONE; 119 float sheetOffsetX = 0.f; 120 float sheetOffsetY = 0.f; 121 bool keyboardShow = false; 122 ResetSheetPopupInfo123 void Reset() 124 { 125 finalPlacement = Placement::NONE; 126 placementOnTarget = true; 127 placementRechecked = false; 128 showArrow = true; 129 arrowOffsetX = 0.f; 130 arrowOffsetY = 0.f; 131 arrowPosition = SheetArrowPosition::NONE; 132 sheetOffsetX = 0.f; 133 sheetOffsetY = 0.f; 134 keyboardShow = false; 135 } 136 }; 137 138 struct SheetKeyHash { operatorSheetKeyHash139 size_t operator()(const SheetKey& sheetKey) const 140 { 141 return sheetKey.isStartUpByUIContext ? sheetKey.contentId : sheetKey.targetId; 142 } 143 }; 144 145 enum SheetLevel { 146 OVERLAY, 147 EMBEDDED, 148 }; 149 150 enum ScrollSizeMode { 151 FOLLOW_DETENT, 152 CONTINUOUS, 153 }; 154 155 struct SheetHeight { 156 std::optional<Dimension> height; 157 std::optional<SheetMode> sheetMode; 158 159 bool operator==(const SheetHeight& sheetHeight) const 160 { 161 return (height == sheetHeight.height && sheetMode == sheetHeight.sheetMode); 162 } 163 164 bool operator!=(const SheetHeight& sheetHeight) const 165 { 166 return !(*this == sheetHeight); 167 } 168 }; 169 170 enum class SheetKeyboardAvoidMode { 171 NONE, 172 TRANSLATE_AND_RESIZE, 173 RESIZE_ONLY, 174 TRANSLATE_AND_SCROLL, 175 POPUP_SHEET, 176 }; 177 178 struct SheetStyle { 179 SheetHeight sheetHeight; 180 std::optional<bool> showDragBar; 181 std::optional<bool> enableFloatingDragBar; 182 std::optional<bool> showCloseIcon; 183 std::optional<bool> isTitleBuilder; 184 std::optional<SheetType> sheetType; 185 std::optional<Color> backgroundColor; 186 std::optional<Color> maskColor; 187 std::optional<OffsetF> bottomOffset; 188 std::optional<BlurStyleOption> backgroundBlurStyle; 189 std::optional<std::string> sheetTitle; 190 std::optional<std::string> sheetSubtitle; 191 std::vector<SheetHeight> detents; 192 std::optional<bool> interactive; 193 std::optional<bool> showInPage; 194 std::optional<ScrollSizeMode> scrollSizeMode; 195 std::optional<SheetKeyboardAvoidMode> sheetKeyboardAvoidMode; 196 std::optional<NG::BorderWidthProperty> borderWidth; // border width 197 std::optional<NG::BorderColorProperty> borderColor; // border color 198 std::optional<NG::BorderStyleProperty> borderStyle; // border style 199 std::optional<Shadow> shadow; 200 std::optional<Dimension> width; 201 std::optional<int32_t> instanceId; // uiContext instanceId 202 std::optional<bool> enableHoverMode; 203 std::optional<HoverModeAreaType> hoverModeArea; 204 std::optional<SheetEffectEdge> sheetEffectEdge; 205 std::optional<NG::BorderRadiusProperty> radius; 206 std::optional<SheetHeight> detentSelection; 207 std::optional<Placement> placement; 208 std::optional<bool> placementOnTarget; 209 std::optional<bool> showInSubWindow; 210 std::optional<ModalTransition> modalTransition; 211 212 bool operator==(const SheetStyle& sheetStyle) const 213 { 214 return (sheetHeight == sheetStyle.sheetHeight && 215 enableFloatingDragBar == sheetStyle.enableFloatingDragBar && 216 showDragBar == sheetStyle.showDragBar && showCloseIcon == sheetStyle.showCloseIcon && 217 isTitleBuilder == sheetStyle.isTitleBuilder && sheetType == sheetStyle.sheetType && 218 backgroundColor == sheetStyle.backgroundColor && maskColor == sheetStyle.maskColor && 219 detents == sheetStyle.detents && backgroundBlurStyle == sheetStyle.backgroundBlurStyle && 220 sheetTitle == sheetStyle.sheetTitle && sheetSubtitle == sheetStyle.sheetSubtitle && 221 interactive == sheetStyle.interactive && showInPage == sheetStyle.showInPage && 222 borderWidth == sheetStyle.borderWidth && borderColor == sheetStyle.borderColor && 223 borderStyle == sheetStyle.borderStyle && shadow == sheetStyle.shadow && width == sheetStyle.width && 224 instanceId == sheetStyle.instanceId && scrollSizeMode == sheetStyle.scrollSizeMode && 225 sheetKeyboardAvoidMode == sheetStyle.sheetKeyboardAvoidMode && 226 bottomOffset == sheetStyle.bottomOffset && enableHoverMode == sheetStyle.enableHoverMode && 227 hoverModeArea == sheetStyle.hoverModeArea && radius == sheetStyle.radius && 228 detentSelection == sheetStyle.detentSelection && sheetEffectEdge == sheetStyle.sheetEffectEdge && 229 placement == sheetStyle.placement && placementOnTarget == sheetStyle.placementOnTarget && 230 showInSubWindow == sheetStyle.showInSubWindow && modalTransition == sheetStyle.modalTransition); 231 } 232 PartialUpdateSheetStyle233 void PartialUpdate(const SheetStyle& sheetStyle) 234 { 235 if (sheetStyle.sheetHeight.height.has_value() && !sheetStyle.sheetHeight.sheetMode.has_value()) { 236 sheetHeight.height = sheetStyle.sheetHeight.height; 237 sheetHeight.sheetMode.reset(); 238 } else if (!sheetStyle.sheetHeight.height.has_value() && sheetStyle.sheetHeight.sheetMode.has_value()) { 239 sheetHeight.sheetMode = sheetStyle.sheetHeight.sheetMode; 240 sheetHeight.height.reset(); 241 } else { 242 sheetHeight.sheetMode = sheetStyle.sheetHeight.sheetMode.has_value() ? 243 sheetStyle.sheetHeight.sheetMode : sheetHeight.sheetMode; 244 } 245 showDragBar = sheetStyle.showDragBar.has_value() ? sheetStyle.showDragBar : showDragBar; 246 enableFloatingDragBar = sheetStyle.enableFloatingDragBar.has_value() ? 247 sheetStyle.enableFloatingDragBar : enableFloatingDragBar; 248 showCloseIcon = sheetStyle.showCloseIcon.has_value() ? sheetStyle.showCloseIcon : showCloseIcon; 249 isTitleBuilder = sheetStyle.isTitleBuilder.has_value() ? sheetStyle.isTitleBuilder : isTitleBuilder; 250 sheetType = sheetStyle.sheetType.has_value() ? sheetStyle.sheetType : sheetType; 251 backgroundColor = sheetStyle.backgroundColor.has_value() ? sheetStyle.backgroundColor : backgroundColor; 252 maskColor = sheetStyle.maskColor.has_value() ? sheetStyle.maskColor : maskColor; 253 backgroundBlurStyle = sheetStyle.backgroundBlurStyle.has_value() ? 254 sheetStyle.backgroundBlurStyle : backgroundBlurStyle; 255 sheetTitle = sheetStyle.sheetTitle.has_value() ? sheetStyle.sheetTitle : sheetTitle; 256 sheetSubtitle = sheetStyle.sheetSubtitle.has_value() ? sheetStyle.sheetSubtitle : sheetSubtitle; 257 detents = !sheetStyle.detents.empty() ? sheetStyle.detents : detents; 258 interactive = sheetStyle.interactive.has_value() ? sheetStyle.interactive : interactive; 259 scrollSizeMode = sheetStyle.scrollSizeMode.has_value() ? sheetStyle.scrollSizeMode : scrollSizeMode; 260 sheetKeyboardAvoidMode = 261 sheetStyle.sheetKeyboardAvoidMode.has_value() ? sheetStyle.sheetKeyboardAvoidMode : sheetKeyboardAvoidMode; 262 borderWidth = sheetStyle.borderWidth.has_value() ? sheetStyle.borderWidth : borderWidth; 263 borderColor = sheetStyle.borderColor.has_value() ? sheetStyle.borderColor : borderColor; 264 borderStyle = sheetStyle.borderStyle.has_value() ? sheetStyle.borderStyle : borderStyle; 265 shadow = sheetStyle.shadow.has_value() ? sheetStyle.shadow : shadow; 266 width = sheetStyle.width.has_value() ? sheetStyle.width : width; 267 bottomOffset = sheetStyle.bottomOffset.has_value() ? sheetStyle.bottomOffset : bottomOffset; 268 enableHoverMode = sheetStyle.enableHoverMode.has_value() ? sheetStyle.enableHoverMode : enableHoverMode; 269 hoverModeArea = sheetStyle.hoverModeArea.has_value() ? sheetStyle.hoverModeArea : hoverModeArea; 270 radius = sheetStyle.radius.has_value() ? sheetStyle.radius : radius; 271 detentSelection = sheetStyle.detentSelection.has_value() ? sheetStyle.detentSelection : detentSelection; 272 sheetEffectEdge = sheetStyle.sheetEffectEdge.has_value() ? sheetStyle.sheetEffectEdge : sheetEffectEdge; 273 placement = sheetStyle.placement.has_value() ? sheetStyle.placement : placement; 274 placementOnTarget = sheetStyle.placementOnTarget.has_value() ? 275 sheetStyle.placementOnTarget : placementOnTarget; 276 modalTransition = sheetStyle.modalTransition.has_value() ? sheetStyle.modalTransition : modalTransition; 277 } 278 279 // Register the set/get method of the resource. SetDetentsResObjsSheetStyle280 void SetDetentsResObjs(std::vector<RefPtr<ResourceObject>>&& resObjs) 281 { 282 detentsObj_ = std::move(resObjs); 283 } 284 GetDetentsResObjsSheetStyle285 const std::vector<RefPtr<ResourceObject>>& GetDetentsResObjs() const 286 { 287 return detentsObj_; 288 } 289 ACE_SHEET_CREATE_RESOURCE_FUNCTIONS(SheetHeight); 290 ACE_SHEET_CREATE_RESOURCE_FUNCTIONS(DetentSelection); 291 ACE_SHEET_CREATE_RESOURCE_FUNCTIONS(SheetWidth); 292 ACE_SHEET_CREATE_RESOURCE_FUNCTIONS(ShowClose); 293 ACE_SHEET_CREATE_RESOURCE_FUNCTIONS(MaskColor); 294 ACE_SHEET_CREATE_RESOURCE_FUNCTIONS(MainTitle); 295 ACE_SHEET_CREATE_RESOURCE_FUNCTIONS(SubTitle); 296 ACE_SHEET_CREATE_RESOURCE_FUNCTIONS(BorderWidth); 297 ACE_SHEET_CREATE_RESOURCE_FUNCTIONS(BorderColor); 298 ACE_SHEET_CREATE_RESOURCE_FUNCTIONS(Radius); 299 ACE_SHEET_CREATE_RESOURCE_FUNCTIONS(BackgroundColor); 300 std::vector<RefPtr<ResourceObject>> detentsObj_; 301 }; 302 } // namespace OHOS::Ace::NG 303 304 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_OVERLAY_SHEET_STYLE_H 305