• 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/text/text_theme.h"
21 #include "core/components/theme/theme.h"
22 #include "core/components/theme/theme_constants.h"
23 #include "core/components/theme/theme_constants_defines.h"
24 
25 namespace OHOS::Ace::NG {
26 /**
27  * TextTheme defines color and styles of ThemeComponent. RichEditorTheme should be built
28  * using RichEditorTheme::Builder.
29  */
30 
31 namespace {
32 constexpr Color DEFAULT_TEXT_COLOR = Color(0xe5000000);
33 constexpr float DRAG_BACKGROUND_OPACITY = 0.95f;
34 constexpr float DEFAULT_TEXT_SIZE = 16.0f;
35 } // namespace
36 
37 class RichEditorTheme : public virtual Theme {
38     DECLARE_ACE_TYPE(RichEditorTheme, Theme);
39 
40 public:
41     class Builder {
42     public:
43         Builder() = default;
44         ~Builder() = default;
45 
Build(const RefPtr<ThemeConstants> & themeConstants)46         RefPtr<RichEditorTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
47         {
48             RefPtr<RichEditorTheme> theme = AceType::MakeRefPtr<RichEditorTheme>();
49             if (!themeConstants) {
50                 return theme;
51             }
52             InitThemeDefaults(themeConstants, theme);
53             ParsePattern(themeConstants, theme);
54             return theme;
55         }
56 
57     protected:
InitThemeDefaults(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RichEditorTheme> & theme)58         void InitThemeDefaults(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RichEditorTheme>& theme) const
59         {
60             CHECK_NULL_VOID(theme && themeConstants);
61             theme->padding_ = Edge(themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_HORIZONTAL),
62                 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_VERTICAL),
63                 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_HORIZONTAL),
64                 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_VERTICAL));
65         }
66 
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<RichEditorTheme> & theme)67         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<RichEditorTheme>& theme) const
68         {
69             if (!theme) {
70                 return;
71             }
72             RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_RICH_EDITOR);
73             if (!pattern) {
74                 return;
75             }
76             auto draggable = pattern->GetAttr<std::string>("draggable", "0");
77             theme->draggable_ = StringUtils::StringToInt(draggable);
78             auto dragBackgroundColor = pattern->GetAttr<Color>("drag_background_color", Color::WHITE);
79             if (Container::CurrentColorMode() == ColorMode::DARK) {
80                 dragBackgroundColor = dragBackgroundColor.ChangeOpacity(DRAG_BACKGROUND_OPACITY);
81             }
82             theme->dragBackgroundColor_ = dragBackgroundColor;
83             theme->dragCornerRadius_ = pattern->GetAttr<Dimension>("drag_corner_radius", 18.0_vp);
84             theme->defaultCaretHeight_ = pattern->GetAttr<Dimension>("default_caret_height", 18.5_vp);
85             theme->disabledAlpha_ = static_cast<float>(pattern->GetAttr<double>("text_color_disabled_alpha", 0.0));
86             theme->placeholderColor_ = pattern->GetAttr<Color>("tips_text_color", Color(0x99000000));
87             theme->caretColor_ = pattern->GetAttr<Color>("caret_color", Color(0xff007dff));
88             theme->selectedBackgroundColor_ = pattern->GetAttr<Color>("selected_background_color", Color(0xff007dff));
89             theme->previewUnderlineColor_ = pattern->GetAttr<Color>("preview_underline_color", Color(0xff007dff));
90             theme->popIconColor_ = pattern->GetAttr<Color>("pop_icon_color", Color(0x99000000));
91             theme->menuTitleColor_ = pattern->GetAttr<Color>("menu_title_color", Color(0x99000000));
92             theme->menuTextColor_ = pattern->GetAttr<Color>("menu_text_color", Color(0x99000000));
93             theme->menuIconColor_ = pattern->GetAttr<Color>("menu_icon_color", Color(0x99000000));
94             theme->previewUnderlineWidth_ = pattern->GetAttr<Dimension>("preview_underline_width", 2.0_vp);
95             auto showHandle = pattern->GetAttr<std::string>("rich_editor_show_handle", "0");
96             theme->richeditorShowHandle_ = StringUtils::StringToInt(showHandle);
97             theme->textStyle_.SetTextColor(pattern->GetAttr<Color>("default_text_color", DEFAULT_TEXT_COLOR));
98             theme->textStyle_.SetTextDecorationColor(pattern->GetAttr<Color>("default_text_color", DEFAULT_TEXT_COLOR));
99             theme->textStyle_.SetFontSize(Dimension(DEFAULT_TEXT_SIZE, DimensionUnit::FP));
100             theme->aiWriteBundleName_ = pattern->GetAttr<std::string>("rich_editor_writting_bundle_name", "");
101             theme->aiWriteAbilityName_ = pattern->GetAttr<std::string>("rich_editor_writting_ability_name", "");
102             theme->aiWriteIsSupport_ = pattern->GetAttr<std::string>("rich_editor_writting_is_support", "");
103             auto translateIsSupport = pattern->GetAttr<std::string>("menu_translate_is_support", "0");
104             theme->translateIsSupport_ = StringUtils::StringToInt(translateIsSupport);
105             auto searchIsSupport = pattern->GetAttr<std::string>("richeditor_menu_search_is_support", "0");
106             theme->searchIsSupport_ = StringUtils::StringToInt(searchIsSupport);
107             theme->urlDisabledOpacity_ = pattern->GetAttr<double>("interactive_disable", URL_DISA_OPACITY);
108             theme->urlDefaultColor_ = pattern->GetAttr<Color>("font_emphasize", Color(0xff007dff));
109             theme->urlDisabledColor_ = theme->urlDefaultColor_.BlendOpacity(theme->urlDisabledOpacity_);
110             theme->urlHoverColor_ = pattern->GetAttr<Color>("interactive_hover", Color(0x0C182431));
111             theme->urlPressColor_ = pattern->GetAttr<Color>("interactive_pressed", Color(0x19182431));
112             theme->cameraSymbolId_ = themeConstants->GetSymbolByName("sys.symbol.camera");
113             theme->scanSymbolId_ = themeConstants->GetSymbolByName("sys.symbol.line_viewfinder");
114             theme->imageSymbolId_ = themeConstants->GetSymbolByName("sys.symbol.picture");
115             theme->chevronRightSymbolId_ = themeConstants->GetSymbolByName("sys.symbol.chevron_right");
116             theme->borderRadius_ = Radius(pattern->GetAttr<Dimension>("rich_editor_border_radius", 0.0_vp));
117         }
118     };
119 
120     ~RichEditorTheme() override = default;
121 
GetDraggable()122     bool GetDraggable() const
123     {
124         return draggable_;
125     }
126 
GetDefaultCaretHeight()127     const Dimension& GetDefaultCaretHeight() const
128     {
129         return defaultCaretHeight_;
130     }
131 
GetDisabledAlpha()132     float GetDisabledAlpha() const
133     {
134         return disabledAlpha_;
135     }
136 
GetScrollbarMinHeight()137     const Dimension& GetScrollbarMinHeight()
138     {
139         return scrollbarMinHeight_;
140     }
141 
GetPadding()142     const Edge& GetPadding() const
143     {
144         return padding_;
145     }
146 
GetInsertCursorOffset()147     const Dimension& GetInsertCursorOffset() const
148     {
149         return insertCursorOffset_;
150     }
151 
GetPreviewUnderLineColor()152     const Color& GetPreviewUnderLineColor() const
153     {
154         return previewUnderlineColor_;
155     }
156 
GetPopIconColor()157     const Color& GetPopIconColor() const
158     {
159         return popIconColor_;
160     }
161 
GetMenuTitleColor()162     const Color& GetMenuTitleColor() const
163     {
164         return menuTitleColor_;
165     }
166 
GetMenuTextColor()167     const Color& GetMenuTextColor() const
168     {
169         return menuTextColor_;
170     }
171 
GetMenuIconColor()172     const Color& GetMenuIconColor() const
173     {
174         return menuIconColor_;
175     }
176 
GetPreviewUnderlineWidth()177     const Dimension& GetPreviewUnderlineWidth()
178     {
179         return previewUnderlineWidth_;
180     }
181 
GetPlaceholderColor()182     const Color& GetPlaceholderColor() const
183     {
184         return placeholderColor_;
185     }
186 
GetCaretColor()187     const Color GetCaretColor()
188     {
189         return caretColor_;
190     }
191 
GetSelectedBackgroundColor()192     const Color GetSelectedBackgroundColor()
193     {
194         return selectedBackgroundColor_;
195     }
196 
IsRichEditorShowHandle()197     bool IsRichEditorShowHandle() const
198     {
199         return richeditorShowHandle_;
200     }
201 
GetDragBackgroundColor()202     const Color& GetDragBackgroundColor() const
203     {
204         return dragBackgroundColor_;
205     }
206 
GetTextStyle()207     TextStyle GetTextStyle() const
208     {
209         return textStyle_;
210     }
211 
GetDragCornerRadius()212     Dimension GetDragCornerRadius() const
213     {
214         return dragCornerRadius_;
215     }
GetAIWriteBundleName()216     const std::string& GetAIWriteBundleName() const
217     {
218         return aiWriteBundleName_;
219     }
GetAIWriteAbilityName()220     const std::string& GetAIWriteAbilityName() const
221     {
222         return aiWriteAbilityName_;
223     }
224 
GetAIWriteIsSupport()225     const std::string& GetAIWriteIsSupport() const
226     {
227         return aiWriteIsSupport_;
228     }
229 
GetTranslateIsSupport()230     bool GetTranslateIsSupport() const
231     {
232         return translateIsSupport_;
233     }
234 
GetSearchIsSupport()235     bool GetSearchIsSupport() const
236     {
237         return searchIsSupport_;
238     }
239 
GetUrlDisabledColor()240     const Color& GetUrlDisabledColor() const
241     {
242         return urlDisabledColor_;
243     }
244 
GetUrlDefaultColor()245     const Color& GetUrlDefaultColor() const
246     {
247         return urlDefaultColor_;
248     }
249 
GetUrlHoverColor()250     const Color& GetUrlHoverColor() const
251     {
252         return urlHoverColor_;
253     }
254 
GetUrlPressColor()255     const Color& GetUrlPressColor() const
256     {
257         return urlPressColor_;
258     }
259 
GetCameraSymbolId()260     uint32_t GetCameraSymbolId() const
261     {
262         return cameraSymbolId_;
263     }
264 
GetScanSymbolId()265     uint32_t GetScanSymbolId() const
266     {
267         return scanSymbolId_;
268     }
269 
GetImageSymbolId()270     uint32_t GetImageSymbolId() const
271     {
272         return imageSymbolId_;
273     }
274 
GetChevronRightSymbolId()275     uint32_t GetChevronRightSymbolId() const
276     {
277         return chevronRightSymbolId_;
278     }
279 
GetBorderRadius()280     const Radius& GetBorderRadius() const
281     {
282         return borderRadius_;
283     }
284 protected:
285     RichEditorTheme() = default;
286     TextStyle textStyle_;
287     float urlDisabledOpacity_ = URL_DISA_OPACITY;
288     Color urlDisabledColor_ = Color(0x99000000);
289     Color urlDefaultColor_ = Color(0x99000000);
290     Color urlHoverColor_ = Color(0x99000000);
291     Color urlPressColor_ = Color(0x99000000);
292 
293 private:
294     float disabledAlpha_ = 0.0f;
295     bool draggable_ = false;
296     Dimension defaultCaretHeight_ = 18.5_vp;
297     Dimension scrollbarMinHeight_ = 4.0_vp;
298     Edge padding_;
299 
300     // UX::insert cursor offset up by 24vp
301     Dimension insertCursorOffset_ = 24.0_vp;
302     Color placeholderColor_ = Color(0x99000000);
303     Color caretColor_ = Color(0xff007dff);
304     Color selectedBackgroundColor_ = Color(0xff007dff);
305     Color dragBackgroundColor_ = Color::WHITE;
306     Dimension dragCornerRadius_ = 18.0_vp;
307     Color previewUnderlineColor_ = Color(0xff007dff);
308     Color popIconColor_ = Color(0x99000000);
309     Color menuTitleColor_ = Color(0x99000000);
310     Color menuTextColor_ = Color(0x99000000);
311     Color menuIconColor_ = Color(0x99000000);
312     Dimension previewUnderlineWidth_ = 2.0_vp;
313     bool richeditorShowHandle_ = false;
314     std::string aiWriteBundleName_;
315     std::string aiWriteAbilityName_;
316     std::string aiWriteIsSupport_;
317     bool translateIsSupport_ = false;
318     bool searchIsSupport_ = false;
319     uint32_t cameraSymbolId_;
320     uint32_t scanSymbolId_;
321     uint32_t imageSymbolId_;
322     uint32_t chevronRightSymbolId_;
323     Radius borderRadius_;
324 };
325 } // namespace OHOS::Ace::NG
326 
327 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICH_EDITOR_RICH_EDITOR_THEME_H
328