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