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