/* * 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_column.h" #include "base/log/ace_trace.h" #include "core/components_ng/pattern/linear_layout/column_model.h" #include "core/components_ng/pattern/linear_layout/column_model_ng.h" #include "frameworks/bridge/declarative_frontend/jsview/models/column_model_impl.h" namespace OHOS::Ace { std::unique_ptr ColumnModel::instance_ = nullptr; std::mutex ColumnModel::mutex_; ColumnModel* ColumnModel::GetInstance() { if (!instance_) { std::lock_guard lock(mutex_); if (!instance_) { #ifdef NG_BUILD instance_.reset(new NG::ColumnModelNG()); #else if (Container::IsCurrentUseNewPipeline()) { instance_.reset(new NG::ColumnModelNG()); } else { instance_.reset(new Framework::ColumnModelImpl()); } #endif } } return instance_.get(); } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { std::string JSColumn::inspectorTag_ = ""; void JSColumn::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.IsValid() ? value : Dimension(); } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) { space = Dimension(); } } HorizontalAlignDeclaration* 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(); } } ColumnModel::GetInstance()->Create(space, declaration, inspectorTag_); } void JSColumn::CreateWithWrap(const JSCallbackInfo& info) { ColumnModel::GetInstance()->CreateWithWrap(); } void JSColumn::SetInspectorTag(const std::string& inspectorTag) { inspectorTag_ = inspectorTag; } void JSColumn::ClearInspectorTag() { inspectorTag_.clear(); } void JSColumn::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))) { ColumnModel::GetInstance()->SetAlignItems(static_cast(value)); } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) { ColumnModel::GetInstance()->SetAlignItems(FlexAlign::CENTER); } } void JSColumn::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))) { ColumnModel::GetInstance()->SetJustifyContent(static_cast(value)); } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) { ColumnModel::GetInstance()->SetJustifyContent(FlexAlign::FLEX_START); } } void JSColumn::SetReverse(const JSCallbackInfo& info) { if (info[0]->IsBoolean()) { ColumnModel::GetInstance()->SetIsReverse(info[0]->ToBoolean()); } else { ColumnModel::GetInstance()->SetIsReverse(true); } } void JSColumn::JSBind(BindingTarget globalObj) { JSClass::Declare("Column"); MethodOptions opt = MethodOptions::NONE; JSClass::StaticMethod("create", &JSColumn::Create, opt); JSClass::StaticMethod("createWithWrap", &JSColumn::CreateWithWrap, opt); JSClass::StaticMethod("fillParent", &JSFlex::SetFillParent, opt); JSClass::StaticMethod("wrapContent", &JSFlex::SetWrapContent, opt); JSClass::StaticMethod("justifyContent", &JSColumn::SetJustifyContent, opt); JSClass::StaticMethod("alignItems", &JSColumn::SetAlignItems, opt); JSClass::StaticMethod("reverse", &JSColumn::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("onAttach", &JSInteractableView::JsOnAttach); JSClass::StaticMethod("onAppear", &JSInteractableView::JsOnAppear); JSClass::StaticMethod("onDetach", &JSInteractableView::JsOnDetach); JSClass::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear); 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("onPan", &JSInteractableView::JsOnPan); JSClass::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage); JSClass::StaticMethod("pointLight", &JSViewAbstract::JsPointLight, opt); JSClass::InheritAndBind(globalObj); JSClass::Declare("HorizontalAlignDeclaration"); JSClass::Bind( globalObj, HorizontalAlignDeclaration::ConstructorCallback, HorizontalAlignDeclaration::DestructorCallback); } void HorizontalAlignDeclaration::ConstructorCallback(const JSCallbackInfo& args) { auto align = HorizontalAlign::CENTER; if (args.Length() > 0 && args[0]->IsNumber()) { auto value = args[0]->ToNumber(); if (value >= static_cast(HorizontalAlign::START) && value <= static_cast(HorizontalAlign::END)) { align = static_cast(value); } } auto obj = new HorizontalAlignDeclaration(align); args.SetReturnValue(obj); } void HorizontalAlignDeclaration::DestructorCallback(HorizontalAlignDeclaration* obj) { delete obj; } } // namespace OHOS::Ace::Framework