/* * Copyright (c) 2021-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 "bridge/declarative_frontend/jsview/js_badge.h" #include "base/geometry/dimension.h" #include "base/log/ace_trace.h" #include "base/utils/utils.h" #include "core/components/common/layout/grid_container_info.h" #include "core/components_ng/pattern/badge/badge_model_ng.h" #include "frameworks/bridge/declarative_frontend/jsview/models/badge_model_impl.h" namespace OHOS::Ace { std::unique_ptr BadgeModel::instance_ = nullptr; std::mutex BadgeModel::mutex_; BadgeModel* BadgeModel::GetInstance() { if (!instance_) { std::lock_guard lock(mutex_); if (!instance_) { #ifdef NG_BUILD instance_.reset(new NG::BadgeModelNG()); #else if (Container::IsCurrentUseNewPipeline()) { instance_.reset(new NG::BadgeModelNG()); } else { instance_.reset(new Framework::BadgeModelImpl()); } #endif } } return instance_.get(); } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { void JSBadge::Create(const JSCallbackInfo& info) { if (!info[0]->IsObject()) { return; } BadgeParameters badgeParameters = CreateBadgeParameters(info); BadgeModel::GetInstance()->Create(badgeParameters); } BadgeParameters JSBadge::CreateBadgeParameters(const JSCallbackInfo& info) { BadgeParameters badgeParameters; if (!info[0]->IsObject()) { return badgeParameters; } auto badgeTheme = GetTheme(); CHECK_NULL_RETURN_NOLOG(badgeTheme, BadgeParameters()); auto obj = JSRef::Cast(info[0]); auto value = obj->GetProperty("value"); if (!value->IsNull() && value->IsString()) { auto label = value->ToString(); badgeParameters.badgeValue = label; } auto position = obj->GetProperty("position"); if (!position->IsNull() && position->IsNumber()) { badgeParameters.isPositionXy = false; badgeParameters.badgePosition = position->ToNumber(); } else if (!position->IsNull() && position->IsObject()) { badgeParameters.isPositionXy = true; auto postionValue = JSRef::Cast(position); JSRef xVal = postionValue->GetProperty("x"); JSRef yVal = postionValue->GetProperty("y"); CalcDimension dimenX; CalcDimension dimenY; bool xResult = ParseJsDimensionVp(xVal, dimenX); bool yResult = ParseJsDimensionVp(yVal, dimenY); if (!(xResult || yResult)) { badgeParameters.badgePositionX = badgeTheme->GetBadgePositionX(); badgeParameters.badgePositionY = badgeTheme->GetBadgePositionY(); } else { badgeParameters.badgePositionX = dimenX; badgeParameters.badgePositionY = dimenY; } } auto style = obj->GetProperty("style"); if (!style->IsNull() && style->IsObject()) { auto value = JSRef::Cast(style); JSRef colorValue = value->GetProperty("color"); JSRef fontSizeValue = value->GetProperty("fontSize"); JSRef badgeSizeValue = value->GetProperty("badgeSize"); JSRef badgeColorValue = value->GetProperty("badgeColor"); JSRef borderColorValue = value->GetProperty("borderColor"); JSRef borderWidthValue = value->GetProperty("borderWidth"); JSRef fontWeightValue = value->GetProperty("fontWeight"); Color colorVal; if (ParseJsColor(colorValue, colorVal)) { badgeParameters.badgeTextColor = colorVal; } CalcDimension fontSize; if (ParseJsDimensionNG(fontSizeValue, fontSize, DimensionUnit::FP)) { if (fontSize.IsNonNegative() && fontSize.Unit() != DimensionUnit::PERCENT) { badgeParameters.badgeFontSize = fontSize; } else { badgeParameters.badgeFontSize = badgeTheme->GetBadgeFontSize(); } } else if (!fontSizeValue->IsUndefined()) { badgeParameters.badgeFontSize = badgeTheme->GetBadgeFontSize(); } else { badgeParameters.badgeFontSize = UNDEFINED_DIMENSION; } CalcDimension badgeSize; if (ParseJsDimensionNG(badgeSizeValue, badgeSize, DimensionUnit::FP)) { if (badgeSize.IsNonNegative() && badgeSize.Unit() != DimensionUnit::PERCENT) { badgeParameters.badgeCircleSize = badgeSize; } else { badgeParameters.badgeCircleSize = badgeTheme->GetBadgeCircleSize(); } } else { badgeParameters.badgeCircleSize = badgeTheme->GetBadgeCircleSize(); } Color color; if (ParseJsColor(badgeColorValue, color)) { badgeParameters.badgeColor = color; } CalcDimension borderWidth; if (ParseJsDimensionVp(borderWidthValue, borderWidth)) { badgeParameters.badgeBorderWidth = borderWidth; } else { badgeParameters.badgeBorderWidth = badgeTheme->GetBadgeBorderWidth(); } Color borderColor; if (ParseJsColor(borderColorValue, borderColor)) { badgeParameters.badgeBorderColor = borderColor; } else { badgeParameters.badgeBorderColor = badgeTheme->GetBadgeBorderColor(); } std::string fontWeight; if (fontWeightValue->IsNumber()) { fontWeight = std::to_string(fontWeightValue->ToNumber()); } else { if (!ParseJsString(fontWeightValue, fontWeight)) { badgeParameters.badgeFontWeight = FontWeight::NORMAL; } } badgeParameters.badgeFontWeight = ConvertStrToFontWeight(fontWeight); } auto count = obj->GetProperty("count"); if (!count->IsNull() && count->IsNumber()) { badgeParameters.badgeCount = count->ToNumber(); } auto maxCount = obj->GetProperty("maxCount"); if (!maxCount->IsNull() && maxCount->IsNumber()) { badgeParameters.badgeMaxCount = maxCount->ToNumber(); } else { badgeParameters.badgeMaxCount = badgeTheme->GetMaxCount(); } return badgeParameters; } void JSBadge::JSBind(BindingTarget globalObj) { JSClass::Declare("Badge"); MethodOptions opt = MethodOptions::NONE; JSClass::StaticMethod("create", &JSBadge::Create, opt); JSClass::StaticMethod("onTouch", &JSInteractableView::JsOnTouch); JSClass::StaticMethod("onAppear", &JSInteractableView::JsOnAppear); JSClass::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear); JSClass::InheritAndBind(globalObj); } void JSBadge::SetDefaultTheme(OHOS::Ace::RefPtr& badge) { auto badgeTheme = GetTheme(); if (!badgeTheme) { LOGE("Get badge theme error"); return; } badge->SetBadgeColor(badgeTheme->GetBadgeColor()); badge->SetMessageCount(badgeTheme->GetMessageCount()); badge->SetBadgePosition(badgeTheme->GetBadgePosition()); badge->SetBadgePositionX(badgeTheme->GetBadgePositionX()); badge->SetBadgePositionY(badgeTheme->GetBadgePositionY()); badge->SetIsPositionXy(badgeTheme->GetIsPositionXy()); badge->SetBadgeTextColor(badgeTheme->GetBadgeTextColor()); badge->SetBadgeCircleSize(badgeTheme->GetBadgeCircleSize()); } void JSBadge::SetCustomizedTheme(const JSRef& obj, OHOS::Ace::RefPtr& badge) { auto count = obj->GetProperty("count"); if (!count->IsNull() && count->IsNumber()) { auto value = count->ToNumber(); badge->SetMessageCount(value); } SetPosition(obj, badge); auto maxCount = obj->GetProperty("maxCount"); if (!maxCount->IsNull() && maxCount->IsNumber()) { auto value = maxCount->ToNumber(); badge->SetMaxCount(value); } auto style = obj->GetProperty("style"); if (!style->IsNull() && style->IsObject()) { auto value = JSRef::Cast(style); JSRef colorValue = value->GetProperty("color"); JSRef fontSizeValue = value->GetProperty("fontSize"); JSRef badgeSizeValue = value->GetProperty("badgeSize"); JSRef badgeColorValue = value->GetProperty("badgeColor"); Color colorVal; if (ParseJsColor(colorValue, colorVal)) { badge->SetBadgeTextColor(colorVal); } CalcDimension fontSize; if (ParseJsDimensionFp(fontSizeValue, fontSize)) { badge->SetBadgeFontSize(fontSize); } CalcDimension badgeSize; if (ParseJsDimensionFp(badgeSizeValue, badgeSize)) { if (badgeSize.IsNonNegative()) { badge->SetBadgeCircleSize(badgeSize); } } Color badgeColor; if (ParseJsColor(badgeColorValue, badgeColor)) { badge->SetBadgeColor(badgeColor); } } auto value = obj->GetProperty("value"); if (!value->IsNull() && value->IsString()) { auto label = value->ToString(); badge->SetBadgeLabel(label); } } void JSBadge::SetPosition(const JSRef& obj, OHOS::Ace::RefPtr& badge) { auto position = obj->GetProperty("position"); if (!position->IsNull() && position->IsNumber()) { badge->SetIsPositionXy(false); auto value = position->ToNumber(); badge->SetBadgePosition(static_cast(value)); } else if (!position->IsNull() && position->IsObject()) { badge->SetIsPositionXy(true); auto postionValue = JSRef::Cast(position); JSRef xVal = postionValue->GetProperty("x"); JSRef yVal = postionValue->GetProperty("y"); CalcDimension dimenX; CalcDimension dimenY; bool xResult = ParseJsDimensionVp(xVal, dimenX); bool yResult = ParseJsDimensionVp(yVal, dimenY); if (xResult && yResult) { badge->SetBadgePositionX(dimenX); badge->SetBadgePositionY(dimenY); } } } } // namespace OHOS::Ace::Framework