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 #include "core/components_ng/pattern/navigation/navdestination_node_base.h"
17
18 #include "base/utils/utf_helper.h"
19 #include "base/json/json_util.h"
20 #include "core/components_ng/pattern/navigation/bar_item_node.h"
21 #include "core/components_ng/pattern/image/image_layout_property.h"
22 #include "core/components_ng/pattern/text/text_layout_property.h"
23
24 namespace OHOS::Ace::NG {
GetBarItemsString(bool isMenu) const25 std::string NavDestinationNodeBase::GetBarItemsString(bool isMenu) const
26 {
27 auto jsonValue = JsonUtil::Create(true);
28 auto parentNodeOfBarItems = isMenu ? DynamicCast<FrameNode>(GetMenu()) : DynamicCast<FrameNode>(GetToolBarNode());
29 CHECK_NULL_RETURN(parentNodeOfBarItems, "");
30 if (parentNodeOfBarItems->GetChildren().empty()) {
31 return "";
32 }
33 auto jsonOptions = JsonUtil::CreateArray(true);
34 int32_t i = 0;
35 for (auto iter = parentNodeOfBarItems->GetChildren().begin(); iter != parentNodeOfBarItems->GetChildren().end();
36 ++iter, i++) {
37 auto jsonToolBarItem = JsonUtil::CreateArray(true);
38 auto barItemNode = DynamicCast<BarItemNode>(*iter);
39 if (!barItemNode) {
40 jsonToolBarItem->Put("value", "");
41 jsonToolBarItem->Put("icon", "");
42 continue;
43 }
44 auto iconNode = DynamicCast<FrameNode>(barItemNode->GetIconNode());
45 if (iconNode) {
46 auto imageLayoutProperty = iconNode->GetLayoutProperty<ImageLayoutProperty>();
47 if (!imageLayoutProperty || !imageLayoutProperty->HasImageSourceInfo()) {
48 jsonToolBarItem->Put("icon", "");
49 } else {
50 jsonToolBarItem->Put("icon", imageLayoutProperty->GetImageSourceInfoValue().GetSrc().c_str());
51 }
52 } else {
53 jsonToolBarItem->Put("icon", "");
54 }
55 auto textNode = DynamicCast<FrameNode>(barItemNode->GetTextNode());
56 if (textNode) {
57 auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
58 if (!textLayoutProperty) {
59 jsonToolBarItem->Put("value", "");
60 } else {
61 jsonToolBarItem->Put("value", UtfUtils::Str16ToStr8(textLayoutProperty->GetContentValue(u"")).c_str());
62 }
63 } else {
64 jsonToolBarItem->Put("value", "");
65 }
66 auto index_ = std::to_string(i);
67 jsonOptions->Put(index_.c_str(), jsonToolBarItem);
68 }
69 jsonValue->Put("items", jsonOptions);
70 return jsonValue->ToString();
71 }
72
IsToolBarVisible() const73 bool NavDestinationNodeBase::IsToolBarVisible() const
74 {
75 auto toolBarNode = AceType::DynamicCast<FrameNode>(GetToolBarNode());
76 CHECK_NULL_RETURN(toolBarNode, false);
77 auto layoutProperty = toolBarNode->GetLayoutProperty();
78 CHECK_NULL_RETURN(layoutProperty, false);
79 return layoutProperty->GetVisibilityValue(VisibleType::VISIBLE) == VisibleType::VISIBLE;
80 }
81 } // namespace OHOS::Ace::NG
82