/* * Copyright (c) 2021-2022 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_row.h" #include "base/log/ace_trace.h" #include "core/components_ng/pattern/linear_layout/row_model.h" #include "core/components_ng/pattern/linear_layout/row_model_ng.h" #include "frameworks/bridge/declarative_frontend/jsview/models/row_model_impl.h" namespace OHOS::Ace { std::unique_ptr RowModel::instance_ = nullptr; std::mutex RowModel::mutex_; RowModel* RowModel::GetInstance() { if (!instance_) { std::lock_guard lock(mutex_); if (!instance_) { #ifdef NG_BUILD instance_.reset(new NG::RowModelNG()); #else if (Container::IsCurrentUseNewPipeline()) { instance_.reset(new NG::RowModelNG()); } else { instance_.reset(new Framework::RowModelImpl()); } #endif } } return instance_.get(); } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { void JSRow::Create(const JSCallbackInfo& info) { std::optional space; if (info.Length() > 0 && info[0]->IsObject()) { JSRef obj = JSRef::Cast(info[0]); JSRef spaceVal = obj->GetProperty("space"); CalcDimension value; if (ParseJsDimensionVp(spaceVal, value)) { space = value; } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) { space = Dimension(); } } VerticalAlignDeclaration* declaration = nullptr; if (info.Length() > 0 && info[0]->IsObject()) { JSRef obj = JSRef::Cast(info[0]); JSRef useAlign = obj->GetProperty("useAlign"); if (useAlign->IsObject()) { declaration = JSRef::Cast(useAlign)->Unwrap(); } } RowModel::GetInstance()->Create(space, declaration, ""); } void JSRow::CreateWithWrap(const JSCallbackInfo& info) { RowModel::GetInstance()->CreateWithWrap(); } void JSRow::SetAlignItems(int32_t value) { if ((value == static_cast(FlexAlign::FLEX_START)) || (value == static_cast(FlexAlign::FLEX_END)) || (value == static_cast(FlexAlign::CENTER)) || (value == static_cast(FlexAlign::STRETCH))) { RowModel::GetInstance()->SetAlignItems(static_cast(value)); } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) { RowModel::GetInstance()->SetAlignItems(FlexAlign::CENTER); // FIXME: we have a design issue here, setters return void, can not signal error to JS LOGE("invalid value for justifyContent"); } } void JSRow::SetJustifyContent(int32_t value) { if ((value == static_cast(FlexAlign::FLEX_START)) || (value == static_cast(FlexAlign::FLEX_END)) || (value == static_cast(FlexAlign::CENTER)) || (value == static_cast(FlexAlign::SPACE_BETWEEN)) || (value == static_cast(FlexAlign::SPACE_AROUND)) || (value == static_cast(FlexAlign::SPACE_EVENLY))) { RowModel::GetInstance()->SetJustifyContent(static_cast(value)); } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) { RowModel::GetInstance()->SetJustifyContent(FlexAlign::FLEX_START); LOGE("invalid value for justifyContent"); } } void JSRow::SetReverse(const JSCallbackInfo& info) { if (info[0]->IsBoolean()) { RowModel::GetInstance()->SetIsReverse(info[0]->ToBoolean()); } else { RowModel::GetInstance()->SetIsReverse(true); } } void JSRow::JSBind(BindingTarget globalObj) { JSClass::Declare("Row"); MethodOptions opt = MethodOptions::NONE; JSClass::StaticMethod("create", &JSRow::Create, opt); JSClass::StaticMethod("createWithWrap", &JSRow::CreateWithWrap, opt); JSClass::StaticMethod("fillParent", &JSFlex::SetFillParent, opt); JSClass::StaticMethod("wrapContent", &JSFlex::SetWrapContent, opt); JSClass::StaticMethod("justifyContent", &JSRow::SetJustifyContent, opt); JSClass::StaticMethod("alignItems", &JSRow::SetAlignItems, opt); JSClass::StaticMethod("reverse", &JSRow::SetReverse, opt); JSClass::StaticMethod("alignContent", &JSFlex::SetAlignContent, opt); JSClass::StaticMethod("height", &JSFlex::JsHeight, opt); JSClass::StaticMethod("width", &JSFlex::JsWidth, opt); JSClass::StaticMethod("size", &JSFlex::JsSize, opt); JSClass::StaticMethod("onTouch", &JSInteractableView::JsOnTouch); JSClass::StaticMethod("onHover", &JSInteractableView::JsOnHover); JSClass::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey); JSClass::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete); JSClass::StaticMethod("onClick", &JSInteractableView::JsOnClick); JSClass::StaticMethod("onAttach", &JSInteractableView::JsOnAttach); JSClass::StaticMethod("onAppear", &JSInteractableView::JsOnAppear); JSClass::StaticMethod("onDetach", &JSInteractableView::JsOnDetach); JSClass::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear); JSClass::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage); JSClass::StaticMethod("pointLight", &JSViewAbstract::JsPointLight, opt); JSClass::InheritAndBind(globalObj); JSClass::Declare("VerticalAlignDeclaration"); JSClass::Bind( globalObj, VerticalAlignDeclaration::ConstructorCallback, VerticalAlignDeclaration::DestructorCallback); } void VerticalAlignDeclaration::ConstructorCallback(const JSCallbackInfo& args) { auto align = VerticalAlign::CENTER; if (args.Length() > 0 && args[0]->IsNumber()) { auto value = args[0]->ToNumber(); if (value >= static_cast(VerticalAlign::TOP) && value <= static_cast(VerticalAlign::BOTTOM)) { align = static_cast(value); } } auto obj = new VerticalAlignDeclaration(align); args.SetReturnValue(obj); } void VerticalAlignDeclaration::DestructorCallback(VerticalAlignDeclaration* obj) { delete obj; } } // namespace OHOS::Ace::Framework