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 COMPONENT_COMMON_H 17 #define COMPONENT_COMMON_H 18 19 #include <string> 20 #include "components/ui_view.h" 21 22 namespace Updater { 23 struct UxViewCommonInfo { 24 int x; 25 int y; 26 int w; 27 int h; 28 std::string id; 29 std::string type; 30 bool visible; 31 }; 32 33 class ComponentInterface { 34 public: 35 virtual ~ComponentInterface() = default; 36 virtual const char *GetComponentType() = 0; 37 virtual OHOS::UIView *GetOhosView() = 0; 38 }; 39 40 // crtp util to ensure type conversion safety and make component code less repetitive 41 template<class Component> 42 class ComponentCommon : public ComponentInterface { 43 public: 44 ~ComponentCommon() override = default; GetComponentType()45 const char *GetComponentType() override 46 { 47 static_assert(Component::COMPONENT_TYPE != nullptr, "you must not assign a nullptr to COMPONNET_TYPE"); 48 return Component::COMPONENT_TYPE; 49 } GetOhosView()50 OHOS::UIView *GetOhosView() override 51 { 52 return static_cast<Component *>(this); 53 } SetViewCommonInfo(const UxViewCommonInfo & common)54 void SetViewCommonInfo(const UxViewCommonInfo &common) 55 { 56 viewId_ = common.id; 57 auto child = static_cast<Component *>(this); 58 child->SetPosition(common.x, common.y, common.w, common.h); 59 child->SetVisible(common.visible); 60 child->SetViewId(viewId_.c_str()); 61 } 62 protected: 63 std::string viewId_ {}; 64 private: 65 friend Component; 66 ComponentCommon() = default; 67 }; 68 } 69 70 71 #endif