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/nav_bar_node.h"
17
18 #include "base/memory/ace_type.h"
19 #include "base/memory/referenced.h"
20 #include "core/components_ng/base/view_stack_processor.h"
21 #include "core/components_ng/pattern/image/image_layout_property.h"
22 #include "core/components_ng/pattern/linear_layout/linear_layout_pattern.h"
23 #include "core/components_ng/pattern/navigation/bar_item_node.h"
24 #include "core/components_ng/pattern/navigation/nav_bar_layout_property.h"
25 #include "core/components_ng/pattern/navigation/navigation_declaration.h"
26 #include "core/components_ng/pattern/navigation/navigation_pattern.h"
27 #include "core/components_ng/pattern/text/text_layout_property.h"
28 #include "core/components_v2/inspector/inspector_constants.h"
29 #include "core/pipeline_ng/ui_task_scheduler.h"
30
31 namespace OHOS::Ace::NG {
32
GetOrCreateNavBarNode(const std::string & tag,int32_t nodeId,const std::function<RefPtr<Pattern> (void)> & patternCreator)33 RefPtr<NavBarNode> NavBarNode::GetOrCreateNavBarNode(
34 const std::string& tag, int32_t nodeId, const std::function<RefPtr<Pattern>(void)>& patternCreator)
35 {
36 auto frameNode = GetFrameNode(tag, nodeId);
37 CHECK_NULL_RETURN_NOLOG(!frameNode, AceType::DynamicCast<NavBarNode>(frameNode));
38 auto pattern = patternCreator ? patternCreator() : MakeRefPtr<Pattern>();
39 auto navBarNode = AceType::MakeRefPtr<NavBarNode>(tag, nodeId, pattern);
40 navBarNode->InitializePatternAndContext();
41 ElementRegister::GetInstance()->AddUINode(navBarNode);
42 return navBarNode;
43 }
44
AddChildToGroup(const RefPtr<UINode> & child,int32_t slot)45 void NavBarNode::AddChildToGroup(const RefPtr<UINode>& child, int32_t slot)
46 {
47 auto pattern = AceType::DynamicCast<NavigationPattern>(GetPattern());
48 CHECK_NULL_VOID(pattern);
49 auto contentNode = GetNavBarContentNode();
50 if (!contentNode) {
51 auto nodeId = ElementRegister::GetInstance()->MakeUniqueId();
52 contentNode = FrameNode::GetOrCreateFrameNode(
53 V2::NAVBAR_CONTENT_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<LinearLayoutPattern>(true); });
54 SetNavBarContentNode(contentNode);
55 auto layoutProperty = GetLayoutProperty<NavBarLayoutProperty>();
56 CHECK_NULL_VOID(layoutProperty);
57 AddChild(contentNode);
58 }
59 contentNode->AddChild(child);
60 }
61
GetTitleString() const62 std::string NavBarNode::GetTitleString() const
63 {
64 auto title = DynamicCast<FrameNode>(GetTitle());
65 CHECK_NULL_RETURN(title, "");
66 if (title->GetTag() != V2::TEXT_ETS_TAG) {
67 return "";
68 }
69 auto textLayoutProperty = title->GetLayoutProperty<TextLayoutProperty>();
70 CHECK_NULL_RETURN(textLayoutProperty, "");
71 return textLayoutProperty->GetContentValue("");
72 }
73
GetSubtitleString() const74 std::string NavBarNode::GetSubtitleString() const
75 {
76 auto subtitle = DynamicCast<FrameNode>(GetSubtitle());
77 CHECK_NULL_RETURN(subtitle, "");
78 if (subtitle->GetTag() != V2::TEXT_ETS_TAG) {
79 return "";
80 }
81 auto textLayoutProperty = subtitle->GetLayoutProperty<TextLayoutProperty>();
82 CHECK_NULL_RETURN(textLayoutProperty, "");
83 return textLayoutProperty->GetContentValue("");
84 }
85
GetBarItemsString(bool isMenu) const86 std::string NavBarNode::GetBarItemsString(bool isMenu) const
87 {
88 auto jsonValue = JsonUtil::Create(true);
89 auto parentNodeOfBarItems = isMenu ? DynamicCast<FrameNode>(GetMenu()) : DynamicCast<FrameNode>(GetToolBarNode());
90 CHECK_NULL_RETURN_NOLOG(parentNodeOfBarItems, "");
91 if (!parentNodeOfBarItems->GetChildren().empty()) {
92 auto jsonOptions = JsonUtil::CreateArray(true);
93 int32_t i = 0;
94 for (auto iter = parentNodeOfBarItems->GetChildren().begin(); iter != parentNodeOfBarItems->GetChildren().end();
95 ++iter, i++) {
96 auto jsonToolBarItem = JsonUtil::CreateArray(true);
97 auto barItemNode = DynamicCast<BarItemNode>(*iter);
98 if (!barItemNode) {
99 jsonToolBarItem->Put("value", "");
100 jsonToolBarItem->Put("icon", "");
101 continue;
102 }
103 auto iconNode = DynamicCast<FrameNode>(barItemNode->GetIconNode());
104 if (iconNode) {
105 auto imageLayoutProperty = iconNode->GetLayoutProperty<ImageLayoutProperty>();
106 if (!imageLayoutProperty || !imageLayoutProperty->HasImageSourceInfo()) {
107 jsonToolBarItem->Put("icon", "");
108 } else {
109 jsonToolBarItem->Put("icon", imageLayoutProperty->GetImageSourceInfoValue().GetSrc().c_str());
110 }
111 } else {
112 jsonToolBarItem->Put("icon", "");
113 }
114 auto textNode = DynamicCast<FrameNode>(barItemNode->GetTextNode());
115 if (textNode) {
116 auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
117 if (!textLayoutProperty) {
118 jsonToolBarItem->Put("value", "");
119 } else {
120 jsonToolBarItem->Put("value", textLayoutProperty->GetContentValue("").c_str());
121 }
122 } else {
123 jsonToolBarItem->Put("value", "");
124 }
125 auto index_ = std::to_string(i);
126 jsonOptions->Put(index_.c_str(), jsonToolBarItem);
127 }
128 jsonValue->Put("items", jsonOptions);
129 return jsonValue->ToString();
130 }
131 return "";
132 }
133
ToJsonValue(std::unique_ptr<JsonValue> & json) const134 void NavBarNode::ToJsonValue(std::unique_ptr<JsonValue>& json) const
135 {
136 json->Put("title", GetTitleString().c_str());
137 json->Put("subtitle", GetSubtitleString().c_str());
138 json->Put("menus", GetBarItemsString(true).c_str());
139 json->Put("toolBar", GetBarItemsString(false).c_str());
140 auto layoutProperty = GetLayoutProperty<NavBarLayoutProperty>();
141 CHECK_NULL_VOID(layoutProperty);
142 layoutProperty->ToJsonValue(json);
143 }
144
145 } // namespace OHOS::Ace::NG