• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30 namespace {
31 constexpr Color DEFAULT_TEXT_COLOR = Color(0xe5000000);
32 constexpr float DRAG_BACKGROUND_OPACITY = 0.95f;
33 constexpr float DEFAULT_TEXT_SIZE = 16.0f;
34 } // namespace
35 
36 class RichEditorTheme : public virtual Theme {
37     DECLARE_ACE_TYPE(RichEditorTheme, Theme);
38 
39 public:
40     class Builder {
41     public:
42         Builder() = default;
43         ~Builder() = default;
44 
Build(const RefPtr<ThemeConstants> & themeConstants)45         RefPtr<RichEditorTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
46         {
47             RefPtr<RichEditorTheme> theme = AceType::Claim(new RichEditorTheme());
48             if (!themeConstants) {
49                 return theme;
50             }
51             theme->padding_ = Edge(themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_HORIZONTAL),
52                 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_VERTICAL),
53                 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_HORIZONTAL),
54                 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_VERTICAL));
55             ParsePattern(themeConstants, theme);
56             return theme;
57         }
58 
59     private:
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RichEditorTheme> & theme)60         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RichEditorTheme>& theme) const
61         {
62             if (!theme) {
63                 return;
64             }
65             RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_RICH_EDITOR);
66             if (!pattern) {
67                 return;
68             }
69             auto draggable = pattern->GetAttr<std::string>("draggable", "0");
70             theme->draggable_ = StringUtils::StringToInt(draggable);
71             auto dragBackgroundColor = pattern->GetAttr<Color>("drag_background_color", Color::WHITE);
72             if (SystemProperties::GetColorMode() == ColorMode::DARK) {
73                 dragBackgroundColor = dragBackgroundColor.ChangeOpacity(DRAG_BACKGROUND_OPACITY);
74             }
75             theme->dragBackgroundColor_ = dragBackgroundColor;
76             theme->dragCornerRadius_ = pattern->GetAttr<Dimension>("drag_corner_radius", 18.0_vp);
77             theme->defaultCaretHeight_ = pattern->GetAttr<Dimension>("default_caret_height", 18.5_vp);
78             theme->disabledAlpha_ = static_cast<float>(pattern->GetAttr<double>("text_color_disabled_alpha", 0.0));
79             theme->placeholderColor_ = pattern->GetAttr<Color>("tips_text_color", Color(0x99000000));
80             theme->caretColor_ = pattern->GetAttr<Color>("caret_color", Color(0xff007dff));
81             theme->selectedBackgroundColor_ = pattern->GetAttr<Color>("selected_background_color", Color(0xff007dff));
82             theme->previewUnderlineColor_ = pattern->GetAttr<Color>("preview_underline_color", Color(0xff007dff));
83             theme->popIconColor_ = pattern->GetAttr<Color>("pop_icon_color", Color(0x99000000));
84             theme->menuTitleColor_ = pattern->GetAttr<Color>("menu_title_color", Color(0x99000000));
85             theme->menuTextColor_ = pattern->GetAttr<Color>("menu_text_color", Color(0x99000000));
86             theme->menuIconColor_ = pattern->GetAttr<Color>("menu_icon_color", Color(0x99000000));
87             theme->previewUnderlineWidth_ = pattern->GetAttr<Dimension>("preview_underline_width", 2.0_vp);
88             auto showHandle = pattern->GetAttr<std::string>("rich_editor_show_handle", "0");
89             theme->richeditorShowHandle_ = StringUtils::StringToInt(showHandle);
90             theme->textStyle_.SetTextColor(pattern->GetAttr<Color>("default_text_color", DEFAULT_TEXT_COLOR));
91             theme->textStyle_.SetTextDecorationColor(pattern->GetAttr<Color>("default_text_color", DEFAULT_TEXT_COLOR));
92             theme->textStyle_.SetFontSize(Dimension(DEFAULT_TEXT_SIZE, DimensionUnit::FP));
93             theme->aiWriteBundleName_ = pattern->GetAttr<std::string>("rich_editor_writting_bundle_name", "");
94             theme->aiWriteAbilityName_ = pattern->GetAttr<std::string>("rich_editor_writting_ability_name", "");
95             theme->aiWriteIsSupport_ = pattern->GetAttr<std::string>("rich_editor_writting_is_support", "");
96         }
97     };
98 
99     ~RichEditorTheme() override = default;
100 
GetDraggable()101     bool GetDraggable() const
102     {
103         return draggable_;
104     }
105 
GetDefaultCaretHeight()106     const Dimension& GetDefaultCaretHeight() const
107     {
108         return defaultCaretHeight_;
109     }
110 
GetDisabledAlpha()111     float GetDisabledAlpha() const
112     {
113         return disabledAlpha_;
114     }
115 
GetScrollbarMinHeight()116     const Dimension& GetScrollbarMinHeight()
117     {
118         return scrollbarMinHeight_;
119     }
120 
GetPadding()121     const Edge& GetPadding() const
122     {
123         return padding_;
124     }
125 
GetInsertCursorOffset()126     const Dimension& GetInsertCursorOffset() const
127     {
128         return insertCursorOffset_;
129     }
130 
GetPreviewUnderLineColor()131     const Color& GetPreviewUnderLineColor() const
132     {
133         return previewUnderlineColor_;
134     }
135 
GetPopIconColor()136     const Color& GetPopIconColor() const
137     {
138         return popIconColor_;
139     }
140 
GetMenuTitleColor()141     const Color& GetMenuTitleColor() const
142     {
143         return menuTitleColor_;
144     }
145 
GetMenuTextColor()146     const Color& GetMenuTextColor() const
147     {
148         return menuTextColor_;
149     }
150 
GetMenuIconColor()151     const Color& GetMenuIconColor() const
152     {
153         return menuIconColor_;
154     }
155 
GetPreviewUnderlineWidth()156     const Dimension& GetPreviewUnderlineWidth()
157     {
158         return previewUnderlineWidth_;
159     }
160 
GetPlaceholderColor()161     const Color& GetPlaceholderColor() const
162     {
163         return placeholderColor_;
164     }
165 
GetCaretColor()166     const Color GetCaretColor()
167     {
168         return caretColor_;
169     }
170 
GetSelectedBackgroundColor()171     const Color GetSelectedBackgroundColor()
172     {
173         return selectedBackgroundColor_;
174     }
175 
IsRichEditorShowHandle()176     bool IsRichEditorShowHandle() const
177     {
178         return richeditorShowHandle_;
179     }
180 
GetDragBackgroundColor()181     const Color& GetDragBackgroundColor() const
182     {
183         return dragBackgroundColor_;
184     }
185 
GetTextStyle()186     TextStyle GetTextStyle() const
187     {
188         return textStyle_;
189     }
190 
GetDragCornerRadius()191     Dimension GetDragCornerRadius() const
192     {
193         return dragCornerRadius_;
194     }
GetAIWriteBundleName()195     const std::string& GetAIWriteBundleName() const
196     {
197         return aiWriteBundleName_;
198     }
GetAIWriteAbilityName()199     const std::string& GetAIWriteAbilityName() const
200     {
201         return aiWriteAbilityName_;
202     }
203 
GetAIWriteIsSupport()204     const std::string& GetAIWriteIsSupport() const
205     {
206         return aiWriteIsSupport_;
207     }
208 protected:
209     RichEditorTheme() = default;
210 
211 private:
212     float disabledAlpha_ = 0.0f;
213     bool draggable_ = false;
214     Dimension defaultCaretHeight_ = 18.5_vp;
215     Dimension scrollbarMinHeight_ = 4.0_vp;
216     Edge padding_;
217 
218     // UX::insert cursor offset up by 24vp
219     Dimension insertCursorOffset_ = 24.0_vp;
220     TextStyle textStyle_;
221     Color placeholderColor_ = Color(0x99000000);
222     Color caretColor_ = Color(0xff007dff);
223     Color selectedBackgroundColor_ = Color(0xff007dff);
224     Color dragBackgroundColor_ = Color::WHITE;
225     Dimension dragCornerRadius_ = 18.0_vp;
226     Color previewUnderlineColor_ = Color(0xff007dff);
227     Color popIconColor_ = Color(0x99000000);
228     Color menuTitleColor_ = Color(0x99000000);
229     Color menuTextColor_ = Color(0x99000000);
230     Color menuIconColor_ = Color(0x99000000);
231     Dimension previewUnderlineWidth_ = 2.0_vp;
232     bool richeditorShowHandle_ = false;
233     std::string aiWriteBundleName_;
234     std::string aiWriteAbilityName_;
235     std::string aiWriteIsSupport_;
236 };
237 } // namespace OHOS::Ace::NG
238 
239 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICH_EDITOR_RICH_EDITOR_THEME_H
240