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 theme->padding_ = Edge(themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_HORIZONTAL), 45 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_VERTICAL), 46 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_HORIZONTAL), 47 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_VERTICAL)); 48 ParsePattern(themeConstants, theme); 49 return theme; 50 } 51 52 private: ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RichEditorTheme> & theme)53 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RichEditorTheme>& theme) const 54 { 55 if (!theme) { 56 return; 57 } 58 RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_RICH_EDITOR); 59 if (!pattern) { 60 return; 61 } 62 auto draggable = pattern->GetAttr<std::string>("draggable", "0"); 63 theme->draggable_ = StringUtils::StringToInt(draggable); 64 theme->defaultCaretHeight_ = pattern->GetAttr<Dimension>("default_caret_height", 18.5_vp); 65 theme->disabledAlpha_ = static_cast<float>(pattern->GetAttr<double>("text_color_disabled_alpha", 0.0)); 66 } 67 }; 68 69 ~RichEditorTheme() override = default; 70 GetDraggable()71 bool GetDraggable() const 72 { 73 return draggable_; 74 } 75 GetDefaultCaretHeight()76 const Dimension& GetDefaultCaretHeight() const 77 { 78 return defaultCaretHeight_; 79 } 80 GetDisabledAlpha()81 float GetDisabledAlpha() const 82 { 83 return disabledAlpha_; 84 } 85 GetScrollbarMinHeight()86 const Dimension& GetScrollbarMinHeight() 87 { 88 return scrollbarMinHeight_; 89 } 90 GetPadding()91 const Edge& GetPadding() const 92 { 93 return padding_; 94 } 95 GetInsertCursorOffset()96 const Dimension& GetInsertCursorOffset() const 97 { 98 return insertCursorOffset_; 99 } 100 101 protected: 102 RichEditorTheme() = default; 103 104 private: 105 float disabledAlpha_ = 0.0f; 106 bool draggable_ = false; 107 Dimension defaultCaretHeight_ = 18.5_vp; 108 Dimension scrollbarMinHeight_ = 4.0_vp; 109 Edge padding_; 110 111 // UX::insert cursor offset up by 8vp 112 Dimension insertCursorOffset_ = 8.0_vp; 113 }; 114 } // namespace OHOS::Ace::NG 115 116 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICH_EDITOR_RICH_EDITOR_THEME_H 117