1 /*
2 * Copyright (c) 2021-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/side_bar/side_bar_container_component.h"
17
18 #include "core/components/side_bar/render_side_bar_container.h"
19 #include "core/components/side_bar/side_bar_container_element.h"
20 #include "core/gestures/tap_gesture.h"
21 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
22
23 namespace OHOS::Ace {
24
SideBarContainerComponent(const std::list<RefPtr<Component>> & children)25 SideBarContainerComponent::SideBarContainerComponent(const std::list<RefPtr<Component>>& children)
26 : StackComponent(Alignment::CENTER_LEFT, StackFit::KEEP, Overflow::SCROLL, children)
27 {
28 declaration_ = AceType::MakeRefPtr<SideBarDeclaration>();
29 declaration_->Init();
30 }
31
CreateRenderNode()32 RefPtr<RenderNode> SideBarContainerComponent::CreateRenderNode()
33 {
34 return RenderSideBarContainer::Create();
35 }
36
CreateElement()37 RefPtr<Element> SideBarContainerComponent::CreateElement()
38 {
39 return AceType::MakeRefPtr<SideBarContainerElement>();
40 }
41
BuildButton()42 RefPtr<Component> SideBarContainerComponent::BuildButton()
43 {
44 RefPtr<ImageComponent> imageComponent = AceType::MakeRefPtr<OHOS::Ace::ImageComponent>();
45 if (sideStatus_ == SideStatus::SHOW) {
46 if (GetShowIcon().empty()) {
47 imageComponent->SetResourceId(InternalResource::ResourceId::SIDE_BAR);
48 } else {
49 imageComponent->SetSrc(GetShowIcon());
50 }
51 } else {
52 if (GetHiddenIcon().empty()) {
53 imageComponent->SetResourceId(InternalResource::ResourceId::SIDE_BAR);
54 } else {
55 imageComponent->SetSrc(GetHiddenIcon());
56 }
57 }
58 imageComponent->SetUseSkiaSvg(false);
59 imageComponent->SetImageFit(ImageFit::FILL);
60 return imageComponent;
61 }
62
Build()63 void SideBarContainerComponent::Build()
64 {
65 if (!declaration_) {
66 return;
67 }
68
69 auto children = GetChildren();
70 RefPtr<BoxComponent> barbox = AceType::MakeRefPtr<BoxComponent>();
71
72 RefPtr<Component>& sidebar = children.front();
73 barbox->SetChild(sidebar);
74
75 RefPtr<BoxComponent> contentbox = AceType::MakeRefPtr<BoxComponent>();
76 RefPtr<Component>& content = children.back();
77 contentbox->SetChild(content);
78
79 auto tapGesture = AceType::MakeRefPtr<TapGesture>();
80 tapGesture->SetOnActionId([weak = WeakClaim(this)](GestureEvent& info) {
81 auto component = weak.Upgrade();
82 if (component && component->buttonClick_) {
83 component->buttonClick_();
84 }
85 });
86
87 RefPtr<BoxComponent> btnbox = AceType::MakeRefPtr<BoxComponent>();
88 btnbox->SetOnClick(tapGesture);
89 btnbox->SetChild(BuildButton());
90 btnbox->SetWidth(Dimension(declaration_->GetImageWidth(), DimensionUnit::VP));
91 btnbox->SetHeight(Dimension(declaration_->GetImageHeight(), DimensionUnit::VP));
92 btnbox->SetFlex(BoxFlex::FLEX_XY);
93 btnbox->SetEnableDebugBoundary(true);
94 RefPtr<DisplayComponent> displayBtn = AceType::MakeRefPtr<DisplayComponent>(btnbox);
95 displayBtn->SetLeft(Dimension(declaration_->GetLeft(), DimensionUnit::VP));
96 displayBtn->SetTop(Dimension(declaration_->GetTop(), DimensionUnit::VP));
97 displayBtn->SetPositionType(PositionType::PTABSOLUTE);
98 if (!GetShowControlButton()) {
99 displayBtn->SetVisible(VisibleType::GONE);
100 } else {
101 displayBtn->SetVisible(VisibleType::VISIBLE);
102 }
103
104 ClearChildren();
105 AppendChild(contentbox);
106 AppendChild(barbox);
107 #if !defined(PREVIEW)
108 auto btnComposed = AceType::MakeRefPtr<V2::InspectorComposedComponent>(
109 V2::InspectorComposedComponent::GenerateId(), V2::IMAGE_COMPONENT_TAG, displayBtn);
110 AppendChild(btnComposed);
111 #else
112 AppendChild(displayBtn);
113 #endif
114 }
115
SetButtonWidth(double width)116 void SideBarContainerComponent::SetButtonWidth(double width)
117 {
118 declaration_->SetImageWidth(width);
119 }
120
SetButtonHeight(double height)121 void SideBarContainerComponent::SetButtonHeight(double height)
122 {
123 declaration_->SetImageHeight(height);
124 }
125
GetButtonWidth() const126 double SideBarContainerComponent::GetButtonWidth() const
127 {
128 return declaration_->GetImageWidth();
129 }
130
GetButtonHeight() const131 double SideBarContainerComponent::GetButtonHeight() const
132 {
133 return declaration_->GetImageHeight();
134 }
135
GetButtonTop() const136 double SideBarContainerComponent::GetButtonTop() const
137 {
138 return declaration_->GetTop();
139 }
140
GetButtonLeft() const141 double SideBarContainerComponent::GetButtonLeft() const
142 {
143 return declaration_->GetLeft();
144 }
145
SetShowIcon(const std::string & path)146 void SideBarContainerComponent::SetShowIcon(const std::string& path)
147 {
148 declaration_->SetShowIcon(path);
149 }
150
GetShowIcon() const151 std::string& SideBarContainerComponent::GetShowIcon() const
152 {
153 return declaration_->GetShowIcon();
154 }
155
SetHiddenIcon(const std::string & path)156 void SideBarContainerComponent::SetHiddenIcon(const std::string& path)
157 {
158 declaration_->SetHiddenIcon(path);
159 }
160
GetHiddenIcon() const161 std::string& SideBarContainerComponent::GetHiddenIcon() const
162 {
163 return declaration_->GetHiddenIcon();
164 }
165
SetSwitchIcon(const std::string & path)166 void SideBarContainerComponent::SetSwitchIcon(const std::string& path)
167 {
168 declaration_->SetSwitchIcon(path);
169 }
170
GetSwitchIcon() const171 std::string& SideBarContainerComponent::GetSwitchIcon() const
172 {
173 return declaration_->GetSwitchIcon();
174 }
175
SetButtonLeft(double left)176 void SideBarContainerComponent::SetButtonLeft(double left)
177 {
178 declaration_->SetLeft(left);
179 }
180
SetButtonTop(double top)181 void SideBarContainerComponent::SetButtonTop(double top)
182 {
183 declaration_->SetTop(top);
184 }
185
SetClickedFunction(std::function<void ()> && clickCallback)186 void SideBarContainerComponent::SetClickedFunction(std::function<void()>&& clickCallback)
187 {
188 buttonClick_ = std::move(clickCallback);
189 }
190 } // namespace OHOS::Ace