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