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 #if !defined(PREVIEW) && defined(OHOS_PLATFORM)
18 #include "interfaces/inner_api/ui_session/ui_session_manager.h"
19 #endif
20
21 #include "bridge/common/utils/utils.h"
22 #include "core/common/container.h"
23 #include "core/components/common/properties/text_style.h"
24 #include "core/components_ng/base/view_abstract_model.h"
25 #include "core/components_ng/pattern/security_component/save_button/save_button_model_ng.h"
26 #include "core/components_ng/pattern/security_component/security_component_theme.h"
27
28 using OHOS::Ace::NG::SaveButtonModelNG;
29 using OHOS::Ace::NG::SecurityComponentTheme;
30
31 namespace OHOS::Ace::Framework {
ParseComponentStyle(const JSCallbackInfo & info,SaveButtonSaveDescription & text,SaveButtonIconStyle & icon,int32_t & bg)32 bool JSSaveButton::ParseComponentStyle(const JSCallbackInfo& info,
33 SaveButtonSaveDescription& text, SaveButtonIconStyle& icon, int32_t& bg)
34 {
35 if (!info[0]->IsObject()) {
36 return false;
37 }
38
39 auto paramObject = JSRef<JSObject>::Cast(info[0]);
40 auto value = paramObject->GetProperty("text");
41 if (value->IsNumber()) {
42 text = static_cast<SaveButtonSaveDescription>(value->ToNumber<int32_t>());
43 if ((text < SaveButtonSaveDescription::DOWNLOAD) ||
44 (text > SaveButtonSaveDescription::MAX_LABEL_TYPE)) {
45 return false;
46 }
47 } else {
48 text = SaveButtonSaveDescription::TEXT_NULL;
49 }
50
51 value = paramObject->GetProperty("icon");
52 if (value->IsNumber()) {
53 icon = static_cast<SaveButtonIconStyle>(value->ToNumber<int32_t>());
54 if ((icon < SaveButtonIconStyle::ICON_FULL_FILLED) ||
55 (icon > SaveButtonIconStyle::ICON_PICTURE)) {
56 return false;
57 }
58 } else {
59 icon = SaveButtonIconStyle::ICON_NULL;
60 }
61
62 if ((text == SaveButtonSaveDescription::TEXT_NULL) &&
63 (icon == SaveButtonIconStyle::ICON_NULL)) {
64 return false;
65 }
66
67 value = paramObject->GetProperty("buttonType");
68 if (value->IsNumber()) {
69 bg = value->ToNumber<int32_t>();
70 if ((bg < static_cast<int32_t>(ButtonType::NORMAL)) ||
71 (bg > static_cast<int32_t>(ButtonType::CIRCLE))) {
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
115 int32_t res = static_cast<int32_t>(SecurityComponentHandleResult::CLICK_GRANT_FAILED);
116 #ifdef SECURITY_COMPONENT_ENABLE
117 auto secEventValue = info.GetSecCompHandleEvent();
118 if (secEventValue != nullptr) {
119 res = secEventValue->GetInt("handleRes", res);
120 if (res == static_cast<int32_t>(SecurityComponentHandleResult::DROP_CLICK)) {
121 return;
122 }
123 }
124 #endif
125 JSRef<JSVal> errorParam = JSRef<JSVal>::Make(ToJSValue(res));
126 JSRef<JSVal> params[] = { clickEventParam, errorParam };
127 JsFunction::ExecuteJS(2, params);
128 }
129
JsOnClick(const JSCallbackInfo & info)130 void JSSaveButton::JsOnClick(const JSCallbackInfo& info)
131 {
132 if (!info[0]->IsFunction()) {
133 return;
134 }
135 auto jsOnClickFunc = AceType::MakeRefPtr<JsSaveButtonClickFunction>(JSRef<JSFunc>::Cast(info[0]));
136 auto frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
137 auto onTap = [execCtx = info.GetExecutionContext(), func = jsOnClickFunc, node = frameNode](GestureEvent& info) {
138 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
139 ACE_SCORING_EVENT("onClick");
140 func->Execute(info);
141 #if !defined(PREVIEW) && defined(OHOS_PLATFORM)
142 JSInteractableView::ReportClickEvent(node);
143 #endif
144 };
145
146 NG::ViewAbstract::SetOnClick(std::move(onTap));
147 }
148
JSBind(BindingTarget globalObj)149 void JSSaveButton::JSBind(BindingTarget globalObj)
150 {
151 JSClass<JSSaveButton>::Declare("SaveButton");
152 MethodOptions opt = MethodOptions::NONE;
153 JSClass<JSSaveButton>::StaticMethod("create", &JSSaveButton::Create, opt);
154 JSClass<JSSaveButton>::StaticMethod("iconSize", &JSSecButtonBase::SetIconSize);
155 JSClass<JSSaveButton>::StaticMethod("layoutDirection", &JSSecButtonBase::SetLayoutDirection);
156 JSClass<JSSaveButton>::StaticMethod("fontSize", &JSSecButtonBase::SetFontSize);
157 JSClass<JSSaveButton>::StaticMethod("fontStyle", &JSSecButtonBase::SetFontStyle);
158 JSClass<JSSaveButton>::StaticMethod("iconColor", &JSSecButtonBase::SetIconColor);
159 JSClass<JSSaveButton>::StaticMethod("fontWeight", &JSSecButtonBase::SetFontWeight);
160 JSClass<JSSaveButton>::StaticMethod("fontFamily", &JSSecButtonBase::SetFontFamily);
161 JSClass<JSSaveButton>::StaticMethod("fontColor", &JSSecButtonBase::SetFontColor);
162 JSClass<JSSaveButton>::StaticMethod("backgroundColor", &JSSecButtonBase::SetBackgroundColor);
163 JSClass<JSSaveButton>::StaticMethod("borderStyle", &JSSecButtonBase::SetBackgroundBorderStyle);
164 JSClass<JSSaveButton>::StaticMethod("borderWidth", &JSSecButtonBase::SetBackgroundBorderWidth);
165 JSClass<JSSaveButton>::StaticMethod("borderColor", &JSSecButtonBase::SetBackgroundBorderColor);
166 JSClass<JSSaveButton>::StaticMethod("borderRadius", &JSSecButtonBase::SetBackgroundBorderRadius);
167 JSClass<JSSaveButton>::StaticMethod("padding", &JSSecButtonBase::SetBackgroundPadding);
168 JSClass<JSSaveButton>::StaticMethod("textIconSpace", &JSSecButtonBase::SetTextIconSpace);
169 JSClass<JSSaveButton>::StaticMethod("onClick", &JSSaveButton::JsOnClick);
170 JSClass<JSSaveButton>::StaticMethod("key", &JSViewAbstract::JsKey);
171 JSClass<JSSaveButton>::StaticMethod("position", &JSViewAbstract::JsPosition);
172 JSClass<JSSaveButton>::StaticMethod("markAnchor", &JSViewAbstract::JsMarkAnchor);
173 JSClass<JSSaveButton>::StaticMethod("offset", &JSViewAbstract::JsOffset);
174 JSClass<JSSaveButton>::StaticMethod("pop", &JSViewAbstract::Pop, opt);
175 JSClass<JSSaveButton>::StaticMethod("width", &JSViewAbstract::JsWidth);
176 JSClass<JSSaveButton>::StaticMethod("height", &JSViewAbstract::JsHeight);
177 JSClass<JSSaveButton>::StaticMethod("size", &JSViewAbstract::JsSize);
178 JSClass<JSSaveButton>::StaticMethod("constraintSize", &JSViewAbstract::JsConstraintSize);
179 JSClass<JSSaveButton>::StaticMethod("debugLine", &JSViewAbstract::JsDebugLine);
180 JSClass<JSSaveButton>::Bind<>(globalObj);
181 }
182 } // namespace OHOS::Ace::Framework
183