• 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             auto translateIsSupport = pattern->GetAttr<std::string>("menu_translate_is_support", "0");
97             theme->translateIsSupport_ = StringUtils::StringToInt(translateIsSupport);
98             auto disabledOpacity = pattern->GetAttr<double>("interactive_disable", URL_DISA_OPACITY);
99             theme->urlDefaultColor_ = pattern->GetAttr<Color>("font_emphasize", Color(0xff007dff));
100             theme->urlDisabledColor_ = theme->urlDefaultColor_.BlendOpacity(disabledOpacity);
101             theme->urlHoverColor_ = pattern->GetAttr<Color>("interactive_hover", Color(0x0C182431));
102             theme->urlPressColor_ = pattern->GetAttr<Color>("interactive_pressed", Color(0x19182431));
103         }
104     };
105 
106     ~RichEditorTheme() override = default;
107 
GetDraggable()108     bool GetDraggable() const
109     {
110         return draggable_;
111     }
112 
GetDefaultCaretHeight()113     const Dimension& GetDefaultCaretHeight() const
114     {
115         return defaultCaretHeight_;
116     }
117 
GetDisabledAlpha()118     float GetDisabledAlpha() const
119     {
120         return disabledAlpha_;
121     }
122 
GetScrollbarMinHeight()123     const Dimension& GetScrollbarMinHeight()
124     {
125         return scrollbarMinHeight_;
126     }
127 
GetPadding()128     const Edge& GetPadding() const
129     {
130         return padding_;
131     }
132 
GetInsertCursorOffset()133     const Dimension& GetInsertCursorOffset() const
134     {
135         return insertCursorOffset_;
136     }
137 
GetPreviewUnderLineColor()138     const Color& GetPreviewUnderLineColor() const
139     {
140         return previewUnderlineColor_;
141     }
142 
GetPopIconColor()143     const Color& GetPopIconColor() const
144     {
145         return popIconColor_;
146     }
147 
GetMenuTitleColor()148     const Color& GetMenuTitleColor() const
149     {
150         return menuTitleColor_;
151     }
152 
GetMenuTextColor()153     const Color& GetMenuTextColor() const
154     {
155         return menuTextColor_;
156     }
157 
GetMenuIconColor()158     const Color& GetMenuIconColor() const
159     {
160         return menuIconColor_;
161     }
162 
GetPreviewUnderlineWidth()163     const Dimension& GetPreviewUnderlineWidth()
164     {
165         return previewUnderlineWidth_;
166     }
167 
GetPlaceholderColor()168     const Color& GetPlaceholderColor() const
169     {
170         return placeholderColor_;
171     }
172 
GetCaretColor()173     const Color GetCaretColor()
174     {
175         return caretColor_;
176     }
177 
GetSelectedBackgroundColor()178     const Color GetSelectedBackgroundColor()
179     {
180         return selectedBackgroundColor_;
181     }
182 
IsRichEditorShowHandle()183     bool IsRichEditorShowHandle() const
184     {
185         return richeditorShowHandle_;
186     }
187 
GetDragBackgroundColor()188     const Color& GetDragBackgroundColor() const
189     {
190         return dragBackgroundColor_;
191     }
192 
GetTextStyle()193     TextStyle GetTextStyle() const
194     {
195         return textStyle_;
196     }
197 
GetDragCornerRadius()198     Dimension GetDragCornerRadius() const
199     {
200         return dragCornerRadius_;
201     }
GetAIWriteBundleName()202     const std::string& GetAIWriteBundleName() const
203     {
204         return aiWriteBundleName_;
205     }
GetAIWriteAbilityName()206     const std::string& GetAIWriteAbilityName() const
207     {
208         return aiWriteAbilityName_;
209     }
210 
GetAIWriteIsSupport()211     const std::string& GetAIWriteIsSupport() const
212     {
213         return aiWriteIsSupport_;
214     }
215 
GetTranslateIsSupport()216     bool GetTranslateIsSupport() const
217     {
218         return translateIsSupport_;
219     }
220 
GetUrlDisabledColor()221     const Color& GetUrlDisabledColor() const
222     {
223         return urlDisabledColor_;
224     }
225 
GetUrlDefaultColor()226     const Color& GetUrlDefaultColor() const
227     {
228         return urlDefaultColor_;
229     }
230 
GetUrlHoverColor()231     const Color& GetUrlHoverColor() const
232     {
233         return urlHoverColor_;
234     }
235 
GetUrlPressColor()236     const Color& GetUrlPressColor() const
237     {
238         return urlPressColor_;
239     }
240 protected:
241     RichEditorTheme() = default;
242 
243 private:
244     float disabledAlpha_ = 0.0f;
245     bool draggable_ = false;
246     Dimension defaultCaretHeight_ = 18.5_vp;
247     Dimension scrollbarMinHeight_ = 4.0_vp;
248     Edge padding_;
249 
250     // UX::insert cursor offset up by 24vp
251     Dimension insertCursorOffset_ = 24.0_vp;
252     TextStyle textStyle_;
253     Color placeholderColor_ = Color(0x99000000);
254     Color caretColor_ = Color(0xff007dff);
255     Color selectedBackgroundColor_ = Color(0xff007dff);
256     Color dragBackgroundColor_ = Color::WHITE;
257     Dimension dragCornerRadius_ = 18.0_vp;
258     Color previewUnderlineColor_ = Color(0xff007dff);
259     Color popIconColor_ = Color(0x99000000);
260     Color menuTitleColor_ = Color(0x99000000);
261     Color menuTextColor_ = Color(0x99000000);
262     Color menuIconColor_ = Color(0x99000000);
263     Dimension previewUnderlineWidth_ = 2.0_vp;
264     bool richeditorShowHandle_ = false;
265     std::string aiWriteBundleName_;
266     std::string aiWriteAbilityName_;
267     std::string aiWriteIsSupport_;
268     bool translateIsSupport_ = false;
269     Color urlDisabledColor_ = Color(0x99000000);
270     Color urlDefaultColor_ = Color(0x99000000);
271     Color urlHoverColor_ = Color(0x99000000);
272     Color urlPressColor_ = Color(0x99000000);
273 };
274 } // namespace OHOS::Ace::NG
275 
276 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICH_EDITOR_RICH_EDITOR_THEME_H
277