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_location_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/location_button/location_button_model_ng.h"
23 #include "core/components_ng/pattern/security_component/security_component_theme.h"
24
25 using OHOS::Ace::NG::LocationButtonModelNG;
26 using OHOS::Ace::NG::SecurityComponentTheme;
27
28 namespace OHOS::Ace::Framework {
ParseComponentStyle(const JSCallbackInfo & info,LocationButtonLocationDescription & text,LocationButtonIconStyle & icon,int32_t & bg)29 bool JSLocationButton::ParseComponentStyle(const JSCallbackInfo& info,
30 LocationButtonLocationDescription& text, LocationButtonIconStyle& 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<LocationButtonLocationDescription>(value->ToNumber<int32_t>());
40 if ((text < LocationButtonLocationDescription::CURRENT_LOCATION) ||
41 (text > LocationButtonLocationDescription::CURRENT_POSITION)) {
42 return false;
43 }
44 } else {
45 text = LocationButtonLocationDescription::TEXT_NULL;
46 }
47
48 value = paramObject->GetProperty("icon");
49 if (value->IsNumber()) {
50 icon = static_cast<LocationButtonIconStyle>(value->ToNumber<int32_t>());
51 if ((icon < LocationButtonIconStyle::ICON_FULL_FILLED) ||
52 (icon > LocationButtonIconStyle::ICON_LINE)) {
53 return false;
54 }
55 } else {
56 icon = LocationButtonIconStyle::ICON_NULL;
57 }
58
59 if ((text == LocationButtonLocationDescription::TEXT_NULL) &&
60 (icon == LocationButtonIconStyle::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 return false;
70 }
71 } else {
72 bg = BUTTON_TYPE_NULL;
73 }
74 return true;
75 }
76
Create(const JSCallbackInfo & info)77 void JSLocationButton::Create(const JSCallbackInfo& info)
78 {
79 LocationButtonLocationDescription textDesc;
80 LocationButtonIconStyle iconType;
81 int32_t backgroundType = 0;
82 if (!ParseComponentStyle(info, textDesc, iconType, backgroundType)) {
83 LocationButtonModelNG::GetInstance()->Create(
84 static_cast<int32_t>(LocationButtonLocationDescription::CURRENT_LOCATION),
85 static_cast<int32_t>(LocationButtonIconStyle::ICON_LINE),
86 static_cast<int32_t>(ButtonType::CAPSULE));
87 } else {
88 LocationButtonModelNG::GetInstance()->Create(static_cast<int32_t>(textDesc),
89 static_cast<int32_t>(iconType), backgroundType);
90 }
91 }
92
Execute(GestureEvent & info)93 void JsLocationButtonClickFunction::Execute(GestureEvent& info)
94 {
95 JSRef<JSObject> clickEventParam = JSRef<JSObject>::New();
96 Offset globalOffset = info.GetGlobalLocation();
97 Offset localOffset = info.GetLocalLocation();
98 clickEventParam->SetProperty<double>("screenX", SystemProperties::Px2Vp(globalOffset.GetX()));
99 clickEventParam->SetProperty<double>("screenY", SystemProperties::Px2Vp(globalOffset.GetY()));
100 clickEventParam->SetProperty<double>("x", SystemProperties::Px2Vp(localOffset.GetX()));
101 clickEventParam->SetProperty<double>("y", SystemProperties::Px2Vp(localOffset.GetY()));
102 clickEventParam->SetProperty<double>("timestamp",
103 static_cast<double>(info.GetTimeStamp().time_since_epoch().count()));
104 clickEventParam->SetProperty<double>("source", static_cast<int32_t>(info.GetSourceDevice()));
105 clickEventParam->SetProperty<double>("pressure", info.GetForce());
106 if (info.GetTiltX().has_value()) {
107 clickEventParam->SetProperty<double>("tiltX", info.GetTiltX().value());
108 }
109 if (info.GetTiltY().has_value()) {
110 clickEventParam->SetProperty<double>("tiltY", info.GetTiltY().value());
111 }
112 clickEventParam->SetProperty<double>("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
113 auto target = CreateEventTargetObject(info);
114 clickEventParam->SetPropertyObject("target", target);
115
116 int32_t res = static_cast<int32_t>(SecurityComponentHandleResult::CLICK_GRANT_FAILED);
117 #ifdef SECURITY_COMPONENT_ENABLE
118 auto secEventValue = info.GetSecCompHandleEvent();
119 if (secEventValue != nullptr) {
120 res = secEventValue->GetInt("handleRes", res);
121 }
122 #endif
123 JSRef<JSVal> errorParam = JSRef<JSVal>::Make(ToJSValue(res));
124 JSRef<JSVal> params[] = { clickEventParam, errorParam };
125 JsFunction::ExecuteJS(2, params);
126 }
127
JsOnClick(const JSCallbackInfo & info)128 void JSLocationButton::JsOnClick(const JSCallbackInfo& info)
129 {
130 if (!info[0]->IsFunction()) {
131 return;
132 }
133 auto jsOnClickFunc = AceType::MakeRefPtr<JsLocationButtonClickFunction>(JSRef<JSFunc>::Cast(info[0]));
134 auto onTap = [execCtx = info.GetExecutionContext(), func = jsOnClickFunc](GestureEvent& info) {
135 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
136 ACE_SCORING_EVENT("onClick");
137 func->Execute(info);
138 };
139
140 NG::ViewAbstract::SetOnClick(std::move(onTap));
141 }
142
JSBind(BindingTarget globalObj)143 void JSLocationButton::JSBind(BindingTarget globalObj)
144 {
145 JSClass<JSLocationButton>::Declare("LocationButton");
146 MethodOptions opt = MethodOptions::NONE;
147 JSClass<JSLocationButton>::StaticMethod("create", &JSLocationButton::Create, opt);
148 JSClass<JSLocationButton>::StaticMethod("iconSize", &JSSecButtonBase::SetIconSize);
149 JSClass<JSLocationButton>::StaticMethod("layoutDirection", &JSSecButtonBase::SetLayoutDirection);
150 JSClass<JSLocationButton>::StaticMethod("fontSize", &JSSecButtonBase::SetFontSize);
151 JSClass<JSLocationButton>::StaticMethod("fontStyle", &JSSecButtonBase::SetFontStyle);
152 JSClass<JSLocationButton>::StaticMethod("iconColor", &JSSecButtonBase::SetIconColor);
153 JSClass<JSLocationButton>::StaticMethod("fontWeight", &JSSecButtonBase::SetFontWeight);
154 JSClass<JSLocationButton>::StaticMethod("fontFamily", &JSSecButtonBase::SetFontFamily);
155 JSClass<JSLocationButton>::StaticMethod("fontColor", &JSSecButtonBase::SetFontColor);
156 JSClass<JSLocationButton>::StaticMethod("backgroundColor", &JSSecButtonBase::SetBackgroundColor);
157 JSClass<JSLocationButton>::StaticMethod("borderStyle", &JSSecButtonBase::SetBackgroundBorderStyle);
158 JSClass<JSLocationButton>::StaticMethod("borderWidth", &JSSecButtonBase::SetBackgroundBorderWidth);
159 JSClass<JSLocationButton>::StaticMethod("borderColor", &JSSecButtonBase::SetBackgroundBorderColor);
160 JSClass<JSLocationButton>::StaticMethod("borderRadius", &JSSecButtonBase::SetBackgroundBorderRadius);
161 JSClass<JSLocationButton>::StaticMethod("padding", &JSSecButtonBase::SetBackgroundPadding);
162 JSClass<JSLocationButton>::StaticMethod("textIconSpace", &JSSecButtonBase::SetTextIconSpace);
163 JSClass<JSLocationButton>::StaticMethod("onClick", &JSLocationButton::JsOnClick);
164 JSClass<JSLocationButton>::StaticMethod("key", &JSViewAbstract::JsKey);
165 JSClass<JSLocationButton>::StaticMethod("position", &JSViewAbstract::JsPosition);
166 JSClass<JSLocationButton>::StaticMethod("markAnchor", &JSViewAbstract::JsMarkAnchor);
167 JSClass<JSLocationButton>::StaticMethod("offset", &JSViewAbstract::JsOffset);
168 JSClass<JSLocationButton>::StaticMethod("pop", &JSViewAbstract::Pop, opt);
169 #if defined(PREVIEW)
170 JSClass<JSLocationButton>::StaticMethod("debugLine", &JSViewAbstract::JsDebugLine);
171 #endif
172 JSClass<JSLocationButton>::Bind<>(globalObj);
173 }
174 } // namespace OHOS::Ace::Framework
175