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.Collections;
using Avalonia.Controls.Shapes; using Avalonia.Controls.Shapes;
using Avalonia.Controls.Utils; using Avalonia.Controls.Utils;
using Avalonia.Layout; using Avalonia.Layout;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Utilities;
using Avalonia.VisualTree; using Avalonia.VisualTree;
namespace Avalonia.Controls namespace Avalonia.Controls
@ -88,6 +90,18 @@ namespace Avalonia.Controls
AffectsMeasure<Border>(BorderThicknessProperty); 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> /// <summary>
/// Gets or sets a brush with which to paint the background. /// Gets or sets a brush with which to paint the background.
/// </summary> /// </summary>
@ -169,29 +183,39 @@ namespace Avalonia.Controls
set => SetValue(BoxShadowProperty, value); set => SetValue(BoxShadowProperty, value);
} }
private Thickness _layoutThickness = default; private Thickness? _layoutThickness;
private double _scale;
private Thickness LayoutThickness private Thickness LayoutThickness
{ {
get get
{ {
if (_layoutThickness == default) VerifyScale();
if (_layoutThickness == null)
{ {
var borderThickness = BorderThickness; var borderThickness = BorderThickness;
if (UseLayoutRounding) if (UseLayoutRounding)
{ borderThickness = LayoutHelper.RoundLayoutThickness(BorderThickness, _scale, _scale);
var scale = LayoutHelper.GetLayoutScale(this);
borderThickness = LayoutHelper.RoundLayoutThickness(BorderThickness, scale, scale);
}
_layoutThickness = borderThickness; _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> /// <summary>
/// Renders the control. /// Renders the control.
/// </summary> /// </summary>

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

@ -8,6 +8,7 @@ using Avalonia.Layout;
using Avalonia.LogicalTree; using Avalonia.LogicalTree;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Metadata; using Avalonia.Metadata;
using Avalonia.Utilities;
namespace Avalonia.Controls.Presenters namespace Avalonia.Controls.Presenters
{ {
@ -249,6 +250,10 @@ namespace Avalonia.Controls.Presenters
case nameof(TemplatedParent): case nameof(TemplatedParent):
TemplatedParentChanged(change); TemplatedParentChanged(change);
break; break;
case nameof(UseLayoutRounding):
case nameof(BorderThickness):
_layoutThickness = null;
break;
} }
} }
@ -329,29 +334,39 @@ namespace Avalonia.Controls.Presenters
InvalidateMeasure(); InvalidateMeasure();
} }
private Thickness _layoutThickness = default; private Thickness? _layoutThickness;
private double _scale;
private Thickness LayoutThickness private Thickness LayoutThickness
{ {
get get
{ {
if (_layoutThickness == default) VerifyScale();
if (_layoutThickness == null)
{ {
var borderThickness = BorderThickness; var borderThickness = BorderThickness;
if (UseLayoutRounding) if (UseLayoutRounding)
{ borderThickness = LayoutHelper.RoundLayoutThickness(BorderThickness, _scale, _scale);
var scale = LayoutHelper.GetLayoutScale(this);
borderThickness = LayoutHelper.RoundLayoutThickness(BorderThickness, scale, scale);
}
_layoutThickness = borderThickness; _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/> /// <inheritdoc/>
public override void Render(DrawingContext context) 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 // Margin has to be treated separately because the layout rounding function is not linear
// f(a + b) != f(a) + f(b) // 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 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); margin = LayoutHelper.RoundLayoutThickness(margin, scale, scale);
} }

Loading…
Cancel
Save