• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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/menu/menu_item_group/menu_item_group_pattern.h"
17 
18 #include <queue>
19 
20 #include "core/components_ng/base/ui_node.h"
21 #include "core/components_ng/pattern/menu/menu_item/menu_item_pattern.h"
22 #include "core/components_ng/pattern/menu/menu_layout_property.h"
23 #include "core/components_ng/pattern/menu/menu_pattern.h"
24 #include "core/components_ng/pattern/text/text_layout_property.h"
25 #include "core/components_v2/inspector/inspector_constants.h"
26 
27 namespace OHOS::Ace::NG {
OnMountToParentDone()28 void MenuItemGroupPattern::OnMountToParentDone()
29 {
30     ModifyFontSize();
31     auto host = GetHost();
32     CHECK_NULL_VOID(host);
33     const auto& children = host->GetChildren();
34     for (const auto& child : children) {
35         if (child->GetTag() == V2::MENU_ITEM_ETS_TAG) {
36             auto itemNode = AceType::DynamicCast<FrameNode>(child);
37             CHECK_NULL_VOID(itemNode);
38             auto itemPattern = itemNode->GetPattern<MenuItemPattern>();
39             CHECK_NULL_VOID(itemPattern);
40             itemPattern->UpdateTextNodes();
41         }
42     }
43 }
44 
ModifyFontSize()45 void MenuItemGroupPattern::ModifyFontSize()
46 {
47     auto menu = GetMenu();
48     CHECK_NULL_VOID(menu);
49     auto menuProperty = menu->GetLayoutProperty<MenuLayoutProperty>();
50     CHECK_NULL_VOID(menuProperty);
51     auto menuFontSize = menuProperty->GetFontSize();
52     if (!menuFontSize.has_value()) {
53         return;
54     }
55 
56     if (headerContent_) {
57         auto headerProperty = headerContent_->GetLayoutProperty<TextLayoutProperty>();
58         CHECK_NULL_VOID(headerProperty);
59         headerProperty->UpdateFontSize(menuFontSize.value());
60         headerContent_->MarkModifyDone();
61     }
62 
63     if (footerContent_) {
64         auto footerProperty = footerContent_->GetLayoutProperty<TextLayoutProperty>();
65         CHECK_NULL_VOID(footerProperty);
66         footerProperty->UpdateFontSize(menuFontSize.value());
67         footerContent_->MarkModifyDone();
68     }
69 }
70 
AddHeader(const RefPtr<NG::UINode> & header)71 void MenuItemGroupPattern::AddHeader(const RefPtr<NG::UINode>& header)
72 {
73     auto host = GetHost();
74     CHECK_NULL_VOID(host);
75     if (headerIndex_ < 0) {
76         headerIndex_ = itemStartIndex_;
77         host->AddChild(header);
78         itemStartIndex_++;
79     } else {
80         host->ReplaceChild(host->GetChildAtIndex(headerIndex_), header);
81     }
82     auto frameNode = AceType::DynamicCast<FrameNode>(header);
83     CHECK_NULL_VOID(frameNode);
84 }
85 
AddFooter(const RefPtr<NG::UINode> & footer)86 void MenuItemGroupPattern::AddFooter(const RefPtr<NG::UINode>& footer)
87 {
88     auto host = GetHost();
89     CHECK_NULL_VOID(host);
90     if (footerIndex_ < 0) {
91         footerIndex_ = itemStartIndex_;
92         host->AddChild(footer);
93         itemStartIndex_++;
94     } else {
95         host->ReplaceChild(host->GetChildAtIndex(footerIndex_), footer);
96     }
97     auto frameNode = AceType::DynamicCast<FrameNode>(footer);
98     CHECK_NULL_VOID(frameNode);
99 }
100 
GetMenu()101 RefPtr<FrameNode> MenuItemGroupPattern::GetMenu()
102 {
103     auto host = GetHost();
104     CHECK_NULL_RETURN(host, nullptr);
105     auto parent = host->GetParent();
106     while (parent) {
107         if (parent->GetTag() == V2::MENU_ETS_TAG) {
108             return DynamicCast<FrameNode>(parent);
109         }
110         parent = parent->GetParent();
111     }
112     return nullptr;
113 }
114 
GetHeaderContent()115 std::string MenuItemGroupPattern::GetHeaderContent()
116 {
117     CHECK_NULL_RETURN(headerContent_, "");
118     auto content = headerContent_->GetLayoutProperty<TextLayoutProperty>();
119     CHECK_NULL_RETURN(content, "");
120     return content->GetContentValue("");
121 }
122 
UpdateMenuItemIconInfo()123 void MenuItemGroupPattern::UpdateMenuItemIconInfo()
124 {
125     auto host = GetHost();
126     CHECK_NULL_VOID(host);
127     std::queue<RefPtr<UINode>> nodes;
128     nodes.emplace(host);
129     while (!nodes.empty()) {
130         auto currentNode = nodes.front();
131         nodes.pop();
132         if (DynamicCast<FrameNode>(currentNode) && DynamicCast<FrameNode>(currentNode)->GetPattern<MenuItemPattern>()) {
133             auto itemPattern = DynamicCast<FrameNode>(currentNode)->GetPattern<MenuItemPattern>();
134             hasSelectIcon_ |= itemPattern->HasSelectIcon();
135             hasStartIcon_ |= itemPattern->HasStartIcon();
136         }
137         for (const auto& child : currentNode->GetChildren()) {
138             nodes.emplace(child);
139         }
140     }
141 }
142 } // namespace OHOS::Ace::NG
143