From e384e2130a7cecea6a481ed625376dcbbd484c07 Mon Sep 17 00:00:00 2001 From: Jakub Florkowski Date: Sun, 25 Aug 2024 18:11:47 +0200 Subject: [PATCH] Update a tabbar with gradient on app theme change --- .../Handlers/TabbedPage/iOS/TabbedRenderer.cs | 23 +++++++++++-- .../Platform/Android/TabbedPageManager.cs | 22 +++++++++++++ .../TestCases.HostApp/Issues/Issue24356.xaml | 23 +++++++++++++ .../Issues/Issue24356.xaml.cs | 11 +++++++ .../Tests/Issues/Issue24356.cs | 33 +++++++++++++++++++ 5 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/Issue24356.xaml create mode 100644 src/Controls/tests/TestCases.HostApp/Issues/Issue24356.xaml.cs create mode 100644 src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue24356.cs diff --git a/src/Controls/src/Core/Compatibility/Handlers/TabbedPage/iOS/TabbedRenderer.cs b/src/Controls/src/Core/Compatibility/Handlers/TabbedPage/iOS/TabbedRenderer.cs index 4720766995bc..6256b4d5b115 100644 --- a/src/Controls/src/Core/Compatibility/Handlers/TabbedPage/iOS/TabbedRenderer.cs +++ b/src/Controls/src/Core/Compatibility/Handlers/TabbedPage/iOS/TabbedRenderer.cs @@ -29,6 +29,8 @@ public class TabbedRenderer : UITabBarController, IPlatformViewHandler UITabBarAppearance _tabBarAppearance; WeakReference _element; + Brush _currentBarBackground; + IMauiContext MauiContext => _mauiContext; public static IPropertyMapper Mapper = new PropertyMapper(TabbedViewHandler.ViewMapper); public static CommandMapper CommandMapper = new CommandMapper(TabbedViewHandler.ViewCommandMapper); @@ -360,9 +362,26 @@ void UpdateBarBackground() if (Tabbed is not TabbedPage tabbed || TabBar == null) return; - var barBackground = tabbed.BarBackground; + if(_currentBarBackground is GradientBrush oldBarBackground) + { + oldBarBackground.Parent = null; + oldBarBackground.InvalidateGradientBrushRequested -= OnBarBackgroundChanged; + } + + _currentBarBackground = tabbed.BarBackground; - TabBar.UpdateBackground(barBackground); + if(_currentBarBackground is GradientBrush newGradientBrush) + { + newGradientBrush.Parent = tabbed; + newGradientBrush.InvalidateGradientBrushRequested += OnBarBackgroundChanged; + } + + TabBar.UpdateBackground(_currentBarBackground); + } + + void OnBarBackgroundChanged(object sender, EventArgs e) + { + TabBar.UpdateBackground(_currentBarBackground); } void UpdateBarTextColor() diff --git a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs index 4fd95c9a6e81..584dcf8b7024 100644 --- a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs +++ b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs @@ -574,8 +574,30 @@ internal void UpdateBarBackground() if (_currentBarBackground == Element.BarBackground) return; + if(_currentBarBackground is GradientBrush oldGradientBrush) + { + oldGradientBrush.Parent = null; + oldGradientBrush.InvalidateGradientBrushRequested -= OnBarBackgroundChanged; + } + _currentBarBackground = Element.BarBackground; + if(_currentBarBackground is GradientBrush newGradientBrush) + { + newGradientBrush.Parent = Element; + newGradientBrush.InvalidateGradientBrushRequested += OnBarBackgroundChanged; + } + + RefreshBarBackground(); + } + + void OnBarBackgroundChanged(object sender, EventArgs e) + { + RefreshBarBackground(); + } + + void RefreshBarBackground() + { if (IsBottomTabPlacement) _bottomNavigationView.UpdateBackground(_currentBarBackground); else diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue24356.xaml b/src/Controls/tests/TestCases.HostApp/Issues/Issue24356.xaml new file mode 100644 index 000000000000..0a79389c32d2 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue24356.xaml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue24356.xaml.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue24356.xaml.cs new file mode 100644 index 000000000000..9808f9db5bea --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue24356.xaml.cs @@ -0,0 +1,11 @@ +namespace Maui.Controls.Sample.Issues +{ + [Issue(IssueTracker.Github, 24356, "AppThemeBinding BarBackground with Brush in NavigationPage and TabbedPage not working", PlatformAffected.All)] + public partial class Issue24356 : TabbedPage + { + public Issue24356() + { + InitializeComponent(); + } + } +} diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue24356.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue24356.cs new file mode 100644 index 000000000000..11bd24338f75 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue24356.cs @@ -0,0 +1,33 @@ +#if ANDROID || IOS +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues +{ + public class Issue24356 : _IssuesUITest + { + public Issue24356(TestDevice testDevice) : base(testDevice) + { + } + + public override string Issue => "AppThemeBinding BarBackground with Brush in NavigationPage and TabbedPage not working"; + + [Test] + [Category(UITestCategories.TabbedPage)] + public void GradientInTabBarShouldChange() + { + try { + App.WaitForElement("lightThemeLabel"); + App.SetDarkTheme(); + App.WaitForElement("darkThemeLabel"); + VerifyScreenshot(); + } + finally + { + App.SetLightTheme(); + } + } + } +} +#endif \ No newline at end of file