• 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_save_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/save_button/save_button_model_ng.h"
23 #include "core/components_ng/pattern/security_component/security_component_theme.h"
24 
25 using OHOS::Ace::NG::SaveButtonModelNG;
26 using OHOS::Ace::NG::SecurityComponentTheme;
27 
28 namespace OHOS::Ace::Framework {
ParseComponentStyle(const JSCallbackInfo & info,SaveButtonSaveDescription & text,SaveButtonIconStyle & icon,int32_t & bg)29 bool JSSaveButton::ParseComponentStyle(const JSCallbackInfo& info,
30     SaveButtonSaveDescription& text, SaveButtonIconStyle& 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<SaveButtonSaveDescription>(value->ToNumber<int32_t>());
40         if ((text < SaveButtonSaveDescription::DOWNLOAD) ||
41             (text > SaveButtonSaveDescription::MAX_LABEL_TYPE)) {
42             return false;
43         }
44     } else {
45         text = SaveButtonSaveDescription::TEXT_NULL;
46     }
47 
48     value = paramObject->GetProperty("icon");
49     if (value->IsNumber()) {
50         icon = static_cast<SaveButtonIconStyle>(value->ToNumber<int32_t>());
51         if ((icon < SaveButtonIconStyle::ICON_FULL_FILLED) ||
52             (icon > SaveButtonIconStyle::ICON_LINE)) {
53             return false;
54         }
55     } else {
56         icon = SaveButtonIconStyle::ICON_NULL;
57     }
58 
59     if ((text == SaveButtonSaveDescription::TEXT_NULL) &&
60         (icon == SaveButtonIconStyle::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 JSSaveButton::Create(const JSCallbackInfo& info)
78 {
79     SaveButtonSaveDescription textDesc;
80     SaveButtonIconStyle iconType;
81     int32_t backgroundType = 0;
82     if (!ParseComponentStyle(info, textDesc, iconType, backgroundType)) {
83         SaveButtonModelNG::GetInstance()->Create(
84             static_cast<int32_t>(SaveButtonSaveDescription::DOWNLOAD),
85             static_cast<int32_t>(SaveButtonIconStyle::ICON_FULL_FILLED),
86             static_cast<int32_t>(ButtonType::CAPSULE));
87     } else {
88         SaveButtonModelNG::GetInstance()->Create(static_cast<int32_t>(textDesc),
89             static_cast<int32_t>(iconType), backgroundType);
90     }
91 }
92 
Execute(GestureEvent & info)93 void JsSaveButtonClickFunction::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", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetX()));
99     clickEventParam->SetProperty<double>("screenY", PipelineBase::Px2VpWithCurrentDensity(globalOffset.GetY()));
100     clickEventParam->SetProperty<double>("x", PipelineBase::Px2VpWithCurrentDensity(localOffset.GetX()));
101     clickEventParam->SetProperty<double>("y", PipelineBase::Px2VpWithCurrentDensity(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         if (res == static_cast<int32_t>(SecurityComponentHandleResult::DROP_CLICK)) {
122             return;
123         }
124     }
125 #endif
126     JSRef<JSVal> errorParam = JSRef<JSVal>::Make(ToJSValue(res));
127     JSRef<JSVal> params[] = { clickEventParam, errorParam };
128     JsFunction::ExecuteJS(2, params);
129 }
130 
JsOnClick(const JSCallbackInfo & info)131 void JSSaveButton::JsOnClick(const JSCallbackInfo& info)
132 {
133     if (!info[0]->IsFunction()) {
134         return;
135     }
136     auto jsOnClickFunc = AceType::MakeRefPtr<JsSaveButtonClickFunction>(JSRef<JSFunc>::Cast(info[0]));
137     auto onTap = [execCtx = info.GetExecutionContext(), func = jsOnClickFunc](GestureEvent& info) {
138         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
139         ACE_SCORING_EVENT("onClick");
140         func->Execute(info);
141     };
142 
143     NG::ViewAbstract::SetOnClick(std::move(onTap));
144 }
145 
JSBind(BindingTarget globalObj)146 void JSSaveButton::JSBind(BindingTarget globalObj)
147 {
148     JSClass<JSSaveButton>::Declare("SaveButton");
149     MethodOptions opt = MethodOptions::NONE;
150     JSClass<JSSaveButton>::StaticMethod("create", &JSSaveButton::Create, opt);
151     JSClass<JSSaveButton>::StaticMethod("iconSize", &JSSecButtonBase::SetIconSize);
152     JSClass<JSSaveButton>::StaticMethod("layoutDirection", &JSSecButtonBase::SetLayoutDirection);
153     JSClass<JSSaveButton>::StaticMethod("fontSize", &JSSecButtonBase::SetFontSize);
154     JSClass<JSSaveButton>::StaticMethod("fontStyle", &JSSecButtonBase::SetFontStyle);
155     JSClass<JSSaveButton>::StaticMethod("iconColor", &JSSecButtonBase::SetIconColor);
156     JSClass<JSSaveButton>::StaticMethod("fontWeight", &JSSecButtonBase::SetFontWeight);
157     JSClass<JSSaveButton>::StaticMethod("fontFamily", &JSSecButtonBase::SetFontFamily);
158     JSClass<JSSaveButton>::StaticMethod("fontColor", &JSSecButtonBase::SetFontColor);
159     JSClass<JSSaveButton>::StaticMethod("backgroundColor", &JSSecButtonBase::SetBackgroundColor);
160     JSClass<JSSaveButton>::StaticMethod("borderStyle", &JSSecButtonBase::SetBackgroundBorderStyle);
161     JSClass<JSSaveButton>::StaticMethod("borderWidth", &JSSecButtonBase::SetBackgroundBorderWidth);
162     JSClass<JSSaveButton>::StaticMethod("borderColor", &JSSecButtonBase::SetBackgroundBorderColor);
163     JSClass<JSSaveButton>::StaticMethod("borderRadius", &JSSecButtonBase::SetBackgroundBorderRadius);
164     JSClass<JSSaveButton>::StaticMethod("padding", &JSSecButtonBase::SetBackgroundPadding);
165     JSClass<JSSaveButton>::StaticMethod("textIconSpace", &JSSecButtonBase::SetTextIconSpace);
166     JSClass<JSSaveButton>::StaticMethod("onClick", &JSSaveButton::JsOnClick);
167     JSClass<JSSaveButton>::StaticMethod("key", &JSViewAbstract::JsKey);
168     JSClass<JSSaveButton>::StaticMethod("position", &JSViewAbstract::JsPosition);
169     JSClass<JSSaveButton>::StaticMethod("markAnchor", &JSViewAbstract::JsMarkAnchor);
170     JSClass<JSSaveButton>::StaticMethod("offset", &JSViewAbstract::JsOffset);
171     JSClass<JSSaveButton>::StaticMethod("pop", &JSViewAbstract::Pop, opt);
172     JSClass<JSSaveButton>::StaticMethod("width", &JSViewAbstract::JsWidth);
173     JSClass<JSSaveButton>::StaticMethod("height", &JSViewAbstract::JsHeight);
174     JSClass<JSSaveButton>::StaticMethod("size", &JSViewAbstract::JsSize);
175     JSClass<JSSaveButton>::StaticMethod("constraintSize", &JSViewAbstract::JsConstraintSize);
176     JSClass<JSSaveButton>::StaticMethod("debugLine", &JSViewAbstract::JsDebugLine);
177     JSClass<JSSaveButton>::Bind<>(globalObj);
178 }
179 } // namespace OHOS::Ace::Framework
180