1 /* 2 * Copyright (c) 2024 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_WRAPPER_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_OVERLAY_SHEET_WRAPPER_PATTERN_H 18 19 #include "core/common/ace_engine.h" 20 #include "core/components_ng/pattern/navrouter/navdestination_pattern.h" 21 #include "core/components_ng/pattern/overlay/popup_base_pattern.h" 22 #include "core/components_ng/pattern/overlay/sheet_presentation_pattern.h" 23 #include "core/components_ng/pattern/overlay/sheet_wrapper_layout_algorithm.h" 24 #include "core/components_ng/pattern/overlay/sheet_wrapper_paint_method.h" 25 26 namespace OHOS::Ace::NG { 27 namespace { 28 constexpr int32_t INVALID_SUBWINDOW_ID = -1; 29 } 30 class SheetWrapperPattern : virtual public PopupBasePattern { 31 DECLARE_ACE_TYPE(SheetWrapperPattern, PopupBasePattern); 32 33 public: 34 SheetWrapperPattern() = default; 35 SheetWrapperPattern(int32_t targetId,const std::string & targetTag)36 SheetWrapperPattern(int32_t targetId, const std::string& targetTag) : targetId_(targetId), targetTag_(targetTag) {} 37 38 ~SheetWrapperPattern() override = default; 39 GetFocusPattern()40 FocusPattern GetFocusPattern() const override 41 { 42 return { FocusType::SCOPE, true }; 43 } 44 CreateLayoutAlgorithm()45 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 46 { 47 return MakeRefPtr<SheetWrapperLayoutAlgorithm>(); 48 } 49 OnAttachToMainTree()50 void OnAttachToMainTree() override 51 { 52 auto host = GetHost(); 53 CHECK_NULL_VOID(host); 54 auto rootNode = AceType::DynamicCast<FrameNode>(host->GetParent()); 55 CHECK_NULL_VOID(rootNode); 56 if (rootNode->GetTag() != V2::NAVDESTINATION_VIEW_ETS_TAG) { 57 return; 58 } 59 auto wrapperRenderContext = host->GetRenderContext(); 60 CHECK_NULL_VOID(wrapperRenderContext); 61 auto navDestinationPattern = rootNode->GetPattern<NavDestinationPattern>(); 62 CHECK_NULL_VOID(navDestinationPattern); 63 auto zIndex = navDestinationPattern->GetTitlebarZIndex(); 64 65 // set the sheet to float on the NavDestination's titlebar when the sheet shows in NavDestination 66 wrapperRenderContext->UpdateZIndex(zIndex + 1); 67 } 68 CreateNodePaintMethod()69 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 70 { 71 return MakeRefPtr<SheetWrapperPaintMethod>(); 72 } 73 GetTargetNode()74 RefPtr<FrameNode> GetTargetNode() const 75 { 76 return FrameNode::GetFrameNode(targetTag_, targetId_); 77 } 78 GetSheetPageNode()79 RefPtr<FrameNode> GetSheetPageNode() const 80 { 81 return sheetPageNode_; 82 } 83 GetSheetMaskNode()84 RefPtr<FrameNode> GetSheetMaskNode() const 85 { 86 return sheetMaskNode_; 87 } 88 SetSheetPageNode(RefPtr<FrameNode> node)89 void SetSheetPageNode(RefPtr<FrameNode> node) 90 { 91 sheetPageNode_ = node; 92 } 93 SetSheetMaskNode(RefPtr<FrameNode> node)94 void SetSheetMaskNode(RefPtr<FrameNode> node) 95 { 96 sheetMaskNode_ = node; 97 } 98 InitSubWindowId()99 void InitSubWindowId() 100 { 101 auto container = Container::Current(); 102 CHECK_NULL_VOID(container); 103 if (!container->IsSubContainer()) { 104 return; 105 } 106 subWindowId_ = Container::CurrentId(); 107 TAG_LOGI(AceLogTag::ACE_SHEET, "sheet wrapper open in subwindow id is %u", subWindowId_); 108 auto currentId = SubwindowManager::GetInstance()->GetParentContainerId(subWindowId_); 109 container = AceEngine::Get().GetContainer(currentId); 110 CHECK_NULL_VOID(container); 111 if (container->IsUIExtensionWindow()) { 112 TAG_LOGI(AceLogTag::ACE_SHEET, "sheet host window is UEC"); 113 isShowInUEC_ = true; 114 } 115 } 116 InitMainWindowRect(int32_t subwindowId)117 void InitMainWindowRect(int32_t subwindowId) 118 { 119 if (subWindowId_ == INVALID_SUBWINDOW_ID) { 120 return; 121 } 122 auto instanceId = SubwindowManager::GetInstance()->GetParentContainerId(subwindowId); 123 auto mainWindowContext = PipelineContext::GetContextByContainerId(instanceId); 124 CHECK_NULL_VOID(mainWindowContext); 125 auto windowGlobalRect = mainWindowContext->GetDisplayWindowRectInfo(); 126 mainWindowRect_ = RectF(windowGlobalRect.Left(), windowGlobalRect.Top(), 127 windowGlobalRect.Width(), windowGlobalRect.Height()); 128 if (isShowInUEC_) { 129 auto subwindow = SubwindowManager::GetInstance()->GetSubwindowByType(subWindowId_, 130 SubwindowType::TYPE_SHEET); 131 CHECK_NULL_VOID(subwindow); 132 auto rect = subwindow->GetUIExtensionHostWindowRect(); 133 mainWindowRect_ = RectF(rect.Left(), rect.Top(), rect.Width(), rect.Height()); 134 } 135 } 136 OnAttachToFrameNode()137 void OnAttachToFrameNode() override 138 { 139 auto host = GetHost(); 140 CHECK_NULL_VOID(host); 141 auto maskLayoutProps = host->GetLayoutProperty(); 142 CHECK_NULL_VOID(maskLayoutProps); 143 maskLayoutProps->UpdateMeasureType(MeasureType::MATCH_PARENT); 144 maskLayoutProps->UpdateAlignment(Alignment::TOP_LEFT); 145 auto maskRenderContext = host->GetRenderContext(); 146 CHECK_NULL_VOID(maskRenderContext); 147 maskRenderContext->UpdateClipEdge(true); 148 InitSubWindowId(); 149 InitMainWindowRect(subWindowId_); 150 } 151 GetMainWindowRect()152 RectF GetMainWindowRect() const 153 { 154 return mainWindowRect_; 155 } 156 ShowInUEC()157 bool ShowInUEC() const 158 { 159 return isShowInUEC_; 160 } 161 GetSubWindowId()162 int32_t GetSubWindowId() const 163 { 164 return subWindowId_; 165 } 166 167 protected: AvoidKeyboard()168 bool AvoidKeyboard() const override 169 { 170 return false; 171 } 172 AvoidBottom()173 bool AvoidBottom() const override 174 { 175 return false; 176 } 177 178 private: 179 int32_t targetId_ = -1; 180 std::string targetTag_; 181 182 RefPtr<FrameNode> sheetPageNode_; 183 RefPtr<FrameNode> sheetMaskNode_; 184 RectF mainWindowRect_; 185 bool isShowInUEC_ = false; 186 int32_t subWindowId_ = -1; 187 ACE_DISALLOW_COPY_AND_MOVE(SheetWrapperPattern); 188 }; 189 } // namespace OHOS::Ace::NG 190 191 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_OVERLAY_SHEET_WRAPPER_PATTERN_H 192