@ -101,6 +101,14 @@ namespace Avalonia.Controls
"SystemBarColor" ,
inherits : true ) ;
/// <summary>
/// Defines the AutoSafeAreaPadding attached property.
/// </summary>
public static readonly AttachedProperty < bool > AutoSafeAreaPaddingProperty =
AvaloniaProperty . RegisterAttached < TopLevel , Control , bool > (
"AutoSafeAreaPadding" ,
defaultValue : true ) ;
/// <summary>
/// Defines the <see cref="BackRequested"/> event.
/// </summary>
@ -155,6 +163,12 @@ namespace Avalonia.Controls
}
} ) ;
AutoSafeAreaPaddingProperty . Changed . AddClassHandler < Control > ( ( view , e ) = >
{
var topLevel = view as TopLevel ? ? view . Parent as TopLevel ;
topLevel ? . InvalidateChildInsetsPadding ( ) ;
} ) ;
PointerOverElementProperty . Changed . AddClassHandler < TopLevel > ( ( topLevel , e ) = >
{
if ( e . OldValue is InputElement oldInputElement )
@ -478,25 +492,44 @@ namespace Avalonia.Controls
}
/// <summary>
/// Helper for setting the color of the platform's system bars
/// Helper for setting the color of the platform's system bars.
/// </summary>
/// <param name="control">The main view attached to the toplevel, or the toplevel</param>
/// <param name="color">The color to set</param>
/// <param name="control">The main view attached to the toplevel, or the toplevel. </param>
/// <param name="color">The color to set. </param>
public static void SetSystemBarColor ( Control control , SolidColorBrush ? color )
{
control . SetValue ( SystemBarColorProperty , color ) ;
}
/// <summary>
/// Helper for getting the color of the platform's system bars
/// Helper for getting the color of the platform's system bars.
/// </summary>
/// <param name="control">The main view attached to the toplevel, or the toplevel</param>
/// <returns>The current color of the platform's system bars</returns>
/// <param name="control">The main view attached to the toplevel, or the toplevel. </param>
/// <returns>The current color of the platform's system bars. </returns>
public static SolidColorBrush ? GetSystemBarColor ( Control control )
{
return control . GetValue ( SystemBarColorProperty ) ;
}
/// <summary>
/// Enabled or disables whenever TopLevel should automatically adjust paddings depending on the safe area.
/// </summary>
/// <param name="control">The main view attached to the toplevel, or the toplevel.</param>
/// <param name="value">Value to be set.</param>
public static void SetAutoSafeAreaPadding ( Control control , bool value )
{
control . SetValue ( AutoSafeAreaPaddingProperty , value ) ;
}
/// <summary>
/// Gets if auto safe area padding is enabled.
/// </summary>
/// <param name="control">The main view attached to the toplevel, or the toplevel.</param>
public static bool GetAutoSafeAreaPadding ( Control control )
{
return control . GetValue ( AutoSafeAreaPaddingProperty ) ;
}
/// <inheritdoc/>
double ILayoutRoot . LayoutScaling = > PlatformImpl ? . RenderScaling ? ? 1 ;
@ -585,12 +618,41 @@ namespace Avalonia.Controls
{
base . OnPropertyChanged ( change ) ;
if ( _ platformImplBindings . TryGetValue ( change . Property , out var bindingAction ) )
if ( change . Property = = ContentProperty )
{
InvalidateChildInsetsPadding ( ) ;
}
else if ( _ platformImplBindings . TryGetValue ( change . Property , out var bindingAction ) )
{
bindingAction ( ) ;
}
}
private IDisposable ? _ insetsPaddings ;
private void InvalidateChildInsetsPadding ( )
{
if ( Content is Control child
& & InsetsManager is { } insetsManager )
{
insetsManager . SafeAreaChanged - = InsetsManagerOnSafeAreaChanged ;
_ insetsPaddings ? . Dispose ( ) ;
if ( child . GetValue ( AutoSafeAreaPaddingProperty ) )
{
insetsManager . SafeAreaChanged + = InsetsManagerOnSafeAreaChanged ;
_ insetsPaddings = child . SetValue (
PaddingProperty ,
insetsManager . SafeAreaPadding ,
BindingPriority . Style ) ; // lower priority, so it can be redefined by user
}
void InsetsManagerOnSafeAreaChanged ( object? sender , SafeAreaChangedArgs e )
{
InvalidateChildInsetsPadding ( ) ;
}
}
}
/// <summary>
/// Creates the layout manager for this <see cref="TopLevel" />.
/// </summary>