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_TOAST_TOAST_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TOAST_TOAST_PATTERN_H 18 19 #include <cstdint> 20 21 #include "base/geometry/ng/offset_t.h" 22 #include "base/geometry/ng/size_t.h" 23 #include "base/memory/ace_type.h" 24 #include "base/memory/referenced.h" 25 #include "core/components/toast/toast_theme.h" 26 #include "core/components_ng/pattern/overlay/popup_base_pattern.h" 27 #include "core/components_ng/pattern/pattern.h" 28 #include "core/components_ng/pattern/toast/toast_accessibility_property.h" 29 #include "core/components_ng/pattern/text/text_layout_property.h" 30 #include "core/components_ng/pattern/toast/toast_layout_algorithm.h" 31 #include "core/components_ng/pattern/toast/toast_layout_property.h" 32 33 namespace OHOS::Ace::NG { 34 class ToastPattern : public PopupBasePattern { 35 DECLARE_ACE_TYPE(ToastPattern, PopupBasePattern); 36 37 public: 38 ToastPattern() = default; 39 ~ToastPattern() override = default; 40 IsAtomicNode()41 bool IsAtomicNode() const override 42 { 43 return false; 44 } 45 CreateAccessibilityProperty()46 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 47 { 48 return MakeRefPtr<ToastAccessibilityProperty>(); 49 } 50 CreateLayoutProperty()51 RefPtr<LayoutProperty> CreateLayoutProperty() override 52 { 53 return AceType::MakeRefPtr<ToastLayoutProperty>(); 54 } 55 CreateLayoutAlgorithm()56 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 57 { 58 return AceType::MakeRefPtr<ToastLayoutAlgorithm>(); 59 } 60 61 void InitWrapperRect(LayoutWrapper* layoutWrapper, const RefPtr<ToastLayoutProperty>& toastProps); 62 63 void OnAttachToFrameNode() override; 64 65 void OnDetachFromFrameNode(FrameNode* node) override; 66 67 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& changeConfig) override; 68 69 void OnColorConfigurationUpdate() override; 70 71 void DumpInfo() override; 72 void DumpInfo(std::unique_ptr<JsonValue>& json) override; DumpSimplifyInfo(std::shared_ptr<JsonValue> & json)73 void DumpSimplifyInfo(std::shared_ptr<JsonValue>& json) override {} 74 SetTextNode(RefPtr<FrameNode> textNode)75 void SetTextNode(RefPtr<FrameNode> textNode) 76 { 77 textNode_ = textNode; 78 } 79 IsDefaultToast()80 bool IsDefaultToast() const 81 { 82 auto layoutProp = GetLayoutProperty<ToastLayoutProperty>(); 83 CHECK_NULL_RETURN(layoutProp, false); 84 auto showMode = layoutProp->GetShowModeValue(ToastShowMode::DEFAULT); 85 return showMode == ToastShowMode::DEFAULT; 86 } 87 IsSystemTopMost()88 bool IsSystemTopMost() const 89 { 90 auto layoutProp = GetLayoutProperty<ToastLayoutProperty>(); 91 CHECK_NULL_RETURN(layoutProp, false); 92 auto showMode = layoutProp->GetShowModeValue(ToastShowMode::DEFAULT); 93 return showMode == ToastShowMode::SYSTEM_TOP_MOST; 94 } 95 IsTopMostToast()96 bool IsTopMostToast() const 97 { 98 auto layoutProp = GetLayoutProperty<ToastLayoutProperty>(); 99 CHECK_NULL_RETURN(layoutProp, false); 100 auto showMode = layoutProp->GetShowModeValue(ToastShowMode::DEFAULT); 101 return showMode == ToastShowMode::TOP_MOST; 102 } 103 AvoidKeyboard()104 bool AvoidKeyboard() const override 105 { 106 return IsDefaultToast(); 107 } 108 UpdateFoldDisplayModeChangedCallbackId(std::optional<int32_t> id)109 void UpdateFoldDisplayModeChangedCallbackId(std::optional<int32_t> id) 110 { 111 foldDisplayModeChangedCallbackId_ = id; 112 } 113 HasFoldDisplayModeChangedCallbackId()114 bool HasFoldDisplayModeChangedCallbackId() 115 { 116 return foldDisplayModeChangedCallbackId_.has_value(); 117 } 118 UpdateHalfFoldHoverChangedCallbackId(std::optional<int32_t> id)119 void UpdateHalfFoldHoverChangedCallbackId(std::optional<int32_t> id) 120 { 121 halfFoldHoverChangedCallbackId_ = id; 122 } 123 HasHalfFoldHoverChangedCallbackId()124 bool HasHalfFoldHoverChangedCallbackId() 125 { 126 return halfFoldHoverChangedCallbackId_.has_value(); 127 } 128 SetToastInfo(const ToastInfo & toastInfo)129 void SetToastInfo(const ToastInfo& toastInfo) 130 { 131 toastInfo_ = toastInfo; 132 } 133 GetToastInfo()134 ToastInfo& GetToastInfo() 135 { 136 return toastInfo_; 137 } 138 139 bool IsShowInFreeMultiWindow() const; 140 141 bool IsUIExtensionSubWindow() const; 142 IsAlignedWithHostWindow()143 bool IsAlignedWithHostWindow() const 144 { 145 return IsUIExtensionSubWindow() && IsTopMostToast(); 146 } 147 148 void InitUIExtensionHostWindowRect(); 149 GetUiExtensionHostWindowRect()150 Rect GetUiExtensionHostWindowRect() const 151 { 152 return uiExtensionHostWindowRect_; 153 } GetLimitPos()154 Dimension GetLimitPos() const 155 { 156 return limitPos_; 157 } 158 RefPtr<PipelineContext> GetToastContext(); 159 private: 160 void BeforeCreateLayoutWrapper() override; 161 void UpdateToastSize(const RefPtr<FrameNode>& toast); 162 void UpdateTextSizeConstraint(const RefPtr<FrameNode>& text); 163 void FoldStatusChangedAnimation(); 164 void UpdateHoverModeRect(const RefPtr<ToastLayoutProperty>& toastProps, 165 const RefPtr<SafeAreaManager>& safeAreaManager, float safeAreaTop, float safeAreaBottom); 166 Dimension GetOffsetX(const RefPtr<LayoutWrapper>& layoutWrapper); 167 Dimension GetOffsetY(const RefPtr<LayoutWrapper>& layoutWrapper); 168 void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override; 169 170 double GetBottomValue(const RefPtr<LayoutWrapper>& layoutWrapper); 171 double GetTextMaxHeight(); 172 double GetTextMaxWidth(); 173 int32_t GetTextLineHeight(const RefPtr<FrameNode>& textNode); 174 175 void AdjustOffsetForKeyboard(Dimension& offsetY, double toastBottom, float textHeight, bool& needResizeBottom); 176 NG::SizeF GetSystemTopMostSubwindowSize() const; 177 178 RefPtr<FrameNode> textNode_; 179 std::optional<int32_t> foldDisplayModeChangedCallbackId_; 180 std::optional<int32_t> halfFoldHoverChangedCallbackId_; 181 ToastInfo toastInfo_; 182 ACE_DISALLOW_COPY_AND_MOVE(ToastPattern); 183 Dimension defaultBottom_; 184 Rect wrapperRect_; 185 bool isHoverMode_ = false; 186 bool expandDisplay_ = false; 187 Rect uiExtensionHostWindowRect_; 188 Dimension limitPos_; 189 int32_t rowKeyboardCallbackId_ = -1; 190 }; 191 } // namespace OHOS::Ace::NG 192 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TOAST_TOAST_PATTERN_H 193