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