1 /* 2 * Copyright (c) 2021 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_IMAGE_IMAGE_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_IMAGE_THEME_H 18 19 #include "core/components/theme/theme.h" 20 #include "core/components/theme/theme_constants.h" 21 #include "core/components/theme/theme_constants_defines.h" 22 #include "core/components/theme/theme_manager.h" 23 #include "frameworks/bridge/common/utils/utils.h" 24 25 namespace OHOS::Ace { 26 27 /** 28 * ImageTheme defines color and styles of Image. ImageTheme should be built 29 * using ImageTheme::Builder. 30 */ 31 class ImageTheme : public virtual Theme { 32 DECLARE_ACE_TYPE(ImageTheme, Theme); 33 34 public: 35 class Builder { 36 public: 37 Builder() = default; 38 ~Builder() = default; 39 Build(const RefPtr<ThemeConstants> & themeConstants)40 RefPtr<ImageTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 41 { 42 RefPtr<ImageTheme> theme = AceType::MakeRefPtr<ImageTheme>(); 43 if (!themeConstants) { 44 return theme; 45 } 46 auto themeStyle = themeConstants->GetThemeStyle(); 47 theme->fillColor_ = themeConstants->GetColor(THEME_IMAGE_FILL_COLOR); 48 ParsePattern(themeConstants, theme); 49 return theme; 50 } 51 52 private: ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<ImageTheme> & theme)53 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<ImageTheme>& theme) const 54 { 55 RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_IMAGE); 56 if (!pattern) { 57 LOGW("find pattern of image fail"); 58 return; 59 } 60 theme->fillColor_ = pattern->GetAttr<Color>("fill_color", Color()); 61 auto draggable = pattern->GetAttr<std::string>("draggable", "0"); 62 theme->draggable_ = StringUtils::StringToInt(draggable); 63 theme->minEdgeAntialiasing_ = pattern->GetAttr<double>("min_edge_antialiasing", 0.0); 64 theme->cornerRadius_ = pattern->GetAttr<Dimension>("image_animator_corner_radius", 0.0_vp); 65 theme->clipEdge_ = static_cast<bool>(pattern->GetAttr<double>("image_animator_clip_edge", 0.0)); 66 } 67 }; 68 69 ~ImageTheme() override = default; 70 GetClipEdge()71 bool GetClipEdge() const 72 { 73 return clipEdge_; 74 } 75 GetFillColor()76 const Color& GetFillColor() const 77 { 78 return fillColor_; 79 } 80 GetDraggable()81 bool GetDraggable() const 82 { 83 return draggable_; 84 } 85 GetMinEdgeAntialiasing()86 float GetMinEdgeAntialiasing() const 87 { 88 return minEdgeAntialiasing_; 89 } 90 GetCornerRadius()91 const Dimension& GetCornerRadius() const 92 { 93 return cornerRadius_; 94 } 95 96 protected: 97 ImageTheme() = default; 98 99 private: 100 Color fillColor_; 101 bool draggable_ = false; 102 float minEdgeAntialiasing_ = 0.0; 103 Dimension cornerRadius_ ; 104 bool clipEdge_ = false; 105 }; 106 107 } // namespace OHOS::Ace 108 109 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_IMAGE_THEME_H 110