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/side_bar/side_bar_container_view.h"
17
18 #include "base/geometry/dimension.h"
19 #include "core/components_ng/base/view_stack_processor.h"
20 #include "core/components_ng/pattern/image/image_pattern.h"
21 #include "core/components_ng/pattern/side_bar/side_bar_container_layout_property.h"
22 #include "core/components_ng/pattern/side_bar/side_bar_container_pattern.h"
23 #include "core/components_v2/inspector/inspector_constants.h"
24
25 namespace OHOS::Ace::NG {
26
Create()27 void SideBarContainerView::Create()
28 {
29 auto* stack = ViewStackProcessor::GetInstance();
30 auto nodeId = stack->ClaimNodeId();
31 auto sideBarContainerNode = FrameNode::GetOrCreateFrameNode(
32 V2::SIDE_BAR_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<SideBarContainerPattern>(); });
33
34 CHECK_NULL_VOID(sideBarContainerNode);
35
36 stack->Push(sideBarContainerNode);
37 }
38
Pop()39 void SideBarContainerView::Pop()
40 {
41 auto sideBarContainerNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
42 CHECK_NULL_VOID(sideBarContainerNode);
43
44 auto children = sideBarContainerNode->GetChildren();
45 if (children.empty()) {
46 LOGE("SideBarContainerView::Pop children is null.");
47 return;
48 }
49
50 auto pattern = sideBarContainerNode->GetPattern<SideBarContainerPattern>();
51 CHECK_NULL_VOID(pattern);
52
53 if (pattern->HasControlButton()) {
54 return;
55 }
56
57 auto sideBarNode = children.front();
58 sideBarNode->MovePosition(DEFAULT_NODE_SLOT);
59 sideBarContainerNode->RebuildRenderContextTree();
60
61 CreateAndMountControlButton(sideBarContainerNode);
62 }
63
CreateAndMountControlButton(const RefPtr<FrameNode> & parentNode)64 void SideBarContainerView::CreateAndMountControlButton(const RefPtr<FrameNode>& parentNode)
65 {
66 auto layoutProperty = parentNode->GetLayoutProperty<SideBarContainerLayoutProperty>();
67 CHECK_NULL_VOID(layoutProperty);
68 auto showSideBar = layoutProperty->GetShowSideBar().value_or(true);
69
70 ImageSourceInfo info((std::string()));
71 if (showSideBar) {
72 if (layoutProperty->GetControlButtonShowIconStr().has_value()) {
73 info.SetSrc(layoutProperty->GetControlButtonShowIconStr().value());
74 } else {
75 info.SetResourceId(InternalResource::ResourceId::SIDE_BAR);
76 }
77 } else {
78 if (layoutProperty->GetControlButtonHiddenIconStr().has_value()) {
79 info.SetSrc(layoutProperty->GetControlButtonHiddenIconStr().value());
80 } else {
81 info.SetResourceId(InternalResource::ResourceId::SIDE_BAR);
82 }
83 }
84
85 int32_t imgNodeId = ElementRegister::GetInstance()->MakeUniqueId();
86 auto imgNode = FrameNode::GetOrCreateFrameNode(
87 V2::IMAGE_ETS_TAG, imgNodeId, []() { return AceType::MakeRefPtr<ImagePattern>(); });
88
89 auto imgHub = imgNode->GetEventHub<EventHub>();
90 CHECK_NULL_VOID(imgHub);
91 auto gestureHub = imgHub->GetOrCreateGestureEventHub();
92 CHECK_NULL_VOID(gestureHub);
93 auto parentPattern = parentNode->GetPattern<SideBarContainerPattern>();
94 parentPattern->SetHasControlButton(true);
95 parentPattern->InitControlButtonTouchEvent(gestureHub);
96
97 auto imageLayoutProperty = imgNode->GetLayoutProperty<ImageLayoutProperty>();
98 CHECK_NULL_VOID(imageLayoutProperty);
99 imageLayoutProperty->UpdateImageSourceInfo(info);
100 imageLayoutProperty->UpdateImageFit(ImageFit::FILL);
101
102 imgNode->MountToParent(parentNode);
103 imgNode->MarkModifyDone();
104 }
105
SetSideBarContainerType(SideBarContainerType type)106 void SideBarContainerView::SetSideBarContainerType(SideBarContainerType type)
107 {
108 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, SideBarContainerType, type);
109 }
110
SetShowSideBar(bool isShow)111 void SideBarContainerView::SetShowSideBar(bool isShow)
112 {
113 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, ShowSideBar, isShow);
114 }
115
SetShowControlButton(bool showControlButton)116 void SideBarContainerView::SetShowControlButton(bool showControlButton)
117 {
118 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, ShowControlButton, showControlButton);
119 }
120
SetSideBarWidth(const Dimension & sideBarWidth)121 void SideBarContainerView::SetSideBarWidth(const Dimension& sideBarWidth)
122 {
123 MarkNeedInitRealSideBarWidth();
124 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, SideBarWidth, sideBarWidth);
125 }
126
SetMinSideBarWidth(const Dimension & minSideBarWidth)127 void SideBarContainerView::SetMinSideBarWidth(const Dimension& minSideBarWidth)
128 {
129 MarkNeedInitRealSideBarWidth();
130 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, MinSideBarWidth, minSideBarWidth);
131 }
132
SetMaxSideBarWidth(const Dimension & maxSideBarWidth)133 void SideBarContainerView::SetMaxSideBarWidth(const Dimension& maxSideBarWidth)
134 {
135 MarkNeedInitRealSideBarWidth();
136 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, MaxSideBarWidth, maxSideBarWidth);
137 }
138
SetAutoHide(bool autoHide)139 void SideBarContainerView::SetAutoHide(bool autoHide)
140 {
141 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, AutoHide, autoHide);
142 }
143
SetSideBarPosition(SideBarPosition sideBarPosition)144 void SideBarContainerView::SetSideBarPosition(SideBarPosition sideBarPosition)
145 {
146 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, SideBarPosition, sideBarPosition);
147 }
148
SetControlButtonWidth(const Dimension & width)149 void SideBarContainerView::SetControlButtonWidth(const Dimension& width)
150 {
151 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, ControlButtonWidth, width);
152 }
153
SetControlButtonHeight(const Dimension & height)154 void SideBarContainerView::SetControlButtonHeight(const Dimension& height)
155 {
156 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, ControlButtonHeight, height);
157 }
158
SetControlButtonLeft(const Dimension & left)159 void SideBarContainerView::SetControlButtonLeft(const Dimension& left)
160 {
161 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, ControlButtonLeft, left);
162 }
163
SetControlButtonTop(const Dimension & top)164 void SideBarContainerView::SetControlButtonTop(const Dimension& top)
165 {
166 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, ControlButtonTop, top);
167 }
168
SetControlButtonShowIconStr(const std::string & showIconStr)169 void SideBarContainerView::SetControlButtonShowIconStr(const std::string& showIconStr)
170 {
171 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, ControlButtonShowIconStr, showIconStr);
172 }
173
SetControlButtonHiddenIconStr(const std::string & hiddenIconStr)174 void SideBarContainerView::SetControlButtonHiddenIconStr(const std::string& hiddenIconStr)
175 {
176 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, ControlButtonHiddenIconStr, hiddenIconStr);
177 }
178
SetControlButtonSwitchingIconStr(const std::string & switchingIconStr)179 void SideBarContainerView::SetControlButtonSwitchingIconStr(const std::string& switchingIconStr)
180 {
181 ACE_UPDATE_LAYOUT_PROPERTY(SideBarContainerLayoutProperty, ControlButtonSwitchingIconStr, switchingIconStr);
182 }
183
SetOnChange(ChangeEvent && onChange)184 void SideBarContainerView::SetOnChange(ChangeEvent&& onChange)
185 {
186 auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
187 CHECK_NULL_VOID(frameNode);
188 auto eventHub = frameNode->GetEventHub<SideBarContainerEventHub>();
189 CHECK_NULL_VOID(eventHub);
190 eventHub->SetOnChange(std::move(onChange));
191 }
192
MarkNeedInitRealSideBarWidth()193 void SideBarContainerView::MarkNeedInitRealSideBarWidth()
194 {
195 auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
196 CHECK_NULL_VOID(frameNode);
197 auto pattern = frameNode->GetPattern<SideBarContainerPattern>();
198 CHECK_NULL_VOID(pattern);
199 pattern->MarkNeedInitRealSideBarWidth(true);
200 }
201
202 } // namespace OHOS::Ace::NG
203