1 /*
2 * Copyright (c) 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 "frameworks/bridge/declarative_frontend/jsview/js_save_button.h"
17 #include "interfaces/inner_api/ui_session/ui_session_manager.h"
18
19 #include "bridge/common/utils/utils.h"
20 #include "core/common/container.h"
21 #include "core/components/common/properties/text_style.h"
22 #include "core/components_ng/base/view_abstract_model.h"
23 #include "core/components_ng/pattern/security_component/save_button/save_button_model_ng.h"
24 #include "core/components_ng/pattern/security_component/security_component_theme.h"
25
26 using OHOS::Ace::NG::SaveButtonModelNG;
27 using OHOS::Ace::NG::SecurityComponentTheme;
28
29 namespace OHOS::Ace::Framework {
ParseComponentStyle(const JSCallbackInfo & info,SaveButtonSaveDescription & text,SaveButtonIconStyle & icon,int32_t & bg)30 bool JSSaveButton::ParseComponentStyle(const JSCallbackInfo& info,
31 SaveButtonSaveDescription& text, SaveButtonIconStyle& icon, int32_t& bg)
32 {
33 if (!info[0]->IsObject()) {
34 return false;
35 }
36
37 auto paramObject = JSRef<JSObject>::Cast(info[0]);
38 auto value = paramObject->GetProperty("text");
39 if (value->IsNumber()) {
40 text = static_cast<SaveButtonSaveDescription>(value->ToNumber<int32_t>());
41 if ((text < SaveButtonSaveDescription::DOWNLOAD) ||
42 (text > SaveButtonSaveDescription::MAX_LABEL_TYPE)) {
43 return false;
44 }
45 } else {
46 text = SaveButtonSaveDescription::TEXT_NULL;
47 }
48
49 value = paramObject->GetProperty("icon");
50 if (value->IsNumber()) {
51 icon = static_cast<SaveButtonIconStyle>(value->ToNumber<int32_t>());
52 if ((icon < SaveButtonIconStyle::ICON_FULL_FILLED) ||
53 (icon > SaveButtonIconStyle::ICON_PICTURE)) {
54 return false;
55 }
56 } else {
57 icon = SaveButtonIconStyle::ICON_NULL;
58 }
59
60 if ((text == SaveButtonSaveDescription::TEXT_NULL) &&
61 (icon == SaveButtonIconStyle::ICON_NULL)) {
62 return false;
63 }
64
65 value = paramObject->GetProperty("buttonType");
66 if (value->IsNumber()) {
67 bg = value->ToNumber<int32_t>();
68 if ((bg != static_cast<int32_t>(ButtonType::NORMAL)) &&
69 (bg != static_cast<int32_t>(ButtonType::CIRCLE)) &&
70 (bg != static_cast<int32_t>(ButtonType::CAPSULE)) &&
71 (bg != static_cast<int32_t>(ButtonType::ROUNDED_RECTANGLE))) {
72 return false;
73 }
74 } else {
75 bg = static_cast<int32_t>(ButtonType::CAPSULE);
76 }
77 return true;
78 }
79
Create(const JSCallbackInfo & info)80 void JSSaveButton::Create(const JSCallbackInfo& info)
81 {
82 SaveButtonSaveDescription textDesc;
83 SaveButtonIconStyle iconType;
84 int32_t backgroundType = 0;
85 if (!ParseComponentStyle(info, textDesc, iconType, backgroundType)) {
86 SaveButtonModelNG::GetInstance()->Create(
87 static_cast<int32_t>(SaveButtonSaveDescription::DOWNLOAD),
88 static_cast<int32_t>(SaveButtonIconStyle::ICON_FULL_FILLED),
89 static_cast<int32_t>(ButtonType::CAPSULE), false);
90 } else {
91 SaveButtonModelNG::GetInstance()->Create(static_cast<int32_t>(textDesc),
92 static_cast<int32_t>(iconType), backgroundType, false);
93 }
94 }
95
Execute(GestureEvent & info)96 void JsSaveButtonClickFunction::Execute(GestureEvent& info)
97 {
98 JSRef<JSObject> clickEventParam = JSRef<JSObject>::New();
99 Offset globalOffset = info.GetGlobalLocation();
100 Offset localOffset = info.GetLocalLocation();
101 clickEventParam->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
102 clickEventParam->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
103 clickEventParam->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
104 clickEventParam->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
105 clickEventParam->SetProperty<double>("timestamp",
106 static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
107 clickEventParam->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
108 clickEventParam->SetProperty<double>("pressure", info.GetForce());
109 clickEventParam->SetProperty<double>("tiltX", info.GetTiltX().value_or(0.0f));
110 clickEventParam->SetProperty<double>("tiltY", info.GetTiltY().value_or(0.0f));
111 clickEventParam->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
112 auto target = CreateEventTargetObject(info);
113 clickEventParam->SetPropertyObject("target", target);
114 clickEventParam->SetProperty<int32_t>("targetDisplayId", info.GetTargetDisplayId());
115
116 int32_t res = static_cast<int32_t>(SecurityComponentHandleResult::CLICK_GRANT_FAILED);
117 JSRef<JSObject> errorMessage = JSRef<JSObject>::New();
118 #ifdef SECURITY_COMPONENT_ENABLE
119 auto secEventValue = info.GetSecCompHandleEvent();
120 if (secEventValue != nullptr) {
121 res = secEventValue->GetInt("handleRes", res);
122 int32_t code = static_cast<int32_t>(SecurityComponentErrorCode::SUCCESS);
123 std::string message;
124 if (res == static_cast<int32_t>(SecurityComponentHandleResult::DROP_CLICK)) {
125 return;
126 }
127 code = secEventValue->GetInt("code", code);
128 errorMessage->SetProperty<int32_t>("code", code);
129 message = secEventValue->GetString("message", message);
130 errorMessage->SetProperty<std::string>("message", message);
131 }
132 #endif
133 JSRef<JSVal> errorParam = JSRef<JSVal>::Make(ToJSValue(res));
134 JSRef<JSVal> params[] = { clickEventParam, errorParam, errorMessage };
135 JsFunction::ExecuteJS(3, params); // 3 means three params.
136 }
137
JsOnClick(const JSCallbackInfo & info)138 void JSSaveButton::JsOnClick(const JSCallbackInfo& info)
139 {
140 if (!info[0]->IsFunction()) {
141 return;
142 }
143 auto jsOnClickFunc = AceType::MakeRefPtr<JsSaveButtonClickFunction>(JSRef<JSFunc>::Cast(info[0]));
144 auto frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
145 auto onTap = [execCtx = info.GetExecutionContext(), func = jsOnClickFunc, node = frameNode](GestureEvent& info) {
146 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
147 ACE_SCORING_EVENT("onClick");
148 func->Execute(info);
149 #if !defined(PREVIEW) && defined(OHOS_PLATFORM)
150 JSInteractableView::ReportClickEvent(node);
151 #endif
152 };
153
154 double distanceThreshold = std::numeric_limits<double>::infinity();
155 if (info.Length() > 1 && info[1]->IsNumber()) {
156 distanceThreshold = info[1]->ToNumber<double>();
157 distanceThreshold = Dimension(distanceThreshold, DimensionUnit::VP).ConvertToPx();
158 }
159 NG::ViewAbstract::SetOnClick(std::move(onTap), distanceThreshold);
160 }
161
JSBind(BindingTarget globalObj)162 void JSSaveButton::JSBind(BindingTarget globalObj)
163 {
164 JSClass<JSSaveButton>::Declare("SaveButton");
165 MethodOptions opt = MethodOptions::NONE;
166 JSClass<JSSaveButton>::StaticMethod("create", &JSSaveButton::Create, opt);
167 JSClass<JSSaveButton>::StaticMethod("iconSize", &JSSecButtonBase::SetIconSize);
168 JSClass<JSSaveButton>::StaticMethod("layoutDirection", &JSSecButtonBase::SetLayoutDirection);
169 JSClass<JSSaveButton>::StaticMethod("fontSize", &JSSecButtonBase::SetFontSize);
170 JSClass<JSSaveButton>::StaticMethod("fontStyle", &JSSecButtonBase::SetFontStyle);
171 JSClass<JSSaveButton>::StaticMethod("iconColor", &JSSecButtonBase::SetIconColor);
172 JSClass<JSSaveButton>::StaticMethod("fontWeight", &JSSecButtonBase::SetFontWeight);
173 JSClass<JSSaveButton>::StaticMethod("fontFamily", &JSSecButtonBase::SetFontFamily);
174 JSClass<JSSaveButton>::StaticMethod("fontColor", &JSSecButtonBase::SetFontColor);
175 JSClass<JSSaveButton>::StaticMethod("backgroundColor", &JSSecButtonBase::SetBackgroundColor);
176 JSClass<JSSaveButton>::StaticMethod("borderStyle", &JSSecButtonBase::SetBackgroundBorderStyle);
177 JSClass<JSSaveButton>::StaticMethod("borderWidth", &JSSecButtonBase::SetBackgroundBorderWidth);
178 JSClass<JSSaveButton>::StaticMethod("borderColor", &JSSecButtonBase::SetBackgroundBorderColor);
179 JSClass<JSSaveButton>::StaticMethod("borderRadius", &JSSecButtonBase::SetBackgroundBorderRadius);
180 JSClass<JSSaveButton>::StaticMethod("padding", &JSSecButtonBase::SetBackgroundPadding);
181 JSClass<JSSaveButton>::StaticMethod("textIconSpace", &JSSecButtonBase::SetTextIconSpace);
182 JSClass<JSSaveButton>::StaticMethod("align", &JSSecButtonBase::SetAlign);
183 JSClass<JSSaveButton>::StaticMethod("onClick", &JSSaveButton::JsOnClick);
184 JSClass<JSSaveButton>::StaticMethod("key", &JSViewAbstract::JsKey);
185 JSClass<JSSaveButton>::StaticMethod("position", &JSViewAbstract::JsPosition);
186 JSClass<JSSaveButton>::StaticMethod("markAnchor", &JSViewAbstract::JsMarkAnchor);
187 JSClass<JSSaveButton>::StaticMethod("offset", &JSViewAbstract::JsOffset);
188 JSClass<JSSaveButton>::StaticMethod("pop", &JSViewAbstract::Pop, opt);
189 JSClass<JSSaveButton>::StaticMethod("width", &JSViewAbstract::JsWidth);
190 JSClass<JSSaveButton>::StaticMethod("height", &JSViewAbstract::JsHeight);
191 JSClass<JSSaveButton>::StaticMethod("size", &JSViewAbstract::JsSize);
192 JSClass<JSSaveButton>::StaticMethod("constraintSize", &JSViewAbstract::JsConstraintSize);
193 JSClass<JSSaveButton>::StaticMethod("debugLine", &JSViewAbstract::JsDebugLine);
194 JSClass<JSSaveButton>::StaticMethod("alignRules", &JSViewAbstract::JsAlignRules);
195 JSClass<JSSaveButton>::StaticMethod("id", &JSViewAbstract::JsId);
196 JSClass<JSSaveButton>::StaticMethod("chainMode", &JSViewAbstract::JsChainMode);
197 JSClass<JSSaveButton>::StaticMethod("maxFontScale", &JSSecButtonBase::SetMaxFontScale);
198 JSClass<JSSaveButton>::StaticMethod("minFontScale", &JSSecButtonBase::SetMinFontScale);
199 JSClass<JSSaveButton>::StaticMethod("maxLines", &JSSecButtonBase::SetMaxLines);
200 JSClass<JSSaveButton>::StaticMethod("maxFontSize", &JSSecButtonBase::SetMaxFontSize);
201 JSClass<JSSaveButton>::StaticMethod("minFontSize", &JSSecButtonBase::SetMinFontSize);
202 JSClass<JSSaveButton>::StaticMethod("heightAdaptivePolicy", &JSSecButtonBase::SetHeightAdaptivePolicy);
203 JSClass<JSSaveButton>::StaticMethod("enabled", &JSViewAbstract::JsEnabled);
204 JSClass<JSSaveButton>::Bind<>(globalObj);
205 }
206 } // namespace OHOS::Ace::Framework
207