diff --git a/adapter/ohos/entrance/ui_content_impl.cpp b/adapter/ohos/entrance/ui_content_impl.cpp index 379b93f01b0a1179bf22cdc9f132a9207e3fcb08..62b8b63018b7d0e58524fd03dc793d590f68d1f5 100644 --- a/adapter/ohos/entrance/ui_content_impl.cpp +++ b/adapter/ohos/entrance/ui_content_impl.cpp @@ -3220,4 +3220,14 @@ void UIContentImpl::SetContentNodeGrayScale(float grayscale) CHECK_NULL_VOID(renderContext); renderContext->UpdateFrontGrayScale(Dimension(grayscale)); } + +void UIContentImpl::SetStatusBarItemColor(uint32_t color) +{ + ContainerScope scope(instanceId_); + auto container = Platform::AceContainer::GetContainer(instanceId_); + CHECK_NULL_VOID(container); + auto appBar = container->GetAppBar(); + CHECK_NULL_VOID(appBar); + appBar->SetStatusBarItemColor(IsDarkColor(color)); +} } // namespace OHOS::Ace diff --git a/adapter/ohos/entrance/ui_content_impl.h b/adapter/ohos/entrance/ui_content_impl.h index 6f1be07b891347d67064ec333424dbc0f4d1f033..97041b61165921cc3d3f39631059ead79187c84f 100644 --- a/adapter/ohos/entrance/ui_content_impl.h +++ b/adapter/ohos/entrance/ui_content_impl.h @@ -313,6 +313,8 @@ public: void SetContentNodeGrayScale(float grayscale) override; + void SetStatusBarItemColor(uint32_t color) override; + private: UIContentErrorCode InitializeInner( OHOS::Rosen::Window* window, const std::string& contentInfo, napi_value storage, bool isNamedRouter); diff --git a/adapter/preview/entrance/ui_content_impl.cpp b/adapter/preview/entrance/ui_content_impl.cpp index 428c50210b37452f26d65332b8ac21295e3166bb..c8bfd2d3c78a8f7bcac61c001ee533e30e589c99 100644 --- a/adapter/preview/entrance/ui_content_impl.cpp +++ b/adapter/preview/entrance/ui_content_impl.cpp @@ -575,4 +575,14 @@ int32_t UIContentImpl::CreateModalUIExtension( } void UIContentImpl::CloseModalUIExtension(int32_t sessionId) {} + +void UIContentImpl::SetStatusBarItemColor(uint32_t color) +{ + ContainerScope scope(instanceId_); + auto container = Platform::AceContainer::GetContainer(instanceId_); + CHECK_NULL_VOID(container); + auto appBar = container->GetAppBar(); + CHECK_NULL_VOID(appBar); + appBar->SetStatusBarItemColor(IsDarkColor(color)); +} } // namespace OHOS::Ace diff --git a/adapter/preview/entrance/ui_content_impl.h b/adapter/preview/entrance/ui_content_impl.h index 8de90cc6e14d76ee1216383a9491e94882b0ce9d..58d391fc2640ecd74a46a08f2add710567e2afc5 100644 --- a/adapter/preview/entrance/ui_content_impl.h +++ b/adapter/preview/entrance/ui_content_impl.h @@ -145,6 +145,8 @@ public: void SetContentNodeGrayScale(float grayscale) override {}; + void SetStatusBarItemColor(uint32_t color) override; + private: UIContentErrorCode CommonInitialize(OHOS::Rosen::Window* window, const std::string& contentInfo, napi_value storage); diff --git a/frameworks/base/utils/utils.h b/frameworks/base/utils/utils.h index da903c65977f2c6399d48f173574920a0d927100..7c4e576c075842804688f980e76ad209ffccbdcd 100644 --- a/frameworks/base/utils/utils.h +++ b/frameworks/base/utils/utils.h @@ -278,6 +278,16 @@ inline float CalculateFriction(float gamma) return SCROLL_RATIO * static_cast(std::pow(1.0 - gamma, 2)); } +inline bool IsDarkColor(uint32_t color) +{ + constexpr int lightThresholds = 128; + int r = (color >> 16) & 0xFF; + int g = (color >> 8) & 0xFF; + int b = color & 0xFF; + int gray = (r * 299 + g * 587 + b * 114) / 1000; + return gray < lightThresholds; +} + bool RealPath(const std::string& fileName, char* realPath); } // namespace OHOS::Ace diff --git a/frameworks/core/components_ng/pattern/app_bar/app_bar_theme.h b/frameworks/core/components_ng/pattern/app_bar/app_bar_theme.h index aa83fb153b80433c32c20353316f353bbcaf7e37..22151fbe02df227118709ac3375bd5f14b91f1a9 100644 --- a/frameworks/core/components_ng/pattern/app_bar/app_bar_theme.h +++ b/frameworks/core/components_ng/pattern/app_bar/app_bar_theme.h @@ -104,9 +104,14 @@ public: return std::nullopt; } - const Color& GetIconColor() const + const Color& GetIconColorLight() const { - return iconColor_; + return iconColorLight_; + } + + const Color& GetIconColorDark() const + { + return iconColorDark_; } const Color& GetDividerColor() const @@ -124,15 +129,26 @@ public: return hoverColor_; } - const Color& GetClickEffectColor() const + const Color& GetClickEffectColorLight() const + { + return clickEffectColorLight_; + } + + const Color& GetClickEffectColorDark() const + { + return clickEffectColorDark_; + } + + const Color& GetFocusedOutlineColorLight() const { - return clickEffectColor_; + return focusedOutlineColorLight_; } - const Color& GetFocusedOutlineColor() const + const Color& GetFocusedOutlineColorDark() const { - return focusedOutlineColor_; + return focusedOutlineColorDark_; } + const Color& GetBlurColorLight() const { return blurColorLight_; @@ -266,24 +282,9 @@ private: theme->iconSize_ = appBarPattern->GetAttr("icon_size", 0.0_vp); theme->iconCornerRadius_ = appBarPattern->GetAttr("icon_corner_radius", 0.0_vp); theme->appBarFontSize_ = appBarPattern->GetAttr("app_bar_font_size", 0.0_vp); - theme->textColorPrimary_ = appBarPattern->GetAttr("text_color_primary", Color()); - theme->appBarBgColor_ = appBarPattern->GetAttr("app_bar_bg_color", Color()); theme->bundleName_ = appBarPattern->GetAttr("app_bar_bundle_name", "com.ohos.hag.famanager"); theme->abilityName_ = appBarPattern->GetAttr("app_bar_ability_name", "FaPanelAbility"); theme->stageAbilityName_ = appBarPattern->GetAttr("app_bar_stage_ability_name", "PanelAbility"); - - theme->iconColor_ = appBarPattern->GetAttr("icon_color", Color()); - theme->dividerColor_ = appBarPattern->GetAttr("divider_color", Color()); - theme->borderColor_ = appBarPattern->GetAttr("border_color", Color()); - theme->hoverColor_ = appBarPattern->GetAttr("hover_color", Color()); - theme->clickEffectColor_ = appBarPattern->GetAttr("click_effect_color", Color()); - theme->focusedOutlineColor_ = appBarPattern->GetAttr("focused_outline_color", Color()); - theme->blurColorLight_ = appBarPattern->GetAttr("blur_color_light", Color(0x99FFFFFF)); - theme->blurColorDark_ = appBarPattern->GetAttr("blur_color_dark", Color(0x99000000)); - theme->dividerColorLight_ = appBarPattern->GetAttr("divider_color_light", Color(0x33000000)); - theme->dividerColorDark_ = appBarPattern->GetAttr("divider_color_dark", Color(0x33FFFFFF)); - theme->borderColorLight_ = appBarPattern->GetAttr("border_color_light", Color(0x33182431)); - theme->borderColorDark_ = appBarPattern->GetAttr("border_color_dark", Color(0x4DFFFFFF)); theme->bentRadius_ = appBarPattern->GetAttr("bent_radius", 18.0_vp); theme->rightAngle_ = appBarPattern->GetAttr("right_angle", 0.0_vp); theme->menuBarWidth_ = appBarPattern->GetAttr("menu_bar_width", 80.0_vp); @@ -304,6 +305,29 @@ private: theme->blurRadius_ = appBarPattern->GetAttr("blur_radius", 5.0_vp); } + static void initColorParameters(const RefPtr& theme, const RefPtr& appBarPattern) + { + theme->textColorPrimary_ = appBarPattern->GetAttr("text_color_primary", Color()); + theme->appBarBgColor_ = appBarPattern->GetAttr("app_bar_bg_color", Color()); + theme->iconColorLight_ = appBarPattern->GetAttr("icon_color_light", Color(0xff182431)); + theme->iconColorDark_ = appBarPattern->GetAttr("icon_color_dark", Color(0xdbffffff)); + theme->dividerColor_ = appBarPattern->GetAttr("divider_color", Color()); + theme->borderColor_ = appBarPattern->GetAttr("border_color", Color()); + theme->hoverColor_ = appBarPattern->GetAttr("hover_color", Color()); + theme->clickEffectColorLight_ = appBarPattern->GetAttr("click_effect_color_light", Color(0x19182431)); + theme->clickEffectColorDark_ = appBarPattern->GetAttr("click_effect_color_Dark", Color(0x26ffffff)); + theme->focusedOutlineColorLight_ = appBarPattern->GetAttr("focused_outline_color_light", + Color(0xff007dff)); + theme->focusedOutlineColorDark_ = appBarPattern->GetAttr("focused_outline_color_dark", + Color(0xff3f97e9)); + theme->blurColorLight_ = appBarPattern->GetAttr("blur_color_light", Color(0x99FFFFFF)); + theme->blurColorDark_ = appBarPattern->GetAttr("blur_color_dark", Color(0x99000000)); + theme->dividerColorLight_ = appBarPattern->GetAttr("divider_color_light", Color(0x33000000)); + theme->dividerColorDark_ = appBarPattern->GetAttr("divider_color_dark", Color(0x33FFFFFF)); + theme->borderColorLight_ = appBarPattern->GetAttr("border_color_light", Color(0x33182431)); + theme->borderColorDark_ = appBarPattern->GetAttr("border_color_dark", Color(0x4DFFFFFF)); + } + Dimension appBarHeight_; Dimension iconSize_; Dimension iconCornerRadius_; @@ -314,12 +338,15 @@ private: std::string abilityName_; std::string stageAbilityName_; - Color iconColor_; + Color iconColorLight_; + Color iconColorDark_; Color dividerColor_; Color borderColor_; Color hoverColor_; - Color clickEffectColor_; - Color focusedOutlineColor_; + Color clickEffectColorLight_; + Color clickEffectColorDark_; + Color focusedOutlineColorLight_; + Color focusedOutlineColorDark_; Color blurColorLight_; Color blurColorDark_; Color dividerColorLight_; diff --git a/frameworks/core/components_ng/pattern/app_bar/app_bar_view.cpp b/frameworks/core/components_ng/pattern/app_bar/app_bar_view.cpp index 29d3dac9c08a916e85769cc5d9c124681b4a1564..2a21f8e69f013390763329b5c1a34761ec3ffd55 100644 --- a/frameworks/core/components_ng/pattern/app_bar/app_bar_view.cpp +++ b/frameworks/core/components_ng/pattern/app_bar/app_bar_view.cpp @@ -68,7 +68,7 @@ RefPtr AppBarView::Create(const RefPtr& stage) atomicService_ = atom; auto pattern = atom->GetPattern(); CHECK_NULL_RETURN(pattern, nullptr); - pattern->UpdateColor(); + pattern->UpdateColor(std::nullopt); pattern->UpdateLayout(); return atom; } @@ -378,4 +378,17 @@ std::optional AppBarView::GetAppBarRect() offset.AddY(manager->GetSystemSafeArea().top_.Length()); return RectF(offset, size); } + +void AppBarView::SetStatusBarItemColor(bool isLight) +{ + auto atom = atomicService_.Upgrade(); + CHECK_NULL_VOID(atom); + auto pattern = atom->GetPattern(); + CHECK_NULL_VOID(pattern); + auto theme = GetAppBarTheme(); + auto menuBar = pattern->GetMenuBar(); + pattern->settedColorMode = isLight; + pattern->UpdateMenuBarColor(theme, menuBar, isLight); + pattern->UpdateColor(isLight); +} } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/pattern/app_bar/app_bar_view.h b/frameworks/core/components_ng/pattern/app_bar/app_bar_view.h index 5f71e2d04ff6d1ca770d8063bacb75f06f68c49f..616621e7a02bc846c7cec6ef38535764c90278ab 100644 --- a/frameworks/core/components_ng/pattern/app_bar/app_bar_view.h +++ b/frameworks/core/components_ng/pattern/app_bar/app_bar_view.h @@ -43,6 +43,7 @@ public: void SetContent(const std::string& content) {} void SetFontStyle(Ace::FontStyle fontStyle) {} void SetIconColor(const std::optional& color) {} + void SetStatusBarItemColor(bool isLight); std::optional GetAppBarRect(); private: diff --git a/frameworks/core/components_ng/pattern/app_bar/atomic_service_pattern.cpp b/frameworks/core/components_ng/pattern/app_bar/atomic_service_pattern.cpp index bfaea67e8eef6717eb732120a099e3030075dc5a..9e73b2b5a4593b5bb6815ae28a552b68d4fefd83 100644 --- a/frameworks/core/components_ng/pattern/app_bar/atomic_service_pattern.cpp +++ b/frameworks/core/components_ng/pattern/app_bar/atomic_service_pattern.cpp @@ -56,7 +56,11 @@ void AtomicServicePattern::BeforeCreateLayoutWrapper() renderContext->UpdatePosition(OffsetT(0.0_vp, Dimension(topMargin, DimensionUnit::PX))); auto menuBar = GetMenuBar(); - UpdateMenuBarColor(theme, menuBar, SystemProperties::GetColorMode() != ColorMode::DARK); + if (settedColorMode.has_value()) { + UpdateMenuBarColor(theme, menuBar, settedColorMode.value()); + } else { + UpdateMenuBarColor(theme, menuBar, SystemProperties::GetColorMode() != ColorMode::DARK); + } menuBarRow->MarkModifyDone(); menuBarRow->MarkDirtyNode(); @@ -84,7 +88,11 @@ void AtomicServicePattern::OnColorConfigurationUpdate() auto pipeline = PipelineContext::GetCurrentContext(); CHECK_NULL_VOID(pipeline); host->GetRenderContext()->UpdateBackgroundColor(pipeline->GetAppBgColor()); - UpdateColor(); + if (settedColorMode.has_value()) { + UpdateColor(settedColorMode); + } else { + UpdateColor(SystemProperties::GetColorMode() != ColorMode::DARK); + } } RefPtr AtomicServicePattern::GetMenuBarRow() @@ -143,24 +151,25 @@ RefPtr AtomicServicePattern::GetCloseIcon() return closeIcon; } -void AtomicServicePattern::UpdateColor() +void AtomicServicePattern::UpdateColor(std::optional isLight) { auto pipeline = PipelineContext::GetCurrentContext(); CHECK_NULL_VOID(pipeline); auto theme = pipeline->GetTheme(); - auto isLight = SystemProperties::GetColorMode() != ColorMode::DARK; - + if (!(isLight.has_value())) { + isLight = SystemProperties::GetColorMode() != ColorMode::DARK; + } auto menuButton = GetMenuButton(); - UpdateButtonColor(theme, menuButton); + UpdateButtonColor(theme, menuButton, isLight.value()); auto divider = GetDivider(); - UpdateDividerColor(theme, divider, isLight); + UpdateDividerColor(theme, divider, isLight.value()); auto closeButton = GetCloseButton(); - UpdateButtonColor(theme, closeButton); + UpdateButtonColor(theme, closeButton, isLight.value()); auto menuIcon = GetMenuIcon(); - UpdateIconColor(theme, menuIcon); + UpdateIconColor(theme, menuIcon, isLight.value()); auto closeIcon = GetCloseIcon(); - UpdateIconColor(theme, closeIcon); + UpdateIconColor(theme, closeIcon, isLight.value()); } void AtomicServicePattern::UpdateMenuBarColor(RefPtr& theme, RefPtr& menuBar, bool isLight) @@ -186,16 +195,24 @@ void AtomicServicePattern::UpdateMenuBarColor(RefPtr& theme, RefPtr menuBar->MarkDirtyNode(PROPERTY_UPDATE_LAYOUT); } -void AtomicServicePattern::UpdateButtonColor(RefPtr& theme, RefPtr& button) +void AtomicServicePattern::UpdateButtonColor(RefPtr& theme, RefPtr& button, bool isLight) { CHECK_NULL_VOID(theme); CHECK_NULL_VOID(button); // pressed color auto buttonPattern = button->GetPattern(); CHECK_NULL_VOID(buttonPattern); - buttonPattern->SetClickedColor(theme->GetClickEffectColor()); + if (isLight) { + buttonPattern->SetClickedColor(theme->GetClickEffectColorLight()); + } else { + buttonPattern->SetClickedColor(theme->GetClickEffectColorDark()); + } // focus border color - buttonPattern->SetFocusBorderColor(theme->GetFocusedOutlineColor()); + if (isLight) { + buttonPattern->SetFocusBorderColor(theme->GetFocusedOutlineColorLight()); + } else { + buttonPattern->SetFocusBorderColor(theme->GetFocusedOutlineColorDark()); + } button->MarkModifyDone(); button->MarkDirtyNode(); @@ -217,14 +234,16 @@ void AtomicServicePattern::UpdateDividerColor(RefPtr& theme, RefPtr divider->MarkDirtyNode(); } -void AtomicServicePattern::UpdateIconColor(RefPtr& theme, RefPtr& icon) +void AtomicServicePattern::UpdateIconColor(RefPtr& theme, RefPtr& icon, bool isLight) { CHECK_NULL_VOID(theme); CHECK_NULL_VOID(icon); // fill color - auto color = theme->GetIconColor(); + auto color = isLight ? theme->GetIconColorLight() : theme->GetIconColorDark(); ACE_UPDATE_NODE_PAINT_PROPERTY(ImageRenderProperty, SvgFillColor, color, icon); ACE_UPDATE_NODE_RENDER_CONTEXT(ForegroundColor, color, icon); + icon->MarkModifyDone(); + icon->MarkDirtyNode(); } void AtomicServicePattern::UpdateLayout() diff --git a/frameworks/core/components_ng/pattern/app_bar/atomic_service_pattern.h b/frameworks/core/components_ng/pattern/app_bar/atomic_service_pattern.h index 6f6994d2e29370a59921b04999a12e6625438af0..8495a125accd455fe27fa147ef5a085779d5b547 100644 --- a/frameworks/core/components_ng/pattern/app_bar/atomic_service_pattern.h +++ b/frameworks/core/components_ng/pattern/app_bar/atomic_service_pattern.h @@ -16,6 +16,7 @@ #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_ATOMIC_SERVICE_PATTERN_H #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_ATOMIC_SERVICE_PATTERN_H +#include #include "core/components_ng/pattern/app_bar/app_bar_theme.h" #include "core/components_ng/pattern/app_bar/atomic_service_layout_algorithm.h" #include "core/components_ng/pattern/linear_layout/linear_layout_pattern.h" @@ -44,15 +45,17 @@ public: RefPtr GetCloseButton(); RefPtr GetMenuIcon(); RefPtr GetCloseIcon(); - void UpdateColor(); + void UpdateColor(std::optional isLight); void UpdateMenuBarColor(RefPtr& theme, RefPtr& menuBar, bool isLight); - void UpdateButtonColor(RefPtr& theme, RefPtr& button); + void UpdateButtonColor(RefPtr& theme, RefPtr& button, bool isLight); void UpdateDividerColor(RefPtr& theme, RefPtr& divider, bool isLight); - void UpdateIconColor(RefPtr& theme, RefPtr& icon); + void UpdateIconColor(RefPtr& theme, RefPtr& icon, bool isLight); void UpdateLayout(); void UpdateMenuBarLayout(RefPtr& theme, RefPtr& menuBar, bool isRtl); void UpdateButtonLayout(RefPtr& theme, RefPtr& button, bool isLeft); void UpdateIconLayout(RefPtr& theme, RefPtr& icon, bool isLeft); + + std::optional settedColorMode = std::nullopt; }; } // namespace OHOS::Ace::NG #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_ATOMIC_SERVICE_PATTERN_H diff --git a/interfaces/inner_api/ace/ui_content.h b/interfaces/inner_api/ace/ui_content.h index 5ad5d0d1f9a9211cf227f503ea5c2bfc60769651..b0f0e712fc587aa8c937ea65a63d1b52f3c6a7ab 100644 --- a/interfaces/inner_api/ace/ui_content.h +++ b/interfaces/inner_api/ace/ui_content.h @@ -410,6 +410,8 @@ public: const std::function)>& callback) const {}; virtual void SetContentNodeGrayScale(float grayscale) {}; + + virtual void SetStatusBarItemColor(uint32_t color) {}; }; } // namespace OHOS::Ace diff --git a/test/unittest/core/pattern/app_bar/app_bar_test_ng.cpp b/test/unittest/core/pattern/app_bar/app_bar_test_ng.cpp index ca3bc2172ccc6eb2bf47932c4714fc14121e2519..bfe33a8168fc98beb59f866b50ff24e6dadf3bf9 100644 --- a/test/unittest/core/pattern/app_bar/app_bar_test_ng.cpp +++ b/test/unittest/core/pattern/app_bar/app_bar_test_ng.cpp @@ -247,8 +247,8 @@ HWTEST_F(AppBarTestNg, TestUpdateIconColor008, TestSize.Level1) auto icon = pattern->GetMenuIcon(); auto renderContext = icon->GetRenderContext(); - pattern->UpdateIconColor(theme, icon); - EXPECT_EQ(renderContext->GetForegroundColorValue(Color::RED), theme->GetIconColor()); + pattern->UpdateIconColor(theme, icon, true); + EXPECT_EQ(renderContext->GetForegroundColorValue(Color::RED), theme->GetIconColorLight()); } /**