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", SystemProperties::Px2Vp(globalOffset.GetX()));
98 clickEventParam->SetProperty<double>("screenY", SystemProperties::Px2Vp(globalOffset.GetY()));
99 clickEventParam->SetProperty<double>("x", SystemProperties::Px2Vp(localOffset.GetX()));
100 clickEventParam->SetProperty<double>("y", SystemProperties::Px2Vp(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 }
121 #endif
122 JSRef<JSVal> errorParam = JSRef<JSVal>::Make(ToJSValue(res));
123 JSRef<JSVal> params[] = { clickEventParam, errorParam };
124 JsFunction::ExecuteJS(2, params);
125 }
126
JsOnClick(const JSCallbackInfo & info)127 void JSPasteButton::JsOnClick(const JSCallbackInfo& info)
128 {
129 if (!info[0]->IsFunction()) {
130 return;
131 }
132 auto jsOnClickFunc = AceType::MakeRefPtr<JsPasteButtonClickFunction>(JSRef<JSFunc>::Cast(info[0]));
133 auto onTap = [execCtx = info.GetExecutionContext(), func = jsOnClickFunc](GestureEvent& info) {
134 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
135 ACE_SCORING_EVENT("onClick");
136 func->Execute(info);
137 };
138
139 NG::ViewAbstract::SetOnClick(std::move(onTap));
140 }
141
JSBind(BindingTarget globalObj)142 void JSPasteButton::JSBind(BindingTarget globalObj)
143 {
144 JSClass<JSPasteButton>::Declare("PasteButton");
145 MethodOptions opt = MethodOptions::NONE;
146 JSClass<JSPasteButton>::StaticMethod("create", &JSPasteButton::Create, opt);
147 JSClass<JSPasteButton>::StaticMethod("iconSize", &JSSecButtonBase::SetIconSize);
148 JSClass<JSPasteButton>::StaticMethod("layoutDirection", &JSSecButtonBase::SetLayoutDirection);
149 JSClass<JSPasteButton>::StaticMethod("fontSize", &JSSecButtonBase::SetFontSize);
150 JSClass<JSPasteButton>::StaticMethod("fontStyle", &JSSecButtonBase::SetFontStyle);
151 JSClass<JSPasteButton>::StaticMethod("iconColor", &JSSecButtonBase::SetIconColor);
152 JSClass<JSPasteButton>::StaticMethod("fontWeight", &JSSecButtonBase::SetFontWeight);
153 JSClass<JSPasteButton>::StaticMethod("fontFamily", &JSSecButtonBase::SetFontFamily);
154 JSClass<JSPasteButton>::StaticMethod("fontColor", &JSSecButtonBase::SetFontColor);
155 JSClass<JSPasteButton>::StaticMethod("backgroundColor", &JSSecButtonBase::SetBackgroundColor);
156 JSClass<JSPasteButton>::StaticMethod("borderStyle", &JSSecButtonBase::SetBackgroundBorderStyle);
157 JSClass<JSPasteButton>::StaticMethod("borderWidth", &JSSecButtonBase::SetBackgroundBorderWidth);
158 JSClass<JSPasteButton>::StaticMethod("borderColor", &JSSecButtonBase::SetBackgroundBorderColor);
159 JSClass<JSPasteButton>::StaticMethod("borderRadius", &JSSecButtonBase::SetBackgroundBorderRadius);
160 JSClass<JSPasteButton>::StaticMethod("padding", &JSSecButtonBase::SetBackgroundPadding);
161 JSClass<JSPasteButton>::StaticMethod("textIconSpace", &JSSecButtonBase::SetTextIconSpace);
162 JSClass<JSPasteButton>::StaticMethod("onClick", &JSPasteButton::JsOnClick);
163 JSClass<JSPasteButton>::StaticMethod("key", &JSViewAbstract::JsKey);
164 JSClass<JSPasteButton>::StaticMethod("position", &JSViewAbstract::JsPosition);
165 JSClass<JSPasteButton>::StaticMethod("markAnchor", &JSViewAbstract::JsMarkAnchor);
166 JSClass<JSPasteButton>::StaticMethod("offset", &JSViewAbstract::JsOffset);
167 JSClass<JSPasteButton>::StaticMethod("pop", &JSViewAbstract::Pop, opt);
168 #if defined(PREVIEW)
169 JSClass<JSPasteButton>::StaticMethod("debugLine", &JSViewAbstract::JsDebugLine);
170 #endif
171 JSClass<JSPasteButton>::Bind<>(globalObj);
172 }
173 } // namespace OHOS::Ace::Framework
174