1 /* 2 * Copyright (c) 2025 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 COMPONENT_COMMON_H 17 #define COMPONENT_COMMON_H 18 19 #include <memory> 20 #include <string> 21 #include <type_traits> 22 #include "components/ui_view.h" 23 #include "json_visitor.h" 24 25 namespace Updater { 26 struct UxViewCommonInfo { 27 int x; 28 int y; 29 int w; 30 int h; 31 std::string id; 32 std::string type; 33 bool visible; 34 }; 35 36 struct Serializable { 37 virtual ~Serializable() = default; 38 39 virtual bool DeserializeFromJson(const JsonNode &componentNode, const JsonNode &defaultNode) = 0; 40 virtual const std::string &GetStructKey() const = 0; 41 }; 42 43 struct UxViewSpecificInfo : public Serializable { 44 virtual const std::string &GetType() const = 0; 45 virtual bool IsValid() const = 0; 46 }; 47 48 struct UxViewInfo { 49 UxViewCommonInfo commonInfo {}; 50 // Each derived view component has its own specific data item definitions. 51 std::unique_ptr<UxViewSpecificInfo> specificInfo; 52 }; 53 54 template <typename TComponent> 55 struct SpecificInfoWrapper : public UxViewSpecificInfo { 56 SpecificInfoWrapper() = default; 57 DISALLOW_COPY_MOVE(SpecificInfoWrapper); 58 59 using TSpecificInfo = typename TComponent::SpecificInfoType; 60 61 TSpecificInfo data; 62 DeserializeFromJsonSpecificInfoWrapper63 bool DeserializeFromJson(const JsonNode &componentNode, const JsonNode &defaultNode) override 64 { 65 return Visit<SETVAL>(componentNode, defaultNode, data); 66 } 67 GetStructKeySpecificInfoWrapper68 const std::string &GetStructKey() const override 69 { 70 static std::string key {Traits<TSpecificInfo>::STRUCT_KEY}; 71 return key; 72 } 73 GetTypeSpecificInfoWrapper74 const std::string &GetType() const override 75 { 76 static std::string type {TComponent::COMPONENT_TYPE}; 77 return type; 78 } 79 IsValidSpecificInfoWrapper80 bool IsValid() const override 81 { 82 return TComponent::IsValid(data); 83 } 84 }; 85 86 class ComponentInterface { 87 public: 88 virtual ~ComponentInterface() = default; 89 virtual const char *GetComponentType() = 0; 90 virtual OHOS::UIView *GetOhosView() = 0; 91 virtual void SetViewCommonInfo(const UxViewCommonInfo &common) = 0; 92 }; 93 94 template <typename T> 95 struct IsUpdaterComponent { 96 inline static constexpr bool value = std::is_base_of_v<ComponentInterface, T>; 97 }; 98 99 template <typename T> 100 using EnableIfIsUpdaterComponent = std::enable_if_t<IsUpdaterComponent<T>::value>; 101 102 template <typename T> 103 using EnableIfNotUpdaterComponent = std::enable_if_t<!IsUpdaterComponent<T>::value>; 104 105 } // namespace Updater 106 107 #endif 108