• 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/bar_item_layout_algorithm.h"
17 
18 #include "base/geometry/ng/offset_t.h"
19 #include "base/geometry/ng/size_t.h"
20 #include "base/memory/ace_type.h"
21 #include "base/utils/utils.h"
22 #include "core/components_ng/base/frame_node.h"
23 #include "core/components_ng/pattern/image/image_layout_property.h"
24 #include "core/components_ng/pattern/navigation/bar_item_node.h"
25 #include "core/components_ng/pattern/navigation/navigation_declaration.h"
26 #include "core/components_ng/property/layout_constraint.h"
27 #include "core/components_ng/property/measure_property.h"
28 #include "core/components_ng/property/measure_utils.h"
29 
30 namespace OHOS::Ace::NG {
31 
MeasureIcon(LayoutWrapper * layoutWrapper,const RefPtr<BarItemNode> & hostNode,const RefPtr<LayoutProperty> & barItemLayoutProperty)32 void BarItemLayoutAlgorithm::MeasureIcon(LayoutWrapper* layoutWrapper, const RefPtr<BarItemNode>& hostNode,
33     const RefPtr<LayoutProperty>& barItemLayoutProperty)
34 {
35     auto iconNode = hostNode->GetIconNode();
36     CHECK_NULL_VOID(iconNode);
37     auto index = hostNode->GetChildIndexById(iconNode->GetId());
38     auto iconWrapper = layoutWrapper->GetOrCreateChildByIndex((index));
39     CHECK_NULL_VOID(iconWrapper);
40     auto constraint = barItemLayoutProperty->CreateChildConstraint();
41 
42     constraint.selfIdealSize =
43         OptionalSizeF(static_cast<float>(iconSize_.ConvertToPx()), static_cast<float>(iconSize_.ConvertToPx()));
44     iconWrapper->Measure(constraint);
45 }
46 
MeasureText(LayoutWrapper * layoutWrapper,const RefPtr<BarItemNode> & hostNode,const RefPtr<LayoutProperty> & barItemLayoutProperty)47 void BarItemLayoutAlgorithm::MeasureText(LayoutWrapper* layoutWrapper, const RefPtr<BarItemNode>& hostNode,
48     const RefPtr<LayoutProperty>& barItemLayoutProperty)
49 {
50     auto textNode = hostNode->GetTextNode();
51     CHECK_NULL_VOID(textNode);
52     auto index = hostNode->GetChildIndexById(textNode->GetId());
53     auto textWrapper = layoutWrapper->GetOrCreateChildByIndex((index));
54     CHECK_NULL_VOID(textWrapper);
55     auto constraint = barItemLayoutProperty->CreateChildConstraint();
56     textWrapper->Measure(constraint);
57 }
58 
LayoutIcon(LayoutWrapper * layoutWrapper,const RefPtr<BarItemNode> & hostNode,const RefPtr<LayoutProperty> & barItemLayoutProperty,float textHeight)59 float BarItemLayoutAlgorithm::LayoutIcon(LayoutWrapper* layoutWrapper, const RefPtr<BarItemNode>& hostNode,
60     const RefPtr<LayoutProperty>& barItemLayoutProperty, float textHeight)
61 {
62     auto iconNode = hostNode->GetIconNode();
63     CHECK_NULL_RETURN(iconNode, 0.0f);
64     auto index = hostNode->GetChildIndexById(iconNode->GetId());
65     auto iconWrapper = layoutWrapper->GetOrCreateChildByIndex(index);
66     CHECK_NULL_RETURN(iconWrapper, 0.0f);
67     auto geometryNode = iconWrapper->GetGeometryNode();
68 
69     auto offset = OffsetF(0.0f, 0.0f);
70     geometryNode->SetMarginFrameOffset(offset);
71     iconWrapper->Layout();
72     return 0.0f;
73 }
74 
LayoutText(LayoutWrapper * layoutWrapper,const RefPtr<BarItemNode> & hostNode,const RefPtr<LayoutProperty> & barItemLayoutProperty,float iconOffsetY)75 void BarItemLayoutAlgorithm::LayoutText(LayoutWrapper* layoutWrapper, const RefPtr<BarItemNode>& hostNode,
76     const RefPtr<LayoutProperty>& barItemLayoutProperty, float iconOffsetY)
77 {
78     auto textNode = hostNode->GetTextNode();
79     CHECK_NULL_VOID(textNode);
80     auto index = hostNode->GetChildIndexById(textNode->GetId());
81     auto textWrapper = layoutWrapper->GetOrCreateChildByIndex(index);
82     CHECK_NULL_VOID(textWrapper);
83     auto geometryNode = textWrapper->GetGeometryNode();
84     auto textOffsetY = iconSize_ + TEXT_TOP_PADDING;
85     auto offset = OffsetF(0.0f, iconOffsetY + static_cast<float>(textOffsetY.ConvertToPx()));
86     geometryNode->SetMarginFrameOffset(offset);
87     textWrapper->Layout();
88 }
89 
Measure(LayoutWrapper * layoutWrapper)90 void BarItemLayoutAlgorithm::Measure(LayoutWrapper* layoutWrapper)
91 {
92     auto hostNode = AceType::DynamicCast<BarItemNode>(layoutWrapper->GetHostNode());
93     CHECK_NULL_VOID(hostNode);
94     auto barItemLayoutProperty = AceType::DynamicCast<LayoutProperty>(layoutWrapper->GetLayoutProperty());
95     CHECK_NULL_VOID(barItemLayoutProperty);
96     const auto& constraint = barItemLayoutProperty->GetLayoutConstraint();
97     CHECK_NULL_VOID(constraint);
98 
99     // get parameters from theme
100     auto theme = NavigationGetTheme();
101     CHECK_NULL_VOID(theme);
102     iconSize_ = theme->GetMenuIconSize();
103     auto size = SizeF(static_cast<float>(iconSize_.ConvertToPx()), static_cast<float>(iconSize_.ConvertToPx()));
104     MeasureIcon(layoutWrapper, hostNode, barItemLayoutProperty);
105     MeasureText(layoutWrapper, hostNode, barItemLayoutProperty);
106     layoutWrapper->GetGeometryNode()->SetFrameSize(size);
107 }
108 
Layout(LayoutWrapper * layoutWrapper)109 void BarItemLayoutAlgorithm::Layout(LayoutWrapper* layoutWrapper)
110 {
111     auto hostNode = AceType::DynamicCast<BarItemNode>(layoutWrapper->GetHostNode());
112     CHECK_NULL_VOID(hostNode);
113     auto barItemLayoutProperty = AceType::DynamicCast<LayoutProperty>(layoutWrapper->GetLayoutProperty());
114     CHECK_NULL_VOID(barItemLayoutProperty);
115 
116     float textHeight = 0.0f;
117     auto textNode = hostNode->GetTextNode();
118     if (textNode) {
119         auto index = hostNode->GetChildIndexById(textNode->GetId());
120         auto textWrapper = layoutWrapper->GetOrCreateChildByIndex(index);
121         CHECK_NULL_VOID(textWrapper);
122         auto geometryNode = textWrapper->GetGeometryNode();
123         textHeight = geometryNode->GetFrameSize().Height();
124     }
125 
126     float iconOffsetY = LayoutIcon(layoutWrapper, hostNode, barItemLayoutProperty, textHeight);
127     LayoutText(layoutWrapper, hostNode, barItemLayoutProperty, iconOffsetY);
128 }
129 
130 } // namespace OHOS::Ace::NG
131