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