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 "frameworks/bridge/declarative_frontend/jsview/js_symbol.h"
16
17 #include "frameworks/bridge/declarative_frontend/engine/bindings.h"
18 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
19 #include "frameworks/bridge/declarative_frontend/jsview/js_interactable_view.h"
20 #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h"
21 #include "frameworks/core/components_ng/pattern/symbol/symbol_model.h"
22 #include "frameworks/core/components_ng/pattern/symbol/symbol_model_ng.h"
23
24 namespace OHOS::Ace {
25
26 std::unique_ptr<SymbolModel> SymbolModel::instance_ = nullptr;
27 std::mutex SymbolModel::mutex_;
28
GetInstance()29 SymbolModel* SymbolModel::GetInstance()
30 {
31 if (!instance_) {
32 std::lock_guard<std::mutex> lock(mutex_);
33 if (!instance_) {
34 instance_.reset(new NG::SymbolModelNG());
35 }
36 }
37 return instance_.get();
38 }
39
40 } // namespace OHOS::Ace
41
42 namespace OHOS::Ace::Framework {
43
JSBind(BindingTarget globalObj)44 void JSSymbol::JSBind(BindingTarget globalObj)
45 {
46 JSClass<JSSymbol>::Declare("SymbolGlyph");
47
48 MethodOptions opt = MethodOptions::NONE;
49 JSClass<JSSymbol>::StaticMethod("create", &JSSymbol::Create, opt);
50 JSClass<JSSymbol>::StaticMethod("fontWeight", &JSSymbol::SetFontWeight, opt);
51 JSClass<JSSymbol>::StaticMethod("fontSize", &JSSymbol::SetFontSize, opt);
52 JSClass<JSSymbol>::StaticMethod("renderingStrategy", &JSSymbol::SetSymbolRenderingStrategy, opt);
53 JSClass<JSSymbol>::StaticMethod("fontColor", &JSSymbol::SetFontColor, opt);
54 JSClass<JSSymbol>::StaticMethod("effectStrategy", &JSSymbol::SetSymbolEffect, opt);
55 JSClass<JSSymbol>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
56 JSClass<JSSymbol>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
57 JSClass<JSSymbol>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
58 JSClass<JSSymbol>::StaticMethod("clip", &JSSymbol::JsClip);
59 JSClass<JSSymbol>::InheritAndBind<JSViewAbstract>(globalObj);
60 }
61
Create(const JSCallbackInfo & info)62 void JSSymbol::Create(const JSCallbackInfo& info)
63 {
64 if (info[0]->IsUndefined()) {
65 SymbolModel::GetInstance()->Create(0);
66 return;
67 }
68 uint32_t symbolId;
69 RefPtr<ResourceObject> resourceObject;
70 ParseJsSymbolId(info[0], symbolId, resourceObject);
71 SymbolModel::GetInstance()->Create(symbolId);
72 }
73
SetFontSize(const JSCallbackInfo & info)74 void JSSymbol::SetFontSize(const JSCallbackInfo& info)
75 {
76 if (info.Length() < 1) {
77 return;
78 }
79 auto theme = GetTheme<TextTheme>();
80 CHECK_NULL_VOID(theme);
81 CalcDimension fontSize = theme->GetTextStyle().GetFontSize();
82 if (!ParseJsDimensionFpNG(info[0], fontSize, false)) {
83 fontSize = theme->GetTextStyle().GetFontSize();
84 SymbolModel::GetInstance()->SetFontSize(fontSize);
85 return;
86 }
87 if (fontSize.IsNegative()) {
88 fontSize = theme->GetTextStyle().GetFontSize();
89 }
90
91 SymbolModel::GetInstance()->SetFontSize(fontSize);
92 }
93
SetFontWeight(const std::string & value)94 void JSSymbol::SetFontWeight(const std::string& value)
95 {
96 SymbolModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(value));
97 }
98
SetSymbolRenderingStrategy(const JSCallbackInfo & info)99 void JSSymbol::SetSymbolRenderingStrategy(const JSCallbackInfo& info)
100 {
101 uint32_t strategy = 0;
102 ParseJsInteger(info[0], strategy);
103 SymbolModel::GetInstance()->SetSymbolRenderingStrategy(strategy);
104 }
105
SetFontColor(const JSCallbackInfo & info)106 void JSSymbol::SetFontColor(const JSCallbackInfo& info)
107 {
108 std::vector<Color> symbolColor;
109 if (!ParseJsSymbolColor(info[0], symbolColor)) {
110 return;
111 }
112 SymbolModel::GetInstance()->SetFontColor(symbolColor);
113 }
114
SetSymbolEffect(const JSCallbackInfo & info)115 void JSSymbol::SetSymbolEffect(const JSCallbackInfo& info)
116 {
117 uint32_t strategy = 0;
118 ParseJsInteger(info[0], strategy);
119 SymbolModel::GetInstance()->SetSymbolEffect(strategy);
120 }
121
JsClip(const JSCallbackInfo & info)122 void JSSymbol::JsClip(const JSCallbackInfo& info)
123 {
124 JSViewAbstract::JsClip(info);
125 if (info[0]->IsBoolean()) {
126 SymbolModel::GetInstance()->SetClipEdge();
127 }
128 }
129 } // namespace OHOS::Ace::Framework
130