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 #include "core/components_ng/pattern/navigation/tool_bar_node.h"
17 
18 #include "base/memory/ace_type.h"
19 #include "base/memory/referenced.h"
20 #include "core/components_ng/pattern/navigation/tool_bar_pattern.h"
21 
22 namespace OHOS::Ace::NG {
NavToolbarNode(const std::string & tag,int32_t nodeId)23 NavToolbarNode::NavToolbarNode(const std::string& tag, int32_t nodeId)
24     : FrameNode(tag, nodeId, MakeRefPtr<NavToolbarPattern>())
25 {}
26 
~NavToolbarNode()27 NavToolbarNode::~NavToolbarNode()
28 {
29     auto pipeline = GetContextRefPtr();
30     CHECK_NULL_VOID(pipeline);
31     auto overlayManager = pipeline->GetOverlayManager();
32     CHECK_NULL_VOID(overlayManager);
33     auto toolBarPattern = GetPattern<NavToolbarPattern>();
34     CHECK_NULL_VOID(toolBarPattern);
35     auto toolBarDialog = toolBarPattern->GetDialogNode();
36     if (toolBarDialog) {
37         overlayManager->CloseDialog(toolBarDialog);
38     }
39 }
40 
GetOrCreateToolbarNode(const std::string & tag,int32_t nodeId,const std::function<RefPtr<Pattern> (void)> & patternCreator)41 RefPtr<NavToolbarNode> NavToolbarNode::GetOrCreateToolbarNode(
42     const std::string& tag, int32_t nodeId, const std::function<RefPtr<Pattern>(void)>& patternCreator)
43 {
44     auto frameNode = GetFrameNode(tag, nodeId);
45     CHECK_NULL_RETURN(!frameNode, AceType::DynamicCast<NavToolbarNode>(frameNode));
46     auto pattern = patternCreator ? patternCreator() : MakeRefPtr<Pattern>();
47     auto toolbarNode = AceType::MakeRefPtr<NavToolbarNode>(tag, nodeId, pattern);
48     toolbarNode->InitializePatternAndContext();
49     ElementRegister::GetInstance()->AddUINode(toolbarNode);
50     return toolbarNode;
51 }
52 } // namespace OHOS::Ace::NG
53