1 /*
2  * Copyright (c) 2022 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/title_bar_node.h"
17 
18 #include "base/memory/ace_type.h"
19 #include "base/memory/referenced.h"
20 #include "core/components_ng/pattern/navigation/title_bar_pattern.h"
21 
22 namespace OHOS::Ace::NG {
23 
TitleBarNode(const std::string & tag,int32_t nodeId)24 TitleBarNode::TitleBarNode(const std::string& tag, int32_t nodeId)
25     : FrameNode(tag, nodeId, MakeRefPtr<TitleBarPattern>())
26 {}
27 
~TitleBarNode()28 TitleBarNode::~TitleBarNode()
29 {
30     auto pipeline = GetContextRefPtr();
31     CHECK_NULL_VOID(pipeline);
32     auto overlayManager = pipeline->GetOverlayManager();
33     CHECK_NULL_VOID(overlayManager);
34     auto titleBarPattern = GetPattern<TitleBarPattern>();
35     CHECK_NULL_VOID(titleBarPattern);
36     auto backButtonDialog = titleBarPattern->GetBackButtonDialogNode();
37     if (backButtonDialog) {
38         overlayManager->CloseDialog(backButtonDialog);
39     }
40     auto menuItemDialog = titleBarPattern->GetLargeFontPopUpDialogNode();
41     if (menuItemDialog) {
42         overlayManager->CloseDialog(menuItemDialog);
43     }
44 }
45 
GetOrCreateTitleBarNode(const std::string & tag,int32_t nodeId,const std::function<RefPtr<Pattern> (void)> & patternCreator)46 RefPtr<TitleBarNode> TitleBarNode::GetOrCreateTitleBarNode(
47     const std::string& tag, int32_t nodeId, const std::function<RefPtr<Pattern>(void)>& patternCreator)
48 {
49     auto frameNode = GetFrameNode(tag, nodeId);
50     CHECK_NULL_RETURN(!frameNode, AceType::DynamicCast<TitleBarNode>(frameNode));
51     auto pattern = patternCreator ? patternCreator() : MakeRefPtr<Pattern>();
52     auto titleBarNode = AceType::MakeRefPtr<TitleBarNode>(tag, nodeId, pattern);
53     titleBarNode->InitializePatternAndContext();
54     ElementRegister::GetInstance()->AddUINode(titleBarNode);
55     return titleBarNode;
56 }
57 
58 // The function is only used for fast preview.
FastPreviewUpdateChild(int32_t slot,const RefPtr<UINode> & newChild)59 void TitleBarNode::FastPreviewUpdateChild(int32_t slot, const RefPtr<UINode>& newChild)
60 {
61     auto oldChild = GetChildAtIndex(slot);
62     if (title_ == oldChild) {
63         title_ = newChild;
64     } else if (menu_ == oldChild) {
65         menu_ = newChild;
66     }
67     UINode::FastPreviewUpdateChild(slot, newChild);
68 }
69 
MarkIsInitialTitle(bool isInitialTitle)70 void TitleBarNode::MarkIsInitialTitle(bool isInitialTitle)
71 {
72     auto pattern = GetPattern<TitleBarPattern>();
73     pattern->MarkIsInitialTitle(isInitialTitle);
74 }
75 
76 } // namespace OHOS::Ace::NG
77