1 /* 2 * Copyright (c) 2024 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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_THEME_JS_BUTTON_THEME_H 17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_THEME_JS_BUTTON_THEME_H 18 19 #include "bridge/declarative_frontend/ark_theme/theme_apply/js_theme_utils.h" 20 #include "core/components_ng/base/view_stack_model.h" 21 #include "core/components_ng/pattern/button/button_model.h" 22 23 namespace OHOS::Ace::Framework { 24 class JSButtonTheme { 25 public: ApplyTheme(const ButtonRole & role,const ButtonStyleMode & styleMode,bool isLabelButton)26 static bool ApplyTheme(const ButtonRole& role, const ButtonStyleMode& styleMode, bool isLabelButton) 27 { 28 JSButtonTheme::buttonRole = role; 29 JSButtonTheme::buttonStyleMode = styleMode; 30 return JSButtonTheme::ApplyTheme(isLabelButton); 31 } ApplyTheme(const ButtonRole & role,bool isLabelButton)32 static bool ApplyTheme(const ButtonRole& role, bool isLabelButton) 33 { 34 JSButtonTheme::buttonRole = role; 35 return JSButtonTheme::ApplyTheme(isLabelButton); 36 } ApplyTheme(const ButtonStyleMode & styleMode,bool isLabelButton)37 static bool ApplyTheme(const ButtonStyleMode& styleMode, bool isLabelButton) 38 { 39 JSButtonTheme::buttonStyleMode = styleMode; 40 return JSButtonTheme::ApplyTheme(isLabelButton); 41 } 42 private: 43 // last button role value 44 inline static ButtonRole buttonRole = ButtonRole::NORMAL; 45 // last button style mode value 46 inline static ButtonStyleMode buttonStyleMode = ButtonStyleMode::EMPHASIZE; 47 ApplyTheme(bool isLabelButton)48 static bool ApplyTheme(bool isLabelButton) 49 { 50 auto themeColors = JSThemeUtils::GetThemeColors(); 51 if (!themeColors) { 52 // no need to apply custom theme colors 53 return false; 54 } 55 56 // normal 57 ViewStackModel::GetInstance()->SetVisualState(VisualState::NORMAL); 58 if (isLabelButton) { 59 ButtonModel::GetInstance()->SetFontColor(JSButtonTheme::FontColor(themeColors)); 60 } 61 ButtonModel::GetInstance()->BackgroundColor(JSButtonTheme::BackgroundColor(themeColors, false), true); 62 63 // clear state 64 ViewStackModel::GetInstance()->ClearVisualState(); 65 return true; 66 } 67 FontColor(const std::optional<JSThemeColors> & themeColors)68 static Color FontColor(const std::optional<JSThemeColors>& themeColors) 69 { 70 switch (JSButtonTheme::buttonStyleMode) { 71 case ButtonStyleMode::NORMAL: 72 case ButtonStyleMode::TEXT: 73 if (JSButtonTheme::buttonRole == ButtonRole::ERROR) { 74 return themeColors->Warning(); 75 } 76 return themeColors->FontEmphasize(); 77 case ButtonStyleMode::EMPHASIZE: 78 default: 79 return themeColors->FontOnPrimary(); 80 } 81 } 82 BackgroundColor(const std::optional<JSThemeColors> & themeColors,bool isPressed)83 static Color BackgroundColor(const std::optional<JSThemeColors>& themeColors, bool isPressed) 84 { 85 switch (JSButtonTheme::buttonStyleMode) { 86 case ButtonStyleMode::TEXT: 87 return Color::TRANSPARENT; 88 case ButtonStyleMode::NORMAL: 89 return themeColors->CompBackgroundTertiary(); 90 case ButtonStyleMode::EMPHASIZE: 91 default: 92 if (JSButtonTheme::buttonRole == ButtonRole::ERROR) { 93 return themeColors->Warning(); 94 } 95 return themeColors->BackgroundEmphasize(); 96 } 97 } 98 }; 99 } // namespace OHOS::Ace::Framework 100 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_THEME_JS_BUTTON_THEME_H 101