From 9f1663fad815ad4de8c0e7252ebd192cb938bd6e Mon Sep 17 00:00:00 2001 From: kseniazakharova Date: Tue, 27 Aug 2024 13:53:10 +0300 Subject: [PATCH 1/2] [HOS-2732] - Create ArkThemeWrapper for TextInput --- .../theme_apply/js_textinput_theme.h | 71 ------------------ .../src/theme/ArkThemeWhiteList.ts | 1 - .../declarative_frontend/engine/arkTheme.js | 1 - .../jsview/js_textfield.cpp | 12 ++-- .../jsview/js_textinput.cpp | 2 - .../jsview/models/text_field_model_impl.h | 2 + .../components/text_field/textfield_theme.h | 16 ++--- .../components/theme/theme_manager_impl.cpp | 4 +- .../text_field_content_modifier.cpp | 5 ++ .../text_field/text_field_content_modifier.h | 1 + .../text_field_layout_algorithm.cpp | 25 ++++--- .../text_field/text_field_layout_algorithm.h | 2 +- .../pattern/text_field/text_field_model.h | 2 + .../text_field/text_field_model_ng.cpp | 14 +++- .../pattern/text_field/text_field_model_ng.h | 2 + .../text_field/text_field_paint_property.cpp | 3 +- .../pattern/text_field/text_field_pattern.cpp | 35 ++++++++- .../pattern/text_field/text_field_pattern.h | 2 + .../text_field/text_field_theme_wrapper.h | 72 +++++++++++++++++++ 19 files changed, 170 insertions(+), 102 deletions(-) delete mode 100644 frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_textinput_theme.h create mode 100644 frameworks/core/components_ng/pattern/text_field/text_field_theme_wrapper.h diff --git a/frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_textinput_theme.h b/frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_textinput_theme.h deleted file mode 100644 index 47a36bfe8c9c..000000000000 --- a/frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_textinput_theme.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_THEME_JS_TEXTINPUT_THEME_H -#define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_THEME_JS_TEXTINPUT_THEME_H - -#include "bridge/declarative_frontend/ark_theme/theme_apply/js_theme_utils.h" -#include "core/components_ng/base/view_abstract_model.h" -#include "core/components_ng/base/view_stack_model.h" -#include "core/components_ng/pattern/text_field/text_field_model.h" - -namespace OHOS::Ace::Framework { -class JSTextInputTheme { -public: - static void ApplyTheme() - { - // check whether we have Theme for id or not - auto themeColors = JSThemeUtils::GetThemeColors(); - if (!themeColors) { - // no need to apply custom theme colors - return; - } - - // normal - ViewStackModel::GetInstance()->SetVisualState(VisualState::NORMAL); - TextFieldModel::GetInstance()->SetBackgroundColor(themeColors->CompBackgroundTertiary(), false); - TextFieldModel::GetInstance()->SetTextColor(themeColors->FontPrimary()); - TextFieldModel::GetInstance()->SetPlaceholderColor(themeColors->FontSecondary()); - TextFieldModel::GetInstance()->SetCaretColor(themeColors->Brand()); - ViewAbstractModel::GetInstance()->SetOuterBorderColor(themeColors->InteractiveFocus()); - ViewAbstractModel::GetInstance()->SetOuterBorderWidth({}); - Color selectedColor = themeColors.value().Brand(); - // Alpha = 255 means opaque - if (selectedColor.GetAlpha() == JSThemeUtils::DEFAULT_ALPHA) { - // Default setting of 20% opacity - selectedColor = selectedColor.ChangeOpacity(JSThemeUtils::DEFAULT_OPACITY); - } - TextFieldModel::GetInstance()->SetSelectedBackgroundColor(selectedColor); - - // focused - ViewStackModel::GetInstance()->SetVisualState(VisualState::FOCUSED); - TextFieldModel::GetInstance()->SetBackgroundColor(themeColors->CompBackgroundTertiary(), false); - TextFieldModel::GetInstance()->SetCaretColor(themeColors->Brand()); - CalcDimension outerBorderWidth(JSTextInputTheme::outerBorderWidthPx, OHOS::Ace::DimensionUnit::PX); - ViewAbstractModel::GetInstance()->SetOuterBorderWidth(outerBorderWidth); - - // pressed - ViewStackModel::GetInstance()->SetVisualState(VisualState::PRESSED); - TextFieldModel::GetInstance()->SetCaretColor(themeColors->Brand()); - ViewAbstractModel::GetInstance()->SetOuterBorderWidth({}); - - // clear state - ViewStackModel::GetInstance()->ClearVisualState(); - } -private: - static constexpr double outerBorderWidthPx = 2.0; -}; -} -#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_THEME_JS_TEXTINPUT_THEME_H \ No newline at end of file diff --git a/frameworks/bridge/declarative_frontend/ark_theme/theme_manager/src/theme/ArkThemeWhiteList.ts b/frameworks/bridge/declarative_frontend/ark_theme/theme_manager/src/theme/ArkThemeWhiteList.ts index 89f4eef4ec04..1c1288630abb 100644 --- a/frameworks/bridge/declarative_frontend/ark_theme/theme_manager/src/theme/ArkThemeWhiteList.ts +++ b/frameworks/bridge/declarative_frontend/ark_theme/theme_manager/src/theme/ArkThemeWhiteList.ts @@ -33,7 +33,6 @@ class ArkThemeWhiteList { 'Select', 'Slider', 'TextClock', - 'TextInput', 'TextPicker', 'TimePicker', 'Toggle', diff --git a/frameworks/bridge/declarative_frontend/engine/arkTheme.js b/frameworks/bridge/declarative_frontend/engine/arkTheme.js index 8e792324e8a9..b2b2398c88cc 100644 --- a/frameworks/bridge/declarative_frontend/engine/arkTheme.js +++ b/frameworks/bridge/declarative_frontend/engine/arkTheme.js @@ -1087,7 +1087,6 @@ ArkThemeWhiteList.whiteList = [ 'Select', 'Slider', 'TextClock', - 'TextInput', 'TextPicker', 'TimePicker', 'Toggle', diff --git a/frameworks/bridge/declarative_frontend/jsview/js_textfield.cpp b/frameworks/bridge/declarative_frontend/jsview/js_textfield.cpp index edc1a737cd7f..a51a48fdd055 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_textfield.cpp +++ b/frameworks/bridge/declarative_frontend/jsview/js_textfield.cpp @@ -513,9 +513,8 @@ void JSTextField::SetTextColor(const JSCallbackInfo& info) } Color textColor; if (!ParseJsColor(info[0], textColor)) { - auto theme = GetTheme(); - CHECK_NULL_VOID(theme); - textColor = theme->GetTextColor(); + TextFieldModel::GetInstance()->ResetTextColor(); + return; } TextFieldModel::GetInstance()->SetTextColor(textColor); } @@ -639,8 +638,11 @@ void JSTextField::SetBackgroundColor(const JSCallbackInfo& info) return; } Color backgroundColor; - bool tmp = !ParseJsColor(info[0], backgroundColor); - TextFieldModel::GetInstance()->SetBackgroundColor(backgroundColor, tmp); + if (!ParseJsColor(info[0], backgroundColor)) { + TextFieldModel::GetInstance()->ResetBackgroundColor(); + return; + } + TextFieldModel::GetInstance()->SetBackgroundColor(backgroundColor, false); } void JSTextField::JsHeight(const JSCallbackInfo& info) diff --git a/frameworks/bridge/declarative_frontend/jsview/js_textinput.cpp b/frameworks/bridge/declarative_frontend/jsview/js_textinput.cpp index c2f4ce722727..3cfde7663459 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_textinput.cpp +++ b/frameworks/bridge/declarative_frontend/jsview/js_textinput.cpp @@ -27,7 +27,6 @@ #include "frameworks/bridge/declarative_frontend/jsview/js_text_editable_controller.h" #include "frameworks/bridge/declarative_frontend/jsview/js_textfield.h" #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h" -#include "frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_textinput_theme.h" #include "frameworks/bridge/declarative_frontend/view_stack_processor.h" #include "frameworks/core/common/ime/text_input_action.h" #include "frameworks/core/common/ime/text_input_type.h" @@ -135,7 +134,6 @@ void JSTextInput::JSBind(BindingTarget globalObj) void JSTextInput::Create(const JSCallbackInfo& info) { JSTextField::CreateTextInput(info); - JSTextInputTheme::ApplyTheme(); } void JSTextInputController::JSBind(BindingTarget globalObj) diff --git a/frameworks/bridge/declarative_frontend/jsview/models/text_field_model_impl.h b/frameworks/bridge/declarative_frontend/jsview/models/text_field_model_impl.h index f45f3a5dd03a..d38178341e18 100644 --- a/frameworks/bridge/declarative_frontend/jsview/models/text_field_model_impl.h +++ b/frameworks/bridge/declarative_frontend/jsview/models/text_field_model_impl.h @@ -51,6 +51,7 @@ public: void SetFontSize(const Dimension& value) override; void SetFontWeight(FontWeight value) override; void SetTextColor(const Color& value) override; + void ResetTextColor() override {}; void SetFontStyle(FontStyle value) override; void SetFontFamily(const std::vector& value) override; void SetInputFilter(const std::string& value, const std::function& onError) override; @@ -74,6 +75,7 @@ public: void SetShowUnit(std::function&& unitAction) override {}; void SetOnChangeEvent(std::function&& func) override {}; void SetBackgroundColor(const Color& color, bool tmp) override; + void ResetBackgroundColor() override {}; void SetHeight(const Dimension& value) override; void SetPadding(const NG::PaddingProperty& newPadding, Edge oldPadding, bool tmp) override; void SetBackBorder() override; diff --git a/frameworks/core/components/text_field/textfield_theme.h b/frameworks/core/components/text_field/textfield_theme.h index 0dc1c04451e3..2dea2278e0ed 100644 --- a/frameworks/core/components/text_field/textfield_theme.h +++ b/frameworks/core/components/text_field/textfield_theme.h @@ -55,7 +55,7 @@ public: return theme; } - private: + protected: void ParsePattern(const RefPtr& themeStyle, const RefPtr& theme) const { if (!themeStyle || !theme) { @@ -70,7 +70,7 @@ public: ParsePatternSubSecondPart(pattern, theme); ParsePatternSubThirdPart(pattern, theme); } - + private: void ParsePatternSubFirstPart(const RefPtr& pattern, const RefPtr& theme) const { theme->padding_ = Edge(pattern->GetAttr("textfield_padding_horizontal", 0.0_vp), @@ -657,6 +657,12 @@ public: } protected: TextFieldTheme() = default; + TextStyle textStyle_; + Color textColor_; + Color placeholderColor_; + Color bgColor_; + Color focusBgColor_; + Color cursorColor_; private: Edge padding_; @@ -668,13 +674,9 @@ private: FontWeight fontWeight_ = FontWeight::NORMAL; Radius borderRadius_; - Color bgColor_; Radius borderRadiusSize_; - Color placeholderColor_; - Color focusBgColor_; Color focusPlaceholderColor_; Color focusTextColor_; - Color textColor_; Color disableTextColor_; Color underlineActivedColor_; Color underlineTypingColor_; @@ -696,7 +698,6 @@ private: Color passwordErrorInputColor_; Color passwordErrorBorderColor_; Color passwordErrorLableColor_; - TextStyle textStyle_; TextStyle errorTextStyle_; TextStyle countTextStyle_; TextStyle overCountStyle_; @@ -721,7 +722,6 @@ private: Dimension overHideLength_; // UX::cursor state cursor-color=#000000, cursor blur-radius=0.9, cursor-width=2, cursor-height=24, cursor-radius=1 - Color cursorColor_; Dimension cursorRadius_; Dimension cursorWidth_; bool needFade_ = false; diff --git a/frameworks/core/components/theme/theme_manager_impl.cpp b/frameworks/core/components/theme/theme_manager_impl.cpp index bba59713a461..8300396a9b5f 100644 --- a/frameworks/core/components/theme/theme_manager_impl.cpp +++ b/frameworks/core/components/theme/theme_manager_impl.cpp @@ -82,6 +82,7 @@ #include "core/components_ng/pattern/radio/radio_theme_wrapper.h" #include "core/components_ng/pattern/swiper/swiper_theme_wrapper.h" #include "core/components_ng/pattern/text/text_theme_wrapper.h" +#include "core/components_ng/pattern/text_field/text_field_theme_wrapper.h" #include "core/components_ng/token_theme/token_theme_storage.h" namespace OHOS::Ace { @@ -173,7 +174,8 @@ const std::unordered_map(*)(const RefPt { QrcodeTheme::TypeId(), &ThemeWrapperBuildFunc }, { RadioTheme::TypeId(), &ThemeWrapperBuildFunc }, { SwiperIndicatorTheme::TypeId(), &ThemeWrapperBuildFunc }, - { TextTheme::TypeId(), &ThemeWrapperBuildFunc } + { TextTheme::TypeId(), &ThemeWrapperBuildFunc }, + { TextFieldTheme::TypeId(), &ThemeWrapperBuildFunc } }; } // namespace diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_content_modifier.cpp b/frameworks/core/components_ng/pattern/text_field/text_field_content_modifier.cpp index 16c177b643b1..83784cf833b0 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_content_modifier.cpp +++ b/frameworks/core/components_ng/pattern/text_field/text_field_content_modifier.cpp @@ -361,6 +361,11 @@ void TextFieldContentModifier::SetTextColor(const Color& value) animatableTextColor_->Set(LinearColor(value)); } +void TextFieldContentModifier::ResetTextColor() +{ + textColor_.reset(); +} + void TextFieldContentModifier::SetTextOverflow(const TextOverflow value) { if (textOverflow_->Get() != static_cast(value)) { diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_content_modifier.h b/frameworks/core/components_ng/pattern/text_field/text_field_content_modifier.h index 8ecef9037262..8341fa5eecd9 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_content_modifier.h +++ b/frameworks/core/components_ng/pattern/text_field/text_field_content_modifier.h @@ -43,6 +43,7 @@ public: void SetAdaptMaxFontSize(const Dimension& value); void SetFontWeight(const FontWeight& value); void SetTextColor(const Color& value); + void ResetTextColor(); void SetFontStyle(const OHOS::Ace::FontStyle& value); void SetContentOffset(OffsetF& value); float GetContentOffsetY(); diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_layout_algorithm.cpp b/frameworks/core/components_ng/pattern/text_field/text_field_layout_algorithm.cpp index 6b986691a4e4..eb291ed63adf 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/text_field/text_field_layout_algorithm.cpp @@ -50,17 +50,20 @@ void TextFieldLayoutAlgorithm::ConstructTextStyles( CHECK_NULL_VOID(frameNode); auto pipeline = frameNode->GetContext(); CHECK_NULL_VOID(pipeline); - auto textFieldTheme = pipeline->GetTheme(); + auto textFieldTheme = pipeline->GetTheme(frameNode->GetThemeScopeId()); CHECK_NULL_VOID(textFieldTheme); auto pattern = frameNode->GetPattern(); CHECK_NULL_VOID(pattern); auto textFieldLayoutProperty = pattern->GetLayoutProperty(); CHECK_NULL_VOID(textFieldLayoutProperty); + auto textFieldPaintProperty = pattern->GetPaintProperty(); + CHECK_NULL_VOID(textFieldPaintProperty); auto isInlineStyle = pattern->IsNormalInlineState(); auto isTextArea = pattern->IsTextArea(); if (!pattern->GetTextValue().empty()) { - UpdateTextStyle(frameNode, textFieldLayoutProperty, textFieldTheme, textStyle, pattern->IsDisabled()); + UpdateTextStyle(frameNode, textFieldLayoutProperty, textFieldTheme, textStyle, pattern->IsDisabled(), + textFieldPaintProperty->HasTextColorFlagByUser()); textContent = pattern->GetTextValue(); UpdateTextStyleTextOverflowAndWordBreak(textStyle, isTextArea, isInlineStyle, textFieldLayoutProperty); } else { @@ -428,16 +431,18 @@ TextAlign TextFieldLayoutAlgorithm::GetCounterNodeAlignment(LayoutWrapper* layou void TextFieldLayoutAlgorithm::UpdateCounterNode( uint32_t textLength, uint32_t maxLength, const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper) { - auto pipeline = PipelineBase::GetCurrentContext(); - CHECK_NULL_VOID(pipeline); - auto theme = pipeline->GetTheme(); - CHECK_NULL_VOID(theme); auto frameNode = layoutWrapper->GetHostNode(); CHECK_NULL_VOID(frameNode); auto pattern = frameNode->GetPattern(); CHECK_NULL_VOID(pattern); auto counterNode = pattern->GetCounterNode().Upgrade(); CHECK_NULL_VOID(counterNode); + auto pipeline = PipelineBase::GetCurrentContext(); + CHECK_NULL_VOID(pipeline); + auto host = counterNode->GetHostNode(); + CHECK_NULL_VOID(host); + auto theme = pipeline->GetTheme(host->GetThemeScopeId()); + CHECK_NULL_VOID(theme); auto textLayoutProperty = DynamicCast(counterNode->GetLayoutProperty()); CHECK_NULL_VOID(textLayoutProperty); auto textFieldLayoutProperty = pattern->GetLayoutProperty(); @@ -458,8 +463,7 @@ void TextFieldLayoutAlgorithm::UpdateCounterNode( textLayoutProperty->UpdateFontWeight(countTextStyle.GetFontWeight()); textLayoutProperty->UpdateTextAlign(GetCounterNodeAlignment(layoutWrapper)); textLayoutProperty->UpdateMaxLines(COUNTER_TEXT_MAXLINE); - auto host = counterNode->GetHostNode(); - CHECK_NULL_VOID(host); + auto context = host->GetRenderContext(); CHECK_NULL_VOID(context); context->UpdateForegroundColor(countTextStyle.GetTextColor()); @@ -576,7 +580,7 @@ float TextFieldLayoutAlgorithm::GetVisualTextWidth() const void TextFieldLayoutAlgorithm::UpdateTextStyle(const RefPtr& frameNode, const RefPtr& layoutProperty, const RefPtr& theme, - TextStyle& textStyle, bool isDisabled) + TextStyle& textStyle, bool isDisabled, bool isTextColorByUser) { const std::vector defaultFontFamily = { "sans-serif" }; textStyle.SetFontFamilies(layoutProperty->GetFontFamilyValue(defaultFontFamily)); @@ -606,6 +610,9 @@ void TextFieldLayoutAlgorithm::UpdateTextStyle(const RefPtr& frameNod } else if (renderContext->HasForegroundColorStrategy()) { textStyle.SetTextColor(Color::BLACK); } else { + if (!isTextColorByUser) { + layoutProperty->UpdateTextColor(theme ? theme->GetTextColor() : textStyle.GetTextColor()); + } textStyle.SetTextColor( layoutProperty->GetTextColorValue(theme ? theme->GetTextColor() : textStyle.GetTextColor())); } diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_layout_algorithm.h b/frameworks/core/components_ng/pattern/text_field/text_field_layout_algorithm.h index a3fa51585ee6..c18aa5264e1b 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_layout_algorithm.h +++ b/frameworks/core/components_ng/pattern/text_field/text_field_layout_algorithm.h @@ -78,7 +78,7 @@ public: static void UpdateTextStyle(const RefPtr& frameNode, const RefPtr& layoutProperty, const RefPtr& theme, - TextStyle& textStyle, bool isDisabled); + TextStyle& textStyle, bool isDisabled, bool isTextColorByUser = false); static void UpdatePlaceholderTextStyle(const RefPtr& frameNode, const RefPtr& layoutProperty, const RefPtr& theme, TextStyle& textStyle, bool isDisabled); diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_model.h b/frameworks/core/components_ng/pattern/text_field/text_field_model.h index 96ffe8bcb360..42b70e4debf9 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_model.h +++ b/frameworks/core/components_ng/pattern/text_field/text_field_model.h @@ -283,6 +283,7 @@ public: virtual void SetFontSize(const Dimension& value) = 0; virtual void SetFontWeight(FontWeight value) = 0; virtual void SetTextColor(const Color& value) = 0; + virtual void ResetTextColor() = 0; virtual void SetWordBreak(Ace::WordBreak value) {}; virtual void SetFontStyle(FontStyle value) = 0; virtual void SetFontFamily(const std::vector& value) = 0; @@ -304,6 +305,7 @@ public: virtual void ResetMaxLength() = 0; virtual void SetForegroundColor(const Color& value) = 0; virtual void SetBackgroundColor(const Color& color, bool tmp) = 0; + virtual void ResetBackgroundColor() = 0; virtual void SetHeight(const Dimension& value) = 0; virtual void SetPadding(const NG::PaddingProperty& newPadding, Edge oldPadding, bool tmp) = 0; virtual void SetMargin() {}; diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_model_ng.cpp b/frameworks/core/components_ng/pattern/text_field/text_field_model_ng.cpp index 8644f0eaeadc..fff94ec19a7c 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_model_ng.cpp +++ b/frameworks/core/components_ng/pattern/text_field/text_field_model_ng.cpp @@ -78,7 +78,7 @@ void TextFieldModelNG::CreateNode( } auto themeManager = pipeline->GetThemeManager(); CHECK_NULL_VOID(themeManager); - auto textFieldTheme = themeManager->GetTheme(); + auto textFieldTheme = themeManager->GetTheme(frameNode->GetThemeScopeId()); CHECK_NULL_VOID(textFieldTheme); textfieldPaintProperty->UpdatePressBgColor(textFieldTheme->GetPressColor()); textfieldPaintProperty->UpdateHoverBgColor(textFieldTheme->GetHoverColor()); @@ -399,6 +399,14 @@ void TextFieldModelNG::SetTextColor(const Color& value) ACE_UPDATE_RENDER_CONTEXT(ForegroundColorFlag, true); ACE_UPDATE_PAINT_PROPERTY(TextFieldPaintProperty, TextColorFlagByUser, value); } +void TextFieldModelNG::ResetTextColor() +{ + ACE_RESET_LAYOUT_PROPERTY_WITH_FLAG(TextFieldLayoutProperty, TextColor, PROPERTY_UPDATE_MEASURE_SELF); + ACE_RESET_PAINT_PROPERTY_WITH_FLAG(TextFieldPaintProperty, TextColorFlagByUser, PROPERTY_UPDATE_MEASURE_SELF); + ACE_RESET_RENDER_CONTEXT(RenderContext, ForegroundColor); + ACE_RESET_RENDER_CONTEXT(RenderContext, ForegroundColorStrategy); + ACE_RESET_RENDER_CONTEXT(RenderContext, ForegroundColorFlag); +} void TextFieldModelNG::SetWordBreak(Ace::WordBreak value) { ACE_UPDATE_LAYOUT_PROPERTY(TextFieldLayoutProperty, WordBreak, value); @@ -668,6 +676,10 @@ void TextFieldModelNG::SetBackgroundColor(const Color& color, bool tmp) ACE_UPDATE_PAINT_PROPERTY(TextFieldPaintProperty, BackgroundColor, backgroundColor); } +void TextFieldModelNG::ResetBackgroundColor() { + ACE_RESET_PAINT_PROPERTY_WITH_FLAG(TextFieldPaintProperty, BackgroundColor, PROPERTY_UPDATE_RENDER); +} + void TextFieldModelNG::SetBackgroundColor(FrameNode* frameNode, const Color& color) { CHECK_NULL_VOID(frameNode); diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_model_ng.h b/frameworks/core/components_ng/pattern/text_field/text_field_model_ng.h index 972ff4368631..e3c12118f8ee 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_model_ng.h +++ b/frameworks/core/components_ng/pattern/text_field/text_field_model_ng.h @@ -50,6 +50,7 @@ public: void SetFontSize(const Dimension& value) override; void SetFontWeight(FontWeight value) override; void SetTextColor(const Color& value) override; + void ResetTextColor() override; void SetWordBreak(Ace::WordBreak value) override; void SetFontStyle(Ace::FontStyle value) override; void SetFontFamily(const std::vector& value) override; @@ -87,6 +88,7 @@ public: void SetShowCounterBorder(bool value) override; void SetOnChangeEvent(std::function&& func) override; void SetBackgroundColor(const Color& color, bool tmp) override; + void ResetBackgroundColor() override; void SetHeight(const Dimension& value) override; void SetPadding(const NG::PaddingProperty& newPadding, Edge oldPadding, bool tmp) override; void SetMargin() override; diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_paint_property.cpp b/frameworks/core/components_ng/pattern/text_field/text_field_paint_property.cpp index 11c1ea33723d..ef883e7e7143 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_paint_property.cpp +++ b/frameworks/core/components_ng/pattern/text_field/text_field_paint_property.cpp @@ -26,7 +26,8 @@ void TextFieldPaintProperty::ToJsonValue(std::unique_ptr& json, const } auto pipeline = PipelineBase::GetCurrentContext(); CHECK_NULL_VOID(pipeline); - auto textFieldTheme = pipeline->GetTheme(); + auto host = GetHost(); + auto textFieldTheme = pipeline->GetTheme(host ? host->GetThemeScopeId() : 0); CHECK_NULL_VOID(textFieldTheme); json->PutExtAttr("placeholderColor", propCursorColor_.value_or(Color()).ColorToString().c_str(), filter); diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_pattern.cpp b/frameworks/core/components_ng/pattern/text_field/text_field_pattern.cpp index 08a615d1309b..e907e04ccf38 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_pattern.cpp +++ b/frameworks/core/components_ng/pattern/text_field/text_field_pattern.cpp @@ -3951,6 +3951,39 @@ bool TextFieldPattern::CloseCustomKeyboard() void TextFieldPattern::OnTextInputActionUpdate(TextInputAction value) {} +bool TextFieldPattern::OnThemeScopeUpdate(int32_t themeScopeId) +{ + bool result = false; + auto node = GetHost(); + CHECK_NULL_RETURN(node, result); + auto paintProperty = GetPaintProperty(); + CHECK_NULL_RETURN(paintProperty, false); + auto textFieldLayoutProperty = GetLayoutProperty(); + CHECK_NULL_RETURN(textFieldLayoutProperty, false); + auto pipeline = PipelineBase::GetCurrentContext(); + CHECK_NULL_RETURN(pipeline, result); + auto textFieldTheme = pipeline->GetTheme(themeScopeId); + CHECK_NULL_RETURN(textFieldTheme, result); + + if (!paintProperty->HasBackgroundColor()) { + Color bgColor = textFieldTheme->GetBgColor(); + auto renderContext = node->GetRenderContext(); + CHECK_NULL_RETURN(renderContext, result); + renderContext->UpdateBackgroundColor(bgColor); + result = true; + } + + if (!paintProperty->HasTextColorFlagByUser()) { + auto host = GetHost(); + CHECK_NULL_RETURN(host, false); + host->MarkDirtyNode(PROPERTY_UPDATE_MEASURE); + } + + paintProperty->UpdateCursorColor(textFieldTheme->GetCursorColor()); + + return result; +} + bool TextFieldPattern::BeforeIMEInsertValue(const std::string& insertValue, int32_t offset) { auto host = GetHost(); @@ -5693,7 +5726,7 @@ RefPtr TextFieldPattern::GetTheme() const { auto context = PipelineBase::GetCurrentContextSafely(); CHECK_NULL_RETURN(context, nullptr); - auto theme = context->GetTheme(); + auto theme = context->GetTheme(GetThemeScopeId()); return theme; } diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_pattern.h b/frameworks/core/components_ng/pattern/text_field/text_field_pattern.h index e168387f9d12..94111a5af3a4 100644 --- a/frameworks/core/components_ng/pattern/text_field/text_field_pattern.h +++ b/frameworks/core/components_ng/pattern/text_field/text_field_pattern.h @@ -1554,6 +1554,8 @@ protected: RefPtr selectController_; bool needToRefreshSelectOverlay_ = false; bool isTextChangedAtCreation_ = false; + + bool OnThemeScopeUpdate(int32_t themeScopeId) override; private: Offset ConvertTouchOffsetToTextOffset(const Offset& touchOffset); diff --git a/frameworks/core/components_ng/pattern/text_field/text_field_theme_wrapper.h b/frameworks/core/components_ng/pattern/text_field/text_field_theme_wrapper.h new file mode 100644 index 000000000000..bf971d7f1e05 --- /dev/null +++ b/frameworks/core/components_ng/pattern/text_field/text_field_theme_wrapper.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_THEME_WRAPPER_H +#define FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_THEME_WRAPPER_H + +#include + +#include "base/memory/ace_type.h" +#include "core/components/text_field/textfield_theme.h" +#include "core/components_ng/token_theme/token_theme_wrapper.h" + +namespace OHOS::Ace::NG { + +/** + * TextThemeWrapper defines color and styles of TextComponent basing on TokenTheme's data. + * TextThemeWrapper should be built using TextThemeWrapper::Builder. + */ +class TextFieldThemeWrapper : public TextFieldTheme, public TokenThemeWrapper +{ + DECLARE_ACE_TYPE(TextFieldThemeWrapper, TextFieldTheme); + +public: + class WrapperBuilder : public Builder + { + public: + WrapperBuilder() = default; + ~WrapperBuilder() = default; + + RefPtr BuildWrapper(const RefPtr& themeConstants) const + { + auto wrapper = AceType::Claim(new TextFieldThemeWrapper()); + auto theme = AceType::DynamicCast(wrapper); + if (!themeConstants) { + return wrapper; + } + ParsePattern(themeConstants->GetThemeStyle(), theme); + return wrapper; + } + }; + + ~TextFieldThemeWrapper() override = default; + + void ApplyTokenTheme(const TokenTheme& theme) override + { + if (auto colors = theme.Colors(); colors) { + textColor_ = colors->FontPrimary(); + placeholderColor_ = colors->FontSecondary(); + bgColor_ = colors->CompBackgroundTertiary(); + focusBgColor_ = colors->CompBackgroundTertiary(); + cursorColor_ = colors->Brand(); + } + } + +protected: + TextFieldThemeWrapper() = default; +}; + +} // namespace +#endif -- Gitee From 2f2b37d815cb1566af8d8b4f55b3fec19ea287ee Mon Sep 17 00:00:00 2001 From: Denis Uvarov Date: Fri, 20 Sep 2024 12:30:02 +0300 Subject: [PATCH 2/2] Theme options for BindSheet: * Get ThemeScopeId from associated item --- .../components_ng/base/view_abstract_model_ng.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/frameworks/core/components_ng/base/view_abstract_model_ng.cpp b/frameworks/core/components_ng/base/view_abstract_model_ng.cpp index cd39eacd3aaa..59ed28b56180 100644 --- a/frameworks/core/components_ng/base/view_abstract_model_ng.cpp +++ b/frameworks/core/components_ng/base/view_abstract_model_ng.cpp @@ -474,11 +474,20 @@ void ViewAbstractModelNG::BindSheet(bool isShow, std::functionGetMainFrameNode()); CHECK_NULL_VOID(targetNode); + auto themeId = targetNode->GetThemeScopeId(); auto instanceId = sheetStyle.instanceId.has_value() ? sheetStyle.instanceId.value() : Container::CurrentId(); - auto buildNodeFunc = [buildFunc, instanceId]() -> RefPtr { + auto buildNodeFunc = [buildFunc, instanceId, themeId]() -> RefPtr { NG::ScopedViewStackProcessor builderViewStackProcess(instanceId); buildFunc(); auto customNode = NG::ViewStackProcessor::GetInstance()->Finish(); + auto theme = customNode->GetThemeScopeId(); + if (!theme) { + // Update only if not set another value + customNode->AllowUseParentTheme(false); + customNode->UpdateThemeScopeId(themeId); + customNode->UpdateThemeScopeUpdate(themeId); + auto upd = customNode->GetThemeScopeId(); + } return customNode; }; auto buildTitleNodeFunc = [titleBuildFunc, instanceId]() -> RefPtr { -- Gitee