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