Browse Source

Merge branch 'master' into fixes/TextBoxTextAlignment

pull/3915/head
Dariusz Komosiński 6 years ago
committed by GitHub
parent
commit
59ce8b21cb
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      Documentation/build.md
  2. 53
      samples/RenderDemo/Controls/LineBoundsDemoControl.cs
  3. 3
      samples/RenderDemo/MainWindow.xaml
  4. 9
      samples/RenderDemo/Pages/LineBoundsPage.xaml
  5. 19
      samples/RenderDemo/Pages/LineBoundsPage.xaml.cs
  6. 3
      samples/RenderDemo/RenderDemo.csproj
  7. 2
      src/Avalonia.Controls/AutoCompleteBox.cs
  8. 39
      src/Avalonia.Controls/ContextMenu.cs
  9. 28
      src/Avalonia.Controls/Repeater/ItemsRepeater.cs
  10. 36
      src/Avalonia.Controls/Repeater/ViewManager.cs
  11. 35
      src/Avalonia.Controls/SelectionModel.cs
  12. 10
      src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs
  13. 10
      src/Avalonia.Layout/FlowLayoutAlgorithm.cs
  14. 20
      src/Avalonia.Layout/StackLayout.cs
  15. 2
      src/Avalonia.Layout/StackLayoutState.cs
  16. 1
      src/Avalonia.Layout/UniformGridLayout.cs
  17. 24
      src/Avalonia.Remote.Protocol/DesignMessages.cs
  18. 4
      src/Avalonia.Visuals/Media/BoxShadows.cs
  19. 6
      src/Avalonia.Visuals/Media/DrawingContext.cs
  20. 15
      src/Avalonia.Visuals/Media/PixelRect.cs
  21. 11
      src/Avalonia.Visuals/Rendering/DeferredRenderer.cs
  22. 4
      src/Avalonia.Visuals/Rendering/SceneGraph/BrushDrawOperation.cs
  23. 2
      src/Avalonia.Visuals/Rendering/SceneGraph/CustomDrawOperation.cs
  24. 4
      src/Avalonia.Visuals/Rendering/SceneGraph/DrawOperation.cs
  25. 2
      src/Avalonia.Visuals/Rendering/SceneGraph/GeometryNode.cs
  26. 2
      src/Avalonia.Visuals/Rendering/SceneGraph/GlyphRunNode.cs
  27. 2
      src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs
  28. 68
      src/Avalonia.Visuals/Rendering/SceneGraph/LineBoundsHelper.cs
  29. 2
      src/Avalonia.Visuals/Rendering/SceneGraph/LineNode.cs
  30. 4
      src/Avalonia.Visuals/Rendering/SceneGraph/OpacityMaskNode.cs
  31. 2
      src/Avalonia.Visuals/Rendering/SceneGraph/RectangleNode.cs
  32. 2
      src/Avalonia.Visuals/Rendering/SceneGraph/TextNode.cs
  33. 2
      src/Skia/Avalonia.Skia/GeometryImpl.cs
  34. 87
      tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs
  35. 40
      tests/Avalonia.Controls.UnitTests/ListBoxTests.cs
  36. 54
      tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs
  37. 46
      tests/Avalonia.Visuals.UnitTests/Media/PixelRectTests.cs
  38. 17
      tests/Avalonia.Visuals.UnitTests/Rendering/SceneGraph/DrawOperationTests.cs

2
Documentation/build.md

@ -36,7 +36,7 @@ Avalonia requires [CastXML](https://github.com/CastXML/CastXML) for XML processi
On macOS:
```
brew install castxml
brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/8a004a91a7fcd3f6620d5b01b6541ff0a640ffba/Formula/castxml.rb
```
On Debian based Linux (Debian, Ubuntu, Mint, etc):

53
samples/RenderDemo/Controls/LineBoundsDemoControl.cs

@ -0,0 +1,53 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Rendering.SceneGraph;
using Avalonia.Threading;
namespace RenderDemo.Controls
{
public class LineBoundsDemoControl : Control
{
static LineBoundsDemoControl()
{
AffectsRender<LineBoundsDemoControl>(AngleProperty);
}
public LineBoundsDemoControl()
{
var timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1 / 60.0);
timer.Tick += (sender, e) => Angle += Math.PI / 360;
timer.Start();
}
public static readonly StyledProperty<double> AngleProperty =
AvaloniaProperty.Register<LineBoundsDemoControl, double>(nameof(Angle));
public double Angle
{
get => GetValue(AngleProperty);
set => SetValue(AngleProperty, value);
}
public override void Render(DrawingContext drawingContext)
{
var lineLength = Math.Sqrt((100 * 100) + (100 * 100));
var diffX = LineBoundsHelper.CalculateAdjSide(Angle, lineLength);
var diffY = LineBoundsHelper.CalculateOppSide(Angle, lineLength);
var p1 = new Point(200, 200);
var p2 = new Point(p1.X + diffX, p1.Y + diffY);
var pen = new Pen(Brushes.Green, 20, lineCap: PenLineCap.Square);
var boundPen = new Pen(Brushes.Black);
drawingContext.DrawLine(pen, p1, p2);
drawingContext.DrawRectangle(boundPen, LineBoundsHelper.CalculateBounds(p1, p2, pen));
}
}
}

3
samples/RenderDemo/MainWindow.xaml

@ -44,6 +44,9 @@
<TabItem Header="GlyphRun">
<pages:GlyphRunPage/>
</TabItem>
<TabItem Header="LineBounds">
<pages:LineBoundsPage />
</TabItem>
</TabControl>
</DockPanel>
</Window>

9
samples/RenderDemo/Pages/LineBoundsPage.xaml

@ -0,0 +1,9 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
xmlns:controls="clr-namespace:RenderDemo.Controls"
x:Class="RenderDemo.Pages.LineBoundsPage">
<controls:LineBoundsDemoControl />
</UserControl>

19
samples/RenderDemo/Pages/LineBoundsPage.xaml.cs

@ -0,0 +1,19 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace RenderDemo.Pages
{
public class LineBoundsPage : UserControl
{
public LineBoundsPage()
{
this.InitializeComponent();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}

3
samples/RenderDemo/RenderDemo.csproj

@ -3,6 +3,9 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\src\Avalonia.Visuals\Rendering\SceneGraph\LineBoundsHelper.cs" Link="LineBoundsHelper.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Avalonia.Diagnostics\Avalonia.Diagnostics.csproj" />
<ProjectReference Include="..\..\src\Linux\Avalonia.LinuxFramebuffer\Avalonia.LinuxFramebuffer.csproj" />

2
src/Avalonia.Controls/AutoCompleteBox.cs

@ -10,6 +10,7 @@ using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Collections;
@ -1100,6 +1101,7 @@ namespace Avalonia.Controls
{
_textBoxSubscriptions =
_textBox.GetObservable(TextBox.TextProperty)
.Skip(1)
.Subscribe(_ => OnTextBoxTextChanged());
if (Text != null)

39
src/Avalonia.Controls/ContextMenu.cs

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Avalonia.Controls.Generators;
@ -9,18 +10,19 @@ using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.LogicalTree;
using Avalonia.Styling;
namespace Avalonia.Controls
{
/// <summary>
/// A control context menu.
/// </summary>
public class ContextMenu : MenuBase
public class ContextMenu : MenuBase, ISetterValue
{
private static readonly ITemplate<IPanel> DefaultPanel =
new FuncTemplate<IPanel>(() => new StackPanel { Orientation = Orientation.Vertical });
private Popup _popup;
private Control _attachedControl;
private List<Control> _attachedControls;
private IInputElement _previousFocus;
/// <summary>
@ -74,13 +76,14 @@ namespace Avalonia.Controls
if (e.OldValue is ContextMenu oldMenu)
{
control.PointerReleased -= ControlPointerReleased;
oldMenu._attachedControl = null;
oldMenu._attachedControls?.Remove(control);
((ISetLogicalParent)oldMenu._popup)?.SetParent(null);
}
if (e.NewValue is ContextMenu newMenu)
{
newMenu._attachedControl = control;
newMenu._attachedControls ??= new List<Control>();
newMenu._attachedControls.Add(control);
control.PointerReleased += ControlPointerReleased;
}
}
@ -96,18 +99,22 @@ namespace Avalonia.Controls
/// <param name="control">The control.</param>
public void Open(Control control)
{
if (control is null && _attachedControl is null)
if (control is null && (_attachedControls is null || _attachedControls.Count == 0))
{
throw new ArgumentNullException(nameof(control));
}
if (control is object && _attachedControl is object && control != _attachedControl)
if (control is object &&
_attachedControls is object &&
!_attachedControls.Contains(control))
{
throw new ArgumentException(
"Cannot show ContentMenu on a different control to the one it is attached to.",
nameof(control));
}
control ??= _attachedControls[0];
if (IsOpen)
{
return;
@ -126,7 +133,12 @@ namespace Avalonia.Controls
_popup.Closed += PopupClosed;
}
((ISetLogicalParent)_popup).SetParent(control);
if (_popup.Parent != control)
{
((ISetLogicalParent)_popup).SetParent(null);
((ISetLogicalParent)_popup).SetParent(control);
}
_popup.Child = this;
_popup.IsOpen = true;
@ -155,6 +167,17 @@ namespace Avalonia.Controls
}
}
void ISetterValue.Initialize(ISetter setter)
{
// ContextMenu can be assigned to the ContextMenu property in a setter. This overrides
// the behavior defined in Control which requires controls to be wrapped in a <template>.
if (!(setter is Setter s && s.Property == ContextMenuProperty))
{
throw new InvalidOperationException(
"Cannot use a control as a Setter value. Wrap the control in a <Template>.");
}
}
protected override IItemContainerGenerator CreateItemContainerGenerator()
{
return new MenuItemContainerGenerator(this);
@ -179,7 +202,7 @@ namespace Avalonia.Controls
SelectedIndex = -1;
IsOpen = false;
if (_attachedControl is null)
if (_attachedControls is null || _attachedControls.Count == 0)
{
((ISetLogicalParent)_popup).SetParent(null);
}

28
src/Avalonia.Controls/Repeater/ItemsRepeater.cs

@ -10,6 +10,7 @@ using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.VisualTree;
namespace Avalonia.Controls
{
@ -379,14 +380,19 @@ namespace Avalonia.Controls
{
if (property == ItemsProperty)
{
var oldEnumerable = oldValue.GetValueOrDefault<IEnumerable>();
var newEnumerable = newValue.GetValueOrDefault<IEnumerable>();
var newDataSource = newEnumerable as ItemsSourceView;
if (newEnumerable != null && newDataSource == null)
if (oldEnumerable != newEnumerable)
{
newDataSource = new ItemsSourceView(newEnumerable);
}
var newDataSource = newEnumerable as ItemsSourceView;
if (newEnumerable != null && newDataSource == null)
{
newDataSource = new ItemsSourceView(newEnumerable);
}
OnDataSourcePropertyChanged(ItemsSourceView, newDataSource);
OnDataSourcePropertyChanged(ItemsSourceView, newDataSource);
}
}
else if (property == ItemTemplateProperty)
{
@ -431,8 +437,16 @@ namespace Avalonia.Controls
private int GetElementIndexImpl(IControl element)
{
var virtInfo = TryGetVirtualizationInfo(element);
return _viewManager.GetElementIndex(virtInfo);
// Verify that element is actually a child of this ItemsRepeater
var parent = element.GetVisualParent();
if (parent == this)
{
var virtInfo = TryGetVirtualizationInfo(element);
return _viewManager.GetElementIndex(virtInfo);
}
return -1;
}
private IControl GetElementFromIndexImpl(int index)

36
src/Avalonia.Controls/Repeater/ViewManager.cs

@ -388,19 +388,24 @@ namespace Avalonia.Controls
}
case NotifyCollectionChangedAction.Reset:
if (_owner.ItemsSourceView.HasKeyIndexMapping)
// If we get multiple resets back to back before
// running layout, we dont have to clear all the elements again.
if (!_isDataSourceStableResetPending)
{
_isDataSourceStableResetPending = true;
}
if (_owner.ItemsSourceView.HasKeyIndexMapping)
{
_isDataSourceStableResetPending = true;
}
// Walk through all the elements and make sure they are cleared, they will go into
// the stable id reset pool.
foreach (var element in _owner.Children)
{
var virtInfo = ItemsRepeater.GetVirtualizationInfo(element);
if (virtInfo.IsRealized && virtInfo.AutoRecycleCandidate)
// Walk through all the elements and make sure they are cleared, they will go into
// the stable id reset pool.
foreach (var element in _owner.Children)
{
_owner.ClearElementImpl(element);
var virtInfo = ItemsRepeater.GetVirtualizationInfo(element);
if (virtInfo.IsRealized && virtInfo.AutoRecycleCandidate)
{
_owner.ClearElementImpl(element);
}
}
}
@ -441,6 +446,9 @@ namespace Avalonia.Controls
}
_resetPool.Clear();
// Flush the realized indices once the stable reset pool is cleared to start fresh.
InvalidateRealizedIndicesHeldByLayout();
}
}
@ -498,6 +506,10 @@ namespace Avalonia.Controls
var virtInfo = ItemsRepeater.GetVirtualizationInfo(element);
virtInfo.MoveOwnershipToLayoutFromUniqueIdResetPool();
UpdateElementIndex(element, virtInfo, index);
// Update realized indices
_firstRealizedElementIndexHeldByLayout = Math.Min(_firstRealizedElementIndexHeldByLayout, index);
_lastRealizedElementIndexHeldByLayout = Math.Max(_lastRealizedElementIndexHeldByLayout, index);
}
}
@ -519,6 +531,10 @@ namespace Avalonia.Controls
_pinnedPool.RemoveAt(i);
element = elementInfo.PinnedElement;
elementInfo.VirtualizationInfo.MoveOwnershipToLayoutFromPinnedPool();
// Update realized indices
_firstRealizedElementIndexHeldByLayout = Math.Min(_firstRealizedElementIndexHeldByLayout, index);
_lastRealizedElementIndexHeldByLayout = Math.Max(_lastRealizedElementIndexHeldByLayout, index);
break;
}
}

35
src/Avalonia.Controls/SelectionModel.cs

@ -20,6 +20,7 @@ namespace Avalonia.Controls
private bool _singleSelect;
private bool _autoSelect;
private int _operationCount;
private IndexPath _oldAnchorIndex;
private IReadOnlyList<IndexPath>? _selectedIndicesCached;
private IReadOnlyList<object?>? _selectedItemsCached;
private SelectionModelChildrenRequestedEventArgs? _childrenRequestedEventArgs;
@ -142,6 +143,8 @@ namespace Avalonia.Controls
}
set
{
var oldValue = AnchorIndex;
if (value != null)
{
SelectionTreeHelper.TraverseIndexPath(
@ -155,7 +158,10 @@ namespace Avalonia.Controls
_rootNode.AnchorIndex = -1;
}
RaisePropertyChanged("AnchorIndex");
if (_operationCount == 0 && oldValue != AnchorIndex)
{
RaisePropertyChanged("AnchorIndex");
}
}
}
@ -633,19 +639,18 @@ namespace Avalonia.Controls
_selectedIndicesCached = null;
_selectedItemsCached = null;
// Raise SelectionChanged event
if (e != null)
{
SelectionChanged?.Invoke(this, e);
}
RaisePropertyChanged(nameof(SelectedIndex));
RaisePropertyChanged(nameof(SelectedIndices));
RaisePropertyChanged(nameof(SelectedIndex));
RaisePropertyChanged(nameof(SelectedIndices));
if (_rootNode.Source != null)
{
RaisePropertyChanged(nameof(SelectedItem));
RaisePropertyChanged(nameof(SelectedItems));
if (_rootNode.Source != null)
{
RaisePropertyChanged(nameof(SelectedItem));
RaisePropertyChanged(nameof(SelectedItems));
}
}
}
@ -785,6 +790,7 @@ namespace Avalonia.Controls
{
if (_operationCount++ == 0)
{
_oldAnchorIndex = AnchorIndex;
_rootNode.BeginOperation();
}
}
@ -808,13 +814,16 @@ namespace Avalonia.Controls
var changeSet = new SelectionModelChangeSet(changes);
e = changeSet.CreateEventArgs();
}
}
OnSelectionChanged(e);
OnSelectionChanged(e);
if (_oldAnchorIndex != AnchorIndex)
{
RaisePropertyChanged(nameof(AnchorIndex));
}
if (_operationCount == 0)
{
_rootNode.Cleanup();
_oldAnchorIndex = default;
}
}

10
src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs

@ -234,18 +234,10 @@ namespace Avalonia.DesignerSupport.Remote
}
catch (Exception e)
{
var xmlException = e as XmlException;
s_transport.Send(new UpdateXamlResultMessage
{
Error = e.ToString(),
Exception = new ExceptionDetails
{
ExceptionType = e.GetType().FullName,
Message = e.Message.ToString(),
LineNumber = xmlException?.LineNumber,
LinePosition = xmlException?.LinePosition,
}
Exception = new ExceptionDetails(e),
});
}
}

10
src/Avalonia.Layout/FlowLayoutAlgorithm.cs

@ -74,6 +74,7 @@ namespace Avalonia.Layout
double lineSpacing,
int maxItemsPerLine,
ScrollOrientation orientation,
bool disableVirtualization,
string layoutId)
{
_orientation.ScrollOrientation = orientation;
@ -95,14 +96,14 @@ namespace Avalonia.Layout
_elementManager.OnBeginMeasure(orientation);
int anchorIndex = GetAnchorIndex(availableSize, isWrapping, minItemSpacing, layoutId);
Generate(GenerateDirection.Forward, anchorIndex, availableSize, minItemSpacing, lineSpacing, maxItemsPerLine, layoutId);
Generate(GenerateDirection.Backward, anchorIndex, availableSize, minItemSpacing, lineSpacing, maxItemsPerLine, layoutId);
Generate(GenerateDirection.Forward, anchorIndex, availableSize, minItemSpacing, lineSpacing, maxItemsPerLine, disableVirtualization, layoutId);
Generate(GenerateDirection.Backward, anchorIndex, availableSize, minItemSpacing, lineSpacing, maxItemsPerLine, disableVirtualization, layoutId);
if (isWrapping && IsReflowRequired())
{
var firstElementBounds = _elementManager.GetLayoutBoundsForRealizedIndex(0);
_orientation.SetMinorStart(ref firstElementBounds, 0);
_elementManager.SetLayoutBoundsForRealizedIndex(0, firstElementBounds);
Generate(GenerateDirection.Forward, 0 /*anchorIndex*/, availableSize, minItemSpacing, lineSpacing, maxItemsPerLine, layoutId);
Generate(GenerateDirection.Forward, 0 /*anchorIndex*/, availableSize, minItemSpacing, lineSpacing, maxItemsPerLine, disableVirtualization, layoutId);
}
RaiseLineArranged();
@ -273,6 +274,7 @@ namespace Avalonia.Layout
double minItemSpacing,
double lineSpacing,
int maxItemsPerLine,
bool disableVirtualization,
string layoutId)
{
if (anchorIndex != -1)
@ -288,7 +290,7 @@ namespace Avalonia.Layout
bool lineNeedsReposition = false;
while (_elementManager.IsIndexValidInData(currentIndex) &&
ShouldContinueFillingUpSpace(previousIndex, direction))
(disableVirtualization || ShouldContinueFillingUpSpace(previousIndex, direction)))
{
// Ensure layout element.
_elementManager.EnsureElementRealized(direction == GenerateDirection.Forward, currentIndex, layoutId);

20
src/Avalonia.Layout/StackLayout.cs

@ -14,6 +14,12 @@ namespace Avalonia.Layout
/// </summary>
public class StackLayout : VirtualizingLayout, IFlowLayoutAlgorithmDelegates
{
/// <summary>
/// Defines the <see cref="DisableVirtualization"/> property.
/// </summary>
public static readonly StyledProperty<bool> DisableVirtualizationProperty =
AvaloniaProperty.Register<StackLayout, bool>(nameof(DisableVirtualization));
/// <summary>
/// Defines the <see cref="Orientation"/> property.
/// </summary>
@ -36,6 +42,15 @@ namespace Avalonia.Layout
LayoutId = "StackLayout";
}
/// <summary>
/// Gets or sets a value indicating whether virtualization is disabled on the layout.
/// </summary>
public bool DisableVirtualization
{
get => GetValue(DisableVirtualizationProperty);
set => SetValue(DisableVirtualizationProperty, value);
}
/// <summary>
/// Gets or sets the axis along which items are laid out.
/// </summary>
@ -262,6 +277,8 @@ namespace Avalonia.Layout
protected internal override Size MeasureOverride(VirtualizingLayoutContext context, Size availableSize)
{
((StackLayoutState)context.LayoutState).OnMeasureStart();
var desiredSize = GetFlowAlgorithm(context).Measure(
availableSize,
context,
@ -270,6 +287,7 @@ namespace Avalonia.Layout
Spacing,
int.MaxValue,
_orientation.ScrollOrientation,
DisableVirtualization,
LayoutId);
return new Size(desiredSize.Width, desiredSize.Height);
@ -284,8 +302,6 @@ namespace Avalonia.Layout
FlowLayoutAlgorithm.LineAlignment.Start,
LayoutId);
((StackLayoutState)context.LayoutState).OnArrangeLayoutEnd();
return new Size(value.Width, value.Height);
}

2
src/Avalonia.Layout/StackLayoutState.cs

@ -56,6 +56,6 @@ namespace Avalonia.Layout
MaxArrangeBounds = Math.Max(MaxArrangeBounds, minorSize);
}
internal void OnArrangeLayoutEnd() => MaxArrangeBounds = 0;
internal void OnMeasureStart() => MaxArrangeBounds = 0;
}
}

1
src/Avalonia.Layout/UniformGridLayout.cs

@ -433,6 +433,7 @@ namespace Avalonia.Layout
LineSpacing,
_maximumRowsOrColumns,
_orientation.ScrollOrientation,
false,
LayoutId);
// If after Measure the first item is in the realization rect, then we revoke grid state's ownership,

24
src/Avalonia.Remote.Protocol/DesignMessages.cs

@ -1,4 +1,7 @@
using System;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Xml;
namespace Avalonia.Remote.Protocol.Designer
{
@ -26,6 +29,27 @@ namespace Avalonia.Remote.Protocol.Designer
public class ExceptionDetails
{
public ExceptionDetails()
{
}
public ExceptionDetails(Exception e)
{
if (e is TargetInvocationException)
{
e = e.InnerException;
}
ExceptionType = e.GetType().Name;
Message = e.Message;
if (e is XmlException xml)
{
LineNumber = xml.LineNumber;
LinePosition = xml.LinePosition;
}
}
public string ExceptionType { get; set; }
public string Message { get; set; }
public int? LineNumber { get; set; }

4
src/Avalonia.Visuals/Media/BoxShadows.cs

@ -21,7 +21,7 @@ namespace Avalonia.Media
{
_first = shadow;
_list = null;
Count = 1;
Count = _first.IsEmpty ? 0 : 1;
}
public BoxShadows(BoxShadow first, BoxShadow[] rest)
@ -105,8 +105,6 @@ namespace Avalonia.Media
return false;
}
}
public static implicit operator BoxShadows(BoxShadow shadow) => new BoxShadows(shadow);
public bool Equals(BoxShadows other)
{

6
src/Avalonia.Visuals/Media/DrawingContext.cs

@ -141,13 +141,13 @@ namespace Avalonia.Media
/// <param name="radiusY">The radius in the Y dimension of the rounded corners.
/// This value will be clamped to the range of 0 to Height/2
/// </param>
/// <param name="boxShadow">Box shadow effect parameters</param>
/// <param name="boxShadows">Box shadow effect parameters</param>
/// <remarks>
/// The brush and the pen can both be null. If the brush is null, then no fill is performed.
/// If the pen is null, then no stoke is performed. If both the pen and the brush are null, then the drawing is not visible.
/// </remarks>
public void DrawRectangle(IBrush brush, IPen pen, Rect rect, double radiusX = 0, double radiusY = 0,
BoxShadow boxShadow = default)
BoxShadows boxShadows = default)
{
if (brush == null && !PenIsVisible(pen))
{
@ -164,7 +164,7 @@ namespace Avalonia.Media
radiusY = Math.Min(radiusY, rect.Height / 2);
}
PlatformImpl.DrawRectangle(brush, pen, new RoundedRect(rect, radiusX, radiusY), boxShadow);
PlatformImpl.DrawRectangle(brush, pen, new RoundedRect(rect, radiusX, radiusY), boxShadows);
}
/// <summary>

15
src/Avalonia.Visuals/Media/PixelRect.cs

@ -377,7 +377,7 @@ namespace Avalonia
/// <returns>The device-independent rect.</returns>
public static PixelRect FromRect(Rect rect, double scale) => new PixelRect(
PixelPoint.FromPoint(rect.Position, scale),
PixelSize.FromSize(rect.Size, scale));
FromPointCeiling(rect.BottomRight, new Vector(scale, scale)));
/// <summary>
/// Converts a <see cref="Rect"/> to device pixels using the specified scaling factor.
@ -387,7 +387,7 @@ namespace Avalonia
/// <returns>The device-independent point.</returns>
public static PixelRect FromRect(Rect rect, Vector scale) => new PixelRect(
PixelPoint.FromPoint(rect.Position, scale),
PixelSize.FromSize(rect.Size, scale));
FromPointCeiling(rect.BottomRight, scale));
/// <summary>
/// Converts a <see cref="Rect"/> to device pixels using the specified dots per inch (DPI).
@ -397,7 +397,7 @@ namespace Avalonia
/// <returns>The device-independent point.</returns>
public static PixelRect FromRectWithDpi(Rect rect, double dpi) => new PixelRect(
PixelPoint.FromPointWithDpi(rect.Position, dpi),
PixelSize.FromSizeWithDpi(rect.Size, dpi));
FromPointCeiling(rect.BottomRight, new Vector(dpi / 96, dpi / 96)));
/// <summary>
/// Converts a <see cref="Rect"/> to device pixels using the specified dots per inch (DPI).
@ -407,7 +407,7 @@ namespace Avalonia
/// <returns>The device-independent point.</returns>
public static PixelRect FromRectWithDpi(Rect rect, Vector dpi) => new PixelRect(
PixelPoint.FromPointWithDpi(rect.Position, dpi),
PixelSize.FromSizeWithDpi(rect.Size, dpi));
FromPointCeiling(rect.BottomRight, dpi / 96));
/// <summary>
/// Returns the string representation of the rectangle.
@ -441,5 +441,12 @@ namespace Avalonia
);
}
}
private static PixelPoint FromPointCeiling(Point point, Vector scale)
{
return new PixelPoint(
(int)Math.Ceiling(point.X * scale.X),
(int)Math.Ceiling(point.Y * scale.Y));
}
}
}

11
src/Avalonia.Visuals/Rendering/DeferredRenderer.cs

@ -443,11 +443,12 @@ namespace Avalonia.Rendering
private static Rect SnapToDevicePixels(Rect rect, double scale)
{
return new Rect(
Math.Floor(rect.X * scale) / scale,
Math.Floor(rect.Y * scale) / scale,
Math.Ceiling(rect.Width * scale) / scale,
Math.Ceiling(rect.Height * scale) / scale);
new Point(
Math.Floor(rect.X * scale) / scale,
Math.Floor(rect.Y * scale) / scale),
new Point(
Math.Ceiling(rect.Right * scale) / scale,
Math.Ceiling(rect.Bottom * scale) / scale));
}
private void RenderOverlay(Scene scene, ref IDrawingContextImpl parentContent)

4
src/Avalonia.Visuals/Rendering/SceneGraph/BrushDrawOperation.cs

@ -9,8 +9,8 @@ namespace Avalonia.Rendering.SceneGraph
/// </summary>
internal abstract class BrushDrawOperation : DrawOperation
{
public BrushDrawOperation(Rect bounds, Matrix transform, IPen pen)
: base(bounds, transform, pen)
public BrushDrawOperation(Rect bounds, Matrix transform)
: base(bounds, transform)
{
}

2
src/Avalonia.Visuals/Rendering/SceneGraph/CustomDrawOperation.cs

@ -9,7 +9,7 @@ namespace Avalonia.Rendering.SceneGraph
public Matrix Transform { get; }
public ICustomDrawOperation Custom { get; }
public CustomDrawOperation(ICustomDrawOperation custom, Matrix transform)
: base(custom.Bounds, transform, null)
: base(custom.Bounds, transform)
{
Transform = transform;
Custom = custom;

4
src/Avalonia.Visuals/Rendering/SceneGraph/DrawOperation.cs

@ -9,9 +9,9 @@ namespace Avalonia.Rendering.SceneGraph
/// </summary>
internal abstract class DrawOperation : IDrawOperation
{
public DrawOperation(Rect bounds, Matrix transform, IPen pen)
public DrawOperation(Rect bounds, Matrix transform)
{
bounds = bounds.Inflate((pen?.Thickness ?? 0) / 2).TransformToAABB(transform);
bounds = bounds.TransformToAABB(transform);
Bounds = new Rect(
new Point(Math.Floor(bounds.X), Math.Floor(bounds.Y)),
new Point(Math.Ceiling(bounds.Right), Math.Ceiling(bounds.Bottom)));

2
src/Avalonia.Visuals/Rendering/SceneGraph/GeometryNode.cs

@ -24,7 +24,7 @@ namespace Avalonia.Rendering.SceneGraph
IPen pen,
IGeometryImpl geometry,
IDictionary<IVisual, Scene> childScenes = null)
: base(geometry.GetRenderBounds(pen), transform, null)
: base(geometry.GetRenderBounds(pen), transform)
{
Transform = transform;
Brush = brush?.ToImmutable();

2
src/Avalonia.Visuals/Rendering/SceneGraph/GlyphRunNode.cs

@ -25,7 +25,7 @@ namespace Avalonia.Rendering.SceneGraph
GlyphRun glyphRun,
Point baselineOrigin,
IDictionary<IVisual, Scene> childScenes = null)
: base(glyphRun.Bounds, transform, null)
: base(glyphRun.Bounds, transform)
{
Transform = transform;
Foreground = foreground?.ToImmutable();

2
src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs

@ -19,7 +19,7 @@ namespace Avalonia.Rendering.SceneGraph
/// <param name="destRect">The destination rect.</param>
/// <param name="bitmapInterpolationMode">The bitmap interpolation mode.</param>
public ImageNode(Matrix transform, IRef<IBitmapImpl> source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode)
: base(destRect, transform, null)
: base(destRect, transform)
{
Transform = transform;
Source = source.Clone();

68
src/Avalonia.Visuals/Rendering/SceneGraph/LineBoundsHelper.cs

@ -0,0 +1,68 @@
using System;
using Avalonia.Media;
namespace Avalonia.Rendering.SceneGraph
{
internal static class LineBoundsHelper
{
private static double CalculateAngle(Point p1, Point p2)
{
var xDiff = p2.X - p1.X;
var yDiff = p2.Y - p1.Y;
return Math.Atan2(yDiff, xDiff);
}
internal static double CalculateOppSide(double angle, double hyp)
{
return Math.Sin(angle) * hyp;
}
internal static double CalculateAdjSide(double angle, double hyp)
{
return Math.Cos(angle) * hyp;
}
private static (Point p1, Point p2) TranslatePointsAlongTangent(Point p1, Point p2, double angle, double distance)
{
var xDiff = CalculateOppSide(angle, distance);
var yDiff = CalculateAdjSide(angle, distance);
var c1 = new Point(p1.X + xDiff, p1.Y - yDiff);
var c2 = new Point(p1.X - xDiff, p1.Y + yDiff);
var c3 = new Point(p2.X + xDiff, p2.Y - yDiff);
var c4 = new Point(p2.X - xDiff, p2.Y + yDiff);
var minX = Math.Min(c1.X, Math.Min(c2.X, Math.Min(c3.X, c4.X)));
var minY = Math.Min(c1.Y, Math.Min(c2.Y, Math.Min(c3.Y, c4.Y)));
var maxX = Math.Max(c1.X, Math.Max(c2.X, Math.Max(c3.X, c4.X)));
var maxY = Math.Max(c1.Y, Math.Max(c2.Y, Math.Max(c3.Y, c4.Y)));
return (new Point(minX, minY), new Point(maxX, maxY));
}
private static Rect CalculateBounds(Point p1, Point p2, double thickness, double angleToCorner)
{
var pts = TranslatePointsAlongTangent(p1, p2, angleToCorner, thickness / 2);
return new Rect(pts.p1, pts.p2);
}
public static Rect CalculateBounds(Point p1, Point p2, IPen p)
{
var radians = CalculateAngle(p1, p2);
if (p.LineCap != PenLineCap.Flat)
{
var pts = TranslatePointsAlongTangent(p1, p2, radians - Math.PI / 2, p.Thickness / 2);
return CalculateBounds(pts.p1, pts.p2, p.Thickness, radians);
}
else
{
return CalculateBounds(p1, p2, p.Thickness, radians);
}
}
}
}

2
src/Avalonia.Visuals/Rendering/SceneGraph/LineNode.cs

@ -25,7 +25,7 @@ namespace Avalonia.Rendering.SceneGraph
Point p1,
Point p2,
IDictionary<IVisual, Scene> childScenes = null)
: base(new Rect(p1, p2), transform, pen)
: base(LineBoundsHelper.CalculateBounds(p1, p2, pen), transform)
{
Transform = transform;
Pen = pen?.ToImmutable();

4
src/Avalonia.Visuals/Rendering/SceneGraph/OpacityMaskNode.cs

@ -18,7 +18,7 @@ namespace Avalonia.Rendering.SceneGraph
/// <param name="bounds">The bounds of the mask.</param>
/// <param name="childScenes">Child scenes for drawing visual brushes.</param>
public OpacityMaskNode(IBrush mask, Rect bounds, IDictionary<IVisual, Scene> childScenes = null)
: base(Rect.Empty, Matrix.Identity, null)
: base(Rect.Empty, Matrix.Identity)
{
Mask = mask?.ToImmutable();
MaskBounds = bounds;
@ -30,7 +30,7 @@ namespace Avalonia.Rendering.SceneGraph
/// opacity mask pop.
/// </summary>
public OpacityMaskNode()
: base(Rect.Empty, Matrix.Identity, null)
: base(Rect.Empty, Matrix.Identity)
{
}

2
src/Avalonia.Visuals/Rendering/SceneGraph/RectangleNode.cs

@ -28,7 +28,7 @@ namespace Avalonia.Rendering.SceneGraph
RoundedRect rect,
BoxShadows boxShadows,
IDictionary<IVisual, Scene> childScenes = null)
: base(boxShadows.TransformBounds(rect.Rect), transform, pen)
: base(boxShadows.TransformBounds(rect.Rect).Inflate((pen?.Thickness ?? 0) / 2), transform)
{
Transform = transform;
Brush = brush?.ToImmutable();

2
src/Avalonia.Visuals/Rendering/SceneGraph/TextNode.cs

@ -24,7 +24,7 @@ namespace Avalonia.Rendering.SceneGraph
Point origin,
IFormattedTextImpl text,
IDictionary<IVisual, Scene> childScenes = null)
: base(text.Bounds.Translate(origin), transform, null)
: base(text.Bounds.Translate(origin), transform)
{
Transform = transform;
Foreground = foreground?.ToImmutable();

2
src/Skia/Avalonia.Skia/GeometryImpl.cs

@ -95,7 +95,7 @@ namespace Avalonia.Skia
UpdatePathCache(strokeWidth);
}
return _pathCache.CachedGeometryRenderBounds.Inflate(strokeWidth / 2.0);
return _pathCache.CachedGeometryRenderBounds;
}
/// <inheritdoc />

87
tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs

@ -1,7 +1,10 @@
using System;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Avalonia.Markup.Xaml.MarkupExtensions;
using Avalonia.Platform;
using Avalonia.UnitTests;
using Castle.DynamicProxy.Generators;
using Moq;
using Xunit;
@ -168,6 +171,90 @@ namespace Avalonia.Controls.UnitTests
}
}
[Fact]
public void Context_Menu_In_Resources_Can_Be_Shared()
{
using (Application())
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Window.Resources>
<ContextMenu x:Key='contextMenu'>
<MenuItem>Foo</MenuItem>
</ContextMenu>
</Window.Resources>
<StackPanel>
<TextBlock Name='target1' ContextMenu='{StaticResource contextMenu}'/>
<TextBlock Name='target2' ContextMenu='{StaticResource contextMenu}'/>
</StackPanel>
</Window>";
var loader = new AvaloniaXamlLoader();
var window = (Window)loader.Load(xaml);
var target1 = window.Find<TextBlock>("target1");
var target2 = window.Find<TextBlock>("target2");
var mouse = new MouseTestHelper();
Assert.NotNull(target1.ContextMenu);
Assert.NotNull(target2.ContextMenu);
Assert.Same(target1.ContextMenu, target2.ContextMenu);
window.Show();
var menu = target1.ContextMenu;
mouse.Click(target1, MouseButton.Right);
Assert.True(menu.IsOpen);
mouse.Click(target2, MouseButton.Right);
Assert.True(menu.IsOpen);
}
}
[Fact]
public void Context_Menu_Can_Be_Set_In_Style()
{
using (Application())
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Window.Styles>
<Style Selector='TextBlock'>
<Setter Property='ContextMenu'>
<ContextMenu>
<MenuItem>Foo</MenuItem>
</ContextMenu>
</Setter>
</Style>
</Window.Styles>
<StackPanel>
<TextBlock Name='target1'/>
<TextBlock Name='target2'/>
</StackPanel>
</Window>";
var loader = new AvaloniaXamlLoader();
var window = (Window)loader.Load(xaml);
var target1 = window.Find<TextBlock>("target1");
var target2 = window.Find<TextBlock>("target2");
var mouse = new MouseTestHelper();
Assert.NotNull(target1.ContextMenu);
Assert.NotNull(target2.ContextMenu);
Assert.Same(target1.ContextMenu, target2.ContextMenu);
window.Show();
var menu = target1.ContextMenu;
mouse.Click(target1, MouseButton.Right);
Assert.True(menu.IsOpen);
mouse.Click(target2, MouseButton.Right);
Assert.True(menu.IsOpen);
}
}
[Fact(Skip = "The only reason this test was 'passing' before was that the author forgot to call Window.ApplyTemplate()")]
public void Cancelling_Closing_Leaves_ContextMenuOpen()
{

40
tests/Avalonia.Controls.UnitTests/ListBoxTests.cs

@ -367,6 +367,46 @@ namespace Avalonia.Controls.UnitTests
}
}
[Fact]
public void Clicking_Item_Should_Raise_BringIntoView_For_Correct_Control()
{
// Issue #3934
var items = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToArray();
var target = new ListBox
{
Template = ListBoxTemplate(),
Items = items,
ItemTemplate = new FuncDataTemplate<string>((x, _) => new TextBlock { Height = 10 }),
SelectionMode = SelectionMode.AlwaysSelected,
VirtualizationMode = ItemVirtualizationMode.None,
};
Prepare(target);
// First an item that is not index 0 must be selected.
_mouse.Click(target.Presenter.Panel.Children[1]);
Assert.Equal(new IndexPath(1), target.Selection.AnchorIndex);
// We're going to be clicking on item 9.
var item = (ListBoxItem)target.Presenter.Panel.Children[9];
var raised = 0;
// Make sure a RequestBringIntoView event is raised for item 9. It won't be handled
// by the ScrollContentPresenter as the item is already visible, so we don't need
// handledEventsToo: true. Issue #3934 failed here because item 0 was being scrolled
// into view due to SelectionMode.AlwaysSelected.
target.AddHandler(Control.RequestBringIntoViewEvent, (s, e) =>
{
Assert.Same(item, e.TargetObject);
++raised;
});
// Click item 9.
_mouse.Click(item);
Assert.Equal(1, raised);
}
private FuncControlTemplate ListBoxTemplate()
{
return new FuncControlTemplate<ListBox>((parent, scope) =>

54
tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs

@ -1458,6 +1458,60 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(1, raised);
}
[Fact]
public void Batch_Update_Does_Not_Raise_PropertyChanged_Until_Operation_Finished()
{
var data = new[] { "foo", "bar", "baz", "qux" };
var target = new SelectionModel { Source = data };
var raised = 0;
target.SelectedIndex = new IndexPath(1);
Assert.Equal(new IndexPath(1), target.AnchorIndex);
target.PropertyChanged += (s, e) => ++raised;
using (target.Update())
{
target.ClearSelection();
Assert.Equal(0, raised);
target.AnchorIndex = new IndexPath(2);
Assert.Equal(0, raised);
target.SelectedIndex = new IndexPath(3);
Assert.Equal(0, raised);
}
Assert.Equal(new IndexPath(3), target.AnchorIndex);
Assert.Equal(5, raised);
}
[Fact]
public void Batch_Update_Does_Not_Raise_PropertyChanged_If_Nothing_Changed()
{
var data = new[] { "foo", "bar", "baz", "qux" };
var target = new SelectionModel { Source = data };
var raised = 0;
target.SelectedIndex = new IndexPath(1);
Assert.Equal(new IndexPath(1), target.AnchorIndex);
target.PropertyChanged += (s, e) => ++raised;
using (target.Update())
{
target.ClearSelection();
target.SelectedIndex = new IndexPath(1);
}
Assert.Equal(0, raised);
}
[Fact]
public void AutoSelect_Selects_When_Enabled()
{

46
tests/Avalonia.Visuals.UnitTests/Media/PixelRectTests.cs

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
namespace Avalonia.Visuals.UnitTests.Media
{
public class PixelRectTests
{
[Fact]
public void FromRect_Snaps_To_Device_Pixels()
{
var rect = new Rect(189, 189, 26, 164);
var result = PixelRect.FromRect(rect, 1.5);
Assert.Equal(new PixelRect(283, 283, 40, 247), result);
}
[Fact]
public void FromRect_Vector_Snaps_To_Device_Pixels()
{
var rect = new Rect(189, 189, 26, 164);
var result = PixelRect.FromRect(rect, new Vector(1.5, 1.5));
Assert.Equal(new PixelRect(283, 283, 40, 247), result);
}
[Fact]
public void FromRectWithDpi_Snaps_To_Device_Pixels()
{
var rect = new Rect(189, 189, 26, 164);
var result = PixelRect.FromRectWithDpi(rect, 144);
Assert.Equal(new PixelRect(283, 283, 40, 247), result);
}
[Fact]
public void FromRectWithDpi_Vector_Snaps_To_Device_Pixels()
{
var rect = new Rect(189, 189, 26, 164);
var result = PixelRect.FromRectWithDpi(rect, new Vector(144, 144));
Assert.Equal(new PixelRect(283, 283, 40, 247), result);
}
}
}

17
tests/Avalonia.Visuals.UnitTests/Rendering/SceneGraph/DrawOperationTests.cs

@ -35,7 +35,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering.SceneGraph
double expectedWidth,
double expectedHeight)
{
var target = new TestDrawOperation(
var target = new TestRectangleDrawOperation(
new Rect(x, y, width, height),
Matrix.CreateScale(scaleX, scaleY),
penThickness.HasValue ? new Pen(Brushes.Black, penThickness.Value) : null);
@ -74,10 +74,23 @@ namespace Avalonia.Visuals.UnitTests.Rendering.SceneGraph
geometryNode.HitTest(new Point());
}
private class TestRectangleDrawOperation : RectangleNode
{
public TestRectangleDrawOperation(Rect bounds, Matrix transform, Pen pen)
: base(transform, pen.Brush, pen, bounds, new BoxShadows())
{
}
public override bool HitTest(Point p) => false;
public override void Render(IDrawingContextImpl context) { }
}
private class TestDrawOperation : DrawOperation
{
public TestDrawOperation(Rect bounds, Matrix transform, Pen pen)
:base(bounds, transform, pen)
:base(bounds, transform)
{
}

Loading…
Cancel
Save