• 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 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_rating_bridge.h"
16 
17 #include "core/interfaces/native/node/api.h"
18 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19 
20 namespace OHOS::Ace::NG {
21 constexpr double STEPS_DEFAULT = 0.5;
22 constexpr double STEPS_MIN_SIZE = 0.1;
23 constexpr int32_t STARS_DEFAULT = 5;
24 constexpr int NUM_0 = 0;
25 constexpr int NUM_1 = 1;
26 constexpr int NUM_2 = 2;
27 constexpr int NUM_3 = 3;
28 
SetStars(ArkUIRuntimeCallInfo * runtimeCallInfo)29 ArkUINativeModuleValue RatingBridge::SetStars(ArkUIRuntimeCallInfo* runtimeCallInfo)
30 {
31     EcmaVM* vm = runtimeCallInfo->GetVM();
32     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
33     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
34     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
35     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
36     auto input = secondArg->ToNumber(vm)->Value();
37     int32_t stars = static_cast<int32_t>(input);
38     if (stars <= 0) {
39         stars = STARS_DEFAULT;
40     }
41     GetArkUIInternalNodeAPI()->GetRatingModifier().SetStars(nativeNode, stars);
42     return panda::JSValueRef::Undefined(vm);
43 }
44 
ResetStars(ArkUIRuntimeCallInfo * runtimeCallInfo)45 ArkUINativeModuleValue RatingBridge::ResetStars(ArkUIRuntimeCallInfo* runtimeCallInfo)
46 {
47     EcmaVM* vm = runtimeCallInfo->GetVM();
48     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
49     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
50     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
51     GetArkUIInternalNodeAPI()->GetRatingModifier().ResetStars(nativeNode);
52     return panda::JSValueRef::Undefined(vm);
53 }
SetRatingStepSize(ArkUIRuntimeCallInfo * runtimeCallInfo)54 ArkUINativeModuleValue RatingBridge::SetRatingStepSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
55 {
56     EcmaVM* vm = runtimeCallInfo->GetVM();
57     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
58     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
59     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
60     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
61 
62     if (secondArg->IsNull() || !secondArg->IsNumber()) {
63         GetArkUIInternalNodeAPI()->GetRatingModifier().ResetRatingStepSize(nativeNode);
64         return panda::JSValueRef::Undefined(vm);
65     }
66     auto steps  = secondArg->ToNumber(vm)->Value();
67     if (LessNotEqual(steps, STEPS_MIN_SIZE)) {
68         steps = STEPS_DEFAULT;
69     }
70     GetArkUIInternalNodeAPI()->GetRatingModifier().SetRatingStepSize(nativeNode, steps);
71     return panda::JSValueRef::Undefined(vm);
72 }
73 
ResetRatingStepSize(ArkUIRuntimeCallInfo * runtimeCallInfo)74 ArkUINativeModuleValue RatingBridge::ResetRatingStepSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
75 {
76     EcmaVM* vm = runtimeCallInfo->GetVM();
77     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
78     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
79     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
80     GetArkUIInternalNodeAPI()->GetRatingModifier().ResetRatingStepSize(nativeNode);
81     return panda::JSValueRef::Undefined(vm);
82 }
83 
SetStarStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)84 ArkUINativeModuleValue RatingBridge::SetStarStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
85 {
86     EcmaVM* vm = runtimeCallInfo->GetVM();
87     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
88     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
89     Local<JSValueRef> backgroundUriArg = runtimeCallInfo->GetCallArgRef(NUM_1);
90     Local<JSValueRef> foregroundUriArg = runtimeCallInfo->GetCallArgRef(NUM_2);
91     Local<JSValueRef> secondaryUriArg = runtimeCallInfo->GetCallArgRef(NUM_3);
92     void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
93 
94     std::string backgroundUri;
95     if (backgroundUriArg->IsString()) {
96         backgroundUri = backgroundUriArg->ToString(vm)->ToString();
97     }
98 
99     std::string foregroundUri;
100     if (foregroundUriArg->IsString()) {
101         foregroundUri = foregroundUriArg->ToString(vm)->ToString();
102     }
103 
104     std::string secondaryUri;
105     if (secondaryUriArg->IsString()) {
106         secondaryUri = secondaryUriArg->ToString(vm)->ToString();
107     }
108 
109     if (secondaryUri.empty() && !backgroundUri.empty()) {
110         secondaryUri = backgroundUri;
111     }
112 
113     GetArkUIInternalNodeAPI()->GetRatingModifier().SetStarStyle(
114         nativeNode, backgroundUri.c_str(), foregroundUri.c_str(), secondaryUri.c_str());
115     return panda::JSValueRef::Undefined(vm);
116 }
117 
ResetStarStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)118 ArkUINativeModuleValue RatingBridge::ResetStarStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
119 {
120     EcmaVM* vm = runtimeCallInfo->GetVM();
121     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
122     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
123     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
124     GetArkUIInternalNodeAPI()->GetRatingModifier().ResetStarStyle(nativeNode);
125     return panda::JSValueRef::Undefined(vm);
126 }
127 } // namespace OHOS::Ace::NG