8 changed files with 241 additions and 55 deletions
@ -0,0 +1,83 @@ |
|||
using System; |
|||
using Avalonia.Controls.Platform; |
|||
using UIKit; |
|||
|
|||
namespace Avalonia.iOS; |
|||
#nullable enable |
|||
|
|||
internal class InsetsManager : IInsetsManager |
|||
{ |
|||
private readonly AvaloniaView _view; |
|||
private IAvaloniaViewController? _controller; |
|||
private bool _displayEdgeToEdge; |
|||
|
|||
public InsetsManager(AvaloniaView view) |
|||
{ |
|||
_view = view; |
|||
} |
|||
|
|||
internal void InitWithController(IAvaloniaViewController controller) |
|||
{ |
|||
_controller = controller; |
|||
if (_controller is not null) |
|||
{ |
|||
_controller.SafeAreaPaddingChanged += (_, _) => |
|||
{ |
|||
SafeAreaChanged?.Invoke(this, new SafeAreaChangedArgs(SafeAreaPadding)); |
|||
DisplayEdgeToEdgeChanged?.Invoke(this, _displayEdgeToEdge); |
|||
}; |
|||
} |
|||
} |
|||
|
|||
public SystemBarTheme? SystemBarTheme |
|||
{ |
|||
get => _controller?.PreferredStatusBarStyle switch |
|||
{ |
|||
UIStatusBarStyle.LightContent => Controls.Platform.SystemBarTheme.Dark, |
|||
UIStatusBarStyle.DarkContent => Controls.Platform.SystemBarTheme.Light, |
|||
_ => null |
|||
}; |
|||
set |
|||
{ |
|||
if (_controller != null) |
|||
{ |
|||
_controller.PreferredStatusBarStyle = value switch |
|||
{ |
|||
Controls.Platform.SystemBarTheme.Light => UIStatusBarStyle.DarkContent, |
|||
Controls.Platform.SystemBarTheme.Dark => UIStatusBarStyle.LightContent, |
|||
null => UIStatusBarStyle.Default, |
|||
_ => throw new ArgumentOutOfRangeException(nameof(value), value, null) |
|||
}; |
|||
} |
|||
} |
|||
} |
|||
|
|||
public bool? IsSystemBarVisible |
|||
{ |
|||
get => _controller?.PrefersStatusBarHidden == false; |
|||
set |
|||
{ |
|||
if (_controller is not null) |
|||
{ |
|||
_controller.PrefersStatusBarHidden = value == false; |
|||
} |
|||
} |
|||
} |
|||
public event EventHandler<SafeAreaChangedArgs>? SafeAreaChanged; |
|||
public event EventHandler<bool>? DisplayEdgeToEdgeChanged; |
|||
|
|||
public bool DisplayEdgeToEdge |
|||
{ |
|||
get => _displayEdgeToEdge; |
|||
set |
|||
{ |
|||
if (_displayEdgeToEdge != value) |
|||
{ |
|||
_displayEdgeToEdge = value; |
|||
DisplayEdgeToEdgeChanged?.Invoke(this, value); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public Thickness SafeAreaPadding => _controller?.SafeAreaPadding ?? default; |
|||
} |
|||
@ -0,0 +1,74 @@ |
|||
using System; |
|||
using Avalonia.Metadata; |
|||
using UIKit; |
|||
|
|||
namespace Avalonia.iOS; |
|||
|
|||
[Unstable] |
|||
public interface IAvaloniaViewController |
|||
{ |
|||
UIStatusBarStyle PreferredStatusBarStyle { get; set; } |
|||
bool PrefersStatusBarHidden { get; set; } |
|||
Thickness SafeAreaPadding { get; } |
|||
event EventHandler SafeAreaPaddingChanged; |
|||
} |
|||
|
|||
/// <inheritdoc cref="IAvaloniaViewController" />
|
|||
public class DefaultAvaloniaViewController : UIViewController, IAvaloniaViewController |
|||
{ |
|||
private UIStatusBarStyle? _preferredStatusBarStyle; |
|||
private bool? _prefersStatusBarHidden; |
|||
|
|||
/// <inheritdoc/>
|
|||
public override void ViewDidLayoutSubviews() |
|||
{ |
|||
base.ViewDidLayoutSubviews(); |
|||
var size = View?.Frame.Size ?? default; |
|||
var frame = View?.SafeAreaLayoutGuide.LayoutFrame ?? default; |
|||
var safeArea = new Thickness(frame.Left, frame.Top, size.Width - frame.Right, size.Height - frame.Bottom); |
|||
if (SafeAreaPadding != safeArea) |
|||
{ |
|||
SafeAreaPadding = safeArea; |
|||
SafeAreaPaddingChanged?.Invoke(this, EventArgs.Empty); |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public override bool PrefersStatusBarHidden() |
|||
{ |
|||
return _prefersStatusBarHidden ??= base.PrefersStatusBarHidden(); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public override UIStatusBarStyle PreferredStatusBarStyle() |
|||
{ |
|||
// don't set _preferredStatusBarStyle value if it's null, so we can keep "default" there instead of actual app style.
|
|||
return _preferredStatusBarStyle ?? base.PreferredStatusBarStyle(); |
|||
} |
|||
|
|||
UIStatusBarStyle IAvaloniaViewController.PreferredStatusBarStyle |
|||
{ |
|||
get => _preferredStatusBarStyle ?? UIStatusBarStyle.Default; |
|||
set |
|||
{ |
|||
_preferredStatusBarStyle = value; |
|||
SetNeedsStatusBarAppearanceUpdate(); |
|||
} |
|||
} |
|||
|
|||
bool IAvaloniaViewController.PrefersStatusBarHidden |
|||
{ |
|||
get => _prefersStatusBarHidden ?? false; // false is default on ios/ipados
|
|||
set |
|||
{ |
|||
_prefersStatusBarHidden = value; |
|||
SetNeedsStatusBarAppearanceUpdate(); |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public Thickness SafeAreaPadding { get; private set; } |
|||
|
|||
/// <inheritdoc/>
|
|||
public event EventHandler SafeAreaPaddingChanged; |
|||
} |
|||
Loading…
Reference in new issue