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