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