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