1 /* 2 * Copyright (c) 2021 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_TEXT_FIELD_TEXTFIELD_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_TEXTFIELD_THEME_H 18 19 #include "base/geometry/dimension.h" 20 #include "core/components/common/properties/border.h" 21 #include "core/components/common/properties/color.h" 22 #include "core/components/common/properties/edge.h" 23 #include "core/components/common/properties/radius.h" 24 #include "core/components/theme/theme.h" 25 #include "core/components/theme/theme_constants.h" 26 #include "core/components/theme/theme_constants_defines.h" 27 28 namespace OHOS::Ace { 29 30 /** 31 * TextFieldTheme defines color and styles of SliderComponent. TextFieldTheme should be built 32 * using TextFieldTheme::Builder. 33 */ 34 class TextFieldTheme : public virtual Theme { 35 DECLARE_ACE_TYPE(TextFieldTheme, Theme); 36 37 public: 38 class Builder { 39 public: 40 Builder() = default; 41 ~Builder() = default; 42 Build(const RefPtr<ThemeConstants> & themeConstants)43 RefPtr<TextFieldTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 44 { 45 RefPtr<TextFieldTheme> theme = AceType::Claim(new TextFieldTheme()); 46 if (!themeConstants) { 47 return theme; 48 } 49 // init theme from global data 50 theme->padding_ = Edge(themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_HORIZONTAL), 51 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_VERTICAL), 52 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_HORIZONTAL), 53 themeConstants->GetDimension(THEME_TEXTFIELD_PADDING_VERTICAL)); 54 theme->height_ = themeConstants->GetDimension(THEME_TEXTFIELD_HEIGHT); 55 theme->fontSize_ = themeConstants->GetDimension(THEME_TEXTFIELD_FONT_SIZE); 56 theme->fontWeight_ = FontWeight(themeConstants->GetInt(THEME_TEXTFIELD_FONT_WEIGHT)); 57 theme->borderRadius_ = Radius(themeConstants->GetDimension(THEME_TEXTFIELD_BORDER_RADIUS)); 58 59 theme->bgColor_ = themeConstants->GetColor(THEME_TEXTFIELD_BACKGROUND_COLOR); 60 theme->placeholderColor_ = themeConstants->GetColor(THEME_TEXTFIELD_PLACEHOLDER_COLOR); 61 theme->textColor_ = themeConstants->GetColor(THEME_TEXTFIELD_TEXT_COLOR); 62 theme->selectedColor_ = themeConstants->GetColor(THEME_TEXTFIELD_TEXT_SELECTED_COLOR); 63 theme->hoverColor_ = themeConstants->GetColor(THEME_TEXTFIELD_HOVER_COLOR); 64 theme->pressColor_ = themeConstants->GetColor(THEME_TEXTFIELD_PRESS_COLOR); 65 66 theme->focusBgColor_ = themeConstants->GetColor(THEME_TEXTFIELD_FOCUS_BACKGROUND_COLOR); 67 theme->focusPlaceholderColor_ = themeConstants->GetColor(THEME_TEXTFIELD_FOCUS_PLACEHOLDER_COLOR); 68 theme->focusTextColor_ = themeConstants->GetColor(THEME_TEXTFIELD_FOCUS_TEXT_COLOR); 69 70 theme->disableOpacityRatio_ = themeConstants->GetDouble(THEME_TEXTFIELD_DISABLE_OPACITY_RATIO); 71 theme->disableTextColor_ = themeConstants->GetColor(THEME_TEXTFIELD_TEXT_COLOR_DISABLE); 72 73 theme->overHideLength_ = themeConstants->GetDimension(THEME_TEXTFIELD_OVER_HIDE_LENGTH); 74 75 theme->cursorColor_ = themeConstants->GetColor(THEME_TEXTFIELD_CURSOR_COLOR); 76 theme->cursorRadius_ = themeConstants->GetDimension(THEME_TEXTFIELD_CURSOR_RADIUS); 77 theme->needFade_ = static_cast<bool>(themeConstants->GetInt(THEME_TEXTFIELD_NEED_FADE)); 78 79 theme->iconSize_ = themeConstants->GetDimension(THEME_TEXTFIELD_ICON_SIZE); 80 theme->iconHotZoneSize_ = themeConstants->GetDimension(THEME_TEXTFIELD_ICON_HOT_ZONE_SIZE); 81 82 theme->showEllipsis_ = static_cast<bool>(themeConstants->GetInt(THEME_TEXTFIELD_SHOW_ELLIPSIS)); 83 84 theme->errorSpacing_ = themeConstants->GetDimension(THEME_TEXTFIELD_ERROR_SPACING); 85 theme->errorIsInner_ = static_cast<bool>(themeConstants->GetInt(THEME_TEXTFIELD_ERROR_IS_INNER)); 86 theme->errorBorderWidth_ = themeConstants->GetDimension(THEME_TEXTFIELD_ERROR_BORDER_WIDTH); 87 theme->errorBorderColor_ = themeConstants->GetColor(THEME_TEXTFIELD_ERROR_BORDER_COLOR); 88 theme->errorTextStyle_.SetTextColor(themeConstants->GetColor(THEME_TEXTFIELD_ERROR_COLOR)); 89 theme->errorTextStyle_.SetFontSize(themeConstants->GetDimension(THEME_TEXTFIELD_ERROR_FONT_SIZE)); 90 theme->errorTextStyle_.SetFontWeight(FontWeight(themeConstants->GetInt(THEME_TEXTFIELD_ERROR_FONT_WEIGHT))); 91 92 theme->countTextStyle_.SetTextColor(themeConstants->GetColor(THEME_TEXTFIELD_COUNT_TEXT_COLOR)); 93 theme->countTextStyle_.SetFontSize(themeConstants->GetDimension(THEME_TEXTFIELD_COUNT_FONT_SIZE)); 94 theme->countTextStyle_.SetFontWeight( 95 FontWeight(themeConstants->GetInt(THEME_TEXTFIELD_COUNT_FONT_WEIGHT))); 96 theme->overCountStyle_.SetTextColor(themeConstants->GetColor(THEME_TEXTFIELD_OVER_COUNT_TEXT_COLOR)); 97 theme->overCountStyle_.SetFontSize(themeConstants->GetDimension(THEME_TEXTFIELD_OVER_COUNT_FONT_SIZE)); 98 theme->overCountStyle_.SetFontWeight( 99 FontWeight(themeConstants->GetInt(THEME_TEXTFIELD_OVER_COUNT_FONT_WEIGHT))); 100 101 theme->countTextStyleOuter_.SetTextColor(themeConstants->GetColor(THEME_TEXTFIELD_COUNT_TEXT_COLOR)); 102 theme->countTextStyleOuter_.SetFontSize( 103 themeConstants->GetDimension(THEME_TEXTFIELD_COUNT_FONT_SIZE_OUTER)); 104 theme->countTextStyleOuter_.SetFontWeight( 105 FontWeight(themeConstants->GetInt(THEME_TEXTFIELD_COUNT_FONT_WEIGHT))); 106 theme->overCountStyleOuter_.SetTextColor(themeConstants->GetColor(THEME_TEXTFIELD_OVER_COUNT_TEXT_COLOR)); 107 theme->overCountStyleOuter_.SetFontSize( 108 themeConstants->GetDimension(THEME_TEXTFIELD_COUNT_FONT_SIZE_OUTER)); 109 theme->overCountStyleOuter_.SetFontWeight( 110 FontWeight(themeConstants->GetInt(THEME_TEXTFIELD_OVER_COUNT_FONT_WEIGHT))); 111 112 ParsePattern(themeConstants->GetThemeStyle(), theme); 113 return theme; 114 } 115 116 private: ParsePattern(const RefPtr<ThemeStyle> & themeStyle,const RefPtr<TextFieldTheme> & theme)117 void ParsePattern(const RefPtr<ThemeStyle>& themeStyle, const RefPtr<TextFieldTheme>& theme) const 118 { 119 if (!themeStyle || !theme) { 120 return; 121 } 122 auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>("textfield_pattern", nullptr); 123 if (!pattern) { 124 LOGW("find pattern of textfield fail"); 125 return; 126 } 127 const double defaultErrorAlpha = 0.6; 128 theme->fontSize_ = pattern->GetAttr<Dimension>(PATTERN_TEXT_SIZE, 0.0_fp); 129 theme->textColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, Color()); 130 theme->focusTextColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR_FOCUSED, Color()); 131 theme->placeholderColor_ = pattern->GetAttr<Color>("tips_text_color", Color()); 132 theme->focusPlaceholderColor_ = pattern->GetAttr<Color>("tips_text_color_focused", Color()); 133 theme->bgColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR, Color()); 134 theme->focusBgColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_FOCUSED, Color()); 135 // color of error border blend 60% opacity 136 theme->errorBorderColor_ = pattern->GetAttr<Color>("error_text_border_color", Color()) 137 .BlendOpacity(pattern->GetAttr<double>("error_text_border_color_alpha", defaultErrorAlpha)); 138 theme->errorTextStyle_.SetTextColor(pattern->GetAttr<Color>("error_text_color", Color())); 139 theme->errorTextStyle_.SetFontSize(pattern->GetAttr<Dimension>("error_text_font_size", 0.0_fp)); 140 141 theme->countTextStyle_.SetTextColor(pattern->GetAttr<Color>("count_text_color", Color())); 142 theme->countTextStyle_.SetFontSize(pattern->GetAttr<Dimension>("count_text_font_size", 0.0_fp)); 143 theme->overCountStyle_.SetTextColor(pattern->GetAttr<Color>("over_text_color", Color())); 144 theme->overCountStyle_.SetFontSize(pattern->GetAttr<Dimension>("over_text_font_size", 0.0_fp)); 145 146 theme->countTextStyleOuter_.SetTextColor(pattern->GetAttr<Color>("count_outer_text_color", Color())); 147 theme->countTextStyleOuter_.SetFontSize( 148 pattern->GetAttr<Dimension>("count_outer_text_font_size", 0.0_fp)); 149 theme->overCountStyleOuter_.SetTextColor(pattern->GetAttr<Color>("over_outer_text_color", Color())); 150 theme->overCountStyleOuter_.SetFontSize( 151 pattern->GetAttr<Dimension>("over_outer_text_font_size", 0.0_fp)); 152 theme->selectedColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_SELECTED, Color()); 153 theme->disableTextColor_ = pattern->GetAttr<Color>(PATTERN_TEXT_COLOR_DISABLED, Color()); 154 theme->cursorColor_ = pattern->GetAttr<Color>("cursor_color", Color()); 155 theme->hoverColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_HOVERED, Color()); 156 theme->pressColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_PRESSED, Color()); 157 theme->disabledIconFillColor_ = theme->bgColor_.BlendOpacity(theme->disableOpacityRatio_); 158 } 159 }; 160 161 ~TextFieldTheme() override = default; 162 GetPadding()163 const Edge& GetPadding() const 164 { 165 return padding_; 166 } 167 GetHeight()168 const Dimension& GetHeight() const 169 { 170 return height_; 171 } 172 GetFontSize()173 const Dimension& GetFontSize() const 174 { 175 return fontSize_; 176 } 177 GetFontWeight()178 const FontWeight& GetFontWeight() const 179 { 180 return fontWeight_; 181 } 182 GetBorderRadius()183 const Radius& GetBorderRadius() const 184 { 185 return borderRadius_; 186 } 187 GetBgColor()188 const Color& GetBgColor() const 189 { 190 return bgColor_; 191 } 192 GetPlaceholderColor()193 const Color& GetPlaceholderColor() const 194 { 195 return placeholderColor_; 196 } 197 GetFocusBgColor()198 const Color& GetFocusBgColor() const 199 { 200 return focusBgColor_; 201 } 202 GetFocusPlaceholderColor()203 const Color& GetFocusPlaceholderColor() const 204 { 205 return focusPlaceholderColor_; 206 } 207 GetFocusTextColor()208 const Color& GetFocusTextColor() const 209 { 210 return focusTextColor_; 211 } 212 GetTextColor()213 const Color& GetTextColor() const 214 { 215 return textColor_; 216 } 217 GetDisableTextColor()218 const Color& GetDisableTextColor() const 219 { 220 return disableTextColor_; 221 } 222 GetSelectedColor()223 const Color& GetSelectedColor() const 224 { 225 return selectedColor_; 226 } 227 GetHoverColor()228 const Color& GetHoverColor() const 229 { 230 return hoverColor_; 231 } 232 GetPressColor()233 const Color& GetPressColor() const 234 { 235 return pressColor_; 236 } 237 GetDisableOpacityRatio()238 double GetDisableOpacityRatio() const 239 { 240 return disableOpacityRatio_; 241 } 242 GetDisabledIconFillColor()243 const Color& GetDisabledIconFillColor() const 244 { 245 return disabledIconFillColor_; 246 } 247 GetOverHideLength()248 const Dimension& GetOverHideLength() const 249 { 250 return overHideLength_; 251 } 252 GetCursorColor()253 const Color& GetCursorColor() const 254 { 255 return cursorColor_; 256 } 257 GetCursorRadius()258 const Dimension& GetCursorRadius() const 259 { 260 return cursorRadius_; 261 } 262 NeedFade()263 bool NeedFade() const 264 { 265 return needFade_; 266 } 267 GetIconSize()268 const Dimension& GetIconSize() const 269 { 270 return iconSize_; 271 } 272 GetIconHotZoneSize()273 const Dimension& GetIconHotZoneSize() const 274 { 275 return iconHotZoneSize_; 276 } 277 ShowEllipsis()278 bool ShowEllipsis() const 279 { 280 return showEllipsis_; 281 } 282 GetErrorSpacing()283 const Dimension& GetErrorSpacing() const 284 { 285 return errorSpacing_; 286 } 287 GetErrorIsInner()288 bool GetErrorIsInner() const 289 { 290 return errorIsInner_; 291 } 292 GetErrorBorderWidth()293 const Dimension& GetErrorBorderWidth() const 294 { 295 return errorBorderWidth_; 296 } 297 GetErrorBorderColor()298 const Color& GetErrorBorderColor() const 299 { 300 return errorBorderColor_; 301 } 302 GetErrorTextStyle()303 const TextStyle& GetErrorTextStyle() const 304 { 305 return errorTextStyle_; 306 } 307 GetCountTextStyle()308 const TextStyle& GetCountTextStyle() const 309 { 310 return countTextStyle_; 311 } 312 GetOverCountStyle()313 const TextStyle& GetOverCountStyle() const 314 { 315 return overCountStyle_; 316 } 317 GetCountTextStyleOuter()318 const TextStyle& GetCountTextStyleOuter() const 319 { 320 return countTextStyleOuter_; 321 } 322 GetOverCountStyleOuter()323 const TextStyle& GetOverCountStyleOuter() const 324 { 325 return overCountStyleOuter_; 326 } 327 328 protected: 329 TextFieldTheme() = default; 330 331 private: 332 Edge padding_; 333 Dimension height_; 334 Dimension fontSize_; 335 FontWeight fontWeight_ = FontWeight::NORMAL; 336 Radius borderRadius_; 337 338 Color bgColor_; 339 Color placeholderColor_; 340 Color focusBgColor_; 341 Color focusPlaceholderColor_; 342 Color focusTextColor_; 343 Color textColor_; 344 Color disableTextColor_; 345 Color selectedColor_; 346 Color hoverColor_; 347 Color pressColor_; 348 Color disabledIconFillColor_; 349 Dimension errorSpacing_; 350 bool errorIsInner_ = false; 351 Dimension errorBorderWidth_; 352 Color errorBorderColor_; 353 TextStyle errorTextStyle_; 354 TextStyle countTextStyle_; 355 TextStyle overCountStyle_; 356 TextStyle countTextStyleOuter_; 357 TextStyle overCountStyleOuter_; 358 359 // UX::disable state: opacity is set to 38% of the default 360 double disableOpacityRatio_ = 1.0; 361 362 // UX::over length state 363 Dimension overHideLength_; 364 365 // UX::cursor state cursor-color=#000000, cursor blur-radius=0.9, cursor-width=2, cursor-height=24, cursor-radius=1 366 Color cursorColor_; 367 Dimension cursorRadius_; 368 bool needFade_ = false; 369 370 // UX::icon size = 24, hotZoneSize = 36 371 Dimension iconSize_; 372 Dimension iconHotZoneSize_; 373 374 bool showEllipsis_ = true; 375 }; 376 377 } // namespace OHOS::Ace 378 379 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_TEXTFIELD_THEME_H 380