Browse Source

Merge branch 'master' into fix-styledproperty-defaultvalue-box

pull/2935/head
Steven Kirk 7 years ago
committed by GitHub
parent
commit
ecee8ace80
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      scripts/ReplaceNugetCache.sh
  2. 7
      src/Avalonia.Diagnostics/DevTools.xaml.cs
  3. 19
      src/Avalonia.Native/MacOSMountedVolumeInfoProvider.cs
  4. 160
      src/Avalonia.Visuals/Vector.cs
  5. 9
      src/Windows/Avalonia.Win32/WindowsMountedVolumeInfoListener.cs
  6. 112
      tests/Avalonia.Visuals.UnitTests/VectorTests.cs

1
scripts/ReplaceNugetCache.sh

@ -2,7 +2,6 @@
cp ../samples/ControlCatalog.NetCore/bin/Debug/netcoreapp2.0/Avalonia**.dll ~/.nuget/packages/avalonia/$1/lib/netcoreapp2.0/
cp ../samples/ControlCatalog.NetCore/bin/Debug/netcoreapp2.0/Avalonia**.dll ~/.nuget/packages/avalonia/$1/lib/netstandard2.0/
cp ../samples/ControlCatalog.NetCore/bin/Debug/netcoreapp2.0/Avalonia**.dll ~/.nuget/packages/avalonia.gtk3/$1/lib/netstandard2.0/
cp ../samples/ControlCatalog.NetCore/bin/Debug/netcoreapp2.0/Avalonia**.dll ~/.nuget/packages/avalonia.skia/$1/lib/netstandard2.0/
cp ../samples/ControlCatalog.NetCore/bin/Debug/netcoreapp2.0/Avalonia**.dll ~/.nuget/packages/avalonia.native/$1/lib/netstandard2.0/

7
src/Avalonia.Diagnostics/DevTools.xaml.cs

@ -28,6 +28,11 @@ namespace Avalonia
{
Diagnostics.DevTools.Attach(control, gesture);
}
public static void OpenDevTools(this TopLevel control)
{
Diagnostics.DevTools.OpenDevTools(control);
}
}
}
@ -73,7 +78,7 @@ namespace Avalonia.Diagnostics
RoutingStrategies.Tunnel);
}
private static void OpenDevTools(TopLevel control)
internal static void OpenDevTools(TopLevel control)
{
if (s_open.TryGetValue(control, out var devToolsWindow))
{

19
src/Avalonia.Native/MacOSMountedVolumeInfoProvider.cs

@ -8,16 +8,16 @@ using Avalonia.Controls.Platform;
namespace Avalonia.Native
{
internal class WindowsMountedVolumeInfoListener : IDisposable
internal class MacOSMountedVolumeInfoListener : IDisposable
{
private readonly CompositeDisposable _disposables;
private readonly ObservableCollection<MountedVolumeInfo> _targetObs;
private bool _beenDisposed = false;
private ObservableCollection<MountedVolumeInfo> mountedDrives;
public WindowsMountedVolumeInfoListener(ObservableCollection<MountedVolumeInfo> mountedDrives)
public MacOSMountedVolumeInfoListener(ObservableCollection<MountedVolumeInfo> mountedDrives)
{
this.mountedDrives = mountedDrives;
_disposables = new CompositeDisposable();
var pollTimer = Observable.Interval(TimeSpan.FromSeconds(1))
@ -30,7 +30,8 @@ namespace Avalonia.Native
private void Poll(long _)
{
var mountVolInfos = Directory.GetDirectories("/Volumes")
var mountVolInfos = Directory.GetDirectories("/Volumes/")
.Where(p=> p != null)
.Select(p => new MountedVolumeInfo()
{
VolumeLabel = Path.GetFileName(p),
@ -38,15 +39,15 @@ namespace Avalonia.Native
VolumeSizeBytes = 0
})
.ToArray();
if (_targetObs.SequenceEqual(mountVolInfos))
if (mountedDrives.SequenceEqual(mountVolInfos))
return;
else
{
_targetObs.Clear();
mountedDrives.Clear();
foreach (var i in mountVolInfos)
_targetObs.Add(i);
mountedDrives.Add(i);
}
}
@ -72,7 +73,7 @@ namespace Avalonia.Native
public IDisposable Listen(ObservableCollection<MountedVolumeInfo> mountedDrives)
{
Contract.Requires<ArgumentNullException>(mountedDrives != null);
return new WindowsMountedVolumeInfoListener(mountedDrives);
return new MacOSMountedVolumeInfoListener(mountedDrives);
}
}
}

160
src/Avalonia.Visuals/Vector.cs

@ -65,9 +65,7 @@ namespace Avalonia
/// <param name="b">Second vector</param>
/// <returns>The dot product</returns>
public static double operator *(Vector a, Vector b)
{
return a.X * b.X + a.Y * b.Y;
}
=> Dot(a, b);
/// <summary>
/// Scales a vector.
@ -76,9 +74,7 @@ namespace Avalonia
/// <param name="scale">The scaling factor.</param>
/// <returns>The scaled vector.</returns>
public static Vector operator *(Vector vector, double scale)
{
return new Vector(vector._x * scale, vector._y * scale);
}
=> Multiply(vector, scale);
/// <summary>
/// Scales a vector.
@ -87,14 +83,17 @@ namespace Avalonia
/// <param name="scale">The divisor.</param>
/// <returns>The scaled vector.</returns>
public static Vector operator /(Vector vector, double scale)
{
return new Vector(vector._x / scale, vector._y / scale);
}
=> Divide(vector, scale);
/// <summary>
/// Length of the vector
/// </summary>
public double Length => Math.Sqrt(X * X + Y * Y);
public double Length => Math.Sqrt(SquaredLength);
/// <summary>
/// Squared Length of the vector
/// </summary>
public double SquaredLength => _x * _x + _y * _y;
/// <summary>
/// Negates a vector.
@ -102,9 +101,7 @@ namespace Avalonia
/// <param name="a">The vector.</param>
/// <returns>The negated vector.</returns>
public static Vector operator -(Vector a)
{
return new Vector(-a._x, -a._y);
}
=> Negate(a);
/// <summary>
/// Adds two vectors.
@ -113,9 +110,7 @@ namespace Avalonia
/// <param name="b">The second vector.</param>
/// <returns>A vector that is the result of the addition.</returns>
public static Vector operator +(Vector a, Vector b)
{
return new Vector(a._x + b._x, a._y + b._y);
}
=> Add(a, b);
/// <summary>
/// Subtracts two vectors.
@ -124,9 +119,7 @@ namespace Avalonia
/// <param name="b">The second vector.</param>
/// <returns>A vector that is the result of the subtraction.</returns>
public static Vector operator -(Vector a, Vector b)
{
return new Vector(a._x - b._x, a._y - b._y);
}
=> Subtract(a, b);
/// <summary>
/// Check if two vectors are equal (bitwise).
@ -155,7 +148,8 @@ namespace Avalonia
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(null, obj))
return false;
return obj is Vector vector && Equals(vector);
}
@ -206,5 +200,131 @@ namespace Avalonia
{
return new Vector(_x, y);
}
/// <summary>
/// Returns a normalized version of this vector.
/// </summary>
/// <returns>The normalized vector.</returns>
public Vector Normalize()
=> Normalize(this);
/// <summary>
/// Returns a negated version of this vector.
/// </summary>
/// <returns>The negated vector.</returns>
public Vector Negate()
=> Negate(this);
/// <summary>
/// Returns the dot product of two vectors.
/// </summary>
/// <param name="a">The first vector.</param>
/// <param name="b">The second vector.</param>
/// <returns>The dot product.</returns>
public static double Dot(Vector a, Vector b)
=> a._x * b._x + a._y * b._y;
/// <summary>
/// Returns the cross product of two vectors.
/// </summary>
/// <param name="a">The first vector.</param>
/// <param name="b">The second vector.</param>
/// <returns>The cross product.</returns>
public static double Cross(Vector a, Vector b)
=> a._x * b._y - a._y * b._x;
/// <summary>
/// Normalizes the given vector.
/// </summary>
/// <param name="vector">The vector</param>
/// <returns>The normalized vector.</returns>
public static Vector Normalize(Vector vector)
=> Divide(vector, vector.Length);
/// <summary>
/// Divides the first vector by the second.
/// </summary>
/// <param name="a">The first vector.</param>
/// <param name="b">The second vector.</param>
/// <returns>The scaled vector.</returns>
public static Vector Divide(Vector a, Vector b)
=> new Vector(a._x / b._x, a._y / b._y);
/// <summary>
/// Divides the vector by the given scalar.
/// </summary>
/// <param name="vector">The vector</param>
/// <param name="scalar">The scalar value</param>
/// <returns>The scaled vector.</returns>
public static Vector Divide(Vector vector, double scalar)
=> new Vector(vector._x / scalar, vector._y / scalar);
/// <summary>
/// Multiplies the first vector by the second.
/// </summary>
/// <param name="a">The first vector.</param>
/// <param name="b">The second vector.</param>
/// <returns>The scaled vector.</returns>
public static Vector Multiply(Vector a, Vector b)
=> new Vector(a._x * b._x, a._y * b._y);
/// <summary>
/// Multiplies the vector by the given scalar.
/// </summary>
/// <param name="vector">The vector</param>
/// <param name="scalar">The scalar value</param>
/// <returns>The scaled vector.</returns>
public static Vector Multiply(Vector vector, double scalar)
=> new Vector(vector._x * scalar, vector._y * scalar);
/// <summary>
/// Adds the second to the first vector
/// </summary>
/// <param name="a">The first vector.</param>
/// <param name="b">The second vector.</param>
/// <returns>The summed vector.</returns>
public static Vector Add(Vector a, Vector b)
=> new Vector(a._x + b._x, a._y + b._y);
/// <summary>
/// Subtracts the second from the first vector
/// </summary>
/// <param name="a">The first vector.</param>
/// <param name="b">The second vector.</param>
/// <returns>The difference vector.</returns>
public static Vector Subtract(Vector a, Vector b)
=> new Vector(a._x - b._x, a._y - b._y);
/// <summary>
/// Negates the vector
/// </summary>
/// <param name="vector">The vector to negate.</param>
/// <returns>The scaled vector.</returns>
public static Vector Negate(Vector vector)
=> new Vector(-vector._x, -vector._y);
/// <summary>
/// Returnes the vector (0.0, 0.0)
/// </summary>
public static Vector Zero
=> new Vector(0, 0);
/// <summary>
/// Returnes the vector (1.0, 1.0)
/// </summary>
public static Vector One
=> new Vector(1, 1);
/// <summary>
/// Returnes the vector (1.0, 0.0)
/// </summary>
public static Vector UnitX
=> new Vector(1, 0);
/// <summary>
/// Returnes the vector (0.0, 1.0)
/// </summary>
public static Vector UnitY
=> new Vector(0, 1);
}
}

9
src/Windows/Avalonia.Win32/WindowsMountedVolumeInfoListener.cs

@ -10,8 +10,7 @@ namespace Avalonia.Win32
{
internal class WindowsMountedVolumeInfoListener : IDisposable
{
private readonly CompositeDisposable _disposables;
private readonly ObservableCollection<MountedVolumeInfo> _targetObs = new ObservableCollection<MountedVolumeInfo>();
private readonly CompositeDisposable _disposables;
private bool _beenDisposed = false;
private ObservableCollection<MountedVolumeInfo> mountedDrives;
@ -41,14 +40,14 @@ namespace Avalonia.Win32
})
.ToArray();
if (_targetObs.SequenceEqual(mountVolInfos))
if (mountedDrives.SequenceEqual(mountVolInfos))
return;
else
{
_targetObs.Clear();
mountedDrives.Clear();
foreach (var i in mountVolInfos)
_targetObs.Add(i);
mountedDrives.Add(i);
}
}

112
tests/Avalonia.Visuals.UnitTests/VectorTests.cs

@ -0,0 +1,112 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using Xunit;
using Avalonia;
using System;
namespace Avalonia.Visuals.UnitTests
{
public class VectorTests
{
[Fact]
public void Length_Should_Return_Correct_Length_Of_Vector()
{
var vector = new Vector(2, 4);
var length = Math.Sqrt(2 * 2 + 4 * 4);
Assert.Equal(length, vector.Length);
}
[Fact]
public void Length_Squared_Should_Return_Correct_Length_Of_Vector()
{
var vectorA = new Vector(2, 4);
var squaredLengthA = 2 * 2 + 4 * 4;
Assert.Equal(squaredLengthA, vectorA.SquaredLength);
}
[Fact]
public void Normalize_Should_Return_Normalized_Vector()
{
// the length of a normalized vector must be 1
var vectorA = new Vector(13, 84);
var vectorB = new Vector(-34, 345);
var vectorC = new Vector(-34, -84);
Assert.Equal(1.0, vectorA.Normalize().Length);
Assert.Equal(1.0, vectorB.Normalize().Length);
Assert.Equal(1.0, vectorC.Normalize().Length);
}
[Fact]
public void Negate_Should_Return_Negated_Vector()
{
var vector = new Vector(2, 4);
var negated = new Vector(-2, -4);
Assert.Equal(negated, vector.Negate());
}
[Fact]
public void Dot_Should_Return_Correct_Value()
{
var a = new Vector(-6, 8.0);
var b = new Vector(5, 12.0);
Assert.Equal(66.0, Vector.Dot(a, b));
}
[Fact]
public void Cross_Should_Return_Correct_Value()
{
var a = new Vector(-6, 8.0);
var b = new Vector(5, 12.0);
Assert.Equal(-112.0, Vector.Cross(a, b));
}
[Fact]
public void Divied_By_Vector_Should_Return_Correct_Value()
{
var a = new Vector(10, 2);
var b = new Vector(5, 2);
var expected = new Vector(2, 1);
Assert.Equal(expected, Vector.Divide(a, b));
}
[Fact]
public void Divied_Should_Return_Correct_Value()
{
var vector = new Vector(10, 2);
var expected = new Vector(5, 1);
Assert.Equal(expected, Vector.Divide(vector, 2));
}
[Fact]
public void Multiply_By_Vector_Should_Return_Correct_Value()
{
var a = new Vector(10, 2);
var b = new Vector(2, 2);
var expected = new Vector(20, 4);
Assert.Equal(expected, Vector.Multiply(a, b));
}
[Fact]
public void Multiply_Should_Return_Correct_Value()
{
var vector = new Vector(10, 2);
var expected = new Vector(20, 4);
Assert.Equal(expected, Vector.Multiply(vector, 2));
}
}
}
Loading…
Cancel
Save