1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "frameworks/bridge/declarative_frontend/jsview/js_flex.h" 17 18 #include "core/components_ng/base/view_abstract_model.h" 19 #include "core/components_ng/pattern/flex/flex_model.h" 20 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h" 21 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h" 22 #include "frameworks/bridge/declarative_frontend/jsview/models/flex_model_impl.h" 23 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h" 24 #include "frameworks/core/components_ng/pattern/flex/flex_model_ng.h" 25 26 namespace OHOS::Ace { 27 28 std::unique_ptr<FlexModel> FlexModel::instance_ = nullptr; 29 std::mutex FlexModel::mutex_; 30 GetInstance()31FlexModel* FlexModel::GetInstance() 32 { 33 if (!instance_) { 34 std::lock_guard<std::mutex> lock(mutex_); 35 if (!instance_) { 36 #ifdef NG_BUILD 37 instance_.reset(new NG::FlexModelNG()); 38 #else 39 if (Container::IsCurrentUseNewPipeline()) { 40 instance_.reset(new NG::FlexModelNG()); 41 } else { 42 instance_.reset(new Framework::FlexModelImpl()); 43 } 44 #endif 45 } 46 } 47 return instance_.get(); 48 } 49 50 } // namespace OHOS::Ace 51 namespace OHOS::Ace::Framework { 52 SetFillParent()53void JSFlex::SetFillParent() 54 { 55 /* Deprecated */ 56 } 57 SetWrapContent()58void JSFlex::SetWrapContent() 59 { 60 /* Deprecated */ 61 } 62 SetJustifyContent(int32_t value)63void JSFlex::SetJustifyContent(int32_t value) 64 { 65 if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) || 66 (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) || 67 (value == static_cast<int32_t>(FlexAlign::SPACE_AROUND)) || 68 (value == static_cast<int32_t>(FlexAlign::SPACE_BETWEEN)) || 69 (value == static_cast<int32_t>(FlexAlign::SPACE_EVENLY))) { 70 FlexModel::GetInstance()->SetJustifyContent(value); 71 } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) { 72 FlexModel::GetInstance()->SetJustifyContent(static_cast<int32_t>(FlexAlign::FLEX_START)); 73 } 74 } 75 SetAlignItems(int32_t value)76void JSFlex::SetAlignItems(int32_t value) 77 { 78 if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) || 79 (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) || 80 (value == static_cast<int32_t>(FlexAlign::STRETCH))) { 81 FlexModel::GetInstance()->SetAlignItems(value); 82 } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) { 83 FlexModel::GetInstance()->SetAlignItems(static_cast<int32_t>(FlexAlign::FLEX_START)); 84 } 85 } 86 SetAlignContent(int32_t value)87void JSFlex::SetAlignContent(int32_t value) 88 { 89 if ((value == static_cast<int32_t>(WrapAlignment::START)) || 90 (value == static_cast<int32_t>(WrapAlignment::CENTER)) || (value == static_cast<int32_t>(WrapAlignment::END)) || 91 (value == static_cast<int32_t>(WrapAlignment::SPACE_AROUND)) || 92 (value == static_cast<int32_t>(WrapAlignment::SPACE_BETWEEN)) || 93 (value == static_cast<int32_t>(WrapAlignment::STRETCH))) { 94 FlexModel::GetInstance()->SetAlignContent(value); 95 } 96 } 97 JsHeight(const JSCallbackInfo & info)98void JSFlex::JsHeight(const JSCallbackInfo& info) 99 { 100 if (info.Length() < 1) { 101 return; 102 } 103 104 SetHeight(info[0]); 105 } 106 SetHeight(const JSRef<JSVal> & jsValue)107void JSFlex::SetHeight(const JSRef<JSVal>& jsValue) 108 { 109 if (!JSViewAbstract::JsHeight(jsValue)) { 110 // JsHeight return false, just return. 111 return; 112 } 113 FlexModel::GetInstance()->SetHasHeight(); 114 } 115 JsWidth(const JSCallbackInfo & info)116void JSFlex::JsWidth(const JSCallbackInfo& info) 117 { 118 if (info.Length() < 1) { 119 return; 120 } 121 122 SetWidth(info[0]); 123 } 124 SetWidth(const JSRef<JSVal> & jsValue)125void JSFlex::SetWidth(const JSRef<JSVal>& jsValue) 126 { 127 if (!JSViewAbstract::JsWidth(jsValue)) { 128 // JsWidth return false, just return. 129 return; 130 } 131 FlexModel::GetInstance()->SetHasWidth(); 132 } 133 JsSize(const JSCallbackInfo & info)134void JSFlex::JsSize(const JSCallbackInfo& info) 135 { 136 if (info.Length() < 1) { 137 return; 138 } 139 140 if (info[0]->IsUndefined()) { 141 ViewAbstractModel::GetInstance()->ClearWidthOrHeight(true); 142 ViewAbstractModel::GetInstance()->ClearWidthOrHeight(false); 143 return; 144 } 145 146 if (!info[0]->IsObject()) { 147 return; 148 } 149 150 JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]); 151 SetWidth(sizeObj->GetProperty("width")); 152 SetHeight(sizeObj->GetProperty("height")); 153 } 154 155 }; // namespace OHOS::Ace::Framework 156