From 1ddaff742b03e32250ff51e984f8bc16bc471949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E9=A3=9E?= Date: Thu, 5 Dec 2024 20:23:15 +0800 Subject: [PATCH 1/3] Create Button Component of ThemeWrapper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡飞 --- .../ark_theme/theme_apply/js_button_theme.h | 100 ---------------- .../src/theme/ArkThemeWhiteList.ts | 1 - .../declarative_frontend/engine/arkTheme.js | 1 - .../declarative_frontend/jsview/js_button.cpp | 29 ++--- .../jsview/models/button_model_impl.h | 2 + .../core/components/button/button_theme.h | 11 +- .../components/theme/theme_manager_impl.cpp | 2 + .../button/button_layout_algorithm.cpp | 19 +-- .../pattern/button/button_layout_property.h | 3 + .../pattern/button/button_model.h | 2 + .../pattern/button/button_model_ng.cpp | 80 ++++--------- .../pattern/button/button_model_ng.h | 2 + .../pattern/button/button_pattern.cpp | 72 +++++++++--- .../pattern/button/button_pattern.h | 42 ++++++- .../pattern/button/button_theme_wrapper.h | 110 ++++++++++++++++++ .../pattern/button/toggle_button_pattern.cpp | 2 +- 16 files changed, 265 insertions(+), 213 deletions(-) delete mode 100644 frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_button_theme.h create mode 100644 frameworks/core/components_ng/pattern/button/button_theme_wrapper.h diff --git a/frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_button_theme.h b/frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_button_theme.h deleted file mode 100644 index 2d57bf2384..0000000000 --- a/frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_button_theme.h +++ /dev/null @@ -1,100 +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_BUTTON_THEME_H -#define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_THEME_JS_BUTTON_THEME_H - -#include "bridge/declarative_frontend/ark_theme/theme_apply/js_theme_utils.h" -#include "core/components_ng/base/view_stack_model.h" -#include "core/components_ng/pattern/button/button_model.h" - -namespace OHOS::Ace::Framework { -class JSButtonTheme { -public: - static bool ApplyTheme(const ButtonRole& role, const ButtonStyleMode& styleMode, bool isLabelButton) - { - JSButtonTheme::buttonRole = role; - JSButtonTheme::buttonStyleMode = styleMode; - return JSButtonTheme::ApplyTheme(isLabelButton); - } - static bool ApplyTheme(const ButtonRole& role, bool isLabelButton) - { - JSButtonTheme::buttonRole = role; - return JSButtonTheme::ApplyTheme(isLabelButton); - } - static bool ApplyTheme(const ButtonStyleMode& styleMode, bool isLabelButton) - { - JSButtonTheme::buttonStyleMode = styleMode; - return JSButtonTheme::ApplyTheme(isLabelButton); - } -private: - // last button role value - inline static ButtonRole buttonRole = ButtonRole::NORMAL; - // last button style mode value - inline static ButtonStyleMode buttonStyleMode = ButtonStyleMode::EMPHASIZE; - - static bool ApplyTheme(bool isLabelButton) - { - auto themeColors = JSThemeUtils::GetThemeColors(); - if (!themeColors) { - // no need to apply custom theme colors - return false; - } - - // normal - ViewStackModel::GetInstance()->SetVisualState(VisualState::NORMAL); - if (isLabelButton) { - ButtonModel::GetInstance()->SetFontColor(JSButtonTheme::FontColor(themeColors)); - } - ButtonModel::GetInstance()->BackgroundColor(JSButtonTheme::BackgroundColor(themeColors, false), true); - - // clear state - ViewStackModel::GetInstance()->ClearVisualState(); - return true; - } - - static Color FontColor(const std::optional& themeColors) - { - switch (JSButtonTheme::buttonStyleMode) { - case ButtonStyleMode::NORMAL: - case ButtonStyleMode::TEXT: - if (JSButtonTheme::buttonRole == ButtonRole::ERROR) { - return themeColors->Warning(); - } - return themeColors->FontEmphasize(); - case ButtonStyleMode::EMPHASIZE: - default: - return themeColors->FontOnPrimary(); - } - } - - static Color BackgroundColor(const std::optional& themeColors, bool isPressed) - { - switch (JSButtonTheme::buttonStyleMode) { - case ButtonStyleMode::TEXT: - return Color::TRANSPARENT; - case ButtonStyleMode::NORMAL: - return themeColors->CompBackgroundTertiary(); - case ButtonStyleMode::EMPHASIZE: - default: - if (JSButtonTheme::buttonRole == ButtonRole::ERROR) { - return themeColors->Warning(); - } - return themeColors->BackgroundEmphasize(); - } - } -}; -} // namespace OHOS::Ace::Framework -#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_THEME_JS_BUTTON_THEME_H 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 a9842986fa..57420be55c 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 @@ -22,7 +22,6 @@ class ArkThemeWhiteList { private static whiteList: Array = [ 'AlphabetIndexer', 'Badge', - 'Button', 'Checkbox', 'CheckboxGroup', 'Counter', diff --git a/frameworks/bridge/declarative_frontend/engine/arkTheme.js b/frameworks/bridge/declarative_frontend/engine/arkTheme.js index 086d179f18..0f102814a4 100644 --- a/frameworks/bridge/declarative_frontend/engine/arkTheme.js +++ b/frameworks/bridge/declarative_frontend/engine/arkTheme.js @@ -943,7 +943,6 @@ class ArkThemeWhiteList { ArkThemeWhiteList.whiteList = [ 'AlphabetIndexer', 'Badge', - 'Button', 'Checkbox', 'CheckboxGroup', 'Counter', diff --git a/frameworks/bridge/declarative_frontend/jsview/js_button.cpp b/frameworks/bridge/declarative_frontend/jsview/js_button.cpp index e49b01185f..1a25abacee 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_button.cpp +++ b/frameworks/bridge/declarative_frontend/jsview/js_button.cpp @@ -27,7 +27,6 @@ #include "core/components/button/button_theme.h" #include "core/components_ng/base/view_stack_processor.h" #include "core/components_ng/pattern/button/button_model_ng.h" -#include "frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_button_theme.h" #include "frameworks/bridge/declarative_frontend/engine/functions/js_click_function.h" #include "frameworks/bridge/declarative_frontend/engine/jsi/js_ui_index.h" #include "frameworks/bridge/declarative_frontend/jsview/js_utils.h" @@ -151,9 +150,7 @@ void JSButton::SetFontFamily(const JSCallbackInfo& info) { std::vector fontFamilies; if (!ParseJsFontFamilies(info[0], fontFamilies) || fontFamilies.empty()) { - auto pipelineContext = PipelineBase::GetCurrentContext(); - CHECK_NULL_VOID(pipelineContext); - auto textTheme = pipelineContext->GetTheme(); + auto textTheme = GetTheme(); CHECK_NULL_VOID(textTheme); fontFamilies = textTheme->GetTextStyle().GetFontFamilies(); } @@ -165,8 +162,8 @@ void JSButton::SetTextColor(const JSCallbackInfo& info) { Color textColor; if (!ParseJsColor(info[0], textColor)) { - auto buttonTheme = PipelineBase::GetCurrentContext()->GetTheme(); - textColor = buttonTheme->GetTextStyle().GetTextColor(); + ButtonModel::GetInstance()->ResetFontColor(); + return; } ButtonModel::GetInstance()->SetFontColor(textColor); @@ -199,9 +196,7 @@ void JSButton::SetButtonStyle(const JSCallbackInfo& info) } } auto buttonStyleMode = static_cast(value); - if (!JSButtonTheme::ApplyTheme(buttonStyleMode, isLabelButton_)) { - ButtonModel::GetInstance()->SetButtonStyle(buttonStyleMode); - } + ButtonModel::GetInstance()->SetButtonStyle(buttonStyleMode); } void JSButton::SetControlSize(const JSCallbackInfo& info) @@ -226,9 +221,7 @@ void JSButton::SetRole(const JSCallbackInfo& info) } } auto buttonRole = static_cast(value); - if (!JSButtonTheme::ApplyTheme(buttonRole, isLabelButton_)) { - ButtonModel::GetInstance()->SetRole(buttonRole); - } + ButtonModel::GetInstance()->SetRole(buttonRole); } void JSButton::SetStateEffect(const JSCallbackInfo& info) @@ -396,9 +389,6 @@ void JSButton::CreateWithLabel(const JSCallbackInfo& info) ButtonModel::GetInstance()->CreateWithLabel(para, buttonChildren); ButtonModel::GetInstance()->Create(para, buttonChildren); isLabelButton_ = true; - auto buttonRole = para.buttonRole.value_or(ButtonRole::NORMAL); - auto buttonStyleMode = para.buttonStyleMode.value_or(ButtonStyleMode::EMPHASIZE); - JSButtonTheme::ApplyTheme(buttonRole, buttonStyleMode, isLabelButton_); ButtonModel::GetInstance()->SetCreateWithLabel(true); } @@ -407,9 +397,6 @@ void JSButton::CreateWithChild(const JSCallbackInfo& info) CreateWithPara para = ParseCreatePara(info, false); ButtonModel::GetInstance()->CreateWithChild(para); isLabelButton_ = false; - auto buttonRole = para.buttonRole.value_or(ButtonRole::NORMAL); - auto buttonStyleMode = para.buttonStyleMode.value_or(ButtonStyleMode::EMPHASIZE); - JSButtonTheme::ApplyTheme(buttonRole, buttonStyleMode, isLabelButton_); ButtonModel::GetInstance()->SetCreateWithLabel(false); } @@ -563,10 +550,8 @@ void JSButton::JsBackgroundColor(const JSCallbackInfo& info) Color backgroundColor; bool colorFlag = ParseJsColor(info[0], backgroundColor); if (!colorFlag) { - auto buttonTheme = GetTheme(); - if (buttonTheme) { - backgroundColor = buttonTheme->GetBgColor(); - } + ButtonModel::GetInstance()->ResetBackgroundColor(); + return; } ButtonModel::GetInstance()->BackgroundColor(backgroundColor, colorFlag); diff --git a/frameworks/bridge/declarative_frontend/jsview/models/button_model_impl.h b/frameworks/bridge/declarative_frontend/jsview/models/button_model_impl.h index 41e71086f6..d089e5d0a3 100644 --- a/frameworks/bridge/declarative_frontend/jsview/models/button_model_impl.h +++ b/frameworks/bridge/declarative_frontend/jsview/models/button_model_impl.h @@ -45,6 +45,8 @@ public: const std::optional& radiusBottomLeft, const std::optional& radiusBottomRight) override {}; void SetRemoteMessage(RemoteCallback&& remoteCallback) override; + void ResetFontColor() override {}; + void ResetBackgroundColor() override {}; private: static RefPtr GetTextComponent(); diff --git a/frameworks/core/components/button/button_theme.h b/frameworks/core/components/button/button_theme.h index e56df0b581..d198d07cb9 100644 --- a/frameworks/core/components/button/button_theme.h +++ b/frameworks/core/components/button/button_theme.h @@ -47,7 +47,7 @@ public: return theme; } - private: + protected: void ParsePattern(const RefPtr& themeConstants, const RefPtr& theme) const { if (!themeConstants) { @@ -128,6 +128,7 @@ public: ParseSubStylePattern(buttonPattern, theme); } + private: void ParseSubStylePattern(const RefPtr& buttonPattern, const RefPtr& theme) const { std::unordered_map normalBgColorMap_ = { { ButtonStyleMode::EMPHASIZE, @@ -180,7 +181,7 @@ public: return radius_; } - const Color& GetBgColor() const + virtual const Color& GetBgColor() const { return bgColor_; } @@ -355,7 +356,7 @@ public: return innerPadding_; } - Color GetBgColor(ButtonStyleMode buttonStyle, ButtonRole buttonRole) const + virtual Color GetBgColor(ButtonStyleMode buttonStyle, ButtonRole buttonRole) const { auto bgColorMapByRole_ = bgColorMap_.find(buttonRole); if (bgColorMapByRole_ != bgColorMap_.end()) { @@ -368,7 +369,7 @@ public: return bgColor_; } - const Color& GetTextColor(ButtonStyleMode buttonStyle, ButtonRole buttonRole) const + virtual const Color& GetTextColor(ButtonStyleMode buttonStyle, ButtonRole buttonRole) const { auto roleResult = textColorByRoleMap_.find(buttonRole); auto result = textColorMap_.find(buttonStyle); @@ -452,6 +453,7 @@ public: protected: ButtonTheme() = default; + TextStyle textStyle_; private: Color bgColor_; @@ -471,7 +473,6 @@ private: Color downloadTextColor_; Color downloadBorderColor_; Color downloadProgressColor_; - TextStyle textStyle_; Edge padding_; Edge minCircleButtonPadding_; Edge maxCircleButtonPadding_; diff --git a/frameworks/core/components/theme/theme_manager_impl.cpp b/frameworks/core/components/theme/theme_manager_impl.cpp index ff1a0a2b83..fd0dfb4c4c 100644 --- a/frameworks/core/components/theme/theme_manager_impl.cpp +++ b/frameworks/core/components/theme/theme_manager_impl.cpp @@ -63,6 +63,7 @@ #include "core/components/tool_bar/tool_bar_theme.h" #include "core/components/video/video_theme.h" #include "core/components_ng/pattern/app_bar/app_bar_theme.h" +#include "core/components_ng/pattern/button/button_theme_wrapper.h" #include "core/components_ng/pattern/container_modal/container_modal_theme.h" #include "core/components_ng/pattern/form/form_theme.h" #include "core/components_ng/pattern/gauge/gauge_theme.h" @@ -160,6 +161,7 @@ RefPtr ThemeWrapperBuildFunc(const RefPtr const std::unordered_map(*)(const RefPtr&)> TOKEN_THEME_WRAPPER_BUILDERS = { + { ButtonTheme::TypeId(), &ThemeWrapperBuildFunc }, { TextTheme::TypeId(), &ThemeWrapperBuildFunc } }; } // namespace diff --git a/frameworks/core/components_ng/pattern/button/button_layout_algorithm.cpp b/frameworks/core/components_ng/pattern/button/button_layout_algorithm.cpp index e084aa773e..432ffcf92e 100644 --- a/frameworks/core/components_ng/pattern/button/button_layout_algorithm.cpp +++ b/frameworks/core/components_ng/pattern/button/button_layout_algorithm.cpp @@ -115,7 +115,7 @@ std::optional ButtonLayoutAlgorithm::HandleLabelCircleButtonConstraint(La CHECK_NULL_RETURN(host, constraintSize); auto* context = host->GetContextWithCheck(); CHECK_NULL_RETURN(context, constraintSize); - auto buttonTheme = context->GetTheme(); + auto buttonTheme = context->GetTheme(host->GetThemeScopeId()); CHECK_NULL_RETURN(buttonTheme, constraintSize); const auto& padding = buttonLayoutProperty->CreatePaddingAndBorder(); auto defaultHeight = GetDefaultHeight(layoutWrapper); @@ -155,7 +155,7 @@ void ButtonLayoutAlgorithm::HandleAdaptiveText(LayoutWrapper* layoutWrapper, Lay CHECK_NULL_VOID(host); auto* context = host->GetContextWithCheck(); CHECK_NULL_VOID(context); - auto buttonTheme = context->GetTheme(); + auto buttonTheme = context->GetTheme(host->GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); auto childWrapper = layoutWrapper->GetOrCreateChildByIndex(0); CHECK_NULL_VOID(childWrapper); @@ -238,7 +238,7 @@ void ButtonLayoutAlgorithm::PerformMeasureSelf(LayoutWrapper* layoutWrapper) CHECK_NULL_VOID(host); auto* context = host->GetContextWithCheck(); CHECK_NULL_VOID(context); - auto buttonTheme = context->GetTheme(); + auto buttonTheme = context->GetTheme(host->GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); auto defaultHeight = GetDefaultHeight(layoutWrapper); @@ -331,7 +331,7 @@ float ButtonLayoutAlgorithm::GetDefaultHeight(LayoutWrapper* layoutWrapper) CHECK_NULL_RETURN(frameNode, 0.0); auto* context = frameNode->GetContext(); CHECK_NULL_RETURN(context, 0.0); - auto buttonTheme = context->GetTheme(); + auto buttonTheme = context->GetTheme(frameNode->GetThemeScopeId()); CHECK_NULL_RETURN(buttonTheme, 0.0); if (frameNode->GetTag() == V2::TOGGLE_ETS_TAG) { auto toggleTheme = context->GetTheme(); @@ -350,7 +350,7 @@ float ButtonLayoutAlgorithm::GetDefaultBorderRadius(LayoutWrapper* layoutWrapper CHECK_NULL_RETURN(frameNode, 0.0f); auto* context = frameNode->GetContext(); CHECK_NULL_RETURN(context, 0.0f); - auto buttonTheme = context->GetTheme(); + auto buttonTheme = context->GetTheme(frameNode->GetThemeScopeId()); CHECK_NULL_RETURN(buttonTheme, 0.0f); ControlSize controlSize = layoutProperty->GetControlSize().value_or(ControlSize::NORMAL); return static_cast(buttonTheme->GetBorderRadius(controlSize).ConvertToPx()); @@ -380,7 +380,10 @@ bool ButtonLayoutAlgorithm::NeedAgingMeasure(LayoutWrapper* layoutWrapper) CHECK_NULL_RETURN(buttonLayoutProperty, false); auto pipeline = NG::PipelineContext::GetCurrentContextSafely(); CHECK_NULL_RETURN(pipeline, false); - auto buttonTheme = pipeline->GetTheme(); + auto frameNode = layoutWrapper->GetHostNode(); + CHECK_NULL_RETURN(frameNode, false); + auto buttonTheme = pipeline->GetTheme(frameNode->GetThemeScopeId()); + CHECK_NULL_RETURN(buttonTheme, false); float agingPadding = buttonTheme->GetAgingNormalPadding().ConvertToPx() * 2.0f; if (buttonLayoutProperty->HasControlSize() && buttonLayoutProperty->GetControlSize() == ControlSize::SMALL) { agingPadding = buttonTheme->GetAgingSmallPadding().ConvertToPx() * 2.0f; @@ -440,7 +443,9 @@ bool ButtonLayoutAlgorithm::IsAging(LayoutWrapper* layoutWrapper) } auto pipeline = NG::PipelineContext::GetCurrentContextSafely(); CHECK_NULL_RETURN(pipeline, false); - auto buttonTheme = pipeline->GetTheme(); + auto frameNode = layoutWrapper->GetHostNode(); + CHECK_NULL_RETURN(frameNode, false); + auto buttonTheme = pipeline->GetTheme(frameNode->GetThemeScopeId()); CHECK_NULL_RETURN(buttonTheme, false); auto fontScale = pipeline->GetFontScale(); if (!(NearEqual(fontScale, buttonTheme->GetBigFontSizeScale()) || diff --git a/frameworks/core/components_ng/pattern/button/button_layout_property.h b/frameworks/core/components_ng/pattern/button/button_layout_property.h index 1b642f58b7..2fe6925950 100644 --- a/frameworks/core/components_ng/pattern/button/button_layout_property.h +++ b/frameworks/core/components_ng/pattern/button/button_layout_property.h @@ -46,6 +46,7 @@ public: value->propFontSize_ = CloneFontSize(); value->propFontWeight_ = CloneFontWeight(); value->propFontColor_ = CloneFontColor(); + value->propBackgroundColor_ = CloneBackgroundColor(); value->propFontFamily_ = CloneFontFamily(); value->propFontStyle_ = CloneFontStyle(); value->propLabel_ = CloneLabel(); @@ -65,6 +66,7 @@ public: ResetFontSize(); ResetFontWeight(); ResetFontColor(); + ResetBackgroundColor(); ResetFontFamily(); ResetFontStyle(); ResetLabel(); @@ -81,6 +83,7 @@ public: ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(FontSize, Dimension, PROPERTY_UPDATE_NORMAL); ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(FontWeight, Ace::FontWeight, PROPERTY_UPDATE_NORMAL); ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(FontColor, Color, PROPERTY_UPDATE_NORMAL); + ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(BackgroundColor, Color, PROPERTY_UPDATE_NORMAL); ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(FontFamily, std::vector, PROPERTY_UPDATE_NORMAL); ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(FontStyle, Ace::FontStyle, PROPERTY_UPDATE_NORMAL); ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(Label, std::string, PROPERTY_UPDATE_NORMAL); diff --git a/frameworks/core/components_ng/pattern/button/button_model.h b/frameworks/core/components_ng/pattern/button/button_model.h index d492bc1cce..67344ef368 100644 --- a/frameworks/core/components_ng/pattern/button/button_model.h +++ b/frameworks/core/components_ng/pattern/button/button_model.h @@ -59,6 +59,8 @@ public: virtual void SetControlSize(const std::optional& controlSize) {} virtual void SetRole(const std::optional& buttonRole) {} virtual void SetCreateWithLabel(bool isLabelButton) {} + virtual void ResetFontColor() = 0; + virtual void ResetBackgroundColor() = 0; private: static std::unique_ptr instance_; diff --git a/frameworks/core/components_ng/pattern/button/button_model_ng.cpp b/frameworks/core/components_ng/pattern/button/button_model_ng.cpp index 23674d5a4b..78232211ed 100644 --- a/frameworks/core/components_ng/pattern/button/button_model_ng.cpp +++ b/frameworks/core/components_ng/pattern/button/button_model_ng.cpp @@ -76,18 +76,6 @@ void ButtonModelNG::SetButtonStyle(const std::optional& buttonS { if (buttonStyle.has_value()) { ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, ButtonStyle, buttonStyle.value()); - auto context = PipelineBase::GetCurrentContextSafely(); - CHECK_NULL_VOID(context); - auto buttonTheme = context->GetTheme(); - CHECK_NULL_VOID(buttonTheme); - auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode(); - auto layoutProperty = frameNode->GetLayoutProperty(); - CHECK_NULL_VOID(layoutProperty); - ButtonRole buttonRole = layoutProperty->GetButtonRole().value_or(ButtonRole::NORMAL); - auto bgColor = buttonTheme->GetBgColor(buttonStyle.value(), buttonRole); - auto textColor = buttonTheme->GetTextColor(buttonStyle.value(), buttonRole); - BackgroundColor(bgColor, true); - SetFontColor(textColor); } } @@ -95,18 +83,6 @@ void ButtonModelNG::SetRole(const std::optional& buttonRole) { if (buttonRole.has_value()) { ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, ButtonRole, buttonRole.value()); - auto context = PipelineBase::GetCurrentContextSafely(); - CHECK_NULL_VOID(context); - auto buttonTheme = context->GetTheme(); - CHECK_NULL_VOID(buttonTheme); - auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode(); - auto layoutProperty = frameNode->GetLayoutProperty(); - CHECK_NULL_VOID(layoutProperty); - ButtonStyleMode buttonStyleMode = layoutProperty->GetButtonStyle().value_or(ButtonStyleMode::EMPHASIZE); - auto bgColor = buttonTheme->GetBgColor(buttonStyleMode, buttonRole.value()); - auto textColor = buttonTheme->GetTextColor(buttonStyleMode, buttonRole.value()); - BackgroundColor(bgColor, true); - SetFontColor(textColor); } } @@ -114,9 +90,11 @@ void ButtonModelNG::SetControlSize(const std::optional& controlSize { if (controlSize.has_value()) { ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, ControlSize, controlSize.value()); + auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode(); + CHECK_NULL_VOID(frameNode); auto context = PipelineBase::GetCurrentContextSafely(); CHECK_NULL_VOID(context); - auto buttonTheme = context->GetTheme(); + auto buttonTheme = context->GetTheme(frameNode->GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); auto padding = buttonTheme->GetPadding(controlSize.value()); PaddingProperty defaultPadding = { CalcLength(padding.Left()), CalcLength(padding.Right()), @@ -131,17 +109,6 @@ void ButtonModelNG::SetRole(FrameNode* frameNode, const std::optionalGetTheme(); - CHECK_NULL_VOID(buttonTheme); - auto layoutProperty = frameNode->GetLayoutProperty(); - CHECK_NULL_VOID(layoutProperty); - ButtonStyleMode buttonStyleMode = layoutProperty->GetButtonStyle().value_or(ButtonStyleMode::EMPHASIZE); - auto bgColor = buttonTheme->GetBgColor(buttonStyleMode, buttonRole.value()); - auto textColor = buttonTheme->GetTextColor(buttonStyleMode, buttonRole.value()); - BackgroundColor(frameNode, bgColor, true); - SetFontColor(frameNode, textColor); } } @@ -149,17 +116,6 @@ void ButtonModelNG::SetButtonStyle(FrameNode* frameNode, const std::optionalGetTheme(); - CHECK_NULL_VOID(buttonTheme); - auto layoutProperty = frameNode->GetLayoutProperty(); - CHECK_NULL_VOID(layoutProperty); - ButtonRole buttonRole = layoutProperty->GetButtonRole().value_or(ButtonRole::NORMAL); - auto bgColor = buttonTheme->GetBgColor(buttonStyle.value(), buttonRole); - auto textColor = buttonTheme->GetTextColor(buttonStyle.value(), buttonRole); - BackgroundColor(frameNode, bgColor, true); - SetFontColor(frameNode, textColor); } } @@ -169,7 +125,7 @@ void ButtonModelNG::SetControlSize(FrameNode* frameNode, const std::optionalGetTheme(); + auto buttonTheme = context->GetTheme(frameNode->GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); auto padding = buttonTheme->GetPadding(controlSize.value()); PaddingProperty defaultPadding = { CalcLength(padding.Left()), CalcLength(padding.Right()), @@ -217,7 +173,7 @@ void ButtonModelNG::CreateWithLabel(const std::string& label) } auto context = PipelineBase::GetCurrentContextSafely(); CHECK_NULL_VOID(context); - auto buttonTheme = context->GetTheme(); + auto buttonTheme = context->GetTheme(buttonNode->GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); auto padding = buttonTheme->GetPadding(); PaddingProperty defaultPadding = { CalcLength(padding.Left()), CalcLength(padding.Right()), @@ -245,9 +201,7 @@ void ButtonModelNG::SetLabel(FrameNode* frameNode, const char* label) if (layoutProperty->GetPaddingProperty()) { return; } - auto context = frameNode->GetContext(); - CHECK_NULL_VOID(context); - auto buttonTheme = context->GetTheme(); + auto buttonTheme = PipelineBase::GetCurrentContextSafely()->GetTheme(frameNode->GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); auto padding = buttonTheme->GetPadding(); PaddingProperty defaultPadding = { CalcLength(padding.Left()), CalcLength(padding.Right()), @@ -290,7 +244,7 @@ RefPtr ButtonModelNG::CreateFrameNode(int32_t nodeId) } auto context = PipelineBase::GetCurrentContextSafely(); CHECK_NULL_RETURN(context, nullptr); - auto buttonTheme = context->GetTheme(); + auto buttonTheme = context->GetTheme(frameNode->GetThemeScopeId()); CHECK_NULL_RETURN(buttonTheme, nullptr); auto padding = buttonTheme->GetPadding(); PaddingProperty defaultPadding = { CalcLength(padding.Left()), CalcLength(padding.Right()), @@ -316,6 +270,7 @@ void ButtonModelNG::OnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& cl void ButtonModelNG::BackgroundColor(const Color& color, const bool& colorFlag) { + ACE_UPDATE_LAYOUT_PROPERTY(ButtonLayoutProperty, BackgroundColor, color); ViewAbstract::SetBackgroundColor(color); } @@ -422,10 +377,7 @@ void ButtonModelNG::SetTextDefaultStyle(const RefPtr& textNode, const CHECK_NULL_VOID(textNode); auto textLayoutProperty = textNode->GetLayoutProperty(); CHECK_NULL_VOID(textLayoutProperty); - auto context = textNode->GetContext(); - CHECK_NULL_VOID(context); - auto buttonTheme = context->GetTheme(); - CHECK_NULL_VOID(buttonTheme); + auto buttonTheme = PipelineBase::GetCurrentContextSafely()->GetTheme(textNode->GetThemeScopeId()); auto textStyle = buttonTheme->GetTextStyle(); textLayoutProperty->UpdateContent(label); textLayoutProperty->UpdateTextOverflow(TextOverflow::ELLIPSIS); @@ -510,7 +462,7 @@ void ButtonModelNG::SetLabelStyle(FrameNode* frameNode, const ButtonParameters& CHECK_NULL_VOID(layoutProperty); auto context = PipelineBase::GetCurrentContextSafely(); CHECK_NULL_VOID(context); - auto buttonTheme = context->GetTheme(); + auto buttonTheme = context->GetTheme(frameNode->GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); auto themeFontSize = buttonTheme->GetTextSize(layoutProperty->GetControlSizeValue(ControlSize::NORMAL)); ACE_UPDATE_NODE_LAYOUT_PROPERTY(ButtonLayoutProperty, FontSize, themeFontSize, frameNode); @@ -656,4 +608,16 @@ void ButtonModelNG::SetCreateWithLabel(FrameNode* frameNode, bool createWithLabe } property->UpdateCreateWithLabel(createWithLabel); } + +void ButtonModelNG::ResetFontColor() +{ + ACE_RESET_LAYOUT_PROPERTY_WITH_FLAG(ButtonLayoutProperty, FontColor, PROPERTY_UPDATE_NORMAL); + ACE_RESET_RENDER_CONTEXT(RenderContext, ForegroundColor); +} + +void ButtonModelNG::ResetBackgroundColor() +{ + ACE_RESET_LAYOUT_PROPERTY_WITH_FLAG(ButtonLayoutProperty, BackgroundColor, PROPERTY_UPDATE_NORMAL); + ACE_RESET_RENDER_CONTEXT(RenderContext, BackgroundColor); +} } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/button/button_model_ng.h b/frameworks/core/components_ng/pattern/button/button_model_ng.h index a5999d8a98..1cda0b8c76 100644 --- a/frameworks/core/components_ng/pattern/button/button_model_ng.h +++ b/frameworks/core/components_ng/pattern/button/button_model_ng.h @@ -58,6 +58,8 @@ public: void SetControlSize(const std::optional& controlSize) override; void SetRole(const std::optional& buttonRole) override; void SetCreateWithLabel(bool createWithLabel) override; + void ResetFontColor() override; + void ResetBackgroundColor() override; static RefPtr CreateFrameNode(int32_t nodeId); static void SetFontSize(FrameNode* frameNode, const Dimension& fontSize); static void SetFontWeight(FrameNode* frameNode, const Ace::FontWeight& fontWeight); diff --git a/frameworks/core/components_ng/pattern/button/button_pattern.cpp b/frameworks/core/components_ng/pattern/button/button_pattern.cpp index a7e240437d..58a21d4292 100644 --- a/frameworks/core/components_ng/pattern/button/button_pattern.cpp +++ b/frameworks/core/components_ng/pattern/button/button_pattern.cpp @@ -44,7 +44,7 @@ void ButtonPattern::OnAttachToFrameNode() CHECK_NULL_VOID(host); auto* pipeline = host->GetContextWithCheck(); CHECK_NULL_VOID(pipeline); - auto buttonTheme = pipeline->GetTheme(); + auto buttonTheme = pipeline->GetTheme(GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); clickedColor_ = buttonTheme->GetClickedColor(); auto renderContext = host->GetRenderContext(); @@ -57,7 +57,9 @@ bool ButtonPattern::NeedAgingUpdateText(RefPtr& layoutProp CHECK_NULL_RETURN(layoutProperty, false); auto pipeline = NG::PipelineContext::GetCurrentContextSafely(); CHECK_NULL_RETURN(pipeline, false); - auto buttonTheme = pipeline->GetTheme(); + auto host = layoutProperty->GetHost(); + CHECK_NULL_RETURN(host, false); + auto buttonTheme = pipeline->GetTheme(host->GetThemeScopeId()); CHECK_NULL_RETURN(buttonTheme, false); auto fontScale = pipeline->GetFontScale(); @@ -101,6 +103,8 @@ void ButtonPattern::UpdateTextLayoutProperty( } if (layoutProperty->GetFontColor().has_value()) { textLayoutProperty->UpdateTextColor(layoutProperty->GetFontColor().value()); + } else { + textLayoutProperty->ResetTextColor(); } if (layoutProperty->GetFontStyle().has_value()) { textLayoutProperty->UpdateItalicFontStyle(layoutProperty->GetFontStyle().value()); @@ -111,7 +115,9 @@ void ButtonPattern::UpdateTextLayoutProperty( auto pipeline = NG::PipelineContext::GetCurrentContextSafely(); CHECK_NULL_VOID(pipeline); - auto buttonTheme = pipeline->GetTheme(); + auto host = layoutProperty->GetHost(); + CHECK_NULL_VOID(host); + auto buttonTheme = pipeline->GetTheme(host->GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); if (NeedAgingUpdateText(layoutProperty)) { textLayoutProperty->UpdateMaxLines(buttonTheme->GetAgingTextMaxLines()); @@ -145,11 +151,11 @@ void ButtonPattern::UpdateTextStyle( CHECK_NULL_VOID(host); auto* pipeline = host->GetContextWithCheck(); CHECK_NULL_VOID(pipeline); - auto buttonTheme = pipeline->GetTheme(); + auto buttonTheme = pipeline->GetTheme(host->GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); if (!textLayoutProperty->HasTextColor()) { - ButtonStyleMode buttonStyle = layoutProperty->GetButtonStyle().value_or(ButtonStyleMode::EMPHASIZE); - ButtonRole buttonRole = layoutProperty->GetButtonRole().value_or(ButtonRole::NORMAL); + ButtonStyleMode buttonStyle = layoutProperty->GetButtonStyleValue(ButtonStyleMode::EMPHASIZE); + ButtonRole buttonRole = layoutProperty->GetButtonRoleValue(ButtonRole::NORMAL); Color fontColor = buttonTheme->GetTextColor(buttonStyle, buttonRole); textLayoutProperty->UpdateTextColor(fontColor); } @@ -187,7 +193,6 @@ void ButtonPattern::InitButtonLabel() CHECK_NULL_VOID(textNode); auto textLayoutProperty = textNode->GetLayoutProperty(); CHECK_NULL_VOID(textLayoutProperty); - UpdateTextLayoutProperty(layoutProperty, textLayoutProperty); auto buttonRenderContext = host->GetRenderContext(); CHECK_NULL_VOID(buttonRenderContext); auto textRenderContext = textNode->GetRenderContext(); @@ -197,6 +202,8 @@ void ButtonPattern::InitButtonLabel() } else { textRenderContext->UpdateClipEdge(buttonRenderContext->GetClipEdgeValue(true)); } + textRenderContext->ResetForegroundColor(); + UpdateTextLayoutProperty(layoutProperty, textLayoutProperty); textNode->MarkModifyDone(); textNode->MarkDirtyNode(); } @@ -416,7 +423,7 @@ void ButtonPattern::HandleBackgroundColor() CHECK_NULL_VOID(renderContext); auto layoutProperty = GetLayoutProperty(); CHECK_NULL_VOID(layoutProperty); - auto buttonTheme = pipeline->GetTheme(); + auto buttonTheme = pipeline->GetTheme(host->GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); ButtonStyleMode buttonStyle = layoutProperty->GetButtonStyle().value_or(ButtonStyleMode::EMPHASIZE); ButtonRole buttonRole = layoutProperty->GetButtonRole().value_or(ButtonRole::NORMAL); @@ -429,8 +436,6 @@ void ButtonPattern::HandleBackgroundColor() if (!renderContext->HasBackgroundColor()) { renderContext->UpdateBackgroundColor(buttonTheme->GetBgColor(buttonStyle, buttonRole)); } - themeBgColor_ = buttonTheme->GetBgColor(buttonStyle, buttonRole); - themeTextColor_ = buttonTheme->GetTextColor(buttonStyle, buttonRole); } void ButtonPattern::HandleEnabled() @@ -447,7 +452,7 @@ void ButtonPattern::HandleEnabled() CHECK_NULL_VOID(renderContext); auto* pipeline = host->GetContextWithCheck(); CHECK_NULL_VOID(pipeline); - auto theme = pipeline->GetTheme(); + auto theme = pipeline->GetTheme(GetThemeScopeId()); CHECK_NULL_VOID(theme); auto alpha = theme->GetBgDisabledAlpha(); auto originalOpacity = renderContext->GetOpacityValue(1.0); @@ -461,7 +466,7 @@ void ButtonPattern::AnimateTouchAndHover(RefPtr& renderContext, i CHECK_NULL_VOID(host); auto* pipeline = host->GetContextWithCheck(); CHECK_NULL_VOID(pipeline); - auto theme = pipeline->GetTheme(); + auto theme = pipeline->GetTheme(GetThemeScopeId()); CHECK_NULL_VOID(theme); TAG_LOGD(AceLogTag::ACE_SELECT_COMPONENT, "button animate touch from %{public}d to %{public}d", typeFrom, typeTo); Color blendColorFrom = GetColorFromType(theme, typeFrom); @@ -572,7 +577,7 @@ void ButtonPattern::OnColorConfigurationUpdate() } auto pipeline = node->GetContextWithCheck(); CHECK_NULL_VOID(pipeline); - auto buttonTheme = pipeline->GetTheme(); + auto buttonTheme = pipeline->GetTheme(GetThemeScopeId()); ButtonStyleMode buttonStyle = buttonLayoutProperty->GetButtonStyle().value_or(ButtonStyleMode::EMPHASIZE); ButtonRole buttonRole = buttonLayoutProperty->GetButtonRole().value_or(ButtonRole::NORMAL); auto renderContext = node->GetRenderContext(); @@ -591,6 +596,47 @@ void ButtonPattern::OnColorConfigurationUpdate() } } +bool ButtonPattern::OnThemeScopeUpdate(int32_t themeScopeId) +{ + bool result = false; + auto node = GetHost(); + CHECK_NULL_RETURN(node, result); + auto buttonLayoutProperty = node->GetLayoutProperty(); + CHECK_NULL_RETURN(buttonLayoutProperty, result); + if (buttonLayoutProperty->GetCreateWithLabelValue(true)) { + node->SetNeedCallChildrenUpdate(false); + } + + auto textNode = DynamicCast(node->GetFirstChild()); + CHECK_NULL_RETURN(textNode, result); + auto textLayoutProperty = textNode->GetLayoutProperty(); + CHECK_NULL_RETURN(textLayoutProperty, result); + auto pipeline = PipelineBase::GetCurrentContext(); + CHECK_NULL_RETURN(pipeline, result); + auto buttonTheme = pipeline->GetTheme(themeScopeId); + CHECK_NULL_RETURN(buttonTheme, result); + auto renderContext = node->GetRenderContext(); + CHECK_NULL_RETURN(renderContext, result); + + ButtonStyleMode buttonStyle = buttonLayoutProperty->GetButtonStyleValue(ButtonStyleMode::EMPHASIZE); + ButtonRole buttonRole = buttonLayoutProperty->GetButtonRoleValue(ButtonRole::NORMAL); + + if (!isFontColor_) { + Color fontColor = buttonTheme->GetTextColor(buttonStyle, buttonRole); + textLayoutProperty->UpdateTextColor(fontColor); + textNode->MarkDirtyNode(); + result = true; + } + + if (!isBackgroundColor_) { + Color bgColor = buttonTheme->GetBgColor(buttonStyle, buttonRole); + renderContext->UpdateBackgroundColor(bgColor); + result = true; + } + + return result; +} + void ButtonPattern::SetBuilderFunc(ButtonMakeCallback&& makeFunc) { if (makeFunc == nullptr) { diff --git a/frameworks/core/components_ng/pattern/button/button_pattern.h b/frameworks/core/components_ng/pattern/button/button_pattern.h index 01066d272a..6b42a547b2 100644 --- a/frameworks/core/components_ng/pattern/button/button_pattern.h +++ b/frameworks/core/components_ng/pattern/button/button_pattern.h @@ -75,7 +75,15 @@ public: focusPaintParam.SetPaintWidth(focusBorderWidth_); return { FocusType::NODE, true, FocusStyleType::INNER_BORDER, focusPaintParam }; } - return { FocusType::NODE, true, FocusStyleType::OUTER_BORDER }; + FocusPattern focusPattern(FocusType::NODE, true, FocusStyleType::OUTER_BORDER); + auto pipeline = PipelineBase::GetCurrentContext(); + CHECK_NULL_RETURN(pipeline, focusPattern); + auto theme = pipeline->GetTheme(GetThemeScopeId()); + CHECK_NULL_RETURN(theme, focusPattern); + FocusPaintParam focusPaintParam; + focusPaintParam.SetPaintColor(theme->GetBgFocusColor()); + focusPattern.SetFocusPaintParams(focusPaintParam); + return focusPattern; } bool IsNeedAdjustByAspectRatio() override @@ -130,9 +138,11 @@ public: CHECK_NULL_VOID(layoutProperty); auto context = PipelineBase::GetCurrentContext(); CHECK_NULL_VOID(context); - auto buttonTheme = context->GetTheme(); + auto buttonTheme = context->GetTheme(GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); auto textStyle = buttonTheme->GetTextStyle(); + ButtonStyleMode buttonStyle = layoutProperty->GetButtonStyle().value_or(ButtonStyleMode::EMPHASIZE); + ButtonRole buttonRole = layoutProperty->GetButtonRole().value_or(ButtonRole::NORMAL); auto buttonType = layoutProperty->GetType().value_or(ButtonType::CAPSULE); json->PutExtAttr("type", host->GetTag() == "Toggle" ? "ToggleType.Button" : ConvertButtonTypeToString(buttonType).c_str(), filter); @@ -142,8 +152,9 @@ public: .c_str(), filter); json->PutExtAttr("fontWeight", V2::ConvertWrapFontWeightToStirng( layoutProperty->GetFontWeight().value_or(FontWeight::MEDIUM)).c_str(), filter); + Color themeFontColor = buttonTheme->GetTextColor(buttonStyle, buttonRole); json->PutExtAttr("fontColor", layoutProperty->GetFontColor() - .value_or(layoutProperty->HasLabel() ? textStyle.GetTextColor() : Color::BLACK) + .value_or(layoutProperty->HasLabel() ? themeFontColor : Color::BLACK) .ColorToString() .c_str(), filter); json->PutExtAttr("fontStyle", @@ -357,6 +368,8 @@ public: return hasCustomPadding_; } + bool OnThemeScopeUpdate(int32_t themeScopeId) override; + protected: bool IsNeedInitClickEventRecorder() const override { @@ -392,8 +405,6 @@ private: void OnFontScaleConfigurationUpdate() override; Color backgroundColor_; Color focusBorderColor_; - Color themeBgColor_; - Color themeTextColor_; bool isSetClickedColor_ = false; ComponentButtonType buttonType_ = ComponentButtonType::BUTTON; void FireBuilder(); @@ -408,6 +419,27 @@ private: bool isHover_ = false; bool isPress_ = false; + bool GetButtonFontColor() + { + auto host = GetHost(); + CHECK_NULL_RETURN(host, false); + auto buttonLayoutProperty = host->GetLayoutProperty(); + CHECK_NULL_RETURN(buttonLayoutProperty, false); + return buttonLayoutProperty->HasFontColor(); + } + + bool GetButtonBackgroundColor() + { + auto host = GetHost(); + CHECK_NULL_RETURN(host, false); + auto buttonLayoutProperty = host->GetLayoutProperty(); + CHECK_NULL_RETURN(buttonLayoutProperty, false); + return buttonLayoutProperty->HasBackgroundColor(); + } + + bool isFontColor_; + bool isBackgroundColor_; + bool isInHover_ = false; Offset localLocation_; Dimension focusBorderWidth_; diff --git a/frameworks/core/components_ng/pattern/button/button_theme_wrapper.h b/frameworks/core/components_ng/pattern/button/button_theme_wrapper.h new file mode 100644 index 0000000000..43cf9cf2e7 --- /dev/null +++ b/frameworks/core/components_ng/pattern/button/button_theme_wrapper.h @@ -0,0 +1,110 @@ +/* + * 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_BUTTON_BUTTON_THEME_WRAPPER_H +#define FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_BUTTON_BUTTON_THEME_WRAPPER_H + +#include "core/components/button/button_theme.h" +#include "core/components_ng/token_theme/token_theme_wrapper.h" + +namespace OHOS::Ace::NG { + +class ButtonThemeWrapper : public ButtonTheme, public TokenThemeWrapper +{ + DECLARE_ACE_TYPE(ButtonThemeWrapper, ButtonTheme); + +public: + class WrapperBuilder : public Builder + { + public: + WrapperBuilder() = default; + ~WrapperBuilder() = default; + + RefPtr BuildWrapper(const RefPtr& themeConstants) const + { + auto themeWrapper = AceType::Claim(new ButtonThemeWrapper()); + auto theme = AceType::DynamicCast(themeWrapper); + if (!themeConstants) { + return themeWrapper; + } + ParsePattern(themeConstants, theme); + return themeWrapper; + } + }; + + ~ButtonThemeWrapper() override = default; + + void ApplyTokenTheme(const TokenTheme& theme) override + { + if (auto colors = theme.Colors(); colors) { + warningColor_ = colors->Warning(); + fontEmphasizeColor_ = colors->FontEmphasize(); + fontOnPrimaryColor_ = colors->FontOnPrimary(); + backgroundEmphasizeColor_ = colors->BackgroundEmphasize(); + compBackgroundTertiaryColor_ = colors->CompBackgroundTertiary(); + + textStyle_.SetTextColor(fontOnPrimaryColor_); + } + } + + const Color& GetBgColor() const override + { + return backgroundEmphasizeColor_; + } + + Color GetBgColor(ButtonStyleMode buttonStyle, ButtonRole buttonRole) const override + { + switch (buttonStyle) { + case ButtonStyleMode::TEXT: + return Color::TRANSPARENT; + case ButtonStyleMode::NORMAL: + return compBackgroundTertiaryColor_; + case ButtonStyleMode::EMPHASIZE: + default: + if (buttonRole == ButtonRole::ERROR) { + return warningColor_; + } + return backgroundEmphasizeColor_; + } + } + + const Color& GetTextColor(ButtonStyleMode buttonStyle, ButtonRole buttonRole) const override + { + switch (buttonStyle) { + case ButtonStyleMode::NORMAL: + case ButtonStyleMode::TEXT: + if (buttonRole == ButtonRole::ERROR) { + return warningColor_; + } + return fontEmphasizeColor_; + case ButtonStyleMode::EMPHASIZE: + default: + return fontOnPrimaryColor_; + } + } + +protected: + ButtonThemeWrapper() = default; + +private: + Color warningColor_; + Color fontEmphasizeColor_; + Color fontOnPrimaryColor_; + Color backgroundEmphasizeColor_; + Color compBackgroundTertiaryColor_; +}; + +} // namespace OHOS::Ace::NG +#endif // FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_BUTTON_BUTTON_THEME_WRAPPER_H \ No newline at end of file diff --git a/frameworks/core/components_ng/pattern/button/toggle_button_pattern.cpp b/frameworks/core/components_ng/pattern/button/toggle_button_pattern.cpp index 086b3ad7ca..6995407b88 100644 --- a/frameworks/core/components_ng/pattern/button/toggle_button_pattern.cpp +++ b/frameworks/core/components_ng/pattern/button/toggle_button_pattern.cpp @@ -47,7 +47,7 @@ void ToggleButtonPattern::InitParameters() textFontSize_ = toggleTheme->GetTextFontSize(); textColor_ = toggleTheme->GetTextColor(); disabledAlpha_ = toggleTheme->GetDisabledAlpha(); - auto buttonTheme = pipeline->GetTheme(); + auto buttonTheme = pipeline->GetTheme(GetThemeScopeId()); CHECK_NULL_VOID(buttonTheme); clickedColor_ = buttonTheme->GetClickedColor(); } -- Gitee From ce79a130858d0f010e0229063e7d2a699ae8e9c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E9=A3=9E?= Date: Fri, 6 Dec 2024 08:15:33 +0000 Subject: [PATCH 2/3] update frameworks/core/components_ng/pattern/button/button_pattern.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡飞 --- frameworks/core/components_ng/pattern/button/button_pattern.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frameworks/core/components_ng/pattern/button/button_pattern.cpp b/frameworks/core/components_ng/pattern/button/button_pattern.cpp index 58a21d4292..ce9991092a 100644 --- a/frameworks/core/components_ng/pattern/button/button_pattern.cpp +++ b/frameworks/core/components_ng/pattern/button/button_pattern.cpp @@ -436,6 +436,8 @@ void ButtonPattern::HandleBackgroundColor() if (!renderContext->HasBackgroundColor()) { renderContext->UpdateBackgroundColor(buttonTheme->GetBgColor(buttonStyle, buttonRole)); } + themeBgColor_ = buttonTheme->GetBgColor(buttonStyle, buttonRole); + themeTextColor_ = buttonTheme->GetTextColor(buttonStyle, buttonRole); } void ButtonPattern::HandleEnabled() -- Gitee From 0a101dce843234df2973cafedc2489be081bc081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E9=A3=9E?= Date: Fri, 6 Dec 2024 08:16:31 +0000 Subject: [PATCH 3/3] update frameworks/core/components_ng/pattern/button/button_pattern.h. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡飞 --- frameworks/core/components_ng/pattern/button/button_pattern.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frameworks/core/components_ng/pattern/button/button_pattern.h b/frameworks/core/components_ng/pattern/button/button_pattern.h index 6b42a547b2..07b238c1ad 100644 --- a/frameworks/core/components_ng/pattern/button/button_pattern.h +++ b/frameworks/core/components_ng/pattern/button/button_pattern.h @@ -405,6 +405,8 @@ private: void OnFontScaleConfigurationUpdate() override; Color backgroundColor_; Color focusBorderColor_; + Color themeBgColor_; + Color themeTextColor_; bool isSetClickedColor_ = false; ComponentButtonType buttonType_ = ComponentButtonType::BUTTON; void FireBuilder(); -- Gitee