/* * 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_stack.h" #include "base/log/ace_trace.h" #include "core/common/container.h" #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h" #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h" #include "frameworks/bridge/declarative_frontend/jsview/models/stack_model_impl.h" #include "frameworks/core/components_ng/pattern/stack/stack_model_ng.h" namespace OHOS::Ace { std::unique_ptr StackModel::instance_ = nullptr; StackModel* StackModel::GetInstance() { if (!instance_) { #ifdef NG_BUILD instance_.reset(new NG::StackModelNG()); #else if (Container::IsCurrentUseNewPipeline()) { instance_.reset(new NG::StackModelNG()); } else { instance_.reset(new Framework::StackModelImpl()); } #endif } return instance_.get(); } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { const static std::array ALIGNMENT_ARR { Alignment::TOP_LEFT, Alignment::TOP_CENTER, Alignment::TOP_RIGHT, Alignment::CENTER_LEFT, Alignment::CENTER, Alignment::CENTER_RIGHT, Alignment::BOTTOM_LEFT, Alignment::BOTTOM_CENTER, Alignment::BOTTOM_RIGHT }; void JSStack::SetStackFit(int value) { if (value >= static_cast(StackFit::KEEP) && value <= static_cast(StackFit::FIRST_CHILD)) { StackModel::GetInstance()->SetStackFit(static_cast(value)); } else { LOGE("Invalid value for stackfit"); } } void JSStack::SetOverflow(int value) { if (value >= static_cast(Overflow::CLIP) && value <= static_cast(Overflow::OBSERVABLE)) { StackModel::GetInstance()->SetOverflow(static_cast(value)); } else { LOGE("Invalid value for overflow"); } } void JSStack::SetAlignment(int value) { Alignment alignment = Alignment::TOP_LEFT; switch (value) { case 0: alignment = Alignment::TOP_LEFT; break; case 1: alignment = Alignment::TOP_CENTER; break; case 2: alignment = Alignment::TOP_RIGHT; break; case 3: alignment = Alignment::CENTER_LEFT; break; case 4: alignment = Alignment::CENTER; break; case 5: alignment = Alignment::CENTER_RIGHT; break; case 6: alignment = Alignment::BOTTOM_LEFT; break; case 7: alignment = Alignment::BOTTOM_CENTER; break; case 8: alignment = Alignment::BOTTOM_RIGHT; break; default: LOGE("Invalid value for alignment"); return; } StackModel::GetInstance()->SetAlignment(alignment); } void JSStack::SetWidth(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGE("The arg is wrong, it is supposed to have at least 1 arguments"); return; } SetWidth(info[0]); } void JSStack::SetWidth(const JSRef& jsValue) { JSViewAbstract::JsWidth(jsValue); StackModel::GetInstance()->SetHasWidth(); } void JSStack::SetHeight(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGE("The arg is wrong, it is supposed to have at least 1 arguments"); return; } SetHeight(info[0]); } void JSStack::SetHeight(const JSRef& jsValue) { JSViewAbstract::JsHeight(jsValue); StackModel::GetInstance()->SetHasHeight(); } void JSStack::SetSize(const JSCallbackInfo& info) { if (info.Length() < 1) { LOGE("The arg is wrong, it is supposed to have atleast 1 arguments"); return; } if (!info[0]->IsObject()) { LOGE("arg is not Object or String."); return; } JSRef sizeObj = JSRef::Cast(info[0]); SetWidth(sizeObj->GetProperty("width")); SetHeight(sizeObj->GetProperty("height")); } void JSStack::Create(const JSCallbackInfo& info) { Alignment alignment = Alignment::CENTER; if (info.Length() > 0 && info[0]->IsObject()) { JSRef obj = JSRef::Cast(info[0]); JSRef stackAlign = obj->GetProperty("alignContent"); if (stackAlign->IsNumber()) { int32_t value = stackAlign->ToNumber(); alignment = (value >= 0 && value < static_cast(ALIGNMENT_ARR.size())) ? ALIGNMENT_ARR[value] : Alignment::CENTER; } } StackModel::GetInstance()->Create(alignment); } void JSStack::JSBind(BindingTarget globalObj) { JSClass::Declare("Stack"); MethodOptions opt = MethodOptions::NONE; JSClass::StaticMethod("create", &JSStack::Create, opt); JSClass::StaticMethod("stackFit", &JSStack::SetStackFit, opt); JSClass::StaticMethod("overflow", &JSStack::SetOverflow, opt); JSClass::StaticMethod("alignContent", &JSStack::SetAlignment, opt); JSClass::StaticMethod("onTouch", &JSInteractableView::JsOnTouch); JSClass::StaticMethod("width", SetWidth); JSClass::StaticMethod("height", SetHeight); JSClass::StaticMethod("size", SetSize); JSClass::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey); JSClass::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete); JSClass::StaticMethod("onClick", &JSInteractableView::JsOnClick); JSClass::StaticMethod("onHover", &JSInteractableView::JsOnHover); JSClass::StaticMethod("onAppear", &JSInteractableView::JsOnAppear); JSClass::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear); JSClass::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage); JSClass::Inherit(); JSClass::Inherit(); JSClass::Bind<>(globalObj); } } // namespace OHOS::Ace::Framework