1 /*
2 * Copyright (c) 2024 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 "bridge/cj_frontend/interfaces/cj_ffi/cj_panel_ffi.h"
17
18 #include "cj_lambda.h"
19 #include "bridge/cj_frontend/interfaces/cj_ffi/utils.h"
20 #include "core/components/panel/sliding_events.h"
21 #include "core/components_ng/base/view_abstract.h"
22 #include "core/components_ng/base/view_abstract_model_ng.h"
23 #include "core/components_ng/base/view_stack_processor.h"
24 #include "core/components_ng/pattern/panel/sliding_panel_model_ng.h"
25
26 using namespace OHOS::Ace;
27 using namespace OHOS::Ace::Framework;
28
29 namespace {
30 const std::vector<BorderStyle> BORDER_STYLES = { BorderStyle::SOLID, BorderStyle::DASHED, BorderStyle::DOTTED };
31 const std::vector<PanelMode> PANEL_MODES = { PanelMode::MINI, PanelMode::HALF, PanelMode::FULL, PanelMode::AUTO };
32 const std::vector<PanelType> PANEL_TYPES = { PanelType::MINI_BAR, PanelType::FOLDABLE_BAR, PanelType::TEMP_DISPLAY };
33 const std::vector<VisibleType> PANEL_VISIBLE_TYPES = { VisibleType::GONE, VisibleType::VISIBLE,
34 VisibleType::INVISIBLE };
35 } // namespace
36
37 extern "C" {
FfiOHOSAceFrameworkPanelCreate(bool isShow)38 void FfiOHOSAceFrameworkPanelCreate(bool isShow)
39 {
40 SlidingPanelModel::GetInstance()->Create(isShow);
41 }
42
FfiOHOSAceFrameworkPanelType(int32_t type)43 void FfiOHOSAceFrameworkPanelType(int32_t type)
44 {
45 if (!Utils::CheckParamsValid(type, PANEL_TYPES.size())) {
46 LOGE("invalid value for panel type");
47 return;
48 }
49 SlidingPanelModel::GetInstance()->SetPanelType(PANEL_TYPES[type]);
50 }
51
FfiOHOSAceFrameworkPanelMode(int32_t mode)52 void FfiOHOSAceFrameworkPanelMode(int32_t mode)
53 {
54 if (!Utils::CheckParamsValid(mode, PANEL_MODES.size())) {
55 LOGE("invalid value for panel mode");
56 return;
57 }
58 SlidingPanelModel::GetInstance()->SetPanelMode(PANEL_MODES[mode]);
59 }
60
FfiOHOSAceFrameworkPanelDragBar(bool val)61 void FfiOHOSAceFrameworkPanelDragBar(bool val)
62 {
63 SlidingPanelModel::GetInstance()->SetHasDragBar(val);
64 }
65
FfiOHOSAceFrameworkPanelFullHeight(double height,int32_t unit)66 void FfiOHOSAceFrameworkPanelFullHeight(double height, int32_t unit)
67 {
68 Dimension fullHeight = Dimension(height, static_cast<DimensionUnit>(unit));
69 SlidingPanelModel::GetInstance()->SetFullHeight(fullHeight);
70 }
FfiOHOSAceFrameworkPanelHalfHeight(double height,int32_t unit)71 void FfiOHOSAceFrameworkPanelHalfHeight(double height, int32_t unit)
72 {
73 Dimension halfHeight = Dimension(height, static_cast<DimensionUnit>(unit));
74 SlidingPanelModel::GetInstance()->SetHalfHeight(halfHeight);
75 }
FfiOHOSAceFrameworkPanelMiniHeight(double height,int32_t unit)76 void FfiOHOSAceFrameworkPanelMiniHeight(double height, int32_t unit)
77 {
78 Dimension minHeight = Dimension(height, static_cast<DimensionUnit>(unit));
79 SlidingPanelModel::GetInstance()->SetMiniHeight(minHeight);
80 }
81
FfiOHOSAceFrameworkPanelShow(bool isShow)82 void FfiOHOSAceFrameworkPanelShow(bool isShow)
83 {
84 SlidingPanelModel::GetInstance()->SetIsShow(isShow);
85 }
86
FfiOHOSAceFrameworkPanelShowCloseIcon(bool showCloseIcon)87 void FfiOHOSAceFrameworkPanelShowCloseIcon(bool showCloseIcon)
88 {
89 SlidingPanelModel::GetInstance()->SetShowCloseIcon(showCloseIcon);
90 }
91
FfiOHOSAceFrameworkPanelBackgroundMask(uint32_t color)92 void FfiOHOSAceFrameworkPanelBackgroundMask(uint32_t color)
93 {
94 SlidingPanelModel::GetInstance()->SetBackgroundMask(Color(color));
95 }
96
FfiOHOSAceFrameworkPanelOnChange(void (* callback)(double width,double height,int32_t mode))97 void FfiOHOSAceFrameworkPanelOnChange(void (*callback)(double width, double height, int32_t mode))
98 {
99 auto onSizeChangeNG = [ffiCallback = CJLambda::Create(callback)](const BaseEventInfo* info) {
100 auto eventInfo = TypeInfoHelper::DynamicCast<SlidingPanelSizeChangeEvent>(info);
101 if (!eventInfo) {
102 return;
103 }
104 auto height = eventInfo->GetHeight();
105 auto width = eventInfo->GetWidth();
106 auto mode = static_cast<int32_t>(eventInfo->GetMode());
107 ffiCallback(width, height, mode);
108 };
109 SlidingPanelModel::GetInstance()->SetOnSizeChange(std::move(onSizeChangeNG));
110 }
111
FfiOHOSAceFrameworkPanelOnHeightChange(void (* callback)(double height))112 void FfiOHOSAceFrameworkPanelOnHeightChange(void (*callback)(double height))
113 {
114 auto onHeightChangeNG = [ffiCallback = CJLambda::Create(callback)](double height) { ffiCallback(height); };
115 SlidingPanelModel::GetInstance()->SetOnHeightChange(std::move(onHeightChangeNG));
116 }
117
FfiOHOSAceFrameworkPanelPop()118 void FfiOHOSAceFrameworkPanelPop()
119 {
120 SlidingPanelModel::GetInstance()->Pop();
121 }
122
FfiOHOSAceFrameworkPanelBackgroundColor(uint32_t color)123 void FfiOHOSAceFrameworkPanelBackgroundColor(uint32_t color)
124 {
125 SlidingPanelModel::GetInstance()->SetBackgroundColor(Color(color));
126 }
FfiOHOSAceFrameworkPanelBorder(CJBorder params)127 void FfiOHOSAceFrameworkPanelBorder(CJBorder params)
128 {
129 Dimension widthDime(params.width, static_cast<DimensionUnit>(params.widthUnit));
130 Dimension radiusDime(params.radius, static_cast<DimensionUnit>(params.radiusUnit));
131 if (!Utils::CheckParamsValid(params.style, BORDER_STYLES.size())) {
132 LOGE("invalid value for border style");
133 return;
134 }
135 SlidingPanelModel::GetInstance()->SetBorderWidth(widthDime);
136 SlidingPanelModel::GetInstance()->SetBorderStyle(BORDER_STYLES[params.style]);
137 SlidingPanelModel::GetInstance()->SetBorderColor(Color(params.color));
138 ViewAbstractModel::GetInstance()->SetBorderRadius(radiusDime);
139 }
FfiOHOSAceFrameworkPanelBorderWidth(double width,int32_t unit)140 void FfiOHOSAceFrameworkPanelBorderWidth(double width, int32_t unit)
141 {
142 SlidingPanelModel::GetInstance()->SetBorderWidth(Dimension(width, static_cast<DimensionUnit>(unit)));
143 }
FfiOHOSAceFrameworkPanelBorderColor(uint32_t color)144 void FfiOHOSAceFrameworkPanelBorderColor(uint32_t color)
145 {
146 SlidingPanelModel::GetInstance()->SetBorderColor(Color(color));
147 }
FfiOHOSAceFrameworkPanelBorderRadius(double radius,int32_t unit)148 void FfiOHOSAceFrameworkPanelBorderRadius(double radius, int32_t unit)
149 {
150 ViewAbstractModel::GetInstance()->SetBorderRadius(Dimension(radius, static_cast<DimensionUnit>(unit)));
151 }
FfiOHOSAceFrameworkPanelBorderStyle(int32_t style)152 void FfiOHOSAceFrameworkPanelBorderStyle(int32_t style)
153 {
154 if (!Utils::CheckParamsValid(style, BORDER_STYLES.size())) {
155 LOGE("invalid value for border style");
156 return;
157 }
158 SlidingPanelModel::GetInstance()->SetBorderStyle(BORDER_STYLES[style]);
159 }
160 }
161