• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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/js_frontend/engine/quickjs/badge_bridge.h"
17 
18 #include "frameworks/bridge/common/utils/utils.h"
19 
20 namespace OHOS::Ace::Framework {
21 
ParseBadgeConfig(JSContext * ctx,JSValue valObject)22 void BadgeBridge::ParseBadgeConfig(JSContext* ctx, JSValue valObject)
23 {
24     JSPropertyEnum* pTab = nullptr;
25     uint32_t len = 0;
26     if (!CheckAndGetJsProperty(ctx, valObject, &pTab, &len)) {
27         return;
28     }
29     for (uint32_t i = 0; i < len; i++) {
30         const char* key = JS_AtomToCString(ctx, pTab[i].atom);
31         if (key == nullptr) {
32             JS_FreeAtom(ctx, pTab[i].atom);
33             LOGW("key is null. Ignoring!");
34             continue;
35         }
36         JSValue val = JS_GetProperty(ctx, valObject, pTab[i].atom);
37         if (JS_IsString(val) || JS_IsNumber(val)) {
38             ScopedString styleVal(ctx, val);
39             const char* valStr = styleVal.get();
40             // static linear map must be sorted by key.
41             static const LinearMapNode<void (*)(const char*, BadgeBridge&)> badgeConfigOperators[] = {
42                 { DOM_BADGE_COLOR,
43                     [](const char* valStr, BadgeBridge& badgeBridge) {
44                         badgeBridge.badgeConfig_.badgeColor = { Color::FromString(valStr), true };
45                     } },
46                 { DOM_BADGE_CIRCLE_SIZE,
47                     [](const char* valStr, BadgeBridge& badgeBridge) {
48                         badgeBridge.badgeConfig_.badgeSize = { StringToDimension(valStr), true };
49                     } },
50                 { DOM_BADGE_TEXT_COLOR,
51                     [](const char* valStr, BadgeBridge& badgeBridge) {
52                         badgeBridge.badgeConfig_.textColor = { Color::FromString(valStr), true };
53                     } },
54                 { DOM_BADGE_TEXT_FONT_SIZE,
55                     [](const char* valStr, BadgeBridge& badgeBridge) {
56                         badgeBridge.badgeConfig_.textSize = { StringToDimension(valStr), true };
57                     } }
58             };
59             auto operatorIter = BinarySearchFindIndex(badgeConfigOperators, ArraySize(badgeConfigOperators), key);
60             if (operatorIter != -1) {
61                 badgeConfigOperators[operatorIter].value(valStr, *this);
62             } else {
63                 LOGD("key : %{public}s unsupported. Ignoring!", key);
64             }
65         } else {
66             LOGD("value of unsupported type. Ignoring!");
67         }
68         JS_FreeAtom(ctx, pTab[i].atom);
69         JS_FreeCString(ctx, key);
70         JS_FreeValue(ctx, val);
71     }
72     js_free(ctx, pTab);
73 }
74 
75 } // namespace OHOS::Ace::Framework