• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetOrCreateTitleBarNode(const std::string & tag,int32_t nodeId,const std::function<RefPtr<Pattern> (void)> & patternCreator)28 RefPtr<TitleBarNode> TitleBarNode::GetOrCreateTitleBarNode(
29     const std::string& tag, int32_t nodeId, const std::function<RefPtr<Pattern>(void)>& patternCreator)
30 {
31     auto frameNode = GetFrameNode(tag, nodeId);
32     CHECK_NULL_RETURN_NOLOG(!frameNode, AceType::DynamicCast<TitleBarNode>(frameNode));
33     auto pattern = patternCreator ? patternCreator() : MakeRefPtr<Pattern>();
34     auto titleBarNode = AceType::MakeRefPtr<TitleBarNode>(tag, nodeId, pattern);
35     titleBarNode->InitializePatternAndContext();
36     ElementRegister::GetInstance()->AddUINode(titleBarNode);
37     return titleBarNode;
38 }
39 
40 // The function is only used for fast preview.
FastPreviewUpdateChild(int32_t slot,const RefPtr<UINode> & newChild)41 void TitleBarNode::FastPreviewUpdateChild(int32_t slot, const RefPtr<UINode>& newChild)
42 {
43     auto oldChild = GetChildAtIndex(slot);
44     if (title_ == oldChild) {
45         title_ = newChild;
46     } else if (menu_ == oldChild) {
47         menu_ = newChild;
48     }
49     UINode::FastPreviewUpdateChild(slot, newChild);
50 }
51 
52 } // namespace OHOS::Ace::NG
53