1 /* 2 * Copyright (c) 2022-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_GAUGE_GAUGE_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GAUGE_GAUGE_THEME_H 18 19 #include "base/i18n/localization.h" 20 #include "core/components/theme/theme.h" 21 #include "core/components/theme/theme_constants.h" 22 #include "core/components/theme/theme_constants_defines.h" 23 24 namespace OHOS::Ace::NG { 25 namespace { 26 inline constexpr float DEFAULT_START_DEGREE = 0.0f; 27 inline constexpr float DEFAULT_END_DEGREE = 360.0f; 28 inline constexpr float EDGE = 15.0f; 29 inline constexpr float HEIGHT_OFFSET = 5.0f; 30 inline constexpr float INDICATOR_STROKE_WIDTH = 5.0f; 31 inline constexpr int8_t UNSELECT_ALPHA = 125; 32 inline constexpr float DEFAULT_MIN_VALUE = 0.0f; 33 inline constexpr float DEFAULT_MAX_VALUE = 100.0f; 34 inline constexpr float MONOCHROME_CIRCULAR_BACKGROUND_COLOR_OPACITY = 0.2f; 35 inline constexpr float INDICATOR_WIDTH_RATIO = 0.135f; 36 inline constexpr float INDICATOR_HEIGHT_RATIO = 0.087f; 37 inline constexpr float INDICATOR_CORNER_RADIUS_RATIO = 0.01f; 38 inline constexpr float INDICATOR_BORDER_WIDTH_RATIO_DOUBLE = 0.06f; 39 inline constexpr float INDICATOR_BORDER_WIDTH_RATIO = 0.03f; 40 inline constexpr float DESCRIPTION_NODE_WIDTH_RATIO = 0.4444f; 41 inline constexpr float DESCRIPTION_NODE_HEIGHT_RATIO = 0.254f; 42 inline constexpr float DESCRIPTION_IMAGE_NODE_WIDTH_RATIO = 0.286f; 43 inline constexpr float DESCRIPTION_IMAGE_NODE_HEIGHT_RATIO = 0.286f; 44 inline constexpr float DESCRIPTION_X = 0.2778f; 45 inline constexpr float DESCRIPTION_Y = 0.746f; 46 inline constexpr float DESCRIPTION_IMAGE_X = 0.357f; 47 inline constexpr float DESCRIPTION_IMAGE_Y = 0.714f; 48 inline constexpr float LIMIT_VALUE_MIN_OR_MAX_HEIGHT_RATIO = 0.2222f; 49 inline constexpr float LIMIT_VALUE_Y = 0.722f; 50 inline constexpr float DEFAULT_GAUGE_SHADOW_RADIUS = 20.0f; 51 inline constexpr float DEFAULT_GAUGE_SHADOW_OFFSETX = 5.0f; 52 inline constexpr float DEFAULT_GAUGE_SHADOW_OFFSETY = 5.0f; 53 inline constexpr float SHADOW_ALPHA = 0.4f; 54 inline constexpr Dimension INDICATOR_DISTANCE_TO_TOP = 8.0_vp; 55 inline constexpr Dimension LIMIT_VALUE_MAX_FONTSIZE = 40.0_vp; 56 inline constexpr int32_t COLORS_MAX_COUNT = 9; 57 inline constexpr float RADIUS_TO_DIAMETER = 2.0f; 58 inline constexpr float INDICATOR_WIDTH_RADIO = 0.135f; 59 inline constexpr float INDICATOR_HEIGHT_RADIO = 0.095f; 60 inline constexpr float LIMIT_VALUE_MIN_SAFE_DISTANCE_RATIO = 0.031; 61 inline constexpr float LIMIT_VALUE_MAX_SAFE_DISTANCE_RATIO = 0.031; 62 inline constexpr float LIMIT_VALUE_SPACE_SAFE_DISTANCE_RATIO = 0.0635; 63 inline const std::vector<Color> GAUGE_DEFAULT_COLOR { Color(0xFF64BB5C), Color(0xFFF7CE00), Color(0xFFE84026) }; 64 } // namespace 65 66 /** 67 * GaugeTheme defines color and styles of GaugePattern. GaugeTheme should be built 68 * using GaugeTheme::Builder. 69 */ 70 class GaugeTheme : public virtual Theme { 71 DECLARE_ACE_TYPE(GaugeTheme, Theme); 72 73 public: 74 class Builder { 75 public: 76 Builder() = default; 77 ~Builder() = default; 78 Build(const RefPtr<ThemeConstants> & themeConstants)79 RefPtr<GaugeTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 80 { 81 RefPtr<GaugeTheme> theme = AceType::Claim(new GaugeTheme()); 82 if (!themeConstants) { 83 return theme; 84 } 85 theme->trackThickness_ = themeConstants->GetDimension(THEME_PROGRERSS_THICKNESS); 86 ParsePattern(themeConstants, theme); 87 return theme; 88 } 89 90 private: ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<GaugeTheme> & theme)91 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<GaugeTheme>& theme) const 92 { 93 RefPtr<ThemeStyle> gaugePattern = themeConstants->GetPatternByName(THEME_PATTERN_GAUGE); 94 if (!gaugePattern) { 95 return; 96 } 97 98 theme->indicatorColor_ = gaugePattern->GetAttr<Color>("indicator_color", Color::BLACK); 99 theme->indicatorBorderColor_ = gaugePattern->GetAttr<Color>("indicator_border_color", Color::WHITE); 100 theme->limitValueMinFontSize_ = gaugePattern->GetAttr<Dimension>("limit_value_min_font_size", 10.0_vp); 101 } 102 }; 103 104 ~GaugeTheme() override = default; 105 GetIndicatorColor()106 const Color& GetIndicatorColor() const 107 { 108 return indicatorColor_; 109 } 110 GetIndicatorBorderColor()111 const Color& GetIndicatorBorderColor() const 112 { 113 return indicatorBorderColor_; 114 } 115 GetLimitValueMinFontSize()116 const Dimension& GetLimitValueMinFontSize() const 117 { 118 return limitValueMinFontSize_; 119 } 120 GetTrackThickness()121 const Dimension& GetTrackThickness() const 122 { 123 return trackThickness_; 124 } 125 126 protected: 127 GaugeTheme() = default; 128 129 private: 130 Color indicatorColor_; 131 Color indicatorBorderColor_; 132 Dimension limitValueMinFontSize_; 133 Dimension trackThickness_; 134 }; 135 } // namespace OHOS::Ace::NG 136 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GAUGE_GAUGE_THEME_H