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::Claim(new 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 } 117 }; 118 119 ~RichEditorTheme() override = default; 120 GetDraggable()121 bool GetDraggable() const 122 { 123 return draggable_; 124 } 125 GetDefaultCaretHeight()126 const Dimension& GetDefaultCaretHeight() const 127 { 128 return defaultCaretHeight_; 129 } 130 GetDisabledAlpha()131 float GetDisabledAlpha() const 132 { 133 return disabledAlpha_; 134 } 135 GetScrollbarMinHeight()136 const Dimension& GetScrollbarMinHeight() 137 { 138 return scrollbarMinHeight_; 139 } 140 GetPadding()141 const Edge& GetPadding() const 142 { 143 return padding_; 144 } 145 GetInsertCursorOffset()146 const Dimension& GetInsertCursorOffset() const 147 { 148 return insertCursorOffset_; 149 } 150 GetPreviewUnderLineColor()151 const Color& GetPreviewUnderLineColor() const 152 { 153 return previewUnderlineColor_; 154 } 155 GetPopIconColor()156 const Color& GetPopIconColor() const 157 { 158 return popIconColor_; 159 } 160 GetMenuTitleColor()161 const Color& GetMenuTitleColor() const 162 { 163 return menuTitleColor_; 164 } 165 GetMenuTextColor()166 const Color& GetMenuTextColor() const 167 { 168 return menuTextColor_; 169 } 170 GetMenuIconColor()171 const Color& GetMenuIconColor() const 172 { 173 return menuIconColor_; 174 } 175 GetPreviewUnderlineWidth()176 const Dimension& GetPreviewUnderlineWidth() 177 { 178 return previewUnderlineWidth_; 179 } 180 GetPlaceholderColor()181 const Color& GetPlaceholderColor() const 182 { 183 return placeholderColor_; 184 } 185 GetCaretColor()186 const Color GetCaretColor() 187 { 188 return caretColor_; 189 } 190 GetSelectedBackgroundColor()191 const Color GetSelectedBackgroundColor() 192 { 193 return selectedBackgroundColor_; 194 } 195 IsRichEditorShowHandle()196 bool IsRichEditorShowHandle() const 197 { 198 return richeditorShowHandle_; 199 } 200 GetDragBackgroundColor()201 const Color& GetDragBackgroundColor() const 202 { 203 return dragBackgroundColor_; 204 } 205 GetTextStyle()206 TextStyle GetTextStyle() const 207 { 208 return textStyle_; 209 } 210 GetDragCornerRadius()211 Dimension GetDragCornerRadius() const 212 { 213 return dragCornerRadius_; 214 } GetAIWriteBundleName()215 const std::string& GetAIWriteBundleName() const 216 { 217 return aiWriteBundleName_; 218 } GetAIWriteAbilityName()219 const std::string& GetAIWriteAbilityName() const 220 { 221 return aiWriteAbilityName_; 222 } 223 GetAIWriteIsSupport()224 const std::string& GetAIWriteIsSupport() const 225 { 226 return aiWriteIsSupport_; 227 } 228 GetTranslateIsSupport()229 bool GetTranslateIsSupport() const 230 { 231 return translateIsSupport_; 232 } 233 GetSearchIsSupport()234 bool GetSearchIsSupport() const 235 { 236 return searchIsSupport_; 237 } 238 GetUrlDisabledColor()239 const Color& GetUrlDisabledColor() const 240 { 241 return urlDisabledColor_; 242 } 243 GetUrlDefaultColor()244 const Color& GetUrlDefaultColor() const 245 { 246 return urlDefaultColor_; 247 } 248 GetUrlHoverColor()249 const Color& GetUrlHoverColor() const 250 { 251 return urlHoverColor_; 252 } 253 GetUrlPressColor()254 const Color& GetUrlPressColor() const 255 { 256 return urlPressColor_; 257 } 258 GetCameraSymbolId()259 uint32_t GetCameraSymbolId() const 260 { 261 return cameraSymbolId_; 262 } 263 GetScanSymbolId()264 uint32_t GetScanSymbolId() const 265 { 266 return scanSymbolId_; 267 } 268 GetImageSymbolId()269 uint32_t GetImageSymbolId() const 270 { 271 return imageSymbolId_; 272 } 273 GetChevronRightSymbolId()274 uint32_t GetChevronRightSymbolId() const 275 { 276 return chevronRightSymbolId_; 277 } 278 protected: 279 RichEditorTheme() = default; 280 TextStyle textStyle_; 281 float urlDisabledOpacity_ = URL_DISA_OPACITY; 282 Color urlDisabledColor_ = Color(0x99000000); 283 Color urlDefaultColor_ = Color(0x99000000); 284 Color urlHoverColor_ = Color(0x99000000); 285 Color urlPressColor_ = Color(0x99000000); 286 287 private: 288 float disabledAlpha_ = 0.0f; 289 bool draggable_ = false; 290 Dimension defaultCaretHeight_ = 18.5_vp; 291 Dimension scrollbarMinHeight_ = 4.0_vp; 292 Edge padding_; 293 294 // UX::insert cursor offset up by 24vp 295 Dimension insertCursorOffset_ = 24.0_vp; 296 Color placeholderColor_ = Color(0x99000000); 297 Color caretColor_ = Color(0xff007dff); 298 Color selectedBackgroundColor_ = Color(0xff007dff); 299 Color dragBackgroundColor_ = Color::WHITE; 300 Dimension dragCornerRadius_ = 18.0_vp; 301 Color previewUnderlineColor_ = Color(0xff007dff); 302 Color popIconColor_ = Color(0x99000000); 303 Color menuTitleColor_ = Color(0x99000000); 304 Color menuTextColor_ = Color(0x99000000); 305 Color menuIconColor_ = Color(0x99000000); 306 Dimension previewUnderlineWidth_ = 2.0_vp; 307 bool richeditorShowHandle_ = false; 308 std::string aiWriteBundleName_; 309 std::string aiWriteAbilityName_; 310 std::string aiWriteIsSupport_; 311 bool translateIsSupport_ = false; 312 bool searchIsSupport_ = false; 313 uint32_t cameraSymbolId_; 314 uint32_t scanSymbolId_; 315 uint32_t imageSymbolId_; 316 uint32_t chevronRightSymbolId_; 317 }; 318 } // namespace OHOS::Ace::NG 319 320 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICH_EDITOR_RICH_EDITOR_THEME_H 321