From ffee5e166c5697bddf229588e1e7ab0ba43dd304 Mon Sep 17 00:00:00 2001 From: mlyuestc Date: Wed, 10 Sep 2025 09:49:06 +0000 Subject: [PATCH] fix colormode bug Signed-off-by: mlyuestc --- adapter/ohos/entrance/ace_container.cpp | 1 + .../core/components/theme/theme_manager.h | 2 + .../components/theme/theme_manager_impl.cpp | 59 +++++++++++++------ .../components/theme/theme_manager_impl.h | 8 ++- .../core/manager/theme_manager_test_ng.cpp | 32 ++++++++++ 5 files changed, 84 insertions(+), 18 deletions(-) diff --git a/adapter/ohos/entrance/ace_container.cpp b/adapter/ohos/entrance/ace_container.cpp index 81edbceebcf..67b80a60d5c 100644 --- a/adapter/ohos/entrance/ace_container.cpp +++ b/adapter/ohos/entrance/ace_container.cpp @@ -2917,6 +2917,7 @@ void AceContainer::AttachView(std::shared_ptr window, const RefPtrGetWindow()->GetRSUIDirector(); + CHECK_NULL_VOID(rsUiDirector); auto rsUiContext = rsUiDirector->GetRSUIContext(); if (rsUiDirector && rsUiContext) { rsUiContext->GetRSTransaction()->ExecuteSynchronousTask(syncTask); diff --git a/frameworks/core/components/theme/theme_manager.h b/frameworks/core/components/theme/theme_manager.h index f07bfe1b3da..8f1db5cf0ba 100644 --- a/frameworks/core/components/theme/theme_manager.h +++ b/frameworks/core/components/theme/theme_manager.h @@ -63,6 +63,8 @@ public: virtual void LoadResourceThemes() {} + virtual void SetInstanceId(int32_t instanceId) {} + template RefPtr GetTheme() { diff --git a/frameworks/core/components/theme/theme_manager_impl.cpp b/frameworks/core/components/theme/theme_manager_impl.cpp index 0a28469716a..ab6af8b71c5 100644 --- a/frameworks/core/components/theme/theme_manager_impl.cpp +++ b/frameworks/core/components/theme/theme_manager_impl.cpp @@ -226,9 +226,10 @@ ThemeManagerImpl::ThemeManagerImpl(RefPtr& resourceAdapter) void ThemeManagerImpl::RegisterThemeKit(ThemeType type, Ace::Kit::BuildFunc func) { - auto key = GetThemesMapKey(type); - auto findIter = themes_.find(key); - if (findIter != themes_.end()) { + auto colorMode = GetCurrentColorMode(); + auto& themes = CurrentColorModeThemes(colorMode); + auto findIter = themes.find(type); + if (findIter != themes.end()) { return; } THEME_BUILDERS_KIT.insert({ type, func }); @@ -245,9 +246,10 @@ std::string ThemeManagerImpl::GetThemesMapKey(ThemeType type) const RefPtr ThemeManagerImpl::GetTheme(ThemeType type) { - auto key = GetThemesMapKey(type); - auto findIter = themes_.find(key); - if (findIter != themes_.end()) { + auto colorMode = GetCurrentColorMode(); + auto& themes = CurrentColorModeThemes(colorMode); + auto findIter = themes.find(type); + if (findIter != themes.end()) { return findIter->second; } @@ -264,8 +266,9 @@ RefPtr ThemeManagerImpl::GetThemeOrigin(ThemeType type) } auto theme = builderIter->second(themeConstants_); - auto key = GetThemesMapKey(type); - themes_.emplace(key, theme); + auto colorMode = GetCurrentColorMode(); + auto& themes = CurrentColorModeThemes(colorMode); + themes.emplace(type, theme); return theme; } @@ -275,7 +278,8 @@ RefPtr ThemeManagerImpl::GetThemeKit(ThemeType type) if (builderIterKit == THEME_BUILDERS_KIT.end()) { return nullptr; } - auto key = GetThemesMapKey(type); + auto colorMode = GetCurrentColorMode(); + auto& themes = CurrentColorModeThemes(colorMode); if (auto pipeline = NG::PipelineContext::GetCurrentContext(); pipeline) { ColorMode localMode = pipeline->GetLocalColorMode(); ColorMode systemMode = pipeline->GetColorMode(); @@ -293,12 +297,12 @@ RefPtr ThemeManagerImpl::GetThemeKit(ThemeType type) ResourceManager::GetInstance().UpdateColorMode( pipeline->GetBundleName(), pipeline->GetModuleName(), pipeline->GetInstanceId(), localMode); } - themes_.emplace(key, theme); + themes.emplace(type, theme); return theme; } auto theme = builderIterKit->second(); - themes_.emplace(key, theme); + themes.emplace(type, theme); return theme; } @@ -411,9 +415,13 @@ RefPtr ThemeManagerImpl::GetThemeKit(ThemeType type, int32_t themeScopeId Color ThemeManagerImpl::GetBackgroundColor() const { - auto key = GetThemesMapKey(AppTheme::TypeId()); - auto findIter = themes_.find(key); - if (findIter != themes_.end()) { + auto colorMode = GetCurrentColorMode(); + auto themes = lightThemes_; + if (colorMode == ColorMode::DARK) { + themes = darkThemes_; + } + auto findIter = themes.find(AppTheme::TypeId()); + if (findIter != themes.end()) { auto appTheme = AceType::DynamicCast(findIter->second); if (appTheme) { return appTheme->GetBackgroundColor(); @@ -436,7 +444,8 @@ Color ThemeManagerImpl::GetBackgroundColor() const void ThemeManagerImpl::LoadResourceThemes() { - themes_.clear(); + darkThemes_.clear(); + lightThemes_.clear(); themeWrappersLight_.clear(); themeWrappersDark_.clear(); themeConstants_->LoadTheme(currentThemeId_); @@ -447,12 +456,28 @@ ThemeManagerImpl::ThemeWrappers& ThemeManagerImpl::GetThemeWrappers(ColorMode mo return mode == ColorMode::DARK ? themeWrappersDark_ : themeWrappersLight_; } +void ThemeManagerImpl::SetInstanceId(int32_t instanceId) +{ + instanceId_ = instanceId; +} + ColorMode ThemeManagerImpl::GetCurrentColorMode() const { - auto pipelineContext = NG::PipelineContext::GetCurrentContext(); - ColorMode systemMode = Container::CurrentColorMode(); + auto container = Container::GetContainer(instanceId_); + CHECK_NULL_RETURN(container, ColorMode::LIGHT); + auto pipelineBase = container->GetPipelineContext(); + auto pipelineContext = DynamicCast(pipelineBase); + ColorMode systemMode = container->GetColorMode(); CHECK_NULL_RETURN(pipelineContext, systemMode); ColorMode localMode = pipelineContext->GetLocalColorMode(); return localMode == ColorMode::COLOR_MODE_UNDEFINED ? systemMode : localMode; } + +ThemeManagerImpl::ColorModeThemes& ThemeManagerImpl::CurrentColorModeThemes(ColorMode mode) +{ + if (mode == ColorMode::DARK) { + return darkThemes_; + } + return lightThemes_; +} } // namespace OHOS::Ace diff --git a/frameworks/core/components/theme/theme_manager_impl.h b/frameworks/core/components/theme/theme_manager_impl.h index 0b0cf9e6e4a..2def923f313 100644 --- a/frameworks/core/components/theme/theme_manager_impl.h +++ b/frameworks/core/components/theme/theme_manager_impl.h @@ -137,9 +137,13 @@ public: std::string GetThemesMapKey(ThemeType type) const; + void SetInstanceId(int32_t instanceId) override; + private: using ThemeWrappers = std::unordered_map>; - std::unordered_map> themes_; + using ColorModeThemes = std::unordered_map>; + ColorModeThemes darkThemes_; + ColorModeThemes lightThemes_; ThemeWrappers themeWrappersLight_; ThemeWrappers themeWrappersDark_; @@ -150,6 +154,8 @@ private: ThemeWrappers& GetThemeWrappers(ColorMode mode); ColorMode GetCurrentColorMode() const; + ColorModeThemes& CurrentColorModeThemes(ColorMode mode); + int32_t instanceId_ = 0; }; } // namespace OHOS::Ace #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_THEME_THEME_MANAGER_H diff --git a/test/unittest/core/manager/theme_manager_test_ng.cpp b/test/unittest/core/manager/theme_manager_test_ng.cpp index 46d82259de1..876bd6ccd1c 100644 --- a/test/unittest/core/manager/theme_manager_test_ng.cpp +++ b/test/unittest/core/manager/theme_manager_test_ng.cpp @@ -77,4 +77,36 @@ HWTEST_F(ThemeManagerTestNg, GetThemesMapKey001, TestSize.Level1) themeManager->GetThemesMapKey(ButtonTheme::TypeId()); EXPECT_TRUE(AceType::InstanceOf(theme)); } + +/** + * @tc.name: CurrentColorModeThemes001 + * @tc.desc: CurrentColorModeThemes_LIGHT + * @tc.type: FUNC + */ +HWTEST_F(ThemeManagerTestNg, CurrentColorModeThemes001, TestSize.Level1) +{ + /** + * @tc.steps: create themeManager + */ + auto themeManager = AceType::MakeRefPtr(); + auto theme = themeManager->GetThemeOrigin(ButtonTheme::TypeId()); + themeManager->CurrentColorModeThemes(ColorMode::LIGHT); + EXPECT_TRUE(AceType::InstanceOf(theme)); +} + +/** + * @tc.name: CurrentColorModeThemes002 + * @tc.desc: CurrentColorModeThemes_DARK + * @tc.type: FUNC + */ +HWTEST_F(ThemeManagerTestNg, CurrentColorModeThemes002, TestSize.Level1) +{ + /** + * @tc.steps: create themeManager + */ + auto themeManager = AceType::MakeRefPtr(); + auto theme = themeManager->GetThemeOrigin(ButtonTheme::TypeId()); + themeManager->CurrentColorModeThemes(ColorMode::DARK); + EXPECT_TRUE(AceType::InstanceOf(theme)); +} } -- Gitee