|
|
@@ -6,6 +6,7 @@ using Avalonia.Input.Raw;
|
|
|
using Avalonia.Layout;
|
|
|
using Avalonia.Logging;
|
|
|
using Avalonia.LogicalTree;
|
|
|
+using Avalonia.Media;
|
|
|
using Avalonia.Platform;
|
|
|
using Avalonia.Rendering;
|
|
|
using Avalonia.Styling;
|
|
|
@@ -43,13 +44,35 @@ namespace Avalonia.Controls
|
|
|
public static readonly StyledProperty<IInputElement> PointerOverElementProperty =
|
|
|
AvaloniaProperty.Register<TopLevel, IInputElement>(nameof(IInputRoot.PointerOverElement));
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Defines the <see cref="TransparencyLevelHint"/> property.
|
|
|
+ /// </summary>
|
|
|
+ public static readonly StyledProperty<WindowTransparencyLevel> TransparencyLevelHintProperty =
|
|
|
+ AvaloniaProperty.Register<TopLevel, WindowTransparencyLevel>(nameof(TransparencyLevelHint), WindowTransparencyLevel.None);
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Defines the <see cref="ActualTransparencyLevel"/> property.
|
|
|
+ /// </summary>
|
|
|
+ public static readonly DirectProperty<TopLevel, WindowTransparencyLevel> ActualTransparencyLevelProperty =
|
|
|
+ AvaloniaProperty.RegisterDirect<TopLevel, WindowTransparencyLevel>(nameof(ActualTransparencyLevel),
|
|
|
+ o => o.ActualTransparencyLevel,
|
|
|
+ unsetValue: WindowTransparencyLevel.None);
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Defines the <see cref="TransparencyBackgroundFallbackProperty"/> property.
|
|
|
+ /// </summary>
|
|
|
+ public static readonly StyledProperty<IBrush> TransparencyBackgroundFallbackProperty =
|
|
|
+ AvaloniaProperty.Register<TopLevel, IBrush>(nameof(TransparencyBackgroundFallback), Brushes.White);
|
|
|
+
|
|
|
private readonly IInputManager _inputManager;
|
|
|
private readonly IAccessKeyHandler _accessKeyHandler;
|
|
|
private readonly IKeyboardNavigationHandler _keyboardNavigationHandler;
|
|
|
private readonly IPlatformRenderInterface _renderInterface;
|
|
|
private readonly IGlobalStyles _globalStyles;
|
|
|
private Size _clientSize;
|
|
|
+ private WindowTransparencyLevel _actualTransparencyLevel;
|
|
|
private ILayoutManager _layoutManager;
|
|
|
+ private Border _transparencyFallbackBorder;
|
|
|
|
|
|
/// <summary>
|
|
|
/// Initializes static members of the <see cref="TopLevel"/> class.
|
|
|
@@ -57,6 +80,16 @@ namespace Avalonia.Controls
|
|
|
static TopLevel()
|
|
|
{
|
|
|
AffectsMeasure<TopLevel>(ClientSizeProperty);
|
|
|
+
|
|
|
+ TransparencyLevelHintProperty.Changed.AddClassHandler<TopLevel>(
|
|
|
+ (tl, e) =>
|
|
|
+ {
|
|
|
+ if (tl.PlatformImpl != null)
|
|
|
+ {
|
|
|
+ tl.PlatformImpl.SetTransparencyLevelHint((WindowTransparencyLevel)e.NewValue);
|
|
|
+ tl.HandleTransparencyLevelChanged(tl.PlatformImpl.TransparencyLevel);
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -85,6 +118,8 @@ namespace Avalonia.Controls
|
|
|
|
|
|
PlatformImpl = impl;
|
|
|
|
|
|
+ _actualTransparencyLevel = PlatformImpl.TransparencyLevel;
|
|
|
+
|
|
|
dependencyResolver = dependencyResolver ?? AvaloniaLocator.Current;
|
|
|
var styler = TryGetService<IStyler>(dependencyResolver);
|
|
|
|
|
|
@@ -108,6 +143,7 @@ namespace Avalonia.Controls
|
|
|
impl.Paint = HandlePaint;
|
|
|
impl.Resized = HandleResized;
|
|
|
impl.ScalingChanged = HandleScalingChanged;
|
|
|
+ impl.TransparencyLevelChanged = HandleTransparencyLevelChanged;
|
|
|
|
|
|
_keyboardNavigationHandler?.SetOwner(this);
|
|
|
_accessKeyHandler?.SetOwner(this);
|
|
|
@@ -155,6 +191,34 @@ namespace Avalonia.Controls
|
|
|
protected set { SetAndRaise(ClientSizeProperty, ref _clientSize, value); }
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Gets or sets the <see cref="WindowTransparencyLevel"/> that the TopLevel should use when possible.
|
|
|
+ /// </summary>
|
|
|
+ public WindowTransparencyLevel TransparencyLevelHint
|
|
|
+ {
|
|
|
+ get { return GetValue(TransparencyLevelHintProperty); }
|
|
|
+ set { SetValue(TransparencyLevelHintProperty, value); }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Gets the acheived <see cref="WindowTransparencyLevel"/> that the platform was able to provide.
|
|
|
+ /// </summary>
|
|
|
+ public WindowTransparencyLevel ActualTransparencyLevel
|
|
|
+ {
|
|
|
+ get => _actualTransparencyLevel;
|
|
|
+ private set => SetAndRaise(ActualTransparencyLevelProperty, ref _actualTransparencyLevel, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Gets or sets the <see cref="IBrush"/> that transparency will blend with when transparency is not supported.
|
|
|
+ /// By default this is a solid white brush.
|
|
|
+ /// </summary>
|
|
|
+ public IBrush TransparencyBackgroundFallback
|
|
|
+ {
|
|
|
+ get => GetValue(TransparencyBackgroundFallbackProperty);
|
|
|
+ set => SetValue(TransparencyBackgroundFallbackProperty, value);
|
|
|
+ }
|
|
|
+
|
|
|
public ILayoutManager LayoutManager
|
|
|
{
|
|
|
get
|
|
|
@@ -312,6 +376,39 @@ namespace Avalonia.Controls
|
|
|
LayoutHelper.InvalidateSelfAndChildrenMeasure(this);
|
|
|
}
|
|
|
|
|
|
+ private bool TransparencyLevelsMatch (WindowTransparencyLevel requested, WindowTransparencyLevel received)
|
|
|
+ {
|
|
|
+ if(requested == received)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else if(requested >= WindowTransparencyLevel.Blur && received >= WindowTransparencyLevel.Blur)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected virtual void HandleTransparencyLevelChanged(WindowTransparencyLevel transparencyLevel)
|
|
|
+ {
|
|
|
+ if(_transparencyFallbackBorder != null)
|
|
|
+ {
|
|
|
+ if(transparencyLevel == WindowTransparencyLevel.None ||
|
|
|
+ TransparencyLevelHint == WindowTransparencyLevel.None ||
|
|
|
+ !TransparencyLevelsMatch(TransparencyLevelHint, transparencyLevel))
|
|
|
+ {
|
|
|
+ _transparencyFallbackBorder.Background = TransparencyBackgroundFallback;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ _transparencyFallbackBorder.Background = Brushes.Transparent;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ActualTransparencyLevel = transparencyLevel;
|
|
|
+ }
|
|
|
+
|
|
|
/// <inheritdoc/>
|
|
|
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
|
|
{
|
|
|
@@ -321,6 +418,15 @@ namespace Avalonia.Controls
|
|
|
$"Control '{GetType().Name}' is a top level control and cannot be added as a child.");
|
|
|
}
|
|
|
|
|
|
+ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
|
|
+ {
|
|
|
+ base.OnApplyTemplate(e);
|
|
|
+
|
|
|
+ _transparencyFallbackBorder = e.NameScope.Find<Border>("PART_TransparencyFallback");
|
|
|
+
|
|
|
+ HandleTransparencyLevelChanged(PlatformImpl.TransparencyLevel);
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// Raises the <see cref="Opened"/> event.
|
|
|
/// </summary>
|