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