Browse Source

Added invalidation to LayoutThickness property in Border and ContentPresenter and implemented minor review fixes.

pull/7867/head
AndrejBunjac 5 years ago
parent
commit
a39de875aa
  1. 38
      src/Avalonia.Controls/Border.cs
  2. 29
      src/Avalonia.Controls/Presenters/ContentPresenter.cs
  3. 2
      src/Avalonia.Layout/Layoutable.cs

38
src/Avalonia.Controls/Border.cs

@ -1,8 +1,10 @@
using System;
using Avalonia.Collections;
using Avalonia.Controls.Shapes;
using Avalonia.Controls.Utils;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Utilities;
using Avalonia.VisualTree;
namespace Avalonia.Controls
@ -88,6 +90,18 @@ namespace Avalonia.Controls
AffectsMeasure<Border>(BorderThicknessProperty);
}
protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change)
{
base.OnPropertyChanged(change);
switch (change.Property.Name)
{
case nameof(UseLayoutRounding):
case nameof(BorderThickness):
_layoutThickness = null;
break;
}
}
/// <summary>
/// Gets or sets a brush with which to paint the background.
/// </summary>
@ -169,29 +183,39 @@ namespace Avalonia.Controls
set => SetValue(BoxShadowProperty, value);
}
private Thickness _layoutThickness = default;
private Thickness? _layoutThickness;
private double _scale;
private Thickness LayoutThickness
{
get
{
if (_layoutThickness == default)
VerifyScale();
if (_layoutThickness == null)
{
var borderThickness = BorderThickness;
if (UseLayoutRounding)
{
var scale = LayoutHelper.GetLayoutScale(this);
borderThickness = LayoutHelper.RoundLayoutThickness(BorderThickness, scale, scale);
}
borderThickness = LayoutHelper.RoundLayoutThickness(BorderThickness, _scale, _scale);
_layoutThickness = borderThickness;
}
return _layoutThickness;
return _layoutThickness.Value;
}
}
private void VerifyScale()
{
var currentScale = LayoutHelper.GetLayoutScale(this);
if (MathUtilities.AreClose(currentScale, _scale))
return;
_scale = currentScale;
_layoutThickness = null;
}
/// <summary>
/// Renders the control.
/// </summary>

29
src/Avalonia.Controls/Presenters/ContentPresenter.cs

@ -8,6 +8,7 @@ using Avalonia.Layout;
using Avalonia.LogicalTree;
using Avalonia.Media;
using Avalonia.Metadata;
using Avalonia.Utilities;
namespace Avalonia.Controls.Presenters
{
@ -249,6 +250,10 @@ namespace Avalonia.Controls.Presenters
case nameof(TemplatedParent):
TemplatedParentChanged(change);
break;
case nameof(UseLayoutRounding):
case nameof(BorderThickness):
_layoutThickness = null;
break;
}
}
@ -329,29 +334,39 @@ namespace Avalonia.Controls.Presenters
InvalidateMeasure();
}
private Thickness _layoutThickness = default;
private Thickness? _layoutThickness;
private double _scale;
private Thickness LayoutThickness
{
get
{
if (_layoutThickness == default)
VerifyScale();
if (_layoutThickness == null)
{
var borderThickness = BorderThickness;
if (UseLayoutRounding)
{
var scale = LayoutHelper.GetLayoutScale(this);
borderThickness = LayoutHelper.RoundLayoutThickness(BorderThickness, scale, scale);
}
borderThickness = LayoutHelper.RoundLayoutThickness(BorderThickness, _scale, _scale);
_layoutThickness = borderThickness;
}
return _layoutThickness;
return _layoutThickness.Value;
}
}
private void VerifyScale()
{
var currentScale = LayoutHelper.GetLayoutScale(this);
if (MathUtilities.AreClose(currentScale, _scale))
return;
_scale = currentScale;
_layoutThickness = null;
}
/// <inheritdoc/>
public override void Render(DrawingContext context)
{

2
src/Avalonia.Layout/Layoutable.cs

@ -653,7 +653,7 @@ namespace Avalonia.Layout
// Margin has to be treated separately because the layout rounding function is not linear
// f(a + b) != f(a) + f(b)
// If the margin isn't pre-rounded some sizes will be offset by 1 pixel in certain scales.
if (UseLayoutRounding)
if (useLayoutRounding)
{
margin = LayoutHelper.RoundLayoutThickness(margin, scale, scale);
}

Loading…
Cancel
Save