1 /* 2 * Copyright (c) 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_LINEAR_SPLIT_LINEAR_SPLIT_MODEL_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_LINEAR_SPLIT_LINEAR_SPLIT_MODEL_H 18 19 #include <functional> 20 #include <memory> 21 #include <mutex> 22 #include <unordered_map> 23 24 #include "base/geometry/dimension.h" 25 #include "base/memory/referenced.h" 26 #include "core/common/resource/resource_object.h" 27 28 namespace OHOS::Ace::NG { 29 30 enum class SplitType { 31 ROW_SPLIT, 32 COLUMN_SPLIT, 33 }; 34 35 struct ColumnSplitDivider final { 36 Dimension startMargin = 0.0_vp; 37 Dimension endMargin = 0.0_vp; 38 struct resourceUpdater { 39 RefPtr<ResourceObject> resObj; 40 std::function<void(const RefPtr<ResourceObject>&, NG::ColumnSplitDivider&)> updateFunc; 41 }; 42 std::unordered_map<std::string, resourceUpdater> resMap_; 43 bool operator==(const ColumnSplitDivider& columnSplitDivider) const 44 { 45 return (startMargin == columnSplitDivider.startMargin) && (endMargin == columnSplitDivider.endMargin); 46 } 47 AddResourcefinal48 void AddResource( 49 const std::string& key, 50 const RefPtr<ResourceObject>& resObj, 51 std::function<void(const RefPtr<ResourceObject>&, NG::ColumnSplitDivider&)>&& updateFunc) 52 { 53 if (resObj == nullptr || !updateFunc) { 54 return; 55 } 56 resMap_[key] = {resObj, std::move(updateFunc)}; 57 } 58 ReloadResourcesfinal59 void ReloadResources() 60 { 61 for (const auto& [key, resourceUpdater] : resMap_) { 62 resourceUpdater.updateFunc(resourceUpdater.resObj, *this); 63 } 64 } 65 }; 66 67 } // namespace OHOS::Ace::NG 68 69 namespace OHOS::Ace { 70 71 class ACE_FORCE_EXPORT LinearSplitModel { 72 public: 73 static LinearSplitModel* GetInstance(); 74 virtual ~LinearSplitModel() = default; 75 76 virtual void Create(NG::SplitType splitType) = 0; 77 virtual void SetResizable(NG::SplitType splitType, bool resizable) = 0; 78 virtual void SetDivider(NG::SplitType splitType, const NG::ColumnSplitDivider& divider) = 0; ResetResObj(const std::string & key)79 virtual void ResetResObj(const std::string& key) {}; 80 81 private: 82 static std::unique_ptr<LinearSplitModel> instance_; 83 static std::mutex mutex_; 84 }; 85 86 } // namespace OHOS::Ace 87 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_LINEAR_SPLIT_LINEAR_SPLIT_MODEL_H 88