1 /* 2 * Copyright (c) 2023 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_PATTERNS_MENU_MENU_ITEM_MODEL_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_ITEM_MODEL_H 18 19 #include <functional> 20 #include <optional> 21 22 #include "core/components_ng/base/frame_node.h" 23 #include "core/components/common/properties/text_style.h" 24 25 namespace OHOS::Ace { 26 enum class MenuItemFontColorType { 27 FONT_COLOR, 28 LABEL_FONT_COLOR 29 }; 30 enum class MenuItemFontSizeType { 31 FONT_SIZE, 32 LABEL_FONT_SIZE 33 }; 34 enum class MenuItemStringType { 35 SELECT_ICON, 36 CONTENT, 37 LABEL_INFO 38 }; 39 enum class MenuItemFontFamilyType { 40 FONT_FAMILY, 41 LABEL_FONT_FAMILY 42 }; 43 enum class MenuItemIconType { 44 START_ICON, 45 END_ICON 46 }; 47 template<typename T> 48 class ResourceHolder { 49 public: 50 virtual ~ResourceHolder() = default; 51 struct ResourceUpdater { 52 RefPtr<ResourceObject> resObj; 53 std::function<void(const RefPtr<ResourceObject>&, T&)> updateFunc; 54 }; 55 using ResourceMap = std::unordered_map<std::string, ResourceUpdater>; AddResource(const std::string & key,const RefPtr<ResourceObject> & resObj,std::function<void (const RefPtr<ResourceObject> &,T &)> && updateFunc)56 void AddResource(const std::string& key, const RefPtr<ResourceObject>& resObj, 57 std::function<void(const RefPtr<ResourceObject>&, T&)>&& updateFunc) 58 { 59 if (resObj == nullptr || !updateFunc) { 60 return; 61 } 62 resources_[key] = {resObj, std::move(updateFunc)}; 63 } GetResource(const std::string & key)64 const RefPtr<ResourceObject> GetResource(const std::string& key) const 65 { 66 auto it = resources_.find(key); 67 return (it != resources_.end()) ? it->second.resObj : nullptr; 68 } HasResources()69 bool HasResources() const 70 { 71 return !resources_.empty(); 72 } ReloadResources(T & context)73 void ReloadResources(T& context) 74 { 75 for (const auto& [key, updater] : resources_) { 76 updater.updateFunc(updater.resObj, context); 77 } 78 } 79 protected: 80 ResourceMap resources_; 81 }; 82 struct MenuItemProperties : public ResourceHolder<MenuItemProperties> { 83 std::string content; 84 std::optional<ImageSourceInfo> startIcon; 85 std::optional<ImageSourceInfo> endIcon; 86 std::optional<std::string> labelInfo; 87 std::optional<std::function<void()>> buildFunc; 88 std::function<void(WeakPtr<NG::FrameNode>)> startApply; 89 std::function<void(WeakPtr<NG::FrameNode>)> endApply; 90 }; 91 class ACE_FORCE_EXPORT MenuItemModel { 92 public: 93 static MenuItemModel* GetInstance(); 94 virtual ~MenuItemModel() = default; 95 96 // createMenuItem with custom 97 virtual void Create(const RefPtr<NG::UINode>& customNode); 98 virtual void Create(const MenuItemProperties& props); 99 virtual void SetSelected(bool isSelected = false); 100 virtual void SetSelectIcon(bool isShow = false); 101 virtual void SetSelectIconSymbol(std::function<void(WeakPtr<NG::FrameNode>)> &&symbolApply); 102 virtual void SetSelectIconSrc(const std::string& src); 103 virtual void SetOnChange(std::function<void(bool)>&& onChange); 104 virtual void SetFontSize(const Dimension& fontSize); 105 virtual void SetFontWeight(FontWeight weight); 106 virtual void SetFontStyle(Ace::FontStyle style); 107 virtual void SetFontColor(const std::optional<Color>& color); 108 virtual void SetFontFamily(const std::vector<std::string>& families); 109 virtual void SetLabelFontSize(const Dimension& fontSize); 110 virtual void SetLabelFontWeight(FontWeight weight); 111 virtual void SetLabelFontStyle(Ace::FontStyle style); 112 virtual void SetLabelFontColor(const std::optional<Color>& color); 113 virtual void SetLabelFontFamily(const std::vector<std::string>& families); 114 virtual void SetSelectedChangeEvent(std::function<void(bool)>&& selectedChangeEvent); 115 virtual void CreateWithColorResourceObj(const RefPtr<ResourceObject>& resObj, MenuItemFontColorType type); 116 virtual void CreateWithDimensionFpResourceObj(const RefPtr<ResourceObject>& resObj, MenuItemFontSizeType type); 117 virtual void CreateWithFontFamilyResourceObj(const RefPtr<ResourceObject>& resObj, MenuItemFontFamilyType type); 118 virtual void CreateWithStringResourceObj(const RefPtr<ResourceObject>& resObj, MenuItemStringType type); 119 virtual void CreateWithMediaResourceObj(const RefPtr<ResourceObject>& resObj, const MenuItemIconType type); 120 121 private: 122 static std::unique_ptr<MenuItemModel> instance_; 123 static std::mutex mutex_; 124 }; 125 } // namespace OHOS::Ace 126 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_ITEM_MODEL_H 127