Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.Maui.Sample.ViewModels.Behaviors;
using CommunityToolkit.Maui.Behaviors;
using CommunityToolkit.Maui.Sample.ViewModels.Behaviors;

namespace CommunityToolkit.Maui.Sample.Pages.Behaviors;

Expand All @@ -7,5 +8,18 @@ public class BehaviorsGalleryPage : BaseGalleryPage<BehaviorsGalleryViewModel>
public BehaviorsGalleryPage(IDeviceInfo deviceInfo, BehaviorsGalleryViewModel behaviorsGalleryViewModel)
: base("Behaviors", deviceInfo, behaviorsGalleryViewModel)
{
#if ANDROID || IOS
AddStatusBarBehavior();
#endif
}

void AddStatusBarBehavior()
{
Behaviors.Add(new StatusBarBehavior
{
StatusBarColor = Color.FromRgb(25, 118, 210),
StatusBarStyle = Core.StatusBarStyle.LightContent,
ApplyOn = StatusBarApplyOn.OnPageNavigatedTo,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@

<Label Text="Select StatusBar and NavigationBar style" />

<RadioButton VerticalOptions="Center" IsChecked="{Binding IsDefaultChecked}">
<RadioButton.Content>
<Label Text="Default" Margin="10,0,0,0" VerticalTextAlignment="Center" VerticalOptions="Center" />
</RadioButton.Content>
</RadioButton>
<RadioButton
Content="Default"
IsChecked="{Binding IsDefaultChecked}"
VerticalOptions="Center" />

<RadioButton VerticalOptions="Center" IsChecked="{Binding IsLightContentChecked}">
<RadioButton.Content>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@

using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Core.Platform;
using CommunityToolkit.Maui.Extensions;

namespace CommunityToolkit.Maui.Behaviors;

/// <summary>
/// When to apply the status bar color and style.
/// </summary>
public enum StatusBarApplyOn
{
/// <summary>
/// Apply color and style when the behavior has been attached to the page.
/// </summary>
OnBehaviorAttachedTo,

/// <summary>
/// Apply color and style when the page has been navigated to.
/// </summary>
OnPageNavigatedTo,
}

/// <summary>
/// <see cref="PlatformBehavior{TView,TPlatformView}"/> that controls the Status bar color
/// </summary>
Expand All @@ -19,13 +34,18 @@ public class StatusBarBehavior : PlatformBehavior<Page>
public static readonly BindableProperty StatusBarColorProperty =
BindableProperty.Create(nameof(StatusBarColor), typeof(Color), typeof(StatusBarBehavior), Colors.Transparent);


/// <summary>
/// <see cref="BindableProperty"/> that manages the StatusBarColor property.
/// </summary>
public static readonly BindableProperty StatusBarStyleProperty =
BindableProperty.Create(nameof(StatusBarStyle), typeof(StatusBarStyle), typeof(StatusBarBehavior), StatusBarStyle.Default);

/// <summary>
/// <see cref="BindableProperty"/> that manages the ApplyOn property.
/// </summary>
public static readonly BindableProperty ApplyOnProperty =
BindableProperty.Create(nameof(ApplyOn), typeof(StatusBarApplyOn), typeof(StatusBarBehavior), StatusBarApplyOn.OnBehaviorAttachedTo);

/// <summary>
/// Property that holds the value of the Status bar color.
/// </summary>
Expand All @@ -44,6 +64,16 @@ public StatusBarStyle StatusBarStyle
set => SetValue(StatusBarStyleProperty, value);
}

/// <summary>
/// When the status bar color and style should be applied.
/// </summary>
public StatusBarApplyOn ApplyOn
{
get => (StatusBarApplyOn)GetValue(ApplyOnProperty);
set => SetValue(ApplyOnProperty, value);
}


#if !(WINDOWS || MACCATALYST || TIZEN)

/// <inheritdoc />
Expand All @@ -55,28 +85,49 @@ protected override void OnAttachedTo(Page bindable, Android.Views.View platformV
protected override void OnAttachedTo(Page bindable, object platformView)
#endif
{
StatusBar.SetColor(StatusBarColor);
StatusBar.SetStyle(StatusBarStyle);
if (ApplyOn == StatusBarApplyOn.OnBehaviorAttachedTo)
{
StatusBar.SetColor(StatusBarColor);
StatusBar.SetStyle(StatusBarStyle);
}

bindable.NavigatedTo += OnPageNavigatedTo;
#if IOS
bindable.SizeChanged += new EventHandler(page_SizeChanged);
bindable.SizeChanged += OnPageSizeChanged;
#endif
}

#if IOS
/// <inheritdoc />
#if IOS
protected override void OnDetachedFrom(Page bindable, UIKit.UIView platformView)
#elif ANDROID
protected override void OnDetachedFrom(Page bindable, Android.Views.View platformView)
#else
protected override void OnDetachedFrom(Page bindable, object platformView)
#endif
{
bindable.SizeChanged -= new EventHandler(page_SizeChanged);
}
#if IOS
bindable.SizeChanged -= OnPageSizeChanged;
#endif
bindable.NavigatedTo -= OnPageNavigatedTo;
}

#if IOS
void page_SizeChanged(object? sender, EventArgs e)
void OnPageSizeChanged(object? sender, EventArgs e)
{
StatusBar.UpdateBarSize();
}
#endif

void OnPageNavigatedTo(object? sender, NavigatedToEventArgs e)
{
if (ApplyOn == StatusBarApplyOn.OnPageNavigatedTo)
{
StatusBar.SetColor(StatusBarColor);
StatusBar.SetStyle(StatusBarStyle);
}
}

/// <inheritdoc />
protected override void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
Expand Down