/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "frameworks/bridge/declarative_frontend/jsview/js_symbol.h" #include "core/components_ng/pattern/symbol/constants.h" #include "frameworks/bridge/declarative_frontend/engine/bindings.h" #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h" #include "frameworks/bridge/declarative_frontend/jsview/js_interactable_view.h" #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h" #include "frameworks/core/components_ng/pattern/symbol/symbol_model.h" #include "frameworks/core/components_ng/pattern/symbol/symbol_model_ng.h" namespace OHOS::Ace { std::unique_ptr SymbolModel::instance_ = nullptr; std::mutex SymbolModel::mutex_; SymbolModel* SymbolModel::GetInstance() { static NG::SymbolModelNG instance; return &instance; } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { const std::map SYMBOL_EFFECT_TYPE_MAP = { { "ScaleSymbolEffect", SymbolEffectType::SCALE }, { "HierarchicalSymbolEffect", SymbolEffectType::HIERARCHICAL }, { "AppearSymbolEffect", SymbolEffectType::APPEAR }, { "DisappearSymbolEffect", SymbolEffectType::DISAPPEAR }, { "BounceSymbolEffect", SymbolEffectType::BOUNCE }, { "ReplaceSymbolEffect", SymbolEffectType::REPLACE }, { "PulseSymbolEffect", SymbolEffectType::PULSE }, }; void JSSymbol::JSBind(BindingTarget globalObj) { JSClass::Declare("SymbolGlyph"); MethodOptions opt = MethodOptions::NONE; JSClass::StaticMethod("create", &JSSymbol::Create, opt); JSClass::StaticMethod("fontWeight", &JSSymbol::SetFontWeight, opt); JSClass::StaticMethod("fontSize", &JSSymbol::SetFontSize, opt); JSClass::StaticMethod("renderingStrategy", &JSSymbol::SetSymbolRenderingStrategy, opt); JSClass::StaticMethod("fontColor", &JSSymbol::SetFontColor, opt); JSClass::StaticMethod("effectStrategy", &JSSymbol::SetSymbolEffect, opt); JSClass::StaticMethod("onTouch", &JSInteractableView::JsOnTouch); JSClass::StaticMethod("onAttach", &JSInteractableView::JsOnAttach); JSClass::StaticMethod("onAppear", &JSInteractableView::JsOnAppear); JSClass::StaticMethod("onDetach", &JSInteractableView::JsOnDetach); JSClass::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear); JSClass::StaticMethod("clip", &JSSymbol::JsClip); JSClass::StaticMethod("symbolEffect", &JSSymbol::SetSymbolEffectOptions, opt); JSClass::InheritAndBind(globalObj); } void JSSymbol::Create(const JSCallbackInfo& info) { if (info[0]->IsUndefined()) { SymbolModel::GetInstance()->Create(0); return; } uint32_t symbolId; RefPtr resourceObject; ParseJsSymbolId(info[0], symbolId, resourceObject); SymbolModel::GetInstance()->Create(symbolId); } void JSSymbol::SetFontSize(const JSCallbackInfo& info) { if (info.Length() < 1) { return; } auto theme = GetTheme(); CHECK_NULL_VOID(theme); CalcDimension fontSize = theme->GetTextStyle().GetFontSize(); if (!ParseJsDimensionFpNG(info[0], fontSize, false)) { fontSize = theme->GetTextStyle().GetFontSize(); SymbolModel::GetInstance()->SetFontSize(fontSize); return; } if (fontSize.IsNegative()) { fontSize = theme->GetTextStyle().GetFontSize(); } SymbolModel::GetInstance()->SetFontSize(fontSize); } void JSSymbol::SetFontWeight(const std::string& value) { SymbolModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(value)); } void JSSymbol::SetSymbolRenderingStrategy(const JSCallbackInfo& info) { uint32_t strategy = 0; ParseJsInteger(info[0], strategy); SymbolModel::GetInstance()->SetSymbolRenderingStrategy(strategy); } void JSSymbol::SetFontColor(const JSCallbackInfo& info) { std::vector symbolColor; if (!ParseJsSymbolColor(info[0], symbolColor)) { return; } SymbolModel::GetInstance()->SetFontColor(symbolColor); } void JSSymbol::SetSymbolEffect(const JSCallbackInfo& info) { uint32_t strategy = 0; ParseJsInteger(info[0], strategy); SymbolModel::GetInstance()->SetSymbolEffect(strategy); } void JSSymbol::JsClip(const JSCallbackInfo& info) { JSViewAbstract::JsClip(info); if (info[0]->IsBoolean()) { SymbolModel::GetInstance()->SetClipEdge(); } } void JSSymbol::SetSymbolEffectOptions(const JSCallbackInfo& info) { if (info.Length() < 1 || !info[0]->IsObject()) { return; } auto symbolEffectObj = JSRef::Cast(info[0]); NG::SymbolEffectOptions symbolEffectOptions; parseSymbolEffect(symbolEffectObj, symbolEffectOptions); if (info.Length() > 1 && !info[1]->IsUndefined()) { parseSymbolSwitch(info[1], symbolEffectOptions); } SymbolModel::GetInstance()->SetSymbolEffectOptions(symbolEffectOptions); } void JSSymbol::parseSymbolEffect(const JSRef symbolEffectObj, NG::SymbolEffectOptions& symbolEffectOptions) { auto typeParam = symbolEffectObj->GetProperty("type"); if (typeParam->IsString()) { auto type = typeParam->ToString(); auto iter = SYMBOL_EFFECT_TYPE_MAP.find(type); if (iter != SYMBOL_EFFECT_TYPE_MAP.end()) { symbolEffectOptions.SetEffectType(iter->second); } } auto scopeTypeProperty = symbolEffectObj->GetProperty("scope"); if (scopeTypeProperty->IsNumber()) { auto scopeTypeNum = scopeTypeProperty->ToNumber(); if (scopeTypeNum >= static_cast(ScopeType::LAYER) && scopeTypeNum <= static_cast(ScopeType::WHOLE)) { symbolEffectOptions.SetScopeType(static_cast(scopeTypeNum)); } } auto commonSubTypeProperty = symbolEffectObj->GetProperty("direction"); if (commonSubTypeProperty->IsNumber()) { auto commonSubTypeNum = commonSubTypeProperty->ToNumber(); if (commonSubTypeNum >= static_cast(CommonSubType::DOWN) && commonSubTypeNum <= static_cast(CommonSubType::UP)) { symbolEffectOptions.SetCommonSubType(static_cast(commonSubTypeNum)); } } auto fillStyleProperty = symbolEffectObj->GetProperty("fillStyle"); if (fillStyleProperty->IsNumber()) { auto fillStyleNum = fillStyleProperty->ToNumber(); if (fillStyleNum >= static_cast(FillStyle::CUMULATIVE) && fillStyleNum <= static_cast(FillStyle::ITERATIVE)) { symbolEffectOptions.SetFillStyle(static_cast(fillStyleNum)); } } } void JSSymbol::parseSymbolSwitch(const JSRef jsVal, NG::SymbolEffectOptions& symbolEffectOptions) { if (jsVal->IsBoolean()) { symbolEffectOptions.SetIsActive(jsVal->ToBoolean()); } if (jsVal->IsNumber()) { int32_t triggerValue = -1; ParseJsInteger(jsVal, triggerValue); symbolEffectOptions.SetTriggerNum(triggerValue); } } } // namespace OHOS::Ace::Framework