diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/StatusBarBehaviorPage.xaml b/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/StatusBarBehaviorPage.xaml
index 7698437c54..f60382815e 100644
--- a/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/StatusBarBehaviorPage.xaml
+++ b/samples/CommunityToolkit.Maui.Sample/Pages/Behaviors/StatusBarBehaviorPage.xaml
@@ -1,4 +1,5 @@
+
-
+
-
+
-
-
-
-
-
+
+
+
+
+
+
-
+
-
+
-
+
-
+
-
+
+ VerticalOptions="Center" />
\ No newline at end of file
diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/PlatformSpecific/NavigationBarPage.xaml b/samples/CommunityToolkit.Maui.Sample/Pages/PlatformSpecific/NavigationBarPage.xaml
index 86a5d54c2a..9e57664114 100644
--- a/samples/CommunityToolkit.Maui.Sample/Pages/PlatformSpecific/NavigationBarPage.xaml
+++ b/samples/CommunityToolkit.Maui.Sample/Pages/PlatformSpecific/NavigationBarPage.xaml
@@ -108,8 +108,8 @@
ThumbColor="Blue"
Value="{Binding BlueSliderValue}" />
-
-
+
+
@@ -125,7 +125,7 @@
@@ -135,12 +135,12 @@
-
+
\ No newline at end of file
diff --git a/samples/CommunityToolkit.Maui.Sample/ViewModels/Behaviors/StatusBarBehaviorViewModel.cs b/samples/CommunityToolkit.Maui.Sample/ViewModels/Behaviors/StatusBarBehaviorViewModel.cs
index 8b5a7f560a..2e01da1b5e 100644
--- a/samples/CommunityToolkit.Maui.Sample/ViewModels/Behaviors/StatusBarBehaviorViewModel.cs
+++ b/samples/CommunityToolkit.Maui.Sample/ViewModels/Behaviors/StatusBarBehaviorViewModel.cs
@@ -5,17 +5,40 @@ namespace CommunityToolkit.Maui.Sample.ViewModels.Behaviors;
public partial class StatusBarBehaviorViewModel : BaseViewModel
{
+ public Color StatusBarColor => Color.FromRgba(RedSliderValue, GreenSliderValue, BlueSliderValue, AlphaSliderValue);
+
+ public StatusBarStyle StatusBarStyle
+ {
+ get
+ {
+ if (IsDefaultChecked)
+ {
+ return StatusBarStyle.Default;
+ }
+ if (IsLightContentChecked)
+ {
+ return StatusBarStyle.LightContent;
+ }
+ if (IsDarkContentChecked)
+ {
+ return StatusBarStyle.DarkContent;
+ }
+
+ throw new NotSupportedException($"{nameof(StatusBarStyle)} type is not supported.");
+ }
+ }
+
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(StatusBarColor))]
- public partial double RedSliderValue { get; set; }
+ public partial double RedSliderValue { get; set; } = 0.5;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(StatusBarColor))]
- public partial double GreenSliderValue { get; set; }
+ public partial double GreenSliderValue { get; set; } = 0.5;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(StatusBarColor))]
- public partial double BlueSliderValue { get; set; }
+ public partial double BlueSliderValue { get; set; } = 0.5;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(StatusBarColor))]
@@ -23,36 +46,13 @@ public partial class StatusBarBehaviorViewModel : BaseViewModel
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(StatusBarStyle))]
- public partial bool IsLightContentChecked { get; set; } = true;
+ public partial bool IsLightContentChecked { get; set; }
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(StatusBarStyle))]
- public partial bool IsDarkContentChecked { get; set; } = true;
+ public partial bool IsDarkContentChecked { get; set; }
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(StatusBarStyle))]
public partial bool IsDefaultChecked { get; set; } = true;
-
- public Color StatusBarColor => Color.FromRgba(RedSliderValue, GreenSliderValue, BlueSliderValue, AlphaSliderValue);
-
- public StatusBarStyle StatusBarStyle
- {
- get
- {
- if (IsDefaultChecked)
- {
- return StatusBarStyle.Default;
- }
- if (IsLightContentChecked)
- {
- return StatusBarStyle.LightContent;
- }
- if (IsDarkContentChecked)
- {
- return StatusBarStyle.DarkContent;
- }
-
- throw new NotSupportedException($"{nameof(StatusBarStyle)} {StatusBarStyle} is not supported.");
- }
- }
}
\ No newline at end of file
diff --git a/src/CommunityToolkit.Maui.Core/Extensions/WindowExtensions.android.cs b/src/CommunityToolkit.Maui.Core/Extensions/WindowExtensions.android.cs
index b21466033a..a04de59738 100644
--- a/src/CommunityToolkit.Maui.Core/Extensions/WindowExtensions.android.cs
+++ b/src/CommunityToolkit.Maui.Core/Extensions/WindowExtensions.android.cs
@@ -1,4 +1,7 @@
using Android.Views;
+using Microsoft.Maui.Platform;
+using Activity = Android.App.Activity;
+using DialogFragment = AndroidX.Fragment.App.DialogFragment;
namespace CommunityToolkit.Maui.Core.Extensions;
@@ -8,16 +11,34 @@ namespace CommunityToolkit.Maui.Core.Extensions;
public static class AndroidWindowExtensions
{
///
- /// Gets the current window associated with the specified activity.
+ /// Gets the current visible window associated with the specified activity.
///
/// The activity.
/// The current window.
/// Thrown when the activity window is null.
public static Window GetCurrentWindow(this Activity activity)
{
- var window = activity.Window ?? throw new InvalidOperationException($"{nameof(activity.Window)} cannot be null");
- window.ClearFlags(WindowManagerFlags.TranslucentStatus);
- window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
- return window;
+ Window? currentWindow = null;
+
+ var fragmentManager = activity.GetFragmentManager();
+ if (fragmentManager is not null && fragmentManager.Fragments.OfType().Any())
+ {
+ var fragments = fragmentManager.Fragments;
+ for (var i = fragments.Count - 1; i >= 0; i--)
+ {
+ if (fragments[i] is DialogFragment { Dialog: { IsShowing: true, Window: not null }, IsVisible: true } dialogFragment)
+ {
+ currentWindow = dialogFragment.Dialog.Window;
+ break;
+ }
+ }
+ }
+
+ currentWindow ??= activity.Window ?? throw new InvalidOperationException($"{nameof(activity.Window)} cannot be null");
+
+ currentWindow.ClearFlags(WindowManagerFlags.TranslucentStatus);
+ currentWindow.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
+
+ return currentWindow;
}
}
\ No newline at end of file
diff --git a/src/CommunityToolkit.Maui.Core/Platform/StatusBar/StatusBar.android.cs b/src/CommunityToolkit.Maui.Core/Platform/StatusBar/StatusBar.android.cs
index 50d7fbc709..9d98cb5b7d 100644
--- a/src/CommunityToolkit.Maui.Core/Platform/StatusBar/StatusBar.android.cs
+++ b/src/CommunityToolkit.Maui.Core/Platform/StatusBar/StatusBar.android.cs
@@ -35,57 +35,58 @@ static void PlatformSetColor(Color color)
return;
}
- if (Activity.Window is not null)
+ if (Activity.GetCurrentWindow() is not Window { DecorView.RootView: not null } window)
{
- var platformColor = color.ToPlatform();
+ return;
+ }
- if (OperatingSystem.IsAndroidVersionAtLeast(35))
- {
- const string statusBarOverlayTag = "StatusBarOverlay";
+ var platformColor = color.ToPlatform();
- var window = Activity.GetCurrentWindow();
+ if (OperatingSystem.IsAndroidVersionAtLeast(35))
+ {
+ const string statusBarOverlayTag = "StatusBarOverlay";
- var decorGroup = (ViewGroup)window.DecorView;
- var statusBarOverlay = decorGroup.FindViewWithTag(statusBarOverlayTag);
+ var decorGroup = (ViewGroup)window.DecorView.RootView;
+ var statusBarOverlay = decorGroup.FindViewWithTag(statusBarOverlayTag);
- if (statusBarOverlay is null)
- {
- var statusBarHeight = Activity.Resources?.GetIdentifier("status_bar_height", "dimen", "android") ?? 0;
- var statusBarPixelSize = statusBarHeight > 0 ? Activity.Resources?.GetDimensionPixelSize(statusBarHeight) ?? 0 : 0;
+ if (statusBarOverlay is null)
+ {
+ var statusBarHeight = Activity.Resources?.GetIdentifier("status_bar_height", "dimen", "android") ?? 0;
+ var statusBarPixelSize = statusBarHeight > 0 ? Activity.Resources?.GetDimensionPixelSize(statusBarHeight) ?? 0 : 0;
- statusBarOverlay = new(Activity)
+ statusBarOverlay = new(Activity)
+ {
+ LayoutParameters = new FrameLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, statusBarPixelSize + 3)
{
- LayoutParameters = new FrameLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, statusBarPixelSize + 3)
- {
- Gravity = GravityFlags.Top
- }
- };
-
- decorGroup.AddView(statusBarOverlay);
- statusBarOverlay.SetZ(0);
- }
+ Gravity = GravityFlags.Top
+ }
+ };
- statusBarOverlay.SetBackgroundColor(platformColor);
- }
- else
- {
- Activity.Window.SetStatusBarColor(platformColor);
+ statusBarOverlay.Tag = statusBarOverlayTag;
+ decorGroup.AddView(statusBarOverlay);
+ statusBarOverlay.SetZ(0);
}
- bool isColorTransparent = platformColor == PlatformColor.Transparent;
- if (isColorTransparent)
- {
- Activity.Window.ClearFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
- Activity.Window.SetFlags(WindowManagerFlags.LayoutNoLimits, WindowManagerFlags.LayoutNoLimits);
- }
- else
- {
- Activity.Window.ClearFlags(WindowManagerFlags.LayoutNoLimits);
- Activity.Window.SetFlags(WindowManagerFlags.DrawsSystemBarBackgrounds, WindowManagerFlags.DrawsSystemBarBackgrounds);
- }
+ statusBarOverlay.SetBackgroundColor(platformColor);
+ }
+ else
+ {
+ window.SetStatusBarColor(platformColor);
+ }
- WindowCompat.SetDecorFitsSystemWindows(Activity.Window, !isColorTransparent);
+ bool isColorTransparent = platformColor == PlatformColor.Transparent;
+ if (isColorTransparent)
+ {
+ window.ClearFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
+ window.SetFlags(WindowManagerFlags.LayoutNoLimits, WindowManagerFlags.LayoutNoLimits);
}
+ else
+ {
+ window.ClearFlags(WindowManagerFlags.LayoutNoLimits);
+ window.SetFlags(WindowManagerFlags.DrawsSystemBarBackgrounds, WindowManagerFlags.DrawsSystemBarBackgrounds);
+ }
+
+ WindowCompat.SetDecorFitsSystemWindows(window, !isColorTransparent);
}
static void PlatformSetStyle(StatusBarStyle style)
@@ -98,12 +99,12 @@ static void PlatformSetStyle(StatusBarStyle style)
switch (style)
{
case StatusBarStyle.DarkContent:
- SetStatusBarAppearance(Activity, true);
+ SetStatusBarAppearance(true);
break;
case StatusBarStyle.Default:
case StatusBarStyle.LightContent:
- SetStatusBarAppearance(Activity, false);
+ SetStatusBarAppearance(false);
break;
default:
@@ -111,10 +112,10 @@ static void PlatformSetStyle(StatusBarStyle style)
}
}
- static void SetStatusBarAppearance(Activity activity, bool isLightStatusBars)
+ static void SetStatusBarAppearance(bool isLightStatusBars)
{
- var window = activity.GetCurrentWindow();
- if (WindowCompat.GetInsetsController(window, window.DecorView) is WindowInsetsControllerCompat windowController)
+ if (Activity.GetCurrentWindow() is Window window
+ && WindowCompat.GetInsetsController(window, window.DecorView) is WindowInsetsControllerCompat windowController)
{
windowController.AppearanceLightStatusBars = isLightStatusBars;
}
diff --git a/src/CommunityToolkit.Maui/Extensions/PropertyChangedEventArgsExtensions.shared.cs b/src/CommunityToolkit.Maui/Extensions/PropertyChangedEventArgsExtensions.shared.cs
index 4d4929512b..7156502fb8 100644
--- a/src/CommunityToolkit.Maui/Extensions/PropertyChangedEventArgsExtensions.shared.cs
+++ b/src/CommunityToolkit.Maui/Extensions/PropertyChangedEventArgsExtensions.shared.cs
@@ -7,40 +7,8 @@ namespace CommunityToolkit.Maui.Extensions;
static class PropertyChangedEventArgsExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool Is(this string propertyName, BindableProperty property) =>
- propertyName == property.PropertyName;
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsOneOf(this string propertyName, BindableProperty p0, BindableProperty p1)
- {
- return propertyName == p0.PropertyName
- || propertyName == p1.PropertyName;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsOneOf(this string propertyName, BindableProperty p0, BindableProperty p1, BindableProperty p2)
- {
- return propertyName == p0.PropertyName
- || propertyName == p1.PropertyName
- || propertyName == p2.PropertyName;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsOneOf(this string propertyName, BindableProperty p0, BindableProperty p1, BindableProperty p2, BindableProperty p3)
- {
- return propertyName == p0.PropertyName
- || propertyName == p1.PropertyName
- || propertyName == p2.PropertyName
- || propertyName == p3.PropertyName;
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool IsOneOf(this string propertyName, BindableProperty p0, BindableProperty p1, BindableProperty p2, BindableProperty p3, BindableProperty p4)
+ public static bool IsOneOf(this string propertyName, params IReadOnlyList bindableProperties)
{
- return propertyName == p0.PropertyName
- || propertyName == p1.PropertyName
- || propertyName == p2.PropertyName
- || propertyName == p3.PropertyName
- || propertyName == p4.PropertyName;
+ return bindableProperties.Any(bindableProperty => bindableProperty.PropertyName == propertyName);
}
}
\ No newline at end of file