Skip to content

[Proposal] Android-Specific Navigation Bar Effect #103

@TheCodeTraveler

Description

@TheCodeTraveler

NavigationBarEffect

  • Proposed
  • Prototype: Not Started
  • Implementation: Not Started
    • iOS Support
    • Android Support
    • macOS Support
    • Windows Support
  • Unit Tests: Not Started
  • Sample: Not Started
  • Documentation: Not Started

Summary

Android-specific implementation allowing the user to customize the Navigation Bar

Detailed Design

NavigationBarEffect.cs

namespace Xamarin.CommunityToolkit.PlatformConfiguration.AndroidSpecific
{
	[Preserve(Conditional = true)]
	public static class NavigationBarEffect
	{
		static NavigationBarEffect()
		{
			#region Required work-around to prevent linker from removing the platform-specific implementation
#if __ANDROID__
			if (System.DateTime.Now.Ticks < 0)
				_ = new PlatformNavigationBarEffect();
#endif
			#endregion
		}

		public static readonly BindableProperty ColorProperty = BindableProperty.CreateAttached(
			"Color", typeof(Color), typeof(NavigationBarEffect), Color.Default, propertyChanged: TryGenerateEffect);

		public static readonly BindableProperty StyleProperty = BindableProperty.CreateAttached(
			"Style", typeof(NavigationBarStyle), typeof(NavigationBarEffect), NavigationBarStyle.Default, propertyChanged: TryGenerateEffect);

		public static Color GetColor(BindableObject bindable) =>
			(Color)bindable.GetValue(ColorProperty);

		public static void SetColor(BindableObject bindable, Color value) =>
			bindable.SetValue(ColorProperty, value);

		public static NavigationBarStyle GetStyle(BindableObject bindable) =>
			(NavigationBarStyle)bindable.GetValue(StyleProperty);

		public static void SetStyle(BindableObject bindable, NavigationBarStyle value) =>
			bindable.SetValue(StyleProperty, value);

		public static IPlatformElementConfiguration<XFP.Android, FormsElement> SetNavigationBarColor(this IPlatformElementConfiguration<XFP.Android, FormsElement> config, Color color)
		{
			SetColor(config.Element, color);
			return config;
		}

		public static Color GetNavigationBarColor(this IPlatformElementConfiguration<XFP.Android, FormsElement> config) =>
			GetColor(config.Element);

		public static IPlatformElementConfiguration<XFP.Android, FormsElement> SetNavigationBarStyle(this IPlatformElementConfiguration<XFP.Android, FormsElement> config, NavigationBarStyle style)
		{
			SetStyle(config.Element, style);
			return config;
		}

		public static NavigationBarStyle GetNavigationBarStyle(this IPlatformElementConfiguration<XFP.Android, FormsElement> config) =>
			GetStyle(config.Element);

		static void TryGenerateEffect(BindableObject bindable, object oldValue, object newValue)
		{
			if (bindable is not FormsElement page)
				return;

			DetachEffect(page);

			AttachEffect(page);
		}

		static void AttachEffect(FormsElement element)
		{
			IElementController controller = element;
			if (controller is null || controller.EffectIsAttached(EffectIds.NavigationBar))
				return;

			element.Effects.Add(Effect.Resolve(EffectIds.NavigationBar));
		}

		static void DetachEffect(FormsElement element)
		{
			IElementController controller = element;
			if (controller is null || !controller.EffectIsAttached(EffectIds.NavigationBar))
				return;

			var toRemove = element.Effects.FirstOrDefault(e => e.ResolveId == Effect.Resolve(EffectIds.NavigationBar).ResolveId);
			if (toRemove != null)
				element.Effects.Remove(toRemove);
		}
	}
}

Usage Syntax

XAML Usage

<Setter Property="droid:NavigationBarEffect.Color" Value="{StaticResource NavBarColor}" />

C# Usage

var navigationPage = new NavigationPage();
NavigationBarEffect.SetColor(navigationPage, Colors.Blue);

Metadata

Metadata

Labels

championA member of the .NET MAUI Toolkit core team has chosen to champion this featureproposalA fully fleshed out proposal describing a new feature in syntactic and semantic detail

Type

No type

Projects

Status

Completed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions