diff --git a/NOTICE.md b/NOTICE.md
index 0e1d792e84..92fd725957 100644
--- a/NOTICE.md
+++ b/NOTICE.md
@@ -271,3 +271,35 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
+
+# Chromium
+
+https://github.com/chromium/chromium
+
+// Copyright 2015 The Chromium Authors. All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
diff --git a/build/Moq.props b/build/Moq.props
index 7de9b6b6ba..9e2fd1db5d 100644
--- a/build/Moq.props
+++ b/build/Moq.props
@@ -1,5 +1,5 @@
-
+
diff --git a/readme.md b/readme.md
index 491b517e42..9d317cdd06 100644
--- a/readme.md
+++ b/readme.md
@@ -2,7 +2,7 @@
[](https://www.nuget.org/packages/Avalonia) [](https://www.nuget.org/packages/Avalonia) [](https://www.myget.org/gallery/avalonia-ci) 
-
+
## 📖 About AvaloniaUI
diff --git a/samples/ControlCatalog/MainView.xaml b/samples/ControlCatalog/MainView.xaml
index af2d093bc7..fef68bb5f5 100644
--- a/samples/ControlCatalog/MainView.xaml
+++ b/samples/ControlCatalog/MainView.xaml
@@ -57,6 +57,7 @@
+
diff --git a/samples/ControlCatalog/Models/Person.cs b/samples/ControlCatalog/Models/Person.cs
index 4248cb8056..a0abcfe8f4 100644
--- a/samples/ControlCatalog/Models/Person.cs
+++ b/samples/ControlCatalog/Models/Person.cs
@@ -21,12 +21,12 @@ namespace ControlCatalog.Models
get => _firstName;
set
{
+ _firstName = value;
if (string.IsNullOrWhiteSpace(value))
SetError(nameof(FirstName), "First Name Required");
else
SetError(nameof(FirstName), null);
- _firstName = value;
OnPropertyChanged(nameof(FirstName));
}
@@ -37,12 +37,12 @@ namespace ControlCatalog.Models
get => _lastName;
set
{
+ _lastName = value;
if (string.IsNullOrWhiteSpace(value))
SetError(nameof(LastName), "Last Name Required");
else
SetError(nameof(LastName), null);
- _lastName = value;
OnPropertyChanged(nameof(LastName));
}
}
@@ -95,4 +95,4 @@ namespace ControlCatalog.Models
return null;
}
}
-}
\ No newline at end of file
+}
diff --git a/samples/ControlCatalog/Pages/BorderPage.xaml b/samples/ControlCatalog/Pages/BorderPage.xaml
index c30056d5e5..8133d0e408 100644
--- a/samples/ControlCatalog/Pages/BorderPage.xaml
+++ b/samples/ControlCatalog/Pages/BorderPage.xaml
@@ -29,6 +29,13 @@
Padding="16">
Rounded Corners
+
+
+
+
diff --git a/samples/ControlCatalog/Pages/DialogsPage.xaml b/samples/ControlCatalog/Pages/DialogsPage.xaml
index 0834e829d8..a0e82663bf 100644
--- a/samples/ControlCatalog/Pages/DialogsPage.xaml
+++ b/samples/ControlCatalog/Pages/DialogsPage.xaml
@@ -11,5 +11,7 @@
Decorated window (dialog)
Dialog
Dialog (No taskbar icon)
+ Owned window
+ Owned window (No taskbar icon)
diff --git a/samples/ControlCatalog/Pages/DialogsPage.xaml.cs b/samples/ControlCatalog/Pages/DialogsPage.xaml.cs
index dcb94a89e7..cf6c771e34 100644
--- a/samples/ControlCatalog/Pages/DialogsPage.xaml.cs
+++ b/samples/ControlCatalog/Pages/DialogsPage.xaml.cs
@@ -93,6 +93,21 @@ namespace ControlCatalog.Pages
window.ShowInTaskbar = false;
window.ShowDialog(GetWindow());
};
+ this.FindControl("OwnedWindow").Click += delegate
+ {
+ var window = CreateSampleWindow();
+
+ window.Show(GetWindow());
+ };
+
+ this.FindControl("OwnedWindowNoTaskbar").Click += delegate
+ {
+ var window = CreateSampleWindow();
+
+ window.ShowInTaskbar = false;
+
+ window.Show(GetWindow());
+ };
}
private Window CreateSampleWindow()
diff --git a/samples/ControlCatalog/Pages/ToggleSwitchPage.xaml b/samples/ControlCatalog/Pages/ToggleSwitchPage.xaml
new file mode 100644
index 0000000000..161ee2ee16
--- /dev/null
+++ b/samples/ControlCatalog/Pages/ToggleSwitchPage.xaml
@@ -0,0 +1,79 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/ControlCatalog/Pages/ToggleSwitchPage.xaml.cs b/samples/ControlCatalog/Pages/ToggleSwitchPage.xaml.cs
new file mode 100644
index 0000000000..66f7d14c7f
--- /dev/null
+++ b/samples/ControlCatalog/Pages/ToggleSwitchPage.xaml.cs
@@ -0,0 +1,19 @@
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Markup.Xaml;
+
+namespace ControlCatalog.Pages
+{
+ public class ToggleSwitchPage : UserControl
+ {
+ public ToggleSwitchPage()
+ {
+ this.InitializeComponent();
+ }
+
+ private void InitializeComponent()
+ {
+ AvaloniaXamlLoader.Load(this);
+ }
+ }
+}
diff --git a/samples/RenderDemo/MainWindow.xaml b/samples/RenderDemo/MainWindow.xaml
index c098ef411e..14ccc82043 100644
--- a/samples/RenderDemo/MainWindow.xaml
+++ b/samples/RenderDemo/MainWindow.xaml
@@ -29,6 +29,9 @@
+
+
+
diff --git a/samples/RenderDemo/Pages/TransitionsPage.xaml b/samples/RenderDemo/Pages/TransitionsPage.xaml
new file mode 100644
index 0000000000..d6da293ff3
--- /dev/null
+++ b/samples/RenderDemo/Pages/TransitionsPage.xaml
@@ -0,0 +1,115 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Hover to activate Transform Keyframe Animations.
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/samples/RenderDemo/Pages/TransitionsPage.xaml.cs b/samples/RenderDemo/Pages/TransitionsPage.xaml.cs
new file mode 100644
index 0000000000..5f446c9e99
--- /dev/null
+++ b/samples/RenderDemo/Pages/TransitionsPage.xaml.cs
@@ -0,0 +1,37 @@
+using Avalonia.Animation;
+using Avalonia.Controls;
+using Avalonia.Interactivity;
+using Avalonia.Markup.Xaml;
+using RenderDemo.ViewModels;
+
+namespace RenderDemo.Pages
+{
+ public class TransitionsPage : UserControl
+ {
+ public TransitionsPage()
+ {
+ InitializeComponent();
+ this.DataContext = new AnimationsPageViewModel();
+ }
+
+ private void InitializeComponent()
+ {
+ AvaloniaXamlLoader.Load(this);
+ }
+
+ private void ToggleClock(object sender, RoutedEventArgs args)
+ {
+ var button = sender as Button;
+ var clock = button.Clock;
+
+ if (clock.PlayState == PlayState.Run)
+ {
+ clock.PlayState = PlayState.Pause;
+ }
+ else if (clock.PlayState == PlayState.Pause)
+ {
+ clock.PlayState = PlayState.Run;
+ }
+ }
+ }
+}
diff --git a/src/Avalonia.Animation/TransitionInstance.cs b/src/Avalonia.Animation/TransitionInstance.cs
index ad2001d621..5184341324 100644
--- a/src/Avalonia.Animation/TransitionInstance.cs
+++ b/src/Avalonia.Animation/TransitionInstance.cs
@@ -4,6 +4,7 @@ using System.Reactive.Linq;
using Avalonia.Animation.Easings;
using Avalonia.Animation.Utils;
using Avalonia.Reactive;
+using Avalonia.Utilities;
namespace Avalonia.Animation
{
@@ -13,31 +14,56 @@ namespace Avalonia.Animation
internal class TransitionInstance : SingleSubscriberObservableBase
{
private IDisposable _timerSubscription;
+ private TimeSpan _delay;
private TimeSpan _duration;
private readonly IClock _baseClock;
private IClock _clock;
- public TransitionInstance(IClock clock, TimeSpan Duration)
+ public TransitionInstance(IClock clock, TimeSpan delay, TimeSpan duration)
{
clock = clock ?? throw new ArgumentNullException(nameof(clock));
- _duration = Duration;
+ _delay = delay;
+ _duration = duration;
_baseClock = clock;
}
private void TimerTick(TimeSpan t)
{
- var interpVal = _duration.Ticks == 0 ? 1d : (double)t.Ticks / _duration.Ticks;
+
+ // [<------------- normalizedTotalDur ------------------>]
+ // [<---- Delay ---->][<---------- Duration ------------>]
+ // ^- normalizedDelayEnd
+ // [<---- normalizedInterpVal --->]
+
+ var normalizedInterpVal = 1d;
+
+ if (!MathUtilities.AreClose(_duration.TotalSeconds, 0d))
+ {
+ var normalizedTotalDur = _delay + _duration;
+ var normalizedDelayEnd = _delay.TotalSeconds / normalizedTotalDur.TotalSeconds;
+ var normalizedPresentationTime = t.TotalSeconds / normalizedTotalDur.TotalSeconds;
+
+ if (normalizedPresentationTime < normalizedDelayEnd
+ || MathUtilities.AreClose(normalizedPresentationTime, normalizedDelayEnd))
+ {
+ normalizedInterpVal = 0d;
+ }
+ else
+ {
+ normalizedInterpVal = (t.TotalSeconds - _delay.TotalSeconds) / _duration.TotalSeconds;
+ }
+ }
// Clamp interpolation value.
- if (interpVal >= 1d | interpVal < 0d)
+ if (normalizedInterpVal >= 1d || normalizedInterpVal < 0d)
{
PublishNext(1d);
PublishCompleted();
}
else
{
- PublishNext(interpVal);
+ PublishNext(normalizedInterpVal);
}
}
diff --git a/src/Avalonia.Animation/Transition`1.cs b/src/Avalonia.Animation/Transition`1.cs
index 138131acb9..4542a137e5 100644
--- a/src/Avalonia.Animation/Transition`1.cs
+++ b/src/Avalonia.Animation/Transition`1.cs
@@ -13,10 +13,15 @@ namespace Avalonia.Animation
private AvaloniaProperty _prop;
///
- /// Gets the duration of the animation.
+ /// Gets or sets the duration of the transition.
///
public TimeSpan Duration { get; set; }
+ ///
+ /// Gets or sets delay before starting the transition.
+ ///
+ public TimeSpan Delay { get; set; } = TimeSpan.Zero;
+
///
/// Gets the easing class to be used.
///
@@ -47,7 +52,7 @@ namespace Avalonia.Animation
///
public virtual IDisposable Apply(Animatable control, IClock clock, object oldValue, object newValue)
{
- var transition = DoTransition(new TransitionInstance(clock, Duration), (T)oldValue, (T)newValue);
+ var transition = DoTransition(new TransitionInstance(clock, Delay, Duration), (T)oldValue, (T)newValue);
return control.Bind((AvaloniaProperty)Property, transition, Data.BindingPriority.Animation);
}
}
diff --git a/src/Avalonia.Base/Utilities/MathUtilities.cs b/src/Avalonia.Base/Utilities/MathUtilities.cs
index f16af16ed5..06a1cd4ae5 100644
--- a/src/Avalonia.Base/Utilities/MathUtilities.cs
+++ b/src/Avalonia.Base/Utilities/MathUtilities.cs
@@ -267,6 +267,36 @@ namespace Avalonia.Utilities
}
}
+ ///
+ /// Converts an angle in degrees to radians.
+ ///
+ /// The angle in degrees.
+ /// The angle in radians.
+ public static double Deg2Rad(double angle)
+ {
+ return angle * (Math.PI / 180d);
+ }
+
+ ///
+ /// Converts an angle in gradians to radians.
+ ///
+ /// The angle in gradians.
+ /// The angle in radians.
+ public static double Grad2Rad(double angle)
+ {
+ return angle * (Math.PI / 200d);
+ }
+
+ ///
+ /// Converts an angle in turns to radians.
+ ///
+ /// The angle in turns.
+ /// The angle in radians.
+ public static double Turn2Rad(double angle)
+ {
+ return angle * 2 * Math.PI;
+ }
+
private static void ThrowCannotBeGreaterThanException(double min, double max)
{
throw new ArgumentException($"{min} cannot be greater than {max}.");
diff --git a/src/Avalonia.Controls/Border.cs b/src/Avalonia.Controls/Border.cs
index b355310244..c8337df99c 100644
--- a/src/Avalonia.Controls/Border.cs
+++ b/src/Avalonia.Controls/Border.cs
@@ -1,13 +1,14 @@
using Avalonia.Controls.Utils;
using Avalonia.Layout;
using Avalonia.Media;
+using Avalonia.VisualTree;
namespace Avalonia.Controls
{
///
/// A control which decorates a child with a border and background.
///
- public partial class Border : Decorator
+ public partial class Border : Decorator, IVisualWithRoundRectClip
{
///
/// Defines the property.
@@ -129,5 +130,7 @@ namespace Avalonia.Controls
{
return LayoutHelper.ArrangeChild(Child, finalSize, Padding, BorderThickness);
}
+
+ public CornerRadius ClipToBoundsRadius => CornerRadius;
}
}
diff --git a/src/Avalonia.Controls/Converters/MenuScrollingVisibilityConverter.cs b/src/Avalonia.Controls/Converters/MenuScrollingVisibilityConverter.cs
new file mode 100644
index 0000000000..e6420fe342
--- /dev/null
+++ b/src/Avalonia.Controls/Converters/MenuScrollingVisibilityConverter.cs
@@ -0,0 +1,64 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using Avalonia.Controls.Primitives;
+using Avalonia.Data.Converters;
+using Avalonia.Utilities;
+
+namespace Avalonia.Controls.Converters
+{
+ public class MenuScrollingVisibilityConverter : IMultiValueConverter
+ {
+ public static readonly MenuScrollingVisibilityConverter Instance = new MenuScrollingVisibilityConverter();
+
+ public object Convert(IList values, Type targetType, object parameter, CultureInfo culture)
+ {
+ if (parameter == null ||
+ values == null ||
+ values.Count != 4 ||
+ !(values[0] is ScrollBarVisibility visiblity) ||
+ !(values[1] is double offset) ||
+ !(values[2] is double extent) ||
+ !(values[3] is double viewport))
+ {
+ return AvaloniaProperty.UnsetValue;
+ }
+
+ if (visiblity == ScrollBarVisibility.Auto)
+ {
+ if (extent == viewport)
+ {
+ return false;
+ }
+
+ double target;
+
+ if (parameter is double d)
+ {
+ target = d;
+ }
+ else if (parameter is string s)
+ {
+ target = double.Parse(s, NumberFormatInfo.InvariantInfo);
+ }
+ else
+ {
+ return AvaloniaProperty.UnsetValue;
+ }
+
+ // Calculate the percent so that we can see if we are near the edge of the range
+ double percent = MathUtilities.Clamp(offset * 100.0 / (extent - viewport), 0, 100);
+
+ if (MathUtilities.AreClose(percent, target))
+ {
+ // We are at the end of the range, so no need for this button to be shown
+ return false;
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+ }
+}
diff --git a/src/Avalonia.Controls/LayoutTransformControl.cs b/src/Avalonia.Controls/LayoutTransformControl.cs
index 8d48f6646d..83ad2b3638 100644
--- a/src/Avalonia.Controls/LayoutTransformControl.cs
+++ b/src/Avalonia.Controls/LayoutTransformControl.cs
@@ -14,8 +14,8 @@ namespace Avalonia.Controls
///
public class LayoutTransformControl : Decorator
{
- public static readonly StyledProperty LayoutTransformProperty =
- AvaloniaProperty.Register(nameof(LayoutTransform));
+ public static readonly StyledProperty LayoutTransformProperty =
+ AvaloniaProperty.Register(nameof(LayoutTransform));
public static readonly StyledProperty UseRenderTransformProperty =
AvaloniaProperty.Register(nameof(LayoutTransform));
@@ -37,7 +37,7 @@ namespace Avalonia.Controls
///
/// Gets or sets a graphics transformation that should apply to this element when layout is performed.
///
- public Transform LayoutTransform
+ public ITransform LayoutTransform
{
get { return GetValue(LayoutTransformProperty); }
set { SetValue(LayoutTransformProperty, value); }
diff --git a/src/Avalonia.Controls/Platform/IWindowBaseImpl.cs b/src/Avalonia.Controls/Platform/IWindowBaseImpl.cs
index 8c99dffc28..b190c4f2e7 100644
--- a/src/Avalonia.Controls/Platform/IWindowBaseImpl.cs
+++ b/src/Avalonia.Controls/Platform/IWindowBaseImpl.cs
@@ -1,5 +1,4 @@
using System;
-using Avalonia.Controls;
namespace Avalonia.Platform
{
@@ -46,9 +45,9 @@ namespace Avalonia.Platform
IPlatformHandle Handle { get; }
///
- /// Gets the maximum size of a window on the system.
+ /// Gets a maximum client size hint for an auto-sizing window, in device-independent pixels.
///
- Size MaxClientSize { get; }
+ Size MaxAutoSizeHint { get; }
///
/// Sets whether this window appears on top of all other windows
diff --git a/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs b/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs
index 83ce63f240..3fac440c40 100644
--- a/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs
+++ b/src/Avalonia.Controls/Presenters/ItemVirtualizerSimple.cs
@@ -113,7 +113,7 @@ namespace Avalonia.Controls.Presenters
{
var scrollable = (ILogicalScrollable)Owner;
var visualRoot = Owner.GetVisualRoot();
- var maxAvailableSize = (visualRoot as WindowBase)?.PlatformImpl?.MaxClientSize
+ var maxAvailableSize = (visualRoot as WindowBase)?.PlatformImpl?.MaxAutoSizeHint
?? (visualRoot as TopLevel)?.ClientSize;
// If infinity is passed as the available size and we're virtualized then we need to
diff --git a/src/Avalonia.Controls/Primitives/PopupRoot.cs b/src/Avalonia.Controls/Primitives/PopupRoot.cs
index 854b0cf435..da7352b77f 100644
--- a/src/Avalonia.Controls/Primitives/PopupRoot.cs
+++ b/src/Avalonia.Controls/Primitives/PopupRoot.cs
@@ -121,7 +121,20 @@ namespace Avalonia.Controls.Primitives
protected override Size MeasureOverride(Size availableSize)
{
- var measured = base.MeasureOverride(availableSize);
+ var maxAutoSize = PlatformImpl?.MaxAutoSizeHint ?? Size.Infinity;
+ var constraint = availableSize;
+
+ if (double.IsInfinity(constraint.Width))
+ {
+ constraint = constraint.WithWidth(maxAutoSize.Width);
+ }
+
+ if (double.IsInfinity(constraint.Height))
+ {
+ constraint = constraint.WithHeight(maxAutoSize.Height);
+ }
+
+ var measured = base.MeasureOverride(constraint);
var width = measured.Width;
var height = measured.Height;
var widthCache = Width;
diff --git a/src/Avalonia.Controls/ScrollViewer.cs b/src/Avalonia.Controls/ScrollViewer.cs
index c3f0dc0056..a5f55eaa02 100644
--- a/src/Avalonia.Controls/ScrollViewer.cs
+++ b/src/Avalonia.Controls/ScrollViewer.cs
@@ -181,6 +181,9 @@ namespace Avalonia.Controls
private Size _extent;
private Vector _offset;
private Size _viewport;
+ private Size _oldExtent;
+ private Vector _oldOffset;
+ private Size _oldViewport;
private Size _largeChange;
private Size _smallChange = new Size(DefaultSmallChange, DefaultSmallChange);
@@ -198,6 +201,7 @@ namespace Avalonia.Controls
///
public ScrollViewer()
{
+ LayoutUpdated += OnLayoutUpdated;
}
///
@@ -221,11 +225,9 @@ namespace Avalonia.Controls
private set
{
- var old = _extent;
-
if (SetAndRaise(ExtentProperty, ref _extent, value))
{
- CalculatedPropertiesChanged(extentDelta: value - old);
+ CalculatedPropertiesChanged();
}
}
}
@@ -242,13 +244,11 @@ namespace Avalonia.Controls
set
{
- var old = _offset;
-
value = ValidateOffset(this, value);
if (SetAndRaise(OffsetProperty, ref _offset, value))
{
- CalculatedPropertiesChanged(offsetDelta: value - old);
+ CalculatedPropertiesChanged();
}
}
}
@@ -265,11 +265,9 @@ namespace Avalonia.Controls
private set
{
- var old = _viewport;
-
if (SetAndRaise(ViewportProperty, ref _viewport, value))
{
- CalculatedPropertiesChanged(viewportDelta: value - old);
+ CalculatedPropertiesChanged();
}
}
}
@@ -387,6 +385,38 @@ namespace Avalonia.Controls
///
IControl IScrollAnchorProvider.CurrentAnchor => null; // TODO: Implement
+ ///
+ /// Scrolls the content up one line.
+ ///
+ public void LineUp()
+ {
+ Offset -= new Vector(0, _smallChange.Height);
+ }
+
+ ///
+ /// Scrolls the content down one line.
+ ///
+ public void LineDown()
+ {
+ Offset += new Vector(0, _smallChange.Height);
+ }
+
+ ///
+ /// Scrolls the content left one line.
+ ///
+ public void LineLeft()
+ {
+ Offset -= new Vector(_smallChange.Width, 0);
+ }
+
+ ///
+ /// Scrolls the content right one line.
+ ///
+ public void LineRight()
+ {
+ Offset += new Vector(_smallChange.Width, 0);
+ }
+
///
/// Scrolls to the top-left corner of the content.
///
@@ -549,10 +579,7 @@ namespace Avalonia.Controls
}
}
- private void CalculatedPropertiesChanged(
- Size extentDelta = default,
- Vector offsetDelta = default,
- Size viewportDelta = default)
+ private void CalculatedPropertiesChanged()
{
// Pass old values of 0 here because we don't have the old values at this point,
// and it shouldn't matter as only the template uses these properies.
@@ -573,20 +600,6 @@ namespace Avalonia.Controls
SetAndRaise(SmallChangeProperty, ref _smallChange, new Size(DefaultSmallChange, DefaultSmallChange));
SetAndRaise(LargeChangeProperty, ref _largeChange, Viewport);
}
-
- if (extentDelta != default || offsetDelta != default || viewportDelta != default)
- {
- using var route = BuildEventRoute(ScrollChangedEvent);
-
- if (route.HasHandlers)
- {
- var e = new ScrollChangedEventArgs(
- new Vector(extentDelta.Width, extentDelta.Height),
- offsetDelta,
- new Vector(viewportDelta.Width, viewportDelta.Height));
- route.RaiseEvent(this, e);
- }
- }
}
protected override void OnKeyDown(KeyEventArgs e)
@@ -602,5 +615,38 @@ namespace Avalonia.Controls
e.Handled = true;
}
}
+
+ ///
+ /// Called when a change in scrolling state is detected, such as a change in scroll
+ /// position, extent, or viewport size.
+ ///
+ /// The event args.
+ ///
+ /// If you override this method, call `base.OnScrollChanged(ScrollChangedEventArgs)` to
+ /// ensure that this event is raised.
+ ///
+ protected virtual void OnScrollChanged(ScrollChangedEventArgs e)
+ {
+ RaiseEvent(e);
+ }
+
+ private void OnLayoutUpdated(object sender, EventArgs e) => RaiseScrollChanged();
+
+ private void RaiseScrollChanged()
+ {
+ var extentDelta = new Vector(Extent.Width - _oldExtent.Width, Extent.Height - _oldExtent.Height);
+ var offsetDelta = Offset - _oldOffset;
+ var viewportDelta = new Vector(Viewport.Width - _oldViewport.Width, Viewport.Height - _oldViewport.Height);
+
+ if (!extentDelta.NearlyEquals(default) || !offsetDelta.NearlyEquals(default) || !viewportDelta.NearlyEquals(default))
+ {
+ var e = new ScrollChangedEventArgs(extentDelta, offsetDelta, viewportDelta);
+ OnScrollChanged(e);
+
+ _oldExtent = Extent;
+ _oldOffset = Offset;
+ _oldViewport = Viewport;
+ }
+ }
}
}
diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs
index 42e16e6979..394699ce64 100644
--- a/src/Avalonia.Controls/TextBox.cs
+++ b/src/Avalonia.Controls/TextBox.cs
@@ -347,7 +347,7 @@ namespace Avalonia.Controls
if (IsFocused)
{
- DecideCaretVisibility();
+ _presenter?.ShowCaret();
}
}
@@ -364,14 +364,7 @@ namespace Avalonia.Controls
{
SelectAll();
}
- else
- {
- DecideCaretVisibility();
- }
- }
- private void DecideCaretVisibility()
- {
_presenter?.ShowCaret();
}
@@ -580,15 +573,15 @@ namespace Avalonia.Controls
switch (e.Key)
{
case Key.Left:
- MoveHorizontal(-1, hasWholeWordModifiers);
- movement = true;
selection = DetectSelection();
+ MoveHorizontal(-1, hasWholeWordModifiers, selection);
+ movement = true;
break;
case Key.Right:
- MoveHorizontal(1, hasWholeWordModifiers);
- movement = true;
selection = DetectSelection();
+ MoveHorizontal(1, hasWholeWordModifiers, selection);
+ movement = true;
break;
case Key.Up:
@@ -833,13 +826,21 @@ namespace Avalonia.Controls
return result;
}
- private void MoveHorizontal(int direction, bool wholeWord)
+ private void MoveHorizontal(int direction, bool wholeWord, bool isSelecting)
{
var text = Text ?? string.Empty;
var caretIndex = CaretIndex;
if (!wholeWord)
{
+ if (SelectionStart != SelectionEnd && !isSelecting)
+ {
+ var start = Math.Min(SelectionStart, SelectionEnd);
+ var end = Math.Max(SelectionStart, SelectionEnd);
+ CaretIndex = direction < 0 ? start : end;
+ return;
+ }
+
var index = caretIndex + direction;
if (index < 0 || index > text.Length)
@@ -975,6 +976,7 @@ namespace Avalonia.Controls
{
SelectionStart = 0;
SelectionEnd = Text?.Length ?? 0;
+ CaretIndex = SelectionEnd;
}
private bool DeleteSelection()
@@ -1055,14 +1057,14 @@ namespace Avalonia.Controls
private void SetSelectionForControlBackspace()
{
SelectionStart = CaretIndex;
- MoveHorizontal(-1, true);
+ MoveHorizontal(-1, true, false);
SelectionEnd = CaretIndex;
}
private void SetSelectionForControlDelete()
{
SelectionStart = CaretIndex;
- MoveHorizontal(1, true);
+ MoveHorizontal(1, true, false);
SelectionEnd = CaretIndex;
}
diff --git a/src/Avalonia.Controls/ToggleSwitch.cs b/src/Avalonia.Controls/ToggleSwitch.cs
index 6c6426a31d..4b42c574cf 100644
--- a/src/Avalonia.Controls/ToggleSwitch.cs
+++ b/src/Avalonia.Controls/ToggleSwitch.cs
@@ -1,4 +1,5 @@
-using Avalonia.Controls.Primitives;
+using Avalonia.Controls.Presenters;
+using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.LogicalTree;
@@ -57,6 +58,18 @@ namespace Avalonia.Controls
set { SetValue(OffContentProperty, value); }
}
+ public IContentPresenter OffContentPresenter
+ {
+ get;
+ private set;
+ }
+
+ public IContentPresenter OnContentPresenter
+ {
+ get;
+ private set;
+ }
+
///
/// Gets or Sets the used to display the .
///
@@ -100,6 +113,24 @@ namespace Avalonia.Controls
LogicalChildren.Add(newChild);
}
}
+
+ protected override bool RegisterContentPresenter(IContentPresenter presenter)
+ {
+ var result = base.RegisterContentPresenter(presenter);
+
+ if (presenter.Name == "Part_OnContentPresenter")
+ {
+ OnContentPresenter = presenter;
+ result = true;
+ }
+ else if (presenter.Name == "PART_OffContentPresenter")
+ {
+ OffContentPresenter = presenter;
+ result = true;
+ }
+
+ return result;
+ }
}
}
diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs
index 278bcd0426..5d34444eb8 100644
--- a/src/Avalonia.Controls/TopLevel.cs
+++ b/src/Avalonia.Controls/TopLevel.cs
@@ -276,9 +276,6 @@ namespace Avalonia.Controls
set { SetValue(AccessText.ShowAccessKeyProperty, value); }
}
- ///
- Size ILayoutRoot.MaxClientSize => Size.Infinity;
-
///
double ILayoutRoot.LayoutScaling => PlatformImpl?.Scaling ?? 1;
diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs
index 474d845905..ff7cc41e3b 100644
--- a/src/Avalonia.Controls/Window.cs
+++ b/src/Avalonia.Controls/Window.cs
@@ -69,7 +69,7 @@ namespace Avalonia.Controls
///
public class Window : WindowBase, IStyleable, IFocusScope, ILayoutRoot
{
- private List _children = new List();
+ private readonly List<(Window child, bool isDialog)> _children = new List<(Window, bool)>();
///
/// Defines the property.
@@ -188,7 +188,7 @@ namespace Avalonia.Controls
impl.Closing = HandleClosing;
impl.GotInputWhenDisabled = OnGotInputWhenDisabled;
impl.WindowStateChanged = HandleWindowStateChanged;
- _maxPlatformClientSize = PlatformImpl?.MaxClientSize ?? default(Size);
+ _maxPlatformClientSize = PlatformImpl?.MaxAutoSizeHint ?? default(Size);
this.GetObservable(ClientSizeProperty).Skip(1).Subscribe(x => PlatformImpl?.Resize(x));
PlatformImpl?.ShowTaskbarIcon(ShowInTaskbar);
@@ -318,9 +318,6 @@ namespace Avalonia.Controls
///
public void BeginResizeDrag(WindowEdge edge, PointerPressedEventArgs e) => PlatformImpl?.BeginResizeDrag(edge, e);
- ///
- Size ILayoutRoot.MaxClientSize => _maxPlatformClientSize;
-
///
Type IStyleable.StyleKey => typeof(Window);
@@ -376,7 +373,7 @@ namespace Avalonia.Controls
private void CloseInternal()
{
- foreach (var child in _children.ToList())
+ foreach (var (child, _) in _children.ToList())
{
// if we HandleClosing() before then there will be no children.
child.CloseInternal();
@@ -399,7 +396,7 @@ namespace Avalonia.Controls
{
bool canClose = true;
- foreach (var child in _children.ToList())
+ foreach (var (child, _) in _children.ToList())
{
if (!child.HandleClosing())
{
@@ -472,6 +469,28 @@ namespace Avalonia.Controls
/// The window has already been closed.
///
public override void Show()
+ {
+ ShowCore(null);
+ }
+
+ ///
+ /// Shows the window as a child of .
+ ///
+ /// Window that will be a parent of the shown window.
+ ///
+ /// The window has already been closed.
+ ///
+ public void Show(Window parent)
+ {
+ if (parent is null)
+ {
+ throw new ArgumentNullException(nameof(parent), "Showing a child window requires valid parent.");
+ }
+
+ ShowCore(parent);
+ }
+
+ private void ShowCore(Window parent)
{
if (PlatformImpl == null)
{
@@ -483,7 +502,7 @@ namespace Avalonia.Controls
return;
}
- this.RaiseEvent(new RoutedEventArgs(WindowOpenedEvent));
+ RaiseEvent(new RoutedEventArgs(WindowOpenedEvent));
EnsureInitialized();
IsVisible = true;
@@ -504,6 +523,14 @@ namespace Avalonia.Controls
using (BeginAutoSizing())
{
+ if (parent != null)
+ {
+ PlatformImpl?.SetParent(parent.PlatformImpl);
+ }
+
+ Owner = parent;
+ parent?.AddChild(this, false);
+
PlatformImpl?.Show();
Renderer?.Start();
}
@@ -571,9 +598,9 @@ namespace Avalonia.Controls
using (BeginAutoSizing())
{
- PlatformImpl.SetParent(owner.PlatformImpl);
+ PlatformImpl?.SetParent(owner.PlatformImpl);
Owner = owner;
- owner.AddChild(this);
+ owner.AddChild(this, true);
PlatformImpl?.Show();
Renderer?.Start();
@@ -598,28 +625,57 @@ namespace Avalonia.Controls
private void UpdateEnabled()
{
- PlatformImpl.SetEnabled(_children.Count == 0);
+ bool isEnabled = true;
+
+ foreach (var (_, isDialog) in _children)
+ {
+ if (isDialog)
+ {
+ isEnabled = false;
+ break;
+ }
+ }
+
+ PlatformImpl.SetEnabled(isEnabled);
}
- private void AddChild(Window window)
+ private void AddChild(Window window, bool isDialog)
{
- _children.Add(window);
+ _children.Add((window, isDialog));
UpdateEnabled();
}
private void RemoveChild(Window window)
{
- _children.Remove(window);
+ for (int i = _children.Count - 1; i >= 0; i--)
+ {
+ var (child, _) = _children[i];
+
+ if (ReferenceEquals(child, window))
+ {
+ _children.RemoveAt(i);
+ }
+ }
+
UpdateEnabled();
}
private void OnGotInputWhenDisabled()
{
- var firstChild = _children.FirstOrDefault();
+ Window firstDialogChild = null;
+
+ foreach (var (child, isDialog) in _children)
+ {
+ if (isDialog)
+ {
+ firstDialogChild = child;
+ break;
+ }
+ }
- if (firstChild != null)
+ if (firstDialogChild != null)
{
- firstChild.OnGotInputWhenDisabled();
+ firstDialogChild.OnGotInputWhenDisabled();
}
else
{
@@ -663,15 +719,16 @@ namespace Avalonia.Controls
var sizeToContent = SizeToContent;
var clientSize = ClientSize;
var constraint = clientSize;
+ var maxAutoSize = PlatformImpl?.MaxAutoSizeHint ?? Size.Infinity;
if (sizeToContent.HasFlagCustom(SizeToContent.Width))
{
- constraint = constraint.WithWidth(double.PositiveInfinity);
+ constraint = constraint.WithWidth(maxAutoSize.Width);
}
if (sizeToContent.HasFlagCustom(SizeToContent.Height))
{
- constraint = constraint.WithHeight(double.PositiveInfinity);
+ constraint = constraint.WithHeight(maxAutoSize.Height);
}
var result = base.MeasureOverride(constraint);
diff --git a/src/Avalonia.DesignerSupport/Remote/PreviewerWindowImpl.cs b/src/Avalonia.DesignerSupport/Remote/PreviewerWindowImpl.cs
index 844489ef97..cd64af60e2 100644
--- a/src/Avalonia.DesignerSupport/Remote/PreviewerWindowImpl.cs
+++ b/src/Avalonia.DesignerSupport/Remote/PreviewerWindowImpl.cs
@@ -44,7 +44,7 @@ namespace Avalonia.DesignerSupport.Remote
public IPlatformHandle Handle { get; }
public WindowState WindowState { get; set; }
public Action WindowStateChanged { get; set; }
- public Size MaxClientSize { get; } = new Size(4096, 4096);
+ public Size MaxAutoSizeHint { get; } = new Size(4096, 4096);
public event Action LostFocus
{
add {}
diff --git a/src/Avalonia.DesignerSupport/Remote/Stubs.cs b/src/Avalonia.DesignerSupport/Remote/Stubs.cs
index 64b3af4ea2..b001bc1b76 100644
--- a/src/Avalonia.DesignerSupport/Remote/Stubs.cs
+++ b/src/Avalonia.DesignerSupport/Remote/Stubs.cs
@@ -19,7 +19,7 @@ namespace Avalonia.DesignerSupport.Remote
public Action Deactivated { get; set; }
public Action Activated { get; set; }
public IPlatformHandle Handle { get; }
- public Size MaxClientSize { get; }
+ public Size MaxAutoSizeHint { get; }
public Size ClientSize { get; }
public double Scaling { get; } = 1.0;
public IEnumerable Surfaces { get; }
diff --git a/src/Avalonia.Layout/ILayoutManager.cs b/src/Avalonia.Layout/ILayoutManager.cs
index c3675c18a2..6e63d3edbb 100644
--- a/src/Avalonia.Layout/ILayoutManager.cs
+++ b/src/Avalonia.Layout/ILayoutManager.cs
@@ -1,3 +1,6 @@
+using System;
+
+#nullable enable
namespace Avalonia.Layout
{
@@ -6,6 +9,11 @@ namespace Avalonia.Layout
///
public interface ILayoutManager
{
+ ///
+ /// Raised when the layout manager completes a layout pass.
+ ///
+ event EventHandler LayoutUpdated;
+
///
/// Notifies the layout manager that a control requires a measure.
///
diff --git a/src/Avalonia.Layout/ILayoutRoot.cs b/src/Avalonia.Layout/ILayoutRoot.cs
index 56aca75871..e2f16b338a 100644
--- a/src/Avalonia.Layout/ILayoutRoot.cs
+++ b/src/Avalonia.Layout/ILayoutRoot.cs
@@ -10,11 +10,6 @@ namespace Avalonia.Layout
///
Size ClientSize { get; }
- ///
- /// The maximum client size available.
- ///
- Size MaxClientSize { get; }
-
///
/// The scaling factor to use in layout.
///
diff --git a/src/Avalonia.Layout/ILayoutable.cs b/src/Avalonia.Layout/ILayoutable.cs
index 5c785613a9..316a017f1d 100644
--- a/src/Avalonia.Layout/ILayoutable.cs
+++ b/src/Avalonia.Layout/ILayoutable.cs
@@ -1,5 +1,7 @@
using Avalonia.VisualTree;
+#nullable enable
+
namespace Avalonia.Layout
{
///
diff --git a/src/Avalonia.Layout/LayoutManager.cs b/src/Avalonia.Layout/LayoutManager.cs
index de255331bc..1c0c736b78 100644
--- a/src/Avalonia.Layout/LayoutManager.cs
+++ b/src/Avalonia.Layout/LayoutManager.cs
@@ -3,6 +3,8 @@ using System.Diagnostics;
using Avalonia.Logging;
using Avalonia.Threading;
+#nullable enable
+
namespace Avalonia.Layout
{
///
@@ -21,10 +23,12 @@ namespace Avalonia.Layout
_executeLayoutPass = ExecuteLayoutPass;
}
+ public event EventHandler? LayoutUpdated;
+
///
public void InvalidateMeasure(ILayoutable control)
{
- Contract.Requires(control != null);
+ control = control ?? throw new ArgumentNullException(nameof(control));
Dispatcher.UIThread.VerifyAccess();
if (!control.IsAttachedToVisualTree)
@@ -45,7 +49,7 @@ namespace Avalonia.Layout
///
public void InvalidateArrange(ILayoutable control)
{
- Contract.Requires(control != null);
+ control = control ?? throw new ArgumentNullException(nameof(control));
Dispatcher.UIThread.VerifyAccess();
if (!control.IsAttachedToVisualTree)
@@ -73,7 +77,7 @@ namespace Avalonia.Layout
{
_running = true;
- Stopwatch stopwatch = null;
+ Stopwatch? stopwatch = null;
const LogEventLevel timingLogLevel = LogEventLevel.Information;
bool captureTiming = Logger.IsEnabled(timingLogLevel, LogArea.Layout);
@@ -116,13 +120,14 @@ namespace Avalonia.Layout
if (captureTiming)
{
- stopwatch.Stop();
+ stopwatch!.Stop();
Logger.TryGet(timingLogLevel, LogArea.Layout)?.Log(this, "Layout pass finished in {Time}", stopwatch.Elapsed);
}
}
_queued = false;
+ LayoutUpdated?.Invoke(this, EventArgs.Empty);
}
///
diff --git a/src/Avalonia.Layout/Layoutable.cs b/src/Avalonia.Layout/Layoutable.cs
index 0dd9a56485..8d2a825fa0 100644
--- a/src/Avalonia.Layout/Layoutable.cs
+++ b/src/Avalonia.Layout/Layoutable.cs
@@ -1,8 +1,9 @@
using System;
using Avalonia.Logging;
-using Avalonia.Utilities;
using Avalonia.VisualTree;
+#nullable enable
+
namespace Avalonia.Layout
{
///
@@ -131,6 +132,7 @@ namespace Avalonia.Layout
private bool _measuring;
private Size? _previousMeasure;
private Rect? _previousArrange;
+ private EventHandler? _layoutUpdated;
///
/// Initializes static members of the class.
@@ -153,7 +155,28 @@ namespace Avalonia.Layout
///
/// Occurs when a layout pass completes for the control.
///
- public event EventHandler LayoutUpdated;
+ public event EventHandler? LayoutUpdated
+ {
+ add
+ {
+ if (_layoutUpdated is null && VisualRoot is ILayoutRoot r)
+ {
+ r.LayoutManager.LayoutUpdated += LayoutManagedLayoutUpdated;
+ }
+
+ _layoutUpdated += value;
+ }
+
+ remove
+ {
+ _layoutUpdated -= value;
+
+ if (_layoutUpdated is null && VisualRoot is ILayoutRoot r)
+ {
+ r.LayoutManager.LayoutUpdated -= LayoutManagedLayoutUpdated;
+ }
+ }
+ }
///
/// Gets or sets the width of the element.
@@ -358,12 +381,9 @@ namespace Avalonia.Layout
IsArrangeValid = true;
ArrangeCore(rect);
_previousArrange = rect;
-
- LayoutUpdated?.Invoke(this, EventArgs.Empty);
}
}
-
///
/// Called by InvalidateMeasure
///
@@ -693,6 +713,26 @@ namespace Avalonia.Layout
InvalidateMeasure();
}
+ protected override void OnAttachedToVisualTreeCore(VisualTreeAttachmentEventArgs e)
+ {
+ base.OnAttachedToVisualTreeCore(e);
+
+ if (_layoutUpdated is object && e.Root is ILayoutRoot r)
+ {
+ r.LayoutManager.LayoutUpdated += LayoutManagedLayoutUpdated;
+ }
+ }
+
+ protected override void OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
+ {
+ base.OnDetachedFromVisualTreeCore(e);
+
+ if (_layoutUpdated is object && e.Root is ILayoutRoot r)
+ {
+ r.LayoutManager.LayoutUpdated -= LayoutManagedLayoutUpdated;
+ }
+ }
+
///
protected sealed override void OnVisualParentChanged(IVisual oldParent, IVisual newParent)
{
@@ -701,6 +741,13 @@ namespace Avalonia.Layout
base.OnVisualParentChanged(oldParent, newParent);
}
+ ///
+ /// Called when the layout manager raises a LayoutUpdated event.
+ ///
+ /// The sender.
+ /// The event args.
+ private void LayoutManagedLayoutUpdated(object sender, EventArgs e) => _layoutUpdated?.Invoke(this, e);
+
///
/// Tests whether any of a 's properties include negative values,
/// a NaN or Infinity.
diff --git a/src/Avalonia.Native/WindowImplBase.cs b/src/Avalonia.Native/WindowImplBase.cs
index a7ca528b2b..9783454e0e 100644
--- a/src/Avalonia.Native/WindowImplBase.cs
+++ b/src/Avalonia.Native/WindowImplBase.cs
@@ -336,7 +336,7 @@ namespace Avalonia.Native
_native.BeginMoveDrag();
}
- public Size MaxClientSize => Screen.AllScreens.Select(s => s.Bounds.Size.ToSize(s.PixelDensity))
+ public Size MaxAutoSizeHint => Screen.AllScreens.Select(s => s.Bounds.Size.ToSize(s.PixelDensity))
.OrderByDescending(x => x.Width + x.Height).FirstOrDefault();
public void SetTopmost(bool value)
diff --git a/src/Avalonia.Themes.Default/ContextMenu.xaml b/src/Avalonia.Themes.Default/ContextMenu.xaml
index 75f8f7c23d..9b84253c8a 100644
--- a/src/Avalonia.Themes.Default/ContextMenu.xaml
+++ b/src/Avalonia.Themes.Default/ContextMenu.xaml
@@ -10,7 +10,7 @@
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
-
+
-
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Avalonia.Themes.Fluent/Avalonia.Themes.Fluent.csproj b/src/Avalonia.Themes.Fluent/Avalonia.Themes.Fluent.csproj
index 3c3e14010d..a4eab83e4a 100644
--- a/src/Avalonia.Themes.Fluent/Avalonia.Themes.Fluent.csproj
+++ b/src/Avalonia.Themes.Fluent/Avalonia.Themes.Fluent.csproj
@@ -19,4 +19,4 @@
-
+
\ No newline at end of file
diff --git a/src/Avalonia.Visuals/Animation/Animators/TransformAnimator.cs b/src/Avalonia.Visuals/Animation/Animators/TransformAnimator.cs
index 59a52fe0bd..1b2142f6c9 100644
--- a/src/Avalonia.Visuals/Animation/Animators/TransformAnimator.cs
+++ b/src/Avalonia.Visuals/Animation/Animators/TransformAnimator.cs
@@ -1,6 +1,8 @@
using System;
+using System.Reactive.Disposables;
using Avalonia.Logging;
using Avalonia.Media;
+using Avalonia.Media.Transformation;
namespace Avalonia.Animation.Animators
{
@@ -19,6 +21,12 @@ namespace Avalonia.Animation.Animators
// Check if the Target Property is Transform derived.
if (typeof(Transform).IsAssignableFrom(Property.OwnerType))
{
+ if (ctrl.RenderTransform is TransformOperations)
+ {
+ // HACK: This animator cannot reasonably animate CSS transforms at the moment.
+ return Disposable.Empty;
+ }
+
if (ctrl.RenderTransform == null)
{
var normalTransform = new TransformGroup();
@@ -51,7 +59,7 @@ namespace Avalonia.Animation.Animators
// It's a transform object so let's target that.
if (renderTransformType == Property.OwnerType)
{
- return _doubleAnimator.Apply(animation, ctrl.RenderTransform, clock ?? control.Clock, obsMatch, onComplete);
+ return _doubleAnimator.Apply(animation, (Transform) ctrl.RenderTransform, clock ?? control.Clock, obsMatch, onComplete);
}
// It's a TransformGroup and try finding the target there.
else if (renderTransformType == typeof(TransformGroup))
diff --git a/src/Avalonia.Visuals/Animation/Animators/TransformOperationsAnimator.cs b/src/Avalonia.Visuals/Animation/Animators/TransformOperationsAnimator.cs
new file mode 100644
index 0000000000..8e9d20eb8f
--- /dev/null
+++ b/src/Avalonia.Visuals/Animation/Animators/TransformOperationsAnimator.cs
@@ -0,0 +1,35 @@
+using System;
+using Avalonia.Media;
+using Avalonia.Media.Transformation;
+
+namespace Avalonia.Animation.Animators
+{
+ public class TransformOperationsAnimator : Animator
+ {
+ public TransformOperationsAnimator()
+ {
+ Validate = ValidateTransform;
+ }
+
+ public override TransformOperations Interpolate(double progress, TransformOperations oldValue, TransformOperations newValue)
+ {
+ var oldTransform = EnsureOperations(oldValue);
+ var newTransform = EnsureOperations(newValue);
+
+ return TransformOperations.Interpolate(oldTransform, newTransform, progress);
+ }
+
+ internal static TransformOperations EnsureOperations(ITransform value)
+ {
+ return value as TransformOperations ?? TransformOperations.Identity;
+ }
+
+ private void ValidateTransform(AnimatorKeyFrame kf)
+ {
+ if (!(kf.Value is TransformOperations))
+ {
+ throw new InvalidOperationException($"All keyframes must be of type {typeof(TransformOperations)}.");
+ }
+ }
+ }
+}
diff --git a/src/Avalonia.Visuals/Animation/Transitions/TransformOperationsTransition.cs b/src/Avalonia.Visuals/Animation/Transitions/TransformOperationsTransition.cs
new file mode 100644
index 0000000000..104acb71ad
--- /dev/null
+++ b/src/Avalonia.Visuals/Animation/Transitions/TransformOperationsTransition.cs
@@ -0,0 +1,28 @@
+using System;
+using System.Reactive.Linq;
+using Avalonia.Animation.Animators;
+using Avalonia.Media;
+
+namespace Avalonia.Animation
+{
+ public class TransformOperationsTransition : Transition
+ {
+ private static readonly TransformOperationsAnimator _operationsAnimator = new TransformOperationsAnimator();
+
+ public override IObservable DoTransition(IObservable progress,
+ ITransform oldValue,
+ ITransform newValue)
+ {
+ var oldTransform = TransformOperationsAnimator.EnsureOperations(oldValue);
+ var newTransform = TransformOperationsAnimator.EnsureOperations(newValue);
+
+ return progress
+ .Select(p =>
+ {
+ var f = Easing.Ease(p);
+
+ return _operationsAnimator.Interpolate(f, oldTransform, newTransform);
+ });
+ }
+ }
+}
diff --git a/src/Avalonia.Visuals/Matrix.cs b/src/Avalonia.Visuals/Matrix.cs
index 898c6027a5..206b842220 100644
--- a/src/Avalonia.Visuals/Matrix.cs
+++ b/src/Avalonia.Visuals/Matrix.cs
@@ -54,7 +54,7 @@ namespace Avalonia
///
/// HasInverse Property - returns true if this matrix is invertible, false otherwise.
///
- public bool HasInverse => GetDeterminant() != 0;
+ public bool HasInverse => !MathUtilities.IsZero(GetDeterminant());
///
/// The first element of the first row
@@ -286,7 +286,7 @@ namespace Avalonia
{
double d = GetDeterminant();
- if (d == 0)
+ if (MathUtilities.IsZero(d))
{
throw new InvalidOperationException("Transform is not invertible.");
}
@@ -319,5 +319,76 @@ namespace Avalonia
);
}
}
+
+ ///
+ /// Decomposes given matrix into transform operations.
+ ///
+ /// Matrix to decompose.
+ /// Decomposed matrix.
+ /// The status of the operation.
+ public static bool TryDecomposeTransform(Matrix matrix, out Decomposed decomposed)
+ {
+ decomposed = default;
+
+ var determinant = matrix.GetDeterminant();
+
+ if (MathUtilities.IsZero(determinant))
+ {
+ return false;
+ }
+
+ var m11 = matrix.M11;
+ var m21 = matrix.M21;
+ var m12 = matrix.M12;
+ var m22 = matrix.M22;
+
+ // Translation.
+ decomposed.Translate = new Vector(matrix.M31, matrix.M32);
+
+ // Scale sign.
+ var scaleX = 1d;
+ var scaleY = 1d;
+
+ if (determinant < 0)
+ {
+ if (m11 < m22)
+ {
+ scaleX *= -1d;
+ }
+ else
+ {
+ scaleY *= -1d;
+ }
+ }
+
+ // X Scale.
+ scaleX *= Math.Sqrt(m11 * m11 + m12 * m12);
+
+ m11 /= scaleX;
+ m12 /= scaleX;
+
+ // XY Shear.
+ double scaledShear = m11 * m21 + m12 * m22;
+
+ m21 -= m11 * scaledShear;
+ m22 -= m12 * scaledShear;
+
+ // Y Scale.
+ scaleY *= Math.Sqrt(m21 * m21 + m22 * m22);
+
+ decomposed.Scale = new Vector(scaleX, scaleY);
+ decomposed.Skew = new Vector(scaledShear / scaleY, 0d);
+ decomposed.Angle = Math.Atan2(m12, m11);
+
+ return true;
+ }
+
+ public struct Decomposed
+ {
+ public Vector Translate;
+ public Vector Scale;
+ public Vector Skew;
+ public double Angle;
+ }
}
}
diff --git a/src/Avalonia.Visuals/Media/Color.cs b/src/Avalonia.Visuals/Media/Color.cs
index 2e06d2578f..052ee5e1b7 100644
--- a/src/Avalonia.Visuals/Media/Color.cs
+++ b/src/Avalonia.Visuals/Media/Color.cs
@@ -89,33 +89,64 @@ namespace Avalonia.Media
/// The .
public static Color Parse(string s)
{
- if (s == null) throw new ArgumentNullException(nameof(s));
- if (s.Length == 0) throw new FormatException();
+ if (TryParse(s, out Color color))
+ {
+ return color;
+ }
- if (s[0] == '#')
+ throw new FormatException($"Invalid color string: '{s}'.");
+ }
+
+ ///
+ /// Parses a color string.
+ ///
+ /// The color string.
+ /// The .
+ public static Color Parse(ReadOnlySpan s)
+ {
+ if (TryParse(s, out Color color))
{
- var or = 0u;
+ return color;
+ }
- if (s.Length == 7)
- {
- or = 0xff000000;
- }
- else if (s.Length != 9)
- {
- throw new FormatException($"Invalid color string: '{s}'.");
- }
+ throw new FormatException($"Invalid color string: '{s.ToString()}'.");
+ }
- return FromUInt32(uint.Parse(s.Substring(1), NumberStyles.HexNumber, CultureInfo.InvariantCulture) | or);
+ ///
+ /// Parses a color string.
+ ///
+ /// The color string.
+ /// The parsed color
+ /// The status of the operation.
+ public static bool TryParse(string s, out Color color)
+ {
+ if (s == null)
+ {
+ throw new ArgumentNullException(nameof(s));
+ }
+
+ if (s.Length == 0)
+ {
+ throw new FormatException();
+ }
+
+ if (s[0] == '#' && TryParseInternal(s.AsSpan(), out color))
+ {
+ return true;
}
var knownColor = KnownColors.GetKnownColor(s);
if (knownColor != KnownColor.None)
{
- return knownColor.ToColor();
+ color = knownColor.ToColor();
+
+ return true;
}
- throw new FormatException($"Invalid color string: '{s}'.");
+ color = default;
+
+ return false;
}
///
@@ -126,40 +157,79 @@ namespace Avalonia.Media
/// The status of the operation.
public static bool TryParse(ReadOnlySpan s, out Color color)
{
- color = default;
- if (s == null)
- return false;
if (s.Length == 0)
+ {
+ color = default;
+
return false;
+ }
if (s[0] == '#')
{
- var or = 0u;
+ return TryParseInternal(s, out color);
+ }
+
+ var knownColor = KnownColors.GetKnownColor(s.ToString());
+
+ if (knownColor != KnownColor.None)
+ {
+ color = knownColor.ToColor();
+
+ return true;
+ }
+
+ color = default;
+
+ return false;
+ }
+
+ private static bool TryParseInternal(ReadOnlySpan s, out Color color)
+ {
+ static bool TryParseCore(ReadOnlySpan input, ref Color color)
+ {
+ var alphaComponent = 0u;
- if (s.Length == 7)
+ if (input.Length == 6)
{
- or = 0xff000000;
+ alphaComponent = 0xff000000;
}
- else if (s.Length != 9)
+ else if (input.Length != 8)
{
return false;
}
- if(!uint.TryParse(s.Slice(1).ToString(), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var parsed))
+ // TODO: (netstandard 2.1) Can use allocation free parsing.
+ if (!uint.TryParse(input.ToString(), NumberStyles.HexNumber, CultureInfo.InvariantCulture,
+ out var parsed))
+ {
return false;
- color = FromUInt32(parsed| or);
+ }
+
+ color = FromUInt32(parsed | alphaComponent);
+
return true;
}
- var knownColor = KnownColors.GetKnownColor(s.ToString());
+ color = default;
- if (knownColor != KnownColor.None)
+ ReadOnlySpan input = s.Slice(1);
+
+ // Handle shorthand cases like #FFF (RGB) or #FFFF (ARGB).
+ if (input.Length == 3 || input.Length == 4)
{
- color = knownColor.ToColor();
- return true;
+ var extendedLength = 2 * input.Length;
+ Span extended = stackalloc char[extendedLength];
+
+ for (int i = 0; i < input.Length; i++)
+ {
+ extended[2 * i + 0] = input[i];
+ extended[2 * i + 1] = input[i];
+ }
+
+ return TryParseCore(extended, ref color);
}
- return false;
+ return TryParseCore(input, ref color);
}
///
diff --git a/src/Avalonia.Visuals/Media/DrawingContext.cs b/src/Avalonia.Visuals/Media/DrawingContext.cs
index b1cf1aecc9..ba7191d7a6 100644
--- a/src/Avalonia.Visuals/Media/DrawingContext.cs
+++ b/src/Avalonia.Visuals/Media/DrawingContext.cs
@@ -283,6 +283,12 @@ namespace Avalonia.Media
}
+ public PushedState PushClip(RoundedRect clip)
+ {
+ PlatformImpl.PushClip(clip);
+ return new PushedState(this, PushedState.PushedStateType.Clip);
+ }
+
///
/// Pushes a clip rectangle.
///
diff --git a/src/Avalonia.Visuals/Media/IMutableTransform.cs b/src/Avalonia.Visuals/Media/IMutableTransform.cs
new file mode 100644
index 0000000000..2033c434c0
--- /dev/null
+++ b/src/Avalonia.Visuals/Media/IMutableTransform.cs
@@ -0,0 +1,12 @@
+using System;
+
+namespace Avalonia.Media
+{
+ public interface IMutableTransform : ITransform
+ {
+ ///
+ /// Raised when the transform changes.
+ ///
+ event EventHandler Changed;
+ }
+}
diff --git a/src/Avalonia.Visuals/Media/ITransform.cs b/src/Avalonia.Visuals/Media/ITransform.cs
new file mode 100644
index 0000000000..91577fe38e
--- /dev/null
+++ b/src/Avalonia.Visuals/Media/ITransform.cs
@@ -0,0 +1,10 @@
+using System.ComponentModel;
+
+namespace Avalonia.Media
+{
+ [TypeConverter(typeof(TransformConverter))]
+ public interface ITransform
+ {
+ Matrix Value { get; }
+ }
+}
diff --git a/src/Avalonia.Visuals/Media/Transform.cs b/src/Avalonia.Visuals/Media/Transform.cs
index 70ef1eaaf4..7cf1b35ada 100644
--- a/src/Avalonia.Visuals/Media/Transform.cs
+++ b/src/Avalonia.Visuals/Media/Transform.cs
@@ -8,11 +8,12 @@ namespace Avalonia.Media
///
/// Represents a transform on an .
///
- public abstract class Transform : Animatable
+ public abstract class Transform : Animatable, IMutableTransform
{
static Transform()
{
- Animation.Animation.RegisterAnimator(prop => typeof(Transform).IsAssignableFrom(prop.OwnerType));
+ Animation.Animation.RegisterAnimator(prop =>
+ typeof(ITransform).IsAssignableFrom(prop.OwnerType));
}
///
diff --git a/src/Avalonia.Visuals/Media/TransformConverter.cs b/src/Avalonia.Visuals/Media/TransformConverter.cs
new file mode 100644
index 0000000000..e79c0b8b7b
--- /dev/null
+++ b/src/Avalonia.Visuals/Media/TransformConverter.cs
@@ -0,0 +1,23 @@
+using System;
+using System.ComponentModel;
+using System.Globalization;
+using Avalonia.Media.Transformation;
+
+namespace Avalonia.Media
+{
+ ///
+ /// Creates an from a string representation.
+ ///
+ public class TransformConverter : TypeConverter
+ {
+ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
+ {
+ return sourceType == typeof(string);
+ }
+
+ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
+ {
+ return TransformOperations.Parse((string)value);
+ }
+ }
+}
diff --git a/src/Avalonia.Visuals/Media/Transformation/InterpolationUtilities.cs b/src/Avalonia.Visuals/Media/Transformation/InterpolationUtilities.cs
new file mode 100644
index 0000000000..1e80eabfc8
--- /dev/null
+++ b/src/Avalonia.Visuals/Media/Transformation/InterpolationUtilities.cs
@@ -0,0 +1,40 @@
+namespace Avalonia.Media.Transformation
+{
+ internal static class InterpolationUtilities
+ {
+ public static double InterpolateScalars(double from, double to, double progress)
+ {
+ return from * (1d - progress) + to * progress;
+ }
+
+ public static Vector InterpolateVectors(Vector from, Vector to, double progress)
+ {
+ var x = InterpolateScalars(from.X, to.X, progress);
+ var y = InterpolateScalars(from.Y, to.Y, progress);
+
+ return new Vector(x, y);
+ }
+
+ public static Matrix ComposeTransform(Matrix.Decomposed decomposed)
+ {
+ // According to https://www.w3.org/TR/css-transforms-1/#recomposing-to-a-2d-matrix
+
+ return Matrix.CreateTranslation(decomposed.Translate) *
+ Matrix.CreateRotation(decomposed.Angle) *
+ Matrix.CreateSkew(decomposed.Skew.X, decomposed.Skew.Y) *
+ Matrix.CreateScale(decomposed.Scale);
+ }
+
+ public static Matrix.Decomposed InterpolateDecomposedTransforms(ref Matrix.Decomposed from, ref Matrix.Decomposed to, double progres)
+ {
+ Matrix.Decomposed result = default;
+
+ result.Translate = InterpolateVectors(from.Translate, to.Translate, progres);
+ result.Scale = InterpolateVectors(from.Scale, to.Scale, progres);
+ result.Skew = InterpolateVectors(from.Skew, to.Skew, progres);
+ result.Angle = InterpolateScalars(from.Angle, to.Angle, progres);
+
+ return result;
+ }
+ }
+}
diff --git a/src/Avalonia.Visuals/Media/Transformation/TransformOperation.cs b/src/Avalonia.Visuals/Media/Transformation/TransformOperation.cs
new file mode 100644
index 0000000000..36f5dd98f1
--- /dev/null
+++ b/src/Avalonia.Visuals/Media/Transformation/TransformOperation.cs
@@ -0,0 +1,230 @@
+using System.Runtime.InteropServices;
+
+namespace Avalonia.Media.Transformation
+{
+ ///
+ /// Represents a single primitive transform (like translation, rotation, scale, etc.).
+ ///
+ public struct TransformOperation
+ {
+ public OperationType Type;
+ public Matrix Matrix;
+ public DataLayout Data;
+
+ public enum OperationType
+ {
+ Translate,
+ Rotate,
+ Scale,
+ Skew,
+ Matrix,
+ Identity
+ }
+
+ ///
+ /// Returns whether operation produces the identity matrix.
+ ///
+ public bool IsIdentity => Matrix.IsIdentity;
+
+ ///
+ /// Bakes this operation to a transform matrix.
+ ///
+ public void Bake()
+ {
+ Matrix = Matrix.Identity;
+
+ switch (Type)
+ {
+ case OperationType.Translate:
+ {
+ Matrix = Matrix.CreateTranslation(Data.Translate.X, Data.Translate.Y);
+
+ break;
+ }
+ case OperationType.Rotate:
+ {
+ Matrix = Matrix.CreateRotation(Data.Rotate.Angle);
+
+ break;
+ }
+ case OperationType.Scale:
+ {
+ Matrix = Matrix.CreateScale(Data.Scale.X, Data.Scale.Y);
+
+ break;
+ }
+ case OperationType.Skew:
+ {
+ Matrix = Matrix.CreateSkew(Data.Skew.X, Data.Skew.Y);
+
+ break;
+ }
+ }
+ }
+
+ ///
+ /// Returns new identity transform operation.
+ ///
+ public static TransformOperation Identity =>
+ new TransformOperation { Matrix = Matrix.Identity, Type = OperationType.Identity };
+
+ ///
+ /// Attempts to interpolate between two transform operations.
+ ///
+ /// Source operation.
+ /// Target operation.
+ /// Interpolation progress.
+ /// Interpolation result that will be filled in when operation was successful.
+ ///
+ /// Based upon https://www.w3.org/TR/css-transforms-1/#interpolation-of-transform-functions.
+ ///
+ public static bool TryInterpolate(TransformOperation? from, TransformOperation? to, double progress,
+ ref TransformOperation result)
+ {
+ bool fromIdentity = IsOperationIdentity(ref from);
+ bool toIdentity = IsOperationIdentity(ref to);
+
+ if (fromIdentity && toIdentity)
+ {
+ return true;
+ }
+
+ // ReSharper disable PossibleInvalidOperationException
+ TransformOperation fromValue = fromIdentity ? Identity : from.Value;
+ TransformOperation toValue = toIdentity ? Identity : to.Value;
+ // ReSharper restore PossibleInvalidOperationException
+
+ var interpolationType = toIdentity ? fromValue.Type : toValue.Type;
+
+ result.Type = interpolationType;
+
+ switch (interpolationType)
+ {
+ case OperationType.Translate:
+ {
+ double fromX = fromIdentity ? 0 : fromValue.Data.Translate.X;
+ double fromY = fromIdentity ? 0 : fromValue.Data.Translate.Y;
+
+ double toX = toIdentity ? 0 : toValue.Data.Translate.X;
+ double toY = toIdentity ? 0 : toValue.Data.Translate.Y;
+
+ result.Data.Translate.X = InterpolationUtilities.InterpolateScalars(fromX, toX, progress);
+ result.Data.Translate.Y = InterpolationUtilities.InterpolateScalars(fromY, toY, progress);
+
+ result.Bake();
+
+ break;
+ }
+ case OperationType.Rotate:
+ {
+ double fromAngle = fromIdentity ? 0 : fromValue.Data.Rotate.Angle;
+
+ double toAngle = toIdentity ? 0 : toValue.Data.Rotate.Angle;
+
+ result.Data.Rotate.Angle = InterpolationUtilities.InterpolateScalars(fromAngle, toAngle, progress);
+
+ result.Bake();
+
+ break;
+ }
+ case OperationType.Scale:
+ {
+ double fromX = fromIdentity ? 1 : fromValue.Data.Scale.X;
+ double fromY = fromIdentity ? 1 : fromValue.Data.Scale.Y;
+
+ double toX = toIdentity ? 1 : toValue.Data.Scale.X;
+ double toY = toIdentity ? 1 : toValue.Data.Scale.Y;
+
+ result.Data.Scale.X = InterpolationUtilities.InterpolateScalars(fromX, toX, progress);
+ result.Data.Scale.Y = InterpolationUtilities.InterpolateScalars(fromY, toY, progress);
+
+ result.Bake();
+
+ break;
+ }
+ case OperationType.Skew:
+ {
+ double fromX = fromIdentity ? 0 : fromValue.Data.Skew.X;
+ double fromY = fromIdentity ? 0 : fromValue.Data.Skew.Y;
+
+ double toX = toIdentity ? 0 : toValue.Data.Skew.X;
+ double toY = toIdentity ? 0 : toValue.Data.Skew.Y;
+
+ result.Data.Skew.X = InterpolationUtilities.InterpolateScalars(fromX, toX, progress);
+ result.Data.Skew.Y = InterpolationUtilities.InterpolateScalars(fromY, toY, progress);
+
+ result.Bake();
+
+ break;
+ }
+ case OperationType.Matrix:
+ {
+ var fromMatrix = fromIdentity ? Matrix.Identity : fromValue.Matrix;
+ var toMatrix = toIdentity ? Matrix.Identity : toValue.Matrix;
+
+ if (!Matrix.TryDecomposeTransform(fromMatrix, out Matrix.Decomposed fromDecomposed) ||
+ !Matrix.TryDecomposeTransform(toMatrix, out Matrix.Decomposed toDecomposed))
+ {
+ return false;
+ }
+
+ var interpolated =
+ InterpolationUtilities.InterpolateDecomposedTransforms(
+ ref fromDecomposed, ref toDecomposed,
+ progress);
+
+ result.Matrix = InterpolationUtilities.ComposeTransform(interpolated);
+
+ break;
+ }
+ case OperationType.Identity:
+ {
+ // Do nothing.
+ break;
+ }
+ }
+
+ return true;
+ }
+
+ private static bool IsOperationIdentity(ref TransformOperation? operation)
+ {
+ return !operation.HasValue || operation.Value.IsIdentity;
+ }
+
+ [StructLayout(LayoutKind.Explicit)]
+ public struct DataLayout
+ {
+ [FieldOffset(0)] public SkewLayout Skew;
+
+ [FieldOffset(0)] public ScaleLayout Scale;
+
+ [FieldOffset(0)] public TranslateLayout Translate;
+
+ [FieldOffset(0)] public RotateLayout Rotate;
+
+ public struct SkewLayout
+ {
+ public double X;
+ public double Y;
+ }
+
+ public struct ScaleLayout
+ {
+ public double X;
+ public double Y;
+ }
+
+ public struct TranslateLayout
+ {
+ public double X;
+ public double Y;
+ }
+
+ public struct RotateLayout
+ {
+ public double Angle;
+ }
+ }
+ }
+}
diff --git a/src/Avalonia.Visuals/Media/Transformation/TransformOperations.cs b/src/Avalonia.Visuals/Media/Transformation/TransformOperations.cs
new file mode 100644
index 0000000000..334bb93562
--- /dev/null
+++ b/src/Avalonia.Visuals/Media/Transformation/TransformOperations.cs
@@ -0,0 +1,258 @@
+using System;
+using System.Collections.Generic;
+
+namespace Avalonia.Media.Transformation
+{
+ ///
+ /// Contains a list of that represent primitive transforms that will be
+ /// applied in declared order.
+ ///
+ public sealed class TransformOperations : ITransform
+ {
+ public static TransformOperations Identity { get; } = new TransformOperations(new List());
+
+ private readonly List _operations;
+
+ private TransformOperations(List operations)
+ {
+ _operations = operations ?? throw new ArgumentNullException(nameof(operations));
+
+ IsIdentity = CheckIsIdentity();
+
+ Value = ApplyTransforms();
+ }
+
+ ///
+ /// Returns whether all operations combined together produce the identity matrix.
+ ///
+ public bool IsIdentity { get; }
+
+ public IReadOnlyList Operations => _operations;
+
+ public Matrix Value { get; }
+
+ public static TransformOperations Parse(string s)
+ {
+ return TransformParser.Parse(s);
+ }
+
+ public static Builder CreateBuilder(int capacity)
+ {
+ return new Builder(capacity);
+ }
+
+ public static TransformOperations Interpolate(TransformOperations from, TransformOperations to, double progress)
+ {
+ TransformOperations result = Identity;
+
+ if (!TryInterpolate(from, to, progress, ref result))
+ {
+ // If the matrices cannot be interpolated, fallback to discrete animation logic.
+ // See https://drafts.csswg.org/css-transforms/#matrix-interpolation
+ result = progress < 0.5 ? from : to;
+ }
+
+ return result;
+ }
+
+ private Matrix ApplyTransforms(int startOffset = 0)
+ {
+ Matrix matrix = Matrix.Identity;
+
+ for (var i = startOffset; i < _operations.Count; i++)
+ {
+ TransformOperation operation = _operations[i];
+ matrix *= operation.Matrix;
+ }
+
+ return matrix;
+ }
+
+ private bool CheckIsIdentity()
+ {
+ foreach (TransformOperation operation in _operations)
+ {
+ if (!operation.IsIdentity)
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private static bool TryInterpolate(TransformOperations from, TransformOperations to, double progress, ref TransformOperations result)
+ {
+ bool fromIdentity = from.IsIdentity;
+ bool toIdentity = to.IsIdentity;
+
+ if (fromIdentity && toIdentity)
+ {
+ return true;
+ }
+
+ int matchingPrefixLength = ComputeMatchingPrefixLength(from, to);
+ int fromSize = fromIdentity ? 0 : from._operations.Count;
+ int toSize = toIdentity ? 0 : to._operations.Count;
+ int numOperations = Math.Max(fromSize, toSize);
+
+ var builder = new Builder(matchingPrefixLength);
+
+ for (int i = 0; i < matchingPrefixLength; i++)
+ {
+ TransformOperation interpolated = new TransformOperation
+ {
+ Type = TransformOperation.OperationType.Identity
+ };
+
+ if (!TransformOperation.TryInterpolate(
+ i >= fromSize ? default(TransformOperation?) : from._operations[i],
+ i >= toSize ? default(TransformOperation?) : to._operations[i],
+ progress,
+ ref interpolated))
+ {
+ return false;
+ }
+
+ builder.Append(interpolated);
+ }
+
+ if (matchingPrefixLength < numOperations)
+ {
+ if (!ComputeDecomposedTransform(from, matchingPrefixLength, out Matrix.Decomposed fromDecomposed) ||
+ !ComputeDecomposedTransform(to, matchingPrefixLength, out Matrix.Decomposed toDecomposed))
+ {
+ return false;
+ }
+
+ var transform = InterpolationUtilities.InterpolateDecomposedTransforms(ref fromDecomposed, ref toDecomposed, progress);
+
+ builder.AppendMatrix(InterpolationUtilities.ComposeTransform(transform));
+ }
+
+ result = builder.Build();
+
+ return true;
+ }
+
+ private static bool ComputeDecomposedTransform(TransformOperations operations, int startOffset, out Matrix.Decomposed decomposed)
+ {
+ Matrix transform = operations.ApplyTransforms(startOffset);
+
+ if (!Matrix.TryDecomposeTransform(transform, out decomposed))
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ private static int ComputeMatchingPrefixLength(TransformOperations from, TransformOperations to)
+ {
+ int numOperations = Math.Min(from._operations.Count, to._operations.Count);
+
+ for (int i = 0; i < numOperations; i++)
+ {
+ if (from._operations[i].Type != to._operations[i].Type)
+ {
+ return i;
+ }
+ }
+
+ // If the operations match to the length of the shorter list, then pad its
+ // length with the matching identity operations.
+ // https://drafts.csswg.org/css-transforms/#transform-function-lists
+ return Math.Max(from._operations.Count, to._operations.Count);
+ }
+
+ public readonly struct Builder
+ {
+ private readonly List _operations;
+
+ public Builder(int capacity)
+ {
+ _operations = new List(capacity);
+ }
+
+ public void AppendTranslate(double x, double y)
+ {
+ var toAdd = new TransformOperation();
+
+ toAdd.Type = TransformOperation.OperationType.Translate;
+ toAdd.Data.Translate.X = x;
+ toAdd.Data.Translate.Y = y;
+
+ toAdd.Bake();
+
+ _operations.Add(toAdd);
+ }
+
+ public void AppendRotate(double angle)
+ {
+ var toAdd = new TransformOperation();
+
+ toAdd.Type = TransformOperation.OperationType.Rotate;
+ toAdd.Data.Rotate.Angle = angle;
+
+ toAdd.Bake();
+
+ _operations.Add(toAdd);
+ }
+
+ public void AppendScale(double x, double y)
+ {
+ var toAdd = new TransformOperation();
+
+ toAdd.Type = TransformOperation.OperationType.Scale;
+ toAdd.Data.Scale.X = x;
+ toAdd.Data.Scale.Y = y;
+
+ toAdd.Bake();
+
+ _operations.Add(toAdd);
+ }
+
+ public void AppendSkew(double x, double y)
+ {
+ var toAdd = new TransformOperation();
+
+ toAdd.Type = TransformOperation.OperationType.Skew;
+ toAdd.Data.Skew.X = x;
+ toAdd.Data.Skew.Y = y;
+
+ toAdd.Bake();
+
+ _operations.Add(toAdd);
+ }
+
+ public void AppendMatrix(Matrix matrix)
+ {
+ var toAdd = new TransformOperation();
+
+ toAdd.Type = TransformOperation.OperationType.Matrix;
+ toAdd.Matrix = matrix;
+
+ _operations.Add(toAdd);
+ }
+
+ public void AppendIdentity()
+ {
+ var toAdd = new TransformOperation();
+
+ toAdd.Type = TransformOperation.OperationType.Identity;
+
+ _operations.Add(toAdd);
+ }
+
+ public void Append(TransformOperation toAdd)
+ {
+ _operations.Add(toAdd);
+ }
+
+ public TransformOperations Build()
+ {
+ return new TransformOperations(_operations);
+ }
+ }
+ }
+}
diff --git a/src/Avalonia.Visuals/Media/Transformation/TransformParser.cs b/src/Avalonia.Visuals/Media/Transformation/TransformParser.cs
new file mode 100644
index 0000000000..85f4f5fec1
--- /dev/null
+++ b/src/Avalonia.Visuals/Media/Transformation/TransformParser.cs
@@ -0,0 +1,444 @@
+using System;
+using System.Globalization;
+using Avalonia.Utilities;
+
+namespace Avalonia.Media.Transformation
+{
+ public static class TransformParser
+ {
+ private static readonly (string, TransformFunction)[] s_functionMapping =
+ {
+ ("translate", TransformFunction.Translate),
+ ("translateX", TransformFunction.TranslateX),
+ ("translateY", TransformFunction.TranslateY),
+ ("scale", TransformFunction.Scale),
+ ("scaleX", TransformFunction.ScaleX),
+ ("scaleY", TransformFunction.ScaleY),
+ ("skew", TransformFunction.Skew),
+ ("skewX", TransformFunction.SkewX),
+ ("skewY", TransformFunction.SkewY),
+ ("rotate", TransformFunction.Rotate),
+ ("matrix", TransformFunction.Matrix)
+ };
+
+ private static readonly (string, Unit)[] s_unitMapping =
+ {
+ ("deg", Unit.Degree),
+ ("grad", Unit.Gradian),
+ ("rad", Unit.Radian),
+ ("turn", Unit.Turn),
+ ("px", Unit.Pixel)
+ };
+
+ public static TransformOperations Parse(string s)
+ {
+ void ThrowInvalidFormat()
+ {
+ throw new FormatException($"Invalid transform string: '{s}'.");
+ }
+
+ if (string.IsNullOrEmpty(s))
+ {
+ throw new ArgumentException(nameof(s));
+ }
+
+ var span = s.AsSpan().Trim();
+
+ if (span.Equals("none".AsSpan(), StringComparison.OrdinalIgnoreCase))
+ {
+ return TransformOperations.Identity;
+ }
+
+ var builder = TransformOperations.CreateBuilder(0);
+
+ while (true)
+ {
+ var beginIndex = span.IndexOf('(');
+ var endIndex = span.IndexOf(')');
+
+ if (beginIndex == -1 || endIndex == -1)
+ {
+ ThrowInvalidFormat();
+ }
+
+ var namePart = span.Slice(0, beginIndex).Trim();
+
+ var function = ParseTransformFunction(in namePart);
+
+ if (function == TransformFunction.Invalid)
+ {
+ ThrowInvalidFormat();
+ }
+
+ var valuePart = span.Slice(beginIndex + 1, endIndex - beginIndex - 1).Trim();
+
+ ParseFunction(in valuePart, function, in builder);
+
+ span = span.Slice(endIndex + 1);
+
+ if (span.IsWhiteSpace())
+ {
+ break;
+ }
+ }
+
+ return builder.Build();
+ }
+
+ private static void ParseFunction(
+ in ReadOnlySpan functionPart,
+ TransformFunction function,
+ in TransformOperations.Builder builder)
+ {
+ static UnitValue ParseValue(ReadOnlySpan part)
+ {
+ int unitIndex = -1;
+
+ for (int i = 0; i < part.Length; i++)
+ {
+ char c = part[i];
+
+ if (char.IsDigit(c) || c == '-' || c == '.')
+ {
+ continue;
+ }
+
+ unitIndex = i;
+ break;
+ }
+
+ Unit unit = Unit.None;
+
+ if (unitIndex != -1)
+ {
+ var unitPart = part.Slice(unitIndex, part.Length - unitIndex);
+
+ unit = ParseUnit(unitPart);
+
+ part = part.Slice(0, unitIndex);
+ }
+
+ var value = double.Parse(part.ToString(), NumberStyles.Float, CultureInfo.InvariantCulture);
+
+ return new UnitValue(unit, value);
+ }
+
+ static int ParseValuePair(
+ in ReadOnlySpan part,
+ ref UnitValue leftValue,
+ ref UnitValue rightValue)
+ {
+ var commaIndex = part.IndexOf(',');
+
+ if (commaIndex != -1)
+ {
+ var leftPart = part.Slice(0, commaIndex).Trim();
+ var rightPart = part.Slice(commaIndex + 1, part.Length - commaIndex - 1).Trim();
+
+ leftValue = ParseValue(leftPart);
+ rightValue = ParseValue(rightPart);
+
+ return 2;
+ }
+
+ leftValue = ParseValue(part);
+
+ return 1;
+ }
+
+ static int ParseCommaDelimitedValues(ReadOnlySpan part, in Span outValues)
+ {
+ int valueIndex = 0;
+
+ while (true)
+ {
+ if (valueIndex >= outValues.Length)
+ {
+ throw new FormatException("Too many provided values.");
+ }
+
+ var commaIndex = part.IndexOf(',');
+
+ if (commaIndex == -1)
+ {
+ if (!part.IsWhiteSpace())
+ {
+ outValues[valueIndex++] = ParseValue(part);
+ }
+
+ break;
+ }
+
+ var valuePart = part.Slice(0, commaIndex).Trim();
+
+ outValues[valueIndex++] = ParseValue(valuePart);
+
+ part = part.Slice(commaIndex + 1, part.Length - commaIndex - 1);
+ }
+
+ return valueIndex;
+ }
+
+ switch (function)
+ {
+ case TransformFunction.Scale:
+ case TransformFunction.ScaleX:
+ case TransformFunction.ScaleY:
+ {
+ var scaleX = UnitValue.One;
+ var scaleY = UnitValue.One;
+
+ int count = ParseValuePair(functionPart, ref scaleX, ref scaleY);
+
+ if (count != 1 && (function == TransformFunction.ScaleX || function == TransformFunction.ScaleY))
+ {
+ ThrowFormatInvalidValueCount(function, 1);
+ }
+
+ VerifyZeroOrUnit(function, in scaleX, Unit.None);
+ VerifyZeroOrUnit(function, in scaleY, Unit.None);
+
+ if (function == TransformFunction.ScaleY)
+ {
+ scaleY = scaleX;
+ scaleX = UnitValue.One;
+ }
+ else if (function == TransformFunction.Scale && count == 1)
+ {
+ scaleY = scaleX;
+ }
+
+ builder.AppendScale(scaleX.Value, scaleY.Value);
+
+ break;
+ }
+ case TransformFunction.Skew:
+ case TransformFunction.SkewX:
+ case TransformFunction.SkewY:
+ {
+ var skewX = UnitValue.Zero;
+ var skewY = UnitValue.Zero;
+
+ int count = ParseValuePair(functionPart, ref skewX, ref skewY);
+
+ if (count != 1 && (function == TransformFunction.SkewX || function == TransformFunction.SkewY))
+ {
+ ThrowFormatInvalidValueCount(function, 1);
+ }
+
+ VerifyZeroOrAngle(function, in skewX);
+ VerifyZeroOrAngle(function, in skewY);
+
+ if (function == TransformFunction.SkewY)
+ {
+ skewY = skewX;
+ skewX = UnitValue.Zero;
+ }
+
+ builder.AppendSkew(ToRadians(in skewX), ToRadians(in skewY));
+
+ break;
+ }
+ case TransformFunction.Rotate:
+ {
+ var angle = UnitValue.Zero;
+ UnitValue _ = default;
+
+ int count = ParseValuePair(functionPart, ref angle, ref _);
+
+ if (count != 1)
+ {
+ ThrowFormatInvalidValueCount(function, 1);
+ }
+
+ VerifyZeroOrAngle(function, in angle);
+
+ builder.AppendRotate(ToRadians(in angle));
+
+ break;
+ }
+ case TransformFunction.Translate:
+ case TransformFunction.TranslateX:
+ case TransformFunction.TranslateY:
+ {
+ var translateX = UnitValue.Zero;
+ var translateY = UnitValue.Zero;
+
+ int count = ParseValuePair(functionPart, ref translateX, ref translateY);
+
+ if (count != 1 && (function == TransformFunction.TranslateX || function == TransformFunction.TranslateY))
+ {
+ ThrowFormatInvalidValueCount(function, 1);
+ }
+
+ VerifyZeroOrUnit(function, in translateX, Unit.Pixel);
+ VerifyZeroOrUnit(function, in translateY, Unit.Pixel);
+
+ if (function == TransformFunction.TranslateY)
+ {
+ translateY = translateX;
+ translateX = UnitValue.Zero;
+ }
+
+ builder.AppendTranslate(translateX.Value, translateY.Value);
+
+ break;
+ }
+ case TransformFunction.Matrix:
+ {
+ Span values = stackalloc UnitValue[6];
+
+ int count = ParseCommaDelimitedValues(functionPart, in values);
+
+ if (count != 6)
+ {
+ ThrowFormatInvalidValueCount(function, 6);
+ }
+
+ foreach (UnitValue value in values)
+ {
+ VerifyZeroOrUnit(function, value, Unit.None);
+ }
+
+ var matrix = new Matrix(
+ values[0].Value,
+ values[1].Value,
+ values[2].Value,
+ values[3].Value,
+ values[4].Value,
+ values[5].Value);
+
+ builder.AppendMatrix(matrix);
+
+ break;
+ }
+ }
+ }
+
+ private static void VerifyZeroOrUnit(TransformFunction function, in UnitValue value, Unit unit)
+ {
+ bool isZero = value.Unit == Unit.None && value.Value == 0d;
+
+ if (!isZero && value.Unit != unit)
+ {
+ ThrowFormatInvalidValue(function, in value);
+ }
+ }
+
+ private static void VerifyZeroOrAngle(TransformFunction function, in UnitValue value)
+ {
+ if (value.Value != 0d && !IsAngleUnit(value.Unit))
+ {
+ ThrowFormatInvalidValue(function, in value);
+ }
+ }
+
+ private static bool IsAngleUnit(Unit unit)
+ {
+ switch (unit)
+ {
+ case Unit.Radian:
+ case Unit.Gradian:
+ case Unit.Degree:
+ case Unit.Turn:
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private static void ThrowFormatInvalidValue(TransformFunction function, in UnitValue value)
+ {
+ var unitString = value.Unit == Unit.None ? string.Empty : value.Unit.ToString();
+
+ throw new FormatException($"Invalid value {value.Value} {unitString} for {function}");
+ }
+
+ private static void ThrowFormatInvalidValueCount(TransformFunction function, int count)
+ {
+ throw new FormatException($"Invalid format. {function} expects {count} value(s).");
+ }
+
+ private static Unit ParseUnit(in ReadOnlySpan part)
+ {
+ foreach (var (name, unit) in s_unitMapping)
+ {
+ if (part.Equals(name.AsSpan(), StringComparison.OrdinalIgnoreCase))
+ {
+ return unit;
+ }
+ }
+
+ throw new FormatException($"Invalid unit: {part.ToString()}");
+ }
+
+ private static TransformFunction ParseTransformFunction(in ReadOnlySpan part)
+ {
+ foreach (var (name, transformFunction) in s_functionMapping)
+ {
+ if (part.Equals(name.AsSpan(), StringComparison.OrdinalIgnoreCase))
+ {
+ return transformFunction;
+ }
+ }
+
+ return TransformFunction.Invalid;
+ }
+
+ private static double ToRadians(in UnitValue value)
+ {
+ return value.Unit switch
+ {
+ Unit.Radian => value.Value,
+ Unit.Gradian => MathUtilities.Grad2Rad(value.Value),
+ Unit.Degree => MathUtilities.Deg2Rad(value.Value),
+ Unit.Turn => MathUtilities.Turn2Rad(value.Value),
+ _ => value.Value
+ };
+ }
+
+ private enum Unit
+ {
+ None,
+ Pixel,
+ Radian,
+ Gradian,
+ Degree,
+ Turn
+ }
+
+ private readonly struct UnitValue
+ {
+ public readonly Unit Unit;
+ public readonly double Value;
+
+ public UnitValue(Unit unit, double value)
+ {
+ Unit = unit;
+ Value = value;
+ }
+
+ public static UnitValue Zero => new UnitValue(Unit.None, 0);
+
+ public static UnitValue One => new UnitValue(Unit.None, 1);
+ }
+
+ private enum TransformFunction
+ {
+ Invalid,
+ Translate,
+ TranslateX,
+ TranslateY,
+ Scale,
+ ScaleX,
+ ScaleY,
+ Skew,
+ SkewX,
+ SkewY,
+ Rotate,
+ Matrix
+ }
+ }
+}
diff --git a/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs b/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs
index 660d10c088..c87946b3ea 100644
--- a/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs
+++ b/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs
@@ -107,6 +107,12 @@ namespace Avalonia.Platform
/// The clip rectangle.
void PushClip(Rect clip);
+ ///
+ /// Pushes a clip rounded rectangle.
+ ///
+ /// The clip rounded rectangle
+ void PushClip(RoundedRect clip);
+
///
/// Pops the latest pushed clip rectangle.
///
diff --git a/src/Avalonia.Visuals/Properties/AssemblyInfo.cs b/src/Avalonia.Visuals/Properties/AssemblyInfo.cs
index 6cd6442095..5d802c27b9 100644
--- a/src/Avalonia.Visuals/Properties/AssemblyInfo.cs
+++ b/src/Avalonia.Visuals/Properties/AssemblyInfo.cs
@@ -6,6 +6,7 @@ using Avalonia.Metadata;
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Animation")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Media")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Media.Imaging")]
+[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Media.Transformation")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia")]
[assembly: InternalsVisibleTo("Avalonia.Direct2D1.RenderTests")]
diff --git a/src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs b/src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs
index 78aef8bc72..9ea1b84311 100644
--- a/src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs
+++ b/src/Avalonia.Visuals/Rendering/ImmediateRenderer.cs
@@ -289,7 +289,11 @@ namespace Avalonia.Rendering
using (context.PushPostTransform(m))
using (context.PushOpacity(opacity))
- using (clipToBounds ? context.PushClip(bounds) : default(DrawingContext.PushedState))
+ using (clipToBounds
+ ? visual is IVisualWithRoundRectClip roundClipVisual
+ ? context.PushClip(new RoundedRect(bounds, roundClipVisual.ClipToBoundsRadius))
+ : context.PushClip(bounds)
+ : default(DrawingContext.PushedState))
using (visual.Clip != null ? context.PushGeometryClip(visual.Clip) : default(DrawingContext.PushedState))
using (visual.OpacityMask != null ? context.PushOpacityMask(visual.OpacityMask, bounds) : default(DrawingContext.PushedState))
using (context.PushTransformContainer())
diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/ClipNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/ClipNode.cs
index 34f042e334..ada04bfefd 100644
--- a/src/Avalonia.Visuals/Rendering/SceneGraph/ClipNode.cs
+++ b/src/Avalonia.Visuals/Rendering/SceneGraph/ClipNode.cs
@@ -16,6 +16,16 @@ namespace Avalonia.Rendering.SceneGraph
{
Clip = clip;
}
+
+ ///
+ /// Initializes a new instance of the class that represents a
+ /// clip push.
+ ///
+ /// The clip to push.
+ public ClipNode(RoundedRect clip)
+ {
+ Clip = clip;
+ }
///
/// Initializes a new instance of the class that represents a
@@ -31,7 +41,7 @@ namespace Avalonia.Rendering.SceneGraph
///
/// Gets the clip to be pushed or null if the operation represents a pop.
///
- public Rect? Clip { get; }
+ public RoundedRect? Clip { get; }
///
public bool HitTest(Point p) => false;
@@ -45,7 +55,7 @@ namespace Avalonia.Rendering.SceneGraph
/// The properties of the other draw operation are passed in as arguments to prevent
/// allocation of a not-yet-constructed draw operation object.
///
- public bool Equals(Rect? clip) => Clip == clip;
+ public bool Equals(RoundedRect? clip) => Clip == clip;
///
public void Render(IDrawingContextImpl context)
diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs
index 6ad71ac111..dfb21a0289 100644
--- a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs
+++ b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs
@@ -163,7 +163,7 @@ namespace Avalonia.Rendering.SceneGraph
++_drawOperationindex;
}
}
-
+
public void Custom(ICustomDrawOperation custom)
{
var next = NextDrawAs();
@@ -283,6 +283,21 @@ namespace Avalonia.Rendering.SceneGraph
}
}
+ ///
+ public void PushClip(RoundedRect clip)
+ {
+ var next = NextDrawAs();
+
+ if (next == null || !next.Item.Equals(clip))
+ {
+ Add(new ClipNode(clip));
+ }
+ else
+ {
+ ++_drawOperationindex;
+ }
+ }
+
///
public void PushGeometryClip(IGeometryImpl clip)
{
@@ -368,7 +383,7 @@ namespace Avalonia.Rendering.SceneGraph
{
using (var refCounted = RefCountable.Create(node))
{
- Add(refCounted);
+ Add(refCounted);
}
}
diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/IVisualNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/IVisualNode.cs
index 8b77c37c1f..6d12b5bca4 100644
--- a/src/Avalonia.Visuals/Rendering/SceneGraph/IVisualNode.cs
+++ b/src/Avalonia.Visuals/Rendering/SceneGraph/IVisualNode.cs
@@ -26,6 +26,11 @@ namespace Avalonia.Rendering.SceneGraph
///
Matrix Transform { get; }
+ ///
+ /// Gets the corner radius of visual. Contents are clipped to this radius.
+ ///
+ CornerRadius ClipToBoundsRadius { get; }
+
///
/// Gets the bounds of the node's geometry in global coordinates.
///
diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs
index 263a491933..5da44c5943 100644
--- a/src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs
+++ b/src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs
@@ -163,6 +163,10 @@ namespace Avalonia.Rendering.SceneGraph
var visual = node.Visual;
var opacity = visual.Opacity;
var clipToBounds = visual.ClipToBounds;
+ var clipToBoundsRadius = visual is IVisualWithRoundRectClip roundRectClip ?
+ roundRectClip.ClipToBoundsRadius :
+ default;
+
var bounds = new Rect(visual.Bounds.Size);
var contextImpl = (DeferredDrawingContextImpl)context.PlatformImpl;
@@ -201,6 +205,7 @@ namespace Avalonia.Rendering.SceneGraph
node.ClipBounds = clipBounds;
node.ClipToBounds = clipToBounds;
node.LayoutBounds = globalBounds;
+ node.ClipToBoundsRadius = clipToBoundsRadius;
node.GeometryClip = visual.Clip?.PlatformImpl;
node.Opacity = opacity;
diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs
index 6f566ff6d6..8fb6b2542a 100644
--- a/src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs
+++ b/src/Avalonia.Visuals/Rendering/SceneGraph/VisualNode.cs
@@ -46,6 +46,9 @@ namespace Avalonia.Rendering.SceneGraph
///
public IVisualNode Parent { get; }
+ ///
+ public CornerRadius ClipToBoundsRadius { get; set; }
+
///
public Matrix Transform { get; set; }
@@ -262,6 +265,7 @@ namespace Avalonia.Rendering.SceneGraph
{
Transform = Transform,
ClipBounds = ClipBounds,
+ ClipToBoundsRadius = ClipToBoundsRadius,
ClipToBounds = ClipToBounds,
LayoutBounds = LayoutBounds,
GeometryClip = GeometryClip,
@@ -301,7 +305,10 @@ namespace Avalonia.Rendering.SceneGraph
if (ClipToBounds)
{
context.Transform = Matrix.Identity;
- context.PushClip(ClipBounds);
+ if (ClipToBoundsRadius.IsEmpty)
+ context.PushClip(ClipBounds);
+ else
+ context.PushClip(new RoundedRect(ClipBounds, ClipToBoundsRadius));
}
context.Transform = Transform;
diff --git a/src/Avalonia.Visuals/RoundedRect.cs b/src/Avalonia.Visuals/RoundedRect.cs
index ad860240f2..3452bc1ff8 100644
--- a/src/Avalonia.Visuals/RoundedRect.cs
+++ b/src/Avalonia.Visuals/RoundedRect.cs
@@ -27,6 +27,10 @@ namespace Avalonia
}
}
+ public static bool operator ==(RoundedRect left, RoundedRect right) => left.Equals(right);
+
+ public static bool operator !=(RoundedRect left, RoundedRect right) => !left.Equals(right);
+
public Rect Rect { get; }
public Vector RadiiTopLeft { get; }
public Vector RadiiTopRight { get; }
@@ -74,6 +78,13 @@ namespace Avalonia
}
+ public RoundedRect(in Rect bounds, in CornerRadius radius) : this(bounds,
+ radius.TopLeft, radius.TopRight,
+ radius.BottomRight, radius.BottomLeft)
+ {
+
+ }
+
public static implicit operator RoundedRect(Rect r) => new RoundedRect(r);
public bool IsRounded => RadiiTopLeft != default || RadiiTopRight != default || RadiiBottomRight != default ||
diff --git a/src/Avalonia.Visuals/Size.cs b/src/Avalonia.Visuals/Size.cs
index 0383094a5b..f87b336b50 100644
--- a/src/Avalonia.Visuals/Size.cs
+++ b/src/Avalonia.Visuals/Size.cs
@@ -189,7 +189,7 @@ namespace Avalonia
}
///
- /// Returns a boolean indicating whether the size is equal to the other given size.
+ /// Returns a boolean indicating whether the size is equal to the other given size (bitwise).
///
/// The other size to test equality against.
/// True if this size is equal to other; False otherwise.
@@ -201,6 +201,17 @@ namespace Avalonia
// ReSharper enable CompareOfFloatsByEqualityOperator
}
+ ///
+ /// Returns a boolean indicating whether the size is equal to the other given size (numerically).
+ ///
+ /// The other size to test equality against.
+ /// True if this size is equal to other; False otherwise.
+ public bool NearlyEquals(Size other)
+ {
+ return MathUtilities.AreClose(_width, other._width) &&
+ MathUtilities.AreClose(_height, other._height);
+ }
+
///
/// Checks for equality between a size and an object.
///
diff --git a/src/Avalonia.Visuals/Vector.cs b/src/Avalonia.Visuals/Vector.cs
index 2b5d79173b..6059dc3971 100644
--- a/src/Avalonia.Visuals/Vector.cs
+++ b/src/Avalonia.Visuals/Vector.cs
@@ -2,7 +2,8 @@ using System;
using System.Globalization;
using Avalonia.Animation.Animators;
using Avalonia.Utilities;
-using JetBrains.Annotations;
+
+#nullable enable
namespace Avalonia
{
@@ -17,20 +18,20 @@ namespace Avalonia
}
///
- /// The X vector.
+ /// The X component.
///
private readonly double _x;
///
- /// The Y vector.
+ /// The Y component.
///
private readonly double _y;
///
/// Initializes a new instance of the structure.
///
- /// The X vector.
- /// The Y vector.
+ /// The X component.
+ /// The Y component.
public Vector(double x, double y)
{
_x = x;
@@ -38,12 +39,12 @@ namespace Avalonia
}
///
- /// Gets the X vector.
+ /// Gets the X component.
///
public double X => _x;
///
- /// Gets the Y vector.
+ /// Gets the Y component.
///
public double Y => _y;
@@ -57,18 +58,18 @@ namespace Avalonia
}
///
- /// Calculates the dot product of two vectors
+ /// Calculates the dot product of two vectors.
///
- /// First vector
- /// Second vector
- /// The dot product
+ /// First vector.
+ /// Second vector.
+ /// The dot product.
public static double operator *(Vector a, Vector b)
=> Dot(a, b);
///
/// Scales a vector.
///
- /// The vector
+ /// The vector.
/// The scaling factor.
/// The scaled vector.
public static Vector operator *(Vector vector, double scale)
@@ -77,7 +78,7 @@ namespace Avalonia
///
/// Scales a vector.
///
- /// The vector
+ /// The vector.
/// The divisor.
/// The scaled vector.
public static Vector operator /(Vector vector, double scale)
@@ -100,12 +101,12 @@ namespace Avalonia
}
///
- /// Length of the vector
+ /// Length of the vector.
///
public double Length => Math.Sqrt(SquaredLength);
///
- /// Squared Length of the vector
+ /// Squared Length of the vector.
///
public double SquaredLength => _x * _x + _y * _y;
@@ -154,9 +155,8 @@ namespace Avalonia
/// True if vectors are nearly equal.
public bool NearlyEquals(Vector other)
{
- const float tolerance = float.Epsilon;
-
- return Math.Abs(_x - other._x) < tolerance && Math.Abs(_y - other._y) < tolerance;
+ return MathUtilities.AreClose(_x, other._x) &&
+ MathUtilities.AreClose(_y, other._y);
}
public override bool Equals(object obj) => obj is Vector other && Equals(other);
@@ -189,9 +189,9 @@ namespace Avalonia
}
///
- /// Returns a new vector with the specified X coordinate.
+ /// Returns a new vector with the specified X component.
///
- /// The X coordinate.
+ /// The X component.
/// The new vector.
public Vector WithX(double x)
{
@@ -199,9 +199,9 @@ namespace Avalonia
}
///
- /// Returns a new vector with the specified Y coordinate.
+ /// Returns a new vector with the specified Y component.
///
- /// The Y coordinate.
+ /// The Y component.
/// The new vector.
public Vector WithY(double y)
{
@@ -311,25 +311,25 @@ namespace Avalonia
=> new Vector(-vector._x, -vector._y);
///
- /// Returnes the vector (0.0, 0.0)
+ /// Returns the vector (0.0, 0.0).
///
public static Vector Zero
=> new Vector(0, 0);
///
- /// Returnes the vector (1.0, 1.0)
+ /// Returns the vector (1.0, 1.0).
///
public static Vector One
=> new Vector(1, 1);
///
- /// Returnes the vector (1.0, 0.0)
+ /// Returns the vector (1.0, 0.0).
///
public static Vector UnitX
=> new Vector(1, 0);
///
- /// Returnes the vector (0.0, 1.0)
+ /// Returns the vector (0.0, 1.0).
///
public static Vector UnitY
=> new Vector(0, 1);
diff --git a/src/Avalonia.Visuals/Visual.cs b/src/Avalonia.Visuals/Visual.cs
index a22e8ac829..cd6eb6aac7 100644
--- a/src/Avalonia.Visuals/Visual.cs
+++ b/src/Avalonia.Visuals/Visual.cs
@@ -68,8 +68,8 @@ namespace Avalonia
///
/// Defines the property.
///
- public static readonly StyledProperty RenderTransformProperty =
- AvaloniaProperty.Register(nameof(RenderTransform));
+ public static readonly StyledProperty RenderTransformProperty =
+ AvaloniaProperty.Register(nameof(RenderTransform));
///
/// Defines the property.
@@ -219,7 +219,7 @@ namespace Avalonia
///
/// Gets the render transform of the control.
///
- public Transform RenderTransform
+ public ITransform RenderTransform
{
get { return GetValue(RenderTransformProperty); }
set { SetValue(RenderTransformProperty, value); }
@@ -391,9 +391,9 @@ namespace Avalonia
_visualRoot = e.Root;
- if (RenderTransform != null)
+ if (RenderTransform is IMutableTransform mutableTransform)
{
- RenderTransform.Changed += RenderTransformChanged;
+ mutableTransform.Changed += RenderTransformChanged;
}
EnableTransitions();
@@ -428,9 +428,9 @@ namespace Avalonia
_visualRoot = null;
- if (RenderTransform != null)
+ if (RenderTransform is IMutableTransform mutableTransform)
{
- RenderTransform.Changed -= RenderTransformChanged;
+ mutableTransform.Changed -= RenderTransformChanged;
}
DisableTransitions();
diff --git a/src/Avalonia.Visuals/VisualTree/IVisual.cs b/src/Avalonia.Visuals/VisualTree/IVisual.cs
index 6f905cc269..50787655d9 100644
--- a/src/Avalonia.Visuals/VisualTree/IVisual.cs
+++ b/src/Avalonia.Visuals/VisualTree/IVisual.cs
@@ -76,7 +76,7 @@ namespace Avalonia.VisualTree
///
/// Gets or sets the render transform of the control.
///
- Transform RenderTransform { get; set; }
+ ITransform RenderTransform { get; set; }
///
/// Gets or sets the render transform origin of the control.
diff --git a/src/Avalonia.Visuals/VisualTree/IVisualWithRoundRectClip.cs b/src/Avalonia.Visuals/VisualTree/IVisualWithRoundRectClip.cs
new file mode 100644
index 0000000000..9ace215d03
--- /dev/null
+++ b/src/Avalonia.Visuals/VisualTree/IVisualWithRoundRectClip.cs
@@ -0,0 +1,15 @@
+using System;
+
+namespace Avalonia.VisualTree
+{
+ [Obsolete("Internal API, will be removed in future versions, you've been warned")]
+ public interface IVisualWithRoundRectClip
+ {
+ ///
+ /// Gets a value indicating the corner radius of control's clip bounds
+ ///
+ [Obsolete("Internal API, will be removed in future versions, you've been warned")]
+ CornerRadius ClipToBoundsRadius { get; }
+
+ }
+}
diff --git a/src/Avalonia.X11/X11Window.cs b/src/Avalonia.X11/X11Window.cs
index 1d41fe4bdd..90064cb28d 100644
--- a/src/Avalonia.X11/X11Window.cs
+++ b/src/Avalonia.X11/X11Window.cs
@@ -922,7 +922,7 @@ namespace Avalonia.X11
public IScreenImpl Screen => _platform.Screens;
- public Size MaxClientSize => _platform.X11Screens.Screens.Select(s => s.Bounds.Size.ToSize(s.PixelDensity))
+ public Size MaxAutoSizeHint => _platform.X11Screens.Screens.Select(s => s.Bounds.Size.ToSize(s.PixelDensity))
.OrderByDescending(x => x.Width + x.Height).FirstOrDefault();
diff --git a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs
index 26fdb08a4b..ae756f4eab 100644
--- a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs
+++ b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs
@@ -389,6 +389,12 @@ namespace Avalonia.Skia
Canvas.ClipRect(clip.ToSKRect());
}
+ public void PushClip(RoundedRect clip)
+ {
+ Canvas.Save();
+ Canvas.ClipRoundRect(clip.ToSKRoundRect());
+ }
+
///
public void PopClip()
{
diff --git a/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs b/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs
index 6375f74c59..ec7e0a67ed 100644
--- a/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs
+++ b/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs
@@ -40,6 +40,21 @@ namespace Avalonia.Skia
return new SKRect((float)r.X, (float)r.Y, (float)r.Right, (float)r.Bottom);
}
+ public static SKRoundRect ToSKRoundRect(this RoundedRect r)
+ {
+ var rc = r.Rect.ToSKRect();
+ var result = new SKRoundRect();
+
+ result.SetRectRadii(rc,
+ new[]
+ {
+ r.RadiiTopLeft.ToSKPoint(), r.RadiiTopRight.ToSKPoint(),
+ r.RadiiBottomRight.ToSKPoint(), r.RadiiBottomLeft.ToSKPoint(),
+ });
+
+ return result;
+ }
+
public static Rect ToAvaloniaRect(this SKRect r)
{
return new Rect(r.Left, r.Top, r.Right - r.Left, r.Bottom - r.Top);
diff --git a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs
index 9b7ba4844a..e0de40525f 100644
--- a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs
+++ b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs
@@ -359,6 +359,12 @@ namespace Avalonia.Direct2D1.Media
_deviceContext.PushAxisAlignedClip(clip.ToSharpDX(), AntialiasMode.PerPrimitive);
}
+ public void PushClip(RoundedRect clip)
+ {
+ //TODO: radius
+ _deviceContext.PushAxisAlignedClip(clip.Rect.ToDirect2D(), AntialiasMode.PerPrimitive);
+ }
+
public void PopClip()
{
_deviceContext.PopAxisAlignedClip();
diff --git a/src/Windows/Avalonia.Win32/PopupImpl.cs b/src/Windows/Avalonia.Win32/PopupImpl.cs
index efcf1ea674..7f27a9e841 100644
--- a/src/Windows/Avalonia.Win32/PopupImpl.cs
+++ b/src/Windows/Avalonia.Win32/PopupImpl.cs
@@ -8,12 +8,35 @@ namespace Avalonia.Win32
class PopupImpl : WindowImpl, IPopupImpl
{
private bool _dropShadowHint = true;
+ private Size? _maxAutoSize;
public override void Show()
{
UnmanagedMethods.ShowWindow(Handle.Handle, UnmanagedMethods.ShowWindowCommand.ShowNoActivate);
}
+ public override Size MaxAutoSizeHint
+ {
+ get
+ {
+ if (_maxAutoSize is null)
+ {
+ var monitor = UnmanagedMethods.MonitorFromWindow(
+ Hwnd,
+ UnmanagedMethods.MONITOR.MONITOR_DEFAULTTONEAREST);
+
+ if (monitor != IntPtr.Zero)
+ {
+ var info = UnmanagedMethods.MONITORINFO.Create();
+ UnmanagedMethods.GetMonitorInfo(monitor, ref info);
+ _maxAutoSize = info.rcWork.ToPixelRect().ToRect(Scaling).Size;
+ }
+ }
+
+ return _maxAutoSize ?? Size.Infinity;
+ }
+ }
+
protected override IntPtr CreateWindowOverride(ushort atom)
{
UnmanagedMethods.WindowStyles style =
@@ -47,6 +70,9 @@ namespace Avalonia.Win32
{
switch ((UnmanagedMethods.WindowsMessage)msg)
{
+ case UnmanagedMethods.WindowsMessage.WM_DISPLAYCHANGE:
+ _maxAutoSize = null;
+ goto default;
case UnmanagedMethods.WindowsMessage.WM_MOUSEACTIVATE:
return (IntPtr)UnmanagedMethods.MouseActivate.MA_NOACTIVATE;
default:
diff --git a/src/Windows/Avalonia.Win32/WindowImpl.WndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.WndProc.cs
index 138553b962..50e71aeebe 100644
--- a/src/Windows/Avalonia.Win32/WindowImpl.WndProc.cs
+++ b/src/Windows/Avalonia.Win32/WindowImpl.WndProc.cs
@@ -393,6 +393,8 @@ namespace Avalonia.Win32
case WindowsMessage.WM_GETMINMAXINFO:
{
MINMAXINFO mmi = Marshal.PtrToStructure(lParam);
+
+ _maxTrackSize = mmi.ptMaxTrackSize;
if (_minSize.Width > 0)
{
diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs
index 81340fcde7..6024dc01da 100644
--- a/src/Windows/Avalonia.Win32/WindowImpl.cs
+++ b/src/Windows/Avalonia.Win32/WindowImpl.cs
@@ -66,6 +66,7 @@ namespace Avalonia.Win32
private OleDropTarget _dropTarget;
private Size _minSize;
private Size _maxSize;
+ private POINT _maxTrackSize;
private WindowImpl _parent;
public WindowImpl()
@@ -168,16 +169,7 @@ namespace Avalonia.Win32
public IPlatformHandle Handle { get; private set; }
- public Size MaxClientSize
- {
- get
- {
- return (new Size(
- GetSystemMetrics(SystemMetric.SM_CXMAXTRACK),
- GetSystemMetrics(SystemMetric.SM_CYMAXTRACK))
- - BorderThickness) / Scaling;
- }
- }
+ public virtual Size MaxAutoSizeHint => new Size(_maxTrackSize.X / Scaling, _maxTrackSize.Y / Scaling);
public IMouseDevice MouseDevice => _mouseDevice;
@@ -211,6 +203,8 @@ namespace Avalonia.Win32
public WindowTransparencyLevel TransparencyLevel { get; private set; }
+ protected IntPtr Hwnd => _hwnd;
+
public void SetTransparencyLevelHint (WindowTransparencyLevel transparencyLevel)
{
TransparencyLevel = EnableBlur(transparencyLevel);
diff --git a/tests/Avalonia.Animation.UnitTests/TestClock.cs b/tests/Avalonia.Animation.UnitTests/TestClock.cs
index a1c4ff9277..4812880c03 100644
--- a/tests/Avalonia.Animation.UnitTests/TestClock.cs
+++ b/tests/Avalonia.Animation.UnitTests/TestClock.cs
@@ -5,10 +5,12 @@ namespace Avalonia.Animation.UnitTests
{
internal class TestClock : IClock, IDisposable
{
+ private TimeSpan _curTime;
+
private IObserver _observer;
public PlayState PlayState { get; set; } = PlayState.Run;
-
+
public void Dispose()
{
_observer?.OnCompleted();
@@ -19,6 +21,12 @@ namespace Avalonia.Animation.UnitTests
_observer?.OnNext(time);
}
+ public void Pulse(TimeSpan time)
+ {
+ _curTime += time;
+ _observer?.OnNext(_curTime);
+ }
+
public IDisposable Subscribe(IObserver observer)
{
_observer = observer;
diff --git a/tests/Avalonia.Animation.UnitTests/TransitionsTests.cs b/tests/Avalonia.Animation.UnitTests/TransitionsTests.cs
index 70ffd781a1..640013dedd 100644
--- a/tests/Avalonia.Animation.UnitTests/TransitionsTests.cs
+++ b/tests/Avalonia.Animation.UnitTests/TransitionsTests.cs
@@ -10,13 +10,11 @@ namespace Avalonia.Animation.UnitTests
[Fact]
public void Check_Transitions_Interpolation_Negative_Bounds_Clamp()
{
- var clock = new MockGlobalClock();
+ var clock = new TestClock();
- using (UnitTestApplication.Start(new TestServices(globalClock: clock)))
+ var border = new Border
{
- var border = new Border
- {
- Transitions = new Transitions
+ Transitions = new Transitions
{
new DoubleTransition
{
@@ -24,27 +22,25 @@ namespace Avalonia.Animation.UnitTests
Property = Border.OpacityProperty,
}
}
- };
+ };
- border.Opacity = 0;
+ border.Opacity = 0;
- clock.Pulse(TimeSpan.FromSeconds(0));
- clock.Pulse(TimeSpan.FromSeconds(-0.5));
+ clock.Pulse(TimeSpan.FromSeconds(0));
+ clock.Pulse(TimeSpan.FromSeconds(-0.5));
+
+ Assert.Equal(0, border.Opacity);
- Assert.Equal(0, border.Opacity);
- }
}
[Fact]
public void Check_Transitions_Interpolation_Positive_Bounds_Clamp()
{
- var clock = new MockGlobalClock();
+ var clock = new TestClock();
- using (UnitTestApplication.Start(new TestServices(globalClock: clock)))
+ var border = new Border
{
- var border = new Border
- {
- Transitions = new Transitions
+ Transitions = new Transitions
{
new DoubleTransition
{
@@ -52,34 +48,62 @@ namespace Avalonia.Animation.UnitTests
Property = Border.OpacityProperty,
}
}
- };
+ };
- border.Opacity = 0;
+ border.Opacity = 0;
- clock.Pulse(TimeSpan.FromSeconds(0));
- clock.Pulse(TimeSpan.FromMilliseconds(1001));
+ clock.Pulse(TimeSpan.FromSeconds(0));
+ clock.Pulse(TimeSpan.FromMilliseconds(1001));
+
+ Assert.Equal(0, border.Opacity);
- Assert.Equal(0, border.Opacity);
- }
}
[Fact]
public void TransitionInstance_With_Zero_Duration_Is_Completed_On_First_Tick()
{
- var clock = new MockGlobalClock();
+ var clock = new TestClock();
- using (UnitTestApplication.Start(new TestServices(globalClock: clock)))
+ int i = 0;
+ var inst = new TransitionInstance(clock, TimeSpan.Zero, TimeSpan.Zero).Subscribe(nextValue =>
{
- int i = 0;
- var inst = new TransitionInstance(clock, TimeSpan.Zero).Subscribe(nextValue =>
+ switch (i++)
{
- switch (i++)
- {
- case 0: Assert.Equal(0, nextValue); break;
- case 1: Assert.Equal(1d, nextValue); break;
- }
- });
+ case 0: Assert.Equal(0, nextValue); break;
+ case 1: Assert.Equal(1d, nextValue); break;
+ }
+ });
+
+ clock.Pulse(TimeSpan.FromMilliseconds(10));
+ }
+
+ [Fact]
+ public void TransitionInstance_Properly_Calculates_Delay_And_Duration_Values()
+ {
+ var clock = new TestClock();
+
+ int i = -1;
+ var inst = new TransitionInstance(clock, TimeSpan.FromMilliseconds(30), TimeSpan.FromMilliseconds(70)).Subscribe(nextValue =>
+ {
+ switch (i++)
+ {
+ case 0: Assert.Equal(0, nextValue); break;
+ case 1: Assert.Equal(0, nextValue); break;
+ case 2: Assert.Equal(0, nextValue); break;
+ case 3: Assert.Equal(0, nextValue); break;
+ case 4: Assert.Equal(Math.Round(10d / 70d, 4), Math.Round(nextValue, 4)); break;
+ case 5: Assert.Equal(Math.Round(20d / 70d, 4), Math.Round(nextValue, 4)); break;
+ case 6: Assert.Equal(Math.Round(30d / 70d, 4), Math.Round(nextValue, 4)); break;
+ case 7: Assert.Equal(Math.Round(40d / 70d, 4), Math.Round(nextValue, 4)); break;
+ case 8: Assert.Equal(Math.Round(50d / 70d, 4), Math.Round(nextValue, 4)); break;
+ case 9: Assert.Equal(Math.Round(60d / 70d, 4), Math.Round(nextValue, 4)); break;
+ case 10: Assert.Equal(1d, nextValue); break;
+ }
+ });
+
+ for (int z = 0; z <= 10; z++)
+ {
clock.Pulse(TimeSpan.FromMilliseconds(10));
}
}
diff --git a/tests/Avalonia.Benchmarks/Visuals/MatrixBenchmarks.cs b/tests/Avalonia.Benchmarks/Visuals/MatrixBenchmarks.cs
new file mode 100644
index 0000000000..17e2237eb0
--- /dev/null
+++ b/tests/Avalonia.Benchmarks/Visuals/MatrixBenchmarks.cs
@@ -0,0 +1,16 @@
+using BenchmarkDotNet.Attributes;
+
+namespace Avalonia.Benchmarks.Visuals
+{
+ [MemoryDiagnoser, InProcess]
+ public class MatrixBenchmarks
+ {
+ private static readonly Matrix s_data = Matrix.Identity;
+
+ [Benchmark(Baseline = true)]
+ public bool Decompose()
+ {
+ return Matrix.TryDecomposeTransform(s_data, out Matrix.Decomposed decomposed);
+ }
+ }
+}
diff --git a/tests/Avalonia.Controls.UnitTests/GridTests.cs b/tests/Avalonia.Controls.UnitTests/GridTests.cs
index 353bb9c98d..b3882c534b 100644
--- a/tests/Avalonia.Controls.UnitTests/GridTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/GridTests.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using Avalonia.UnitTests;
using Xunit;
using Xunit.Abstractions;
@@ -1182,13 +1183,18 @@ namespace Avalonia.Controls.UnitTests
foreach (var xgrids in grids)
scope.Children.Add(xgrids);
- var root = new Grid();
- root.UseLayoutRounding = false;
- root.SetValue(Grid.IsSharedSizeScopeProperty, true);
- root.Children.Add(scope);
+ var rootGrid = new Grid();
+ rootGrid.UseLayoutRounding = false;
+ rootGrid.SetValue(Grid.IsSharedSizeScopeProperty, true);
+ rootGrid.Children.Add(scope);
- root.Measure(new Size(50, 50));
- root.Arrange(new Rect(new Point(), new Point(50, 50)));
+ var root = new TestRoot(rootGrid)
+ {
+ Width = 50,
+ Height = 50,
+ };
+
+ root.LayoutManager.ExecuteInitialLayoutPass(root);
PrintColumnDefinitions(grids[0]);
Assert.Equal(5, grids[0].ColumnDefinitions[0].ActualWidth);
diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/PopupRootTests.cs b/tests/Avalonia.Controls.UnitTests/Primitives/PopupRootTests.cs
index 08b9c75dbc..f27ff3928c 100644
--- a/tests/Avalonia.Controls.UnitTests/Primitives/PopupRootTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/Primitives/PopupRootTests.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.Linq;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
@@ -181,18 +182,21 @@ namespace Avalonia.Controls.UnitTests.Primitives
}
[Fact]
- public void Child_Should_Be_Measured_With_Infinity()
+ public void Child_Should_Be_Measured_With_MaxAutoSizeHint()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var child = new ChildControl();
var window = new Window();
- var target = CreateTarget(window);
+ var popupImpl = MockWindowingPlatform.CreatePopupMock(window.PlatformImpl);
+ popupImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1200, 1000));
+ var target = CreateTarget(window, popupImpl.Object);
target.Content = child;
target.Show();
- Assert.Equal(Size.Infinity, child.MeasureSize);
+ Assert.Equal(1, child.MeasureSizes.Count);
+ Assert.Equal(new Size(1200, 1000), child.MeasureSizes[0]);
}
}
@@ -210,7 +214,8 @@ namespace Avalonia.Controls.UnitTests.Primitives
target.Content = child;
target.Show();
- Assert.Equal(new Size(500, 600), child.MeasureSize);
+ Assert.Equal(1, child.MeasureSizes.Count);
+ Assert.Equal(new Size(500, 600), child.MeasureSizes[0]);
}
}
@@ -228,7 +233,8 @@ namespace Avalonia.Controls.UnitTests.Primitives
target.Content = child;
target.Show();
- Assert.Equal(new Size(500, 600), child.MeasureSize);
+ Assert.Equal(1, child.MeasureSizes.Count);
+ Assert.Equal(new Size(500, 600), child.MeasureSizes[0]);
}
}
@@ -365,11 +371,11 @@ namespace Avalonia.Controls.UnitTests.Primitives
private class ChildControl : Control
{
- public Size MeasureSize { get; private set; }
+ public List MeasureSizes { get; } = new List();
protected override Size MeasureOverride(Size availableSize)
{
- MeasureSize = availableSize;
+ MeasureSizes.Add(availableSize);
return base.MeasureOverride(availableSize);
}
}
diff --git a/tests/Avalonia.Controls.UnitTests/ScrollViewerTests.cs b/tests/Avalonia.Controls.UnitTests/ScrollViewerTests.cs
index 8da1e26f0d..deca3cfb75 100644
--- a/tests/Avalonia.Controls.UnitTests/ScrollViewerTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/ScrollViewerTests.cs
@@ -4,6 +4,7 @@ using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Layout;
+using Avalonia.UnitTests;
using Moq;
using Xunit;
@@ -150,12 +151,15 @@ namespace Avalonia.Controls.UnitTests
public void Changing_Extent_Should_Raise_ScrollChanged()
{
var target = new ScrollViewer();
+ var root = new TestRoot(target);
var raised = 0;
target.SetValue(ScrollViewer.ExtentProperty, new Size(100, 100));
target.SetValue(ScrollViewer.ViewportProperty, new Size(50, 50));
target.Offset = new Vector(10, 10);
+ root.LayoutManager.ExecuteInitialLayoutPass(root);
+
target.ScrollChanged += (s, e) =>
{
Assert.Equal(new Vector(11, 12), e.ExtentDelta);
@@ -166,20 +170,26 @@ namespace Avalonia.Controls.UnitTests
target.SetValue(ScrollViewer.ExtentProperty, new Size(111, 112));
- Assert.Equal(1, raised);
+ Assert.Equal(0, raised);
+
+ root.LayoutManager.ExecuteLayoutPass();
+ Assert.Equal(1, raised);
}
[Fact]
public void Changing_Offset_Should_Raise_ScrollChanged()
{
var target = new ScrollViewer();
+ var root = new TestRoot(target);
var raised = 0;
target.SetValue(ScrollViewer.ExtentProperty, new Size(100, 100));
target.SetValue(ScrollViewer.ViewportProperty, new Size(50, 50));
target.Offset = new Vector(10, 10);
+ root.LayoutManager.ExecuteInitialLayoutPass(root);
+
target.ScrollChanged += (s, e) =>
{
Assert.Equal(default, e.ExtentDelta);
@@ -190,20 +200,26 @@ namespace Avalonia.Controls.UnitTests
target.Offset = new Vector(22, 24);
- Assert.Equal(1, raised);
+ Assert.Equal(0, raised);
+ root.LayoutManager.ExecuteLayoutPass();
+
+ Assert.Equal(1, raised);
}
[Fact]
public void Changing_Viewport_Should_Raise_ScrollChanged()
{
var target = new ScrollViewer();
+ var root = new TestRoot(target);
var raised = 0;
target.SetValue(ScrollViewer.ExtentProperty, new Size(100, 100));
target.SetValue(ScrollViewer.ViewportProperty, new Size(50, 50));
target.Offset = new Vector(10, 10);
+ root.LayoutManager.ExecuteInitialLayoutPass(root);
+
target.ScrollChanged += (s, e) =>
{
Assert.Equal(default, e.ExtentDelta);
@@ -214,8 +230,11 @@ namespace Avalonia.Controls.UnitTests
target.SetValue(ScrollViewer.ViewportProperty, new Size(56, 58));
- Assert.Equal(1, raised);
+ Assert.Equal(0, raised);
+
+ root.LayoutManager.ExecuteLayoutPass();
+ Assert.Equal(1, raised);
}
private Control CreateTemplate(ScrollViewer control, INameScope scope)
diff --git a/tests/Avalonia.Controls.UnitTests/WindowTests.cs b/tests/Avalonia.Controls.UnitTests/WindowTests.cs
index 80c8a34ffd..e2b0def00b 100644
--- a/tests/Avalonia.Controls.UnitTests/WindowTests.cs
+++ b/tests/Avalonia.Controls.UnitTests/WindowTests.cs
@@ -297,12 +297,12 @@ namespace Avalonia.Controls.UnitTests
{
var parentWindowImpl = MockWindowingPlatform.CreateWindowMock();
parentWindowImpl.Setup(x => x.ClientSize).Returns(new Size(800, 480));
- parentWindowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
+ parentWindowImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1920, 1080));
parentWindowImpl.Setup(x => x.Scaling).Returns(1);
var windowImpl = MockWindowingPlatform.CreateWindowMock();
windowImpl.Setup(x => x.ClientSize).Returns(new Size(320, 200));
- windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1920, 1080));
+ windowImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1920, 1080));
windowImpl.Setup(x => x.Scaling).Returns(1);
var parentWindowServices = TestServices.StyledWindow.With(
@@ -381,12 +381,15 @@ namespace Avalonia.Controls.UnitTests
}
[Fact]
- public void Child_Should_Be_Measured_With_Infinity_If_SizeToContent_Is_WidthAndHeight()
+ public void Child_Should_Be_Measured_With_MaxAutoSizeHint_If_SizeToContent_Is_WidthAndHeight()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
+ var windowImpl = MockWindowingPlatform.CreateWindowMock();
+ windowImpl.Setup(x => x.MaxAutoSizeHint).Returns(new Size(1200, 1000));
+
var child = new ChildControl();
- var target = new Window
+ var target = new Window(windowImpl.Object)
{
Width = 100,
Height = 50,
@@ -394,10 +397,10 @@ namespace Avalonia.Controls.UnitTests
Content = child
};
- Show(target);
+ target.Show();
Assert.Equal(1, child.MeasureSizes.Count);
- Assert.Equal(Size.Infinity, child.MeasureSizes[0]);
+ Assert.Equal(new Size(1200, 1000), child.MeasureSizes[0]);
}
}
diff --git a/tests/Avalonia.Layout.UnitTests/LayoutableTests.cs b/tests/Avalonia.Layout.UnitTests/LayoutableTests.cs
index a1c1e62f58..a21c8d589d 100644
--- a/tests/Avalonia.Layout.UnitTests/LayoutableTests.cs
+++ b/tests/Avalonia.Layout.UnitTests/LayoutableTests.cs
@@ -203,6 +203,125 @@ namespace Avalonia.Layout.UnitTests
Assert.Equal(new Rect(expectedX, 0, childWidth, 100), target.Bounds);
}
+ [Fact]
+ public void LayoutUpdated_Is_Called_At_End_Of_Layout_Pass()
+ {
+ Border border1;
+ Border border2;
+ var layoutManager = new LayoutManager();
+ var root = new TestRoot
+ {
+ Child = border1 = new Border
+ {
+ Child = border2 = new Border(),
+ },
+ LayoutManager = layoutManager,
+ };
+ var raised = 0;
+
+ void ValidateBounds(object sender, EventArgs e)
+ {
+ Assert.Equal(new Rect(0, 0, 100, 100), border1.Bounds);
+ Assert.Equal(new Rect(0, 0, 100, 100), border2.Bounds);
+ ++raised;
+ }
+
+ root.LayoutUpdated += ValidateBounds;
+ border1.LayoutUpdated += ValidateBounds;
+ border2.LayoutUpdated += ValidateBounds;
+
+ root.Measure(new Size(100, 100));
+ root.Arrange(new Rect(0, 0, 100, 100));
+
+ layoutManager.ExecuteLayoutPass();
+
+ Assert.Equal(3, raised);
+ Assert.Equal(new Rect(0, 0, 100, 100), border1.Bounds);
+ Assert.Equal(new Rect(0, 0, 100, 100), border2.Bounds);
+ }
+
+ [Fact]
+ public void LayoutUpdated_Subscribes_To_LayoutManager()
+ {
+ Border target;
+ var layoutManager = new Mock();
+ layoutManager.SetupAdd(m => m.LayoutUpdated += (sender, args) => { });
+
+ var root = new TestRoot
+ {
+ Child = new Border
+ {
+ Child = target = new Border(),
+ },
+ LayoutManager = layoutManager.Object,
+ };
+
+ void Handler(object sender, EventArgs e) {}
+
+ layoutManager.Invocations.Clear();
+ target.LayoutUpdated += Handler;
+
+ layoutManager.VerifyAdd(
+ x => x.LayoutUpdated += It.IsAny(),
+ Times.Once);
+
+ layoutManager.Invocations.Clear();
+ target.LayoutUpdated -= Handler;
+
+ layoutManager.VerifyRemove(
+ x => x.LayoutUpdated -= It.IsAny(),
+ Times.Once);
+ }
+
+ [Fact]
+ public void LayoutManager_LayoutUpdated_Is_Subscribed_When_Attached_To_Tree()
+ {
+ Border border1;
+ var layoutManager = new Mock();
+ layoutManager.SetupAdd(m => m.LayoutUpdated += (sender, args) => { });
+
+ var root = new TestRoot
+ {
+ Child = border1 = new Border(),
+ LayoutManager = layoutManager.Object,
+ };
+
+ var border2 = new Border();
+ border2.LayoutUpdated += (s, e) => { };
+
+ layoutManager.Invocations.Clear();
+ border1.Child = border2;
+
+ layoutManager.VerifyAdd(
+ x => x.LayoutUpdated += It.IsAny(),
+ Times.Once);
+ }
+
+ [Fact]
+ public void LayoutManager_LayoutUpdated_Is_Unsubscribed_When_Detached_From_Tree()
+ {
+ Border border1;
+ var layoutManager = new Mock();
+ layoutManager.SetupAdd(m => m.LayoutUpdated += (sender, args) => { });
+
+ var root = new TestRoot
+ {
+ Child = border1 = new Border(),
+ LayoutManager = layoutManager.Object,
+ };
+
+ var border2 = new Border();
+ border2.LayoutUpdated += (s, e) => { };
+ border1.Child = border2;
+
+ layoutManager.Invocations.Clear();
+ border1.Child = null;
+
+ layoutManager.VerifyRemove(
+ x => x.LayoutUpdated -= It.IsAny(),
+ Times.Once);
+ }
+
private class TestLayoutable : Layoutable
{
public Size ArrangeSize { get; private set; }
diff --git a/tests/Avalonia.UnitTests/MockWindowingPlatform.cs b/tests/Avalonia.UnitTests/MockWindowingPlatform.cs
index b3e4b4edbc..ee45433089 100644
--- a/tests/Avalonia.UnitTests/MockWindowingPlatform.cs
+++ b/tests/Avalonia.UnitTests/MockWindowingPlatform.cs
@@ -28,6 +28,7 @@ namespace Avalonia.UnitTests
windowImpl.SetupAllProperties();
windowImpl.Setup(x => x.ClientSize).Returns(() => clientSize);
+ windowImpl.Setup(x => x.MaxAutoSizeHint).Returns(s_screenSize);
windowImpl.Setup(x => x.Scaling).Returns(1);
windowImpl.Setup(x => x.Screen).Returns(CreateScreenMock().Object);
windowImpl.Setup(x => x.Position).Returns(() => position);
@@ -79,6 +80,7 @@ namespace Avalonia.UnitTests
popupImpl.SetupAllProperties();
popupImpl.Setup(x => x.ClientSize).Returns(() => clientSize);
+ popupImpl.Setup(x => x.MaxAutoSizeHint).Returns(s_screenSize);
popupImpl.Setup(x => x.Scaling).Returns(1);
popupImpl.Setup(x => x.PopupPositioner).Returns(positioner);
diff --git a/tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs
index e17fd47ff8..f3f3c9a4ca 100644
--- a/tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs
+++ b/tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs
@@ -17,6 +17,41 @@ namespace Avalonia.Visuals.UnitTests.Media
Assert.Equal(0xff, result.A);
}
+ [Fact]
+ public void Try_Parse_Parses_RGB_Hash_Color()
+ {
+ var success = Color.TryParse("#ff8844", out Color result);
+
+ Assert.True(success);
+ Assert.Equal(0xff, result.R);
+ Assert.Equal(0x88, result.G);
+ Assert.Equal(0x44, result.B);
+ Assert.Equal(0xff, result.A);
+ }
+
+ [Fact]
+ public void Parse_Parses_RGB_Hash_Shorthand_Color()
+ {
+ var result = Color.Parse("#f84");
+
+ Assert.Equal(0xff, result.R);
+ Assert.Equal(0x88, result.G);
+ Assert.Equal(0x44, result.B);
+ Assert.Equal(0xff, result.A);
+ }
+
+ [Fact]
+ public void Try_Parse_Parses_RGB_Hash_Shorthand_Color()
+ {
+ var success = Color.TryParse("#f84", out Color result);
+
+ Assert.True(success);
+ Assert.Equal(0xff, result.R);
+ Assert.Equal(0x88, result.G);
+ Assert.Equal(0x44, result.B);
+ Assert.Equal(0xff, result.A);
+ }
+
[Fact]
public void Parse_Parses_ARGB_Hash_Color()
{
@@ -28,6 +63,41 @@ namespace Avalonia.Visuals.UnitTests.Media
Assert.Equal(0x40, result.A);
}
+ [Fact]
+ public void Try_Parse_Parses_ARGB_Hash_Color()
+ {
+ var success = Color.TryParse("#40ff8844", out Color result);
+
+ Assert.True(success);
+ Assert.Equal(0xff, result.R);
+ Assert.Equal(0x88, result.G);
+ Assert.Equal(0x44, result.B);
+ Assert.Equal(0x40, result.A);
+ }
+
+ [Fact]
+ public void Parse_Parses_ARGB_Hash_Shorthand_Color()
+ {
+ var result = Color.Parse("#4f84");
+
+ Assert.Equal(0xff, result.R);
+ Assert.Equal(0x88, result.G);
+ Assert.Equal(0x44, result.B);
+ Assert.Equal(0x44, result.A);
+ }
+
+ [Fact]
+ public void Try_Parse_Parses_ARGB_Hash_Shorthand_Color()
+ {
+ var success = Color.TryParse("#4f84", out Color result);
+
+ Assert.True(success);
+ Assert.Equal(0xff, result.R);
+ Assert.Equal(0x88, result.G);
+ Assert.Equal(0x44, result.B);
+ Assert.Equal(0x44, result.A);
+ }
+
[Fact]
public void Parse_Parses_Named_Color_Lowercase()
{
@@ -39,6 +109,18 @@ namespace Avalonia.Visuals.UnitTests.Media
Assert.Equal(0xff, result.A);
}
+ [Fact]
+ public void TryParse_Parses_Named_Color_Lowercase()
+ {
+ var success = Color.TryParse("red", out Color result);
+
+ Assert.True(success);
+ Assert.Equal(0xff, result.R);
+ Assert.Equal(0x00, result.G);
+ Assert.Equal(0x00, result.B);
+ Assert.Equal(0xff, result.A);
+ }
+
[Fact]
public void Parse_Parses_Named_Color_Uppercase()
{
@@ -50,22 +132,52 @@ namespace Avalonia.Visuals.UnitTests.Media
Assert.Equal(0xff, result.A);
}
+ [Fact]
+ public void TryParse_Parses_Named_Color_Uppercase()
+ {
+ var success = Color.TryParse("RED", out Color result);
+
+ Assert.True(success);
+ Assert.Equal(0xff, result.R);
+ Assert.Equal(0x00, result.G);
+ Assert.Equal(0x00, result.B);
+ Assert.Equal(0xff, result.A);
+ }
+
[Fact]
public void Parse_Hex_Value_Doesnt_Accept_Too_Few_Chars()
{
Assert.Throws(() => Color.Parse("#ff"));
}
+ [Fact]
+ public void TryParse_Hex_Value_Doesnt_Accept_Too_Few_Chars()
+ {
+ Assert.False(Color.TryParse("#ff", out _));
+ }
+
[Fact]
public void Parse_Hex_Value_Doesnt_Accept_Too_Many_Chars()
{
Assert.Throws(() => Color.Parse("#ff5555555"));
}
+ [Fact]
+ public void TryParse_Hex_Value_Doesnt_Accept_Too_Many_Chars()
+ {
+ Assert.False(Color.TryParse("#ff5555555", out _));
+ }
+
[Fact]
public void Parse_Hex_Value_Doesnt_Accept_Invalid_Number()
{
Assert.Throws(() => Color.Parse("#ff808g80"));
}
+
+ [Fact]
+ public void TryParse_Hex_Value_Doesnt_Accept_Invalid_Number()
+ {
+ Assert.False(Color.TryParse("#ff808g80", out _));
+ }
}
}
diff --git a/tests/Avalonia.Visuals.UnitTests/Media/MatrixTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/MatrixTests.cs
index ff1d17164e..6ef48b6161 100644
--- a/tests/Avalonia.Visuals.UnitTests/Media/MatrixTests.cs
+++ b/tests/Avalonia.Visuals.UnitTests/Media/MatrixTests.cs
@@ -1,4 +1,5 @@
-using System.Globalization;
+using System;
+using Avalonia.Utilities;
using Xunit;
namespace Avalonia.Visuals.UnitTests.Media
@@ -6,11 +7,93 @@ namespace Avalonia.Visuals.UnitTests.Media
public class MatrixTests
{
[Fact]
- public void Parse_Parses()
+ public void Can_Parse()
{
var matrix = Matrix.Parse("1,2,3,-4,5 6");
var expected = new Matrix(1, 2, 3, -4, 5, 6);
Assert.Equal(expected, matrix);
}
+
+ [Fact]
+ public void Singular_Has_No_Inverse()
+ {
+ var matrix = new Matrix(0, 0, 0, 0, 0, 0);
+
+ Assert.False(matrix.HasInverse);
+ }
+
+ [Fact]
+ public void Identity_Has_Inverse()
+ {
+ var matrix = Matrix.Identity;
+
+ Assert.True(matrix.HasInverse);
+ }
+
+ [Fact]
+ public void Can_Decompose_Translation()
+ {
+ var matrix = Matrix.CreateTranslation(5, 10);
+
+ var result = Matrix.TryDecomposeTransform(matrix, out Matrix.Decomposed decomposed);
+
+ Assert.Equal(true, result);
+ Assert.Equal(5, decomposed.Translate.X);
+ Assert.Equal(10, decomposed.Translate.Y);
+ }
+
+ [Theory]
+ [InlineData(30d)]
+ [InlineData(0d)]
+ [InlineData(90d)]
+ [InlineData(270d)]
+ public void Can_Decompose_Angle(double angleDeg)
+ {
+ var angleRad = MathUtilities.Deg2Rad(angleDeg);
+
+ var matrix = Matrix.CreateRotation(angleRad);
+
+ var result = Matrix.TryDecomposeTransform(matrix, out Matrix.Decomposed decomposed);
+
+ Assert.Equal(true, result);
+
+ var expected = NormalizeAngle(angleRad);
+ var actual = NormalizeAngle(decomposed.Angle);
+
+ Assert.Equal(expected, actual, 4);
+ }
+
+ [Theory]
+ [InlineData(1d, 1d)]
+ [InlineData(-1d, 1d)]
+ [InlineData(1d, -1d)]
+ [InlineData(5d, 10d)]
+ public void Can_Decompose_Scale(double x, double y)
+ {
+ var matrix = Matrix.CreateScale(x, y);
+
+ var result = Matrix.TryDecomposeTransform(matrix, out Matrix.Decomposed decomposed);
+
+ Assert.Equal(true, result);
+ Assert.Equal(x, decomposed.Scale.X);
+ Assert.Equal(y, decomposed.Scale.Y);
+ }
+
+ private static double NormalizeAngle(double rad)
+ {
+ double twoPi = 2 * Math.PI;
+
+ while (rad < 0)
+ {
+ rad += twoPi;
+ }
+
+ while (rad > twoPi)
+ {
+ rad -= twoPi;
+ }
+
+ return rad;
+ }
}
-}
\ No newline at end of file
+}
diff --git a/tests/Avalonia.Visuals.UnitTests/Media/TransformOperationsTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/TransformOperationsTests.cs
new file mode 100644
index 0000000000..856b4615a5
--- /dev/null
+++ b/tests/Avalonia.Visuals.UnitTests/Media/TransformOperationsTests.cs
@@ -0,0 +1,229 @@
+using Avalonia.Media.Transformation;
+using Avalonia.Utilities;
+using Xunit;
+
+namespace Avalonia.Visuals.UnitTests.Media
+{
+ public class TransformOperationsTests
+ {
+ [Theory]
+ [InlineData("translate(10px)", 10d, 0d)]
+ [InlineData("translate(10px, 10px)", 10d, 10d)]
+ [InlineData("translate(0px, 10px)", 0d, 10d)]
+ [InlineData("translate(10px, 0px)", 10d, 0d)]
+ [InlineData("translateX(10px)", 10d, 0d)]
+ [InlineData("translateY(10px)", 0d, 10d)]
+ public void Can_Parse_Translation(string data, double x, double y)
+ {
+ var transform = TransformOperations.Parse(data);
+
+ var operations = transform.Operations;
+
+ Assert.Single(operations);
+ Assert.Equal(TransformOperation.OperationType.Translate, operations[0].Type);
+ Assert.Equal(x, operations[0].Data.Translate.X);
+ Assert.Equal(y, operations[0].Data.Translate.Y);
+ }
+
+ [Theory]
+ [InlineData("rotate(90deg)", 90d)]
+ [InlineData("rotate(0.5turn)", 180d)]
+ [InlineData("rotate(200grad)", 180d)]
+ [InlineData("rotate(3.14159265rad)", 180d)]
+ public void Can_Parse_Rotation(string data, double angleDeg)
+ {
+ var transform = TransformOperations.Parse(data);
+
+ var operations = transform.Operations;
+
+ Assert.Single(operations);
+ Assert.Equal(TransformOperation.OperationType.Rotate, operations[0].Type);
+ Assert.Equal(MathUtilities.Deg2Rad(angleDeg), operations[0].Data.Rotate.Angle, 4);
+ }
+
+ [Theory]
+ [InlineData("scale(10)", 10d, 10d)]
+ [InlineData("scale(10, 10)", 10d, 10d)]
+ [InlineData("scale(0, 10)", 0d, 10d)]
+ [InlineData("scale(10, 0)", 10d, 0d)]
+ [InlineData("scaleX(10)", 10d, 1d)]
+ [InlineData("scaleY(10)", 1d, 10d)]
+ public void Can_Parse_Scale(string data, double x, double y)
+ {
+ var transform = TransformOperations.Parse(data);
+
+ var operations = transform.Operations;
+
+ Assert.Single(operations);
+ Assert.Equal(TransformOperation.OperationType.Scale, operations[0].Type);
+ Assert.Equal(x, operations[0].Data.Scale.X);
+ Assert.Equal(y, operations[0].Data.Scale.Y);
+ }
+
+ [Theory]
+ [InlineData("skew(90deg)", 90d, 0d)]
+ [InlineData("skew(0.5turn)", 180d, 0d)]
+ [InlineData("skew(200grad)", 180d, 0d)]
+ [InlineData("skew(3.14159265rad)", 180d, 0d)]
+ [InlineData("skewX(90deg)", 90d, 0d)]
+ [InlineData("skewX(0.5turn)", 180d, 0d)]
+ [InlineData("skewX(200grad)", 180d, 0d)]
+ [InlineData("skewX(3.14159265rad)", 180d, 0d)]
+ [InlineData("skew(0, 90deg)", 0d, 90d)]
+ [InlineData("skew(0, 0.5turn)", 0d, 180d)]
+ [InlineData("skew(0, 200grad)", 0d, 180d)]
+ [InlineData("skew(0, 3.14159265rad)", 0d, 180d)]
+ [InlineData("skewY(90deg)", 0d, 90d)]
+ [InlineData("skewY(0.5turn)", 0d, 180d)]
+ [InlineData("skewY(200grad)", 0d, 180d)]
+ [InlineData("skewY(3.14159265rad)", 0d, 180d)]
+ [InlineData("skew(90deg, 90deg)", 90d, 90d)]
+ [InlineData("skew(0.5turn, 0.5turn)", 180d, 180d)]
+ [InlineData("skew(200grad, 200grad)", 180d, 180d)]
+ [InlineData("skew(3.14159265rad, 3.14159265rad)", 180d, 180d)]
+ public void Can_Parse_Skew(string data, double x, double y)
+ {
+ var transform = TransformOperations.Parse(data);
+
+ var operations = transform.Operations;
+
+ Assert.Single(operations);
+ Assert.Equal(TransformOperation.OperationType.Skew, operations[0].Type);
+ Assert.Equal(MathUtilities.Deg2Rad(x), operations[0].Data.Skew.X, 4);
+ Assert.Equal(MathUtilities.Deg2Rad(y), operations[0].Data.Skew.Y, 4);
+ }
+
+ [Fact]
+ public void Can_Parse_Compound_Operations()
+ {
+ var data = "scale(1,2) translate(3px,4px) rotate(5deg) skew(6deg,7deg)";
+
+ var transform = TransformOperations.Parse(data);
+
+ var operations = transform.Operations;
+
+ Assert.Equal(TransformOperation.OperationType.Scale, operations[0].Type);
+ Assert.Equal(1, operations[0].Data.Scale.X);
+ Assert.Equal(2, operations[0].Data.Scale.Y);
+
+ Assert.Equal(TransformOperation.OperationType.Translate, operations[1].Type);
+ Assert.Equal(3, operations[1].Data.Translate.X);
+ Assert.Equal(4, operations[1].Data.Translate.Y);
+
+ Assert.Equal(TransformOperation.OperationType.Rotate, operations[2].Type);
+ Assert.Equal(MathUtilities.Deg2Rad(5), operations[2].Data.Rotate.Angle);
+
+ Assert.Equal(TransformOperation.OperationType.Skew, operations[3].Type);
+ Assert.Equal(MathUtilities.Deg2Rad(6), operations[3].Data.Skew.X);
+ Assert.Equal(MathUtilities.Deg2Rad(7), operations[3].Data.Skew.Y);
+ }
+
+ [Fact]
+ public void Can_Parse_Matrix_Operation()
+ {
+ var data = "matrix(1,2,3,4,5,6)";
+
+ var transform = TransformOperations.Parse(data);
+
+ var operations = transform.Operations;
+
+ Assert.Single(operations);
+ Assert.Equal(TransformOperation.OperationType.Matrix, operations[0].Type);
+
+ var expectedMatrix = new Matrix(1, 2, 3, 4, 5, 6);
+
+ Assert.Equal(expectedMatrix, operations[0].Matrix);
+ }
+
+ [Theory]
+ [InlineData(0d, 10d, 0d)]
+ [InlineData(0.5d, 5d, 10d)]
+ [InlineData(1d, 0d, 20d)]
+ public void Can_Interpolate_Translation(double progress, double x, double y)
+ {
+ var from = TransformOperations.Parse("translateX(10px)");
+ var to = TransformOperations.Parse("translateY(20px)");
+
+ var interpolated = TransformOperations.Interpolate(from, to, progress);
+
+ var operations = interpolated.Operations;
+
+ Assert.Single(operations);
+ Assert.Equal(TransformOperation.OperationType.Translate, operations[0].Type);
+ Assert.Equal(x, operations[0].Data.Translate.X);
+ Assert.Equal(y, operations[0].Data.Translate.Y);
+ }
+
+ [Theory]
+ [InlineData(0d, 10d, 1d)]
+ [InlineData(0.5d, 5.5d, 10.5d)]
+ [InlineData(1d, 1d, 20d)]
+ public void Can_Interpolate_Scale(double progress, double x, double y)
+ {
+ var from = TransformOperations.Parse("scaleX(10)");
+ var to = TransformOperations.Parse("scaleY(20)");
+
+ var interpolated = TransformOperations.Interpolate(from, to, progress);
+
+ var operations = interpolated.Operations;
+
+ Assert.Single(operations);
+ Assert.Equal(TransformOperation.OperationType.Scale, operations[0].Type);
+ Assert.Equal(x, operations[0].Data.Scale.X);
+ Assert.Equal(y, operations[0].Data.Scale.Y);
+ }
+
+ [Theory]
+ [InlineData(0d, 10d, 0d)]
+ [InlineData(0.5d, 5d, 10d)]
+ [InlineData(1d, 0d, 20d)]
+ public void Can_Interpolate_Skew(double progress, double x, double y)
+ {
+ var from = TransformOperations.Parse("skewX(10deg)");
+ var to = TransformOperations.Parse("skewY(20deg)");
+
+ var interpolated = TransformOperations.Interpolate(from, to, progress);
+
+ var operations = interpolated.Operations;
+
+ Assert.Single(operations);
+ Assert.Equal(TransformOperation.OperationType.Skew, operations[0].Type);
+ Assert.Equal(MathUtilities.Deg2Rad(x), operations[0].Data.Skew.X);
+ Assert.Equal(MathUtilities.Deg2Rad(y), operations[0].Data.Skew.Y);
+ }
+
+ [Theory]
+ [InlineData(0d, 10d)]
+ [InlineData(0.5d, 15d)]
+ [InlineData(1d,20d)]
+ public void Can_Interpolate_Rotation(double progress, double angle)
+ {
+ var from = TransformOperations.Parse("rotate(10deg)");
+ var to = TransformOperations.Parse("rotate(20deg)");
+
+ var interpolated = TransformOperations.Interpolate(from, to, progress);
+
+ var operations = interpolated.Operations;
+
+ Assert.Single(operations);
+ Assert.Equal(TransformOperation.OperationType.Rotate, operations[0].Type);
+ Assert.Equal(MathUtilities.Deg2Rad(angle), operations[0].Data.Rotate.Angle);
+ }
+
+ [Fact]
+ public void Interpolation_Fallback_To_Matrix()
+ {
+ double progress = 0.5d;
+
+ var from = TransformOperations.Parse("rotate(45deg)");
+ var to = TransformOperations.Parse("translate(100px, 100px) rotate(1215deg)");
+
+ var interpolated = TransformOperations.Interpolate(from, to, progress);
+
+ var operations = interpolated.Operations;
+
+ Assert.Single(operations);
+ Assert.Equal(TransformOperation.OperationType.Matrix, operations[0].Type);
+ }
+ }
+}