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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICH_EDITOR_RICH_EDITOR_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICH_EDITOR_RICH_EDITOR_THEME_H 18 19 #include "base/geometry/dimension.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 /** 26 * TextTheme defines color and styles of ThemeComponent. RichEditorTheme should be built 27 * using RichEditorTheme::Builder. 28 */ 29 class RichEditorTheme : public virtual Theme { 30 DECLARE_ACE_TYPE(RichEditorTheme, Theme); 31 32 public: 33 class Builder { 34 public: 35 Builder() = default; 36 ~Builder() = default; 37 Build(const RefPtr<ThemeConstants> & themeConstants)38 RefPtr<RichEditorTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 39 { 40 RefPtr<RichEditorTheme> theme = AceType::Claim(new RichEditorTheme()); 41 if (!themeConstants) { 42 return theme; 43 } 44 ParsePattern(themeConstants->GetThemeStyle(), theme); 45 return theme; 46 } 47 48 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<RichEditorTheme> & theme)49 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<RichEditorTheme>& theme) const 50 { 51 if (!themeStyle || !theme) { 52 return; 53 } 54 auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_RICH_EDITOR, nullptr); 55 if (!pattern) { 56 LOGW("find pattern of text fail"); 57 return; 58 } 59 auto draggable = pattern->GetAttr<std::string>("draggable", "0"); 60 theme->draggable_ = StringUtils::StringToInt(draggable); 61 theme->defaultCaretHeight_ = pattern->GetAttr<Dimension>("default_caret_height", 18.5_vp); 62 theme->disabledAlpha_ = static_cast<float>(pattern->GetAttr<double>("text_color_disabled_alpha", 0.0)); 63 } 64 }; 65 66 ~RichEditorTheme() override = default; 67 GetDraggable()68 bool GetDraggable() const 69 { 70 return draggable_; 71 } 72 GetDefaultCaretHeight()73 const Dimension& GetDefaultCaretHeight() const 74 { 75 return defaultCaretHeight_; 76 } 77 GetDisabledAlpha()78 float GetDisabledAlpha() const 79 { 80 return disabledAlpha_; 81 } 82 83 protected: 84 RichEditorTheme() = default; 85 86 private: 87 float disabledAlpha_ = 0.0f; 88 bool draggable_ = false; 89 Dimension defaultCaretHeight_ = 18.5_vp; 90 }; 91 } // namespace OHOS::Ace::NG 92 93 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICH_EDITOR_RICH_EDITOR_THEME_H 94