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
18 #include "bridge/common/utils/utils.h"
19 #include "core/common/container.h"
20 #include "core/components/common/properties/text_style.h"
21 #include "core/components_ng/base/view_abstract_model.h"
22 #include "core/components_ng/pattern/security_component/paste_button/paste_button_model_ng.h"
23 #include "core/components_ng/pattern/security_component/security_component_theme.h"
24
25 using OHOS::Ace::NG::PasteButtonModelNG;
26 using OHOS::Ace::NG::SecurityComponentTheme;
27
28 namespace OHOS::Ace::Framework {
ParseComponentStyle(const JSCallbackInfo & info,PasteButtonPasteDescription & text,PasteButtonIconStyle & icon,int32_t & bg)29 bool JSPasteButton::ParseComponentStyle(const JSCallbackInfo& info,
30 PasteButtonPasteDescription& text, PasteButtonIconStyle& icon, int32_t& bg)
31 {
32 if (!info[0]->IsObject()) {
33 return false;
34 }
35
36 auto paramObject = JSRef<JSObject>::Cast(info[0]);
37 auto value = paramObject->GetProperty("text");
38 if (value->IsNumber()) {
39 text = static_cast<PasteButtonPasteDescription>(value->ToNumber<int32_t>());
40 if ((text < PasteButtonPasteDescription::PASTE) ||
41 (text > PasteButtonPasteDescription::MAX_LABEL_TYPE)) {
42 return false;
43 }
44 } else {
45 text = PasteButtonPasteDescription::TEXT_NULL;
46 }
47
48 value = paramObject->GetProperty("icon");
49 if (value->IsNumber()) {
50 icon = static_cast<PasteButtonIconStyle>(value->ToNumber<int32_t>());
51 if ((icon != PasteButtonIconStyle::ICON_LINE)) {
52 return false;
53 }
54 } else {
55 icon = PasteButtonIconStyle::ICON_NULL;
56 }
57
58 if ((text == PasteButtonPasteDescription::TEXT_NULL) &&
59 (icon == PasteButtonIconStyle::ICON_NULL)) {
60 return false;
61 }
62
63 value = paramObject->GetProperty("buttonType");
64 if (value->IsNumber()) {
65 bg = value->ToNumber<int32_t>();
66 if ((bg < static_cast<int32_t>(ButtonType::NORMAL)) ||
67 (bg > static_cast<int32_t>(ButtonType::CIRCLE))) {
68 return false;
69 }
70 } else {
71 bg = BUTTON_TYPE_NULL;
72 }
73 return true;
74 }
75
Create(const JSCallbackInfo & info)76 void JSPasteButton::Create(const JSCallbackInfo& info)
77 {
78 PasteButtonPasteDescription textDesc;
79 PasteButtonIconStyle iconType;
80 int32_t backgroundType = 0;
81 if (!ParseComponentStyle(info, textDesc, iconType, backgroundType)) {
82 PasteButtonModelNG::GetInstance()->Create(
83 static_cast<int32_t>(PasteButtonPasteDescription::PASTE),
84 static_cast<int32_t>(PasteButtonIconStyle::ICON_LINE),
85 static_cast<int32_t>(ButtonType::CAPSULE));
86 } else {
87 PasteButtonModelNG::GetInstance()->Create(static_cast<int32_t>(textDesc),
88 static_cast<int32_t>(iconType), backgroundType);
89 }
90 }
91
Execute(GestureEvent & info)92 void JsPasteButtonClickFunction::Execute(GestureEvent& info)
93 {
94 JSRef<JSObject> clickEventParam = JSRef<JSObject>::New();
95 Offset globalOffset = info.GetGlobalLocation();
96 Offset localOffset = info.GetLocalLocation();
97 clickEventParam->SetProperty<double>("screenX", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
98 clickEventParam->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
99 clickEventParam->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
100 clickEventParam->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetY()));
101 clickEventParam->SetProperty<double>("timestamp",
102 static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
103 clickEventParam->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
104 clickEventParam->SetProperty<double>("pressure", info.GetForce());
105 if (info.GetTiltX().has_value()) {
106 clickEventParam->SetProperty<double>("tiltX", info.GetTiltX().value());
107 }
108 if (info.GetTiltY().has_value()) {
109 clickEventParam->SetProperty<double>("tiltY", info.GetTiltY().value());
110 }
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 JSPasteButton::JsOnClick(const JSCallbackInfo& info)
131 {
132 if (!info[0]->IsFunction()) {
133 return;
134 }
135 auto jsOnClickFunc = AceType::MakeRefPtr<JsPasteButtonClickFunction>(JSRef<JSFunc>::Cast(info[0]));
136 auto onTap = [execCtx = info.GetExecutionContext(), func = jsOnClickFunc](GestureEvent& info) {
137 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
138 ACE_SCORING_EVENT("onClick");
139 func->Execute(info);
140 };
141
142 NG::ViewAbstract::SetOnClick(std::move(onTap));
143 }
144
JSBind(BindingTarget globalObj)145 void JSPasteButton::JSBind(BindingTarget globalObj)
146 {
147 JSClass<JSPasteButton>::Declare("PasteButton");
148 MethodOptions opt = MethodOptions::NONE;
149 JSClass<JSPasteButton>::StaticMethod("create", &JSPasteButton::Create, opt);
150 JSClass<JSPasteButton>::StaticMethod("iconSize", &JSSecButtonBase::SetIconSize);
151 JSClass<JSPasteButton>::StaticMethod("layoutDirection", &JSSecButtonBase::SetLayoutDirection);
152 JSClass<JSPasteButton>::StaticMethod("fontSize", &JSSecButtonBase::SetFontSize);
153 JSClass<JSPasteButton>::StaticMethod("fontStyle", &JSSecButtonBase::SetFontStyle);
154 JSClass<JSPasteButton>::StaticMethod("iconColor", &JSSecButtonBase::SetIconColor);
155 JSClass<JSPasteButton>::StaticMethod("fontWeight", &JSSecButtonBase::SetFontWeight);
156 JSClass<JSPasteButton>::StaticMethod("fontFamily", &JSSecButtonBase::SetFontFamily);
157 JSClass<JSPasteButton>::StaticMethod("fontColor", &JSSecButtonBase::SetFontColor);
158 JSClass<JSPasteButton>::StaticMethod("backgroundColor", &JSSecButtonBase::SetBackgroundColor);
159 JSClass<JSPasteButton>::StaticMethod("borderStyle", &JSSecButtonBase::SetBackgroundBorderStyle);
160 JSClass<JSPasteButton>::StaticMethod("borderWidth", &JSSecButtonBase::SetBackgroundBorderWidth);
161 JSClass<JSPasteButton>::StaticMethod("borderColor", &JSSecButtonBase::SetBackgroundBorderColor);
162 JSClass<JSPasteButton>::StaticMethod("borderRadius", &JSSecButtonBase::SetBackgroundBorderRadius);
163 JSClass<JSPasteButton>::StaticMethod("padding", &JSSecButtonBase::SetBackgroundPadding);
164 JSClass<JSPasteButton>::StaticMethod("textIconSpace", &JSSecButtonBase::SetTextIconSpace);
165 JSClass<JSPasteButton>::StaticMethod("onClick", &JSPasteButton::JsOnClick);
166 JSClass<JSPasteButton>::StaticMethod("key", &JSViewAbstract::JsKey);
167 JSClass<JSPasteButton>::StaticMethod("position", &JSViewAbstract::JsPosition);
168 JSClass<JSPasteButton>::StaticMethod("markAnchor", &JSViewAbstract::JsMarkAnchor);
169 JSClass<JSPasteButton>::StaticMethod("offset", &JSViewAbstract::JsOffset);
170 JSClass<JSPasteButton>::StaticMethod("pop", &JSViewAbstract::Pop, opt);
171 JSClass<JSPasteButton>::StaticMethod("width", &JSViewAbstract::JsWidth);
172 JSClass<JSPasteButton>::StaticMethod("height", &JSViewAbstract::JsHeight);
173 JSClass<JSPasteButton>::StaticMethod("size", &JSViewAbstract::JsSize);
174 JSClass<JSPasteButton>::StaticMethod("constraintSize", &JSViewAbstract::JsConstraintSize);
175 JSClass<JSPasteButton>::StaticMethod("debugLine", &JSViewAbstract::JsDebugLine);
176 JSClass<JSPasteButton>::Bind<>(globalObj);
177 }
178 } // namespace OHOS::Ace::Framework
179