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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_CLOCK_TEXT_CLOCK_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_CLOCK_TEXT_CLOCK_THEME_H 18 19 #include "core/components/theme/theme.h" 20 #include "core/components/theme/theme_constants.h" 21 22 namespace OHOS::Ace { 23 namespace { 24 constexpr float ERR_TEXT_COLOR_ALPHA = 0.9f; 25 } // namespace 26 27 class TextClockTheme : public virtual Theme { 28 DECLARE_ACE_TYPE(TextClockTheme, Theme); 29 30 public: 31 class Builder { 32 public: 33 Builder() = default; 34 virtual ~Builder() = default; 35 Build(const RefPtr<ThemeConstants> & themeConstants)36 RefPtr<TextClockTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 37 { 38 RefPtr<TextClockTheme> theme = AceType::MakeRefPtr<TextClockTheme>(); 39 if (!themeConstants) { 40 return theme; 41 } 42 InitThemeDefaultsClock(theme); 43 ParsePattern(themeConstants, theme); 44 return theme; 45 } 46 47 protected: InitThemeDefaultsClock(const RefPtr<TextClockTheme> & theme)48 void InitThemeDefaultsClock(const RefPtr<TextClockTheme>& theme) const 49 { 50 CHECK_NULL_VOID(theme); 51 // Styles below do not need to get from ThemeConstants, directly set at here. 52 theme->textStyleClock_.SetFontStyle(FontStyle::NORMAL); 53 theme->textStyleClock_.SetFontWeight(FontWeight::NORMAL); 54 theme->textStyleClock_.SetTextDecoration(TextDecoration::NONE); 55 } 56 ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<TextClockTheme> & theme)57 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<TextClockTheme>& theme) const 58 { 59 CHECK_NULL_VOID(themeConstants); 60 CHECK_NULL_VOID(theme); 61 RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_TEXT); 62 if (!pattern) { 63 LOGW("find pattern of textclock fail"); 64 return; 65 } 66 theme->textStyleClock_.SetTextColor( 67 pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::GRAY) 68 .BlendOpacity(pattern->GetAttr<double>(PATTERN_TEXT_COLOR_ALPHA, ERR_TEXT_COLOR_ALPHA))); 69 theme->textStyleClock_.SetFontSize(pattern->GetAttr<Dimension>("text_font_size", 0.0_vp)); 70 theme->SetTextParseFailedColor( 71 pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color::BLACK) 72 .BlendOpacity(pattern->GetAttr<double>(PATTERN_TEXT_COLOR_ALPHA, ERR_TEXT_COLOR_ALPHA))); 73 } 74 }; 75 76 ~TextClockTheme() override = default; 77 GetTextStyleClock()78 const TextStyle& GetTextStyleClock() const 79 { 80 return textStyleClock_; 81 } 82 GetTextParseFailedColor()83 const Color& GetTextParseFailedColor() const 84 { 85 return textClockParseFailedColor_; 86 } 87 SetTextParseFailedColor(const Color & textColor)88 void SetTextParseFailedColor(const Color& textColor) 89 { 90 textClockParseFailedColor_ = textColor; 91 } 92 93 protected: 94 TextClockTheme() = default; 95 TextStyle textStyleClock_; 96 97 private: 98 // For Parse Failed 99 Color textClockParseFailedColor_; 100 }; 101 102 } // namespace OHOS::Ace 103 104 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_CLOCK_TEXT_CLOCK_THEME_H 105