From 9d335c3a96b3ca1d6c9aedcd6a5c1408375d4e78 Mon Sep 17 00:00:00 2001 From: v-yadli Date: Sun, 13 Dec 2020 02:11:28 +0800 Subject: [PATCH 001/224] fix #4996 --- native/Avalonia.Native/src/OSX/window.mm | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/native/Avalonia.Native/src/OSX/window.mm b/native/Avalonia.Native/src/OSX/window.mm index 8419258fe9..9cb5fcbb58 100644 --- a/native/Avalonia.Native/src/OSX/window.mm +++ b/native/Avalonia.Native/src/OSX/window.mm @@ -1590,7 +1590,12 @@ NSArray* AllLoopModes = [NSArray arrayWithObjects: NSDefaultRunLoopMode, NSEvent if(_parent != nullptr) { - _lastKeyHandled = _parent->BaseEvents->RawKeyEvent(type, timestamp, modifiers, key); + auto handled = _parent->BaseEvents->RawKeyEvent(type, timestamp, modifiers, key); + if (key != LeftCtrl && key != RightCtrl) { + _lastKeyHandled = handled; + } else { + _lastKeyHandled = false; + } } } From c8f029386d88a62a9403c98adb52e519a5529ac3 Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Sat, 22 Jan 2022 14:01:35 +0300 Subject: [PATCH 002/224] Map devices, add some native api --- src/Avalonia.Input/IPenDevice.cs | 10 + src/Avalonia.Input/ITouchPadDevice.cs | 10 + src/Avalonia.Input/PenDevice.cs | 488 ++++++++++++++++++ src/Avalonia.Input/TouchPadDevice.cs | 488 ++++++++++++++++++ .../Interop/UnmanagedMethods.cs | 64 +++ .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 155 ++++++ 6 files changed, 1215 insertions(+) create mode 100644 src/Avalonia.Input/IPenDevice.cs create mode 100644 src/Avalonia.Input/ITouchPadDevice.cs create mode 100644 src/Avalonia.Input/PenDevice.cs create mode 100644 src/Avalonia.Input/TouchPadDevice.cs diff --git a/src/Avalonia.Input/IPenDevice.cs b/src/Avalonia.Input/IPenDevice.cs new file mode 100644 index 0000000000..1cc0fcf76d --- /dev/null +++ b/src/Avalonia.Input/IPenDevice.cs @@ -0,0 +1,10 @@ +namespace Avalonia.Input +{ + /// + /// Represents a pen/stylus device. + /// + public interface IPenDevice : IPointerDevice + { + + } +} diff --git a/src/Avalonia.Input/ITouchPadDevice.cs b/src/Avalonia.Input/ITouchPadDevice.cs new file mode 100644 index 0000000000..ea6c57f948 --- /dev/null +++ b/src/Avalonia.Input/ITouchPadDevice.cs @@ -0,0 +1,10 @@ +namespace Avalonia.Input +{ + /// + /// Represents a touch pad device. + /// + public interface ITouchPadDevice : IPointerDevice + { + + } +} diff --git a/src/Avalonia.Input/PenDevice.cs b/src/Avalonia.Input/PenDevice.cs new file mode 100644 index 0000000000..dd3d60ebb9 --- /dev/null +++ b/src/Avalonia.Input/PenDevice.cs @@ -0,0 +1,488 @@ +using System; +using System.Linq; +using System.Reactive.Linq; +using Avalonia.Input.Raw; +using Avalonia.Interactivity; +using Avalonia.Platform; +using Avalonia.VisualTree; + +namespace Avalonia.Input +{ + /// + /// Represents a pen/stylus device. + /// + public class PenDevice : IPenDevice, IDisposable + { + private int _clickCount; + private Rect _lastClickRect; + private ulong _lastClickTime; + + private readonly Pointer _pointer; + private bool _disposed; + private PixelPoint? _position; + + public PenDevice(Pointer? pointer = null) + { + _pointer = pointer ?? new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true); + } + + /// + /// Gets the control that is currently capturing by the mouse, if any. + /// + /// + /// When an element captures the mouse, it receives mouse input whether the cursor is + /// within the control's bounds or not. To set the mouse capture, call the + /// method. + /// + [Obsolete("Use IPointer instead")] + public IInputElement? Captured => _pointer.Captured; + + /// + /// Gets the mouse position, in screen coordinates. + /// + [Obsolete("Use events instead")] + public PixelPoint Position + { + get => _position ?? new PixelPoint(-1, -1); + protected set => _position = value; + } + + /// + /// Captures mouse input to the specified control. + /// + /// The control. + /// + /// When an element captures the mouse, it receives mouse input whether the cursor is + /// within the control's bounds or not. The current mouse capture control is exposed + /// by the property. + /// + public void Capture(IInputElement? control) + { + _pointer.Capture(control); + } + + /// + /// Gets the mouse position relative to a control. + /// + /// The control. + /// The mouse position in the control's coordinates. + public Point GetPosition(IVisual relativeTo) + { + relativeTo = relativeTo ?? throw new ArgumentNullException(nameof(relativeTo)); + + if (relativeTo.VisualRoot == null) + { + throw new InvalidOperationException("Control is not attached to visual tree."); + } + +#pragma warning disable CS0618 // Type or member is obsolete + var rootPoint = relativeTo.VisualRoot.PointToClient(Position); +#pragma warning restore CS0618 // Type or member is obsolete + var transform = relativeTo.VisualRoot.TransformToVisual(relativeTo); + return rootPoint * transform!.Value; + } + + public void ProcessRawEvent(RawInputEventArgs e) + { + if (!e.Handled && e is RawPointerEventArgs margs) + ProcessRawEvent(margs); + } + + public void TopLevelClosed(IInputRoot root) + { + ClearPointerOver(this, 0, root, PointerPointProperties.None, KeyModifiers.None); + } + + public void SceneInvalidated(IInputRoot root, Rect rect) + { + // Pointer is outside of the target area + if (_position == null ) + { + if (root.PointerOverElement != null) + ClearPointerOver(this, 0, root, PointerPointProperties.None, KeyModifiers.None); + return; + } + + + var clientPoint = root.PointToClient(_position.Value); + + if (rect.Contains(clientPoint)) + { + if (_pointer.Captured == null) + { + SetPointerOver(this, 0 /* TODO: proper timestamp */, root, clientPoint, + PointerPointProperties.None, KeyModifiers.None); + } + else + { + SetPointerOver(this, 0 /* TODO: proper timestamp */, root, _pointer.Captured, + PointerPointProperties.None, KeyModifiers.None); + } + } + } + + int ButtonCount(PointerPointProperties props) + { + var rv = 0; + if (props.IsLeftButtonPressed) + rv++; + if (props.IsMiddleButtonPressed) + rv++; + if (props.IsRightButtonPressed) + rv++; + if (props.IsXButton1Pressed) + rv++; + if (props.IsXButton2Pressed) + rv++; + return rv; + } + + private void ProcessRawEvent(RawPointerEventArgs e) + { + e = e ?? throw new ArgumentNullException(nameof(e)); + + var mouse = (PenDevice)e.Device; + if(mouse._disposed) + return; + + _position = e.Root.PointToScreen(e.Position); + var props = CreateProperties(e); + var keyModifiers = KeyModifiersUtils.ConvertToKey(e.InputModifiers); + switch (e.Type) + { + case RawPointerEventType.LeaveWindow: + LeaveWindow(mouse, e.Timestamp, e.Root, props, keyModifiers); + break; + case RawPointerEventType.LeftButtonDown: + case RawPointerEventType.RightButtonDown: + case RawPointerEventType.MiddleButtonDown: + case RawPointerEventType.XButton1Down: + case RawPointerEventType.XButton2Down: + if (ButtonCount(props) > 1) + e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); + else + e.Handled = MouseDown(mouse, e.Timestamp, e.Root, e.Position, + props, keyModifiers); + break; + case RawPointerEventType.LeftButtonUp: + case RawPointerEventType.RightButtonUp: + case RawPointerEventType.MiddleButtonUp: + case RawPointerEventType.XButton1Up: + case RawPointerEventType.XButton2Up: + if (ButtonCount(props) != 0) + e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); + else + e.Handled = MouseUp(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); + break; + case RawPointerEventType.Move: + e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); + break; + case RawPointerEventType.Wheel: + e.Handled = MouseWheel(mouse, e.Timestamp, e.Root, e.Position, props, ((RawMouseWheelEventArgs)e).Delta, keyModifiers); + break; + } + } + + private void LeaveWindow(IPenDevice device, ulong timestamp, IInputRoot root, PointerPointProperties properties, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + _position = null; + ClearPointerOver(this, timestamp, root, properties, inputModifiers); + } + + + PointerPointProperties CreateProperties(RawPointerEventArgs args) + { + + var kind = PointerUpdateKind.Other; + + if (args.Type == RawPointerEventType.LeftButtonDown) + kind = PointerUpdateKind.LeftButtonPressed; + if (args.Type == RawPointerEventType.MiddleButtonDown) + kind = PointerUpdateKind.MiddleButtonPressed; + if (args.Type == RawPointerEventType.RightButtonDown) + kind = PointerUpdateKind.RightButtonPressed; + if (args.Type == RawPointerEventType.XButton1Down) + kind = PointerUpdateKind.XButton1Pressed; + if (args.Type == RawPointerEventType.XButton2Down) + kind = PointerUpdateKind.XButton2Pressed; + if (args.Type == RawPointerEventType.LeftButtonUp) + kind = PointerUpdateKind.LeftButtonReleased; + if (args.Type == RawPointerEventType.MiddleButtonUp) + kind = PointerUpdateKind.MiddleButtonReleased; + if (args.Type == RawPointerEventType.RightButtonUp) + kind = PointerUpdateKind.RightButtonReleased; + if (args.Type == RawPointerEventType.XButton1Up) + kind = PointerUpdateKind.XButton1Released; + if (args.Type == RawPointerEventType.XButton2Up) + kind = PointerUpdateKind.XButton2Released; + + return new PointerPointProperties(args.InputModifiers, kind); + } + + private MouseButton _lastMouseDownButton; + private bool MouseDown(IPenDevice device, ulong timestamp, IInputElement root, Point p, + PointerPointProperties properties, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + var hit = HitTest(root, p); + + if (hit != null) + { + _pointer.Capture(hit); + var source = GetSource(hit); + if (source != null) + { + var settings = AvaloniaLocator.Current.GetService(); + var doubleClickTime = settings?.DoubleClickTime.TotalMilliseconds ?? 500; + var doubleClickSize = settings?.DoubleClickSize ?? new Size(4, 4); + + if (!_lastClickRect.Contains(p) || timestamp - _lastClickTime > doubleClickTime) + { + _clickCount = 0; + } + + ++_clickCount; + _lastClickTime = timestamp; + _lastClickRect = new Rect(p, new Size()) + .Inflate(new Thickness(doubleClickSize.Width / 2, doubleClickSize.Height / 2)); + _lastMouseDownButton = properties.PointerUpdateKind.GetMouseButton(); + var e = new PointerPressedEventArgs(source, _pointer, root, p, timestamp, properties, inputModifiers, _clickCount); + source.RaiseEvent(e); + return e.Handled; + } + } + + return false; + } + + private bool MouseMove(IPenDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties properties, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + IInputElement? source; + + if (_pointer.Captured == null) + { + source = SetPointerOver(this, timestamp, root, p, properties, inputModifiers); + } + else + { + SetPointerOver(this, timestamp, root, _pointer.Captured, properties, inputModifiers); + source = _pointer.Captured; + } + + if (source is object) + { + var e = new PointerEventArgs(InputElement.PointerMovedEvent, source, _pointer, root, + p, timestamp, properties, inputModifiers); + + source.RaiseEvent(e); + return e.Handled; + } + + return false; + } + + private bool MouseUp(IPenDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties props, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + var hit = HitTest(root, p); + var source = GetSource(hit); + + if (source is not null) + { + var e = new PointerReleasedEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers, + _lastMouseDownButton); + + source?.RaiseEvent(e); + _pointer.Capture(null); + return e.Handled; + } + + return false; + } + + private bool MouseWheel(IPenDevice device, ulong timestamp, IInputRoot root, Point p, + PointerPointProperties props, + Vector delta, KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + var hit = HitTest(root, p); + var source = GetSource(hit); + + if (source is not null) + { + var e = new PointerWheelEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers, delta); + + source?.RaiseEvent(e); + return e.Handled; + } + + return false; + } + + private IInteractive? GetSource(IVisual? hit) + { + if (hit is null) + return null; + + return _pointer.Captured ?? + (hit as IInteractive) ?? + hit.GetSelfAndVisualAncestors().OfType().FirstOrDefault(); + } + + private IInputElement? HitTest(IInputElement root, Point p) + { + root = root ?? throw new ArgumentNullException(nameof(root)); + + return _pointer.Captured ?? root.InputHitTest(p); + } + + PointerEventArgs CreateSimpleEvent(RoutedEvent ev, ulong timestamp, IInteractive? source, + PointerPointProperties properties, + KeyModifiers inputModifiers) + { + return new PointerEventArgs(ev, source, _pointer, null, default, + timestamp, properties, inputModifiers); + } + + private void ClearPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, + PointerPointProperties properties, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + var element = root.PointerOverElement; + var e = CreateSimpleEvent(InputElement.PointerLeaveEvent, timestamp, element, properties, inputModifiers); + + if (element!=null && !element.IsAttachedToVisualTree) + { + // element has been removed from visual tree so do top down cleanup + if (root.IsPointerOver) + ClearChildrenPointerOver(e, root,true); + } + while (element != null) + { + e.Source = element; + e.Handled = false; + element.RaiseEvent(e); + element = (IInputElement?)element.VisualParent; + } + + root.PointerOverElement = null; + } + + private void ClearChildrenPointerOver(PointerEventArgs e, IInputElement element,bool clearRoot) + { + foreach (IInputElement el in element.VisualChildren) + { + if (el.IsPointerOver) + { + ClearChildrenPointerOver(e, el, true); + break; + } + } + if(clearRoot) + { + e.Source = element; + e.Handled = false; + element.RaiseEvent(e); + } + } + + private IInputElement? SetPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, Point p, + PointerPointProperties properties, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + var element = root.InputHitTest(p); + + if (element != root.PointerOverElement) + { + if (element != null) + { + SetPointerOver(device, timestamp, root, element, properties, inputModifiers); + } + else + { + ClearPointerOver(device, timestamp, root, properties, inputModifiers); + } + } + + return element; + } + + private void SetPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, IInputElement element, + PointerPointProperties properties, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + element = element ?? throw new ArgumentNullException(nameof(element)); + + IInputElement? branch = null; + + IInputElement? el = element; + + while (el != null) + { + if (el.IsPointerOver) + { + branch = el; + break; + } + el = (IInputElement?)el.VisualParent; + } + + el = root.PointerOverElement; + + var e = CreateSimpleEvent(InputElement.PointerLeaveEvent, timestamp, el, properties, inputModifiers); + if (el!=null && branch!=null && !el.IsAttachedToVisualTree) + { + ClearChildrenPointerOver(e,branch,false); + } + + while (el != null && el != branch) + { + e.Source = el; + e.Handled = false; + el.RaiseEvent(e); + el = (IInputElement?)el.VisualParent; + } + + el = root.PointerOverElement = element; + e.RoutedEvent = InputElement.PointerEnterEvent; + + while (el != null && el != branch) + { + e.Source = el; + e.Handled = false; + el.RaiseEvent(e); + el = (IInputElement?)el.VisualParent; + } + } + + public void Dispose() + { + _disposed = true; + _pointer?.Dispose(); + } + } +} diff --git a/src/Avalonia.Input/TouchPadDevice.cs b/src/Avalonia.Input/TouchPadDevice.cs new file mode 100644 index 0000000000..fcd254f588 --- /dev/null +++ b/src/Avalonia.Input/TouchPadDevice.cs @@ -0,0 +1,488 @@ +using System; +using System.Linq; +using System.Reactive.Linq; +using Avalonia.Input.Raw; +using Avalonia.Interactivity; +using Avalonia.Platform; +using Avalonia.VisualTree; + +namespace Avalonia.Input +{ + /// + /// Represents a touch pad device. + /// + public class TouchPadDevice : ITouchPadDevice, IDisposable + { + private int _clickCount; + private Rect _lastClickRect; + private ulong _lastClickTime; + + private readonly Pointer _pointer; + private bool _disposed; + private PixelPoint? _position; + + public TouchPadDevice(Pointer? pointer = null) + { + _pointer = pointer ?? new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true); + } + + /// + /// Gets the control that is currently capturing by the mouse, if any. + /// + /// + /// When an element captures the mouse, it receives mouse input whether the cursor is + /// within the control's bounds or not. To set the mouse capture, call the + /// method. + /// + [Obsolete("Use IPointer instead")] + public IInputElement? Captured => _pointer.Captured; + + /// + /// Gets the mouse position, in screen coordinates. + /// + [Obsolete("Use events instead")] + public PixelPoint Position + { + get => _position ?? new PixelPoint(-1, -1); + protected set => _position = value; + } + + /// + /// Captures mouse input to the specified control. + /// + /// The control. + /// + /// When an element captures the mouse, it receives mouse input whether the cursor is + /// within the control's bounds or not. The current mouse capture control is exposed + /// by the property. + /// + public void Capture(IInputElement? control) + { + _pointer.Capture(control); + } + + /// + /// Gets the mouse position relative to a control. + /// + /// The control. + /// The mouse position in the control's coordinates. + public Point GetPosition(IVisual relativeTo) + { + relativeTo = relativeTo ?? throw new ArgumentNullException(nameof(relativeTo)); + + if (relativeTo.VisualRoot == null) + { + throw new InvalidOperationException("Control is not attached to visual tree."); + } + +#pragma warning disable CS0618 // Type or member is obsolete + var rootPoint = relativeTo.VisualRoot.PointToClient(Position); +#pragma warning restore CS0618 // Type or member is obsolete + var transform = relativeTo.VisualRoot.TransformToVisual(relativeTo); + return rootPoint * transform!.Value; + } + + public void ProcessRawEvent(RawInputEventArgs e) + { + if (!e.Handled && e is RawPointerEventArgs margs) + ProcessRawEvent(margs); + } + + public void TopLevelClosed(IInputRoot root) + { + ClearPointerOver(this, 0, root, PointerPointProperties.None, KeyModifiers.None); + } + + public void SceneInvalidated(IInputRoot root, Rect rect) + { + // Pointer is outside of the target area + if (_position == null ) + { + if (root.PointerOverElement != null) + ClearPointerOver(this, 0, root, PointerPointProperties.None, KeyModifiers.None); + return; + } + + + var clientPoint = root.PointToClient(_position.Value); + + if (rect.Contains(clientPoint)) + { + if (_pointer.Captured == null) + { + SetPointerOver(this, 0 /* TODO: proper timestamp */, root, clientPoint, + PointerPointProperties.None, KeyModifiers.None); + } + else + { + SetPointerOver(this, 0 /* TODO: proper timestamp */, root, _pointer.Captured, + PointerPointProperties.None, KeyModifiers.None); + } + } + } + + int ButtonCount(PointerPointProperties props) + { + var rv = 0; + if (props.IsLeftButtonPressed) + rv++; + if (props.IsMiddleButtonPressed) + rv++; + if (props.IsRightButtonPressed) + rv++; + if (props.IsXButton1Pressed) + rv++; + if (props.IsXButton2Pressed) + rv++; + return rv; + } + + private void ProcessRawEvent(RawPointerEventArgs e) + { + e = e ?? throw new ArgumentNullException(nameof(e)); + + var mouse = (TouchPadDevice)e.Device; + if(mouse._disposed) + return; + + _position = e.Root.PointToScreen(e.Position); + var props = CreateProperties(e); + var keyModifiers = KeyModifiersUtils.ConvertToKey(e.InputModifiers); + switch (e.Type) + { + case RawPointerEventType.LeaveWindow: + LeaveWindow(mouse, e.Timestamp, e.Root, props, keyModifiers); + break; + case RawPointerEventType.LeftButtonDown: + case RawPointerEventType.RightButtonDown: + case RawPointerEventType.MiddleButtonDown: + case RawPointerEventType.XButton1Down: + case RawPointerEventType.XButton2Down: + if (ButtonCount(props) > 1) + e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); + else + e.Handled = MouseDown(mouse, e.Timestamp, e.Root, e.Position, + props, keyModifiers); + break; + case RawPointerEventType.LeftButtonUp: + case RawPointerEventType.RightButtonUp: + case RawPointerEventType.MiddleButtonUp: + case RawPointerEventType.XButton1Up: + case RawPointerEventType.XButton2Up: + if (ButtonCount(props) != 0) + e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); + else + e.Handled = MouseUp(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); + break; + case RawPointerEventType.Move: + e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); + break; + case RawPointerEventType.Wheel: + e.Handled = MouseWheel(mouse, e.Timestamp, e.Root, e.Position, props, ((RawMouseWheelEventArgs)e).Delta, keyModifiers); + break; + } + } + + private void LeaveWindow(ITouchPadDevice device, ulong timestamp, IInputRoot root, PointerPointProperties properties, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + _position = null; + ClearPointerOver(this, timestamp, root, properties, inputModifiers); + } + + + PointerPointProperties CreateProperties(RawPointerEventArgs args) + { + + var kind = PointerUpdateKind.Other; + + if (args.Type == RawPointerEventType.LeftButtonDown) + kind = PointerUpdateKind.LeftButtonPressed; + if (args.Type == RawPointerEventType.MiddleButtonDown) + kind = PointerUpdateKind.MiddleButtonPressed; + if (args.Type == RawPointerEventType.RightButtonDown) + kind = PointerUpdateKind.RightButtonPressed; + if (args.Type == RawPointerEventType.XButton1Down) + kind = PointerUpdateKind.XButton1Pressed; + if (args.Type == RawPointerEventType.XButton2Down) + kind = PointerUpdateKind.XButton2Pressed; + if (args.Type == RawPointerEventType.LeftButtonUp) + kind = PointerUpdateKind.LeftButtonReleased; + if (args.Type == RawPointerEventType.MiddleButtonUp) + kind = PointerUpdateKind.MiddleButtonReleased; + if (args.Type == RawPointerEventType.RightButtonUp) + kind = PointerUpdateKind.RightButtonReleased; + if (args.Type == RawPointerEventType.XButton1Up) + kind = PointerUpdateKind.XButton1Released; + if (args.Type == RawPointerEventType.XButton2Up) + kind = PointerUpdateKind.XButton2Released; + + return new PointerPointProperties(args.InputModifiers, kind); + } + + private MouseButton _lastMouseDownButton; + private bool MouseDown(ITouchPadDevice device, ulong timestamp, IInputElement root, Point p, + PointerPointProperties properties, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + var hit = HitTest(root, p); + + if (hit != null) + { + _pointer.Capture(hit); + var source = GetSource(hit); + if (source != null) + { + var settings = AvaloniaLocator.Current.GetService(); + var doubleClickTime = settings?.DoubleClickTime.TotalMilliseconds ?? 500; + var doubleClickSize = settings?.DoubleClickSize ?? new Size(4, 4); + + if (!_lastClickRect.Contains(p) || timestamp - _lastClickTime > doubleClickTime) + { + _clickCount = 0; + } + + ++_clickCount; + _lastClickTime = timestamp; + _lastClickRect = new Rect(p, new Size()) + .Inflate(new Thickness(doubleClickSize.Width / 2, doubleClickSize.Height / 2)); + _lastMouseDownButton = properties.PointerUpdateKind.GetMouseButton(); + var e = new PointerPressedEventArgs(source, _pointer, root, p, timestamp, properties, inputModifiers, _clickCount); + source.RaiseEvent(e); + return e.Handled; + } + } + + return false; + } + + private bool MouseMove(ITouchPadDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties properties, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + IInputElement? source; + + if (_pointer.Captured == null) + { + source = SetPointerOver(this, timestamp, root, p, properties, inputModifiers); + } + else + { + SetPointerOver(this, timestamp, root, _pointer.Captured, properties, inputModifiers); + source = _pointer.Captured; + } + + if (source is object) + { + var e = new PointerEventArgs(InputElement.PointerMovedEvent, source, _pointer, root, + p, timestamp, properties, inputModifiers); + + source.RaiseEvent(e); + return e.Handled; + } + + return false; + } + + private bool MouseUp(ITouchPadDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties props, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + var hit = HitTest(root, p); + var source = GetSource(hit); + + if (source is not null) + { + var e = new PointerReleasedEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers, + _lastMouseDownButton); + + source?.RaiseEvent(e); + _pointer.Capture(null); + return e.Handled; + } + + return false; + } + + private bool MouseWheel(ITouchPadDevice device, ulong timestamp, IInputRoot root, Point p, + PointerPointProperties props, + Vector delta, KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + var hit = HitTest(root, p); + var source = GetSource(hit); + + if (source is not null) + { + var e = new PointerWheelEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers, delta); + + source?.RaiseEvent(e); + return e.Handled; + } + + return false; + } + + private IInteractive? GetSource(IVisual? hit) + { + if (hit is null) + return null; + + return _pointer.Captured ?? + (hit as IInteractive) ?? + hit.GetSelfAndVisualAncestors().OfType().FirstOrDefault(); + } + + private IInputElement? HitTest(IInputElement root, Point p) + { + root = root ?? throw new ArgumentNullException(nameof(root)); + + return _pointer.Captured ?? root.InputHitTest(p); + } + + PointerEventArgs CreateSimpleEvent(RoutedEvent ev, ulong timestamp, IInteractive? source, + PointerPointProperties properties, + KeyModifiers inputModifiers) + { + return new PointerEventArgs(ev, source, _pointer, null, default, + timestamp, properties, inputModifiers); + } + + private void ClearPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, + PointerPointProperties properties, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + var element = root.PointerOverElement; + var e = CreateSimpleEvent(InputElement.PointerLeaveEvent, timestamp, element, properties, inputModifiers); + + if (element!=null && !element.IsAttachedToVisualTree) + { + // element has been removed from visual tree so do top down cleanup + if (root.IsPointerOver) + ClearChildrenPointerOver(e, root,true); + } + while (element != null) + { + e.Source = element; + e.Handled = false; + element.RaiseEvent(e); + element = (IInputElement?)element.VisualParent; + } + + root.PointerOverElement = null; + } + + private void ClearChildrenPointerOver(PointerEventArgs e, IInputElement element,bool clearRoot) + { + foreach (IInputElement el in element.VisualChildren) + { + if (el.IsPointerOver) + { + ClearChildrenPointerOver(e, el, true); + break; + } + } + if(clearRoot) + { + e.Source = element; + e.Handled = false; + element.RaiseEvent(e); + } + } + + private IInputElement? SetPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, Point p, + PointerPointProperties properties, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + + var element = root.InputHitTest(p); + + if (element != root.PointerOverElement) + { + if (element != null) + { + SetPointerOver(device, timestamp, root, element, properties, inputModifiers); + } + else + { + ClearPointerOver(device, timestamp, root, properties, inputModifiers); + } + } + + return element; + } + + private void SetPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, IInputElement element, + PointerPointProperties properties, + KeyModifiers inputModifiers) + { + device = device ?? throw new ArgumentNullException(nameof(device)); + root = root ?? throw new ArgumentNullException(nameof(root)); + element = element ?? throw new ArgumentNullException(nameof(element)); + + IInputElement? branch = null; + + IInputElement? el = element; + + while (el != null) + { + if (el.IsPointerOver) + { + branch = el; + break; + } + el = (IInputElement?)el.VisualParent; + } + + el = root.PointerOverElement; + + var e = CreateSimpleEvent(InputElement.PointerLeaveEvent, timestamp, el, properties, inputModifiers); + if (el!=null && branch!=null && !el.IsAttachedToVisualTree) + { + ClearChildrenPointerOver(e,branch,false); + } + + while (el != null && el != branch) + { + e.Source = el; + e.Handled = false; + el.RaiseEvent(e); + el = (IInputElement?)el.VisualParent; + } + + el = root.PointerOverElement = element; + e.RoutedEvent = InputElement.PointerEnterEvent; + + while (el != null && el != branch) + { + e.Source = el; + e.Handled = false; + el.RaiseEvent(e); + el = (IInputElement?)el.VisualParent; + } + } + + public void Dispose() + { + _disposed = true; + _pointer?.Dispose(); + } + } +} diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index c74c5fbc01..db81e8197f 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -514,6 +514,33 @@ namespace Avalonia.Win32.Interop CS_DROPSHADOW = 0x00020000 } + [Flags] + public enum PointerDeviceChangeFlags + { + PDC_ARRIVAL = 0x001, + PDC_REMOVAL = 0x002, + PDC_ORIENTATION_0 = 0x004, + PDC_ORIENTATION_90 = 0x008, + PDC_ORIENTATION_180 = 0x010, + PDC_ORIENTATION_270 = 0x020, + PDC_MODE_DEFAULT = 0x040, + PDC_MODE_CENTERED = 0x080, + PDC_MAPPING_CHANGE = 0x100, + PDC_RESOLUTION = 0x200, + PDC_ORIGIN = 0x400, + PDC_MODE_ASPECTRATIOPRESERVED = 0x800 + } + + public enum InputType + { + NONE = 0x00000000, + POINTER = 0x00000001, + TOUCH = 0x00000002, + PEN = 0x00000003, + MOUSE = 0x00000004, + TOUCHPAD = 0x00000005 + } + public enum WindowsMessage : uint { WM_NULL = 0x0000, @@ -689,6 +716,25 @@ namespace Avalonia.Win32.Interop WM_EXITSIZEMOVE = 0x0232, WM_DROPFILES = 0x0233, WM_MDIREFRESHMENU = 0x0234, + + WM_POINTERDEVICECHANGE = 0x0238, + WM_POINTERDEVICEINRANGE = 0x239, + WM_POINTERDEVICEOUTOFRANGE = 0x23A, + WM_NCPOINTERUPDATE = 0x0241, + WM_NCPOINTERDOWN = 0x0242, + WM_NCPOINTERUP = 0x0243, + WM_POINTERUPDATE = 0x0245, + WM_POINTERDOWN = 0x0246, + WM_POINTERUP = 0x0247, + WM_POINTERENTER = 0x0249, + WM_POINTERLEAVE = 0x024A, + WM_POINTERACTIVATE = 0x024B, + WM_POINTERCAPTURECHANGED = 0x024C, + WM_TOUCHHITTESTING = 0x024D, + WM_POINTERWHEEL = 0x024E, + WM_POINTERHWHEEL = 0x024F, + WM_POINTERHITTEST = 0x0250, + WM_IME_SETCONTEXT = 0x0281, WM_IME_NOTIFY = 0x0282, WM_IME_CONTROL = 0x0283, @@ -903,6 +949,24 @@ namespace Avalonia.Win32.Interop public const int SizeOf_BITMAPINFOHEADER = 40; + [DllImport("user32.dll", SetLastError = true)] + public static extern int EnableMouseInPointer(bool enable); + + [DllImport("user32.dll", SetLastError = true)] + public static extern bool GetPointerCursorId(uint pointerID, out uint cursorId); + + [DllImport("user32.dll", SetLastError = true)] + public static extern bool GetPointerType(uint pointerID, out InputType pointerType); + + [DllImport("user32.dll", SetLastError = true)] + public static extern void GetUnpredictedMessagePos(); + + [DllImport("user32.dll", SetLastError = true)] + public static extern bool IsMouseInPointerEnabled(); + + [DllImport("user32.dll", SetLastError = true)] + public static extern bool SkipPointerFrameMessages(uint pointerID); + [DllImport("user32.dll")] public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData); diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 88a0744e3e..e16901af2c 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -166,6 +166,10 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MBUTTONDOWN: case WindowsMessage.WM_XBUTTONDOWN: { + if (BelowWin8) + { + break; + } shouldTakeFocus = ShouldTakeFocusOnClick; if (ShouldIgnoreTouchEmulatedMessage()) { @@ -195,6 +199,10 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MBUTTONUP: case WindowsMessage.WM_XBUTTONUP: { + if (BelowWin8) + { + break; + } if (ShouldIgnoreTouchEmulatedMessage()) { break; @@ -219,11 +227,19 @@ namespace Avalonia.Win32 } // Mouse capture is lost case WindowsMessage.WM_CANCELMODE: + if (BelowWin8) + { + break; + } _mouseDevice.Capture(null); break; case WindowsMessage.WM_MOUSEMOVE: { + if (BelowWin8) + { + break; + } if (ShouldIgnoreTouchEmulatedMessage()) { break; @@ -254,6 +270,10 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MOUSEWHEEL: { + if (BelowWin8) + { + break; + } e = new RawMouseWheelEventArgs( _mouseDevice, timestamp, @@ -265,6 +285,10 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MOUSEHWHEEL: { + if (BelowWin8) + { + break; + } e = new RawMouseWheelEventArgs( _mouseDevice, timestamp, @@ -276,6 +300,10 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MOUSELEAVE: { + if (BelowWin8) + { + break; + } _trackingMouse = false; e = new RawPointerEventArgs( _mouseDevice, @@ -291,6 +319,10 @@ namespace Avalonia.Win32 case WindowsMessage.WM_NCMBUTTONDOWN: case WindowsMessage.WM_NCXBUTTONDOWN: { + if (BelowWin8) + { + break; + } e = new RawPointerEventArgs( _mouseDevice, timestamp, @@ -311,6 +343,10 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_TOUCH: { + if (BelowWin8) + { + break; + } var touchInputCount = wParam.ToInt32(); var pTouchInputs = stackalloc TOUCHINPUT[touchInputCount]; @@ -338,6 +374,117 @@ namespace Avalonia.Win32 break; } + + + + + + + + + + + + case WindowsMessage.WM_POINTERDEVICECHANGE: + case WindowsMessage.WM_POINTERDEVICEINRANGE: + case WindowsMessage.WM_POINTERDEVICEOUTOFRANGE: + { + + break; + } + case WindowsMessage.WM_NCPOINTERUPDATE: + case WindowsMessage.WM_NCPOINTERDOWN: + case WindowsMessage.WM_NCPOINTERUP: + { + + break; + } + case WindowsMessage.WM_POINTERUPDATE: + case WindowsMessage.WM_POINTERDOWN: + case WindowsMessage.WM_POINTERUP: + { + + break; + } + case WindowsMessage.WM_POINTERENTER: + case WindowsMessage.WM_POINTERLEAVE: + { + + break; + } + case WindowsMessage.WM_POINTERACTIVATE: + case WindowsMessage.WM_POINTERCAPTURECHANGED: + { + + break; + } + case WindowsMessage.WM_TOUCHHITTESTING: + { + + break; + } + case WindowsMessage.WM_POINTERWHEEL: + { + var pointerId = ToPointerId(wParam); + GetPointerType(pointerId, out var type); + IInputDevice device = _mouseDevice; + switch (type) + { + case InputType.PEN: + + break; + case InputType.TOUCH: + device = _touchDevice; + break; + case InputType.TOUCHPAD: + + break; + } + + + var delta = GetWheelDelta(wParam); + var point = PointFromLParam(lParam); + e = new RawMouseWheelEventArgs( + _mouseDevice, + timestamp, + _owner, + PointToClient(point), + new Vector(0, delta / wheelDelta), + GetMouseModifiers(wParam)); + break; + } + case WindowsMessage.WM_POINTERHWHEEL: + { + + break; + } + case WindowsMessage.WM_POINTERHITTEST: + { + + break; + } + + + + + + + + + + + + + + + + + + + + + + case WindowsMessage.WM_NCPAINT: { if (!HasFullDecorations) @@ -540,6 +687,14 @@ namespace Avalonia.Win32 } } + public static uint GetWheelDelta(IntPtr wParam) => HIWORD(wParam); + public static uint ToPointerId(IntPtr wParam) => LOWORD(wParam); + public static uint LOWORD(IntPtr param) => (uint)param & 0xffff; + public static uint HIWORD(IntPtr param) => (uint)param >> 16; + + + public bool BelowWin8 => Win32Platform.WindowsVersion < PlatformConstants.Windows8; + private void UpdateInputMethod(IntPtr hkl) { // note: for non-ime language, also create it so that emoji panel tracks cursor From 44fda62a9b93cb0f3f6c0096cbfc654f378ab919 Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Sat, 22 Jan 2022 16:56:47 +0300 Subject: [PATCH 003/224] Add PointerInfo, TouchInfo and PenInfo --- src/Avalonia.Input/ITouchPadDevice.cs | 10 - src/Avalonia.Input/TouchPadDevice.cs | 488 ------------------ .../Interop/UnmanagedMethods.cs | 166 +++++- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 14 +- src/Windows/Avalonia.Win32/WindowImpl.cs | 2 + 5 files changed, 159 insertions(+), 521 deletions(-) delete mode 100644 src/Avalonia.Input/ITouchPadDevice.cs delete mode 100644 src/Avalonia.Input/TouchPadDevice.cs diff --git a/src/Avalonia.Input/ITouchPadDevice.cs b/src/Avalonia.Input/ITouchPadDevice.cs deleted file mode 100644 index ea6c57f948..0000000000 --- a/src/Avalonia.Input/ITouchPadDevice.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Avalonia.Input -{ - /// - /// Represents a touch pad device. - /// - public interface ITouchPadDevice : IPointerDevice - { - - } -} diff --git a/src/Avalonia.Input/TouchPadDevice.cs b/src/Avalonia.Input/TouchPadDevice.cs deleted file mode 100644 index fcd254f588..0000000000 --- a/src/Avalonia.Input/TouchPadDevice.cs +++ /dev/null @@ -1,488 +0,0 @@ -using System; -using System.Linq; -using System.Reactive.Linq; -using Avalonia.Input.Raw; -using Avalonia.Interactivity; -using Avalonia.Platform; -using Avalonia.VisualTree; - -namespace Avalonia.Input -{ - /// - /// Represents a touch pad device. - /// - public class TouchPadDevice : ITouchPadDevice, IDisposable - { - private int _clickCount; - private Rect _lastClickRect; - private ulong _lastClickTime; - - private readonly Pointer _pointer; - private bool _disposed; - private PixelPoint? _position; - - public TouchPadDevice(Pointer? pointer = null) - { - _pointer = pointer ?? new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true); - } - - /// - /// Gets the control that is currently capturing by the mouse, if any. - /// - /// - /// When an element captures the mouse, it receives mouse input whether the cursor is - /// within the control's bounds or not. To set the mouse capture, call the - /// method. - /// - [Obsolete("Use IPointer instead")] - public IInputElement? Captured => _pointer.Captured; - - /// - /// Gets the mouse position, in screen coordinates. - /// - [Obsolete("Use events instead")] - public PixelPoint Position - { - get => _position ?? new PixelPoint(-1, -1); - protected set => _position = value; - } - - /// - /// Captures mouse input to the specified control. - /// - /// The control. - /// - /// When an element captures the mouse, it receives mouse input whether the cursor is - /// within the control's bounds or not. The current mouse capture control is exposed - /// by the property. - /// - public void Capture(IInputElement? control) - { - _pointer.Capture(control); - } - - /// - /// Gets the mouse position relative to a control. - /// - /// The control. - /// The mouse position in the control's coordinates. - public Point GetPosition(IVisual relativeTo) - { - relativeTo = relativeTo ?? throw new ArgumentNullException(nameof(relativeTo)); - - if (relativeTo.VisualRoot == null) - { - throw new InvalidOperationException("Control is not attached to visual tree."); - } - -#pragma warning disable CS0618 // Type or member is obsolete - var rootPoint = relativeTo.VisualRoot.PointToClient(Position); -#pragma warning restore CS0618 // Type or member is obsolete - var transform = relativeTo.VisualRoot.TransformToVisual(relativeTo); - return rootPoint * transform!.Value; - } - - public void ProcessRawEvent(RawInputEventArgs e) - { - if (!e.Handled && e is RawPointerEventArgs margs) - ProcessRawEvent(margs); - } - - public void TopLevelClosed(IInputRoot root) - { - ClearPointerOver(this, 0, root, PointerPointProperties.None, KeyModifiers.None); - } - - public void SceneInvalidated(IInputRoot root, Rect rect) - { - // Pointer is outside of the target area - if (_position == null ) - { - if (root.PointerOverElement != null) - ClearPointerOver(this, 0, root, PointerPointProperties.None, KeyModifiers.None); - return; - } - - - var clientPoint = root.PointToClient(_position.Value); - - if (rect.Contains(clientPoint)) - { - if (_pointer.Captured == null) - { - SetPointerOver(this, 0 /* TODO: proper timestamp */, root, clientPoint, - PointerPointProperties.None, KeyModifiers.None); - } - else - { - SetPointerOver(this, 0 /* TODO: proper timestamp */, root, _pointer.Captured, - PointerPointProperties.None, KeyModifiers.None); - } - } - } - - int ButtonCount(PointerPointProperties props) - { - var rv = 0; - if (props.IsLeftButtonPressed) - rv++; - if (props.IsMiddleButtonPressed) - rv++; - if (props.IsRightButtonPressed) - rv++; - if (props.IsXButton1Pressed) - rv++; - if (props.IsXButton2Pressed) - rv++; - return rv; - } - - private void ProcessRawEvent(RawPointerEventArgs e) - { - e = e ?? throw new ArgumentNullException(nameof(e)); - - var mouse = (TouchPadDevice)e.Device; - if(mouse._disposed) - return; - - _position = e.Root.PointToScreen(e.Position); - var props = CreateProperties(e); - var keyModifiers = KeyModifiersUtils.ConvertToKey(e.InputModifiers); - switch (e.Type) - { - case RawPointerEventType.LeaveWindow: - LeaveWindow(mouse, e.Timestamp, e.Root, props, keyModifiers); - break; - case RawPointerEventType.LeftButtonDown: - case RawPointerEventType.RightButtonDown: - case RawPointerEventType.MiddleButtonDown: - case RawPointerEventType.XButton1Down: - case RawPointerEventType.XButton2Down: - if (ButtonCount(props) > 1) - e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); - else - e.Handled = MouseDown(mouse, e.Timestamp, e.Root, e.Position, - props, keyModifiers); - break; - case RawPointerEventType.LeftButtonUp: - case RawPointerEventType.RightButtonUp: - case RawPointerEventType.MiddleButtonUp: - case RawPointerEventType.XButton1Up: - case RawPointerEventType.XButton2Up: - if (ButtonCount(props) != 0) - e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); - else - e.Handled = MouseUp(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); - break; - case RawPointerEventType.Move: - e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); - break; - case RawPointerEventType.Wheel: - e.Handled = MouseWheel(mouse, e.Timestamp, e.Root, e.Position, props, ((RawMouseWheelEventArgs)e).Delta, keyModifiers); - break; - } - } - - private void LeaveWindow(ITouchPadDevice device, ulong timestamp, IInputRoot root, PointerPointProperties properties, - KeyModifiers inputModifiers) - { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - _position = null; - ClearPointerOver(this, timestamp, root, properties, inputModifiers); - } - - - PointerPointProperties CreateProperties(RawPointerEventArgs args) - { - - var kind = PointerUpdateKind.Other; - - if (args.Type == RawPointerEventType.LeftButtonDown) - kind = PointerUpdateKind.LeftButtonPressed; - if (args.Type == RawPointerEventType.MiddleButtonDown) - kind = PointerUpdateKind.MiddleButtonPressed; - if (args.Type == RawPointerEventType.RightButtonDown) - kind = PointerUpdateKind.RightButtonPressed; - if (args.Type == RawPointerEventType.XButton1Down) - kind = PointerUpdateKind.XButton1Pressed; - if (args.Type == RawPointerEventType.XButton2Down) - kind = PointerUpdateKind.XButton2Pressed; - if (args.Type == RawPointerEventType.LeftButtonUp) - kind = PointerUpdateKind.LeftButtonReleased; - if (args.Type == RawPointerEventType.MiddleButtonUp) - kind = PointerUpdateKind.MiddleButtonReleased; - if (args.Type == RawPointerEventType.RightButtonUp) - kind = PointerUpdateKind.RightButtonReleased; - if (args.Type == RawPointerEventType.XButton1Up) - kind = PointerUpdateKind.XButton1Released; - if (args.Type == RawPointerEventType.XButton2Up) - kind = PointerUpdateKind.XButton2Released; - - return new PointerPointProperties(args.InputModifiers, kind); - } - - private MouseButton _lastMouseDownButton; - private bool MouseDown(ITouchPadDevice device, ulong timestamp, IInputElement root, Point p, - PointerPointProperties properties, - KeyModifiers inputModifiers) - { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - var hit = HitTest(root, p); - - if (hit != null) - { - _pointer.Capture(hit); - var source = GetSource(hit); - if (source != null) - { - var settings = AvaloniaLocator.Current.GetService(); - var doubleClickTime = settings?.DoubleClickTime.TotalMilliseconds ?? 500; - var doubleClickSize = settings?.DoubleClickSize ?? new Size(4, 4); - - if (!_lastClickRect.Contains(p) || timestamp - _lastClickTime > doubleClickTime) - { - _clickCount = 0; - } - - ++_clickCount; - _lastClickTime = timestamp; - _lastClickRect = new Rect(p, new Size()) - .Inflate(new Thickness(doubleClickSize.Width / 2, doubleClickSize.Height / 2)); - _lastMouseDownButton = properties.PointerUpdateKind.GetMouseButton(); - var e = new PointerPressedEventArgs(source, _pointer, root, p, timestamp, properties, inputModifiers, _clickCount); - source.RaiseEvent(e); - return e.Handled; - } - } - - return false; - } - - private bool MouseMove(ITouchPadDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties properties, - KeyModifiers inputModifiers) - { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - IInputElement? source; - - if (_pointer.Captured == null) - { - source = SetPointerOver(this, timestamp, root, p, properties, inputModifiers); - } - else - { - SetPointerOver(this, timestamp, root, _pointer.Captured, properties, inputModifiers); - source = _pointer.Captured; - } - - if (source is object) - { - var e = new PointerEventArgs(InputElement.PointerMovedEvent, source, _pointer, root, - p, timestamp, properties, inputModifiers); - - source.RaiseEvent(e); - return e.Handled; - } - - return false; - } - - private bool MouseUp(ITouchPadDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties props, - KeyModifiers inputModifiers) - { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - var hit = HitTest(root, p); - var source = GetSource(hit); - - if (source is not null) - { - var e = new PointerReleasedEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers, - _lastMouseDownButton); - - source?.RaiseEvent(e); - _pointer.Capture(null); - return e.Handled; - } - - return false; - } - - private bool MouseWheel(ITouchPadDevice device, ulong timestamp, IInputRoot root, Point p, - PointerPointProperties props, - Vector delta, KeyModifiers inputModifiers) - { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - var hit = HitTest(root, p); - var source = GetSource(hit); - - if (source is not null) - { - var e = new PointerWheelEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers, delta); - - source?.RaiseEvent(e); - return e.Handled; - } - - return false; - } - - private IInteractive? GetSource(IVisual? hit) - { - if (hit is null) - return null; - - return _pointer.Captured ?? - (hit as IInteractive) ?? - hit.GetSelfAndVisualAncestors().OfType().FirstOrDefault(); - } - - private IInputElement? HitTest(IInputElement root, Point p) - { - root = root ?? throw new ArgumentNullException(nameof(root)); - - return _pointer.Captured ?? root.InputHitTest(p); - } - - PointerEventArgs CreateSimpleEvent(RoutedEvent ev, ulong timestamp, IInteractive? source, - PointerPointProperties properties, - KeyModifiers inputModifiers) - { - return new PointerEventArgs(ev, source, _pointer, null, default, - timestamp, properties, inputModifiers); - } - - private void ClearPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, - PointerPointProperties properties, - KeyModifiers inputModifiers) - { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - var element = root.PointerOverElement; - var e = CreateSimpleEvent(InputElement.PointerLeaveEvent, timestamp, element, properties, inputModifiers); - - if (element!=null && !element.IsAttachedToVisualTree) - { - // element has been removed from visual tree so do top down cleanup - if (root.IsPointerOver) - ClearChildrenPointerOver(e, root,true); - } - while (element != null) - { - e.Source = element; - e.Handled = false; - element.RaiseEvent(e); - element = (IInputElement?)element.VisualParent; - } - - root.PointerOverElement = null; - } - - private void ClearChildrenPointerOver(PointerEventArgs e, IInputElement element,bool clearRoot) - { - foreach (IInputElement el in element.VisualChildren) - { - if (el.IsPointerOver) - { - ClearChildrenPointerOver(e, el, true); - break; - } - } - if(clearRoot) - { - e.Source = element; - e.Handled = false; - element.RaiseEvent(e); - } - } - - private IInputElement? SetPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, Point p, - PointerPointProperties properties, - KeyModifiers inputModifiers) - { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - var element = root.InputHitTest(p); - - if (element != root.PointerOverElement) - { - if (element != null) - { - SetPointerOver(device, timestamp, root, element, properties, inputModifiers); - } - else - { - ClearPointerOver(device, timestamp, root, properties, inputModifiers); - } - } - - return element; - } - - private void SetPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, IInputElement element, - PointerPointProperties properties, - KeyModifiers inputModifiers) - { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - element = element ?? throw new ArgumentNullException(nameof(element)); - - IInputElement? branch = null; - - IInputElement? el = element; - - while (el != null) - { - if (el.IsPointerOver) - { - branch = el; - break; - } - el = (IInputElement?)el.VisualParent; - } - - el = root.PointerOverElement; - - var e = CreateSimpleEvent(InputElement.PointerLeaveEvent, timestamp, el, properties, inputModifiers); - if (el!=null && branch!=null && !el.IsAttachedToVisualTree) - { - ClearChildrenPointerOver(e,branch,false); - } - - while (el != null && el != branch) - { - e.Source = el; - e.Handled = false; - el.RaiseEvent(e); - el = (IInputElement?)el.VisualParent; - } - - el = root.PointerOverElement = element; - e.RoutedEvent = InputElement.PointerEnterEvent; - - while (el != null && el != branch) - { - e.Source = el; - e.Handled = false; - el.RaiseEvent(e); - el = (IInputElement?)el.VisualParent; - } - } - - public void Dispose() - { - _disposed = true; - _pointer?.Dispose(); - } - } -} diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index db81e8197f..5aecd8e8f0 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -225,20 +225,17 @@ namespace Avalonia.Win32.Interop [Flags] public enum ModifierKeys { - MK_CONTROL = 0x0008, + MK_NONE = 0x0001, MK_LBUTTON = 0x0001, - - MK_MBUTTON = 0x0010, - MK_RBUTTON = 0x0002, - MK_SHIFT = 0x0004, - - MK_ALT = 0x0020, + MK_SHIFT = 0x0004, + MK_CONTROL = 0x0008, + MK_MBUTTON = 0x0010, + MK_ALT = 0x0020, MK_XBUTTON1 = 0x0020, - MK_XBUTTON2 = 0x0040 } @@ -531,14 +528,14 @@ namespace Avalonia.Win32.Interop PDC_MODE_ASPECTRATIOPRESERVED = 0x800 } - public enum InputType + public enum PointerInputType { - NONE = 0x00000000, - POINTER = 0x00000001, - TOUCH = 0x00000002, - PEN = 0x00000003, - MOUSE = 0x00000004, - TOUCHPAD = 0x00000005 + PT_NONE = 0x00000000, + PT_POINTER = 0x00000001, + PT_TOUCH = 0x00000002, + PT_PEN = 0x00000003, + PT_MOUSE = 0x00000004, + PT_TOUCHPAD = 0x00000005 } public enum WindowsMessage : uint @@ -882,6 +879,134 @@ namespace Avalonia.Win32.Interop SCF_ISSECURE = 0x00000001, } + [Flags] + public enum PointerFlags + { + POINTER_FLAG_NONE = 0x00000000, + POINTER_FLAG_NEW = 0x00000001, + POINTER_FLAG_INRANGE = 0x00000002, + POINTER_FLAG_INCONTACT = 0x00000004, + POINTER_FLAG_FIRSTBUTTON = 0x00000010, + POINTER_FLAG_SECONDBUTTON = 0x00000020, + POINTER_FLAG_THIRDBUTTON = 0x00000040, + POINTER_FLAG_FOURTHBUTTON = 0x00000080, + POINTER_FLAG_FIFTHBUTTON = 0x00000100, + POINTER_FLAG_PRIMARY = 0x00002000, + POINTER_FLAG_CONFIDENCE = 0x00000400, + POINTER_FLAG_CANCELED = 0x00000800, + POINTER_FLAG_DOWN = 0x00010000, + POINTER_FLAG_UPDATE = 0x00020000, + POINTER_FLAG_UP = 0x00040000, + POINTER_FLAG_WHEEL = 0x00080000, + POINTER_FLAG_HWHEEL = 0x00100000, + POINTER_FLAG_CAPTURECHANGED = 0x00200000, + POINTER_FLAG_HASTRANSFORM = 0x00400000 + } + + public enum PointerButtonChangeType : ulong + { + POINTER_CHANGE_NONE, + POINTER_CHANGE_FIRSTBUTTON_DOWN, + POINTER_CHANGE_FIRSTBUTTON_UP, + POINTER_CHANGE_SECONDBUTTON_DOWN, + POINTER_CHANGE_SECONDBUTTON_UP, + POINTER_CHANGE_THIRDBUTTON_DOWN, + POINTER_CHANGE_THIRDBUTTON_UP, + POINTER_CHANGE_FOURTHBUTTON_DOWN, + POINTER_CHANGE_FOURTHBUTTON_UP, + POINTER_CHANGE_FIFTHBUTTON_DOWN, + POINTER_CHANGE_FIFTHBUTTON_UP + } + + [Flags] + public enum PenFlags + { + PEN_FLAGS_NONE = 0x00000000, + PEN_FLAGS_BARREL = 0x00000001, + PEN_FLAGS_INVERTED = 0x00000002, + PEN_FLAGS_ERASER = 0x00000004, + } + + [Flags] + public enum PenMask + { + PEN_MASK_NONE = 0x00000000, + PEN_MASK_PRESSURE = 0x00000001, + PEN_MASK_ROTATION = 0x00000002, + PEN_MASK_TILT_X = 0x00000004, + PEN_MASK_TILT_Y = 0x00000008 + } + + [Flags] + public enum TouchFlags + { + TOUCH_FLAG_NONE = 0x00000000 + } + + [Flags] + public enum TouchMask + { + TOUCH_MASK_NONE = 0x00000000, + TOUCH_MASK_CONTACTAREA = 0x00000001, + TOUCH_MASK_ORIENTATION = 0x00000002, + TOUCH_MASK_PRESSURE = 0x00000004, + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct POINTER_TOUCH_INFO + { + public POINTER_INFO pointerInfo; + public TouchFlags touchFlags; + public TouchMask touchMask; + public int rcContactLeft; + public int rcContactTop; + public int rcContactRight; + public int rcContactBottom; + public int rcContactRawLeft; + public int rcContactRawTop; + public int rcContactRawRight; + public int rcContactRawBottom; + public uint orientation; + public uint pressure; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct POINTER_PEN_INFO + { + public POINTER_INFO pointerInfo; + public PenFlags penFlags; + public PenMask penMask; + public uint pressure; + public uint rotation; + public int tiltX; + public int tiltY; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct POINTER_INFO + { + public PointerInputType pointerType; + public uint pointerId; + public uint frameId; + public PointerFlags pointerFlags; + public IntPtr sourceDevice; + public IntPtr hwndTarget; + public int ptPixelLocationX; + public int ptPixelLocationY; + public int ptHimetricLocationX; + public int ptHimetricLocationY; + public int ptPixelLocationRawX; + public int ptPixelLocationRawY; + public int ptHimetricLocationRawX; + public int ptHimetricLocationRawY; + public uint dwTime; + public uint historyCount; + public int inputData; + public ModifierKeys dwKeyStates; + public UInt64 PerformanceCount; + public PointerButtonChangeType ButtonChangeType; + } + [StructLayout(LayoutKind.Sequential)] public struct RGBQUAD { @@ -956,7 +1081,16 @@ namespace Avalonia.Win32.Interop public static extern bool GetPointerCursorId(uint pointerID, out uint cursorId); [DllImport("user32.dll", SetLastError = true)] - public static extern bool GetPointerType(uint pointerID, out InputType pointerType); + public static extern bool GetPointerType(uint pointerID, out PointerInputType pointerType); + + [DllImport("User32.dll", SetLastError = true)] + public static extern bool GetPointerInfo(uint pointerID, out POINTER_INFO pointerInfo); + + [DllImport("User32.dll", SetLastError = true)] + public static extern bool GetPointerPenInfo(uint pointerID, out POINTER_PEN_INFO penInfo); + + [DllImport("User32.dll", SetLastError = true)] + public static extern bool GetPointerTouchInfo(uint pointerID, out POINTER_TOUCH_INFO touchInfo); [DllImport("user32.dll", SetLastError = true)] public static extern void GetUnpredictedMessagePos(); diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index e16901af2c..8a0654a9ba 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -427,17 +427,17 @@ namespace Avalonia.Win32 { var pointerId = ToPointerId(wParam); GetPointerType(pointerId, out var type); - IInputDevice device = _mouseDevice; + IInputDevice device; switch (type) { - case InputType.PEN: - + case PointerInputType.PT_PEN: + device = _penDevice; break; - case InputType.TOUCH: + case PointerInputType.PT_TOUCH: device = _touchDevice; break; - case InputType.TOUCHPAD: - + default: + device = _mouseDevice; break; } @@ -445,7 +445,7 @@ namespace Avalonia.Win32 var delta = GetWheelDelta(wParam); var point = PointFromLParam(lParam); e = new RawMouseWheelEventArgs( - _mouseDevice, + device, timestamp, _owner, PointToClient(point), diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index e4f5268285..8f6a7e9741 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -64,6 +64,7 @@ namespace Avalonia.Win32 private const WindowStyles WindowStateMask = (WindowStyles.WS_MAXIMIZE | WindowStyles.WS_MINIMIZE); private readonly TouchDevice _touchDevice; private readonly MouseDevice _mouseDevice; + private readonly PenDevice _penDevice; private readonly ManagedDeferredRendererLock _rendererLock; private readonly FramebufferManager _framebuffer; private readonly IGlPlatformSurface _gl; @@ -96,6 +97,7 @@ namespace Avalonia.Win32 { _touchDevice = new TouchDevice(); _mouseDevice = new WindowsMouseDevice(); + _penDevice = new PenDevice(); #if USE_MANAGED_DRAG _managedDrag = new ManagedWindowResizeDragHelper(this, capture => From 1192e86d90f0d6f4df29acc442a21031e7fb0c3e Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Sat, 22 Jan 2022 18:39:36 +0300 Subject: [PATCH 004/224] Investigate all the messages and note short comments about them and provide a link to a documentation. In case anyone will need this in the future --- .../Interop/UnmanagedMethods.cs | 2 +- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 139 ++++++++++++------ src/Windows/Avalonia.Win32/WindowImpl.cs | 2 +- 3 files changed, 99 insertions(+), 44 deletions(-) diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index 5aecd8e8f0..93457f7bbd 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -730,7 +730,7 @@ namespace Avalonia.Win32.Interop WM_TOUCHHITTESTING = 0x024D, WM_POINTERWHEEL = 0x024E, WM_POINTERHWHEEL = 0x024F, - WM_POINTERHITTEST = 0x0250, + DM_POINTERHITTEST = 0x0250, WM_IME_SETCONTEXT = 0x0281, WM_IME_NOTIFY = 0x0282, diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 8a0654a9ba..3587eddc85 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -227,10 +227,6 @@ namespace Avalonia.Win32 } // Mouse capture is lost case WindowsMessage.WM_CANCELMODE: - if (BelowWin8) - { - break; - } _mouseDevice.Capture(null); break; @@ -263,7 +259,8 @@ namespace Avalonia.Win32 timestamp, _owner, RawPointerEventType.Move, - DipFromLParam(lParam), GetMouseModifiers(wParam)); + DipFromLParam(lParam), + GetMouseModifiers(wParam)); break; } @@ -279,7 +276,8 @@ namespace Avalonia.Win32 timestamp, _owner, PointToClient(PointFromLParam(lParam)), - new Vector(0, (ToInt32(wParam) >> 16) / wheelDelta), GetMouseModifiers(wParam)); + new Vector(0, (ToInt32(wParam) >> 16) / wheelDelta), + GetMouseModifiers(wParam)); break; } @@ -294,7 +292,8 @@ namespace Avalonia.Win32 timestamp, _owner, PointToClient(PointFromLParam(lParam)), - new Vector(-(ToInt32(wParam) >> 16) / wheelDelta, 0), GetMouseModifiers(wParam)); + new Vector(-(ToInt32(wParam) >> 16) / wheelDelta, 0), + GetMouseModifiers(wParam)); break; } @@ -310,7 +309,8 @@ namespace Avalonia.Win32 timestamp, _owner, RawPointerEventType.LeaveWindow, - new Point(-1, -1), WindowsKeyboardDevice.Instance.Modifiers); + new Point(-1, -1), + WindowsKeyboardDevice.Instance.Modifiers); break; } @@ -386,16 +386,24 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERDEVICECHANGE: + { + //notifies about changes in the settings of a monitor that has a digitizer attached to it. + //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-pointerdevicechange + break; + } case WindowsMessage.WM_POINTERDEVICEINRANGE: case WindowsMessage.WM_POINTERDEVICEOUTOFRANGE: { - + //notifies about proximity of pointer device to the digitizer. + //contains pointer id and proximity. + //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-pointerdeviceinrange break; } case WindowsMessage.WM_NCPOINTERUPDATE: case WindowsMessage.WM_NCPOINTERDOWN: case WindowsMessage.WM_NCPOINTERUP: { + //NC stands for non-client area - window header and window border break; } @@ -407,60 +415,79 @@ namespace Avalonia.Win32 break; } case WindowsMessage.WM_POINTERENTER: - case WindowsMessage.WM_POINTERLEAVE: { + //this is not handled by WM_MOUSEENTER so I think there is no need to handle this too. + //but we can detect a new pointer by this message and calling IS_POINTER_NEW_WPARAM + //note: by using a pen there can be a pointer leave or enter inside a window coords + //when you are just lift up the pen above the display break; } - case WindowsMessage.WM_POINTERACTIVATE: - case WindowsMessage.WM_POINTERCAPTURECHANGED: + case WindowsMessage.WM_POINTERLEAVE: { + GetDeviceInfo(wParam, out var device, out var info); + var point = PointToClient(PointFromLParam(lParam)); + e = new RawPointerEventArgs( + device, + timestamp, + _owner, + RawPointerEventType.LeaveWindow, + point, + WindowsKeyboardDevice.Instance.Modifiers); break; } - case WindowsMessage.WM_TOUCHHITTESTING: + case WindowsMessage.WM_POINTERACTIVATE: { - + //occurs when a pointer activates an inactive window. + //we should handle this and return PA_ACTIVATE or PA_NOACTIVATE + //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-pointeractivate break; } + case WindowsMessage.WM_POINTERCAPTURECHANGED: + { + _mouseDevice.Capture(null); + return IntPtr.Zero; + } case WindowsMessage.WM_POINTERWHEEL: { - var pointerId = ToPointerId(wParam); - GetPointerType(pointerId, out var type); - IInputDevice device; - switch (type) - { - case PointerInputType.PT_PEN: - device = _penDevice; - break; - case PointerInputType.PT_TOUCH: - device = _touchDevice; - break; - default: - device = _mouseDevice; - break; - } - + GetDeviceInfo(wParam, out var device, out var info); - var delta = GetWheelDelta(wParam); - var point = PointFromLParam(lParam); - e = new RawMouseWheelEventArgs( - device, - timestamp, - _owner, - PointToClient(point), - new Vector(0, delta / wheelDelta), - GetMouseModifiers(wParam)); + var point = PointToClient(PointFromLParam(lParam)); + var modifiers = GetInputModifiers(info.dwKeyStates); + var delta = new Vector(0, GetWheelDelta(wParam) / wheelDelta); + e = new RawMouseWheelEventArgs(device, timestamp, _owner, point, delta, modifiers); break; } case WindowsMessage.WM_POINTERHWHEEL: { + GetDeviceInfo(wParam, out var device, out var info); + var point = PointToClient(PointFromLParam(lParam)); + var modifiers = GetInputModifiers(info.dwKeyStates); + var delta = new Vector(GetWheelDelta(wParam) / wheelDelta, 0); + e = new RawMouseWheelEventArgs(device, timestamp, _owner, point, delta, modifiers); break; } - case WindowsMessage.WM_POINTERHITTEST: + case WindowsMessage.DM_POINTERHITTEST: { - + //DM stands for direct manipulation. + //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/directmanipulation/direct-manipulation-portal + break; + } + case WindowsMessage.WM_TOUCHHITTESTING: + { + //This is to determine the most probable touch target. + //provides an input bounding box and receives hit proximity + //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-touchhittesting + //https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-touch_hit_testing_input + break; + } + case WindowsMessage.WM_PARENTNOTIFY: + { + //This message is sent in a dialog scenarios. Contains mouse position in an old-way, + //but listed in the wm_pointer reference + //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-parentnotify break; } @@ -687,13 +714,36 @@ namespace Avalonia.Win32 } } + private void GetDeviceInfo(IntPtr wParam, out IInputDevice device, out POINTER_INFO info) + { + var pointerId = ToPointerId(wParam); + GetPointerType(pointerId, out var type);//ToDo we can cache this and invalidate in WM_POINTERDEVICECHANGE + switch (type) + { + case PointerInputType.PT_PEN: + device = _penDevice; + GetPointerPenInfo(pointerId, out var penInfo); + info = penInfo.pointerInfo; + break; + case PointerInputType.PT_TOUCH: + device = _touchDevice; + GetPointerTouchInfo(pointerId, out var touchInfo); + info = touchInfo.pointerInfo; + break; + default: + device = _mouseDevice; + GetPointerInfo(pointerId, out info); + break; + } + } + public static uint GetWheelDelta(IntPtr wParam) => HIWORD(wParam); public static uint ToPointerId(IntPtr wParam) => LOWORD(wParam); public static uint LOWORD(IntPtr param) => (uint)param & 0xffff; public static uint HIWORD(IntPtr param) => (uint)param >> 16; - public bool BelowWin8 => Win32Platform.WindowsVersion < PlatformConstants.Windows8; + public readonly bool BelowWin8 = Win32Platform.WindowsVersion < PlatformConstants.Windows8; private void UpdateInputMethod(IntPtr hkl) { @@ -747,6 +797,11 @@ namespace Avalonia.Win32 private static RawInputModifiers GetMouseModifiers(IntPtr wParam) { var keys = (ModifierKeys)ToInt32(wParam); + return GetInputModifiers(keys); + } + + private static RawInputModifiers GetInputModifiers(ModifierKeys keys) + { var modifiers = WindowsKeyboardDevice.Instance.Modifiers; if (keys.HasAllFlags(ModifierKeys.MK_LBUTTON)) diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index 8f6a7e9741..0f99252d4d 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -76,7 +76,7 @@ namespace Avalonia.Win32 private bool _multitouch; private IInputRoot _owner; private WindowProperties _windowProperties; - private bool _trackingMouse; + private bool _trackingMouse;//ToDo - there is something missed. Needs investigation @Steven Kirk private bool _topmost; private double _scaling = 1; private WindowState _showWindowState; From 35a55c9a56dca3ccc957a559e0b50e1aa48fdab0 Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Sat, 22 Jan 2022 21:37:54 +0300 Subject: [PATCH 005/224] Enable WM_Pointer for mouse events, handle up/down events, some fixes --- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 73 +++++++++++++------ src/Windows/Avalonia.Win32/WindowImpl.cs | 5 ++ 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 3587eddc85..5e31bba925 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -166,7 +166,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MBUTTONDOWN: case WindowsMessage.WM_XBUTTONDOWN: { - if (BelowWin8) + if (Win8Plus) { break; } @@ -199,7 +199,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MBUTTONUP: case WindowsMessage.WM_XBUTTONUP: { - if (BelowWin8) + if (Win8Plus) { break; } @@ -232,7 +232,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MOUSEMOVE: { - if (BelowWin8) + if (Win8Plus) { break; } @@ -267,7 +267,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MOUSEWHEEL: { - if (BelowWin8) + if (Win8Plus) { break; } @@ -283,7 +283,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MOUSEHWHEEL: { - if (BelowWin8) + if (Win8Plus) { break; } @@ -299,7 +299,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MOUSELEAVE: { - if (BelowWin8) + if (Win8Plus) { break; } @@ -319,7 +319,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_NCMBUTTONDOWN: case WindowsMessage.WM_NCXBUTTONDOWN: { - if (BelowWin8) + if (Win8Plus) { break; } @@ -343,7 +343,7 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_TOUCH: { - if (BelowWin8) + if (Win8Plus) { break; } @@ -400,18 +400,53 @@ namespace Avalonia.Win32 break; } case WindowsMessage.WM_NCPOINTERUPDATE: - case WindowsMessage.WM_NCPOINTERDOWN: - case WindowsMessage.WM_NCPOINTERUP: { //NC stands for non-client area - window header and window border - + //As I found above in an old message handling - we dont need to handle NC pointer move/updates. + //All we need is pointer down and up. So this is skipped for now. break; } - case WindowsMessage.WM_POINTERUPDATE: + case WindowsMessage.WM_NCPOINTERDOWN: + case WindowsMessage.WM_NCPOINTERUP: case WindowsMessage.WM_POINTERDOWN: case WindowsMessage.WM_POINTERUP: { + GetDeviceInfo(wParam, out var device, out var info); + var point = PointToClient(PointFromLParam(lParam)); + var modifiers = GetInputModifiers(info.dwKeyStates); + var eventType = info.ButtonChangeType switch + { + PointerButtonChangeType.POINTER_CHANGE_FIRSTBUTTON_DOWN => RawPointerEventType.LeftButtonDown, + PointerButtonChangeType.POINTER_CHANGE_SECONDBUTTON_DOWN => RawPointerEventType.RightButtonDown, + PointerButtonChangeType.POINTER_CHANGE_THIRDBUTTON_DOWN => RawPointerEventType.MiddleButtonDown, + PointerButtonChangeType.POINTER_CHANGE_FOURTHBUTTON_DOWN => RawPointerEventType.XButton1Down, + PointerButtonChangeType.POINTER_CHANGE_FIFTHBUTTON_DOWN => RawPointerEventType.XButton2Down, + + PointerButtonChangeType.POINTER_CHANGE_FIRSTBUTTON_UP => RawPointerEventType.LeftButtonUp, + PointerButtonChangeType.POINTER_CHANGE_SECONDBUTTON_UP => RawPointerEventType.RightButtonUp, + PointerButtonChangeType.POINTER_CHANGE_THIRDBUTTON_UP => RawPointerEventType.MiddleButtonUp, + PointerButtonChangeType.POINTER_CHANGE_FOURTHBUTTON_UP => RawPointerEventType.XButton1Up, + PointerButtonChangeType.POINTER_CHANGE_FIFTHBUTTON_UP => RawPointerEventType.XButton2Up, + }; + if (eventType == RawPointerEventType.NonClientLeftButtonDown && + (WindowsMessage)msg == WindowsMessage.WM_NCPOINTERDOWN) + { + eventType = RawPointerEventType.NonClientLeftButtonDown; + } + e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers); + break; + } + case WindowsMessage.WM_POINTERUPDATE: + { + if (ShouldIgnoreTouchEmulatedMessage()) + { + break; + } + GetDeviceInfo(wParam, out var device, out var info); + var point = PointToClient(PointFromLParam(lParam)); + var modifiers = GetInputModifiers(info.dwKeyStates); + e = new RawPointerEventArgs(device, timestamp, _owner, RawPointerEventType.Move, point, modifiers); break; } case WindowsMessage.WM_POINTERENTER: @@ -455,7 +490,7 @@ namespace Avalonia.Win32 var point = PointToClient(PointFromLParam(lParam)); var modifiers = GetInputModifiers(info.dwKeyStates); - var delta = new Vector(0, GetWheelDelta(wParam) / wheelDelta); + var delta = new Vector(0, (ToInt32(wParam) >> 16) / wheelDelta); e = new RawMouseWheelEventArgs(device, timestamp, _owner, point, delta, modifiers); break; } @@ -465,7 +500,7 @@ namespace Avalonia.Win32 var point = PointToClient(PointFromLParam(lParam)); var modifiers = GetInputModifiers(info.dwKeyStates); - var delta = new Vector(GetWheelDelta(wParam) / wheelDelta, 0); + var delta = new Vector((ToInt32(wParam) >> 16) / wheelDelta, 0); e = new RawMouseWheelEventArgs(device, timestamp, _owner, point, delta, modifiers); break; } @@ -716,7 +751,7 @@ namespace Avalonia.Win32 private void GetDeviceInfo(IntPtr wParam, out IInputDevice device, out POINTER_INFO info) { - var pointerId = ToPointerId(wParam); + var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); GetPointerType(pointerId, out var type);//ToDo we can cache this and invalidate in WM_POINTERDEVICECHANGE switch (type) { @@ -737,13 +772,7 @@ namespace Avalonia.Win32 } } - public static uint GetWheelDelta(IntPtr wParam) => HIWORD(wParam); - public static uint ToPointerId(IntPtr wParam) => LOWORD(wParam); - public static uint LOWORD(IntPtr param) => (uint)param & 0xffff; - public static uint HIWORD(IntPtr param) => (uint)param >> 16; - - - public readonly bool BelowWin8 = Win32Platform.WindowsVersion < PlatformConstants.Windows8; + public readonly bool Win8Plus = Win32Platform.WindowsVersion >= PlatformConstants.Windows8; private void UpdateInputMethod(IntPtr hkl) { diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index 0f99252d4d..0ad198e991 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -126,6 +126,11 @@ namespace Avalonia.Win32 egl.Display is AngleWin32EglDisplay angleDisplay && angleDisplay.PlatformApi == AngleOptions.PlatformApi.DirectX11; + if (Win8Plus && !IsMouseInPointerEnabled()) + { + EnableMouseInPointer(true); + } + CreateWindow(); _framebuffer = new FramebufferManager(_hwnd); UpdateInputMethod(GetKeyboardLayout(0)); From 4cea62fd13701931ba7aedbb2b2f6fce9f9477a6 Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Sat, 22 Jan 2022 23:34:43 +0300 Subject: [PATCH 006/224] PenDevice tuning, Add pen properties to the PointerPointProperties --- src/Avalonia.Input/IKeyboardDevice.cs | 2 + src/Avalonia.Input/IPointer.cs | 3 +- src/Avalonia.Input/PenDevice.cs | 134 +++--------------- src/Avalonia.Input/PointerPoint.cs | 30 +++- src/Avalonia.Input/Raw/RawPointerEventArgs.cs | 8 +- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 64 +++------ 6 files changed, 72 insertions(+), 169 deletions(-) diff --git a/src/Avalonia.Input/IKeyboardDevice.cs b/src/Avalonia.Input/IKeyboardDevice.cs index 9506dc36fb..b3ea7c5f4f 100644 --- a/src/Avalonia.Input/IKeyboardDevice.cs +++ b/src/Avalonia.Input/IKeyboardDevice.cs @@ -47,6 +47,8 @@ namespace Avalonia.Input MiddleMouseButton = 64, XButton1MouseButton = 128, XButton2MouseButton = 256, + BarrelPenButton = 512, + PenEraser = 1024, KeyboardMask = Alt | Control | Shift | Meta } diff --git a/src/Avalonia.Input/IPointer.cs b/src/Avalonia.Input/IPointer.cs index 7af48cef82..361f3ac370 100644 --- a/src/Avalonia.Input/IPointer.cs +++ b/src/Avalonia.Input/IPointer.cs @@ -13,6 +13,7 @@ namespace Avalonia.Input public enum PointerType { Mouse, - Touch + Touch, + Pen } } diff --git a/src/Avalonia.Input/PenDevice.cs b/src/Avalonia.Input/PenDevice.cs index dd3d60ebb9..3c0f3c7709 100644 --- a/src/Avalonia.Input/PenDevice.cs +++ b/src/Avalonia.Input/PenDevice.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Reactive.Linq; using Avalonia.Input.Raw; using Avalonia.Interactivity; -using Avalonia.Platform; using Avalonia.VisualTree; namespace Avalonia.Input @@ -13,17 +12,13 @@ namespace Avalonia.Input /// public class PenDevice : IPenDevice, IDisposable { - private int _clickCount; - private Rect _lastClickRect; - private ulong _lastClickTime; - private readonly Pointer _pointer; private bool _disposed; private PixelPoint? _position; public PenDevice(Pointer? pointer = null) { - _pointer = pointer ?? new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true); + _pointer = pointer ?? new Pointer(Pointer.GetNextFreeId(), PointerType.Pen, true); } /// @@ -37,16 +32,6 @@ namespace Avalonia.Input [Obsolete("Use IPointer instead")] public IInputElement? Captured => _pointer.Captured; - /// - /// Gets the mouse position, in screen coordinates. - /// - [Obsolete("Use events instead")] - public PixelPoint Position - { - get => _position ?? new PixelPoint(-1, -1); - protected set => _position = value; - } - /// /// Captures mouse input to the specified control. /// @@ -76,7 +61,7 @@ namespace Avalonia.Input } #pragma warning disable CS0618 // Type or member is obsolete - var rootPoint = relativeTo.VisualRoot.PointToClient(Position); + var rootPoint = relativeTo.VisualRoot.PointToClient(_position ?? new PixelPoint(-1, -1)); #pragma warning restore CS0618 // Type or member is obsolete var transform = relativeTo.VisualRoot.TransformToVisual(relativeTo); return rootPoint * transform!.Value; @@ -120,29 +105,13 @@ namespace Avalonia.Input } } } - - int ButtonCount(PointerPointProperties props) - { - var rv = 0; - if (props.IsLeftButtonPressed) - rv++; - if (props.IsMiddleButtonPressed) - rv++; - if (props.IsRightButtonPressed) - rv++; - if (props.IsXButton1Pressed) - rv++; - if (props.IsXButton2Pressed) - rv++; - return rv; - } private void ProcessRawEvent(RawPointerEventArgs e) { e = e ?? throw new ArgumentNullException(nameof(e)); - var mouse = (PenDevice)e.Device; - if(mouse._disposed) + var pen = (PenDevice)e.Device; + if(pen._disposed) return; _position = e.Root.PointToScreen(e.Position); @@ -151,34 +120,16 @@ namespace Avalonia.Input switch (e.Type) { case RawPointerEventType.LeaveWindow: - LeaveWindow(mouse, e.Timestamp, e.Root, props, keyModifiers); + LeaveWindow(pen, e.Timestamp, e.Root, props, keyModifiers); break; - case RawPointerEventType.LeftButtonDown: - case RawPointerEventType.RightButtonDown: - case RawPointerEventType.MiddleButtonDown: - case RawPointerEventType.XButton1Down: - case RawPointerEventType.XButton2Down: - if (ButtonCount(props) > 1) - e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); - else - e.Handled = MouseDown(mouse, e.Timestamp, e.Root, e.Position, - props, keyModifiers); + case RawPointerEventType.PenBegin: + e.Handled = PenDown(pen, e.Timestamp, e.Root, e.Position, props, keyModifiers); break; - case RawPointerEventType.LeftButtonUp: - case RawPointerEventType.RightButtonUp: - case RawPointerEventType.MiddleButtonUp: - case RawPointerEventType.XButton1Up: - case RawPointerEventType.XButton2Up: - if (ButtonCount(props) != 0) - e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); - else - e.Handled = MouseUp(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); + case RawPointerEventType.PenEnd: + e.Handled = PenUp(pen, e.Timestamp, e.Root, e.Position, props, keyModifiers); break; - case RawPointerEventType.Move: - e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, keyModifiers); - break; - case RawPointerEventType.Wheel: - e.Handled = MouseWheel(mouse, e.Timestamp, e.Root, e.Position, props, ((RawMouseWheelEventArgs)e).Delta, keyModifiers); + case RawPointerEventType.PenUpdate: + e.Handled = PenMove(pen, e.Timestamp, e.Root, e.Position, props, keyModifiers); break; } } @@ -196,35 +147,18 @@ namespace Avalonia.Input PointerPointProperties CreateProperties(RawPointerEventArgs args) { - var kind = PointerUpdateKind.Other; - if (args.Type == RawPointerEventType.LeftButtonDown) + if (args.Type == RawPointerEventType.PenBegin) kind = PointerUpdateKind.LeftButtonPressed; - if (args.Type == RawPointerEventType.MiddleButtonDown) - kind = PointerUpdateKind.MiddleButtonPressed; - if (args.Type == RawPointerEventType.RightButtonDown) - kind = PointerUpdateKind.RightButtonPressed; - if (args.Type == RawPointerEventType.XButton1Down) - kind = PointerUpdateKind.XButton1Pressed; - if (args.Type == RawPointerEventType.XButton2Down) - kind = PointerUpdateKind.XButton2Pressed; - if (args.Type == RawPointerEventType.LeftButtonUp) + if (args.Type == RawPointerEventType.PenEnd) kind = PointerUpdateKind.LeftButtonReleased; - if (args.Type == RawPointerEventType.MiddleButtonUp) - kind = PointerUpdateKind.MiddleButtonReleased; - if (args.Type == RawPointerEventType.RightButtonUp) - kind = PointerUpdateKind.RightButtonReleased; - if (args.Type == RawPointerEventType.XButton1Up) - kind = PointerUpdateKind.XButton1Released; - if (args.Type == RawPointerEventType.XButton2Up) - kind = PointerUpdateKind.XButton2Released; return new PointerPointProperties(args.InputModifiers, kind); } private MouseButton _lastMouseDownButton; - private bool MouseDown(IPenDevice device, ulong timestamp, IInputElement root, Point p, + private bool PenDown(IPenDevice device, ulong timestamp, IInputElement root, Point p, PointerPointProperties properties, KeyModifiers inputModifiers) { @@ -239,21 +173,8 @@ namespace Avalonia.Input var source = GetSource(hit); if (source != null) { - var settings = AvaloniaLocator.Current.GetService(); - var doubleClickTime = settings?.DoubleClickTime.TotalMilliseconds ?? 500; - var doubleClickSize = settings?.DoubleClickSize ?? new Size(4, 4); - - if (!_lastClickRect.Contains(p) || timestamp - _lastClickTime > doubleClickTime) - { - _clickCount = 0; - } - - ++_clickCount; - _lastClickTime = timestamp; - _lastClickRect = new Rect(p, new Size()) - .Inflate(new Thickness(doubleClickSize.Width / 2, doubleClickSize.Height / 2)); _lastMouseDownButton = properties.PointerUpdateKind.GetMouseButton(); - var e = new PointerPressedEventArgs(source, _pointer, root, p, timestamp, properties, inputModifiers, _clickCount); + var e = new PointerPressedEventArgs(source, _pointer, root, p, timestamp, properties, inputModifiers, 1); source.RaiseEvent(e); return e.Handled; } @@ -262,7 +183,7 @@ namespace Avalonia.Input return false; } - private bool MouseMove(IPenDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties properties, + private bool PenMove(IPenDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties properties, KeyModifiers inputModifiers) { device = device ?? throw new ArgumentNullException(nameof(device)); @@ -292,7 +213,7 @@ namespace Avalonia.Input return false; } - private bool MouseUp(IPenDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties props, + private bool PenUp(IPenDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties props, KeyModifiers inputModifiers) { device = device ?? throw new ArgumentNullException(nameof(device)); @@ -314,27 +235,6 @@ namespace Avalonia.Input return false; } - private bool MouseWheel(IPenDevice device, ulong timestamp, IInputRoot root, Point p, - PointerPointProperties props, - Vector delta, KeyModifiers inputModifiers) - { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - var hit = HitTest(root, p); - var source = GetSource(hit); - - if (source is not null) - { - var e = new PointerWheelEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers, delta); - - source?.RaiseEvent(e); - return e.Handled; - } - - return false; - } - private IInteractive? GetSource(IVisual? hit) { if (hit is null) diff --git a/src/Avalonia.Input/PointerPoint.cs b/src/Avalonia.Input/PointerPoint.cs index 9f8285a8e1..16fab7410c 100644 --- a/src/Avalonia.Input/PointerPoint.cs +++ b/src/Avalonia.Input/PointerPoint.cs @@ -20,6 +20,14 @@ namespace Avalonia.Input public bool IsRightButtonPressed { get; } public bool IsXButton1Pressed { get; } public bool IsXButton2Pressed { get; } + public bool IsBarrelButtonPressed { get; } + public bool IsEraser { get; } + + public float Twist { get; } + public float Pressure { get; } + public float XTilt { get; } + public float YTilt { get; } + public PointerUpdateKind PointerUpdateKind { get; } @@ -36,10 +44,12 @@ namespace Avalonia.Input IsRightButtonPressed = modifiers.HasAllFlags(RawInputModifiers.RightMouseButton); IsXButton1Pressed = modifiers.HasAllFlags(RawInputModifiers.XButton1MouseButton); IsXButton2Pressed = modifiers.HasAllFlags(RawInputModifiers.XButton2MouseButton); + IsBarrelButtonPressed = modifiers.HasAllFlags(RawInputModifiers.BarrelPenButton); + IsEraser = modifiers.HasAllFlags(RawInputModifiers.PenEraser); // The underlying input source might be reporting the previous state, // so make sure that we reflect the current state - + if (kind == PointerUpdateKind.LeftButtonPressed) IsLeftButtonPressed = true; if (kind == PointerUpdateKind.LeftButtonReleased) @@ -60,6 +70,20 @@ namespace Avalonia.Input IsXButton2Pressed = true; if (kind == PointerUpdateKind.XButton2Released) IsXButton2Pressed = false; + if (kind == PointerUpdateKind.BarrelButtonPressed) + IsBarrelButtonPressed = true; + if (kind == PointerUpdateKind.BarrelButtonReleased) + IsBarrelButtonPressed = false; + } + + public PointerPointProperties(RawInputModifiers modifiers, PointerUpdateKind kind, + float twist, float pressure, float xTilt, float yTilt + ) : this (modifiers, kind) + { + Twist = twist; + Pressure = pressure; + XTilt = xTilt; + YTilt = yTilt; } public static PointerPointProperties None { get; } = new PointerPointProperties(); @@ -77,7 +101,9 @@ namespace Avalonia.Input RightButtonReleased, XButton1Released, XButton2Released, - Other + Other, + BarrelButtonPressed, + BarrelButtonReleased } public static class PointerUpdateKindExtensions diff --git a/src/Avalonia.Input/Raw/RawPointerEventArgs.cs b/src/Avalonia.Input/Raw/RawPointerEventArgs.cs index 62a1dd5d84..ea160efef5 100644 --- a/src/Avalonia.Input/Raw/RawPointerEventArgs.cs +++ b/src/Avalonia.Input/Raw/RawPointerEventArgs.cs @@ -21,7 +21,13 @@ namespace Avalonia.Input.Raw TouchBegin, TouchUpdate, TouchEnd, - TouchCancel + TouchCancel, + PenBegin, + PenUpdate, + PenEnd, + PenCancel, + BarrelUp, + BarrelDown, } /// diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 5e31bba925..db11508ed7 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -374,17 +374,6 @@ namespace Avalonia.Win32 break; } - - - - - - - - - - - case WindowsMessage.WM_POINTERDEVICECHANGE: { //notifies about changes in the settings of a monitor that has a digitizer attached to it. @@ -414,7 +403,10 @@ namespace Avalonia.Win32 GetDeviceInfo(wParam, out var device, out var info); var point = PointToClient(PointFromLParam(lParam)); var modifiers = GetInputModifiers(info.dwKeyStates); - var eventType = info.ButtonChangeType switch + + if (info.ButtonChangeType != PointerButtonChangeType.POINTER_CHANGE_NONE) + { + var eventType = info.ButtonChangeType switch { PointerButtonChangeType.POINTER_CHANGE_FIRSTBUTTON_DOWN => RawPointerEventType.LeftButtonDown, PointerButtonChangeType.POINTER_CHANGE_SECONDBUTTON_DOWN => RawPointerEventType.RightButtonDown, @@ -428,12 +420,13 @@ namespace Avalonia.Win32 PointerButtonChangeType.POINTER_CHANGE_FOURTHBUTTON_UP => RawPointerEventType.XButton1Up, PointerButtonChangeType.POINTER_CHANGE_FIFTHBUTTON_UP => RawPointerEventType.XButton2Up, }; - if (eventType == RawPointerEventType.NonClientLeftButtonDown && - (WindowsMessage)msg == WindowsMessage.WM_NCPOINTERDOWN) - { - eventType = RawPointerEventType.NonClientLeftButtonDown; + if (eventType == RawPointerEventType.NonClientLeftButtonDown && + (WindowsMessage)msg == WindowsMessage.WM_NCPOINTERDOWN) + { + eventType = RawPointerEventType.NonClientLeftButtonDown; + } + e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers); } - e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers); break; } case WindowsMessage.WM_POINTERUPDATE: @@ -446,7 +439,8 @@ namespace Avalonia.Win32 var point = PointToClient(PointFromLParam(lParam)); var modifiers = GetInputModifiers(info.dwKeyStates); - e = new RawPointerEventArgs(device, timestamp, _owner, RawPointerEventType.Move, point, modifiers); + e = new RawPointerEventArgs( + device, timestamp, _owner, RawPointerEventType.Move, point, modifiers); break; } case WindowsMessage.WM_POINTERENTER: @@ -462,14 +456,10 @@ namespace Avalonia.Win32 { GetDeviceInfo(wParam, out var device, out var info); var point = PointToClient(PointFromLParam(lParam)); + var modifiers = GetInputModifiers(info.dwKeyStates); e = new RawPointerEventArgs( - device, - timestamp, - _owner, - RawPointerEventType.LeaveWindow, - point, - WindowsKeyboardDevice.Instance.Modifiers); + device, timestamp, _owner, RawPointerEventType.LeaveWindow, point, modifiers); break; } case WindowsMessage.WM_POINTERACTIVATE: @@ -520,33 +510,11 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_PARENTNOTIFY: { - //This message is sent in a dialog scenarios. Contains mouse position in an old-way, - //but listed in the wm_pointer reference + //This message is sent in a dialog scenarios. Contains mouse position. + //Old message, but listed in the wm_pointer reference //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-parentnotify break; } - - - - - - - - - - - - - - - - - - - - - - case WindowsMessage.WM_NCPAINT: { if (!HasFullDecorations) From d3f3166914428a945d24b8369c2b6c18ff0aab34 Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Sun, 23 Jan 2022 14:16:35 +0300 Subject: [PATCH 007/224] Provide Pressure, Tilt, and other parameters to PointerPointProperties. Provide touch events (still a lot to do), use point position from pointer info. --- src/Avalonia.Input/PenDevice.cs | 25 ++-- src/Avalonia.Input/PointerPoint.cs | 12 +- src/Avalonia.Input/Raw/RawPointerEventArgs.cs | 4 - .../Interop/UnmanagedMethods.cs | 2 +- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 111 ++++++++++++------ 5 files changed, 98 insertions(+), 56 deletions(-) diff --git a/src/Avalonia.Input/PenDevice.cs b/src/Avalonia.Input/PenDevice.cs index 3c0f3c7709..c993e7fac4 100644 --- a/src/Avalonia.Input/PenDevice.cs +++ b/src/Avalonia.Input/PenDevice.cs @@ -32,6 +32,14 @@ namespace Avalonia.Input [Obsolete("Use IPointer instead")] public IInputElement? Captured => _pointer.Captured; + public bool IsEraser { get; set; } + public bool IsInverted { get; set; } + public bool IsBarrel { get; set; } + public int XTilt { get; set; } + public int YTilt { get; set; } + public uint Pressure { get; set; } + public uint Twist { get; set; } + /// /// Captures mouse input to the specified control. /// @@ -122,13 +130,13 @@ namespace Avalonia.Input case RawPointerEventType.LeaveWindow: LeaveWindow(pen, e.Timestamp, e.Root, props, keyModifiers); break; - case RawPointerEventType.PenBegin: + case RawPointerEventType.LeftButtonDown: e.Handled = PenDown(pen, e.Timestamp, e.Root, e.Position, props, keyModifiers); break; - case RawPointerEventType.PenEnd: + case RawPointerEventType.LeftButtonUp: e.Handled = PenUp(pen, e.Timestamp, e.Root, e.Position, props, keyModifiers); break; - case RawPointerEventType.PenUpdate: + case RawPointerEventType.Move: e.Handled = PenMove(pen, e.Timestamp, e.Root, e.Position, props, keyModifiers); break; } @@ -145,16 +153,17 @@ namespace Avalonia.Input } - PointerPointProperties CreateProperties(RawPointerEventArgs args) + private PointerPointProperties CreateProperties(RawPointerEventArgs args) { var kind = PointerUpdateKind.Other; - if (args.Type == RawPointerEventType.PenBegin) + if (args.Type == RawPointerEventType.LeftButtonDown) kind = PointerUpdateKind.LeftButtonPressed; - if (args.Type == RawPointerEventType.PenEnd) + if (args.Type == RawPointerEventType.LeftButtonUp) kind = PointerUpdateKind.LeftButtonReleased; - - return new PointerPointProperties(args.InputModifiers, kind); + + return new PointerPointProperties(args.InputModifiers, kind, + Twist, Pressure, XTilt, YTilt, IsEraser, IsInverted, IsBarrel); } private MouseButton _lastMouseDownButton; diff --git a/src/Avalonia.Input/PointerPoint.cs b/src/Avalonia.Input/PointerPoint.cs index 16fab7410c..07ef130fcc 100644 --- a/src/Avalonia.Input/PointerPoint.cs +++ b/src/Avalonia.Input/PointerPoint.cs @@ -22,6 +22,7 @@ namespace Avalonia.Input public bool IsXButton2Pressed { get; } public bool IsBarrelButtonPressed { get; } public bool IsEraser { get; } + public bool IsInverted { get; } public float Twist { get; } public float Pressure { get; } @@ -70,20 +71,19 @@ namespace Avalonia.Input IsXButton2Pressed = true; if (kind == PointerUpdateKind.XButton2Released) IsXButton2Pressed = false; - if (kind == PointerUpdateKind.BarrelButtonPressed) - IsBarrelButtonPressed = true; - if (kind == PointerUpdateKind.BarrelButtonReleased) - IsBarrelButtonPressed = false; } public PointerPointProperties(RawInputModifiers modifiers, PointerUpdateKind kind, - float twist, float pressure, float xTilt, float yTilt + float twist, float pressure, float xTilt, float yTilt, bool isEraser, bool isInverted, bool isBarrel ) : this (modifiers, kind) { Twist = twist; Pressure = pressure; XTilt = xTilt; YTilt = yTilt; + IsEraser = isEraser; + IsInverted = isInverted; + IsBarrelButtonPressed = isBarrel; } public static PointerPointProperties None { get; } = new PointerPointProperties(); @@ -102,8 +102,6 @@ namespace Avalonia.Input XButton1Released, XButton2Released, Other, - BarrelButtonPressed, - BarrelButtonReleased } public static class PointerUpdateKindExtensions diff --git a/src/Avalonia.Input/Raw/RawPointerEventArgs.cs b/src/Avalonia.Input/Raw/RawPointerEventArgs.cs index 6a35ec3fad..c75eb69bfd 100644 --- a/src/Avalonia.Input/Raw/RawPointerEventArgs.cs +++ b/src/Avalonia.Input/Raw/RawPointerEventArgs.cs @@ -22,10 +22,6 @@ namespace Avalonia.Input.Raw TouchUpdate, TouchEnd, TouchCancel, - PenBegin, - PenUpdate, - PenEnd, - PenCancel, BarrelUp, BarrelDown, Magnify, diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index 93457f7bbd..b3cab5d052 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -1003,7 +1003,7 @@ namespace Avalonia.Win32.Interop public uint historyCount; public int inputData; public ModifierKeys dwKeyStates; - public UInt64 PerformanceCount; + public ulong PerformanceCount; public PointerButtonChangeType ButtonChangeType; } diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index db11508ed7..34cd9acf25 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -22,8 +22,8 @@ namespace Avalonia.Win32 uint timestamp = unchecked((uint)GetMessageTime()); RawInputEventArgs e = null; var shouldTakeFocus = false; - - switch ((WindowsMessage)msg) + var message = (WindowsMessage)msg; + switch (message) { case WindowsMessage.WM_ACTIVATE: { @@ -180,7 +180,7 @@ namespace Avalonia.Win32 _mouseDevice, timestamp, _owner, - (WindowsMessage)msg switch + message switch { WindowsMessage.WM_LBUTTONDOWN => RawPointerEventType.LeftButtonDown, WindowsMessage.WM_RBUTTONDOWN => RawPointerEventType.RightButtonDown, @@ -212,7 +212,7 @@ namespace Avalonia.Win32 _mouseDevice, timestamp, _owner, - (WindowsMessage)msg switch + message switch { WindowsMessage.WM_LBUTTONUP => RawPointerEventType.LeftButtonUp, WindowsMessage.WM_RBUTTONUP => RawPointerEventType.RightButtonUp, @@ -327,7 +327,7 @@ namespace Avalonia.Win32 _mouseDevice, timestamp, _owner, - (WindowsMessage)msg switch + message switch { WindowsMessage.WM_NCLBUTTONDOWN => RawPointerEventType .NonClientLeftButtonDown, @@ -400,33 +400,13 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERDOWN: case WindowsMessage.WM_POINTERUP: { + var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); GetDeviceInfo(wParam, out var device, out var info); - var point = PointToClient(PointFromLParam(lParam)); + var eventType = GetEventType(message, info); + var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); - if (info.ButtonChangeType != PointerButtonChangeType.POINTER_CHANGE_NONE) - { - var eventType = info.ButtonChangeType switch - { - PointerButtonChangeType.POINTER_CHANGE_FIRSTBUTTON_DOWN => RawPointerEventType.LeftButtonDown, - PointerButtonChangeType.POINTER_CHANGE_SECONDBUTTON_DOWN => RawPointerEventType.RightButtonDown, - PointerButtonChangeType.POINTER_CHANGE_THIRDBUTTON_DOWN => RawPointerEventType.MiddleButtonDown, - PointerButtonChangeType.POINTER_CHANGE_FOURTHBUTTON_DOWN => RawPointerEventType.XButton1Down, - PointerButtonChangeType.POINTER_CHANGE_FIFTHBUTTON_DOWN => RawPointerEventType.XButton2Down, - - PointerButtonChangeType.POINTER_CHANGE_FIRSTBUTTON_UP => RawPointerEventType.LeftButtonUp, - PointerButtonChangeType.POINTER_CHANGE_SECONDBUTTON_UP => RawPointerEventType.RightButtonUp, - PointerButtonChangeType.POINTER_CHANGE_THIRDBUTTON_UP => RawPointerEventType.MiddleButtonUp, - PointerButtonChangeType.POINTER_CHANGE_FOURTHBUTTON_UP => RawPointerEventType.XButton1Up, - PointerButtonChangeType.POINTER_CHANGE_FIFTHBUTTON_UP => RawPointerEventType.XButton2Up, - }; - if (eventType == RawPointerEventType.NonClientLeftButtonDown && - (WindowsMessage)msg == WindowsMessage.WM_NCPOINTERDOWN) - { - eventType = RawPointerEventType.NonClientLeftButtonDown; - } - e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers); - } + e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers); break; } case WindowsMessage.WM_POINTERUPDATE: @@ -436,11 +416,11 @@ namespace Avalonia.Win32 break; } GetDeviceInfo(wParam, out var device, out var info); - var point = PointToClient(PointFromLParam(lParam)); + var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); + var eventType = device is TouchDevice ? RawPointerEventType.TouchUpdate : RawPointerEventType.Move; - e = new RawPointerEventArgs( - device, timestamp, _owner, RawPointerEventType.Move, point, modifiers); + e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers); break; } case WindowsMessage.WM_POINTERENTER: @@ -455,7 +435,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERLEAVE: { GetDeviceInfo(wParam, out var device, out var info); - var point = PointToClient(PointFromLParam(lParam)); + var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); e = new RawPointerEventArgs( @@ -478,7 +458,7 @@ namespace Avalonia.Win32 { GetDeviceInfo(wParam, out var device, out var info); - var point = PointToClient(PointFromLParam(lParam)); + var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); var delta = new Vector(0, (ToInt32(wParam) >> 16) / wheelDelta); e = new RawMouseWheelEventArgs(device, timestamp, _owner, point, delta, modifiers); @@ -488,7 +468,7 @@ namespace Avalonia.Win32 { GetDeviceInfo(wParam, out var device, out var info); - var point = PointToClient(PointFromLParam(lParam)); + var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); var delta = new Vector((ToInt32(wParam) >> 16) / wheelDelta, 0); e = new RawMouseWheelEventArgs(device, timestamp, _owner, point, delta, modifiers); @@ -696,7 +676,7 @@ namespace Avalonia.Win32 { Input(e); - if ((WindowsMessage)msg == WindowsMessage.WM_KEYDOWN) + if (message == WindowsMessage.WM_KEYDOWN) { // Handling a WM_KEYDOWN message should cause the subsequent WM_CHAR message to // be ignored. This should be safe to do as WM_CHAR should only be produced in @@ -717,6 +697,44 @@ namespace Avalonia.Win32 } } + private static RawPointerEventType GetEventType(WindowsMessage message, POINTER_INFO info) + { + switch (info.pointerType) + { + case PointerInputType.PT_PEN: + return ToEventType(info.ButtonChangeType); + case PointerInputType.PT_TOUCH: + if (info.pointerFlags.HasFlag(PointerFlags.POINTER_FLAG_CANCELED) || + !info.pointerFlags.HasFlag(PointerFlags.POINTER_FLAG_CONFIDENCE)) + { + return RawPointerEventType.TouchCancel; + } + return message == WindowsMessage.WM_POINTERDOWN || message == WindowsMessage.WM_NCPOINTERDOWN + ? RawPointerEventType.TouchBegin + : RawPointerEventType.TouchEnd; + default: + var eventType = ToEventType(info.ButtonChangeType); + if (eventType == RawPointerEventType.LeftButtonDown && + message == WindowsMessage.WM_NCPOINTERDOWN) + { + eventType = RawPointerEventType.NonClientLeftButtonDown; + } + return eventType; + } + } + + private unsafe void ApplyPenInfo(POINTER_PEN_INFO penInfo) + { + _penDevice.IsBarrel = penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_BARREL); + _penDevice.IsEraser = penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_BARREL); + _penDevice.IsInverted = penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_INVERTED); + + _penDevice.XTilt = penInfo.tiltX; + _penDevice.YTilt = penInfo.tiltY; + _penDevice.Pressure = penInfo.pressure; + _penDevice.Twist = penInfo.rotation; + } + private void GetDeviceInfo(IntPtr wParam, out IInputDevice device, out POINTER_INFO info) { var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); @@ -727,6 +745,8 @@ namespace Avalonia.Win32 device = _penDevice; GetPointerPenInfo(pointerId, out var penInfo); info = penInfo.pointerInfo; + + ApplyPenInfo(penInfo); break; case PointerInputType.PT_TOUCH: device = _touchDevice; @@ -740,6 +760,25 @@ namespace Avalonia.Win32 } } + private static RawPointerEventType ToEventType(PointerButtonChangeType type) + { + return type switch + { + PointerButtonChangeType.POINTER_CHANGE_FIRSTBUTTON_DOWN => RawPointerEventType.LeftButtonDown, + PointerButtonChangeType.POINTER_CHANGE_SECONDBUTTON_DOWN => RawPointerEventType.RightButtonDown, + PointerButtonChangeType.POINTER_CHANGE_THIRDBUTTON_DOWN => RawPointerEventType.MiddleButtonDown, + PointerButtonChangeType.POINTER_CHANGE_FOURTHBUTTON_DOWN => RawPointerEventType.XButton1Down, + PointerButtonChangeType.POINTER_CHANGE_FIFTHBUTTON_DOWN => RawPointerEventType.XButton2Down, + + PointerButtonChangeType.POINTER_CHANGE_FIRSTBUTTON_UP => RawPointerEventType.LeftButtonUp, + PointerButtonChangeType.POINTER_CHANGE_SECONDBUTTON_UP => RawPointerEventType.RightButtonUp, + PointerButtonChangeType.POINTER_CHANGE_THIRDBUTTON_UP => RawPointerEventType.MiddleButtonUp, + PointerButtonChangeType.POINTER_CHANGE_FOURTHBUTTON_UP => RawPointerEventType.XButton1Up, + PointerButtonChangeType.POINTER_CHANGE_FIFTHBUTTON_UP => RawPointerEventType.XButton2Up, + _ => RawPointerEventType.Move + }; + } + public readonly bool Win8Plus = Win32Platform.WindowsVersion >= PlatformConstants.Windows8; private void UpdateInputMethod(IntPtr hkl) From c046165714c37e20b305cfae823347966cd95aca Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Sun, 23 Jan 2022 15:36:29 +0300 Subject: [PATCH 008/224] Add History fetch methods (will be implemented a bit later). Raise touch events. --- .../Interop/UnmanagedMethods.cs | 25 +++-- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 103 +++++++++++------- 2 files changed, 76 insertions(+), 52 deletions(-) diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index b3cab5d052..d71efcb074 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -1074,32 +1074,35 @@ namespace Avalonia.Win32.Interop public const int SizeOf_BITMAPINFOHEADER = 40; + [DllImport("user32.dll", SetLastError = true)] + public static extern bool IsMouseInPointerEnabled(); + [DllImport("user32.dll", SetLastError = true)] public static extern int EnableMouseInPointer(bool enable); [DllImport("user32.dll", SetLastError = true)] - public static extern bool GetPointerCursorId(uint pointerID, out uint cursorId); + public static extern bool GetPointerCursorId(uint pointerId, out uint cursorId); [DllImport("user32.dll", SetLastError = true)] - public static extern bool GetPointerType(uint pointerID, out PointerInputType pointerType); + public static extern bool GetPointerType(uint pointerId, out PointerInputType pointerType); - [DllImport("User32.dll", SetLastError = true)] - public static extern bool GetPointerInfo(uint pointerID, out POINTER_INFO pointerInfo); + [DllImport("user32.dll", SetLastError = true)] + public static extern bool GetPointerInfo(uint pointerId, out POINTER_INFO pointerInfo); - [DllImport("User32.dll", SetLastError = true)] - public static extern bool GetPointerPenInfo(uint pointerID, out POINTER_PEN_INFO penInfo); + [DllImport("user32.dll", SetLastError = true)] + public static extern bool GetPointerInfoHistory(uint pointerId, ref int entriesCount, [MarshalAs(UnmanagedType.LPArray), In, Out] POINTER_INFO[] pointerInfos); - [DllImport("User32.dll", SetLastError = true)] - public static extern bool GetPointerTouchInfo(uint pointerID, out POINTER_TOUCH_INFO touchInfo); + [DllImport("user32.dll", SetLastError = true)] + public static extern bool GetPointerPenInfo(uint pointerId, out POINTER_PEN_INFO penInfo); [DllImport("user32.dll", SetLastError = true)] - public static extern void GetUnpredictedMessagePos(); + public static extern bool GetPointerPenInfoHistory(uint pointerId, ref int entriesCount, [MarshalAs(UnmanagedType.LPArray), In, Out] POINTER_PEN_INFO[] penInfos); [DllImport("user32.dll", SetLastError = true)] - public static extern bool IsMouseInPointerEnabled(); + public static extern bool GetPointerTouchInfo(uint pointerId, out POINTER_TOUCH_INFO touchInfo); [DllImport("user32.dll", SetLastError = true)] - public static extern bool SkipPointerFrameMessages(uint pointerID); + public static extern bool GetPointerTouchInfoHistory(uint pointerId, ref int entriesCount, [MarshalAs(UnmanagedType.LPArray), In, Out] POINTER_TOUCH_INFO[] touchInfos); [DllImport("user32.dll")] public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 34cd9acf25..fda68e1561 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -401,26 +401,41 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERUP: { var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); - GetDeviceInfo(wParam, out var device, out var info); + GetDeviceInfo(wParam, out var device, out var info, ref timestamp); var eventType = GetEventType(message, info); var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); - e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers); + if (device is TouchDevice) + { + e = new RawTouchEventArgs(_touchDevice, timestamp, _owner, eventType, point, modifiers, info.pointerId); + } + else + { + e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers); + } break; } case WindowsMessage.WM_POINTERUPDATE: { - if (ShouldIgnoreTouchEmulatedMessage()) - { - break; - } - GetDeviceInfo(wParam, out var device, out var info); + GetDeviceInfo(wParam, out var device, out var info, ref timestamp); var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); - var eventType = device is TouchDevice ? RawPointerEventType.TouchUpdate : RawPointerEventType.Move; - e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers); + if (device is TouchDevice) + { + if (ShouldIgnoreTouchEmulatedMessage()) + { + break; + } + e = new RawTouchEventArgs(_touchDevice, timestamp, _owner, + RawPointerEventType.TouchUpdate, point, modifiers, info.pointerId); + } + else + { + e = new RawPointerEventArgs(device, timestamp, _owner, + RawPointerEventType.Move, point, modifiers); + } break; } case WindowsMessage.WM_POINTERENTER: @@ -434,7 +449,7 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_POINTERLEAVE: { - GetDeviceInfo(wParam, out var device, out var info); + GetDeviceInfo(wParam, out var device, out var info, ref timestamp); var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); @@ -456,7 +471,7 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_POINTERWHEEL: { - GetDeviceInfo(wParam, out var device, out var info); + GetDeviceInfo(wParam, out var device, out var info, ref timestamp); var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); @@ -466,7 +481,7 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_POINTERHWHEEL: { - GetDeviceInfo(wParam, out var device, out var info); + GetDeviceInfo(wParam, out var device, out var info, ref timestamp); var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); @@ -697,36 +712,10 @@ namespace Avalonia.Win32 } } - private static RawPointerEventType GetEventType(WindowsMessage message, POINTER_INFO info) - { - switch (info.pointerType) - { - case PointerInputType.PT_PEN: - return ToEventType(info.ButtonChangeType); - case PointerInputType.PT_TOUCH: - if (info.pointerFlags.HasFlag(PointerFlags.POINTER_FLAG_CANCELED) || - !info.pointerFlags.HasFlag(PointerFlags.POINTER_FLAG_CONFIDENCE)) - { - return RawPointerEventType.TouchCancel; - } - return message == WindowsMessage.WM_POINTERDOWN || message == WindowsMessage.WM_NCPOINTERDOWN - ? RawPointerEventType.TouchBegin - : RawPointerEventType.TouchEnd; - default: - var eventType = ToEventType(info.ButtonChangeType); - if (eventType == RawPointerEventType.LeftButtonDown && - message == WindowsMessage.WM_NCPOINTERDOWN) - { - eventType = RawPointerEventType.NonClientLeftButtonDown; - } - return eventType; - } - } - private unsafe void ApplyPenInfo(POINTER_PEN_INFO penInfo) { _penDevice.IsBarrel = penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_BARREL); - _penDevice.IsEraser = penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_BARREL); + _penDevice.IsEraser = penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_ERASER); _penDevice.IsInverted = penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_INVERTED); _penDevice.XTilt = penInfo.tiltX; @@ -735,10 +724,11 @@ namespace Avalonia.Win32 _penDevice.Twist = penInfo.rotation; } - private void GetDeviceInfo(IntPtr wParam, out IInputDevice device, out POINTER_INFO info) + private void GetDeviceInfo(IntPtr wParam, out IInputDevice device, out POINTER_INFO info, ref uint timestamp) { var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); - GetPointerType(pointerId, out var type);//ToDo we can cache this and invalidate in WM_POINTERDEVICECHANGE + GetPointerType(pointerId, out var type); + //GetPointerCursorId(pointerId, out var cursorId); switch (type) { case PointerInputType.PT_PEN: @@ -758,6 +748,37 @@ namespace Avalonia.Win32 GetPointerInfo(pointerId, out info); break; } + + if (info.dwTime != 0) + { + timestamp = info.dwTime; + } + } + + private static RawPointerEventType GetEventType(WindowsMessage message, POINTER_INFO info) + { + switch (info.pointerType) + { + case PointerInputType.PT_PEN: + return ToEventType(info.ButtonChangeType); + case PointerInputType.PT_TOUCH: + if (info.pointerFlags.HasFlag(PointerFlags.POINTER_FLAG_CANCELED) || + !info.pointerFlags.HasFlag(PointerFlags.POINTER_FLAG_CONFIDENCE)) + { + return RawPointerEventType.TouchCancel; + } + return message == WindowsMessage.WM_POINTERDOWN || message == WindowsMessage.WM_NCPOINTERDOWN + ? RawPointerEventType.TouchBegin + : RawPointerEventType.TouchEnd; + default: + var eventType = ToEventType(info.ButtonChangeType); + if (eventType == RawPointerEventType.LeftButtonDown && + message == WindowsMessage.WM_NCPOINTERDOWN) + { + eventType = RawPointerEventType.NonClientLeftButtonDown; + } + return eventType; + } } private static RawPointerEventType ToEventType(PointerButtonChangeType type) From cbc813235a80918b5f0a57ca54f5abe5b47b08cb Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Sun, 23 Jan 2022 16:35:25 +0300 Subject: [PATCH 009/224] Combine some message handlers --- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 68 +++++++------------ 1 file changed, 24 insertions(+), 44 deletions(-) diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index fda68e1561..0266134199 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -399,26 +399,10 @@ namespace Avalonia.Win32 case WindowsMessage.WM_NCPOINTERUP: case WindowsMessage.WM_POINTERDOWN: case WindowsMessage.WM_POINTERUP: - { - var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); - GetDeviceInfo(wParam, out var device, out var info, ref timestamp); - var eventType = GetEventType(message, info); - var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); - var modifiers = GetInputModifiers(info.dwKeyStates); - - if (device is TouchDevice) - { - e = new RawTouchEventArgs(_touchDevice, timestamp, _owner, eventType, point, modifiers, info.pointerId); - } - else - { - e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers); - } - break; - } case WindowsMessage.WM_POINTERUPDATE: { - GetDeviceInfo(wParam, out var device, out var info, ref timestamp); + GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); + var eventType = GetEventType(message, info); var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); @@ -428,13 +412,11 @@ namespace Avalonia.Win32 { break; } - e = new RawTouchEventArgs(_touchDevice, timestamp, _owner, - RawPointerEventType.TouchUpdate, point, modifiers, info.pointerId); + e = new RawTouchEventArgs(_touchDevice, timestamp, _owner, eventType, point, modifiers, info.pointerId); } else { - e = new RawPointerEventArgs(device, timestamp, _owner, - RawPointerEventType.Move, point, modifiers); + e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers); } break; } @@ -449,7 +431,7 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_POINTERLEAVE: { - GetDeviceInfo(wParam, out var device, out var info, ref timestamp); + GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); @@ -457,6 +439,18 @@ namespace Avalonia.Win32 device, timestamp, _owner, RawPointerEventType.LeaveWindow, point, modifiers); break; } + case WindowsMessage.WM_POINTERWHEEL: + case WindowsMessage.WM_POINTERHWHEEL: + { + GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); + + var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); + var modifiers = GetInputModifiers(info.dwKeyStates); + var val = (ToInt32(wParam) >> 16) / wheelDelta; + var delta = message == WindowsMessage.WM_POINTERHWHEEL ? new Vector(0, val) : new Vector(val, 0); + e = new RawMouseWheelEventArgs(device, timestamp, _owner, point, delta, modifiers); + break; + } case WindowsMessage.WM_POINTERACTIVATE: { //occurs when a pointer activates an inactive window. @@ -469,26 +463,6 @@ namespace Avalonia.Win32 _mouseDevice.Capture(null); return IntPtr.Zero; } - case WindowsMessage.WM_POINTERWHEEL: - { - GetDeviceInfo(wParam, out var device, out var info, ref timestamp); - - var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); - var modifiers = GetInputModifiers(info.dwKeyStates); - var delta = new Vector(0, (ToInt32(wParam) >> 16) / wheelDelta); - e = new RawMouseWheelEventArgs(device, timestamp, _owner, point, delta, modifiers); - break; - } - case WindowsMessage.WM_POINTERHWHEEL: - { - GetDeviceInfo(wParam, out var device, out var info, ref timestamp); - - var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); - var modifiers = GetInputModifiers(info.dwKeyStates); - var delta = new Vector((ToInt32(wParam) >> 16) / wheelDelta, 0); - e = new RawMouseWheelEventArgs(device, timestamp, _owner, point, delta, modifiers); - break; - } case WindowsMessage.DM_POINTERHITTEST: { //DM stands for direct manipulation. @@ -724,7 +698,7 @@ namespace Avalonia.Win32 _penDevice.Twist = penInfo.rotation; } - private void GetDeviceInfo(IntPtr wParam, out IInputDevice device, out POINTER_INFO info, ref uint timestamp) + private void GetDevicePointerInfo(IntPtr wParam, out IInputDevice device, out POINTER_INFO info, ref uint timestamp) { var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); GetPointerType(pointerId, out var type); @@ -757,6 +731,12 @@ namespace Avalonia.Win32 private static RawPointerEventType GetEventType(WindowsMessage message, POINTER_INFO info) { + if (message == WindowsMessage.WM_POINTERUPDATE) + { + return info.pointerType == PointerInputType.PT_TOUCH + ? RawPointerEventType.TouchUpdate + : RawPointerEventType.Move; + } switch (info.pointerType) { case PointerInputType.PT_PEN: From c880572c5f711edb016a727f17003dabc2b1427f Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Sun, 23 Jan 2022 19:27:17 +0300 Subject: [PATCH 010/224] Handle history pointer infos for touch and pen --- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 0266134199..0aaa9d4ce0 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -402,6 +402,62 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERUPDATE: { GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); + + if (info.historyCount > 1) + { + if (info.pointerType == PointerInputType.PT_TOUCH) + { + if (ShouldIgnoreTouchEmulatedMessage()) + { + break; + } + + var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); + var historyCount = (int)info.historyCount; + var historyTouchInfos = new POINTER_TOUCH_INFO[historyCount]; + if (GetPointerTouchInfoHistory(pointerId, ref historyCount, historyTouchInfos)) + { + //last info is the same as the current so skip it + for (int i = 0; i < historyCount - 1; i++) + { + var historyTouchInfo = historyTouchInfos[i]; + var historyInfo = historyTouchInfo.pointerInfo; + var historyEventType = GetEventType(message, historyInfo); + var historyPoint = PointToClient(new PixelPoint( + historyInfo.ptPixelLocationX, historyInfo.ptPixelLocationY)); + var historyModifiers = GetInputModifiers(historyInfo.dwKeyStates); + var historyTimestamp = historyInfo.dwTime == 0 ? timestamp : historyInfo.dwTime; + Input?.Invoke(new RawTouchEventArgs(_touchDevice, historyTimestamp, _owner, + historyEventType, historyPoint, historyModifiers, historyInfo.pointerId)); + } + } + } + else if (info.pointerType == PointerInputType.PT_PEN) + { + var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); + var historyCount = (int)info.historyCount; + var historyPenInfos = new POINTER_PEN_INFO[historyCount]; + if (GetPointerPenInfoHistory(pointerId, ref historyCount, historyPenInfos)) + { + //last info is the same as the current so skip it + for (int i = 0; i < historyCount - 1; i++) + { + var historyPenInfo = historyPenInfos[i]; + var historyInfo = historyPenInfo.pointerInfo; + var historyEventType = GetEventType(message, historyInfo); + var historyPoint = PointToClient(new PixelPoint( + historyInfo.ptPixelLocationX, historyInfo.ptPixelLocationY)); + var historyModifiers = GetInputModifiers(historyInfo.dwKeyStates); + var historyTimestamp = historyInfo.dwTime == 0 ? timestamp : historyInfo.dwTime; + + ApplyPenInfo(historyPenInfo); + Input?.Invoke(new RawPointerEventArgs(_penDevice, historyTimestamp, _owner, + historyEventType, historyPoint, historyModifiers)); + } + } + } + } + var eventType = GetEventType(message, info); var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); @@ -447,7 +503,7 @@ namespace Avalonia.Win32 var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); var val = (ToInt32(wParam) >> 16) / wheelDelta; - var delta = message == WindowsMessage.WM_POINTERHWHEEL ? new Vector(0, val) : new Vector(val, 0); + var delta = message == WindowsMessage.WM_POINTERWHEEL ? new Vector(0, val) : new Vector(val, 0); e = new RawMouseWheelEventArgs(device, timestamp, _owner, point, delta, modifiers); break; } From 8cbd26ffdf6709e5e22487aeadc3d6b99f55af2c Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Sun, 23 Jan 2022 19:49:32 +0300 Subject: [PATCH 011/224] Remove redundant changes, improve tab control pointer type check --- src/Avalonia.Controls/TabControl.cs | 6 ++++-- src/Avalonia.Input/IKeyboardDevice.cs | 2 -- src/Avalonia.Input/PointerPoint.cs | 2 +- src/Avalonia.Input/Raw/RawPointerEventArgs.cs | 2 -- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Avalonia.Controls/TabControl.cs b/src/Avalonia.Controls/TabControl.cs index 306a9d3e6a..955b29f3f9 100644 --- a/src/Avalonia.Controls/TabControl.cs +++ b/src/Avalonia.Controls/TabControl.cs @@ -211,7 +211,8 @@ namespace Avalonia.Controls { base.OnPointerPressed(e); - if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed && e.Pointer.Type == PointerType.Mouse) + if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed && + (e.Pointer.Type == PointerType.Mouse || e.Pointer.Type == PointerType.Pen)) { e.Handled = UpdateSelectionFromEventSource(e.Source); } @@ -219,7 +220,8 @@ namespace Avalonia.Controls protected override void OnPointerReleased(PointerReleasedEventArgs e) { - if (e.InitialPressMouseButton == MouseButton.Left && e.Pointer.Type != PointerType.Mouse) + if (e.InitialPressMouseButton == MouseButton.Left && + (e.Pointer.Type == PointerType.Mouse || e.Pointer.Type == PointerType.Pen)) { var container = GetContainerFromEventSource(e.Source); if (container != null diff --git a/src/Avalonia.Input/IKeyboardDevice.cs b/src/Avalonia.Input/IKeyboardDevice.cs index b3ea7c5f4f..9506dc36fb 100644 --- a/src/Avalonia.Input/IKeyboardDevice.cs +++ b/src/Avalonia.Input/IKeyboardDevice.cs @@ -47,8 +47,6 @@ namespace Avalonia.Input MiddleMouseButton = 64, XButton1MouseButton = 128, XButton2MouseButton = 256, - BarrelPenButton = 512, - PenEraser = 1024, KeyboardMask = Alt | Control | Shift | Meta } diff --git a/src/Avalonia.Input/PointerPoint.cs b/src/Avalonia.Input/PointerPoint.cs index 07ef130fcc..cdfcb8da3a 100644 --- a/src/Avalonia.Input/PointerPoint.cs +++ b/src/Avalonia.Input/PointerPoint.cs @@ -101,7 +101,7 @@ namespace Avalonia.Input RightButtonReleased, XButton1Released, XButton2Released, - Other, + Other } public static class PointerUpdateKindExtensions diff --git a/src/Avalonia.Input/Raw/RawPointerEventArgs.cs b/src/Avalonia.Input/Raw/RawPointerEventArgs.cs index c75eb69bfd..d6406121c7 100644 --- a/src/Avalonia.Input/Raw/RawPointerEventArgs.cs +++ b/src/Avalonia.Input/Raw/RawPointerEventArgs.cs @@ -22,8 +22,6 @@ namespace Avalonia.Input.Raw TouchUpdate, TouchEnd, TouchCancel, - BarrelUp, - BarrelDown, Magnify, Rotate, Swipe From 5127006d1b84880b03fbfca7a25889c376fcc8be Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Sun, 23 Jan 2022 22:12:34 +0300 Subject: [PATCH 012/224] fix touch issues --- .../GestureRecognizers/ScrollGestureRecognizer.cs | 2 +- src/Avalonia.Input/PointerPoint.cs | 2 -- src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs | 7 +++++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Input/GestureRecognizers/ScrollGestureRecognizer.cs b/src/Avalonia.Input/GestureRecognizers/ScrollGestureRecognizer.cs index f8cedb636f..1d97fdfe53 100644 --- a/src/Avalonia.Input/GestureRecognizers/ScrollGestureRecognizer.cs +++ b/src/Avalonia.Input/GestureRecognizers/ScrollGestureRecognizer.cs @@ -101,7 +101,7 @@ namespace Avalonia.Input.GestureRecognizers if (_scrolling) { var vector = _trackedRootPoint - rootPoint; - var elapsed = _lastMoveTimestamp.HasValue ? + var elapsed = _lastMoveTimestamp.HasValue && _lastMoveTimestamp < e.Timestamp ? TimeSpan.FromMilliseconds(e.Timestamp - _lastMoveTimestamp.Value) : TimeSpan.Zero; diff --git a/src/Avalonia.Input/PointerPoint.cs b/src/Avalonia.Input/PointerPoint.cs index cdfcb8da3a..728bb1c579 100644 --- a/src/Avalonia.Input/PointerPoint.cs +++ b/src/Avalonia.Input/PointerPoint.cs @@ -45,8 +45,6 @@ namespace Avalonia.Input IsRightButtonPressed = modifiers.HasAllFlags(RawInputModifiers.RightMouseButton); IsXButton1Pressed = modifiers.HasAllFlags(RawInputModifiers.XButton1MouseButton); IsXButton2Pressed = modifiers.HasAllFlags(RawInputModifiers.XButton2MouseButton); - IsBarrelButtonPressed = modifiers.HasAllFlags(RawInputModifiers.BarrelPenButton); - IsEraser = modifiers.HasAllFlags(RawInputModifiers.PenEraser); // The underlying input source might be reporting the previous state, // so make sure that we reflect the current state diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 0aaa9d4ce0..52981ac7ec 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -488,6 +488,10 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERLEAVE: { GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); + if (device is TouchDevice) + { + break; + } var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); @@ -798,8 +802,7 @@ namespace Avalonia.Win32 case PointerInputType.PT_PEN: return ToEventType(info.ButtonChangeType); case PointerInputType.PT_TOUCH: - if (info.pointerFlags.HasFlag(PointerFlags.POINTER_FLAG_CANCELED) || - !info.pointerFlags.HasFlag(PointerFlags.POINTER_FLAG_CONFIDENCE)) + if (info.pointerFlags.HasFlag(PointerFlags.POINTER_FLAG_CANCELED)) { return RawPointerEventType.TouchCancel; } From 8dee09b830462ed3e0c5d6197c44a95f2049a935 Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Sun, 23 Jan 2022 23:13:39 +0300 Subject: [PATCH 013/224] Allow double click for pen device --- src/Avalonia.Input/PenDevice.cs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Input/PenDevice.cs b/src/Avalonia.Input/PenDevice.cs index c993e7fac4..aa4f89bb85 100644 --- a/src/Avalonia.Input/PenDevice.cs +++ b/src/Avalonia.Input/PenDevice.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Reactive.Linq; using Avalonia.Input.Raw; using Avalonia.Interactivity; +using Avalonia.Platform; using Avalonia.VisualTree; namespace Avalonia.Input @@ -12,6 +13,10 @@ namespace Avalonia.Input /// public class PenDevice : IPenDevice, IDisposable { + private int _clickCount; + private Rect _lastClickRect; + private ulong _lastClickTime; + private readonly Pointer _pointer; private bool _disposed; private PixelPoint? _position; @@ -182,8 +187,21 @@ namespace Avalonia.Input var source = GetSource(hit); if (source != null) { + var settings = AvaloniaLocator.Current.GetService(); + var doubleClickTime = settings?.DoubleClickTime.TotalMilliseconds ?? 500; + var doubleClickSize = settings?.DoubleClickSize ?? new Size(4, 4); + + if (!_lastClickRect.Contains(p) || timestamp - _lastClickTime > doubleClickTime) + { + _clickCount = 0; + } + + ++_clickCount; + _lastClickTime = timestamp; + _lastClickRect = new Rect(p, new Size()) + .Inflate(new Thickness(doubleClickSize.Width / 2, doubleClickSize.Height / 2)); _lastMouseDownButton = properties.PointerUpdateKind.GetMouseButton(); - var e = new PointerPressedEventArgs(source, _pointer, root, p, timestamp, properties, inputModifiers, 1); + var e = new PointerPressedEventArgs(source, _pointer, root, p, timestamp, properties, inputModifiers, _clickCount); source.RaiseEvent(e); return e.Handled; } From 600bb3198c775a76ac9034a5473fea795056be7b Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sun, 23 Jan 2022 21:20:02 -0500 Subject: [PATCH 014/224] Add pressure preview --- samples/ControlCatalog/Pages/PointersPage.cs | 266 ++++++++++++------- 1 file changed, 169 insertions(+), 97 deletions(-) diff --git a/samples/ControlCatalog/Pages/PointersPage.cs b/samples/ControlCatalog/Pages/PointersPage.cs index 2901013cea..9702e8e3e7 100644 --- a/samples/ControlCatalog/Pages/PointersPage.cs +++ b/samples/ControlCatalog/Pages/PointersPage.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.Collections.Generic; using System.Diagnostics; @@ -25,7 +26,8 @@ public class PointersPage : Decorator Items = new[] { new TabItem() { Header = "Contacts", Content = new PointerContactsTab() }, - new TabItem() { Header = "IntermediatePoints", Content = new PointerIntermediatePointsTab() } + new TabItem() { Header = "IntermediatePoints", Content = new PointerIntermediatePointsTab() }, + new TabItem() { Header = "Pressure", Content = new PointerPressureTab() } } }; } @@ -148,7 +150,7 @@ public class PointersPage : Decorator { Children = { - new PointerCanvas(slider, status), + new PointerCanvas(slider, status, true), new Border { Background = Brushes.LightYellow, @@ -182,140 +184,210 @@ public class PointersPage : Decorator } }; } + } - class PointerCanvas : Control + public class PointerPressureTab : Decorator + { + public PointerPressureTab() { - private readonly Slider _slider; - private readonly TextBlock _status; - private int _events; - private Stopwatch _stopwatch = Stopwatch.StartNew(); - private Dictionary _pointers = new(); - class PointerPoints + this[TextBlock.ForegroundProperty] = Brushes.Black; + + var status = new TextBlock() + { + HorizontalAlignment = HorizontalAlignment.Left, + VerticalAlignment = VerticalAlignment.Top, + FontSize = 12 + }; + Child = new Grid { - struct CanvasPoint + Children = { - public IBrush Brush; - public Point Point; - public double Radius; + new PointerCanvas(null, status, false), + status } + }; + } + } + + class PointerCanvas : Control + { + private readonly Slider? _slider; + private readonly TextBlock _status; + private readonly bool _drawPoints; + private int _events; + private Stopwatch _stopwatch = Stopwatch.StartNew(); + private IDisposable? _statusUpdated; + private Dictionary _pointers = new(); + private PointerPointProperties? _lastProperties; + class PointerPoints + { + struct CanvasPoint + { + public IBrush Brush; + public Point Point; + public double Radius; + public double Pressure; + } + + readonly CanvasPoint[] _points = new CanvasPoint[1000]; + int _index; - readonly CanvasPoint[] _points = new CanvasPoint[1000]; - int _index; - - public void Render(DrawingContext context) + public void Render(DrawingContext context, bool drawPoints) + { + + CanvasPoint? prev = null; + for (var c = 0; c < _points.Length; c++) { - - CanvasPoint? prev = null; - for (var c = 0; c < _points.Length; c++) + var i = (c + _index) % _points.Length; + var pt = _points[i]; + var thickness = pt.Pressure == 0 ? 1 : (pt.Pressure / 1024) * 5; + + if (drawPoints) { - var i = (c + _index) % _points.Length; - var pt = _points[i]; if (prev.HasValue && prev.Value.Brush != null && pt.Brush != null) - context.DrawLine(new Pen(Brushes.Black), prev.Value.Point, pt.Point); - prev = pt; + context.DrawLine(new Pen(Brushes.Black, thickness), prev.Value.Point, pt.Point); if (pt.Brush != null) context.DrawEllipse(pt.Brush, null, pt.Point, pt.Radius, pt.Radius); - } - - } - - void AddPoint(Point pt, IBrush brush, double radius) - { - _points[_index] = new CanvasPoint { Point = pt, Brush = brush, Radius = radius }; - _index = (_index + 1) % _points.Length; - } - - public void HandleEvent(PointerEventArgs e, Visual v) - { - e.Handled = true; - if (e.RoutedEvent == PointerPressedEvent) - AddPoint(e.GetPosition(v), Brushes.Green, 10); - else if (e.RoutedEvent == PointerReleasedEvent) - AddPoint(e.GetPosition(v), Brushes.Red, 10); else { - var pts = e.GetIntermediatePoints(v); - for (var c = 0; c < pts.Count; c++) - { - var pt = pts[c]; - AddPoint(pt.Position, c == pts.Count - 1 ? Brushes.Blue : Brushes.Black, - c == pts.Count - 1 ? 5 : 2); - } + if (prev.HasValue && prev.Value.Brush != null && pt.Brush != null) + context.DrawLine(new Pen(Brushes.Black, thickness, lineCap: PenLineCap.Round, lineJoin: PenLineJoin.Round), prev.Value.Point, pt.Point); } + prev = pt; } + } - - public PointerCanvas(Slider slider, TextBlock status) + + void AddPoint(Point pt, IBrush brush, double radius, float pressure) + { + _points[_index] = new CanvasPoint { Point = pt, Brush = brush, Radius = radius, Pressure = pressure }; + _index = (_index + 1) % _points.Length; + } + + public void HandleEvent(PointerEventArgs e, Visual v) { - _slider = slider; - _status = status; - DispatcherTimer.Run(() => + e.Handled = true; + var currentPoint = e.GetCurrentPoint(v); + if (e.RoutedEvent == PointerPressedEvent) + AddPoint(currentPoint.Position, Brushes.Green, 10, currentPoint.Properties.Pressure); + else if (e.RoutedEvent == PointerReleasedEvent) + AddPoint(currentPoint.Position, Brushes.Red, 10, currentPoint.Properties.Pressure); + else { - if (_stopwatch.Elapsed.TotalSeconds > 1) + var pts = e.GetIntermediatePoints(v); + for (var c = 0; c < pts.Count; c++) { - _status.Text = "Events per second: " + (_events / _stopwatch.Elapsed.TotalSeconds); - _stopwatch.Restart(); - _events = 0; + var pt = pts[c]; + AddPoint(pt.Position, c == pts.Count - 1 ? Brushes.Blue : Brushes.Black, + c == pts.Count - 1 ? 5 : 2, pt.Properties.Pressure); } - - return this.GetVisualRoot() != null; - }, TimeSpan.FromMilliseconds(10)); + } } + } + public PointerCanvas(Slider? slider, TextBlock status, bool drawPoints) + { + _slider = slider; + _status = status; + _drawPoints = drawPoints; + } - void HandleEvent(PointerEventArgs e) - { - _events++; - Thread.Sleep((int)_slider.Value); - InvalidateVisual(); + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); - if (e.RoutedEvent == PointerReleasedEvent && e.Pointer.Type == PointerType.Touch) + _statusUpdated = DispatcherTimer.Run(() => + { + if (_stopwatch.Elapsed.TotalSeconds > 1) { - _pointers.Remove(e.Pointer.Id); - return; + _status.Text = $@"Events per second: {(_events / _stopwatch.Elapsed.TotalSeconds)} +PointerUpdateKind: {_lastProperties?.PointerUpdateKind} +IsLeftButtonPressed: {_lastProperties?.IsLeftButtonPressed} +IsRightButtonPressed: {_lastProperties?.IsRightButtonPressed} +IsMiddleButtonPressed: {_lastProperties?.IsMiddleButtonPressed} +IsXButton1Pressed: {_lastProperties?.IsXButton1Pressed} +IsXButton2Pressed: {_lastProperties?.IsXButton2Pressed} +IsBarrelButtonPressed: {_lastProperties?.IsBarrelButtonPressed} +IsEraser: {_lastProperties?.IsEraser} +IsInverted: {_lastProperties?.IsInverted} +Pressure: {_lastProperties?.Pressure} +XTilt: {_lastProperties?.XTilt} +YTilt: {_lastProperties?.YTilt} +Twist: {_lastProperties?.Twist}"; + _stopwatch.Restart(); + _events = 0; } - if (!_pointers.TryGetValue(e.Pointer.Id, out var pt)) - _pointers[e.Pointer.Id] = pt = new PointerPoints(); - pt.HandleEvent(e, this); - - - } - - public override void Render(DrawingContext context) + return true; + }, TimeSpan.FromMilliseconds(10)); + } + + protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnDetachedFromVisualTree(e); + + _statusUpdated?.Dispose(); + } + + void HandleEvent(PointerEventArgs e) + { + _events++; + if (_slider != null) { - context.FillRectangle(Brushes.White, Bounds); - foreach(var pt in _pointers.Values) - pt.Render(context); - base.Render(context); + Thread.Sleep((int)_slider.Value); } + InvalidateVisual(); - protected override void OnPointerPressed(PointerPressedEventArgs e) + if (e.RoutedEvent == PointerReleasedEvent && e.Pointer.Type == PointerType.Touch) { - if (e.ClickCount == 2) - { - _pointers.Clear(); - InvalidateVisual(); - return; - } - - HandleEvent(e); - base.OnPointerPressed(e); + _pointers.Remove(e.Pointer.Id); + return; } + + var lastPointer = e.GetCurrentPoint(this); + _lastProperties = lastPointer.Properties; - protected override void OnPointerMoved(PointerEventArgs e) + if (e.Pointer.Type != PointerType.Pen + || lastPointer.Properties.Pressure > 0) { - HandleEvent(e); - base.OnPointerMoved(e); + if (!_pointers.TryGetValue(e.Pointer.Id, out var pt)) + _pointers[e.Pointer.Id] = pt = new PointerPoints(); + pt.HandleEvent(e, this); } + } + + public override void Render(DrawingContext context) + { + context.FillRectangle(Brushes.White, Bounds); + foreach (var pt in _pointers.Values) + pt.Render(context, _drawPoints); + base.Render(context); + } - protected override void OnPointerReleased(PointerReleasedEventArgs e) + protected override void OnPointerPressed(PointerPressedEventArgs e) + { + if (e.ClickCount == 2) { - HandleEvent(e); - base.OnPointerReleased(e); + _pointers.Clear(); + InvalidateVisual(); + return; } + + HandleEvent(e); + base.OnPointerPressed(e); + } + + protected override void OnPointerMoved(PointerEventArgs e) + { + HandleEvent(e); + base.OnPointerMoved(e); + } + + protected override void OnPointerReleased(PointerReleasedEventArgs e) + { + HandleEvent(e); + base.OnPointerReleased(e); } - } } From bef72b3477574e8db361ee22f58cba6354ce435a Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Mon, 24 Jan 2022 11:39:13 +0300 Subject: [PATCH 015/224] fix some issues by requests --- src/Avalonia.Controls/TabControl.cs | 6 +-- .../ScrollGestureRecognizer.cs | 3 +- src/Avalonia.Input/MouseDevice.cs | 39 +++++++------------ src/Avalonia.Input/PenDevice.cs | 13 +++---- 4 files changed, 24 insertions(+), 37 deletions(-) diff --git a/src/Avalonia.Controls/TabControl.cs b/src/Avalonia.Controls/TabControl.cs index 955b29f3f9..306a9d3e6a 100644 --- a/src/Avalonia.Controls/TabControl.cs +++ b/src/Avalonia.Controls/TabControl.cs @@ -211,8 +211,7 @@ namespace Avalonia.Controls { base.OnPointerPressed(e); - if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed && - (e.Pointer.Type == PointerType.Mouse || e.Pointer.Type == PointerType.Pen)) + if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed && e.Pointer.Type == PointerType.Mouse) { e.Handled = UpdateSelectionFromEventSource(e.Source); } @@ -220,8 +219,7 @@ namespace Avalonia.Controls protected override void OnPointerReleased(PointerReleasedEventArgs e) { - if (e.InitialPressMouseButton == MouseButton.Left && - (e.Pointer.Type == PointerType.Mouse || e.Pointer.Type == PointerType.Pen)) + if (e.InitialPressMouseButton == MouseButton.Left && e.Pointer.Type != PointerType.Mouse) { var container = GetContainerFromEventSource(e.Source); if (container != null diff --git a/src/Avalonia.Input/GestureRecognizers/ScrollGestureRecognizer.cs b/src/Avalonia.Input/GestureRecognizers/ScrollGestureRecognizer.cs index 1d97fdfe53..889b7e3b82 100644 --- a/src/Avalonia.Input/GestureRecognizers/ScrollGestureRecognizer.cs +++ b/src/Avalonia.Input/GestureRecognizers/ScrollGestureRecognizer.cs @@ -66,7 +66,8 @@ namespace Avalonia.Input.GestureRecognizers public void PointerPressed(PointerPressedEventArgs e) { - if (e.Pointer.IsPrimary && e.Pointer.Type == PointerType.Touch) + if (e.Pointer.IsPrimary && + (e.Pointer.Type == PointerType.Touch || e.Pointer.Type == PointerType.Pen)) { EndGesture(); _tracking = e.Pointer; diff --git a/src/Avalonia.Input/MouseDevice.cs b/src/Avalonia.Input/MouseDevice.cs index 087a806f77..38f63402ba 100644 --- a/src/Avalonia.Input/MouseDevice.cs +++ b/src/Avalonia.Input/MouseDevice.cs @@ -205,31 +205,20 @@ namespace Avalonia.Input PointerPointProperties CreateProperties(RawPointerEventArgs args) { - - var kind = PointerUpdateKind.Other; - - if (args.Type == RawPointerEventType.LeftButtonDown) - kind = PointerUpdateKind.LeftButtonPressed; - if (args.Type == RawPointerEventType.MiddleButtonDown) - kind = PointerUpdateKind.MiddleButtonPressed; - if (args.Type == RawPointerEventType.RightButtonDown) - kind = PointerUpdateKind.RightButtonPressed; - if (args.Type == RawPointerEventType.XButton1Down) - kind = PointerUpdateKind.XButton1Pressed; - if (args.Type == RawPointerEventType.XButton2Down) - kind = PointerUpdateKind.XButton2Pressed; - if (args.Type == RawPointerEventType.LeftButtonUp) - kind = PointerUpdateKind.LeftButtonReleased; - if (args.Type == RawPointerEventType.MiddleButtonUp) - kind = PointerUpdateKind.MiddleButtonReleased; - if (args.Type == RawPointerEventType.RightButtonUp) - kind = PointerUpdateKind.RightButtonReleased; - if (args.Type == RawPointerEventType.XButton1Up) - kind = PointerUpdateKind.XButton1Released; - if (args.Type == RawPointerEventType.XButton2Up) - kind = PointerUpdateKind.XButton2Released; - - return new PointerPointProperties(args.InputModifiers, kind); + return new PointerPointProperties(args.InputModifiers, args.Type switch + { + RawPointerEventType.LeftButtonDown => PointerUpdateKind.LeftButtonPressed, + RawPointerEventType.MiddleButtonDown => PointerUpdateKind.MiddleButtonPressed, + RawPointerEventType.RightButtonDown => PointerUpdateKind.RightButtonPressed, + RawPointerEventType.XButton1Down => PointerUpdateKind.XButton1Pressed, + RawPointerEventType.XButton2Down => PointerUpdateKind.XButton2Pressed, + RawPointerEventType.LeftButtonUp => PointerUpdateKind.LeftButtonReleased, + RawPointerEventType.MiddleButtonUp => PointerUpdateKind.MiddleButtonReleased, + RawPointerEventType.RightButtonUp => PointerUpdateKind.RightButtonReleased, + RawPointerEventType.XButton1Up => PointerUpdateKind.XButton1Released, + RawPointerEventType.XButton2Up => PointerUpdateKind.XButton2Released, + _ => PointerUpdateKind.Other, + }); } private MouseButton _lastMouseDownButton; diff --git a/src/Avalonia.Input/PenDevice.cs b/src/Avalonia.Input/PenDevice.cs index aa4f89bb85..0fef462831 100644 --- a/src/Avalonia.Input/PenDevice.cs +++ b/src/Avalonia.Input/PenDevice.cs @@ -160,13 +160,12 @@ namespace Avalonia.Input private PointerPointProperties CreateProperties(RawPointerEventArgs args) { - var kind = PointerUpdateKind.Other; - - if (args.Type == RawPointerEventType.LeftButtonDown) - kind = PointerUpdateKind.LeftButtonPressed; - if (args.Type == RawPointerEventType.LeftButtonUp) - kind = PointerUpdateKind.LeftButtonReleased; - + var kind = args.Type switch + { + RawPointerEventType.LeftButtonDown => PointerUpdateKind.LeftButtonPressed, + RawPointerEventType.LeftButtonUp => PointerUpdateKind.LeftButtonReleased, + _ => PointerUpdateKind.Other, + }; return new PointerPointProperties(args.InputModifiers, kind, Twist, Pressure, XTilt, YTilt, IsEraser, IsInverted, IsBarrel); } From dc9fc4ab146432c1b07e0af563487c6b53a7a47d Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Mon, 24 Jan 2022 12:01:30 +0300 Subject: [PATCH 016/224] Handle Pointer History. Put into intermediate points. --- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 52981ac7ec..7b5e42e490 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -402,6 +402,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERUPDATE: { GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); + Point[]? intermediatePoints = null; if (info.historyCount > 1) { @@ -456,6 +457,23 @@ namespace Avalonia.Win32 } } } + else + { + var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); + var historyCount = (int)info.historyCount; + var historyInfos = new POINTER_INFO[historyCount]; + if (GetPointerInfoHistory(pointerId, ref historyCount, historyInfos)) + { + //last info is the same as the current so skip it + intermediatePoints = new Point[historyCount - 1]; + for (int i = 0;i < historyCount - 1; i++) + { + var historyInfo = historyInfos[i]; + intermediatePoints[i] = PointToClient(new PixelPoint( + historyInfo.ptPixelLocationX, historyInfo.ptPixelLocationY)); + } + } + } } var eventType = GetEventType(message, info); @@ -468,11 +486,17 @@ namespace Avalonia.Win32 { break; } - e = new RawTouchEventArgs(_touchDevice, timestamp, _owner, eventType, point, modifiers, info.pointerId); + e = new RawTouchEventArgs(_touchDevice, timestamp, _owner, eventType, point, modifiers, info.pointerId) + { + IntermediatePoints = intermediatePoints + }; } else { - e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers); + e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers) + { + IntermediatePoints = intermediatePoints + }; } break; } From f04adadc89f04ba881c0c3811786706038059b7e Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Mon, 24 Jan 2022 12:52:58 +0300 Subject: [PATCH 017/224] Release mouse capture if pen is close to digitizer. Release pen capture if pen is out of range of digitizer. --- src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 7b5e42e490..dd961aec48 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -381,13 +381,18 @@ namespace Avalonia.Win32 break; } case WindowsMessage.WM_POINTERDEVICEINRANGE: - case WindowsMessage.WM_POINTERDEVICEOUTOFRANGE: { + _mouseDevice.Capture(null); //notifies about proximity of pointer device to the digitizer. //contains pointer id and proximity. //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-pointerdeviceinrange break; } + case WindowsMessage.WM_POINTERDEVICEOUTOFRANGE: + { + _penDevice.Capture(null); + break; + } case WindowsMessage.WM_NCPOINTERUPDATE: { //NC stands for non-client area - window header and window border @@ -545,6 +550,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERCAPTURECHANGED: { _mouseDevice.Capture(null); + _penDevice.Capture(null); return IntPtr.Zero; } case WindowsMessage.DM_POINTERHITTEST: From 903685839645adf7aa55e718372b26797c75c25f Mon Sep 17 00:00:00 2001 From: Sergey Mikolaytis Date: Mon, 24 Jan 2022 17:02:22 +0300 Subject: [PATCH 018/224] fix MK_NONE --- src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index d71efcb074..0431808a43 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -225,7 +225,7 @@ namespace Avalonia.Win32.Interop [Flags] public enum ModifierKeys { - MK_NONE = 0x0001, + MK_NONE = 0x0000, MK_LBUTTON = 0x0001, MK_RBUTTON = 0x0002, From 47a328ab878aa61e09c588310c42b4d8fc23f97d Mon Sep 17 00:00:00 2001 From: Nathan Garside Date: Thu, 27 Jan 2022 15:04:44 +0000 Subject: [PATCH 019/224] Add window position offset --- src/Windows/Avalonia.Win32/WindowImpl.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index e4f5268285..9c4037be92 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -448,10 +448,20 @@ namespace Avalonia.Win32 { GetWindowRect(_hwnd, out var rc); + // Windows 10 and 11 add a 7 pixel invisible border on the left/right/bottom of windows for resizing + if (Win32Platform.WindowsVersion.Major >= 10 && HasFullDecorations) + { + return new PixelPoint(rc.left + (int)(7 * _scaling), rc.top); + } + return new PixelPoint(rc.left, rc.top); } set { + if (Win32Platform.WindowsVersion.Major >= 10 && HasFullDecorations) + { + value = new PixelPoint(value.X - (int)(7 * _scaling), value.Y); + } SetWindowPos( Handle.Handle, IntPtr.Zero, From 1ae26b326e503527e6ff4615cd9399d5c950e696 Mon Sep 17 00:00:00 2001 From: Nathan Garside Date: Sat, 29 Jan 2022 12:03:01 +0000 Subject: [PATCH 020/224] Calculate border size --- src/Windows/Avalonia.Win32/WindowImpl.cs | 34 ++++++++++++++++-------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index 9c4037be92..94fe9168ab 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -448,20 +448,14 @@ namespace Avalonia.Win32 { GetWindowRect(_hwnd, out var rc); - // Windows 10 and 11 add a 7 pixel invisible border on the left/right/bottom of windows for resizing - if (Win32Platform.WindowsVersion.Major >= 10 && HasFullDecorations) - { - return new PixelPoint(rc.left + (int)(7 * _scaling), rc.top); - } - - return new PixelPoint(rc.left, rc.top); + var border = HiddenBorderSize; + return new PixelPoint(rc.left + border.Width, rc.top + border.Height); } set { - if (Win32Platform.WindowsVersion.Major >= 10 && HasFullDecorations) - { - value = new PixelPoint(value.X - (int)(7 * _scaling), value.Y); - } + var border = HiddenBorderSize; + value = new PixelPoint(value.X - border.Width, value.Y - border.Height); + SetWindowPos( Handle.Handle, IntPtr.Zero, @@ -475,6 +469,24 @@ namespace Avalonia.Win32 private bool HasFullDecorations => _windowProperties.Decorations == SystemDecorations.Full; + private PixelSize HiddenBorderSize + { + get + { + // Windows 10 and 11 add a 7 pixel invisible border on the left/right/bottom of windows for resizing + if (Win32Platform.WindowsVersion.Major < 10 || !HasFullDecorations) + { + return PixelSize.Empty; + } + + DwmGetWindowAttribute(_hwnd, (int)DwmWindowAttribute.DWMWA_EXTENDED_FRAME_BOUNDS, out var clientRect, Marshal.SizeOf(typeof(RECT))); + GetWindowRect(_hwnd, out var frameRect); + var borderWidth = GetSystemMetrics(SystemMetric.SM_CXBORDER); + + return new PixelSize(clientRect.left - frameRect.left - borderWidth, 0); + } + } + public void Move(PixelPoint point) => Position = point; public void SetMinMaxSize(Size minSize, Size maxSize) From c646343beee6da4f8568751ffd56bdd2da1f92ff Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sun, 20 Mar 2022 23:54:04 -0400 Subject: [PATCH 021/224] WIP on intermediate points win --- src/Avalonia.Input/PointerEventArgs.cs | 3 +- src/Avalonia.Input/PointerPoint.cs | 17 +++ src/Avalonia.Input/Raw/RawPointerEventArgs.cs | 10 +- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 141 +++++++++++------- 4 files changed, 112 insertions(+), 59 deletions(-) diff --git a/src/Avalonia.Input/PointerEventArgs.cs b/src/Avalonia.Input/PointerEventArgs.cs index 0604d09dc4..a30c1c51dc 100644 --- a/src/Avalonia.Input/PointerEventArgs.cs +++ b/src/Avalonia.Input/PointerEventArgs.cs @@ -128,7 +128,8 @@ namespace Avalonia.Input for (var c = 0; c < previousPoints.Count; c++) { var pt = previousPoints[c]; - points[c] = new PointerPoint(Pointer, GetPosition(pt.Position, relativeTo), _properties); + var pointProperties = new PointerPointProperties(_properties, pt); + points[c] = new PointerPoint(Pointer, GetPosition(pt.Position, relativeTo), pointProperties); } points[points.Length - 1] = GetCurrentPoint(relativeTo); diff --git a/src/Avalonia.Input/PointerPoint.cs b/src/Avalonia.Input/PointerPoint.cs index 728bb1c579..ba69add7d8 100644 --- a/src/Avalonia.Input/PointerPoint.cs +++ b/src/Avalonia.Input/PointerPoint.cs @@ -84,6 +84,23 @@ namespace Avalonia.Input IsBarrelButtonPressed = isBarrel; } + internal PointerPointProperties(PointerPointProperties basedOn, Raw.RawPointerPoint rawPoint) + { + IsLeftButtonPressed = basedOn.IsLeftButtonPressed; + IsMiddleButtonPressed = basedOn.IsMiddleButtonPressed; + IsRightButtonPressed = basedOn.IsRightButtonPressed; + IsXButton1Pressed = basedOn.IsXButton1Pressed; + IsXButton2Pressed = basedOn.IsXButton2Pressed; + IsInverted = basedOn.IsInverted; + IsEraser = basedOn.IsEraser; + IsBarrelButtonPressed = basedOn.IsBarrelButtonPressed; + + Twist = rawPoint.Twist; + Pressure = rawPoint.Pressure; + XTilt = rawPoint.XTilt; + YTilt = rawPoint.YTilt; + } + public static PointerPointProperties None { get; } = new PointerPointProperties(); } diff --git a/src/Avalonia.Input/Raw/RawPointerEventArgs.cs b/src/Avalonia.Input/Raw/RawPointerEventArgs.cs index c157fa059c..cd99cdc23b 100644 --- a/src/Avalonia.Input/Raw/RawPointerEventArgs.cs +++ b/src/Avalonia.Input/Raw/RawPointerEventArgs.cs @@ -128,10 +128,16 @@ namespace Avalonia.Input.Raw /// Pointer position, in client DIPs. /// public Point Position { get; set; } - + + public float Twist { get; set; } + public float Pressure { get; set; } + public float XTilt { get; set; } + public float YTilt { get; set; } + + public RawPointerPoint() { - Position = default; + this = default; } } } diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 1ef3cbaba2..5c17caa13b 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -1,4 +1,6 @@ using System; +using System.Buffers; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using System.Text; @@ -413,78 +415,105 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERUPDATE: { GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); - Point[]? intermediatePoints = null; + if (info.pointerType == PointerInputType.PT_TOUCH + && ShouldIgnoreTouchEmulatedMessage()) + { + break; + } + var historyCount = (int)info.historyCount; + Lazy> intermediatePoints = null; if (info.historyCount > 1) { - if (info.pointerType == PointerInputType.PT_TOUCH) + intermediatePoints = new Lazy>(() => { - if (ShouldIgnoreTouchEmulatedMessage()) + var list = new List(historyCount - 1); + if (info.pointerType == PointerInputType.PT_TOUCH) { - break; - } - - var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); - var historyCount = (int)info.historyCount; - var historyTouchInfos = new POINTER_TOUCH_INFO[historyCount]; - if (GetPointerTouchInfoHistory(pointerId, ref historyCount, historyTouchInfos)) - { - //last info is the same as the current so skip it - for (int i = 0; i < historyCount - 1; i++) + var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); + var historyTouchInfos = ArrayPool.Shared.Rent(historyCount); + try + { + if (GetPointerTouchInfoHistory(pointerId, ref historyCount, historyTouchInfos)) + { + //last info is the same as the current so skip it + for (int i = 0; i < historyCount - 1; i++) + { + var historyTouchInfo = historyTouchInfos[i]; + var historyInfo = historyTouchInfo.pointerInfo; + var historyPoint = PointToClient(new PixelPoint( + historyInfo.ptPixelLocationX, historyInfo.ptPixelLocationY)); + list.Add(new RawPointerPoint + { + Position = historyPoint, + }); + } + } + } + finally { - var historyTouchInfo = historyTouchInfos[i]; - var historyInfo = historyTouchInfo.pointerInfo; - var historyEventType = GetEventType(message, historyInfo); - var historyPoint = PointToClient(new PixelPoint( - historyInfo.ptPixelLocationX, historyInfo.ptPixelLocationY)); - var historyModifiers = GetInputModifiers(historyInfo.dwKeyStates); - var historyTimestamp = historyInfo.dwTime == 0 ? timestamp : historyInfo.dwTime; - Input?.Invoke(new RawTouchEventArgs(_touchDevice, historyTimestamp, _owner, - historyEventType, historyPoint, historyModifiers, historyInfo.pointerId)); + ArrayPool.Shared.Return(historyTouchInfos); } } - } - else if (info.pointerType == PointerInputType.PT_PEN) - { - var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); - var historyCount = (int)info.historyCount; - var historyPenInfos = new POINTER_PEN_INFO[historyCount]; - if (GetPointerPenInfoHistory(pointerId, ref historyCount, historyPenInfos)) + else if (info.pointerType == PointerInputType.PT_PEN) { - //last info is the same as the current so skip it - for (int i = 0; i < historyCount - 1; i++) + var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); + var historyPenInfos = ArrayPool.Shared.Rent(historyCount); + try { - var historyPenInfo = historyPenInfos[i]; - var historyInfo = historyPenInfo.pointerInfo; - var historyEventType = GetEventType(message, historyInfo); - var historyPoint = PointToClient(new PixelPoint( - historyInfo.ptPixelLocationX, historyInfo.ptPixelLocationY)); - var historyModifiers = GetInputModifiers(historyInfo.dwKeyStates); - var historyTimestamp = historyInfo.dwTime == 0 ? timestamp : historyInfo.dwTime; - - ApplyPenInfo(historyPenInfo); - Input?.Invoke(new RawPointerEventArgs(_penDevice, historyTimestamp, _owner, - historyEventType, historyPoint, historyModifiers)); + if (GetPointerPenInfoHistory(pointerId, ref historyCount, historyPenInfos)) + { + //last info is the same as the current so skip it + for (int i = 0; i < historyCount - 1; i++) + { + var historyPenInfo = historyPenInfos[i]; + var historyInfo = historyPenInfo.pointerInfo; + var historyPoint = PointToClient(new PixelPoint( + historyInfo.ptPixelLocationX, historyInfo.ptPixelLocationY)); + list.Add(new RawPointerPoint + { + Position = historyPoint, + Pressure = historyPenInfo.pressure, + Twist = historyPenInfo.rotation, + XTilt = historyPenInfo.tiltX, + YTilt = historyPenInfo.tiltX + }); + } + } + } + finally + { + ArrayPool.Shared.Return(historyPenInfos); } } - } - else - { - var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); - var historyCount = (int)info.historyCount; - var historyInfos = new POINTER_INFO[historyCount]; - if (GetPointerInfoHistory(pointerId, ref historyCount, historyInfos)) + else { - //last info is the same as the current so skip it - intermediatePoints = new Point[historyCount - 1]; - for (int i = 0;i < historyCount - 1; i++) + var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); + var historyInfos = ArrayPool.Shared.Rent(historyCount); + try + { + if (GetPointerInfoHistory(pointerId, ref historyCount, historyInfos)) + { + //last info is the same as the current so skip it + for (int i = 0; i < historyCount - 1; i++) + { + var historyInfo = historyInfos[i]; + var historyPoint = PointToClient(new PixelPoint( + historyInfo.ptPixelLocationX, historyInfo.ptPixelLocationY)); + list.Add(new RawPointerPoint + { + Position = historyPoint + }); + } + } + } + finally { - var historyInfo = historyInfos[i]; - intermediatePoints[i] = PointToClient(new PixelPoint( - historyInfo.ptPixelLocationX, historyInfo.ptPixelLocationY)); + ArrayPool.Shared.Return(historyInfos); } } - } + return list; + }); } var eventType = GetEventType(message, info); From d8f7dc5d1f2200a114404f9ed9cc7f34a58d0091 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Thu, 24 Mar 2022 20:28:56 -0400 Subject: [PATCH 022/224] Make EnableWmPointerEvents optional --- src/Windows/Avalonia.Win32/Win32Platform.cs | 9 +++ .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 66 +++++++++++++------ src/Windows/Avalonia.Win32/WindowImpl.cs | 14 ++-- 3 files changed, 61 insertions(+), 28 deletions(-) diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index 5cfbab40e4..25a6717122 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -63,8 +63,17 @@ namespace Avalonia /// /// Multitouch allows a surface (a touchpad or touchscreen) to recognize the presence of more than one point of contact with the surface at the same time. /// + [Obsolete("Multitouch is always enabled")] public bool? EnableMultitouch { get; set; } = true; + /// + /// Enables Win8+ WM_POINTER events support. The default value is false. + /// + /// + /// Required for extended Pen and Touch support. + /// + public bool? EnableWmPointerEvents { get; set; } = false; + /// /// Embeds popups to the window when set to true. The default value is false. /// diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 5c17caa13b..d52115425f 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -174,7 +174,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MBUTTONDOWN: case WindowsMessage.WM_XBUTTONDOWN: { - if (Win8Plus) + if (_wmPointerEnabled) { break; } @@ -207,7 +207,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MBUTTONUP: case WindowsMessage.WM_XBUTTONUP: { - if (Win8Plus) + if (_wmPointerEnabled) { break; } @@ -240,7 +240,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MOUSEMOVE: { - if (Win8Plus) + if (_wmPointerEnabled) { break; } @@ -275,7 +275,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MOUSEWHEEL: { - if (Win8Plus) + if (_wmPointerEnabled) { break; } @@ -291,7 +291,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MOUSEHWHEEL: { - if (Win8Plus) + if (_wmPointerEnabled) { break; } @@ -307,7 +307,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_MOUSELEAVE: { - if (Win8Plus) + if (_wmPointerEnabled) { break; } @@ -327,7 +327,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_NCMBUTTONDOWN: case WindowsMessage.WM_NCXBUTTONDOWN: { - if (Win8Plus) + if (_wmPointerEnabled) { break; } @@ -351,7 +351,7 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_TOUCH: { - if (Win8Plus) + if (_wmPointerEnabled) { break; } @@ -384,12 +384,20 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_POINTERDEVICECHANGE: { + if (!_wmPointerEnabled) + { + break; + } //notifies about changes in the settings of a monitor that has a digitizer attached to it. //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-pointerdevicechange break; } case WindowsMessage.WM_POINTERDEVICEINRANGE: { + if (!_wmPointerEnabled) + { + break; + } _mouseDevice.Capture(null); //notifies about proximity of pointer device to the digitizer. //contains pointer id and proximity. @@ -398,11 +406,19 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_POINTERDEVICEOUTOFRANGE: { + if (!_wmPointerEnabled) + { + break; + } _penDevice.Capture(null); break; } case WindowsMessage.WM_NCPOINTERUPDATE: { + if (!_wmPointerEnabled) + { + break; + } //NC stands for non-client area - window header and window border //As I found above in an old message handling - we dont need to handle NC pointer move/updates. //All we need is pointer down and up. So this is skipped for now. @@ -415,8 +431,7 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERUPDATE: { GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); - if (info.pointerType == PointerInputType.PT_TOUCH - && ShouldIgnoreTouchEmulatedMessage()) + if (!_wmPointerEnabled) { break; } @@ -522,10 +537,6 @@ namespace Avalonia.Win32 if (device is TouchDevice) { - if (ShouldIgnoreTouchEmulatedMessage()) - { - break; - } e = new RawTouchEventArgs(_touchDevice, timestamp, _owner, eventType, point, modifiers, info.pointerId) { IntermediatePoints = intermediatePoints @@ -542,6 +553,10 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_POINTERENTER: { + if (!_wmPointerEnabled) + { + break; + } //this is not handled by WM_MOUSEENTER so I think there is no need to handle this too. //but we can detect a new pointer by this message and calling IS_POINTER_NEW_WPARAM @@ -552,6 +567,10 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERLEAVE: { GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); + if (!_wmPointerEnabled) + { + break; + } if (device is TouchDevice) { break; @@ -567,6 +586,10 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERHWHEEL: { GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); + if (!_wmPointerEnabled) + { + break; + } var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); var modifiers = GetInputModifiers(info.dwKeyStates); @@ -577,6 +600,10 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_POINTERACTIVATE: { + if (!_wmPointerEnabled) + { + break; + } //occurs when a pointer activates an inactive window. //we should handle this and return PA_ACTIVATE or PA_NOACTIVATE //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-pointeractivate @@ -584,6 +611,10 @@ namespace Avalonia.Win32 } case WindowsMessage.WM_POINTERCAPTURECHANGED: { + if (!_wmPointerEnabled) + { + break; + } _mouseDevice.Capture(null); _penDevice.Capture(null); return IntPtr.Zero; @@ -913,8 +944,6 @@ namespace Avalonia.Win32 }; } - public readonly bool Win8Plus = Win32Platform.WindowsVersion >= PlatformConstants.Windows8; - private void UpdateInputMethod(IntPtr hkl) { // note: for non-ime language, also create it so that emoji panel tracks cursor @@ -951,10 +980,7 @@ namespace Avalonia.Win32 private bool ShouldIgnoreTouchEmulatedMessage() { - if (!_multitouch) - { - return false; - } + // Note: GetMessageExtraInfo doesn't work with WM_POINTER events. // MI_WP_SIGNATURE // https://docs.microsoft.com/en-us/windows/win32/tablet/system-events-and-mouse-messages diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index becbbe7561..53dde352f7 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -75,7 +75,7 @@ namespace Avalonia.Win32 private WndProc _wndProcDelegate; private string _className; private IntPtr _hwnd; - private bool _multitouch; + private bool _wmPointerEnabled; private IInputRoot _owner; private WindowProperties _windowProperties; private bool _trackingMouse;//ToDo - there is something missed. Needs investigation @Steven Kirk @@ -128,7 +128,10 @@ namespace Avalonia.Win32 egl.Display is AngleWin32EglDisplay angleDisplay && angleDisplay.PlatformApi == AngleOptions.PlatformApi.DirectX11; - if (Win8Plus && !IsMouseInPointerEnabled()) + _wmPointerEnabled = Win32Platform.Options.EnableWmPointerEvents + ?? Win32Platform.WindowsVersion >= PlatformConstants.Windows8; + + if (_wmPointerEnabled && !IsMouseInPointerEnabled()) { EnableMouseInPointer(true); } @@ -795,12 +798,7 @@ namespace Avalonia.Win32 Handle = new WindowImplPlatformHandle(this); - _multitouch = Win32Platform.Options.EnableMultitouch ?? true; - - if (_multitouch) - { - RegisterTouchWindow(_hwnd, 0); - } + RegisterTouchWindow(_hwnd, 0); if (ShCoreAvailable && Win32Platform.WindowsVersion > PlatformConstants.Windows8) { From b599f295f6b94d01bec4d267bc6759da1d69833c Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sat, 16 Apr 2022 01:10:45 -0400 Subject: [PATCH 023/224] Add RawPointerId to the RawPointerEventArgs --- src/Avalonia.Input/Raw/RawPointerEventArgs.cs | 5 ++++ src/Avalonia.Input/Raw/RawTouchEventArgs.cs | 17 ++++++++++--- src/Avalonia.Input/TouchDevice.cs | 13 +++++----- src/Shared/RawEventGrouping.cs | 12 ++++------ .../TouchDeviceTests.cs | 24 ++++++++++++------- 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/Avalonia.Input/Raw/RawPointerEventArgs.cs b/src/Avalonia.Input/Raw/RawPointerEventArgs.cs index 58ea076379..1faa20fbf1 100644 --- a/src/Avalonia.Input/Raw/RawPointerEventArgs.cs +++ b/src/Avalonia.Input/Raw/RawPointerEventArgs.cs @@ -87,6 +87,11 @@ namespace Avalonia.Input.Raw InputModifiers = inputModifiers; } + /// + /// Gets the raw pointer identifier. + /// + public long RawPointerId { get; set; } + /// /// Gets the pointer properties and position, in client DIPs. /// diff --git a/src/Avalonia.Input/Raw/RawTouchEventArgs.cs b/src/Avalonia.Input/Raw/RawTouchEventArgs.cs index 020b40e55b..6706a45f48 100644 --- a/src/Avalonia.Input/Raw/RawTouchEventArgs.cs +++ b/src/Avalonia.Input/Raw/RawTouchEventArgs.cs @@ -1,15 +1,26 @@ +using System; + namespace Avalonia.Input.Raw { public class RawTouchEventArgs : RawPointerEventArgs { public RawTouchEventArgs(IInputDevice device, ulong timestamp, IInputRoot root, RawPointerEventType type, Point position, RawInputModifiers inputModifiers, - long touchPointId) + long rawPointerId) : base(device, timestamp, root, type, position, inputModifiers) { - TouchPointId = touchPointId; + RawPointerId = rawPointerId; + } + + public RawTouchEventArgs(IInputDevice device, ulong timestamp, IInputRoot root, + RawPointerEventType type, RawPointerPoint point, RawInputModifiers inputModifiers, + long rawPointerId) + : base(device, timestamp, root, type, point, inputModifiers) + { + RawPointerId = rawPointerId; } - public long TouchPointId { get; set; } + [Obsolete("Use RawPointerId")] + public long TouchPointId { get => RawPointerId; set => RawPointerId = value; } } } diff --git a/src/Avalonia.Input/TouchDevice.cs b/src/Avalonia.Input/TouchDevice.cs index 54dcc4051e..e914d860fd 100644 --- a/src/Avalonia.Input/TouchDevice.cs +++ b/src/Avalonia.Input/TouchDevice.cs @@ -40,14 +40,14 @@ namespace Avalonia.Input { if (ev.Handled || _disposed) return; - var args = (RawTouchEventArgs)ev; - if (!_pointers.TryGetValue(args.TouchPointId, out var pointer)) + var args = (RawPointerEventArgs)ev; + if (!_pointers.TryGetValue(args.RawPointerId, out var pointer)) { if (args.Type == RawPointerEventType.TouchEnd) return; var hit = args.InputHitTestResult; - _pointers[args.TouchPointId] = pointer = new Pointer(Pointer.GetNextFreeId(), + _pointers[args.RawPointerId] = pointer = new Pointer(Pointer.GetNextFreeId(), PointerType.Touch, _pointers.Count == 0); pointer.Capture(hit); } @@ -88,7 +88,7 @@ namespace Avalonia.Input if (args.Type == RawPointerEventType.TouchEnd) { - _pointers.Remove(args.TouchPointId); + _pointers.Remove(args.RawPointerId); using (pointer) { target.RaiseEvent(new PointerReleasedEventArgs(target, pointer, @@ -101,7 +101,7 @@ namespace Avalonia.Input if (args.Type == RawPointerEventType.TouchCancel) { - _pointers.Remove(args.TouchPointId); + _pointers.Remove(args.RawPointerId); using (pointer) pointer.Capture(null); _lastPointer = null; @@ -129,8 +129,7 @@ namespace Avalonia.Input public IPointer? TryGetPointer(RawPointerEventArgs ev) { - return ev is RawTouchEventArgs args - && _pointers.TryGetValue(args.TouchPointId, out var pointer) + return _pointers.TryGetValue(ev.RawPointerId, out var pointer) ? pointer : null; } diff --git a/src/Shared/RawEventGrouping.cs b/src/Shared/RawEventGrouping.cs index 084593ffc6..966744888c 100644 --- a/src/Shared/RawEventGrouping.cs +++ b/src/Shared/RawEventGrouping.cs @@ -2,10 +2,8 @@ using System; using System.Collections.Generic; using Avalonia.Collections.Pooled; -using Avalonia.Input; using Avalonia.Input.Raw; using Avalonia.Threading; -using JetBrains.Annotations; namespace Avalonia; @@ -19,7 +17,7 @@ internal class RawEventGrouper : IDisposable private readonly Action _eventCallback; private readonly Queue _inputQueue = new(); private readonly Action _dispatchFromQueue; - readonly Dictionary _lastTouchPoints = new(); + readonly Dictionary _lastTouchPoints = new(); RawInputEventArgs? _lastEvent; public RawEventGrouper(Action eventCallback) @@ -49,7 +47,7 @@ internal class RawEventGrouper : IDisposable _lastEvent = null; if (ev is RawTouchEventArgs { Type: RawPointerEventType.TouchUpdate } touchUpdate) - _lastTouchPoints.Remove(touchUpdate.TouchPointId); + _lastTouchPoints.Remove(touchUpdate.RawPointerId); _eventCallback?.Invoke(ev); @@ -88,11 +86,11 @@ internal class RawEventGrouper : IDisposable { if (args is RawTouchEventArgs touchEvent) { - if (_lastTouchPoints.TryGetValue(touchEvent.TouchPointId, out var lastTouchEvent)) + if (_lastTouchPoints.TryGetValue(touchEvent.RawPointerId, out var lastTouchEvent)) MergeEvents(lastTouchEvent, touchEvent); else { - _lastTouchPoints[touchEvent.TouchPointId] = touchEvent; + _lastTouchPoints[touchEvent.RawPointerId] = touchEvent; AddToQueue(touchEvent); } } @@ -105,7 +103,7 @@ internal class RawEventGrouper : IDisposable { _lastTouchPoints.Clear(); if (args is RawTouchEventArgs { Type: RawPointerEventType.TouchUpdate } touchEvent) - _lastTouchPoints[touchEvent.TouchPointId] = touchEvent; + _lastTouchPoints[touchEvent.RawPointerId] = touchEvent; } AddToQueue(args); } diff --git a/tests/Avalonia.Input.UnitTests/TouchDeviceTests.cs b/tests/Avalonia.Input.UnitTests/TouchDeviceTests.cs index 80c5a45c1a..7b7d547346 100644 --- a/tests/Avalonia.Input.UnitTests/TouchDeviceTests.cs +++ b/tests/Avalonia.Input.UnitTests/TouchDeviceTests.cs @@ -219,30 +219,36 @@ namespace Avalonia.Input.UnitTests { for (int i = 0; i < touchPointIds.Length; i++) { - inputManager.ProcessInput(new RawTouchEventArgs(device, 0, + inputManager.ProcessInput(new RawPointerEventArgs(device, 0, root, type, new Point(0, 0), - RawInputModifiers.None, - touchPointIds[i])); + RawInputModifiers.None) + { + RawPointerId = touchPointIds[i] + }); } } private static void TapOnce(IInputManager inputManager, TouchDevice device, IInputRoot root, ulong timestamp = 0, long touchPointId = 0) { - inputManager.ProcessInput(new RawTouchEventArgs(device, timestamp, + inputManager.ProcessInput(new RawPointerEventArgs(device, timestamp, root, RawPointerEventType.TouchBegin, new Point(0, 0), - RawInputModifiers.None, - touchPointId)); - inputManager.ProcessInput(new RawTouchEventArgs(device, timestamp, + RawInputModifiers.None) + { + RawPointerId = touchPointId + }); + inputManager.ProcessInput(new RawPointerEventArgs(device, timestamp, root, RawPointerEventType.TouchEnd, new Point(0, 0), - RawInputModifiers.None, - touchPointId)); + RawInputModifiers.None) + { + RawPointerId = touchPointId + }); } } } From afb828d4ff3aac67f122b2badd6d5c7399c9081b Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sat, 16 Apr 2022 04:00:29 -0400 Subject: [PATCH 024/224] Move IntermediatePoints creation and update RawInputModifiers --- src/Avalonia.Input/IKeyboardDevice.cs | 7 +- src/Avalonia.Input/PenDevice.cs | 404 ++++-------------- src/Avalonia.Input/PointerPoint.cs | 12 +- src/Avalonia.Input/Raw/RawPointerEventArgs.cs | 4 +- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 379 ++++++++-------- src/Windows/Avalonia.Win32/WindowImpl.cs | 7 + 6 files changed, 278 insertions(+), 535 deletions(-) diff --git a/src/Avalonia.Input/IKeyboardDevice.cs b/src/Avalonia.Input/IKeyboardDevice.cs index d0e84e5ad0..1b9a056272 100644 --- a/src/Avalonia.Input/IKeyboardDevice.cs +++ b/src/Avalonia.Input/IKeyboardDevice.cs @@ -42,12 +42,17 @@ namespace Avalonia.Input Control = 2, Shift = 4, Meta = 8, + LeftMouseButton = 16, RightMouseButton = 32, MiddleMouseButton = 64, XButton1MouseButton = 128, XButton2MouseButton = 256, - KeyboardMask = Alt | Control | Shift | Meta + KeyboardMask = Alt | Control | Shift | Meta, + + PenInverted = 512, + PenEraser = 1024, + PenBarrelButton = 2048 } public interface IKeyboardDevice : IInputDevice, INotifyPropertyChanged diff --git a/src/Avalonia.Input/PenDevice.cs b/src/Avalonia.Input/PenDevice.cs index 0fef462831..d22b48562c 100644 --- a/src/Avalonia.Input/PenDevice.cs +++ b/src/Avalonia.Input/PenDevice.cs @@ -1,8 +1,8 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; using Avalonia.Input.Raw; -using Avalonia.Interactivity; using Avalonia.Platform; using Avalonia.VisualTree; @@ -13,72 +13,14 @@ namespace Avalonia.Input /// public class PenDevice : IPenDevice, IDisposable { + private readonly Dictionary _pointers = new(); + private readonly Dictionary _lastPositions = new(); private int _clickCount; private Rect _lastClickRect; private ulong _lastClickTime; + private MouseButton _lastMouseDownButton; - private readonly Pointer _pointer; private bool _disposed; - private PixelPoint? _position; - - public PenDevice(Pointer? pointer = null) - { - _pointer = pointer ?? new Pointer(Pointer.GetNextFreeId(), PointerType.Pen, true); - } - - /// - /// Gets the control that is currently capturing by the mouse, if any. - /// - /// - /// When an element captures the mouse, it receives mouse input whether the cursor is - /// within the control's bounds or not. To set the mouse capture, call the - /// method. - /// - [Obsolete("Use IPointer instead")] - public IInputElement? Captured => _pointer.Captured; - - public bool IsEraser { get; set; } - public bool IsInverted { get; set; } - public bool IsBarrel { get; set; } - public int XTilt { get; set; } - public int YTilt { get; set; } - public uint Pressure { get; set; } - public uint Twist { get; set; } - - /// - /// Captures mouse input to the specified control. - /// - /// The control. - /// - /// When an element captures the mouse, it receives mouse input whether the cursor is - /// within the control's bounds or not. The current mouse capture control is exposed - /// by the property. - /// - public void Capture(IInputElement? control) - { - _pointer.Capture(control); - } - - /// - /// Gets the mouse position relative to a control. - /// - /// The control. - /// The mouse position in the control's coordinates. - public Point GetPosition(IVisual relativeTo) - { - relativeTo = relativeTo ?? throw new ArgumentNullException(nameof(relativeTo)); - - if (relativeTo.VisualRoot == null) - { - throw new InvalidOperationException("Control is not attached to visual tree."); - } - -#pragma warning disable CS0618 // Type or member is obsolete - var rootPoint = relativeTo.VisualRoot.PointToClient(_position ?? new PixelPoint(-1, -1)); -#pragma warning restore CS0618 // Type or member is obsolete - var transform = relativeTo.VisualRoot.TransformToVisual(relativeTo); - return rootPoint * transform!.Value; - } public void ProcessRawEvent(RawInputEventArgs e) { @@ -86,151 +28,93 @@ namespace Avalonia.Input ProcessRawEvent(margs); } - public void TopLevelClosed(IInputRoot root) + private void ProcessRawEvent(RawPointerEventArgs e) { - ClearPointerOver(this, 0, root, PointerPointProperties.None, KeyModifiers.None); - } + e = e ?? throw new ArgumentNullException(nameof(e)); - public void SceneInvalidated(IInputRoot root, Rect rect) - { - // Pointer is outside of the target area - if (_position == null ) + if (!_pointers.TryGetValue(e.RawPointerId, out var pointer)) { - if (root.PointerOverElement != null) - ClearPointerOver(this, 0, root, PointerPointProperties.None, KeyModifiers.None); - return; - } - - - var clientPoint = root.PointToClient(_position.Value); + if (e.Type == RawPointerEventType.LeftButtonUp + || e.Type == RawPointerEventType.TouchEnd) + return; - if (rect.Contains(clientPoint)) - { - if (_pointer.Captured == null) - { - SetPointerOver(this, 0 /* TODO: proper timestamp */, root, clientPoint, - PointerPointProperties.None, KeyModifiers.None); - } - else - { - SetPointerOver(this, 0 /* TODO: proper timestamp */, root, _pointer.Captured, - PointerPointProperties.None, KeyModifiers.None); - } + _pointers[e.RawPointerId] = pointer = new Pointer(Pointer.GetNextFreeId(), + PointerType.Pen, _pointers.Count == 0); } - } - - private void ProcessRawEvent(RawPointerEventArgs e) - { - e = e ?? throw new ArgumentNullException(nameof(e)); - var pen = (PenDevice)e.Device; - if(pen._disposed) - return; + _lastPositions[e.RawPointerId] = e.Root.PointToScreen(e.Position); + + var props = new PointerPointProperties(e.InputModifiers, e.Type.ToUpdateKind(), + e.Point.Twist, e.Point.Pressure, e.Point.XTilt, e.Point.YTilt); + var keyModifiers = e.InputModifiers.ToKeyModifiers(); - _position = e.Root.PointToScreen(e.Position); - var props = CreateProperties(e); - var keyModifiers = KeyModifiersUtils.ConvertToKey(e.InputModifiers); + bool shouldReleasePointer = false; switch (e.Type) { case RawPointerEventType.LeaveWindow: - LeaveWindow(pen, e.Timestamp, e.Root, props, keyModifiers); + shouldReleasePointer = true; break; case RawPointerEventType.LeftButtonDown: - e.Handled = PenDown(pen, e.Timestamp, e.Root, e.Position, props, keyModifiers); + e.Handled = PenDown(pointer, e.Timestamp, e.Root, e.Position, props, keyModifiers, e.InputHitTestResult); break; case RawPointerEventType.LeftButtonUp: - e.Handled = PenUp(pen, e.Timestamp, e.Root, e.Position, props, keyModifiers); + e.Handled = PenUp(pointer, e.Timestamp, e.Root, e.Position, props, keyModifiers, e.InputHitTestResult); break; case RawPointerEventType.Move: - e.Handled = PenMove(pen, e.Timestamp, e.Root, e.Position, props, keyModifiers); + e.Handled = PenMove(pointer, e.Timestamp, e.Root, e.Position, props, keyModifiers, e.InputHitTestResult, e.IntermediatePoints); break; } - } - - private void LeaveWindow(IPenDevice device, ulong timestamp, IInputRoot root, PointerPointProperties properties, - KeyModifiers inputModifiers) - { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - _position = null; - ClearPointerOver(this, timestamp, root, properties, inputModifiers); - } - - private PointerPointProperties CreateProperties(RawPointerEventArgs args) - { - var kind = args.Type switch + if (shouldReleasePointer) { - RawPointerEventType.LeftButtonDown => PointerUpdateKind.LeftButtonPressed, - RawPointerEventType.LeftButtonUp => PointerUpdateKind.LeftButtonReleased, - _ => PointerUpdateKind.Other, - }; - return new PointerPointProperties(args.InputModifiers, kind, - Twist, Pressure, XTilt, YTilt, IsEraser, IsInverted, IsBarrel); + pointer.Dispose(); + _pointers.Remove(e.RawPointerId); + _lastPositions.Remove(e.RawPointerId); + } } - private MouseButton _lastMouseDownButton; - private bool PenDown(IPenDevice device, ulong timestamp, IInputElement root, Point p, - PointerPointProperties properties, - KeyModifiers inputModifiers) + private bool PenDown(Pointer pointer, ulong timestamp, + IInputElement root, Point p, PointerPointProperties properties, + KeyModifiers inputModifiers, IInputElement? hitTest) { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - var hit = HitTest(root, p); + var source = pointer.Captured ?? hitTest; - if (hit != null) + if (source != null) { - _pointer.Capture(hit); - var source = GetSource(hit); - if (source != null) - { - var settings = AvaloniaLocator.Current.GetService(); - var doubleClickTime = settings?.DoubleClickTime.TotalMilliseconds ?? 500; - var doubleClickSize = settings?.DoubleClickSize ?? new Size(4, 4); - - if (!_lastClickRect.Contains(p) || timestamp - _lastClickTime > doubleClickTime) - { - _clickCount = 0; - } + pointer.Capture(source); + var settings = AvaloniaLocator.Current.GetService(); + var doubleClickTime = settings?.DoubleClickTime.TotalMilliseconds ?? 500; + var doubleClickSize = settings?.DoubleClickSize ?? new Size(4, 4); - ++_clickCount; - _lastClickTime = timestamp; - _lastClickRect = new Rect(p, new Size()) - .Inflate(new Thickness(doubleClickSize.Width / 2, doubleClickSize.Height / 2)); - _lastMouseDownButton = properties.PointerUpdateKind.GetMouseButton(); - var e = new PointerPressedEventArgs(source, _pointer, root, p, timestamp, properties, inputModifiers, _clickCount); - source.RaiseEvent(e); - return e.Handled; + if (!_lastClickRect.Contains(p) || timestamp - _lastClickTime > doubleClickTime) + { + _clickCount = 0; } + + ++_clickCount; + _lastClickTime = timestamp; + _lastClickRect = new Rect(p, new Size()) + .Inflate(new Thickness(doubleClickSize.Width / 2, doubleClickSize.Height / 2)); + _lastMouseDownButton = properties.PointerUpdateKind.GetMouseButton(); + var e = new PointerPressedEventArgs(source, pointer, root, p, timestamp, properties, inputModifiers, _clickCount); + source.RaiseEvent(e); + return e.Handled; } return false; } - private bool PenMove(IPenDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties properties, - KeyModifiers inputModifiers) + private bool PenMove(Pointer pointer, ulong timestamp, + IInputRoot root, Point p, PointerPointProperties properties, + KeyModifiers inputModifiers, IInputElement? hitTest, + Lazy?>? intermediatePoints) { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - IInputElement? source; - - if (_pointer.Captured == null) - { - source = SetPointerOver(this, timestamp, root, p, properties, inputModifiers); - } - else - { - SetPointerOver(this, timestamp, root, _pointer.Captured, properties, inputModifiers); - source = _pointer.Captured; - } + var source = pointer.Captured ?? hitTest; - if (source is object) + if (source is not null) { - var e = new PointerEventArgs(InputElement.PointerMovedEvent, source, _pointer, root, - p, timestamp, properties, inputModifiers); + var e = new PointerEventArgs(InputElement.PointerMovedEvent, source, pointer, root, + p, timestamp, properties, inputModifiers, intermediatePoints); source.RaiseEvent(e); return e.Handled; @@ -239,176 +123,52 @@ namespace Avalonia.Input return false; } - private bool PenUp(IPenDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties props, - KeyModifiers inputModifiers) + private bool PenUp(Pointer pointer, ulong timestamp, + IInputElement root, Point p, PointerPointProperties properties, + KeyModifiers inputModifiers, IInputElement? hitTest) { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - var hit = HitTest(root, p); - var source = GetSource(hit); + var source = pointer.Captured ?? hitTest; if (source is not null) { - var e = new PointerReleasedEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers, + var e = new PointerReleasedEventArgs(source, pointer, root, p, timestamp, properties, inputModifiers, _lastMouseDownButton); source?.RaiseEvent(e); - _pointer.Capture(null); + pointer.Capture(null); return e.Handled; } return false; } - private IInteractive? GetSource(IVisual? hit) - { - if (hit is null) - return null; - - return _pointer.Captured ?? - (hit as IInteractive) ?? - hit.GetSelfAndVisualAncestors().OfType().FirstOrDefault(); - } - - private IInputElement? HitTest(IInputElement root, Point p) - { - root = root ?? throw new ArgumentNullException(nameof(root)); - - return _pointer.Captured ?? root.InputHitTest(p); - } - - PointerEventArgs CreateSimpleEvent(RoutedEvent ev, ulong timestamp, IInteractive? source, - PointerPointProperties properties, - KeyModifiers inputModifiers) - { - return new PointerEventArgs(ev, source, _pointer, null, default, - timestamp, properties, inputModifiers); - } - - private void ClearPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, - PointerPointProperties properties, - KeyModifiers inputModifiers) - { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - var element = root.PointerOverElement; - var e = CreateSimpleEvent(InputElement.PointerLeaveEvent, timestamp, element, properties, inputModifiers); - - if (element!=null && !element.IsAttachedToVisualTree) - { - // element has been removed from visual tree so do top down cleanup - if (root.IsPointerOver) - ClearChildrenPointerOver(e, root,true); - } - while (element != null) - { - e.Source = element; - e.Handled = false; - element.RaiseEvent(e); - element = (IInputElement?)element.VisualParent; - } - - root.PointerOverElement = null; - } - - private void ClearChildrenPointerOver(PointerEventArgs e, IInputElement element,bool clearRoot) - { - foreach (IInputElement el in element.VisualChildren) - { - if (el.IsPointerOver) - { - ClearChildrenPointerOver(e, el, true); - break; - } - } - if(clearRoot) - { - e.Source = element; - e.Handled = false; - element.RaiseEvent(e); - } - } - - private IInputElement? SetPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, Point p, - PointerPointProperties properties, - KeyModifiers inputModifiers) + public void Dispose() { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - - var element = root.InputHitTest(p); - - if (element != root.PointerOverElement) - { - if (element != null) - { - SetPointerOver(device, timestamp, root, element, properties, inputModifiers); - } - else - { - ClearPointerOver(device, timestamp, root, properties, inputModifiers); - } - } - - return element; + if (_disposed) + return; + var values = _pointers.Values.ToList(); + _pointers.Clear(); + _disposed = true; + foreach (var p in values) + p.Dispose(); } - private void SetPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, IInputElement element, - PointerPointProperties properties, - KeyModifiers inputModifiers) - { - device = device ?? throw new ArgumentNullException(nameof(device)); - root = root ?? throw new ArgumentNullException(nameof(root)); - element = element ?? throw new ArgumentNullException(nameof(element)); + [Obsolete] + IInputElement? IPointerDevice.Captured => _pointers.Values + .FirstOrDefault(p => p.IsPrimary)?.Captured; - IInputElement? branch = null; + [Obsolete] + void IPointerDevice.Capture(IInputElement? control) => _pointers.Values + .FirstOrDefault(p => p.IsPrimary)?.Capture(control); - IInputElement? el = element; + [Obsolete] + Point IPointerDevice.GetPosition(IVisual relativeTo) => new Point(-1, -1); - while (el != null) - { - if (el.IsPointerOver) - { - branch = el; - break; - } - el = (IInputElement?)el.VisualParent; - } - - el = root.PointerOverElement; - - var e = CreateSimpleEvent(InputElement.PointerLeaveEvent, timestamp, el, properties, inputModifiers); - if (el!=null && branch!=null && !el.IsAttachedToVisualTree) - { - ClearChildrenPointerOver(e,branch,false); - } - - while (el != null && el != branch) - { - e.Source = el; - e.Handled = false; - el.RaiseEvent(e); - el = (IInputElement?)el.VisualParent; - } - - el = root.PointerOverElement = element; - e.RoutedEvent = InputElement.PointerEnterEvent; - - while (el != null && el != branch) - { - e.Source = el; - e.Handled = false; - el.RaiseEvent(e); - el = (IInputElement?)el.VisualParent; - } - } - - public void Dispose() + public IPointer? TryGetPointer(RawPointerEventArgs ev) { - _disposed = true; - _pointer?.Dispose(); + return _pointers.TryGetValue(ev.RawPointerId, out var pointer) + ? pointer + : null; } } } diff --git a/src/Avalonia.Input/PointerPoint.cs b/src/Avalonia.Input/PointerPoint.cs index ba69add7d8..c704aa28c6 100644 --- a/src/Avalonia.Input/PointerPoint.cs +++ b/src/Avalonia.Input/PointerPoint.cs @@ -1,3 +1,5 @@ +using Avalonia.Input.Raw; + namespace Avalonia.Input { public sealed class PointerPoint @@ -45,6 +47,9 @@ namespace Avalonia.Input IsRightButtonPressed = modifiers.HasAllFlags(RawInputModifiers.RightMouseButton); IsXButton1Pressed = modifiers.HasAllFlags(RawInputModifiers.XButton1MouseButton); IsXButton2Pressed = modifiers.HasAllFlags(RawInputModifiers.XButton2MouseButton); + IsInverted = modifiers.HasAllFlags(RawInputModifiers.PenInverted); + IsEraser = modifiers.HasAllFlags(RawInputModifiers.PenEraser); + IsBarrelButtonPressed = modifiers.HasAllFlags(RawInputModifiers.PenBarrelButton); // The underlying input source might be reporting the previous state, // so make sure that we reflect the current state @@ -72,19 +77,16 @@ namespace Avalonia.Input } public PointerPointProperties(RawInputModifiers modifiers, PointerUpdateKind kind, - float twist, float pressure, float xTilt, float yTilt, bool isEraser, bool isInverted, bool isBarrel + float twist, float pressure, float xTilt, float yTilt ) : this (modifiers, kind) { Twist = twist; Pressure = pressure; XTilt = xTilt; YTilt = yTilt; - IsEraser = isEraser; - IsInverted = isInverted; - IsBarrelButtonPressed = isBarrel; } - internal PointerPointProperties(PointerPointProperties basedOn, Raw.RawPointerPoint rawPoint) + internal PointerPointProperties(PointerPointProperties basedOn, RawPointerPoint rawPoint) { IsLeftButtonPressed = basedOn.IsLeftButtonPressed; IsMiddleButtonPressed = basedOn.IsMiddleButtonPressed; diff --git a/src/Avalonia.Input/Raw/RawPointerEventArgs.cs b/src/Avalonia.Input/Raw/RawPointerEventArgs.cs index 1faa20fbf1..0e4e0ed3e2 100644 --- a/src/Avalonia.Input/Raw/RawPointerEventArgs.cs +++ b/src/Avalonia.Input/Raw/RawPointerEventArgs.cs @@ -56,11 +56,12 @@ namespace Avalonia.Input.Raw Contract.Requires(device != null); Contract.Requires(root != null); + Point = new RawPointerPoint(); Position = position; Type = type; InputModifiers = inputModifiers; } - + /// /// Initializes a new instance of the class. /// @@ -145,6 +146,7 @@ namespace Avalonia.Input.Raw public RawPointerPoint() { this = default; + Pressure = 0.5f; } } } diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index d52115425f..6619c60152 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -380,48 +380,6 @@ namespace Avalonia.Win32 return IntPtr.Zero; } - break; - } - case WindowsMessage.WM_POINTERDEVICECHANGE: - { - if (!_wmPointerEnabled) - { - break; - } - //notifies about changes in the settings of a monitor that has a digitizer attached to it. - //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-pointerdevicechange - break; - } - case WindowsMessage.WM_POINTERDEVICEINRANGE: - { - if (!_wmPointerEnabled) - { - break; - } - _mouseDevice.Capture(null); - //notifies about proximity of pointer device to the digitizer. - //contains pointer id and proximity. - //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-pointerdeviceinrange - break; - } - case WindowsMessage.WM_POINTERDEVICEOUTOFRANGE: - { - if (!_wmPointerEnabled) - { - break; - } - _penDevice.Capture(null); - break; - } - case WindowsMessage.WM_NCPOINTERUPDATE: - { - if (!_wmPointerEnabled) - { - break; - } - //NC stands for non-client area - window header and window border - //As I found above in an old message handling - we dont need to handle NC pointer move/updates. - //All we need is pointer down and up. So this is skipped for now. break; } case WindowsMessage.WM_NCPOINTERDOWN: @@ -430,194 +388,92 @@ namespace Avalonia.Win32 case WindowsMessage.WM_POINTERUP: case WindowsMessage.WM_POINTERUPDATE: { - GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); if (!_wmPointerEnabled) { break; } - - var historyCount = (int)info.historyCount; - Lazy> intermediatePoints = null; - if (info.historyCount > 1) - { - intermediatePoints = new Lazy>(() => - { - var list = new List(historyCount - 1); - if (info.pointerType == PointerInputType.PT_TOUCH) - { - var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); - var historyTouchInfos = ArrayPool.Shared.Rent(historyCount); - try - { - if (GetPointerTouchInfoHistory(pointerId, ref historyCount, historyTouchInfos)) - { - //last info is the same as the current so skip it - for (int i = 0; i < historyCount - 1; i++) - { - var historyTouchInfo = historyTouchInfos[i]; - var historyInfo = historyTouchInfo.pointerInfo; - var historyPoint = PointToClient(new PixelPoint( - historyInfo.ptPixelLocationX, historyInfo.ptPixelLocationY)); - list.Add(new RawPointerPoint - { - Position = historyPoint, - }); - } - } - } - finally - { - ArrayPool.Shared.Return(historyTouchInfos); - } - } - else if (info.pointerType == PointerInputType.PT_PEN) - { - var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); - var historyPenInfos = ArrayPool.Shared.Rent(historyCount); - try - { - if (GetPointerPenInfoHistory(pointerId, ref historyCount, historyPenInfos)) - { - //last info is the same as the current so skip it - for (int i = 0; i < historyCount - 1; i++) - { - var historyPenInfo = historyPenInfos[i]; - var historyInfo = historyPenInfo.pointerInfo; - var historyPoint = PointToClient(new PixelPoint( - historyInfo.ptPixelLocationX, historyInfo.ptPixelLocationY)); - list.Add(new RawPointerPoint - { - Position = historyPoint, - Pressure = historyPenInfo.pressure, - Twist = historyPenInfo.rotation, - XTilt = historyPenInfo.tiltX, - YTilt = historyPenInfo.tiltX - }); - } - } - } - finally - { - ArrayPool.Shared.Return(historyPenInfos); - } - } - else - { - var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); - var historyInfos = ArrayPool.Shared.Rent(historyCount); - try - { - if (GetPointerInfoHistory(pointerId, ref historyCount, historyInfos)) - { - //last info is the same as the current so skip it - for (int i = 0; i < historyCount - 1; i++) - { - var historyInfo = historyInfos[i]; - var historyPoint = PointToClient(new PixelPoint( - historyInfo.ptPixelLocationX, historyInfo.ptPixelLocationY)); - list.Add(new RawPointerPoint - { - Position = historyPoint - }); - } - } - } - finally - { - ArrayPool.Shared.Return(historyInfos); - } - } - return list; - }); - } - + GetDevicePointerInfo(wParam, out var device, out var info, out var point, out var modifiers, ref timestamp); var eventType = GetEventType(message, info); - var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); - var modifiers = GetInputModifiers(info.dwKeyStates); - if (device is TouchDevice) - { - e = new RawTouchEventArgs(_touchDevice, timestamp, _owner, eventType, point, modifiers, info.pointerId) - { - IntermediatePoints = intermediatePoints - }; - } - else - { - e = new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers) - { - IntermediatePoints = intermediatePoints - }; - } - break; - } - case WindowsMessage.WM_POINTERENTER: - { - if (!_wmPointerEnabled) - { - break; - } - //this is not handled by WM_MOUSEENTER so I think there is no need to handle this too. - //but we can detect a new pointer by this message and calling IS_POINTER_NEW_WPARAM - - //note: by using a pen there can be a pointer leave or enter inside a window coords - //when you are just lift up the pen above the display + var args = CreatePointerArgs(device, timestamp, eventType, point, modifiers, info.pointerId); + args.IntermediatePoints = CreateLazyIntermediatePoints(info); + e = args; break; } + case WindowsMessage.WM_POINTERDEVICEOUTOFRANGE: case WindowsMessage.WM_POINTERLEAVE: + case WindowsMessage.WM_POINTERCAPTURECHANGED: { - GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); if (!_wmPointerEnabled) { break; } - if (device is TouchDevice) - { - break; - } - var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); - var modifiers = GetInputModifiers(info.dwKeyStates); - - e = new RawPointerEventArgs( - device, timestamp, _owner, RawPointerEventType.LeaveWindow, point, modifiers); + GetDevicePointerInfo(wParam, out var device, out var info, out var point, out var modifiers, ref timestamp); + var eventType = device is TouchDevice ? RawPointerEventType.TouchCancel : RawPointerEventType.LeaveWindow; + e = CreatePointerArgs(device, timestamp, eventType, point, modifiers, info.pointerId); break; } case WindowsMessage.WM_POINTERWHEEL: case WindowsMessage.WM_POINTERHWHEEL: { - GetDevicePointerInfo(wParam, out var device, out var info, ref timestamp); if (!_wmPointerEnabled) { break; } + GetDevicePointerInfo(wParam, out var device, out var info, out var point, out var modifiers, ref timestamp); - var point = PointToClient(new PixelPoint(info.ptPixelLocationX, info.ptPixelLocationY)); - var modifiers = GetInputModifiers(info.dwKeyStates); var val = (ToInt32(wParam) >> 16) / wheelDelta; var delta = message == WindowsMessage.WM_POINTERWHEEL ? new Vector(0, val) : new Vector(val, 0); - e = new RawMouseWheelEventArgs(device, timestamp, _owner, point, delta, modifiers); + e = new RawMouseWheelEventArgs(device, timestamp, _owner, point.Position, delta, modifiers) + { + RawPointerId = info.pointerId + }; break; } - case WindowsMessage.WM_POINTERACTIVATE: + case WindowsMessage.WM_POINTERDEVICEINRANGE: { if (!_wmPointerEnabled) { break; } + + // Do not generate events, but release mouse capture on any other device input. + GetDevicePointerInfo(wParam, out var device, out var info, out var point, out var modifiers, ref timestamp); + if (device != _mouseDevice) + { + _mouseDevice.Capture(null); + return IntPtr.Zero; + } + break; + } + case WindowsMessage.WM_POINTERACTIVATE: + { //occurs when a pointer activates an inactive window. //we should handle this and return PA_ACTIVATE or PA_NOACTIVATE //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-pointeractivate break; } - case WindowsMessage.WM_POINTERCAPTURECHANGED: + case WindowsMessage.WM_POINTERDEVICECHANGE: { - if (!_wmPointerEnabled) - { - break; - } - _mouseDevice.Capture(null); - _penDevice.Capture(null); - return IntPtr.Zero; + //notifies about changes in the settings of a monitor that has a digitizer attached to it. + //https://docs.microsoft.com/en-us/previous-versions/windows/desktop/inputmsg/wm-pointerdevicechange + break; + } + case WindowsMessage.WM_NCPOINTERUPDATE: + { + //NC stands for non-client area - window header and window border + //As I found above in an old message handling - we dont need to handle NC pointer move/updates. + //All we need is pointer down and up. So this is skipped for now. + break; + } + case WindowsMessage.WM_POINTERENTER: + { + //this is not handled by WM_MOUSEENTER so I think there is no need to handle this too. + //but we can detect a new pointer by this message and calling IS_POINTER_NEW_WPARAM + + //note: by using a pen there can be a pointer leave or enter inside a window coords + //when you are just lift up the pen above the display + break; } case WindowsMessage.DM_POINTERHITTEST: { @@ -839,6 +695,11 @@ namespace Avalonia.Win32 _ignoreWmChar = e.Handled; } + if (s_intermediatePointsPooledList.Count > 0) + { + s_intermediatePointsPooledList.Dispose(); + } + if (e.Handled) { return IntPtr.Zero; @@ -851,40 +712,110 @@ namespace Avalonia.Win32 } } - private unsafe void ApplyPenInfo(POINTER_PEN_INFO penInfo) + private unsafe Lazy> CreateLazyIntermediatePoints(POINTER_INFO info) { - _penDevice.IsBarrel = penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_BARREL); - _penDevice.IsEraser = penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_ERASER); - _penDevice.IsInverted = penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_INVERTED); - - _penDevice.XTilt = penInfo.tiltX; - _penDevice.YTilt = penInfo.tiltY; - _penDevice.Pressure = penInfo.pressure; - _penDevice.Twist = penInfo.rotation; + // Limit history size with reasonable value. + // With sizeof(POINTER_TOUCH_INFO) * 100 we can get maximum 14400 bytes. + var historyCount = Math.Min((int)info.historyCount, MaxPointerHistorySize); + if (historyCount > 1) + { + return new Lazy>(() => + { + s_intermediatePointsPooledList.Clear(); + s_intermediatePointsPooledList.Capacity = historyCount; + if (info.pointerType == PointerInputType.PT_TOUCH) + { + if (GetPointerTouchInfoHistory(info.pointerId, ref historyCount, s_historyTouchInfos)) + { + //last info is the same as the current so skip it + for (int i = 0; i < historyCount - 1; i++) + { + var historyTouchInfo = s_historyTouchInfos[i]; + s_intermediatePointsPooledList.Add(CreateRawPointerPoint(historyTouchInfo)); + } + } + } + else if (info.pointerType == PointerInputType.PT_PEN) + { + if (GetPointerPenInfoHistory(info.pointerId, ref historyCount, s_historyPenInfos)) + { + //last info is the same as the current so skip it + for (int i = 0; i < historyCount - 1; i++) + { + var historyPenInfo = s_historyPenInfos[i]; + s_intermediatePointsPooledList.Add(CreateRawPointerPoint(historyPenInfo)); + } + } + } + else + { + // Currently Windows does not return history info for mouse input, but we handle it just for case. + if (GetPointerInfoHistory(info.pointerId, ref historyCount, s_historyInfos)) + { + //last info is the same as the current so skip it + for (int i = 0; i < historyCount - 1; i++) + { + var historyInfo = s_historyInfos[i]; + s_intermediatePointsPooledList.Add(CreateRawPointerPoint(historyInfo)); + } + } + } + return s_intermediatePointsPooledList; + }); + } + + return null; + } + + private RawPointerEventArgs CreatePointerArgs(IInputDevice device, ulong timestamp, RawPointerEventType eventType, RawPointerPoint point, RawInputModifiers modifiers, uint rawPointerId) + { + return device is TouchDevice + ? new RawTouchEventArgs(device, timestamp, _owner, eventType, point, modifiers, rawPointerId) + : new RawPointerEventArgs(device, timestamp, _owner, eventType, point, modifiers) + { + RawPointerId = rawPointerId + }; } - private void GetDevicePointerInfo(IntPtr wParam, out IInputDevice device, out POINTER_INFO info, ref uint timestamp) + private void GetDevicePointerInfo(IntPtr wParam, + out IPointerDevice device, out POINTER_INFO info, out RawPointerPoint point, + out RawInputModifiers modifiers, ref uint timestamp) { var pointerId = (uint)(ToInt32(wParam) & 0xFFFF); GetPointerType(pointerId, out var type); - //GetPointerCursorId(pointerId, out var cursorId); + + modifiers = default; + switch (type) { case PointerInputType.PT_PEN: device = _penDevice; GetPointerPenInfo(pointerId, out var penInfo); info = penInfo.pointerInfo; - - ApplyPenInfo(penInfo); + point = CreateRawPointerPoint(penInfo); + if (penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_BARREL)) + { + modifiers |= RawInputModifiers.PenBarrelButton; + } + if (penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_ERASER)) + { + modifiers |= RawInputModifiers.PenEraser; + } + if (penInfo.penFlags.HasFlag(PenFlags.PEN_FLAGS_INVERTED)) + { + modifiers |= RawInputModifiers.PenInverted; + } break; case PointerInputType.PT_TOUCH: device = _touchDevice; GetPointerTouchInfo(pointerId, out var touchInfo); info = touchInfo.pointerInfo; + point = CreateRawPointerPoint(touchInfo); break; default: device = _mouseDevice; GetPointerInfo(pointerId, out info); + point = CreateRawPointerPoint(info); break; } @@ -892,6 +823,44 @@ namespace Avalonia.Win32 { timestamp = info.dwTime; } + + modifiers |= GetInputModifiers(info.dwKeyStates); + } + + private RawPointerPoint CreateRawPointerPoint(POINTER_INFO pointerInfo) + { + var point = PointToClient(new PixelPoint(pointerInfo.ptPixelLocationX, pointerInfo.ptPixelLocationY)); + return new RawPointerPoint + { + Position = point + }; + } + private RawPointerPoint CreateRawPointerPoint(POINTER_TOUCH_INFO info) + { + var pointerInfo = info.pointerInfo; + var point = PointToClient(new PixelPoint(pointerInfo.ptPixelLocationX, pointerInfo.ptPixelLocationY)); + return new RawPointerPoint + { + Position = point, + // POINTER_PEN_INFO.pressure is normalized to a range between 0 and 1024, with 512 as a default. + // But in our API we use range from 0.0 to 1.0. + Pressure = info.pressure / 1024f + }; + } + private RawPointerPoint CreateRawPointerPoint(POINTER_PEN_INFO info) + { + var pointerInfo = info.pointerInfo; + var point = PointToClient(new PixelPoint(pointerInfo.ptPixelLocationX, pointerInfo.ptPixelLocationY)); + return new RawPointerPoint + { + Position = point, + // POINTER_PEN_INFO.pressure is normalized to a range between 0 and 1024, with 512 as a default. + // But in our API we use range from 0.0 to 1.0. + Pressure = info.pressure / 1024f, + Twist = info.rotation, + XTilt = info.tiltX, + YTilt = info.tiltX + }; } private static RawPointerEventType GetEventType(WindowsMessage message, POINTER_INFO info) @@ -904,8 +873,6 @@ namespace Avalonia.Win32 } switch (info.pointerType) { - case PointerInputType.PT_PEN: - return ToEventType(info.ButtonChangeType); case PointerInputType.PT_TOUCH: if (info.pointerFlags.HasFlag(PointerFlags.POINTER_FLAG_CANCELED)) { diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index 64295f8925..e0f6348c23 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -21,6 +21,7 @@ using Avalonia.Win32.OpenGl; using Avalonia.Win32.WinRT; using Avalonia.Win32.WinRT.Composition; using static Avalonia.Win32.Interop.UnmanagedMethods; +using Avalonia.Collections.Pooled; namespace Avalonia.Win32 { @@ -95,6 +96,12 @@ namespace Avalonia.Win32 private uint _langid; private bool _ignoreWmChar; + private const int MaxPointerHistorySize = 512; + private readonly static PooledList s_intermediatePointsPooledList = new(); + private readonly static POINTER_TOUCH_INFO[] s_historyTouchInfos = new POINTER_TOUCH_INFO[MaxPointerHistorySize]; + private readonly static POINTER_PEN_INFO[] s_historyPenInfos = new POINTER_PEN_INFO[MaxPointerHistorySize]; + private readonly static POINTER_INFO[] s_historyInfos = new POINTER_INFO[MaxPointerHistorySize]; + public WindowImpl() { _touchDevice = new TouchDevice(); From 99e444f5f19a9312ac4eb99076084b18b92040e3 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sat, 16 Apr 2022 04:01:07 -0400 Subject: [PATCH 025/224] Update documentation of pointer types --- src/Avalonia.Input/IPointer.cs | 40 +++++++++++- src/Avalonia.Input/PointerEventArgs.cs | 19 +++++- src/Avalonia.Input/PointerPoint.cs | 86 ++++++++++++++++++++++++-- 3 files changed, 138 insertions(+), 7 deletions(-) diff --git a/src/Avalonia.Input/IPointer.cs b/src/Avalonia.Input/IPointer.cs index 361f3ac370..98f7c81f02 100644 --- a/src/Avalonia.Input/IPointer.cs +++ b/src/Avalonia.Input/IPointer.cs @@ -1,15 +1,53 @@ namespace Avalonia.Input { + /// + /// Identifies specific pointer generated by input device. + /// + /// + /// Some devices, for instance, touchscreen might generate a pointer on each physical contact. + /// public interface IPointer { + /// + /// Gets a unique identifier for the input pointer. + /// int Id { get; } + + /// + /// Captures pointer input to the specified control. + /// + /// The control. + /// + /// When an element captures the pointer, it receives pointer input whether the cursor is + /// within the control's bounds or not. The current pointer capture control is exposed + /// by the property. + /// void Capture(IInputElement? control); + + /// + /// Gets the control that is currently capturing by the pointer, if any. + /// + /// + /// When an element captures the pointer, it receives pointer input whether the cursor is + /// within the control's bounds or not. To set the pointer capture, call the + /// method. + /// IInputElement? Captured { get; } + + /// + /// Gets the pointer device type. + /// PointerType Type { get; } + + /// + /// Gets a value that indicates whether the input is from the primary pointer when multiple pointers are registered. + /// bool IsPrimary { get; } - } + /// + /// Enumerates pointer device types. + /// public enum PointerType { Mouse, diff --git a/src/Avalonia.Input/PointerEventArgs.cs b/src/Avalonia.Input/PointerEventArgs.cs index 79335eb9fc..058c2f9cc1 100644 --- a/src/Avalonia.Input/PointerEventArgs.cs +++ b/src/Avalonia.Input/PointerEventArgs.cs @@ -67,7 +67,14 @@ namespace Avalonia.Input public IPointer? TryGetPointer(RawPointerEventArgs ev) => _ev.Pointer; } + /// + /// Gets specific pointer generated by input device. + /// public IPointer Pointer { get; } + + /// + /// Gets the time when the input occurred. + /// public ulong Timestamp { get; } private IPointerDevice? _device; @@ -91,7 +98,10 @@ namespace Avalonia.Input return mods; } } - + + /// + /// Gets a value that indicates which key modifiers were active at the time that the pointer event was initiated. + /// public KeyModifiers KeyModifiers { get; } private Point GetPosition(Point pt, IVisual? relativeTo) @@ -102,7 +112,12 @@ namespace Avalonia.Input return pt; return pt * _rootVisual.TransformToVisual(relativeTo) ?? default; } - + + /// + /// Gets the pointer position relative to a control. + /// + /// The control. + /// The pointer position in the control's coordinates. public Point GetPosition(IVisual? relativeTo) => GetPosition(_rootVisualPosition, relativeTo); [Obsolete("Use GetCurrentPoint")] diff --git a/src/Avalonia.Input/PointerPoint.cs b/src/Avalonia.Input/PointerPoint.cs index c704aa28c6..71145b5cb0 100644 --- a/src/Avalonia.Input/PointerPoint.cs +++ b/src/Avalonia.Input/PointerPoint.cs @@ -2,6 +2,9 @@ using Avalonia.Input.Raw; namespace Avalonia.Input { + /// + /// Provides basic properties for the input pointer associated with a single mouse, pen/stylus, or touch contact. + /// public sealed class PointerPoint { public PointerPoint(IPointer pointer, Point position, PointerPointProperties properties) @@ -10,34 +13,109 @@ namespace Avalonia.Input Position = position; Properties = properties; } + + /// + /// Gets specific pointer generated by input device. + /// public IPointer Pointer { get; } + + /// + /// Gets extended information about the input pointer. + /// public PointerPointProperties Properties { get; } + + /// + /// Gets the location of the pointer input in client coordinates. + /// public Point Position { get; } } + /// + /// Provides extended properties for a PointerPoint object. + /// public sealed class PointerPointProperties { + /// + /// Gets a value that indicates whether the pointer input was triggered by the primary action mode of an input device. + /// public bool IsLeftButtonPressed { get; } + + /// + /// Gets a value that indicates whether the pointer input was triggered by the tertiary action mode of an input device. + /// public bool IsMiddleButtonPressed { get; } + + /// + /// Gets a value that indicates whether the pointer input was triggered by the secondary action mode (if supported) of an input device. + /// public bool IsRightButtonPressed { get; } + + /// + /// Gets a value that indicates whether the pointer input was triggered by the first extended mouse button (XButton1). + /// public bool IsXButton1Pressed { get; } + + /// + /// Gets a value that indicates whether the pointer input was triggered by the second extended mouse button (XButton2). + /// public bool IsXButton2Pressed { get; } + + /// + /// Gets a value that indicates whether the barrel button of the pen/stylus device is pressed. + /// public bool IsBarrelButtonPressed { get; } + + /// + /// Gets a value that indicates whether the input is from a pen eraser. + /// public bool IsEraser { get; } + + /// + /// Gets a value that indicates whether the digitizer pen is inverted. + /// public bool IsInverted { get; } + /// + /// Gets the clockwise rotation in degrees of a pen device around its own major axis (such as when the user spins the pen in their fingers). + /// + /// + /// A value between 0.0 and 359.0 in degrees of rotation. The default value is 0.0. + /// public float Twist { get; } - public float Pressure { get; } + + /// + /// Gets a value that indicates the force that the pointer device (typically a pen/stylus) exerts on the surface of the digitizer. + /// + /// + /// A value from 0 to 1.0. The default value is 0.5. + /// + public float Pressure { get; } = 0.5f; + + /// + /// Gets the plane angle between the Y-Z plane and the plane that contains the Y axis and the axis of the input device (typically a pen/stylus). + /// + /// + /// The value is 0.0 when the finger or pen is perpendicular to the digitizer surface, between 0.0 and 90.0 when tilted to the right of perpendicular, and between 0.0 and -90.0 when tilted to the left of perpendicular. The default value is 0.0. + /// public float XTilt { get; } - public float YTilt { get; } + /// + /// Gets the plane angle between the X-Z plane and the plane that contains the X axis and the axis of the input device (typically a pen/stylus). + /// + /// + /// The value is 0.0 when the finger or pen is perpendicular to the digitizer surface, between 0.0 and 90.0 when tilted towards the user, and between 0.0 and -90.0 when tilted away from the user. The default value is 0.0. + /// + public float YTilt { get; } + /// + /// Gets the kind of pointer state change. + /// public PointerUpdateKind PointerUpdateKind { get; } private PointerPointProperties() - { + { } - + public PointerPointProperties(RawInputModifiers modifiers, PointerUpdateKind kind) { PointerUpdateKind = kind; From bd2578d68359abcc44eb83118b907f74abaf3ae2 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sat, 16 Apr 2022 04:31:57 -0400 Subject: [PATCH 026/224] Update control catalog pointers page --- samples/ControlCatalog.NetCore/Program.cs | 2 +- samples/ControlCatalog/MainView.xaml | 2 +- samples/ControlCatalog/Pages/PointerCanvas.cs | 221 ++++++++++ .../Pages/PointerContactsTab.cs | 109 +++++ samples/ControlCatalog/Pages/PointersPage.cs | 394 ------------------ .../ControlCatalog/Pages/PointersPage.xaml | 65 +++ .../ControlCatalog/Pages/PointersPage.xaml.cs | 76 ++++ 7 files changed, 473 insertions(+), 396 deletions(-) create mode 100644 samples/ControlCatalog/Pages/PointerCanvas.cs create mode 100644 samples/ControlCatalog/Pages/PointerContactsTab.cs delete mode 100644 samples/ControlCatalog/Pages/PointersPage.cs create mode 100644 samples/ControlCatalog/Pages/PointersPage.xaml create mode 100644 samples/ControlCatalog/Pages/PointersPage.xaml.cs diff --git a/samples/ControlCatalog.NetCore/Program.cs b/samples/ControlCatalog.NetCore/Program.cs index 4b81935452..08ac17d0c4 100644 --- a/samples/ControlCatalog.NetCore/Program.cs +++ b/samples/ControlCatalog.NetCore/Program.cs @@ -114,7 +114,7 @@ namespace ControlCatalog.NetCore }) .With(new Win32PlatformOptions { - EnableMultitouch = true + EnableWmPointerEvents = true }) .UseSkia() .UseManagedSystemDialogs() diff --git a/samples/ControlCatalog/MainView.xaml b/samples/ControlCatalog/MainView.xaml index 85f278b5fa..c61ba231e0 100644 --- a/samples/ControlCatalog/MainView.xaml +++ b/samples/ControlCatalog/MainView.xaml @@ -106,7 +106,7 @@ - + diff --git a/samples/ControlCatalog/Pages/PointerCanvas.cs b/samples/ControlCatalog/Pages/PointerCanvas.cs new file mode 100644 index 0000000000..b815a573f2 --- /dev/null +++ b/samples/ControlCatalog/Pages/PointerCanvas.cs @@ -0,0 +1,221 @@ +#nullable enable +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Threading; + +using Avalonia; +using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Media; +using Avalonia.Threading; + +namespace ControlCatalog.Pages; + +public class PointerCanvas : Control +{ + private readonly Stopwatch _stopwatch = Stopwatch.StartNew(); + private int _events; + private IDisposable? _statusUpdated; + private Dictionary _pointers = new(); + private PointerPointProperties? _lastProperties; + class PointerPoints + { + struct CanvasPoint + { + public IBrush Brush; + public Point Point; + public double Radius; + public double? Pressure; + } + + readonly CanvasPoint[] _points = new CanvasPoint[1000]; + int _index; + + public void Render(DrawingContext context, bool drawPoints) + { + CanvasPoint? prev = null; + for (var c = 0; c < _points.Length; c++) + { + var i = (c + _index) % _points.Length; + var pt = _points[i]; + var pressure = (pt.Pressure ?? prev?.Pressure ?? 0.5); + var thickness = pressure * 10; + var radius = pressure * pt.Radius; + + if (drawPoints) + { + if (pt.Brush != null) + { + context.DrawEllipse(pt.Brush, null, pt.Point, radius, radius); + } + } + else + { + if (prev.HasValue && prev.Value.Brush != null && pt.Brush != null + && prev.Value.Pressure != null && pt.Pressure != null) + { + var linePen = new Pen(Brushes.Black, thickness, null, PenLineCap.Round, PenLineJoin.Round); + context.DrawLine(linePen, prev.Value.Point, pt.Point); + } + } + prev = pt; + } + + } + + void AddPoint(Point pt, IBrush brush, double radius, float? pressure = null) + { + _points[_index] = new CanvasPoint { Point = pt, Brush = brush, Radius = radius, Pressure = pressure }; + _index = (_index + 1) % _points.Length; + } + + public void HandleEvent(PointerEventArgs e, Visual v) + { + e.Handled = true; + var currentPoint = e.GetCurrentPoint(v); + if (e.RoutedEvent == PointerPressedEvent) + AddPoint(currentPoint.Position, Brushes.Green, 10); + else if (e.RoutedEvent == PointerReleasedEvent) + AddPoint(currentPoint.Position, Brushes.Red, 10); + else + { + var pts = e.GetIntermediatePoints(v); + for (var c = 0; c < pts.Count; c++) + { + var pt = pts[c]; + AddPoint(pt.Position, c == pts.Count - 1 ? Brushes.Blue : Brushes.Black, + c == pts.Count - 1 ? 5 : 2, pt.Properties.Pressure); + } + } + } + } + + private int _threadSleep; + public static DirectProperty ThreadSleepProperty = + AvaloniaProperty.RegisterDirect(nameof(ThreadSleep), c => c.ThreadSleep, (c, v) => c.ThreadSleep = v); + + public int ThreadSleep + { + get => _threadSleep; + set => SetAndRaise(ThreadSleepProperty, ref _threadSleep, value); + } + + private bool _drawOnlyPoints; + public static DirectProperty DrawOnlyPointsProperty = + AvaloniaProperty.RegisterDirect(nameof(DrawOnlyPoints), c => c.DrawOnlyPoints, (c, v) => c.DrawOnlyPoints = v); + + public bool DrawOnlyPoints + { + get => _drawOnlyPoints; + set => SetAndRaise(DrawOnlyPointsProperty, ref _drawOnlyPoints, value); + } + + private string? _status; + public static DirectProperty StatusProperty = + AvaloniaProperty.RegisterDirect(nameof(DrawOnlyPoints), c => c.Status, (c, v) => c.Status = v, + defaultBindingMode: Avalonia.Data.BindingMode.TwoWay); + + public string? Status + { + get => _status; + set => SetAndRaise(StatusProperty, ref _status, value); + } + + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + + _statusUpdated = DispatcherTimer.Run(() => + { + if (_stopwatch.Elapsed.TotalSeconds > 1) + { + Status = $@"Events per second: {(_events / _stopwatch.Elapsed.TotalSeconds)} +PointerUpdateKind: {_lastProperties?.PointerUpdateKind} +IsLeftButtonPressed: {_lastProperties?.IsLeftButtonPressed} +IsRightButtonPressed: {_lastProperties?.IsRightButtonPressed} +IsMiddleButtonPressed: {_lastProperties?.IsMiddleButtonPressed} +IsXButton1Pressed: {_lastProperties?.IsXButton1Pressed} +IsXButton2Pressed: {_lastProperties?.IsXButton2Pressed} +IsBarrelButtonPressed: {_lastProperties?.IsBarrelButtonPressed} +IsEraser: {_lastProperties?.IsEraser} +IsInverted: {_lastProperties?.IsInverted} +Pressure: {_lastProperties?.Pressure} +XTilt: {_lastProperties?.XTilt} +YTilt: {_lastProperties?.YTilt} +Twist: {_lastProperties?.Twist}"; + _stopwatch.Restart(); + _events = 0; + } + + return true; + }, TimeSpan.FromMilliseconds(10)); + } + + protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnDetachedFromVisualTree(e); + + _statusUpdated?.Dispose(); + } + + void HandleEvent(PointerEventArgs e) + { + _events++; + if (_threadSleep != 0) + { + Thread.Sleep(_threadSleep); + } + InvalidateVisual(); + + if (e.RoutedEvent == PointerReleasedEvent && e.Pointer.Type == PointerType.Touch) + { + _pointers.Remove(e.Pointer.Id); + return; + } + + var lastPointer = e.GetCurrentPoint(this); + _lastProperties = lastPointer.Properties; + + if (e.Pointer.Type != PointerType.Pen + || lastPointer.Properties.Pressure > 0) + { + if (!_pointers.TryGetValue(e.Pointer.Id, out var pt)) + _pointers[e.Pointer.Id] = pt = new PointerPoints(); + pt.HandleEvent(e, this); + } + } + + public override void Render(DrawingContext context) + { + context.FillRectangle(Brushes.White, Bounds); + foreach (var pt in _pointers.Values) + pt.Render(context, _drawOnlyPoints); + base.Render(context); + } + + protected override void OnPointerPressed(PointerPressedEventArgs e) + { + if (e.ClickCount == 2) + { + _pointers.Clear(); + InvalidateVisual(); + return; + } + + HandleEvent(e); + base.OnPointerPressed(e); + } + + protected override void OnPointerMoved(PointerEventArgs e) + { + HandleEvent(e); + base.OnPointerMoved(e); + } + + protected override void OnPointerReleased(PointerReleasedEventArgs e) + { + HandleEvent(e); + base.OnPointerReleased(e); + } +} diff --git a/samples/ControlCatalog/Pages/PointerContactsTab.cs b/samples/ControlCatalog/Pages/PointerContactsTab.cs new file mode 100644 index 0000000000..b6aabebf99 --- /dev/null +++ b/samples/ControlCatalog/Pages/PointerContactsTab.cs @@ -0,0 +1,109 @@ +#nullable enable +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reactive.Linq; + +using Avalonia; +using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Media; +using Avalonia.Media.Immutable; + +namespace ControlCatalog.Pages; + +public class PointerContactsTab : Control +{ + class PointerInfo + { + public Point Point { get; set; } + public Color Color { get; set; } + } + + private static Color[] AllColors = new[] + { + Colors.Aqua, + Colors.Beige, + Colors.Chartreuse, + Colors.Coral, + Colors.Fuchsia, + Colors.Crimson, + Colors.Lavender, + Colors.Orange, + Colors.Orchid, + Colors.ForestGreen, + Colors.SteelBlue, + Colors.PapayaWhip, + Colors.PaleVioletRed, + Colors.Goldenrod, + Colors.Maroon, + Colors.Moccasin, + Colors.Navy, + Colors.Wheat, + Colors.Violet, + Colors.Sienna, + Colors.Indigo, + Colors.Honeydew + }; + + private Dictionary _pointers = new Dictionary(); + + public PointerContactsTab() + { + ClipToBounds = true; + } + + void UpdatePointer(PointerEventArgs e) + { + if (!_pointers.TryGetValue(e.Pointer, out var info)) + { + if (e.RoutedEvent == PointerMovedEvent) + return; + var colors = AllColors.Except(_pointers.Values.Select(c => c.Color)).ToArray(); + var color = colors[new Random().Next(0, colors.Length - 1)]; + _pointers[e.Pointer] = info = new PointerInfo { Color = color }; + } + + info.Point = e.GetPosition(this); + InvalidateVisual(); + } + + protected override void OnPointerPressed(PointerPressedEventArgs e) + { + UpdatePointer(e); + e.Pointer.Capture(this); + e.Handled = true; + base.OnPointerPressed(e); + } + + protected override void OnPointerMoved(PointerEventArgs e) + { + UpdatePointer(e); + e.Handled = true; + base.OnPointerMoved(e); + } + + protected override void OnPointerReleased(PointerReleasedEventArgs e) + { + _pointers.Remove(e.Pointer); + e.Handled = true; + InvalidateVisual(); + } + + protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e) + { + _pointers.Remove(e.Pointer); + InvalidateVisual(); + } + + public override void Render(DrawingContext context) + { + context.FillRectangle(Brushes.Transparent, new Rect(default, Bounds.Size)); + foreach (var pt in _pointers.Values) + { + var brush = new ImmutableSolidColorBrush(pt.Color); + + context.DrawEllipse(brush, null, pt.Point, 75, 75); + } + } +} diff --git a/samples/ControlCatalog/Pages/PointersPage.cs b/samples/ControlCatalog/Pages/PointersPage.cs deleted file mode 100644 index 0668c248c7..0000000000 --- a/samples/ControlCatalog/Pages/PointersPage.cs +++ /dev/null @@ -1,394 +0,0 @@ -#nullable enable -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Reactive.Linq; -using System.Runtime.InteropServices; -using System.Threading; -using Avalonia; -using Avalonia.Controls; -using Avalonia.Controls.Documents; -using Avalonia.Input; -using Avalonia.Layout; -using Avalonia.Media; -using Avalonia.Media.Immutable; -using Avalonia.Threading; -using Avalonia.VisualTree; - -namespace ControlCatalog.Pages; - -public class PointersPage : Decorator -{ - public PointersPage() - { - Child = new TabControl - { - Items = new[] - { - new TabItem() { Header = "Contacts", Content = new PointerContactsTab() }, - new TabItem() { Header = "IntermediatePoints", Content = new PointerIntermediatePointsTab() }, - new TabItem() { Header = "Pressure", Content = new PointerPressureTab() } - } - }; - } - - - class PointerContactsTab : Control - { - class PointerInfo - { - public Point Point { get; set; } - public Color Color { get; set; } - } - - private static Color[] AllColors = new[] - { - Colors.Aqua, - Colors.Beige, - Colors.Chartreuse, - Colors.Coral, - Colors.Fuchsia, - Colors.Crimson, - Colors.Lavender, - Colors.Orange, - Colors.Orchid, - Colors.ForestGreen, - Colors.SteelBlue, - Colors.PapayaWhip, - Colors.PaleVioletRed, - Colors.Goldenrod, - Colors.Maroon, - Colors.Moccasin, - Colors.Navy, - Colors.Wheat, - Colors.Violet, - Colors.Sienna, - Colors.Indigo, - Colors.Honeydew - }; - - private Dictionary _pointers = new Dictionary(); - - public PointerContactsTab() - { - ClipToBounds = true; - } - - void UpdatePointer(PointerEventArgs e) - { - if (!_pointers.TryGetValue(e.Pointer, out var info)) - { - if (e.RoutedEvent == PointerMovedEvent) - return; - var colors = AllColors.Except(_pointers.Values.Select(c => c.Color)).ToArray(); - var color = colors[new Random().Next(0, colors.Length - 1)]; - _pointers[e.Pointer] = info = new PointerInfo {Color = color}; - } - - info.Point = e.GetPosition(this); - InvalidateVisual(); - } - - protected override void OnPointerPressed(PointerPressedEventArgs e) - { - UpdatePointer(e); - e.Pointer.Capture(this); - e.Handled = true; - base.OnPointerPressed(e); - } - - protected override void OnPointerMoved(PointerEventArgs e) - { - UpdatePointer(e); - e.Handled = true; - base.OnPointerMoved(e); - } - - protected override void OnPointerReleased(PointerReleasedEventArgs e) - { - _pointers.Remove(e.Pointer); - e.Handled = true; - InvalidateVisual(); - } - - protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e) - { - _pointers.Remove(e.Pointer); - InvalidateVisual(); - } - - public override void Render(DrawingContext context) - { - context.FillRectangle(Brushes.Transparent, new Rect(default, Bounds.Size)); - foreach (var pt in _pointers.Values) - { - var brush = new ImmutableSolidColorBrush(pt.Color); - - context.DrawEllipse(brush, null, pt.Point, 75, 75); - } - } - } - - public class PointerIntermediatePointsTab : Decorator - { - public PointerIntermediatePointsTab() - { - this[TextElement.ForegroundProperty] = Brushes.Black; - var slider = new Slider - { - Margin = new Thickness(5), - Minimum = 0, - Maximum = 500 - }; - - var status = new TextBlock() - { - HorizontalAlignment = HorizontalAlignment.Left, - VerticalAlignment = VerticalAlignment.Top, - }; - Child = new Grid - { - Children = - { - new PointerCanvas(slider, status, true), - new Border - { - Background = Brushes.LightYellow, - Child = new StackPanel - { - Children = - { - new StackPanel - { - Orientation = Orientation.Horizontal, - Children = - { - new TextBlock { Text = "Thread sleep:" }, - new TextBlock() - { - [!TextBlock.TextProperty] =slider.GetObservable(Slider.ValueProperty) - .Select(x=>x.ToString()).ToBinding() - } - } - }, - slider - } - }, - - HorizontalAlignment = HorizontalAlignment.Right, - VerticalAlignment = VerticalAlignment.Top, - Width = 300, - Height = 60 - }, - status - } - }; - } - } - - public class PointerPressureTab : Decorator - { - public PointerPressureTab() - { - this[TextBlock.ForegroundProperty] = Brushes.Black; - - var status = new TextBlock() - { - HorizontalAlignment = HorizontalAlignment.Left, - VerticalAlignment = VerticalAlignment.Top, - FontSize = 12 - }; - Child = new Grid - { - Children = - { - new PointerCanvas(null, status, false), - status - } - }; - } - } - - class PointerCanvas : Control - { - private readonly Slider? _slider; - private readonly TextBlock _status; - private readonly bool _drawPoints; - private int _events; - private Stopwatch _stopwatch = Stopwatch.StartNew(); - private IDisposable? _statusUpdated; - private Dictionary _pointers = new(); - private PointerPointProperties? _lastProperties; - class PointerPoints - { - struct CanvasPoint - { - public IBrush Brush; - public Point Point; - public double Radius; - public double Pressure; - } - - readonly CanvasPoint[] _points = new CanvasPoint[1000]; - int _index; - - public void Render(DrawingContext context, bool drawPoints) - { - - CanvasPoint? prev = null; - for (var c = 0; c < _points.Length; c++) - { - var i = (c + _index) % _points.Length; - var pt = _points[i]; - var thickness = pt.Pressure == 0 ? 1 : (pt.Pressure / 1024) * 5; - - if (drawPoints) - { - if (prev.HasValue && prev.Value.Brush != null && pt.Brush != null) - context.DrawLine(new Pen(Brushes.Black, thickness), prev.Value.Point, pt.Point); - if (pt.Brush != null) - context.DrawEllipse(pt.Brush, null, pt.Point, pt.Radius, pt.Radius); - } - else - { - if (prev.HasValue && prev.Value.Brush != null && pt.Brush != null) - context.DrawLine(new Pen(Brushes.Black, thickness, lineCap: PenLineCap.Round, lineJoin: PenLineJoin.Round), prev.Value.Point, pt.Point); - } - prev = pt; - } - - } - - void AddPoint(Point pt, IBrush brush, double radius, float pressure) - { - _points[_index] = new CanvasPoint { Point = pt, Brush = brush, Radius = radius, Pressure = pressure }; - _index = (_index + 1) % _points.Length; - } - - public void HandleEvent(PointerEventArgs e, Visual v) - { - e.Handled = true; - var currentPoint = e.GetCurrentPoint(v); - if (e.RoutedEvent == PointerPressedEvent) - AddPoint(currentPoint.Position, Brushes.Green, 10, currentPoint.Properties.Pressure); - else if (e.RoutedEvent == PointerReleasedEvent) - AddPoint(currentPoint.Position, Brushes.Red, 10, currentPoint.Properties.Pressure); - else - { - var pts = e.GetIntermediatePoints(v); - for (var c = 0; c < pts.Count; c++) - { - var pt = pts[c]; - AddPoint(pt.Position, c == pts.Count - 1 ? Brushes.Blue : Brushes.Black, - c == pts.Count - 1 ? 5 : 2, pt.Properties.Pressure); - } - } - } - } - - public PointerCanvas(Slider? slider, TextBlock status, bool drawPoints) - { - _slider = slider; - _status = status; - _drawPoints = drawPoints; - } - - protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) - { - base.OnAttachedToVisualTree(e); - - _statusUpdated = DispatcherTimer.Run(() => - { - if (_stopwatch.Elapsed.TotalSeconds > 1) - { - _status.Text = $@"Events per second: {(_events / _stopwatch.Elapsed.TotalSeconds)} -PointerUpdateKind: {_lastProperties?.PointerUpdateKind} -IsLeftButtonPressed: {_lastProperties?.IsLeftButtonPressed} -IsRightButtonPressed: {_lastProperties?.IsRightButtonPressed} -IsMiddleButtonPressed: {_lastProperties?.IsMiddleButtonPressed} -IsXButton1Pressed: {_lastProperties?.IsXButton1Pressed} -IsXButton2Pressed: {_lastProperties?.IsXButton2Pressed} -IsBarrelButtonPressed: {_lastProperties?.IsBarrelButtonPressed} -IsEraser: {_lastProperties?.IsEraser} -IsInverted: {_lastProperties?.IsInverted} -Pressure: {_lastProperties?.Pressure} -XTilt: {_lastProperties?.XTilt} -YTilt: {_lastProperties?.YTilt} -Twist: {_lastProperties?.Twist}"; - _stopwatch.Restart(); - _events = 0; - } - - return true; - }, TimeSpan.FromMilliseconds(10)); - } - - protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) - { - base.OnDetachedFromVisualTree(e); - - _statusUpdated?.Dispose(); - } - - void HandleEvent(PointerEventArgs e) - { - _events++; - if (_slider != null) - { - Thread.Sleep((int)_slider.Value); - } - InvalidateVisual(); - - if (e.RoutedEvent == PointerReleasedEvent && e.Pointer.Type == PointerType.Touch) - { - _pointers.Remove(e.Pointer.Id); - return; - } - - var lastPointer = e.GetCurrentPoint(this); - _lastProperties = lastPointer.Properties; - - if (e.Pointer.Type != PointerType.Pen - || lastPointer.Properties.Pressure > 0) - { - if (!_pointers.TryGetValue(e.Pointer.Id, out var pt)) - _pointers[e.Pointer.Id] = pt = new PointerPoints(); - pt.HandleEvent(e, this); - } - } - - public override void Render(DrawingContext context) - { - context.FillRectangle(Brushes.White, Bounds); - foreach (var pt in _pointers.Values) - pt.Render(context, _drawPoints); - base.Render(context); - } - - protected override void OnPointerPressed(PointerPressedEventArgs e) - { - if (e.ClickCount == 2) - { - _pointers.Clear(); - InvalidateVisual(); - return; - } - - HandleEvent(e); - base.OnPointerPressed(e); - } - - protected override void OnPointerMoved(PointerEventArgs e) - { - HandleEvent(e); - base.OnPointerMoved(e); - } - - protected override void OnPointerReleased(PointerReleasedEventArgs e) - { - HandleEvent(e); - base.OnPointerReleased(e); - } - } -} diff --git a/samples/ControlCatalog/Pages/PointersPage.xaml b/samples/ControlCatalog/Pages/PointersPage.xaml new file mode 100644 index 0000000000..1281ec77b6 --- /dev/null +++ b/samples/ControlCatalog/Pages/PointersPage.xaml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + Capture 1 + + + Capture 2 + + + + + diff --git a/samples/ControlCatalog/Pages/PointersPage.xaml.cs b/samples/ControlCatalog/Pages/PointersPage.xaml.cs new file mode 100644 index 0000000000..977cee3d58 --- /dev/null +++ b/samples/ControlCatalog/Pages/PointersPage.xaml.cs @@ -0,0 +1,76 @@ +using System; +using Avalonia; +using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Interactivity; +using Avalonia.Markup.Xaml; + +namespace ControlCatalog.Pages; + +public class PointersPage : UserControl +{ + public PointersPage() + { + this.InitializeComponent(); + + var border1 = this.Get("BorderCapture1"); + var border2 = this.Get("BorderCapture2"); + + border1.PointerPressed += Border_PointerPressed; + border1.PointerReleased += Border_PointerReleased; + border1.PointerCaptureLost += Border_PointerCaptureLost; + border1.PointerMoved += Border_PointerUpdated; + border1.PointerEnter += Border_PointerUpdated; + border1.PointerLeave += Border_PointerUpdated; + + border2.PointerPressed += Border_PointerPressed; + border2.PointerReleased += Border_PointerReleased; + border2.PointerCaptureLost += Border_PointerCaptureLost; + border2.PointerMoved += Border_PointerUpdated; + border2.PointerEnter += Border_PointerUpdated; + border2.PointerLeave += Border_PointerUpdated; + } + + private void Border_PointerUpdated(object sender, PointerEventArgs e) + { + var textBlock = (TextBlock)((Border)sender).Child; + var position = e.GetPosition((Border)sender); + textBlock.Text = @$"Captured: {e.Pointer.Captured == sender} +PointerId: {e.Pointer.Id} +Position: {(int)position.X} {(int)position.Y}"; + e.Handled = true; + } + + private void Border_PointerCaptureLost(object sender, PointerCaptureLostEventArgs e) + { + var textBlock = (TextBlock)((Border)sender).Child; + textBlock.Text = @$"Captured: {e.Pointer.Captured == sender} +PointerId: {e.Pointer.Id} +Position: ??? ???"; + e.Handled = true; + } + + private void Border_PointerReleased(object sender, PointerReleasedEventArgs e) + { + if (e.Pointer.Captured == sender) + { + e.Pointer.Capture(null); + e.Handled = true; + } + else + { + throw new InvalidOperationException("How?"); + } + } + + private void Border_PointerPressed(object sender, PointerPressedEventArgs e) + { + e.Pointer.Capture((Border)sender); + e.Handled = true; + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } +} From e7b281e3fd9cb86db8fbabb2af933821b9eba809 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sat, 16 Apr 2022 04:32:06 -0400 Subject: [PATCH 027/224] Fix GetIntermediatePoints reverse order --- .../Avalonia.Win32/WindowImpl.AppWndProc.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index 6619c60152..1f316a0995 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -714,8 +714,6 @@ namespace Avalonia.Win32 private unsafe Lazy> CreateLazyIntermediatePoints(POINTER_INFO info) { - // Limit history size with reasonable value. - // With sizeof(POINTER_TOUCH_INFO) * 100 we can get maximum 14400 bytes. var historyCount = Math.Min((int)info.historyCount, MaxPointerHistorySize); if (historyCount > 1) { @@ -723,12 +721,15 @@ namespace Avalonia.Win32 { s_intermediatePointsPooledList.Clear(); s_intermediatePointsPooledList.Capacity = historyCount; + + // Pointers in history are ordered from newest to oldest, so we need to reverse iteration. + // Also we skip the newest pointer, because original event arguments already contains it. + if (info.pointerType == PointerInputType.PT_TOUCH) { if (GetPointerTouchInfoHistory(info.pointerId, ref historyCount, s_historyTouchInfos)) { - //last info is the same as the current so skip it - for (int i = 0; i < historyCount - 1; i++) + for (int i = historyCount - 1; i >= 1; i--) { var historyTouchInfo = s_historyTouchInfos[i]; s_intermediatePointsPooledList.Add(CreateRawPointerPoint(historyTouchInfo)); @@ -739,8 +740,8 @@ namespace Avalonia.Win32 { if (GetPointerPenInfoHistory(info.pointerId, ref historyCount, s_historyPenInfos)) { - //last info is the same as the current so skip it - for (int i = 0; i < historyCount - 1; i++) + uint timestamp = 0; + for (int i = historyCount - 1; i >= 1; i--) { var historyPenInfo = s_historyPenInfos[i]; s_intermediatePointsPooledList.Add(CreateRawPointerPoint(historyPenInfo)); @@ -752,8 +753,7 @@ namespace Avalonia.Win32 // Currently Windows does not return history info for mouse input, but we handle it just for case. if (GetPointerInfoHistory(info.pointerId, ref historyCount, s_historyInfos)) { - //last info is the same as the current so skip it - for (int i = 0; i < historyCount - 1; i++) + for (int i = historyCount - 1; i >= 1; i--) { var historyInfo = s_historyInfos[i]; s_intermediatePointsPooledList.Add(CreateRawPointerPoint(historyInfo)); From a08ccd91cba6efec6367ebc9e7d1d3cce226ae6b Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sun, 17 Apr 2022 03:44:22 -0400 Subject: [PATCH 028/224] Fix DataGrid scrolling --- src/Avalonia.Controls.DataGrid/DataGridCell.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls.DataGrid/DataGridCell.cs b/src/Avalonia.Controls.DataGrid/DataGridCell.cs index 67183781d3..ea7d91ed97 100644 --- a/src/Avalonia.Controls.DataGrid/DataGridCell.cs +++ b/src/Avalonia.Controls.DataGrid/DataGridCell.cs @@ -178,9 +178,9 @@ namespace Avalonia.Controls { var handled = OwningGrid.UpdateStateOnMouseLeftButtonDown(e, ColumnIndex, OwningRow.Slot, !e.Handled); - // Do not handle PointerPressed with touch, + // Do not handle PointerPressed with touch or pen, // so we can start scroll gesture on the same event. - if (e.Pointer.Type != PointerType.Touch) + if (e.Pointer.Type != PointerType.Touch && e.Pointer.Type != PointerType.Pen) { e.Handled = handled; } From b2556d62f5e5c6869ef8baf890a5688d024a93e5 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 16 May 2022 11:40:25 +0200 Subject: [PATCH 029/224] Fix some layout rounding issues. Fixes for #8092: - Always round sizes up, not to the nearest pixel, thereby ensuring that `DesiredSize`s don't get rounded down where possible. - Apply rounding to `Padding` and `BorderThickness` in measure pass as well as arrange pass, to ensure that `DesiredSize` takes this rounding into account. --- src/Avalonia.Base/Layout/LayoutHelper.cs | 71 ++++++++- src/Avalonia.Base/Layout/Layoutable.cs | 24 +-- src/Avalonia.Base/Point.cs | 14 +- .../DataGridColumn.cs | 2 +- .../Presenters/ContentPresenter.cs | 4 +- .../Layout/LayoutableTests.cs | 31 ---- .../Layout/LayoutableTests_LayoutRounding.cs | 140 ++++++++++++++++++ .../Rendering/SceneGraph/SceneBuilderTests.cs | 1 + .../BorderTests.cs | 65 ++++++++ .../DecoratorTests.cs | 41 +++++ .../ContentPresenterTests_Layout.cs | 66 ++++++++- .../Primitives/TrackTests.cs | 4 +- tests/Avalonia.UnitTests/TestRoot.cs | 7 +- ...estrictedHeight_VerticalAlign.expected.png | Bin 752 -> 767 bytes ...estrictedHeight_VerticalAlign.expected.png | Bin 557 -> 532 bytes 15 files changed, 414 insertions(+), 56 deletions(-) create mode 100644 tests/Avalonia.Base.UnitTests/Layout/LayoutableTests_LayoutRounding.cs diff --git a/src/Avalonia.Base/Layout/LayoutHelper.cs b/src/Avalonia.Base/Layout/LayoutHelper.cs index d24be57d2b..404d19906a 100644 --- a/src/Avalonia.Base/Layout/LayoutHelper.cs +++ b/src/Avalonia.Base/Layout/LayoutHelper.cs @@ -36,11 +36,28 @@ namespace Avalonia.Layout public static Size MeasureChild(ILayoutable? control, Size availableSize, Thickness padding, Thickness borderThickness) { - return MeasureChild(control, availableSize, padding + borderThickness); + if (IsParentLayoutRounded(control, out double scale)) + { + padding = RoundLayoutThickness(padding, scale, scale); + borderThickness = RoundLayoutThickness(borderThickness, scale, scale); + } + + if (control != null) + { + control.Measure(availableSize.Deflate(padding + borderThickness)); + return control.DesiredSize.Inflate(padding + borderThickness); + } + + return new Size().Inflate(padding + borderThickness); } public static Size MeasureChild(ILayoutable? control, Size availableSize, Thickness padding) { + if (IsParentLayoutRounded(control, out double scale)) + { + padding = RoundLayoutThickness(padding, scale, scale); + } + if (control != null) { control.Measure(availableSize.Deflate(padding)); @@ -137,7 +154,7 @@ namespace Avalonia.Layout /// /// Rounds a size to integer values for layout purposes, compensating for high DPI screen - /// coordinates. + /// coordinates by rounding the size up to the nearest pixel. /// /// Input size. /// DPI along x-dimension. @@ -149,9 +166,9 @@ namespace Avalonia.Layout /// associated with the UseLayoutRounding property and should not be used as a general rounding /// utility. /// - public static Size RoundLayoutSize(Size size, double dpiScaleX, double dpiScaleY) + public static Size RoundLayoutSizeUp(Size size, double dpiScaleX, double dpiScaleY) { - return new Size(RoundLayoutValue(size.Width, dpiScaleX), RoundLayoutValue(size.Height, dpiScaleY)); + return new Size(RoundLayoutValueUp(size.Width, dpiScaleX), RoundLayoutValueUp(size.Height, dpiScaleY)); } /// @@ -178,10 +195,9 @@ namespace Avalonia.Layout ); } - - /// - /// Calculates the value to be used for layout rounding at high DPI. + /// Calculates the value to be used for layout rounding at high DPI by rounding the value + /// up or down to the nearest pixel. /// /// Input value to be rounded. /// Ratio of screen's DPI to layout DPI @@ -217,7 +233,46 @@ namespace Avalonia.Layout return newValue; } - + + /// + /// Calculates the value to be used for layout rounding at high DPI by rounding the value up + /// to the nearest pixel. + /// + /// Input value to be rounded. + /// Ratio of screen's DPI to layout DPI + /// Adjusted value that will produce layout rounding on screen at high dpi. + /// + /// This is a layout helper method. It takes DPI into account and also does not return + /// the rounded value if it is unacceptable for layout, e.g. Infinity or NaN. It's a helper + /// associated with the UseLayoutRounding property and should not be used as a general rounding + /// utility. + /// + public static double RoundLayoutValueUp(double value, double dpiScale) + { + double newValue; + + // If DPI == 1, don't use DPI-aware rounding. + if (!MathUtilities.IsOne(dpiScale)) + { + newValue = Math.Ceiling(value * dpiScale) / dpiScale; + + // If rounding produces a value unacceptable to layout (NaN, Infinity or MaxValue), + // use the original value. + if (double.IsNaN(newValue) || + double.IsInfinity(newValue) || + MathUtilities.AreClose(newValue, double.MaxValue)) + { + newValue = value; + } + } + else + { + newValue = Math.Ceiling(value); + } + + return newValue; + } + /// /// Calculates the min and max height for a control. Ported from WPF. /// diff --git a/src/Avalonia.Base/Layout/Layoutable.cs b/src/Avalonia.Base/Layout/Layoutable.cs index df7aa937a0..0b74d5915a 100644 --- a/src/Avalonia.Base/Layout/Layoutable.cs +++ b/src/Avalonia.Base/Layout/Layoutable.cs @@ -549,6 +549,14 @@ namespace Avalonia.Layout if (IsVisible) { var margin = Margin; + var useLayoutRounding = UseLayoutRounding; + var scale = 1.0; + + if (useLayoutRounding) + { + scale = LayoutHelper.GetLayoutScale(this); + margin = LayoutHelper.RoundLayoutThickness(margin, scale, scale); + } ApplyStyling(); ApplyTemplate(); @@ -585,16 +593,14 @@ namespace Avalonia.Layout height = Math.Min(height, MaxHeight); height = Math.Max(height, MinHeight); - width = Math.Min(width, availableSize.Width); - height = Math.Min(height, availableSize.Height); - - if (UseLayoutRounding) + if (useLayoutRounding) { - var scale = LayoutHelper.GetLayoutScale(this); - width = LayoutHelper.RoundLayoutValue(width, scale); - height = LayoutHelper.RoundLayoutValue(height, scale); + (width, height) = LayoutHelper.RoundLayoutSizeUp(new Size(width, height), scale, scale); } + width = Math.Min(width, availableSize.Width); + height = Math.Min(height, availableSize.Height); + return NonNegative(new Size(width, height).Inflate(margin)); } else @@ -679,8 +685,8 @@ namespace Avalonia.Layout if (useLayoutRounding) { - size = LayoutHelper.RoundLayoutSize(size, scale, scale); - availableSizeMinusMargins = LayoutHelper.RoundLayoutSize(availableSizeMinusMargins, scale, scale); + size = LayoutHelper.RoundLayoutSizeUp(size, scale, scale); + availableSizeMinusMargins = LayoutHelper.RoundLayoutSizeUp(availableSizeMinusMargins, scale, scale); } size = ArrangeOverride(size).Constrain(size); diff --git a/src/Avalonia.Base/Point.cs b/src/Avalonia.Base/Point.cs index 67e7d71fbc..2f226caff4 100644 --- a/src/Avalonia.Base/Point.cs +++ b/src/Avalonia.Base/Point.cs @@ -192,7 +192,7 @@ namespace Avalonia } /// - /// Returns a boolean indicating whether the point is equal to the other given point. + /// Returns a boolean indicating whether the point is equal to the other given point (bitwise). /// /// The other point to test equality against. /// True if this point is equal to other; False otherwise. @@ -204,6 +204,18 @@ namespace Avalonia // ReSharper enable CompareOfFloatsByEqualityOperator } + /// + /// Returns a boolean indicating whether the point is equal to the other given point + /// (numerically). + /// + /// The other point to test equality against. + /// True if this point is equal to other; False otherwise. + public bool NearlyEquals(Point other) + { + return MathUtilities.AreClose(_x, other._x) && + MathUtilities.AreClose(_y, other._y); + } + /// /// Checks for equality between a point and an object. /// diff --git a/src/Avalonia.Controls.DataGrid/DataGridColumn.cs b/src/Avalonia.Controls.DataGrid/DataGridColumn.cs index f3ea48ff80..c415f477d4 100644 --- a/src/Avalonia.Controls.DataGrid/DataGridColumn.cs +++ b/src/Avalonia.Controls.DataGrid/DataGridColumn.cs @@ -855,7 +855,7 @@ namespace Avalonia.Controls if (OwningGrid != null && OwningGrid.UseLayoutRounding) { var scale = LayoutHelper.GetLayoutScale(HeaderCell); - var roundSize = LayoutHelper.RoundLayoutSize(new Size(leftEdge + ActualWidth, 1), scale, scale); + var roundSize = LayoutHelper.RoundLayoutSizeUp(new Size(leftEdge + ActualWidth, 1), scale, scale); LayoutRoundedWidth = roundSize.Width - leftEdge; } else diff --git a/src/Avalonia.Controls/Presenters/ContentPresenter.cs b/src/Avalonia.Controls/Presenters/ContentPresenter.cs index 996cb29534..c67678837b 100644 --- a/src/Avalonia.Controls/Presenters/ContentPresenter.cs +++ b/src/Avalonia.Controls/Presenters/ContentPresenter.cs @@ -635,8 +635,8 @@ namespace Avalonia.Controls.Presenters if (useLayoutRounding) { - sizeForChild = LayoutHelper.RoundLayoutSize(sizeForChild, scale, scale); - availableSize = LayoutHelper.RoundLayoutSize(availableSize, scale, scale); + sizeForChild = LayoutHelper.RoundLayoutSizeUp(sizeForChild, scale, scale); + availableSize = LayoutHelper.RoundLayoutSizeUp(availableSize, scale, scale); } switch (horizontalContentAlignment) diff --git a/tests/Avalonia.Base.UnitTests/Layout/LayoutableTests.cs b/tests/Avalonia.Base.UnitTests/Layout/LayoutableTests.cs index 87fa8cf1f3..f5adaf904e 100644 --- a/tests/Avalonia.Base.UnitTests/Layout/LayoutableTests.cs +++ b/tests/Avalonia.Base.UnitTests/Layout/LayoutableTests.cs @@ -173,37 +173,6 @@ namespace Avalonia.Base.UnitTests.Layout target.Verify(x => x.InvalidateMeasure(root), Times.Once()); } - [Theory] - [InlineData(16, 6, 5.333333333333333)] - [InlineData(18, 10, 4)] - public void UseLayoutRounding_Arranges_Center_Alignment_Correctly_With_Fractional_Scaling( - double containerWidth, - double childWidth, - double expectedX) - { - Border target; - var root = new TestRoot - { - LayoutScaling = 1.5, - UseLayoutRounding = true, - Child = new Decorator - { - Width = containerWidth, - Height = 100, - Child = target = new Border - { - Width = childWidth, - HorizontalAlignment = HorizontalAlignment.Center, - } - } - }; - - root.Measure(new Size(100, 100)); - root.Arrange(new Rect(target.DesiredSize)); - - Assert.Equal(new Rect(expectedX, 0, childWidth, 100), target.Bounds); - } - [Fact] public void LayoutUpdated_Is_Called_At_End_Of_Layout_Pass() { diff --git a/tests/Avalonia.Base.UnitTests/Layout/LayoutableTests_LayoutRounding.cs b/tests/Avalonia.Base.UnitTests/Layout/LayoutableTests_LayoutRounding.cs new file mode 100644 index 0000000000..77f1a8882d --- /dev/null +++ b/tests/Avalonia.Base.UnitTests/Layout/LayoutableTests_LayoutRounding.cs @@ -0,0 +1,140 @@ +using Avalonia.Controls; +using Avalonia.Layout; +using Avalonia.UnitTests; +using Xunit; +using Xunit.Sdk; + +namespace Avalonia.Base.UnitTests.Layout +{ + public class LayoutableTests_LayoutRounding + { + [Theory] + [InlineData(100, 100)] + [InlineData(101, 101.33333333333333)] + [InlineData(103, 103.33333333333333)] + public void Measure_Adjusts_DesiredSize_Upwards_When_Constraint_Allows(double desiredSize, double expectedSize) + { + var target = new TestLayoutable(new Size(desiredSize, desiredSize)); + var root = CreateRoot(1.5, target); + + root.LayoutManager.ExecuteInitialLayoutPass(); + + Assert.Equal(new Size(expectedSize, expectedSize), target.DesiredSize); + } + + [Fact] + public void Measure_Constrains_Adjusted_DesiredSize_To_Constraint() + { + var target = new TestLayoutable(new Size(101, 101)); + var root = CreateRoot(1.5, target, constraint: new Size(101, 101)); + + root.LayoutManager.ExecuteInitialLayoutPass(); + + // Desired width/height with layout rounding is 101.3333 but constraint is 101,101 so + // layout rounding should be ignored. + Assert.Equal(new Size(101, 101), target.DesiredSize); + } + + [Fact] + public void Measure_Adjusts_DesiredSize_Upwards_When_Margin_Present() + { + var target = new TestLayoutable(new Size(101, 101), margin: 1); + var root = CreateRoot(1.5, target); + + root.LayoutManager.ExecuteInitialLayoutPass(); + + // - 1 pixel margin is rounded up to 1.3333; for both sides it is 2.6666 + // - Size of 101 gets rounded up to 101.3333 + // - Final size = 101.3333 + 2.6666 = 104 + AssertEqual(new Size(104, 104), target.DesiredSize); + } + + [Fact] + public void Arrange_Adjusts_Bounds_Upwards_With_Margin() + { + var target = new TestLayoutable(new Size(101, 101), margin: 1); + var root = CreateRoot(1.5, target); + + root.LayoutManager.ExecuteInitialLayoutPass(); + + // - 1 pixel margin is rounded up to 1.3333 + // - Size of 101 gets rounded up to 101.3333 + AssertEqual(new Point(1.3333333333333333, 1.3333333333333333), target.Bounds.Position); + AssertEqual(new Size(101.33333333333333, 101.33333333333333), target.Bounds.Size); + } + + [Theory] + [InlineData(16, 6, 5.333333333333333)] + [InlineData(18, 10, 4)] + public void Arranges_Center_Alignment_Correctly_With_Fractional_Scaling( + double containerWidth, + double childWidth, + double expectedX) + { + Border target; + var root = new TestRoot + { + LayoutScaling = 1.5, + UseLayoutRounding = true, + Child = new Decorator + { + Width = containerWidth, + Height = 100, + Child = target = new Border + { + Width = childWidth, + HorizontalAlignment = HorizontalAlignment.Center, + } + } + }; + + root.Measure(new Size(100, 100)); + root.Arrange(new Rect(target.DesiredSize)); + + Assert.Equal(new Rect(expectedX, 0, childWidth, 100), target.Bounds); + } + + private static TestRoot CreateRoot( + double scaling, + Control child, + Size? constraint = null) + { + return new TestRoot + { + LayoutScaling = scaling, + UseLayoutRounding = true, + Child = child, + ClientSize = constraint ?? new Size(1000, 1000), + }; + } + + private static void AssertEqual(Point expected, Point actual) + { + if (!expected.NearlyEquals(actual)) + { + throw new EqualException(expected, actual); + } + } + + private static void AssertEqual(Size expected, Size actual) + { + if (!expected.NearlyEquals(actual)) + { + throw new EqualException(expected, actual); + } + } + + private class TestLayoutable : Control + { + private Size _desiredSize; + + public TestLayoutable(Size desiredSize, double margin = 0) + { + _desiredSize = desiredSize; + Margin = new Thickness(margin); + } + + protected override Size MeasureOverride(Size availableSize) => _desiredSize; + } + } +} diff --git a/tests/Avalonia.Base.UnitTests/Rendering/SceneGraph/SceneBuilderTests.cs b/tests/Avalonia.Base.UnitTests/Rendering/SceneGraph/SceneBuilderTests.cs index 01afe85b8b..be873c4b67 100644 --- a/tests/Avalonia.Base.UnitTests/Rendering/SceneGraph/SceneBuilderTests.cs +++ b/tests/Avalonia.Base.UnitTests/Rendering/SceneGraph/SceneBuilderTests.cs @@ -805,6 +805,7 @@ namespace Avalonia.Base.UnitTests.Rendering.SceneGraph Canvas canvas; var tree = new TestRoot { + ClientSize = new Size(100, 100), Child = decorator = new Decorator { Margin = new Thickness(0, 10, 0, 0), diff --git a/tests/Avalonia.Controls.UnitTests/BorderTests.cs b/tests/Avalonia.Controls.UnitTests/BorderTests.cs index ab33eaaff9..7af7d1cee2 100644 --- a/tests/Avalonia.Controls.UnitTests/BorderTests.cs +++ b/tests/Avalonia.Controls.UnitTests/BorderTests.cs @@ -1,6 +1,8 @@ +using Avalonia.Layout; using Avalonia.Media; using Avalonia.Rendering; using Avalonia.UnitTests; +using Avalonia.VisualTree; using Moq; using Xunit; @@ -60,5 +62,68 @@ namespace Avalonia.Controls.UnitTests renderer.Verify(x => x.AddDirty(target), Times.Once); } + + public class UseLayoutRounding + { + [Fact] + public void Measure_Rounds_Padding() + { + var target = new Border + { + Padding = new Thickness(1), + Child = new Canvas + { + Width = 101, + Height = 101, + } + }; + + var root = CreatedRoot(1.5, target); + + root.LayoutManager.ExecuteInitialLayoutPass(); + + // - 1 pixel padding is rounded up to 1.3333; for both sides it is 2.6666 + // - Size of 101 gets rounded up to 101.3333 + // - Desired size = 101.3333 + 2.6666 = 104 + Assert.Equal(new Size(104, 104), target.DesiredSize); + } + + [Fact] + public void Measure_Rounds_BorderThickness() + { + var target = new Border + { + BorderThickness = new Thickness(1), + Child = new Canvas + { + Width = 101, + Height = 101, + } + }; + + var root = CreatedRoot(1.5, target); + + root.LayoutManager.ExecuteInitialLayoutPass(); + + // - 1 pixel border thickness is rounded up to 1.3333; for both sides it is 2.6666 + // - Size of 101 gets rounded up to 101.3333 + // - Desired size = 101.3333 + 2.6666 = 104 + Assert.Equal(new Size(104, 104), target.DesiredSize); + } + + private static TestRoot CreatedRoot( + double scaling, + Control child, + Size? constraint = null) + { + return new TestRoot + { + LayoutScaling = scaling, + UseLayoutRounding = true, + Child = child, + ClientSize = constraint ?? new Size(1000, 1000), + }; + } + } } } diff --git a/tests/Avalonia.Controls.UnitTests/DecoratorTests.cs b/tests/Avalonia.Controls.UnitTests/DecoratorTests.cs index 65749efbf9..fe58cd4c7f 100644 --- a/tests/Avalonia.Controls.UnitTests/DecoratorTests.cs +++ b/tests/Avalonia.Controls.UnitTests/DecoratorTests.cs @@ -1,6 +1,7 @@ using System.Collections.Specialized; using System.Linq; using Avalonia.LogicalTree; +using Avalonia.UnitTests; using Xunit; namespace Avalonia.Controls.UnitTests @@ -116,5 +117,45 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(new Size(16, 16), target.DesiredSize); } + + public class UseLayoutRounding + { + [Fact] + public void Measure_Rounds_Padding() + { + var target = new Decorator + { + Padding = new Thickness(1), + Child = new Canvas + { + Width = 101, + Height = 101, + } + }; + + var root = CreatedRoot(1.5, target); + + root.LayoutManager.ExecuteInitialLayoutPass(); + + // - 1 pixel padding is rounded up to 1.3333; for both sides it is 2.6666 + // - Size of 101 gets rounded up to 101.3333 + // - Desired size = 101.3333 + 2.6666 = 104 + Assert.Equal(new Size(104, 104), target.DesiredSize); + } + + private static TestRoot CreatedRoot( + double scaling, + Control child, + Size? constraint = null) + { + return new TestRoot + { + LayoutScaling = scaling, + UseLayoutRounding = true, + Child = child, + ClientSize = constraint ?? new Size(1000, 1000), + }; + } + } } } diff --git a/tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Layout.cs b/tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Layout.cs index ed44fbfc32..e82050528f 100644 --- a/tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Layout.cs +++ b/tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Layout.cs @@ -1,5 +1,6 @@ using Avalonia.Controls.Presenters; using Avalonia.Layout; +using Avalonia.UnitTests; using Xunit; namespace Avalonia.Controls.UnitTests.Presenters @@ -232,5 +233,68 @@ namespace Avalonia.Controls.UnitTests.Presenters Assert.Equal(new Rect(32, 32, 0, 0), content.Bounds); } + + public class UseLayoutRounding + { + [Fact] + public void Measure_Rounds_Padding() + { + var target = new ContentPresenter + { + Padding = new Thickness(1), + Content = new Canvas + { + Width = 101, + Height = 101, + } + }; + + var root = CreatedRoot(1.5, target); + + root.LayoutManager.ExecuteInitialLayoutPass(); + + // - 1 pixel padding is rounded up to 1.3333; for both sides it is 2.6666 + // - Size of 101 gets rounded up to 101.3333 + // - Desired size = 101.3333 + 2.6666 = 104 + Assert.Equal(new Size(104, 104), target.DesiredSize); + } + + [Fact] + public void Measure_Rounds_BorderThickness() + { + var target = new ContentPresenter + { + BorderThickness = new Thickness(1), + Content = new Canvas + { + Width = 101, + Height = 101, + } + }; + + var root = CreatedRoot(1.5, target); + + root.LayoutManager.ExecuteInitialLayoutPass(); + + // - 1 pixel border thickness is rounded up to 1.3333; for both sides it is 2.6666 + // - Size of 101 gets rounded up to 101.3333 + // - Desired size = 101.3333 + 2.6666 = 104 + Assert.Equal(new Size(104, 104), target.DesiredSize); + } + + private static TestRoot CreatedRoot( + double scaling, + Control child, + Size? constraint = null) + { + return new TestRoot + { + LayoutScaling = scaling, + UseLayoutRounding = true, + Child = child, + ClientSize = constraint ?? new Size(1000, 1000), + }; + } + } } -} \ No newline at end of file +} diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/TrackTests.cs b/tests/Avalonia.Controls.UnitTests/Primitives/TrackTests.cs index 59276a94d0..f4001a8ca1 100644 --- a/tests/Avalonia.Controls.UnitTests/Primitives/TrackTests.cs +++ b/tests/Avalonia.Controls.UnitTests/Primitives/TrackTests.cs @@ -67,7 +67,7 @@ namespace Avalonia.Controls.UnitTests.Primitives target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); - Assert.Equal(new Rect(33, 0, 33, 12), thumb.Bounds); + Assert.Equal(new Rect(33, 0, 34, 12), thumb.Bounds); } [Fact] @@ -92,7 +92,7 @@ namespace Avalonia.Controls.UnitTests.Primitives target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); - Assert.Equal(new Rect(0, 33, 12, 33), thumb.Bounds); + Assert.Equal(new Rect(0, 33, 12, 34), thumb.Bounds); } [Fact] diff --git a/tests/Avalonia.UnitTests/TestRoot.cs b/tests/Avalonia.UnitTests/TestRoot.cs index 4601dd7e5b..41e29a85c4 100644 --- a/tests/Avalonia.UnitTests/TestRoot.cs +++ b/tests/Avalonia.UnitTests/TestRoot.cs @@ -41,7 +41,7 @@ namespace Avalonia.UnitTests Child = child; } - public Size ClientSize { get; set; } = new Size(100, 100); + public Size ClientSize { get; set; } = new Size(1000, 1000); public Size MaxClientSize { get; set; } = Size.Infinity; @@ -110,5 +110,10 @@ namespace Avalonia.UnitTests } Visit(this, true); } + + protected override Size MeasureOverride(Size availableSize) + { + return base.MeasureOverride(ClientSize); + } } } diff --git a/tests/TestFiles/Direct2D1/Controls/TextBlock/RestrictedHeight_VerticalAlign.expected.png b/tests/TestFiles/Direct2D1/Controls/TextBlock/RestrictedHeight_VerticalAlign.expected.png index e8624fa457f37565fdc483c474424991e7b696d3..8ca7acb845855a306af678ca22bf356780b0cba5 100644 GIT binary patch delta 679 zcmV;Y0$Ba<1^)$*K~kwnL_t(|UhUekZWBQe#_=;P6++?#fR>V$0tr#VYaoFHqUQ}j zqTo5`httHZz&q=s2Hqv))HO`Y32D$yLd9>u&fZ`DxwlC}=FnUCDFn zb~hwfB%e-?y6(N?m3510L7y(9cZxMYmZy?;*7ZBU6*{8087t3497`|8dQtY)w2jpd zI48ECk8!M=KM(V@`+Pai$bR5kf9XB6Z)|uhd2QVdQz!pLdV0cBZb@`8HRag;K+M;? z;aHdSi`N5*K2`Hla({5Vt(ZOZ#NSvK4$N0EeFe#vL8PG9QqUXUpNcg}-LZ0FO;UF( z=Ij4hlf?awSyyP(qe}|-ZJRF&dLiyeK3W$J+vcl@g4WHd?g*yxM3N(Nf2xO0VUmpqHFGSJWiy@&1&RWbC@rm ztb!hX=IF_7iN`(3BkN9*wOMz~y2TtNqsE0K)nnFRNDefgl-zmV71Czl57L(8d1Q-wI5&g@gbA N002ovPDHLkV1j5cRr>${ delta 663 zcmV;I0%-mJ1@HxsK~kDYL_t(|UhUgIa??N*$8nhhgpv-9Kst&PbZ~-{bWo>HI%eP$ zbliY0bTFi#WQHbHCZVECE@0mCYC&Ev)~@tc(Vy>U=9gVr&L&sF5? z2a=zgt*ZJWIdk3Ow4gWZj_kMw*zr#C&2{Yze1z^>+>Eo|l*ZA6u^yCDYTA#}F7b|b z1-+DUQlBp7XTR-n+9lr6_JMtu-Y0j%e~v?mF0?|+c*2;U(Dpd(9@K4;4-y?YmRFK4 z^g(@eT|_WHMd&L`zBFy;-KXYy|(KU{b8zDnNKxsvFW zH-0;ezHL90^u_#aD^}3IBtx9|jEVjQPyh0jnrD(F8Df4|Lt|DbXw1sJ6tu3b_2LwN zfr_3W`0X%Otk(2a znEu5s`c3DXRyVzJM1r%nkRJ0aMM(FjDeD5@R(^af#<|TF^PCdjI796AKN{2Wx1| x3I&Z>NnUc)osL*?lhFbeli&gb8GzoZ>JL{KmA-v_Vov}7002ovPDHLkV1ik5Q|$l% diff --git a/tests/TestFiles/Skia/Controls/TextBlock/RestrictedHeight_VerticalAlign.expected.png b/tests/TestFiles/Skia/Controls/TextBlock/RestrictedHeight_VerticalAlign.expected.png index 7bd622050e00416936f55efbc6d96232c67bd8c8..a76c6a5b2a824ec785875d92dd0b38407e3049c3 100644 GIT binary patch delta 490 zcmZ3>GKFP=N&RV07srr_IdAW5%)1;Q!uDXlX#W9ggKvom4qEyL4~4P0&0{{ck2_jY zz?fNE$h%Z>(W=lXQ?j31ZCk(J|C5mA@slS#KgDbNF)^S4hwFN;%CwgrT&UI0`gre( zyBn{Z`^PAr`Jt|XYkILgL&iym%_{3BPpMbF-QIRwbLpF=FDusivZW`ln|z07Z}8Ve zAHMMX-Jp@P^FYYz`%&?e|M>j%d7pLG=tA@w(@X6Ni@OMP+s%{|8UZRef2 zOxJB?{S~_W*Ou5%Gj2?N7|#)W?H1GDRgZPpV+}8q9Cgq?Fkj{U;Tz2DaF{%vno$(gm^S9kO54e|f1`v3Z(cq0>#J*i*%C*Mn*p3gV8zGmi= zcRgFP)>dv@6<_N1-{NIR@src0`e9#J_nmMI=P`-C@@dwgWy2)Tif!}f*BA9YQBr<>Yv<0+ed$LhGhl$6qoFtRV$DAqv>tEFoHy;= zjj8AUHLmvgqjP}4f0`Ym-l@|ncRSzK*B<`-V%Iyxa-Mt3j*FJP@}0!LwOcIxMdrlE z`EBc_9=&?AZmz%QzU4W~PG3(r%l<3q)s%g)(bchAU-i5`|6g~~!uOv(cvj{7-f@I; zZO^|R^Na=Arn~kV?eHzSTz#h~bwk)M4ct2k#G&)%(W*Dsx)C6j$|eyBU2 z#3_f(+TH&xR=QqWGxcS(?x8Q~dmJTy>788NygzpSjqgWi)>y6GoVM2Iepc=R`^UZi z3We58xyrynyb(IzS%pj{N(kv(qntpW!+KP9r*m+Cc8U!>XmlI yb8njNTQ2ZS)p@fv+pJj4eL2tLs~9ojqI);H&U7`F+X Date: Mon, 16 May 2022 17:08:53 +0200 Subject: [PATCH 030/224] Fix comparison. --- .../Converters/MenuScrollingVisibilityConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Converters/MenuScrollingVisibilityConverter.cs b/src/Avalonia.Controls/Converters/MenuScrollingVisibilityConverter.cs index 6ab2f4c517..9d859a753a 100644 --- a/src/Avalonia.Controls/Converters/MenuScrollingVisibilityConverter.cs +++ b/src/Avalonia.Controls/Converters/MenuScrollingVisibilityConverter.cs @@ -26,7 +26,7 @@ namespace Avalonia.Controls.Converters if (visibility == ScrollBarVisibility.Auto) { - if (extent == viewport) + if (MathUtilities.AreClose(extent, viewport)) { return false; } From f4cc30d4a10149d4a9d9f88578ee35b3ecfa0b0a Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 29 May 2022 22:28:28 +0200 Subject: [PATCH 031/224] Add failing test for Window.Width/Height. --- .../WindowTests.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/Avalonia.Controls.UnitTests/WindowTests.cs b/tests/Avalonia.Controls.UnitTests/WindowTests.cs index 63ccf74c2b..a8c9b68d12 100644 --- a/tests/Avalonia.Controls.UnitTests/WindowTests.cs +++ b/tests/Avalonia.Controls.UnitTests/WindowTests.cs @@ -695,6 +695,31 @@ namespace Avalonia.Controls.UnitTests } } + [Fact] + public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_Manual() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var child = new Canvas + { + Width = 400, + Height = 800, + }; + + var target = new Window() + { + SizeToContent = SizeToContent.Manual, + Content = child + }; + + Show(target); + + // Values come from MockWindowingPlatform defaults. + Assert.Equal(800, target.Width); + Assert.Equal(600, target.Height); + } + } + [Fact] public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_WidthAndHeight() { @@ -712,6 +737,8 @@ namespace Avalonia.Controls.UnitTests Content = child }; + target.GetObservable(Window.WidthProperty).Subscribe(x => { }); + Show(target); Assert.Equal(400, target.Width); From d2af5dbcac4427c22830d0bb6a2764befa180a72 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sun, 29 May 2022 22:59:50 -0400 Subject: [PATCH 032/224] Implicitly map x:DataType to a DataType on a DataTemplate. --- .../AvaloniaXamlIlCompiler.cs | 8 +- .../Transformers/XDataTypeTransformer.cs | 79 +++++++++++++++++++ .../Xaml/DataTemplateTests.cs | 58 ++++++++++++++ .../Xaml/TreeDataTemplateTests.cs | 17 ++++ 4 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/XDataTypeTransformer.cs diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlCompiler.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlCompiler.cs index 1ca7be67a7..7514b0e12e 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlCompiler.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlCompiler.cs @@ -31,10 +31,10 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions // Before everything else Transformers.Insert(0, new XNameTransformer()); - Transformers.Insert(1, new IgnoredDirectivesTransformer()); - Transformers.Insert(2, _designTransformer = new AvaloniaXamlIlDesignPropertiesTransformer()); - Transformers.Insert(3, _bindingTransformer = new AvaloniaBindingExtensionTransformer()); - + Transformers.Insert(1, new XDataTypeTransformer()); + Transformers.Insert(2, new IgnoredDirectivesTransformer()); + Transformers.Insert(3, _designTransformer = new AvaloniaXamlIlDesignPropertiesTransformer()); + Transformers.Insert(4, _bindingTransformer = new AvaloniaBindingExtensionTransformer()); // Targeted InsertBefore( diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/XDataTypeTransformer.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/XDataTypeTransformer.cs new file mode 100644 index 0000000000..7b90164974 --- /dev/null +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/XDataTypeTransformer.cs @@ -0,0 +1,79 @@ +using System.Collections.Generic; +using System.Linq; +using XamlX; +using XamlX.Ast; +using XamlX.Transform; +using XamlX.Transform.Transformers; +using XamlX.TypeSystem; + +namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers +{ + internal class XDataTypeTransformer : IXamlAstTransformer + { + private const string DataTypePropertyName = "DataType"; + + /// + /// Converts x:DataType directives to regular DataType assignments if property with Avalonia.Metadata.DataTypeAttribute exists. + /// + /// + public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node) + { + if (node is XamlAstObjectNode on) + { + for (var c = 0; c < on.Children.Count; c++) + { + var ch = on.Children[c]; + if (ch is XamlAstXmlDirective { Namespace: XamlNamespaces.Xaml2006, Name: DataTypePropertyName } d) + { + if (on.Children.OfType() + .Any(p => ((XamlAstNamePropertyReference)p.Property)?.Name == DataTypePropertyName)) + { + // Break iteration if any DataType property was already set by user code. + break; + } + + var templateDataTypeAttribute = context.GetAvaloniaTypes().DataTypeAttribute; + + var clrType = on.Type switch + { + XamlAstClrTypeReference clrRef => clrRef.Type, + XamlAstXmlTypeReference xmlRef => TypeReferenceResolver.ResolveType(context, xmlRef.Name, + on.Type.IsMarkupExtension, on, strict: false).Type, + _ => null + }; + if (clrType is null) + { + break; + } + + // Technically it's possible to map "x:DataType" to a property with [DataType] attribute regardless of its name, + // but we go explicitly strict here and check the name as well. + var (declaringType, dataTypeProperty) = GetAllProperties(clrType) + .FirstOrDefault(t => t.property.Name == DataTypePropertyName && t.property.CustomAttributes + .Any(a => a.Type == templateDataTypeAttribute)); + + if (dataTypeProperty is not null) + { + on.Children[c] = new XamlAstXamlPropertyValueNode(d, + new XamlAstNamePropertyReference(d, + new XamlAstClrTypeReference(ch, declaringType, false), dataTypeProperty.Name, + on.Type), + d.Values); + } + } + } + } + + return node; + } + + private static IEnumerable<(IXamlType declaringType, IXamlProperty property)> GetAllProperties(IXamlType t) + { + foreach (var p in t.Properties) + yield return (t, p); + if(t.BaseType!=null) + foreach (var tuple in GetAllProperties(t.BaseType)) + yield return tuple; + } + } +} diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs index 53881467e7..f9e1ce3054 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs @@ -1,5 +1,7 @@ +using System.Linq; using Avalonia.Controls; using Avalonia.Controls.Presenters; +using Avalonia.Markup.Xaml.Templates; using Avalonia.UnitTests; using Xunit; @@ -89,6 +91,62 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml } } + [Fact] + public void XDataType_Should_Be_Assigned_To_Clr_Property() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + + + + + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var target = window.FindControl("target"); + var template = (DataTemplate)window.DataTemplates.First(); + + window.ApplyTemplate(); + target.ApplyTemplate(); + ((ContentPresenter)target.Presenter).UpdateChild(); + + Assert.Equal(typeof(string), template.DataType); + Assert.IsType(target.Presenter.Child); + } + } + + [Fact] + public void XDataType_Should_Be_Ignored_If_DataType_Already_Set() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + + + + + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var target = window.FindControl("target"); + + window.ApplyTemplate(); + target.ApplyTemplate(); + ((ContentPresenter)target.Presenter).UpdateChild(); + + Assert.IsType(target.Presenter.Child); + } + } + [Fact] public void Can_Set_DataContext_In_DataTemplate() { diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TreeDataTemplateTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TreeDataTemplateTests.cs index 3fdac49f31..d4ab473d67 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TreeDataTemplateTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TreeDataTemplateTests.cs @@ -21,5 +21,22 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml Assert.IsType(template.ItemsSource); } } + + [Fact] + public void XDataType_Should_Be_Assigned_To_Clr_Property() + { + using (UnitTestApplication.Start(TestServices.MockPlatformWrapper)) + { + var xaml = @" + + +"; + var templates = (DataTemplates)AvaloniaRuntimeXamlLoader.Load(xaml); + var template = (TreeDataTemplate)(templates.First()); + + Assert.Equal(typeof(string), template.DataType); + } + } } } From 1d9645f01fda85fcbc80e11090cd672b536a559b Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sun, 29 May 2022 23:38:20 -0400 Subject: [PATCH 033/224] Validate DataTemplates --- .../Templates/DataTemplates.cs | 17 +++++++++++++++ .../Templates/ITypedDataTemplate.cs | 10 +++++++++ .../Templates/DataTemplate.cs | 2 +- .../Templates/TreeDataTemplate.cs | 2 +- .../CompiledBindingExtensionTests.cs | 4 ++-- .../Xaml/ControlBindingTests.cs | 8 +++---- .../Xaml/DataTemplateTests.cs | 21 +++++++++++++++++++ .../Xaml/TreeDataTemplateTests.cs | 2 +- 8 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 src/Avalonia.Controls/Templates/ITypedDataTemplate.cs diff --git a/src/Avalonia.Controls/Templates/DataTemplates.cs b/src/Avalonia.Controls/Templates/DataTemplates.cs index f203539536..d4eeda7908 100644 --- a/src/Avalonia.Controls/Templates/DataTemplates.cs +++ b/src/Avalonia.Controls/Templates/DataTemplates.cs @@ -1,3 +1,4 @@ +using System; using Avalonia.Collections; namespace Avalonia.Controls.Templates @@ -13,6 +14,22 @@ namespace Avalonia.Controls.Templates public DataTemplates() { ResetBehavior = ResetBehavior.Remove; + + Validate += ValidateDataTemplate; + } + + private static void ValidateDataTemplate(IDataTemplate template) + { + var valid = template switch + { + ITypedDataTemplate typed => typed.DataType is not null, + _ => true + }; + + if (!valid) + { + throw new InvalidOperationException("DataTemplate inside of DataTemplates must have a DataType set. Set DataType property or use ItemTemplate with single template instead."); + } } } } \ No newline at end of file diff --git a/src/Avalonia.Controls/Templates/ITypedDataTemplate.cs b/src/Avalonia.Controls/Templates/ITypedDataTemplate.cs new file mode 100644 index 0000000000..239dbd79f4 --- /dev/null +++ b/src/Avalonia.Controls/Templates/ITypedDataTemplate.cs @@ -0,0 +1,10 @@ +using System; +using Avalonia.Metadata; + +namespace Avalonia.Controls.Templates; + +public interface ITypedDataTemplate : IDataTemplate +{ + [DataType] + Type? DataType { get; } +} diff --git a/src/Markup/Avalonia.Markup.Xaml/Templates/DataTemplate.cs b/src/Markup/Avalonia.Markup.Xaml/Templates/DataTemplate.cs index d2b24979cc..4da6b1b791 100644 --- a/src/Markup/Avalonia.Markup.Xaml/Templates/DataTemplate.cs +++ b/src/Markup/Avalonia.Markup.Xaml/Templates/DataTemplate.cs @@ -5,7 +5,7 @@ using Avalonia.Metadata; namespace Avalonia.Markup.Xaml.Templates { - public class DataTemplate : IRecyclingDataTemplate + public class DataTemplate : IRecyclingDataTemplate, ITypedDataTemplate { [DataType] public Type DataType { get; set; } diff --git a/src/Markup/Avalonia.Markup.Xaml/Templates/TreeDataTemplate.cs b/src/Markup/Avalonia.Markup.Xaml/Templates/TreeDataTemplate.cs index 10061c3d48..04e8b0a9c0 100644 --- a/src/Markup/Avalonia.Markup.Xaml/Templates/TreeDataTemplate.cs +++ b/src/Markup/Avalonia.Markup.Xaml/Templates/TreeDataTemplate.cs @@ -9,7 +9,7 @@ using Avalonia.Metadata; namespace Avalonia.Markup.Xaml.Templates { - public class TreeDataTemplate : ITreeDataTemplate + public class TreeDataTemplate : ITreeDataTemplate, ITypedDataTemplate { [DataType] public Type DataType { get; set; } diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs index 7e721fd7b2..555a05638b 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs @@ -413,11 +413,11 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions;assembly=Avalonia.Markup.Xaml.UnitTests' x:DataType='local:TestDataContext'> - + - + "; var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlBindingTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlBindingTests.cs index 8188b212e1..affa292a7d 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlBindingTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlBindingTests.cs @@ -74,18 +74,18 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml - + - + - + - + "; diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs index 53881467e7..abbcf6c5a8 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs @@ -1,3 +1,4 @@ +using System; using Avalonia.Controls; using Avalonia.Controls.Presenters; using Avalonia.UnitTests; @@ -132,5 +133,25 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml Assert.Same(viewModel.Child.Child, canvas.DataContext); } } + + [Fact] + public void DataTemplates_Without_Type_Should_Throw() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + + + + + + +"; + Assert.Throws(() => (Window)AvaloniaRuntimeXamlLoader.Load(xaml)); + } + } } } diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TreeDataTemplateTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TreeDataTemplateTests.cs index 3fdac49f31..807b37517a 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TreeDataTemplateTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TreeDataTemplateTests.cs @@ -14,7 +14,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml { using (UnitTestApplication.Start(TestServices.MockPlatformWrapper)) { - var xaml = ""; + var xaml = ""; var templates = (DataTemplates)AvaloniaRuntimeXamlLoader.Load(xaml); var template = (TreeDataTemplate)(templates.First()); From 138be304a0d2c86c663846bfa7adfb1537058fd1 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 30 May 2022 11:53:34 +0200 Subject: [PATCH 034/224] Ensure Window.Width/Height is initialized. - Run resize logic if `Width`/`Height` are still NaN (i.e. not set up) - Always call base class `WindowBase.HandleResize` - In `WindowBase.HandleResize` don't run a layout pass if client size unchanged Fixes `Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_Manual`. --- src/Avalonia.Controls/Window.cs | 40 ++++++++++++++--------------- src/Avalonia.Controls/WindowBase.cs | 12 ++++++--- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index a4f4534b88..9b49e866b8 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -991,28 +991,28 @@ namespace Avalonia.Controls /// protected sealed override void HandleResized(Size clientSize, PlatformResizeReason reason) { - if (ClientSize == clientSize) - return; - - var sizeToContent = SizeToContent; - - // If auto-sizing is enabled, and the resize came from a user resize (or the reason was - // unspecified) then turn off auto-resizing for any window dimension that is not equal - // to the requested size. - if (sizeToContent != SizeToContent.Manual && - CanResize && - reason == PlatformResizeReason.Unspecified || - reason == PlatformResizeReason.User) + if (ClientSize != clientSize || double.IsNaN(Width) || double.IsNaN(Height)) { - if (clientSize.Width != ClientSize.Width) - sizeToContent &= ~SizeToContent.Width; - if (clientSize.Height != ClientSize.Height) - sizeToContent &= ~SizeToContent.Height; - SizeToContent = sizeToContent; - } + var sizeToContent = SizeToContent; + + // If auto-sizing is enabled, and the resize came from a user resize (or the reason was + // unspecified) then turn off auto-resizing for any window dimension that is not equal + // to the requested size. + if (sizeToContent != SizeToContent.Manual && + CanResize && + reason == PlatformResizeReason.Unspecified || + reason == PlatformResizeReason.User) + { + if (clientSize.Width != ClientSize.Width) + sizeToContent &= ~SizeToContent.Width; + if (clientSize.Height != ClientSize.Height) + sizeToContent &= ~SizeToContent.Height; + SizeToContent = sizeToContent; + } - Width = clientSize.Width; - Height = clientSize.Height; + Width = clientSize.Width; + Height = clientSize.Height; + } base.HandleResized(clientSize, reason); } diff --git a/src/Avalonia.Controls/WindowBase.cs b/src/Avalonia.Controls/WindowBase.cs index 12ba143c8a..d5e54a5c08 100644 --- a/src/Avalonia.Controls/WindowBase.cs +++ b/src/Avalonia.Controls/WindowBase.cs @@ -217,10 +217,16 @@ namespace Avalonia.Controls /// The reason for the resize. protected override void HandleResized(Size clientSize, PlatformResizeReason reason) { - ClientSize = clientSize; FrameSize = PlatformImpl?.FrameSize; - LayoutManager.ExecuteLayoutPass(); - Renderer?.Resized(clientSize); + + if (ClientSize != clientSize) + { + ClientSize = clientSize; + LayoutManager.ExecuteLayoutPass(); + Renderer?.Resized(clientSize); + } + + System.Diagnostics.Debug.WriteLine($"HandleResized: ClientSize {ClientSize} | FrameSize {FrameSize}"); } /// From 67e6c41abcbaed905bba22446ee54b995a29bed1 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 30 May 2022 11:54:34 +0200 Subject: [PATCH 035/224] Use FrameSize for window startup location. ...if available. --- src/Avalonia.Controls/Window.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index 9b49e866b8..92f74530e2 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -871,10 +871,10 @@ namespace Avalonia.Controls var scaling = owner?.DesktopScaling ?? PlatformImpl?.DesktopScaling ?? 1; - // TODO: We really need non-client size here. - var rect = new PixelRect( - PixelPoint.Origin, - PixelSize.FromSize(ClientSize, scaling)); + // Use frame size, falling back to client size if the platform can't give it to us. + var rect = FrameSize.HasValue ? + new PixelRect(PixelSize.FromSize(FrameSize.Value, scaling)) : + new PixelRect(PixelSize.FromSize(ClientSize, scaling)); if (startupLocation == WindowStartupLocation.CenterScreen) { From 4c8240b7bf349ca5bc7f74344ba45af73b77f57c Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 30 May 2022 11:57:13 +0200 Subject: [PATCH 036/224] Added integration tests for WindowStartupLocation. --- samples/IntegrationTestApp/MainWindow.axaml | 17 ++ .../IntegrationTestApp/MainWindow.axaml.cs | 38 ++++ .../IntegrationTestApp/ShowWindowTest.axaml | 26 +++ .../ShowWindowTest.axaml.cs | 40 +++++ .../Avalonia.IntegrationTests.Appium.csproj | 4 + .../WindowTests.cs | 164 ++++++++++++++++++ 6 files changed, 289 insertions(+) create mode 100644 samples/IntegrationTestApp/ShowWindowTest.axaml create mode 100644 samples/IntegrationTestApp/ShowWindowTest.axaml.cs create mode 100644 tests/Avalonia.IntegrationTests.Appium/WindowTests.cs diff --git a/samples/IntegrationTestApp/MainWindow.axaml b/samples/IntegrationTestApp/MainWindow.axaml index 19ac68b15b..5151d80932 100644 --- a/samples/IntegrationTestApp/MainWindow.axaml +++ b/samples/IntegrationTestApp/MainWindow.axaml @@ -94,6 +94,23 @@ + + + + + + NonOwned + Owned + Modal + + + Manual + CenterScreen + CenterOwner + + + + diff --git a/samples/IntegrationTestApp/MainWindow.axaml.cs b/samples/IntegrationTestApp/MainWindow.axaml.cs index 9a612aa94d..580548a433 100644 --- a/samples/IntegrationTestApp/MainWindow.axaml.cs +++ b/samples/IntegrationTestApp/MainWindow.axaml.cs @@ -5,6 +5,7 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; +using Avalonia.VisualTree; namespace IntegrationTestApp { @@ -46,6 +47,41 @@ namespace IntegrationTestApp } } + private void ShowWindow() + { + var sizeTextBox = this.GetControl("ShowWindowSize"); + var modeComboBox = this.GetControl("ShowWindowMode"); + var locationComboBox = this.GetControl("ShowWindowLocation"); + var size = !string.IsNullOrWhiteSpace(sizeTextBox.Text) ? Size.Parse(sizeTextBox.Text) : (Size?)null; + var owner = (Window)this.GetVisualRoot()!; + + var window = new ShowWindowTest + { + WindowStartupLocation = (WindowStartupLocation)locationComboBox.SelectedIndex, + }; + + if (size.HasValue) + { + window.Width = size.Value.Width; + window.Height = size.Value.Height; + } + + sizeTextBox.Text = string.Empty; + + switch (modeComboBox.SelectedIndex) + { + case 0: + window.Show(); + break; + case 1: + window.Show(owner); + break; + case 2: + window.ShowDialog(owner); + break; + } + } + private void MenuClicked(object? sender, RoutedEventArgs e) { var clickedMenuItemTextBlock = this.FindControl("ClickedMenuItem"); @@ -64,6 +100,8 @@ namespace IntegrationTestApp this.FindControl("BasicListBox").SelectedIndex = -1; if (source?.Name == "MenuClickedMenuItemReset") this.FindControl("ClickedMenuItem").Text = "None"; + if (source?.Name == "ShowWindow") + ShowWindow(); } } } diff --git a/samples/IntegrationTestApp/ShowWindowTest.axaml b/samples/IntegrationTestApp/ShowWindowTest.axaml new file mode 100644 index 0000000000..e87ae0f32c --- /dev/null +++ b/samples/IntegrationTestApp/ShowWindowTest.axaml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/IntegrationTestApp/ShowWindowTest.axaml.cs b/samples/IntegrationTestApp/ShowWindowTest.axaml.cs new file mode 100644 index 0000000000..9b55864caa --- /dev/null +++ b/samples/IntegrationTestApp/ShowWindowTest.axaml.cs @@ -0,0 +1,40 @@ +using System; +using Avalonia.Controls; +using Avalonia.Interactivity; +using Avalonia.Markup.Xaml; +using Avalonia.Rendering; + +namespace IntegrationTestApp +{ + public class ShowWindowTest : Window + { + public ShowWindowTest() + { + InitializeComponent(); + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + + protected override void OnOpened(EventArgs e) + { + base.OnOpened(e); + this.GetControl("ClientSize").Text = $"{Width}, {Height}"; + this.GetControl("FrameSize").Text = $"{FrameSize}"; + this.GetControl("Position").Text = $"{Position}"; + this.GetControl("ScreenRect").Text = $"{Screens.ScreenFromVisual(this)?.WorkingArea}"; + this.GetControl("Scaling").Text = $"{((IRenderRoot)this).RenderScaling}"; + + if (Owner is not null) + { + var ownerRect = this.GetControl("OwnerRect"); + var owner = (Window)Owner; + ownerRect.Text = $"{owner.Position}, {owner.FrameSize}"; + } + } + + private void CloseWindow_Click(object sender, RoutedEventArgs e) => Close(); + } +} diff --git a/tests/Avalonia.IntegrationTests.Appium/Avalonia.IntegrationTests.Appium.csproj b/tests/Avalonia.IntegrationTests.Appium/Avalonia.IntegrationTests.Appium.csproj index 095f0e63e0..03d9332051 100644 --- a/tests/Avalonia.IntegrationTests.Appium/Avalonia.IntegrationTests.Appium.csproj +++ b/tests/Avalonia.IntegrationTests.Appium/Avalonia.IntegrationTests.Appium.csproj @@ -5,6 +5,10 @@ enable + + + + diff --git a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs new file mode 100644 index 0000000000..771faa1925 --- /dev/null +++ b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs @@ -0,0 +1,164 @@ +using System; +using System.Linq; +using System.Runtime.InteropServices; +using Avalonia.Controls; +using OpenQA.Selenium.Appium; +using Xunit; +using Xunit.Sdk; + +namespace Avalonia.IntegrationTests.Appium +{ + [Collection("Default")] + public class WindowTests + { + private readonly AppiumDriver _session; + + public WindowTests(TestAppFixture fixture) + { + _session = fixture.Session; + + var tabs = _session.FindElementByAccessibilityId("MainTabs"); + var tab = tabs.FindElementByName("Window"); + tab.Click(); + } + + [Theory] + [MemberData(nameof(StartupLocationData))] + public void StartupLocation(string? size, ShowWindowMode mode, WindowStartupLocation location) + { + var mainWindowHandle = GetCurrentWindowHandleHack(); + + try + { + var sizeTextBox = _session.FindElementByAccessibilityId("ShowWindowSize"); + var modeComboBox = _session.FindElementByAccessibilityId("ShowWindowMode"); + var locationComboBox = _session.FindElementByAccessibilityId("ShowWindowLocation"); + var showButton = _session.FindElementByAccessibilityId("ShowWindow"); + + if (size is not null) + sizeTextBox.SendKeys(size); + + modeComboBox.Click(); + _session.FindElementByName(mode.ToString()).SendClick(); + + locationComboBox.Click(); + _session.FindElementByName(location.ToString()).SendClick(); + + showButton.Click(); + SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle); + + var clientSize = Size.Parse(_session.FindElementByAccessibilityId("ClientSize").Text); + var frameSize = Size.Parse(_session.FindElementByAccessibilityId("FrameSize").Text); + var position = PixelPoint.Parse(_session.FindElementByAccessibilityId("Position").Text); + var screenRect = PixelRect.Parse(_session.FindElementByAccessibilityId("ScreenRect").Text); + var scaling = double.Parse(_session.FindElementByAccessibilityId("Scaling").Text); + + Assert.True(frameSize.Width >= clientSize.Width, "Expected frame width >= client width."); + Assert.True(frameSize.Height > clientSize.Height, "Expected frame height > client height."); + + var frameRect = new PixelRect(position, PixelSize.FromSize(frameSize, scaling)); + + switch (location) + { + case WindowStartupLocation.CenterScreen: + { + var expected = screenRect.CenterRect(frameRect); + AssertCloseEnough(expected.Position, frameRect.Position); + break; + } + } + } + finally + { + try + { + var closeButton = _session.FindElementByAccessibilityId("CloseWindow"); + closeButton.Click(); + SwitchToMainWindowHack(mainWindowHandle); + } + catch { } + } + } + + public static TheoryData StartupLocationData() + { + var sizes = new[] { null, "400,300" }; + var data = new TheoryData(); + + foreach (var size in sizes) + { + foreach (var mode in Enum.GetValues()) + { + foreach (var location in Enum.GetValues()) + { + if (!(location == WindowStartupLocation.CenterOwner && mode == ShowWindowMode.NonOwned)) + { + data.Add(size, mode, location); + } + } + } + } + + return data; + } + + private string? GetCurrentWindowHandleHack() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // HACK: WinAppDriver only seems to switch to a newly opened window if the window has an owner, + // otherwise the session remains targeting the previous window. Return the handle for the + // current window so we know which window to switch to when another is opened. + return _session.WindowHandles.Single(); + } + + return null; + } + + private void SwitchToNewWindowHack(string? oldWindowHandle) + { + if (oldWindowHandle is not null) + { + var newWindowHandle = _session.WindowHandles.FirstOrDefault(x => x != oldWindowHandle); + + // HACK: Looks like WinAppDriver only adds window handles for non-owned windows, but luckily + // non-owned windows is where we're having the problem, so if we find a window handle that + // isn't the main window handle then switch to it. + if (newWindowHandle is not null) + _session.SwitchTo().Window(newWindowHandle); + } + } + + private void SwitchToMainWindowHack(string? mainWindowHandle) + { + if (mainWindowHandle is not null) + _session.SwitchTo().Window(mainWindowHandle); + } + + private static void AssertCloseEnough(PixelPoint expected, PixelPoint actual) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // On win32, accurate frame information cannot be obtained until a window is shown but + // WindowStartupLocation needs to be calculated before the window is shown, meaning that + // the position of a centered window can be off by a bit. From initial testing, looks + // like this shouldn't be more than 10 pixels. + if (Math.Abs(expected.X - actual.X) > 10) + throw new EqualException(expected, actual); + if (Math.Abs(expected.Y - actual.Y) > 10) + throw new EqualException(expected, actual); + } + else + { + Assert.Equal(expected, actual); + } + } + + public enum ShowWindowMode + { + NonOwned, + Owned, + Modal + } + } +} From 03e01a6e554227613ce5579cf24884e8a94ad629 Mon Sep 17 00:00:00 2001 From: daniel mayost Date: Mon, 30 May 2022 13:04:46 +0300 Subject: [PATCH 037/224] initial working --- src/Avalonia.Controls/ComboBox.cs | 21 ++-- src/Avalonia.Controls/Control.cs | 7 +- .../Rendering/SceneGraph/SceneBuilderTests.cs | 33 +++++++ .../ComboBoxTests.cs | 99 +++++++++++++++++++ .../FlowDirectionTests.cs | 41 ++++++++ 5 files changed, 181 insertions(+), 20 deletions(-) create mode 100644 tests/Avalonia.Controls.UnitTests/FlowDirectionTests.cs diff --git a/src/Avalonia.Controls/ComboBox.cs b/src/Avalonia.Controls/ComboBox.cs index cbf9b35a05..1f46a3b292 100644 --- a/src/Avalonia.Controls/ComboBox.cs +++ b/src/Avalonia.Controls/ComboBox.cs @@ -184,23 +184,10 @@ namespace Avalonia.Controls this.UpdateSelectionBoxItem(SelectedItem); } - // Because the SelectedItem isn't connected to the visual tree public override void InvalidateMirrorTransform() { base.InvalidateMirrorTransform(); - - if (SelectedItem is Control selectedControl) - { - selectedControl.InvalidateMirrorTransform(); - - foreach (var visual in selectedControl.GetVisualDescendants()) - { - if (visual is Control childControl) - { - childControl.InvalidateMirrorTransform(); - } - } - } + UpdateSelectionBoxItem(SelectedItem); } /// @@ -365,6 +352,8 @@ namespace Avalonia.Controls { parent.GetObservable(IsVisibleProperty).Subscribe(IsVisibleChanged).DisposeWith(_subscriptionsOnOpen); } + + UpdateSelectionBoxItem(SelectedItem); } private void IsVisibleChanged(bool isVisible) @@ -420,8 +409,12 @@ namespace Avalonia.Controls { control.Measure(Size.Infinity); + var flowDirection = control.IsAttachedToVisualTree ? + (control.VisualParent as Control)!.FlowDirection : FlowDirection.LeftToRight; + SelectionBoxItem = new Rectangle { + FlowDirection = flowDirection, Width = control.DesiredSize.Width, Height = control.DesiredSize.Height, Fill = new VisualBrush diff --git a/src/Avalonia.Controls/Control.cs b/src/Avalonia.Controls/Control.cs index d6a5fa0727..16d4ef5c15 100644 --- a/src/Avalonia.Controls/Control.cs +++ b/src/Avalonia.Controls/Control.cs @@ -378,17 +378,12 @@ namespace Avalonia.Controls bool bypassFlowDirectionPolicies = BypassFlowDirectionPolicies; bool parentBypassFlowDirectionPolicies = false; - var parent = this.FindAncestorOfType(); + var parent = ((IVisual)this).VisualParent as Control; if (parent != null) { parentFlowDirection = parent.FlowDirection; parentBypassFlowDirectionPolicies = parent.BypassFlowDirectionPolicies; } - else if (Parent is Control logicalParent) - { - parentFlowDirection = logicalParent.FlowDirection; - parentBypassFlowDirectionPolicies = logicalParent.BypassFlowDirectionPolicies; - } bool thisShouldBeMirrored = flowDirection == FlowDirection.RightToLeft && !bypassFlowDirectionPolicies; bool parentShouldBeMirrored = parentFlowDirection == FlowDirection.RightToLeft && !parentBypassFlowDirectionPolicies; diff --git a/tests/Avalonia.Base.UnitTests/Rendering/SceneGraph/SceneBuilderTests.cs b/tests/Avalonia.Base.UnitTests/Rendering/SceneGraph/SceneBuilderTests.cs index 01afe85b8b..5cc9f57c8e 100644 --- a/tests/Avalonia.Base.UnitTests/Rendering/SceneGraph/SceneBuilderTests.cs +++ b/tests/Avalonia.Base.UnitTests/Rendering/SceneGraph/SceneBuilderTests.cs @@ -349,6 +349,39 @@ namespace Avalonia.Base.UnitTests.Rendering.SceneGraph } } + [Fact] + public void MirrorTransform_For_Control_With_RenderTransform_Should_Be_Correct() + { + using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface)) + { + Border border; + var tree = new TestRoot + { + Width = 400, + Height = 200, + Child = border = new Border + { + HorizontalAlignment = HorizontalAlignment.Left, + Background = Brushes.Red, + Width = 100, + RenderTransform = new ScaleTransform(0.5, 1), + FlowDirection = FlowDirection.RightToLeft + } + }; + + tree.Measure(Size.Infinity); + tree.Arrange(new Rect(tree.DesiredSize)); + + var scene = new Scene(tree); + var sceneBuilder = new SceneBuilder(); + sceneBuilder.UpdateAll(scene); + + var expectedTransform = new Matrix(-1, 0, 0, 1, 100, 0) * Matrix.CreateScale(0.5, 1) * Matrix.CreateTranslation(25, 0); + var borderNode = scene.FindNode(border); + Assert.Equal(expectedTransform, borderNode.Transform); + } + } + [Fact] public void Should_Update_Border_Background_Node() { diff --git a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs index 98695fe88e..0f1925f628 100644 --- a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs @@ -336,5 +336,104 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(1, count); } } + + [Fact] + public void FlowDirection_Of_RectangleContent_Shuold_Be_LeftToRight() + { + using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface)) + { + var items = new[] + { + new ComboBoxItem() + { + Content = new Control() + } + }; + var target = new ComboBox + { + Items = items, + Template = GetTemplate() + }; + + var root = new TestRoot(target); + target.ApplyTemplate(); + target.SelectedIndex = 0; + + var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; + + Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection); + } + } + + [Fact] + public void FlowDirection_Of_RectangleContent_Updated_After_Change_ComboBox() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var items = new[] + { + new ComboBoxItem() + { + Content = new Control() + } + }; + var target = new ComboBox + { + FlowDirection = FlowDirection.RightToLeft, + Items = items, + Template = GetTemplate() + }; + + var root = new TestRoot(target); + + target.ApplyTemplate(); + target.Presenter.ApplyTemplate(); + target.SelectedIndex = 0; + + var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; + + // need help here, the 'rectangle' isn't connected to visual tree for some reason + + Assert.True(rectangle.HasMirrorTransform); + + target.FlowDirection = FlowDirection.LeftToRight; + + Assert.False(rectangle.HasMirrorTransform); + } + } + + [Fact] + public void FlowDirection_Of_RectangleContent_Updated_After_Content_In_VisualTree() + { + using (UnitTestApplication.Start(TestServices.RealFocus)) + { + Control content; + var items = new[] + { + new ComboBoxItem() + { + Content = content = new Control() + } + }; + var target = new ComboBox + { + FlowDirection = FlowDirection.RightToLeft, + Items = items, + Template = GetTemplate() + }; + + var root = new TestRoot(target); + target.ApplyTemplate(); + target.Presenter.ApplyTemplate(); + target.SelectedIndex = 0; + + // need help here how to connect 'content' tio visual tree, or how to + + + var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; + + Assert.Equal(FlowDirection.RightToLeft, rectangle.FlowDirection); + } + } } } diff --git a/tests/Avalonia.Controls.UnitTests/FlowDirectionTests.cs b/tests/Avalonia.Controls.UnitTests/FlowDirectionTests.cs new file mode 100644 index 0000000000..6739eff638 --- /dev/null +++ b/tests/Avalonia.Controls.UnitTests/FlowDirectionTests.cs @@ -0,0 +1,41 @@ +using Avalonia.Media; +using Xunit; + +namespace Avalonia.Controls.UnitTests +{ + public class FlowDirectionTests + { + [Fact] + public void HasMirrorTransform_Should_Be_True() + { + var target = new Control + { + FlowDirection = FlowDirection.RightToLeft, + }; + + Assert.True(target.HasMirrorTransform); + } + + [Fact] + public void HasMirrorTransform_Of_Children_Is_Updated_After_Change() + { + Control child; + var target = new Decorator + { + FlowDirection = FlowDirection.LeftToRight, + Child = child = new Control() + { + FlowDirection = FlowDirection.LeftToRight, + } + }; + + Assert.False(target.HasMirrorTransform); + Assert.False(child.HasMirrorTransform); + + target.FlowDirection = FlowDirection.RightToLeft; + + Assert.True(target.HasMirrorTransform); + Assert.True(child.HasMirrorTransform); + } + } +} From 05db047d2909b96b264ffded9e882bb62278ba25 Mon Sep 17 00:00:00 2001 From: daniel mayost Date: Mon, 30 May 2022 13:09:20 +0300 Subject: [PATCH 038/224] typo --- tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs index 0f1925f628..905ded193c 100644 --- a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs @@ -427,7 +427,7 @@ namespace Avalonia.Controls.UnitTests target.Presenter.ApplyTemplate(); target.SelectedIndex = 0; - // need help here how to connect 'content' tio visual tree, or how to + // need help here how to connect 'content' to visual tree, or how to open popup var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; From ee4c0f97e6e4000f01a46b591ed43afa4eba939b Mon Sep 17 00:00:00 2001 From: daniel mayost Date: Mon, 30 May 2022 14:23:13 +0300 Subject: [PATCH 039/224] remove OnAttachedToVisualTree because bug --- src/Avalonia.Controls/ComboBox.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Avalonia.Controls/ComboBox.cs b/src/Avalonia.Controls/ComboBox.cs index 1f46a3b292..8a6fb361da 100644 --- a/src/Avalonia.Controls/ComboBox.cs +++ b/src/Avalonia.Controls/ComboBox.cs @@ -178,16 +178,10 @@ namespace Avalonia.Controls ComboBoxItem.ContentTemplateProperty); } - protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) - { - base.OnAttachedToVisualTree(e); - this.UpdateSelectionBoxItem(SelectedItem); - } - public override void InvalidateMirrorTransform() { base.InvalidateMirrorTransform(); - UpdateSelectionBoxItem(SelectedItem); + UpdateSelectionBoxItem(SelectedItem); } /// From 7e6edb0f32b753d226aba58889d56c2875b6f282 Mon Sep 17 00:00:00 2001 From: daniel mayost Date: Mon, 30 May 2022 23:42:05 +0300 Subject: [PATCH 040/224] fixes bugs --- src/Avalonia.Controls/ComboBox.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/ComboBox.cs b/src/Avalonia.Controls/ComboBox.cs index 8a6fb361da..a3f87f7695 100644 --- a/src/Avalonia.Controls/ComboBox.cs +++ b/src/Avalonia.Controls/ComboBox.cs @@ -403,8 +403,8 @@ namespace Avalonia.Controls { control.Measure(Size.Infinity); - var flowDirection = control.IsAttachedToVisualTree ? - (control.VisualParent as Control)!.FlowDirection : FlowDirection.LeftToRight; + var flowDirection = + (control.VisualParent as Control)?.FlowDirection ?? FlowDirection.LeftToRight; SelectionBoxItem = new Rectangle { From 496b978cdbb830a0eb44d2b11915ff3c615c492a Mon Sep 17 00:00:00 2001 From: Max Katz Date: Mon, 30 May 2022 18:36:14 -0400 Subject: [PATCH 041/224] Run xDataType after TypeReferenceResolver --- .../CompilerExtensions/AvaloniaXamlIlCompiler.cs | 10 ++++++---- .../Transformers/XDataTypeTransformer.cs | 8 +------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlCompiler.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlCompiler.cs index 7514b0e12e..04a61e5f10 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlCompiler.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlCompiler.cs @@ -31,10 +31,9 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions // Before everything else Transformers.Insert(0, new XNameTransformer()); - Transformers.Insert(1, new XDataTypeTransformer()); - Transformers.Insert(2, new IgnoredDirectivesTransformer()); - Transformers.Insert(3, _designTransformer = new AvaloniaXamlIlDesignPropertiesTransformer()); - Transformers.Insert(4, _bindingTransformer = new AvaloniaBindingExtensionTransformer()); + Transformers.Insert(1, new IgnoredDirectivesTransformer()); + Transformers.Insert(2, _designTransformer = new AvaloniaXamlIlDesignPropertiesTransformer()); + Transformers.Insert(3, _bindingTransformer = new AvaloniaBindingExtensionTransformer()); // Targeted InsertBefore( @@ -57,6 +56,9 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions new AvaloniaXamlIlResolveByNameMarkupExtensionReplacer() ); + InsertAfter( + new XDataTypeTransformer()); + // After everything else InsertBefore( new AddNameScopeRegistration(), diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/XDataTypeTransformer.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/XDataTypeTransformer.cs index 7b90164974..845dc5f831 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/XDataTypeTransformer.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/XDataTypeTransformer.cs @@ -34,13 +34,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers var templateDataTypeAttribute = context.GetAvaloniaTypes().DataTypeAttribute; - var clrType = on.Type switch - { - XamlAstClrTypeReference clrRef => clrRef.Type, - XamlAstXmlTypeReference xmlRef => TypeReferenceResolver.ResolveType(context, xmlRef.Name, - on.Type.IsMarkupExtension, on, strict: false).Type, - _ => null - }; + var clrType = (on.Type as XamlAstClrTypeReference)?.Type; if (clrType is null) { break; From bc2179b337e969838942fcfea868934536d9ef33 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Mon, 30 May 2022 18:49:21 -0400 Subject: [PATCH 042/224] Add XDataType_Should_Be_Ignored_If_DataType_Has_Non_Standard_Name test --- .../CompiledBindingExtensionTests.cs | 3 +- .../Xaml/DataTemplateTests.cs | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs index 7e721fd7b2..a8b95d3aaa 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs @@ -17,6 +17,7 @@ using Avalonia.Markup.Xaml.Templates; using Avalonia.Media; using Avalonia.Metadata; using Avalonia.UnitTests; +using JetBrains.Annotations; using XamlX; using Xunit; @@ -1527,7 +1528,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions [TemplateContent] public object Content { get; set; } - public bool Match(object data) => FancyDataType.IsInstanceOfType(data); + public bool Match(object data) => FancyDataType?.IsInstanceOfType(data) ?? true; public IControl Build(object data) => TemplateContent.Load(Content)?.Control; } diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs index f9e1ce3054..6e99d9e3a6 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/DataTemplateTests.cs @@ -1,7 +1,11 @@ +using System; using System.Linq; using Avalonia.Controls; using Avalonia.Controls.Presenters; +using Avalonia.Controls.Templates; using Avalonia.Markup.Xaml.Templates; +using Avalonia.Markup.Xaml.UnitTests.MarkupExtensions; +using Avalonia.Metadata; using Avalonia.UnitTests; using Xunit; @@ -147,6 +151,37 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml } } + [Fact] + public void XDataType_Should_Be_Ignored_If_DataType_Has_Non_Standard_Name() + { + // We don't want DataType to be mapped to FancyDataType, avoid possible confusion. + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + + + + + + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var target = window.FindControl("target"); + + window.ApplyTemplate(); + target.ApplyTemplate(); + ((ContentPresenter)target.Presenter).UpdateChild(); + + var dataTemplate = (CustomDataTemplate)target.ContentTemplate; + Assert.Null(dataTemplate.FancyDataType); + } + } + [Fact] public void Can_Set_DataContext_In_DataTemplate() { From 402790ba86bfb6fc0f9de817cb3032b681175f38 Mon Sep 17 00:00:00 2001 From: daniel mayost Date: Tue, 31 May 2022 07:53:59 +0300 Subject: [PATCH 043/224] improve UpdateFlowDirection --- src/Avalonia.Controls/ComboBox.cs | 28 +++++++++++++++---- .../ComboBoxTests.cs | 5 ++-- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.Controls/ComboBox.cs b/src/Avalonia.Controls/ComboBox.cs index a3f87f7695..fc7feca7f1 100644 --- a/src/Avalonia.Controls/ComboBox.cs +++ b/src/Avalonia.Controls/ComboBox.cs @@ -178,10 +178,16 @@ namespace Avalonia.Controls ComboBoxItem.ContentTemplateProperty); } + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + UpdateSelectionBoxItem(SelectedItem); + } + public override void InvalidateMirrorTransform() { base.InvalidateMirrorTransform(); - UpdateSelectionBoxItem(SelectedItem); + UpdateFlowDirection(); } /// @@ -347,7 +353,7 @@ namespace Avalonia.Controls parent.GetObservable(IsVisibleProperty).Subscribe(IsVisibleChanged).DisposeWith(_subscriptionsOnOpen); } - UpdateSelectionBoxItem(SelectedItem); + UpdateFlowDirection(); } private void IsVisibleChanged(bool isVisible) @@ -403,12 +409,8 @@ namespace Avalonia.Controls { control.Measure(Size.Infinity); - var flowDirection = - (control.VisualParent as Control)?.FlowDirection ?? FlowDirection.LeftToRight; - SelectionBoxItem = new Rectangle { - FlowDirection = flowDirection, Width = control.DesiredSize.Width, Height = control.DesiredSize.Height, Fill = new VisualBrush @@ -419,6 +421,8 @@ namespace Avalonia.Controls } }; } + + UpdateFlowDirection(); } else { @@ -426,6 +430,18 @@ namespace Avalonia.Controls } } + private void UpdateFlowDirection() + { + var rectangle = SelectionBoxItem as Rectangle; + if (rectangle != null) + { + var content = (rectangle.Fill as VisualBrush)!.Visual as Control; + var flowDirection = (((IVisual)content!).VisualParent as Control)?.FlowDirection ?? FlowDirection.LeftToRight; + + rectangle.FlowDirection = flowDirection; + } + } + private void SelectFocusedItem() { foreach (ItemContainerInfo dropdownItem in ItemContainerGenerator.Containers) diff --git a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs index 905ded193c..0804de3174 100644 --- a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs @@ -351,6 +351,7 @@ namespace Avalonia.Controls.UnitTests }; var target = new ComboBox { + FlowDirection = FlowDirection.RightToLeft, Items = items, Template = GetTemplate() }; @@ -368,7 +369,7 @@ namespace Avalonia.Controls.UnitTests [Fact] public void FlowDirection_Of_RectangleContent_Updated_After_Change_ComboBox() { - using (UnitTestApplication.Start(TestServices.StyledWindow)) + using (UnitTestApplication.Start(TestServices.RealStyler)) { var items = new[] { @@ -385,10 +386,10 @@ namespace Avalonia.Controls.UnitTests }; var root = new TestRoot(target); - target.ApplyTemplate(); target.Presenter.ApplyTemplate(); target.SelectedIndex = 0; + ((ContentPresenter)target.Presenter).UpdateChild(); var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; From c77c500bcd373fa3715cce5d5a851eaab89da408 Mon Sep 17 00:00:00 2001 From: daniel mayost Date: Tue, 31 May 2022 14:35:55 +0300 Subject: [PATCH 044/224] fixes tests --- src/Avalonia.Controls/ComboBox.cs | 6 +- .../ComboBoxTests.cs | 116 +++++++++--------- 2 files changed, 61 insertions(+), 61 deletions(-) diff --git a/src/Avalonia.Controls/ComboBox.cs b/src/Avalonia.Controls/ComboBox.cs index fc7feca7f1..5ba1195159 100644 --- a/src/Avalonia.Controls/ComboBox.cs +++ b/src/Avalonia.Controls/ComboBox.cs @@ -432,11 +432,11 @@ namespace Avalonia.Controls private void UpdateFlowDirection() { - var rectangle = SelectionBoxItem as Rectangle; - if (rectangle != null) + if (SelectionBoxItem is Rectangle rectangle) { var content = (rectangle.Fill as VisualBrush)!.Visual as Control; - var flowDirection = (((IVisual)content!).VisualParent as Control)?.FlowDirection ?? FlowDirection.LeftToRight; + var flowDirection = (((IVisual)content!).VisualParent as Control)?.FlowDirection ?? + FlowDirection.LeftToRight; rectangle.FlowDirection = flowDirection; } diff --git a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs index 0804de3174..70b713d6d0 100644 --- a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs @@ -8,7 +8,7 @@ using Avalonia.Data; using Avalonia.Input; using Avalonia.LogicalTree; using Avalonia.Media; -using Avalonia.Threading; +using Avalonia.VisualTree; using Avalonia.UnitTests; using Xunit; @@ -340,80 +340,77 @@ namespace Avalonia.Controls.UnitTests [Fact] public void FlowDirection_Of_RectangleContent_Shuold_Be_LeftToRight() { - using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface)) + var items = new[] { - var items = new[] - { - new ComboBoxItem() - { - Content = new Control() - } - }; - var target = new ComboBox - { - FlowDirection = FlowDirection.RightToLeft, - Items = items, - Template = GetTemplate() - }; + new ComboBoxItem() + { + Content = new Control() + } + }; + var target = new ComboBox + { + FlowDirection = FlowDirection.RightToLeft, + Items = items, + Template = GetTemplate() + }; - var root = new TestRoot(target); - target.ApplyTemplate(); - target.SelectedIndex = 0; + var root = new TestRoot(target); + target.ApplyTemplate(); + target.SelectedIndex = 0; - var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; + var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; - Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection); - } + Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection); } [Fact] - public void FlowDirection_Of_RectangleContent_Updated_After_Change_ComboBox() + public void FlowDirection_Of_RectangleContent_Updated_After_InvalidateMirrorTransform() { - using (UnitTestApplication.Start(TestServices.RealStyler)) + var parentContent = new Decorator() { - var items = new[] - { - new ComboBoxItem() - { - Content = new Control() - } - }; - var target = new ComboBox + Child = new Control() + }; + var items = new[] + { + new ComboBoxItem() { - FlowDirection = FlowDirection.RightToLeft, - Items = items, - Template = GetTemplate() - }; - - var root = new TestRoot(target); - target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); - target.SelectedIndex = 0; - ((ContentPresenter)target.Presenter).UpdateChild(); - - var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; - - // need help here, the 'rectangle' isn't connected to visual tree for some reason + Content = parentContent.Child + } + }; + var target = new ComboBox + { + FlowDirection = FlowDirection.RightToLeft, + Items = items, + Template = GetTemplate() + }; - Assert.True(rectangle.HasMirrorTransform); + var root = new TestRoot(target); + target.ApplyTemplate(); + target.SelectedIndex = 0; - target.FlowDirection = FlowDirection.LeftToRight; + var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; + Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection); - Assert.False(rectangle.HasMirrorTransform); - } + parentContent.FlowDirection = FlowDirection.RightToLeft; + target.InvalidateMirrorTransform(); + + Assert.Equal(FlowDirection.RightToLeft, rectangle.FlowDirection); } [Fact] - public void FlowDirection_Of_RectangleContent_Updated_After_Content_In_VisualTree() + public void FlowDirection_Of_RectangleContent_Updated_After_OpenPopup() { - using (UnitTestApplication.Start(TestServices.RealFocus)) + using (UnitTestApplication.Start(TestServices.StyledWindow)) { - Control content; - var items = new[] + var parentContent = new Decorator() { + Child = new Control() + }; + var items = new[] + { new ComboBoxItem() { - Content = content = new Control() + Content = parentContent.Child } }; var target = new ComboBox @@ -425,14 +422,17 @@ namespace Avalonia.Controls.UnitTests var root = new TestRoot(target); target.ApplyTemplate(); - target.Presenter.ApplyTemplate(); target.SelectedIndex = 0; - // need help here how to connect 'content' to visual tree, or how to open popup - - var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; + Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection); + parentContent.FlowDirection = FlowDirection.RightToLeft; + + var popup = target.GetVisualDescendants().OfType().First(); + popup.PlacementTarget = new Window(); + popup.Open(); + Assert.Equal(FlowDirection.RightToLeft, rectangle.FlowDirection); } } From a2d83e8fae5a559bca83d87db7f5a30d901618ed Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Tue, 31 May 2022 13:11:07 +0100 Subject: [PATCH 045/224] [OSX] programatically implement child window relationship --- native/Avalonia.Native/src/OSX/AvnView.mm | 5 ++ native/Avalonia.Native/src/OSX/AvnWindow.mm | 38 +++++------ .../Avalonia.Native/src/OSX/WindowBaseImpl.h | 2 + .../Avalonia.Native/src/OSX/WindowBaseImpl.mm | 7 +- native/Avalonia.Native/src/OSX/WindowImpl.h | 8 ++- native/Avalonia.Native/src/OSX/WindowImpl.mm | 66 +++++++++++++------ .../Avalonia.Native/src/OSX/WindowProtocol.h | 1 - 7 files changed, 81 insertions(+), 46 deletions(-) diff --git a/native/Avalonia.Native/src/OSX/AvnView.mm b/native/Avalonia.Native/src/OSX/AvnView.mm index 5436ad22f3..bbb4d59adb 100644 --- a/native/Avalonia.Native/src/OSX/AvnView.mm +++ b/native/Avalonia.Native/src/OSX/AvnView.mm @@ -300,6 +300,11 @@ - (void)mouseDown:(NSEvent *)event { + if(_parent != nullptr) + { + _parent->BringToFront(); + } + _isLeftPressed = true; _lastMouseDownEvent = event; [self mouseEvent:event withType:LeftButtonDown]; diff --git a/native/Avalonia.Native/src/OSX/AvnWindow.mm b/native/Avalonia.Native/src/OSX/AvnWindow.mm index 590dc5e7ac..1445227cf5 100644 --- a/native/Avalonia.Native/src/OSX/AvnWindow.mm +++ b/native/Avalonia.Native/src/OSX/AvnWindow.mm @@ -183,6 +183,11 @@ return self; } +- (void)mouseDown:(NSEvent *)event +{ + _parent->BringToFront(); +} + - (BOOL)windowShouldClose:(NSWindow *)sender { auto window = dynamic_cast(_parent.getRaw()); @@ -209,7 +214,14 @@ { ComPtr parent = _parent; _parent = NULL; - [self restoreParentWindow]; + + auto window = dynamic_cast(parent.getRaw()); + + if(window != nullptr) + { + window->SetParent(nullptr); + } + parent->BaseEvents->Closed(); [parent->View onClosed]; } @@ -220,17 +232,11 @@ if(_canBecomeKeyWindow) { // If the window has a child window being shown as a dialog then don't allow it to become the key window. - for(NSWindow* uch in [self childWindows]) + auto parent = dynamic_cast(_parent.getRaw()); + + if(parent != nullptr) { - if (![uch conformsToProtocol:@protocol(AvnWindowProtocol)]) - { - continue; - } - - id ch = (id ) uch; - - if(ch.isDialog) - return false; + return parent->CanBecomeKeyWindow(); } return true; @@ -273,16 +279,6 @@ [super becomeKeyWindow]; } --(void) restoreParentWindow; -{ - auto parent = [self parentWindow]; - - if(parent != nil) - { - [parent removeChildWindow:self]; - } -} - - (void)windowDidMiniaturize:(NSNotification *)notification { auto parent = dynamic_cast(_parent.operator->()); diff --git a/native/Avalonia.Native/src/OSX/WindowBaseImpl.h b/native/Avalonia.Native/src/OSX/WindowBaseImpl.h index 83850e780c..62c0e2069d 100644 --- a/native/Avalonia.Native/src/OSX/WindowBaseImpl.h +++ b/native/Avalonia.Native/src/OSX/WindowBaseImpl.h @@ -99,6 +99,8 @@ BEGIN_INTERFACE_MAP() virtual bool IsDialog(); id GetWindowProtocol (); + + virtual void BringToFront (); protected: virtual NSWindowStyleMask GetStyle(); diff --git a/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm b/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm index 121679b942..77f0f47934 100644 --- a/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm @@ -143,8 +143,6 @@ HRESULT WindowBaseImpl::Hide() { @autoreleasepool { if (Window != nullptr) { [Window orderOut:Window]; - - [GetWindowProtocol() restoreParentWindow]; } return S_OK; @@ -610,6 +608,11 @@ id WindowBaseImpl::GetWindowProtocol() { return (id ) Window; } +void WindowBaseImpl::BringToFront() +{ + // do nothing. +} + extern IAvnWindow* CreateAvnWindow(IAvnWindowEvents*events, IAvnGlContext* gl) { @autoreleasepool diff --git a/native/Avalonia.Native/src/OSX/WindowImpl.h b/native/Avalonia.Native/src/OSX/WindowImpl.h index 35627685a2..76d5cbf6ea 100644 --- a/native/Avalonia.Native/src/OSX/WindowImpl.h +++ b/native/Avalonia.Native/src/OSX/WindowImpl.h @@ -8,6 +8,7 @@ #import "WindowBaseImpl.h" #include "IWindowStateChanged.h" +#include class WindowImpl : public virtual WindowBaseImpl, public virtual IAvnWindow, public IWindowStateChanged { @@ -22,7 +23,8 @@ private: bool _transitioningWindowState; bool _isClientAreaExtended; bool _isDialog; - WindowImpl* _lastParent; + WindowImpl* _parent; + std::list _children; AvnExtendClientAreaChromeHints _extendClientHints; FORWARD_IUNKNOWN() @@ -91,6 +93,10 @@ BEGIN_INTERFACE_MAP() virtual bool IsDialog() override; virtual void OnInitialiseNSWindow() override; + + virtual void BringToFront () override; + + bool CanBecomeKeyWindow (); protected: virtual NSWindowStyleMask GetStyle() override; diff --git a/native/Avalonia.Native/src/OSX/WindowImpl.mm b/native/Avalonia.Native/src/OSX/WindowImpl.mm index ad804eb280..5333cb23c8 100644 --- a/native/Avalonia.Native/src/OSX/WindowImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowImpl.mm @@ -10,6 +10,7 @@ #include "WindowProtocol.h" WindowImpl::WindowImpl(IAvnWindowEvents *events, IAvnGlContext *gl) : WindowBaseImpl(events, gl) { + _children = std::list(); _isClientAreaExtended = false; _extendClientHints = AvnDefaultChrome; _fullScreenActive = false; @@ -20,7 +21,7 @@ WindowImpl::WindowImpl(IAvnWindowEvents *events, IAvnGlContext *gl) : WindowBase _lastWindowState = Normal; _actualWindowState = Normal; _lastTitle = @""; - _lastParent = nullptr; + _parent = nullptr; WindowEvents = events; } @@ -63,9 +64,9 @@ void WindowImpl::OnInitialiseNSWindow(){ SetExtendClientArea(true); } - if(_lastParent != nullptr) + if(_parent != nullptr) { - SetParent(_lastParent); + SetParent(_parent); } } @@ -96,33 +97,56 @@ HRESULT WindowImpl::SetParent(IAvnWindow *parent) { START_COM_CALL; @autoreleasepool { - if (parent == nullptr) - return E_POINTER; + if(_parent != nullptr) + { + _parent->_children.remove(this); + } auto cparent = dynamic_cast(parent); - if (cparent == nullptr) - return E_INVALIDARG; - - _lastParent = cparent; + _parent = cparent; - if(Window != nullptr){ - // If one tries to show a child window with a minimized parent window, then the parent window will be - // restored but macOS isn't kind enough to *tell* us that, so the window will be left in a non-interactive - // state. Detect this and explicitly restore the parent window ourselves to avoid this situation. - if (cparent->WindowState() == Minimized) - cparent->SetWindowState(Normal); - - [Window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenAuxiliary]; - [cparent->Window addChildWindow:Window ordered:NSWindowAbove]; - - UpdateStyle(); + if(_parent != nullptr && Window != nullptr){ + // If one tries to show a child window with a minimized parent window, then the parent window will be + // restored but macOS isn't kind enough to *tell* us that, so the window will be left in a non-interactive + // state. Detect this and explicitly restore the parent window ourselves to avoid this situation. + if (cparent->WindowState() == Minimized) + cparent->SetWindowState(Normal); + + [Window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenAuxiliary]; + + cparent->_children.push_back(this); + + UpdateStyle(); } return S_OK; } } +void WindowImpl::BringToFront() +{ + Activate(); + + for(auto iterator = _children.begin(); iterator != _children.end(); iterator++) + { + (*iterator)->BringToFront(); + } +} + +bool WindowImpl::CanBecomeKeyWindow() +{ + for(auto iterator = _children.begin(); iterator != _children.end(); iterator++) + { + if((*iterator)->IsDialog()) + { + return false; + } + } + + return true; +} + void WindowImpl::StartStateTransition() { _transitioningWindowState = true; } @@ -534,7 +558,7 @@ bool WindowImpl::IsDialog() { } NSWindowStyleMask WindowImpl::GetStyle() { - unsigned long s = this->_isDialog ? NSWindowStyleMaskDocModalWindow : NSWindowStyleMaskBorderless; + unsigned long s = NSWindowStyleMaskBorderless; switch (_decorations) { case SystemDecorationsNone: diff --git a/native/Avalonia.Native/src/OSX/WindowProtocol.h b/native/Avalonia.Native/src/OSX/WindowProtocol.h index 0e5c5869e7..cb5f86bdb9 100644 --- a/native/Avalonia.Native/src/OSX/WindowProtocol.h +++ b/native/Avalonia.Native/src/OSX/WindowProtocol.h @@ -11,7 +11,6 @@ @protocol AvnWindowProtocol -(void) pollModalSession: (NSModalSession _Nonnull) session; --(void) restoreParentWindow; -(bool) shouldTryToHandleEvents; -(void) setEnabled: (bool) enable; -(void) showAppMenuOnly; From dc1b6a669b8a90bc71344ea6b6df28e2b489e7a0 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Tue, 31 May 2022 13:50:01 +0100 Subject: [PATCH 046/224] [osx] make bringtofront work correctly for owned and modal windows. --- native/Avalonia.Native/src/OSX/AvnView.mm | 5 ----- native/Avalonia.Native/src/OSX/AvnWindow.mm | 9 +++------ native/Avalonia.Native/src/OSX/WindowImpl.mm | 9 ++++++++- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/native/Avalonia.Native/src/OSX/AvnView.mm b/native/Avalonia.Native/src/OSX/AvnView.mm index bbb4d59adb..5436ad22f3 100644 --- a/native/Avalonia.Native/src/OSX/AvnView.mm +++ b/native/Avalonia.Native/src/OSX/AvnView.mm @@ -300,11 +300,6 @@ - (void)mouseDown:(NSEvent *)event { - if(_parent != nullptr) - { - _parent->BringToFront(); - } - _isLeftPressed = true; _lastMouseDownEvent = event; [self mouseEvent:event withType:LeftButtonDown]; diff --git a/native/Avalonia.Native/src/OSX/AvnWindow.mm b/native/Avalonia.Native/src/OSX/AvnWindow.mm index 1445227cf5..60fdb26121 100644 --- a/native/Avalonia.Native/src/OSX/AvnWindow.mm +++ b/native/Avalonia.Native/src/OSX/AvnWindow.mm @@ -183,11 +183,6 @@ return self; } -- (void)mouseDown:(NSEvent *)event -{ - _parent->BringToFront(); -} - - (BOOL)windowShouldClose:(NSWindow *)sender { auto window = dynamic_cast(_parent.getRaw()); @@ -435,8 +430,10 @@ _parent->BaseEvents->RawMouseEvent(NonClientLeftButtonDown, static_cast([event timestamp] * 1000), AvnInputModifiersNone, point, delta); } + + _parent->BringToFront(); } - break; + break; case NSEventTypeMouseEntered: { diff --git a/native/Avalonia.Native/src/OSX/WindowImpl.mm b/native/Avalonia.Native/src/OSX/WindowImpl.mm index 5333cb23c8..8330f4ed86 100644 --- a/native/Avalonia.Native/src/OSX/WindowImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowImpl.mm @@ -126,7 +126,14 @@ HRESULT WindowImpl::SetParent(IAvnWindow *parent) { void WindowImpl::BringToFront() { - Activate(); + if(IsDialog()) + { + Activate(); + } + else + { + [Window orderFront:nullptr]; + } for(auto iterator = _children.begin(); iterator != _children.end(); iterator++) { From f7daa81fbe8d684c13d0d10c8fb2727734e2e480 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Tue, 31 May 2022 14:10:33 +0100 Subject: [PATCH 047/224] dont create nspanel / nswindow at show. --- native/Avalonia.Native/src/OSX/WindowBaseImpl.mm | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm b/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm index 77f0f47934..d105f4cf38 100644 --- a/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm @@ -36,8 +36,10 @@ WindowBaseImpl::WindowBaseImpl(IAvnWindowBaseEvents *events, IAvnGlContext *gl) lastMaxSize = NSSize { CGFLOAT_MAX, CGFLOAT_MAX}; lastMinSize = NSSize { 0, 0 }; - Window = nullptr; lastMenu = nullptr; + + CreateNSWindow(false); + InitialiseNSWindow(); } HRESULT WindowBaseImpl::ObtainNSViewHandle(void **ret) { @@ -88,7 +90,6 @@ HRESULT WindowBaseImpl::Show(bool activate, bool isDialog) { START_COM_CALL; @autoreleasepool { - CreateNSWindow(isDialog); InitialiseNSWindow(); if(hasPosition) @@ -585,6 +586,7 @@ void WindowBaseImpl::InitialiseNSWindow() { [Window setOpaque:false]; + [Window setHasShadow:true]; [Window invalidateShadow]; if (lastMenu != nullptr) { From 041fdb6bc9bcb06711c7715692e2d5771ad6a2eb Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Tue, 31 May 2022 14:11:19 +0100 Subject: [PATCH 048/224] call bring to front when window is made key. --- native/Avalonia.Native/src/OSX/AvnWindow.mm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/native/Avalonia.Native/src/OSX/AvnWindow.mm b/native/Avalonia.Native/src/OSX/AvnWindow.mm index 60fdb26121..52ee48317c 100644 --- a/native/Avalonia.Native/src/OSX/AvnWindow.mm +++ b/native/Avalonia.Native/src/OSX/AvnWindow.mm @@ -274,6 +274,11 @@ [super becomeKeyWindow]; } +- (void)windowDidBecomeKey:(NSNotification *)notification +{ + _parent->BringToFront(); +} + - (void)windowDidMiniaturize:(NSNotification *)notification { auto parent = dynamic_cast(_parent.operator->()); From 2ea49defb60cf0a8412dabde876a2d8c27e1d8c5 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Tue, 31 May 2022 14:28:44 +0100 Subject: [PATCH 049/224] [osx] easily support using nspanel from windowbaseimpl. --- native/Avalonia.Native/src/OSX/WindowBaseImpl.h | 2 +- native/Avalonia.Native/src/OSX/WindowBaseImpl.mm | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/native/Avalonia.Native/src/OSX/WindowBaseImpl.h b/native/Avalonia.Native/src/OSX/WindowBaseImpl.h index 62c0e2069d..4220811fc7 100644 --- a/native/Avalonia.Native/src/OSX/WindowBaseImpl.h +++ b/native/Avalonia.Native/src/OSX/WindowBaseImpl.h @@ -26,7 +26,7 @@ BEGIN_INTERFACE_MAP() virtual ~WindowBaseImpl(); - WindowBaseImpl(IAvnWindowBaseEvents *events, IAvnGlContext *gl); + WindowBaseImpl(IAvnWindowBaseEvents *events, IAvnGlContext *gl, bool usePanel = false); virtual HRESULT ObtainNSWindowHandle(void **ret) override; diff --git a/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm b/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm index d105f4cf38..e88c7f208c 100644 --- a/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm @@ -21,7 +21,7 @@ WindowBaseImpl::~WindowBaseImpl() { Window = nullptr; } -WindowBaseImpl::WindowBaseImpl(IAvnWindowBaseEvents *events, IAvnGlContext *gl) { +WindowBaseImpl::WindowBaseImpl(IAvnWindowBaseEvents *events, IAvnGlContext *gl, bool usePanel) { _shown = false; _inResize = false; BaseEvents = events; @@ -38,7 +38,7 @@ WindowBaseImpl::WindowBaseImpl(IAvnWindowBaseEvents *events, IAvnGlContext *gl) lastMenu = nullptr; - CreateNSWindow(false); + CreateNSWindow(usePanel); InitialiseNSWindow(); } From 76abbc8fbef53c161763e0387d46ace4b22819e0 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Tue, 31 May 2022 16:34:22 +0100 Subject: [PATCH 050/224] ensure frameSize is refreshed when window is show. --- src/Avalonia.Controls/TopLevel.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 57fb82485c..f2e8cdb1cf 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -484,7 +484,11 @@ namespace Avalonia.Controls /// Raises the event. /// /// The event args. - protected virtual void OnOpened(EventArgs e) => Opened?.Invoke(this, e); + protected virtual void OnOpened(EventArgs e) + { + FrameSize = PlatformImpl?.FrameSize; + Opened?.Invoke(this, e); + } /// /// Raises the event. From 67b6811ca8d18ed7e4063cb712577a260a0de232 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Tue, 31 May 2022 16:34:33 +0100 Subject: [PATCH 051/224] add comment to show why test isnt working. --- samples/IntegrationTestApp/ShowWindowTest.axaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/IntegrationTestApp/ShowWindowTest.axaml.cs b/samples/IntegrationTestApp/ShowWindowTest.axaml.cs index 9b55864caa..f5f87c5c1c 100644 --- a/samples/IntegrationTestApp/ShowWindowTest.axaml.cs +++ b/samples/IntegrationTestApp/ShowWindowTest.axaml.cs @@ -25,7 +25,7 @@ namespace IntegrationTestApp this.GetControl("FrameSize").Text = $"{FrameSize}"; this.GetControl("Position").Text = $"{Position}"; this.GetControl("ScreenRect").Text = $"{Screens.ScreenFromVisual(this)?.WorkingArea}"; - this.GetControl("Scaling").Text = $"{((IRenderRoot)this).RenderScaling}"; + this.GetControl("Scaling").Text = $"{((IRenderRoot)this).RenderScaling}"; // TODO Use DesktopScaling from WindowImpl. if (Owner is not null) { From 5bf28b671d42b5682930e27014d5897fe90bda73 Mon Sep 17 00:00:00 2001 From: daniel mayost Date: Tue, 31 May 2022 20:42:16 +0300 Subject: [PATCH 052/224] some fixes --- tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs index 70b713d6d0..aa32af7e51 100644 --- a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs @@ -379,7 +379,6 @@ namespace Avalonia.Controls.UnitTests }; var target = new ComboBox { - FlowDirection = FlowDirection.RightToLeft, Items = items, Template = GetTemplate() }; @@ -392,7 +391,7 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection); parentContent.FlowDirection = FlowDirection.RightToLeft; - target.InvalidateMirrorTransform(); + target.FlowDirection = FlowDirection.RightToLeft; Assert.Equal(FlowDirection.RightToLeft, rectangle.FlowDirection); } From cef238d1950e7ec195f4d8f201181bde29b7f207 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 1 Jun 2022 10:10:43 +0100 Subject: [PATCH 053/224] dispatch bring to front parent when removing a child window. --- native/Avalonia.Native/src/OSX/WindowImpl.mm | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/native/Avalonia.Native/src/OSX/WindowImpl.mm b/native/Avalonia.Native/src/OSX/WindowImpl.mm index 8330f4ed86..7776b2912d 100644 --- a/native/Avalonia.Native/src/OSX/WindowImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowImpl.mm @@ -100,6 +100,11 @@ HRESULT WindowImpl::SetParent(IAvnWindow *parent) { if(_parent != nullptr) { _parent->_children.remove(this); + auto parent = _parent; + + dispatch_async(dispatch_get_main_queue(), ^{ + parent->BringToFront(); + }); } auto cparent = dynamic_cast(parent); From f017acae5685fe8b7c5f9fdf9cbd4308dea08c2e Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 1 Jun 2022 10:15:54 +0100 Subject: [PATCH 054/224] dialogs should not be minimizable. --- native/Avalonia.Native/src/OSX/WindowImpl.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/Avalonia.Native/src/OSX/WindowImpl.mm b/native/Avalonia.Native/src/OSX/WindowImpl.mm index 7776b2912d..8520e3e470 100644 --- a/native/Avalonia.Native/src/OSX/WindowImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowImpl.mm @@ -590,7 +590,7 @@ NSWindowStyleMask WindowImpl::GetStyle() { break; } - if ([Window parentWindow] == nullptr) { + if (!IsDialog()) { s |= NSWindowStyleMaskMiniaturizable; } From b6e66047f21cec797302c305b487b93881b7fa1c Mon Sep 17 00:00:00 2001 From: daniel mayost Date: Wed, 1 Jun 2022 13:54:50 +0300 Subject: [PATCH 055/224] add fixes --- src/Avalonia.Controls/ComboBox.cs | 11 ++++++----- .../FlowDirectionTests.cs | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/Avalonia.Controls/ComboBox.cs b/src/Avalonia.Controls/ComboBox.cs index 5ba1195159..05be5ad00d 100644 --- a/src/Avalonia.Controls/ComboBox.cs +++ b/src/Avalonia.Controls/ComboBox.cs @@ -434,11 +434,12 @@ namespace Avalonia.Controls { if (SelectionBoxItem is Rectangle rectangle) { - var content = (rectangle.Fill as VisualBrush)!.Visual as Control; - var flowDirection = (((IVisual)content!).VisualParent as Control)?.FlowDirection ?? - FlowDirection.LeftToRight; - - rectangle.FlowDirection = flowDirection; + if ((rectangle.Fill as VisualBrush)?.Visual is Control content) + { + var flowDirection = (((IVisual)content!).VisualParent as Control)?.FlowDirection ?? + FlowDirection.LeftToRight; + rectangle.FlowDirection = flowDirection; + } } } diff --git a/tests/Avalonia.Controls.UnitTests/FlowDirectionTests.cs b/tests/Avalonia.Controls.UnitTests/FlowDirectionTests.cs index 6739eff638..6c43103ecb 100644 --- a/tests/Avalonia.Controls.UnitTests/FlowDirectionTests.cs +++ b/tests/Avalonia.Controls.UnitTests/FlowDirectionTests.cs @@ -17,7 +17,23 @@ namespace Avalonia.Controls.UnitTests } [Fact] - public void HasMirrorTransform_Of_Children_Is_Updated_After_Change() + public void HasMirrorTransform_Of_LTR_Children_Should_Be_True_For_RTL_Parent() + { + Control child; + var target = new Decorator + { + FlowDirection = FlowDirection.RightToLeft, + Child = child = new Control() + }; + + child.FlowDirection = FlowDirection.LeftToRight; + + Assert.True(target.HasMirrorTransform); + Assert.True(child.HasMirrorTransform); + } + + [Fact] + public void HasMirrorTransform_Of_Children_Is_Updated_After_Parent_Changeed() { Control child; var target = new Decorator From 0d6e3a55f016bf521263735eac066d26bab37d47 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 1 Jun 2022 17:07:02 +0100 Subject: [PATCH 056/224] add test to show that osx chrome buttons are disabled when modal dialog is opened. --- .../WindowTests.cs | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs index 771faa1925..a82c46b927 100644 --- a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs @@ -22,6 +22,63 @@ namespace Avalonia.IntegrationTests.Appium tab.Click(); } + [PlatformFact(SkipOnWindows = true)] + public void OSX_Parent_Window_Has_Disabled_ChromeButtons_When_Modal_Dialog_Shown() + { + var mainWindowHandle = GetCurrentWindowHandleHack(); + + try + { + var sizeTextBox = _session.FindElementByAccessibilityId("ShowWindowSize"); + var modeComboBox = _session.FindElementByAccessibilityId("ShowWindowMode"); + var locationComboBox = _session.FindElementByAccessibilityId("ShowWindowLocation"); + var showButton = _session.FindElementByAccessibilityId("ShowWindow"); + + var closeButton = + _session.FindElementByXPath( + "/XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeButton[1]"); + + var zoomButton = + _session.FindElementByXPath( + "/XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeButton[2]"); + + var miniturizeButton = + _session.FindElementByXPath( + "/XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeButton[3]"); + + + Assert.True(closeButton.Enabled); + Assert.True(zoomButton.Enabled); + Assert.True(miniturizeButton.Enabled); + + sizeTextBox.SendKeys("400, 400"); + + modeComboBox.Click(); + _session.FindElementByName(ShowWindowMode.Modal.ToString()).SendClick(); + + locationComboBox.Click(); + _session.FindElementByName(WindowStartupLocation.CenterOwner.ToString()).SendClick(); + + showButton.Click(); + + SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle); + + Assert.False(closeButton.Enabled); + Assert.False(zoomButton.Enabled); + Assert.False(miniturizeButton.Enabled); + } + finally + { + try + { + var closeButton = _session.FindElementByAccessibilityId("CloseWindow"); + closeButton.Click(); + SwitchToMainWindowHack(mainWindowHandle); + } + catch { } + } + } + [Theory] [MemberData(nameof(StartupLocationData))] public void StartupLocation(string? size, ShowWindowMode mode, WindowStartupLocation location) @@ -52,7 +109,7 @@ namespace Avalonia.IntegrationTests.Appium var position = PixelPoint.Parse(_session.FindElementByAccessibilityId("Position").Text); var screenRect = PixelRect.Parse(_session.FindElementByAccessibilityId("ScreenRect").Text); var scaling = double.Parse(_session.FindElementByAccessibilityId("Scaling").Text); - + Assert.True(frameSize.Width >= clientSize.Width, "Expected frame width >= client width."); Assert.True(frameSize.Height > clientSize.Height, "Expected frame height > client height."); From b7397a7cdf710fe17d49ee3993196fefd734d74b Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 1 Jun 2022 17:56:36 +0100 Subject: [PATCH 057/224] add a11y identifiers for windows. --- samples/IntegrationTestApp/MainWindow.axaml | 1 + samples/IntegrationTestApp/ShowWindowTest.axaml | 1 + 2 files changed, 2 insertions(+) diff --git a/samples/IntegrationTestApp/MainWindow.axaml b/samples/IntegrationTestApp/MainWindow.axaml index 5151d80932..82348691e9 100644 --- a/samples/IntegrationTestApp/MainWindow.axaml +++ b/samples/IntegrationTestApp/MainWindow.axaml @@ -4,6 +4,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="IntegrationTestApp.MainWindow" + Name="MainWindow" Title="IntegrationTestApp"> diff --git a/samples/IntegrationTestApp/ShowWindowTest.axaml b/samples/IntegrationTestApp/ShowWindowTest.axaml index e87ae0f32c..2525bfc866 100644 --- a/samples/IntegrationTestApp/ShowWindowTest.axaml +++ b/samples/IntegrationTestApp/ShowWindowTest.axaml @@ -1,6 +1,7 @@ From a7713dcda9d0b40d9f1dbcbd21ce2d4cea94f9ef Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 1 Jun 2022 17:56:52 +0100 Subject: [PATCH 058/224] add a unit test for child window order osx. --- .../WindowTests.cs | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs index a82c46b927..1f5ac052ec 100644 --- a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs @@ -2,7 +2,9 @@ using System.Linq; using System.Runtime.InteropServices; using Avalonia.Controls; +using OpenQA.Selenium; using OpenQA.Selenium.Appium; +using SeleniumExtras.PageObjects; using Xunit; using Xunit.Sdk; @@ -21,6 +23,79 @@ namespace Avalonia.IntegrationTests.Appium var tab = tabs.FindElementByName("Window"); tab.Click(); } + + [PlatformFact(SkipOnWindows = true)] + public void OSX_WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent() + { + var mainWindowHandle = GetCurrentWindowHandleHack(); + + try + { + var sizeTextBox = _session.FindElementByAccessibilityId("ShowWindowSize"); + var modeComboBox = _session.FindElementByAccessibilityId("ShowWindowMode"); + var locationComboBox = _session.FindElementByAccessibilityId("ShowWindowLocation"); + var showButton = _session.FindElementByAccessibilityId("ShowWindow"); + + var mainWindow = + _session.FindElementByAccessibilityId("MainWindow"); + + + + sizeTextBox.SendKeys("200, 100"); + + modeComboBox.Click(); + _session.FindElementByName(ShowWindowMode.Modal.ToString()).SendClick(); + + locationComboBox.Click(); + _session.FindElementByName(WindowStartupLocation.CenterOwner.ToString()).SendClick(); + + showButton.Click(); + + var secondaryWindow = _session.FindElementByAccessibilityId("SecondaryWindow"); + + mainWindow.Click(); + + var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); + + int i = 0; + int mainWindowIndex = 0; + int secondaryWindowIndex = 0; + + foreach (var window in windows) + { + i++; + + var child = window.FindElementByXPath("XCUIElementTypeWindow"); + + switch (child.GetAttribute("identifier")) + { + case "MainWindow": + mainWindowIndex = i; + break; + + case "SecondaryWindow": + secondaryWindowIndex = i; + break; + } + + } + + Assert.Equal(1, secondaryWindowIndex); + Assert.Equal(2, mainWindowIndex); + + SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle); + } + finally + { + try + { + var closeButton = _session.FindElementByAccessibilityId("CloseWindow"); + closeButton.Click(); + SwitchToMainWindowHack(mainWindowHandle); + } + catch { } + } + } [PlatformFact(SkipOnWindows = true)] public void OSX_Parent_Window_Has_Disabled_ChromeButtons_When_Modal_Dialog_Shown() From a70dadea18bd0ff1fb9e0b1a6fa657659eb608a4 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 1 Jun 2022 18:17:15 +0100 Subject: [PATCH 059/224] simplify test. --- .../WindowTests.cs | 42 +++++++------------ 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs index 1f5ac052ec..8d6be4d199 100644 --- a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using Avalonia.Controls; @@ -39,8 +40,6 @@ namespace Avalonia.IntegrationTests.Appium var mainWindow = _session.FindElementByAccessibilityId("MainWindow"); - - sizeTextBox.SendKeys("200, 100"); modeComboBox.Click(); @@ -51,37 +50,15 @@ namespace Avalonia.IntegrationTests.Appium showButton.Click(); - var secondaryWindow = _session.FindElementByAccessibilityId("SecondaryWindow"); - mainWindow.Click(); var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); - int i = 0; - int mainWindowIndex = 0; - int secondaryWindowIndex = 0; + int mainWindowIndex = windows.GetWindowOrder("MainWindow"); + int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); - foreach (var window in windows) - { - i++; - - var child = window.FindElementByXPath("XCUIElementTypeWindow"); - - switch (child.GetAttribute("identifier")) - { - case "MainWindow": - mainWindowIndex = i; - break; - - case "SecondaryWindow": - secondaryWindowIndex = i; - break; - } - - } - - Assert.Equal(1, secondaryWindowIndex); - Assert.Equal(2, mainWindowIndex); + Assert.Equal(0, secondaryWindowIndex); + Assert.Equal(1, mainWindowIndex); SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle); } @@ -293,4 +270,13 @@ namespace Avalonia.IntegrationTests.Appium Modal } } + + static class Extensions + { + public static int GetWindowOrder(this IReadOnlyCollection elements, string identifier) + { + return elements.TakeWhile(x => + x.FindElementByXPath("XCUIElementTypeWindow")?.GetAttribute("identifier") != identifier).Count(); + } + } } From 1917def95927cb95d55a1d08ea14943cd5e03849 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 2 Jun 2022 14:15:02 +0100 Subject: [PATCH 060/224] full set of window tests for osx. --- .../ShowWindowTest.axaml.cs | 2 +- .../WindowTests.cs | 226 +++++++++++++----- 2 files changed, 165 insertions(+), 63 deletions(-) diff --git a/samples/IntegrationTestApp/ShowWindowTest.axaml.cs b/samples/IntegrationTestApp/ShowWindowTest.axaml.cs index f5f87c5c1c..3f45f1c5ad 100644 --- a/samples/IntegrationTestApp/ShowWindowTest.axaml.cs +++ b/samples/IntegrationTestApp/ShowWindowTest.axaml.cs @@ -25,7 +25,7 @@ namespace IntegrationTestApp this.GetControl("FrameSize").Text = $"{FrameSize}"; this.GetControl("Position").Text = $"{Position}"; this.GetControl("ScreenRect").Text = $"{Screens.ScreenFromVisual(this)?.WorkingArea}"; - this.GetControl("Scaling").Text = $"{((IRenderRoot)this).RenderScaling}"; // TODO Use DesktopScaling from WindowImpl. + this.GetControl("Scaling").Text = $"{PlatformImpl?.DesktopScaling}"; if (Owner is not null) { diff --git a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs index 8d6be4d199..b027c17ff3 100644 --- a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reactive.Disposables; using System.Runtime.InteropServices; using Avalonia.Controls; using OpenQA.Selenium; @@ -24,53 +25,117 @@ namespace Avalonia.IntegrationTests.Appium var tab = tabs.FindElementByName("Window"); tab.Click(); } + + private IDisposable OpenWindow(ShowWindowMode mode, WindowStartupLocation location, int width = 200, int height = 100) + { + var mainWindow = GetCurrentWindowHandleHack(); + var sizeTextBox = _session.FindElementByAccessibilityId("ShowWindowSize"); + var modeComboBox = _session.FindElementByAccessibilityId("ShowWindowMode"); + var locationComboBox = _session.FindElementByAccessibilityId("ShowWindowLocation"); + var showButton = _session.FindElementByAccessibilityId("ShowWindow"); + + sizeTextBox.SendKeys($"{width}, {height}"); + + modeComboBox.Click(); + _session.FindElementByName(mode.ToString()).SendClick(); + + locationComboBox.Click(); + _session.FindElementByName(location.ToString()).SendClick(); + + showButton.Click(); + + return Disposable.Create(() => + { + try + { + SwitchToNewWindowHack(mainWindow); + var closeButton = _session.FindElementByAccessibilityId("CloseWindow"); + closeButton.SendClick(); + } + catch { } + }); + } [PlatformFact(SkipOnWindows = true)] public void OSX_WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent() { var mainWindowHandle = GetCurrentWindowHandleHack(); + var mainWindow = + _session.FindElementByAccessibilityId("MainWindow"); + try { - var sizeTextBox = _session.FindElementByAccessibilityId("ShowWindowSize"); - var modeComboBox = _session.FindElementByAccessibilityId("ShowWindowMode"); - var locationComboBox = _session.FindElementByAccessibilityId("ShowWindowLocation"); - var showButton = _session.FindElementByAccessibilityId("ShowWindow"); + using (OpenWindow(ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) + { + mainWindow.Click(); - var mainWindow = - _session.FindElementByAccessibilityId("MainWindow"); - - sizeTextBox.SendKeys("200, 100"); + var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); - modeComboBox.Click(); - _session.FindElementByName(ShowWindowMode.Modal.ToString()).SendClick(); + int mainWindowIndex = windows.GetWindowOrder("MainWindow"); + int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); - locationComboBox.Click(); - _session.FindElementByName(WindowStartupLocation.CenterOwner.ToString()).SendClick(); + Assert.Equal(0, secondaryWindowIndex); + Assert.Equal(1, mainWindowIndex); + } + } + finally + { + SwitchToMainWindowHack(mainWindowHandle); + } + } + + [PlatformFact(SkipOnWindows = true)] + public void OSX_WindowOrder_Owned_Dialog_Stays_InFront_Of_Parent() + { + var mainWindowHandle = GetCurrentWindowHandleHack(); + + var mainWindow = + _session.FindElementByAccessibilityId("MainWindow"); - showButton.Click(); + try + { + using (OpenWindow(ShowWindowMode.Owned, WindowStartupLocation.CenterOwner)) + { + mainWindow.Click(); + var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); + + int mainWindowIndex = windows.GetWindowOrder("MainWindow"); + int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); + + Assert.Equal(0, secondaryWindowIndex); + Assert.Equal(1, mainWindowIndex); + } + } + finally + { + SwitchToMainWindowHack(mainWindowHandle); + } + } + + [PlatformFact(SkipOnWindows = true)] + public void OSX_WindowOrder_NonOwned_Window_Does_Not_Stay_InFront_Of_Parent() + { + var mainWindow = + _session.FindElementByAccessibilityId("MainWindow"); + + using (OpenWindow(ShowWindowMode.NonOwned, WindowStartupLocation.CenterOwner, 1400)) + { mainWindow.Click(); + var secondaryWindow = + _session.FindElementByAccessibilityId("SecondaryWindow"); + var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); int mainWindowIndex = windows.GetWindowOrder("MainWindow"); int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); - - Assert.Equal(0, secondaryWindowIndex); - Assert.Equal(1, mainWindowIndex); - SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle); - } - finally - { - try - { - var closeButton = _session.FindElementByAccessibilityId("CloseWindow"); - closeButton.Click(); - SwitchToMainWindowHack(mainWindowHandle); - } - catch { } + Assert.Equal(1, secondaryWindowIndex); + Assert.Equal(0, mainWindowIndex); + + secondaryWindow.SendClick(); } } @@ -81,53 +146,54 @@ namespace Avalonia.IntegrationTests.Appium try { - var sizeTextBox = _session.FindElementByAccessibilityId("ShowWindowSize"); - var modeComboBox = _session.FindElementByAccessibilityId("ShowWindowMode"); - var locationComboBox = _session.FindElementByAccessibilityId("ShowWindowLocation"); - var showButton = _session.FindElementByAccessibilityId("ShowWindow"); + var window = _session.FindWindowOuter("MainWindow"); - var closeButton = - _session.FindElementByXPath( - "/XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeButton[1]"); - - var zoomButton = - _session.FindElementByXPath( - "/XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeButton[2]"); - - var miniturizeButton = - _session.FindElementByXPath( - "/XCUIElementTypeApplication/XCUIElementTypeWindow/XCUIElementTypeButton[3]"); - + var (closeButton, zoomButton, miniturizeButton) + = window.GetChromeButtons(); Assert.True(closeButton.Enabled); Assert.True(zoomButton.Enabled); Assert.True(miniturizeButton.Enabled); - - sizeTextBox.SendKeys("400, 400"); - modeComboBox.Click(); - _session.FindElementByName(ShowWindowMode.Modal.ToString()).SendClick(); - - locationComboBox.Click(); - _session.FindElementByName(WindowStartupLocation.CenterOwner.ToString()).SendClick(); + using (OpenWindow(ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) + { + SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle); - showButton.Click(); - - SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle); - - Assert.False(closeButton.Enabled); - Assert.False(zoomButton.Enabled); - Assert.False(miniturizeButton.Enabled); + Assert.False(closeButton.Enabled); + Assert.False(zoomButton.Enabled); + Assert.False(miniturizeButton.Enabled); + } } finally { - try + SwitchToMainWindowHack(mainWindowHandle); + } + } + + [PlatformFact(SkipOnWindows = true)] + public void OSX_Minimize_Button_Disabled_Modal_Dialog() + { + var mainWindowHandle = GetCurrentWindowHandleHack(); + + try + { + using (OpenWindow(ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) { - var closeButton = _session.FindElementByAccessibilityId("CloseWindow"); - closeButton.Click(); - SwitchToMainWindowHack(mainWindowHandle); + var secondaryWindow = _session.FindWindowOuter("SecondaryWindow"); + + var (closeButton, zoomButton, miniturizeButton) + = secondaryWindow.GetChromeButtons(); + + Assert.True(closeButton.Enabled); + Assert.True(zoomButton.Enabled); + Assert.False(miniturizeButton.Enabled); + + SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle); } - catch { } + } + finally + { + SwitchToMainWindowHack(mainWindowHandle); } } @@ -257,6 +323,13 @@ namespace Avalonia.IntegrationTests.Appium if (Math.Abs(expected.Y - actual.Y) > 10) throw new EqualException(expected, actual); } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + if (Math.Abs(expected.X - actual.X) > 15) + throw new EqualException(expected, actual); + if (Math.Abs(expected.Y - actual.Y) > 15) + throw new EqualException(expected, actual); + } else { Assert.Equal(expected, actual); @@ -278,5 +351,34 @@ namespace Avalonia.IntegrationTests.Appium return elements.TakeWhile(x => x.FindElementByXPath("XCUIElementTypeWindow")?.GetAttribute("identifier") != identifier).Count(); } + + public static AppiumWebElement? FindWindowInner(this AppiumDriver session, string identifier) + { + return session.FindElementsByXPath("XCUIElementTypeWindow") + .FirstOrDefault(x => x.GetAttribute("identifier") == identifier); + } + + public static AppiumWebElement? FindWindowOuter(this AppiumDriver session, string identifier) + { + var windows = session.FindElementsByXPath("XCUIElementTypeWindow"); + + var window = windows.FirstOrDefault(x=>x.FindElementsByXPath("XCUIElementTypeWindow").Any(x => x.GetAttribute("identifier") == identifier)); + + return window; + } + + public static (AppiumWebElement? closeButton, AppiumWebElement? zoomButton, AppiumWebElement? miniturizeButton) GetChromeButtons (this AppiumWebElement outerWindow) + { + var closeButton = + outerWindow.FindElementByXPath("XCUIElementTypeButton[1]"); + + var zoomButton = + outerWindow.FindElementByXPath("XCUIElementTypeButton[2]"); + + var miniturizeButton = + outerWindow.FindElementByXPath("XCUIElementTypeButton[3]"); + + return (closeButton, zoomButton, miniturizeButton); + } } } From c02439aaaf5c1984f8aae75c777173c775c89257 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 30 May 2022 17:10:09 +0200 Subject: [PATCH 061/224] Refactored most of Style into StyleBase. Ready for `ControlTheme` class, which is a style without a selector. --- src/Avalonia.Base/Styling/Style.cs | 148 ++------------------- src/Avalonia.Base/Styling/StyleBase.cs | 137 +++++++++++++++++++ src/Avalonia.Base/Styling/StyleChildren.cs | 10 +- 3 files changed, 151 insertions(+), 144 deletions(-) create mode 100644 src/Avalonia.Base/Styling/StyleBase.cs diff --git a/src/Avalonia.Base/Styling/Style.cs b/src/Avalonia.Base/Styling/Style.cs index 000e588bad..c85c85fe21 100644 --- a/src/Avalonia.Base/Styling/Style.cs +++ b/src/Avalonia.Base/Styling/Style.cs @@ -1,23 +1,12 @@ using System; -using System.Collections.Generic; -using Avalonia.Animation; -using Avalonia.Controls; -using Avalonia.Metadata; namespace Avalonia.Styling { /// /// Defines a style. /// - public class Style : AvaloniaObject, IStyle, IResourceProvider + public class Style : StyleBase { - private IResourceHost? _owner; - private StyleChildren? _children; - private IResourceDictionary? _resources; - private List? _setters; - private List? _animations; - private StyleCache? _childCache; - /// /// Initializes a new instance of the class. /// @@ -34,114 +23,11 @@ namespace Avalonia.Styling Selector = selector(null); } - /// - /// Gets the children of the style. - /// - public IList Children => _children ??= new(this); - - /// - /// Gets the or Application that hosts the style. - /// - public IResourceHost? Owner - { - get => _owner; - private set - { - if (_owner != value) - { - _owner = value; - OwnerChanged?.Invoke(this, EventArgs.Empty); - } - } - } - - /// - /// Gets the parent style if this style is hosted in a collection. - /// - public Style? Parent { get; private set; } - - /// - /// Gets or sets a dictionary of style resources. - /// - public IResourceDictionary Resources - { - get => _resources ?? (Resources = new ResourceDictionary()); - set - { - value = value ?? throw new ArgumentNullException(nameof(value)); - - var hadResources = _resources?.HasResources ?? false; - - _resources = value; - - if (Owner is object) - { - _resources.AddOwner(Owner); - - if (hadResources || _resources.HasResources) - { - Owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); - } - } - } - } - /// /// Gets or sets the style's selector. /// public Selector? Selector { get; set; } - /// - /// Gets the style's setters. - /// - public IList Setters => _setters ??= new List(); - - /// - /// Gets the style's animations. - /// - public IList Animations => _animations ??= new List(); - - bool IResourceNode.HasResources => _resources?.Count > 0; - IReadOnlyList IStyle.Children => (IReadOnlyList?)_children ?? Array.Empty(); - - public event EventHandler? OwnerChanged; - - public void Add(ISetter setter) => Setters.Add(setter); - public void Add(IStyle style) => Children.Add(style); - - public SelectorMatchResult TryAttach(IStyleable target, IStyleHost? host) - { - target = target ?? throw new ArgumentNullException(nameof(target)); - - var match = Selector is object ? Selector.Match(target, Parent) : - target == host ? SelectorMatch.AlwaysThisInstance : SelectorMatch.NeverThisInstance; - - if (match.IsMatch && (_setters is object || _animations is object)) - { - var instance = new StyleInstance(this, target, _setters, _animations, match.Activator); - target.StyleApplied(instance); - instance.Start(); - } - - var result = match.Result; - - if (_children is not null) - { - _childCache ??= new StyleCache(); - var childResult = _childCache.TryAttach(_children, target, host); - if (childResult > result) - result = childResult; - } - - return result; - } - - public bool TryGetResource(object key, out object? result) - { - result = null; - return _resources?.TryGetResource(key, out result) ?? false; - } - /// /// Returns a string representation of the style. /// @@ -158,33 +44,17 @@ namespace Avalonia.Styling } } - void IResourceProvider.AddOwner(IResourceHost owner) - { - owner = owner ?? throw new ArgumentNullException(nameof(owner)); - - if (Owner != null) - { - throw new InvalidOperationException("The Style already has a parent."); - } - - Owner = owner; - _resources?.AddOwner(owner); - } - - void IResourceProvider.RemoveOwner(IResourceHost owner) + protected override SelectorMatch Matches(IStyleable target, IStyleHost? host) { - owner = owner ?? throw new ArgumentNullException(nameof(owner)); - - if (Owner == owner) - { - Owner = null; - _resources?.RemoveOwner(owner); - } + return Selector?.Match(target, Parent) ?? + (target == host ? + SelectorMatch.AlwaysThisInstance : + SelectorMatch.NeverThisInstance); } - internal void SetParent(Style? parent) + internal override void SetParent(StyleBase? parent) { - if (parent?.Selector is not null) + if (parent is Style parentStyle && parentStyle.Selector is not null) { if (Selector is null) throw new InvalidOperationException("Child styles must have a selector."); @@ -192,7 +62,7 @@ namespace Avalonia.Styling throw new InvalidOperationException("Child styles must have a nesting selector."); } - Parent = parent; + base.SetParent(parent); } } } diff --git a/src/Avalonia.Base/Styling/StyleBase.cs b/src/Avalonia.Base/Styling/StyleBase.cs new file mode 100644 index 0000000000..0fc57da728 --- /dev/null +++ b/src/Avalonia.Base/Styling/StyleBase.cs @@ -0,0 +1,137 @@ +using System; +using System.Collections.Generic; +using Avalonia.Animation; +using Avalonia.Controls; +using Avalonia.Metadata; + +namespace Avalonia.Styling +{ + /// + /// Base class for and . + /// + public abstract class StyleBase : AvaloniaObject, IStyle, IResourceProvider + { + private IResourceHost? _owner; + private StyleChildren? _children; + private IResourceDictionary? _resources; + private List? _setters; + private List? _animations; + private StyleCache? _childCache; + + public IList Children => _children ??= new(this); + + public IResourceHost? Owner + { + get => _owner; + private set + { + if (_owner != value) + { + _owner = value; + OwnerChanged?.Invoke(this, EventArgs.Empty); + } + } + } + + public IStyle? Parent { get; private set; } + + public IResourceDictionary Resources + { + get => _resources ?? (Resources = new ResourceDictionary()); + set + { + value = value ?? throw new ArgumentNullException(nameof(value)); + + var hadResources = _resources?.HasResources ?? false; + + _resources = value; + + if (Owner is object) + { + _resources.AddOwner(Owner); + + if (hadResources || _resources.HasResources) + { + Owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); + } + } + } + } + + public IList Setters => _setters ??= new List(); + public IList Animations => _animations ??= new List(); + + bool IResourceNode.HasResources => _resources?.Count > 0; + IReadOnlyList IStyle.Children => (IReadOnlyList?)_children ?? Array.Empty(); + + public void Add(ISetter setter) => Setters.Add(setter); + public void Add(IStyle style) => Children.Add(style); + + public event EventHandler? OwnerChanged; + + public SelectorMatchResult TryAttach(IStyleable target, IStyleHost? host) + { + target = target ?? throw new ArgumentNullException(nameof(target)); + + var result = SelectorMatchResult.NeverThisType; + + if (_setters?.Count > 0 || _animations?.Count > 0) + { + var match = Matches(target, host); + + if (match.IsMatch) + { + var instance = new StyleInstance(this, target, _setters, _animations, match.Activator); + target.StyleApplied(instance); + instance.Start(); + } + + result = match.Result; + } + + if (_children is not null) + { + _childCache ??= new StyleCache(); + var childResult = _childCache.TryAttach(_children, target, host); + if (childResult > result) + result = childResult; + } + + return result; + } + + public bool TryGetResource(object key, out object? result) + { + result = null; + return _resources?.TryGetResource(key, out result) ?? false; + } + + protected abstract SelectorMatch Matches(IStyleable target, IStyleHost? host); + + internal virtual void SetParent(StyleBase? parent) => Parent = parent; + + void IResourceProvider.AddOwner(IResourceHost owner) + { + owner = owner ?? throw new ArgumentNullException(nameof(owner)); + + if (Owner != null) + { + throw new InvalidOperationException("The Style already has a parent."); + } + + Owner = owner; + _resources?.AddOwner(owner); + } + + void IResourceProvider.RemoveOwner(IResourceHost owner) + { + owner = owner ?? throw new ArgumentNullException(nameof(owner)); + + if (Owner == owner) + { + Owner = null; + _resources?.RemoveOwner(owner); + } + } + } +} diff --git a/src/Avalonia.Base/Styling/StyleChildren.cs b/src/Avalonia.Base/Styling/StyleChildren.cs index 5f8635f155..42b0a331ee 100644 --- a/src/Avalonia.Base/Styling/StyleChildren.cs +++ b/src/Avalonia.Base/Styling/StyleChildren.cs @@ -5,20 +5,20 @@ namespace Avalonia.Styling { internal class StyleChildren : Collection { - private readonly Style _owner; + private readonly StyleBase _owner; - public StyleChildren(Style owner) => _owner = owner; + public StyleChildren(StyleBase owner) => _owner = owner; protected override void InsertItem(int index, IStyle item) { - (item as Style)?.SetParent(_owner); + (item as StyleBase)?.SetParent(_owner); base.InsertItem(index, item); } protected override void RemoveItem(int index) { var item = Items[index]; - (item as Style)?.SetParent(null); + (item as StyleBase)?.SetParent(null); if (_owner.Owner is IResourceHost host) (item as IResourceProvider)?.RemoveOwner(host); base.RemoveItem(index); @@ -26,7 +26,7 @@ namespace Avalonia.Styling protected override void SetItem(int index, IStyle item) { - (item as Style)?.SetParent(_owner); + (item as StyleBase)?.SetParent(_owner); base.SetItem(index, item); if (_owner.Owner is IResourceHost host) (item as IResourceProvider)?.AddOwner(host); From 088d8cfc5c147da723e7641cf77c8dc67646e786 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 1 Jun 2022 13:21:52 +0200 Subject: [PATCH 062/224] Initial implementation of control themes. --- src/Avalonia.Base/Styling/ControlTheme.cs | 27 ++++ src/Avalonia.Base/Styling/IStyle.cs | 2 +- src/Avalonia.Base/Styling/IThemed.cs | 13 ++ src/Avalonia.Base/Styling/NestingSelector.cs | 4 +- src/Avalonia.Base/Styling/Style.cs | 8 +- src/Avalonia.Base/Styling/StyleBase.cs | 20 ++- src/Avalonia.Base/Styling/StyleCache.cs | 2 +- src/Avalonia.Base/Styling/Styler.cs | 14 +++ src/Avalonia.Base/Styling/Styles.cs | 2 +- .../Primitives/TemplatedControl.cs | 25 +++- src/Avalonia.Themes.Default/SimpleTheme.cs | 2 +- src/Avalonia.Themes.Fluent/FluentTheme.cs | 2 +- .../Styling/StyleInclude.cs | 2 +- .../TemplatedControlTests_Theming.cs | 119 ++++++++++++++++++ 14 files changed, 227 insertions(+), 15 deletions(-) create mode 100644 src/Avalonia.Base/Styling/ControlTheme.cs create mode 100644 src/Avalonia.Base/Styling/IThemed.cs create mode 100644 tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests_Theming.cs diff --git a/src/Avalonia.Base/Styling/ControlTheme.cs b/src/Avalonia.Base/Styling/ControlTheme.cs new file mode 100644 index 0000000000..54fc972c31 --- /dev/null +++ b/src/Avalonia.Base/Styling/ControlTheme.cs @@ -0,0 +1,27 @@ +using System; + +namespace Avalonia.Styling +{ + /// + /// Defines a switchable theme for a control. + /// + public class ControlTheme : StyleBase + { + /// + /// Gets or sets the type for which this control theme is intended. + /// + public Type? TargetType { get; set; } + + internal override bool HasSelector => TargetType is not null; + + internal override SelectorMatch Match(IStyleable control, object? host, bool subscribe) + { + if (TargetType is null) + throw new InvalidOperationException("ControlTheme has no TargetType."); + + return control.StyleKey == TargetType ? + SelectorMatch.AlwaysThisType : + SelectorMatch.NeverThisType; + } + } +} diff --git a/src/Avalonia.Base/Styling/IStyle.cs b/src/Avalonia.Base/Styling/IStyle.cs index e9faf82c07..417739fb28 100644 --- a/src/Avalonia.Base/Styling/IStyle.cs +++ b/src/Avalonia.Base/Styling/IStyle.cs @@ -23,6 +23,6 @@ namespace Avalonia.Styling /// /// A describing how the style matches the control. /// - SelectorMatchResult TryAttach(IStyleable target, IStyleHost? host); + SelectorMatchResult TryAttach(IStyleable target, object? host); } } diff --git a/src/Avalonia.Base/Styling/IThemed.cs b/src/Avalonia.Base/Styling/IThemed.cs new file mode 100644 index 0000000000..32ae515bcb --- /dev/null +++ b/src/Avalonia.Base/Styling/IThemed.cs @@ -0,0 +1,13 @@ +namespace Avalonia.Styling +{ + /// + /// Represents a themed element. + /// + public interface IThemed + { + /// + /// Gets the theme style for the element. + /// + public ControlTheme? Theme { get; } + } +} diff --git a/src/Avalonia.Base/Styling/NestingSelector.cs b/src/Avalonia.Base/Styling/NestingSelector.cs index 481a937867..6d31f7cb18 100644 --- a/src/Avalonia.Base/Styling/NestingSelector.cs +++ b/src/Avalonia.Base/Styling/NestingSelector.cs @@ -15,9 +15,9 @@ namespace Avalonia.Styling protected override SelectorMatch Evaluate(IStyleable control, IStyle? parent, bool subscribe) { - if (parent is Style s && s.Selector is Selector selector) + if (parent is StyleBase s && s.HasSelector) { - return selector.Match(control, (parent as Style)?.Parent, subscribe); + return s.Match(control, null, subscribe); } throw new InvalidOperationException( diff --git a/src/Avalonia.Base/Styling/Style.cs b/src/Avalonia.Base/Styling/Style.cs index c85c85fe21..ca20ff2b4b 100644 --- a/src/Avalonia.Base/Styling/Style.cs +++ b/src/Avalonia.Base/Styling/Style.cs @@ -28,6 +28,8 @@ namespace Avalonia.Styling /// public Selector? Selector { get; set; } + internal override bool HasSelector => Selector is not null; + /// /// Returns a string representation of the style. /// @@ -44,10 +46,10 @@ namespace Avalonia.Styling } } - protected override SelectorMatch Matches(IStyleable target, IStyleHost? host) + internal override SelectorMatch Match(IStyleable control, object? host, bool subscribe) { - return Selector?.Match(target, Parent) ?? - (target == host ? + return Selector?.Match(control, Parent, subscribe) ?? + (control == host ? SelectorMatch.AlwaysThisInstance : SelectorMatch.NeverThisInstance); } diff --git a/src/Avalonia.Base/Styling/StyleBase.cs b/src/Avalonia.Base/Styling/StyleBase.cs index 0fc57da728..b6bfec62bd 100644 --- a/src/Avalonia.Base/Styling/StyleBase.cs +++ b/src/Avalonia.Base/Styling/StyleBase.cs @@ -64,12 +64,14 @@ namespace Avalonia.Styling bool IResourceNode.HasResources => _resources?.Count > 0; IReadOnlyList IStyle.Children => (IReadOnlyList?)_children ?? Array.Empty(); + internal abstract bool HasSelector { get; } + public void Add(ISetter setter) => Setters.Add(setter); public void Add(IStyle style) => Children.Add(style); public event EventHandler? OwnerChanged; - public SelectorMatchResult TryAttach(IStyleable target, IStyleHost? host) + public SelectorMatchResult TryAttach(IStyleable target, object? host) { target = target ?? throw new ArgumentNullException(nameof(target)); @@ -77,7 +79,7 @@ namespace Avalonia.Styling if (_setters?.Count > 0 || _animations?.Count > 0) { - var match = Matches(target, host); + var match = Match(target, host, subscribe: true); if (match.IsMatch) { @@ -106,7 +108,19 @@ namespace Avalonia.Styling return _resources?.TryGetResource(key, out result) ?? false; } - protected abstract SelectorMatch Matches(IStyleable target, IStyleHost? host); + /// + /// Evaluates the style's selector against the specified target element. + /// + /// The control. + /// The element that hosts the style. + /// + /// Whether the match should subscribe to changes in order to track the match over time, + /// or simply return an immediate result. + /// + /// + /// A describing how the style matches the control. + /// + internal abstract SelectorMatch Match(IStyleable control, object? host, bool subscribe); internal virtual void SetParent(StyleBase? parent) => Parent = parent; diff --git a/src/Avalonia.Base/Styling/StyleCache.cs b/src/Avalonia.Base/Styling/StyleCache.cs index 3285476880..81196f6a27 100644 --- a/src/Avalonia.Base/Styling/StyleCache.cs +++ b/src/Avalonia.Base/Styling/StyleCache.cs @@ -12,7 +12,7 @@ namespace Avalonia.Styling /// internal class StyleCache : Dictionary?> { - public SelectorMatchResult TryAttach(IList styles, IStyleable target, IStyleHost? host) + public SelectorMatchResult TryAttach(IList styles, IStyleable target, object? host) { if (TryGetValue(target.StyleKey, out var cached)) { diff --git a/src/Avalonia.Base/Styling/Styler.cs b/src/Avalonia.Base/Styling/Styler.cs index 74cf77ea40..b9359b3329 100644 --- a/src/Avalonia.Base/Styling/Styler.cs +++ b/src/Avalonia.Base/Styling/Styler.cs @@ -10,6 +10,20 @@ namespace Avalonia.Styling { target = target ?? throw new ArgumentNullException(nameof(target)); + // If the control has a themed templated parent then first apply the styles from + // the templated parent theme. + if (target.TemplatedParent is IThemed themedTemplatedParent) + { + themedTemplatedParent.Theme?.TryAttach(target, themedTemplatedParent); + } + + // If the control itself is themed, then next apply the control theme. + if (target is IThemed themed) + { + themed.Theme?.TryAttach(target, target); + } + + // Apply styles from the rest of the tree. if (target is IStyleHost styleHost) { ApplyStyles(target, styleHost); diff --git a/src/Avalonia.Base/Styling/Styles.cs b/src/Avalonia.Base/Styling/Styles.cs index 7c0bc4ad7f..4c011f1b0d 100644 --- a/src/Avalonia.Base/Styling/Styles.cs +++ b/src/Avalonia.Base/Styling/Styles.cs @@ -109,7 +109,7 @@ namespace Avalonia.Styling set => _styles[index] = value; } - public SelectorMatchResult TryAttach(IStyleable target, IStyleHost? host) + public SelectorMatchResult TryAttach(IStyleable target, object? host) { _cache ??= new StyleCache(); return _cache.TryAttach(this, target, host); diff --git a/src/Avalonia.Controls/Primitives/TemplatedControl.cs b/src/Avalonia.Controls/Primitives/TemplatedControl.cs index db029d38c0..e1f42b6eb0 100644 --- a/src/Avalonia.Controls/Primitives/TemplatedControl.cs +++ b/src/Avalonia.Controls/Primitives/TemplatedControl.cs @@ -12,7 +12,7 @@ namespace Avalonia.Controls.Primitives /// /// A lookless control whose visual appearance is defined by its . /// - public class TemplatedControl : Control, ITemplatedControl + public class TemplatedControl : Control, IThemed, ITemplatedControl { /// /// Defines the property. @@ -86,6 +86,12 @@ namespace Avalonia.Controls.Primitives public static readonly StyledProperty TemplateProperty = AvaloniaProperty.Register(nameof(Template)); + /// + /// Defines the property. + /// + public static readonly StyledProperty ThemeProperty = + AvaloniaProperty.Register(nameof(Theme)); + /// /// Defines the IsTemplateFocusTarget attached property. /// @@ -228,6 +234,15 @@ namespace Avalonia.Controls.Primitives set { SetValue(TemplateProperty, value); } } + /// + /// Gets or sets the theme to be applied to the control. + /// + public ControlTheme? Theme + { + get { return GetValue(ThemeProperty); } + set { SetValue(ThemeProperty, value); } + } + /// /// Gets the value of the IsTemplateFocusTargetProperty attached property on a control. /// @@ -365,6 +380,14 @@ namespace Avalonia.Controls.Primitives { } + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + + if (change.Property == ThemeProperty) + InvalidateStyles(); + } + /// /// Called when the control's template is applied. /// diff --git a/src/Avalonia.Themes.Default/SimpleTheme.cs b/src/Avalonia.Themes.Default/SimpleTheme.cs index 6929660757..d7939a68c1 100644 --- a/src/Avalonia.Themes.Default/SimpleTheme.cs +++ b/src/Avalonia.Themes.Default/SimpleTheme.cs @@ -103,7 +103,7 @@ namespace Avalonia.Themes.Default void IResourceProvider.RemoveOwner(IResourceHost owner) => (Loaded as IResourceProvider)?.RemoveOwner(owner); - public SelectorMatchResult TryAttach(IStyleable target, IStyleHost? host) => Loaded.TryAttach(target, host); + public SelectorMatchResult TryAttach(IStyleable target, object? host) => Loaded.TryAttach(target, host); public bool TryGetResource(object key, out object? value) { diff --git a/src/Avalonia.Themes.Fluent/FluentTheme.cs b/src/Avalonia.Themes.Fluent/FluentTheme.cs index f6b47a5466..befe669029 100644 --- a/src/Avalonia.Themes.Fluent/FluentTheme.cs +++ b/src/Avalonia.Themes.Fluent/FluentTheme.cs @@ -164,7 +164,7 @@ namespace Avalonia.Themes.Fluent } } - public SelectorMatchResult TryAttach(IStyleable target, IStyleHost? host) => Loaded.TryAttach(target, host); + public SelectorMatchResult TryAttach(IStyleable target, object? host) => Loaded.TryAttach(target, host); public bool TryGetResource(object key, out object? value) { diff --git a/src/Markup/Avalonia.Markup.Xaml/Styling/StyleInclude.cs b/src/Markup/Avalonia.Markup.Xaml/Styling/StyleInclude.cs index fa4a27fc50..109e85f1a4 100644 --- a/src/Markup/Avalonia.Markup.Xaml/Styling/StyleInclude.cs +++ b/src/Markup/Avalonia.Markup.Xaml/Styling/StyleInclude.cs @@ -82,7 +82,7 @@ namespace Avalonia.Markup.Xaml.Styling } } - public SelectorMatchResult TryAttach(IStyleable target, IStyleHost? host) => Loaded.TryAttach(target, host); + public SelectorMatchResult TryAttach(IStyleable target, object? host) => Loaded.TryAttach(target, host); public bool TryGetResource(object key, out object? value) { diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests_Theming.cs b/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests_Theming.cs new file mode 100644 index 0000000000..b24adfe7ab --- /dev/null +++ b/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests_Theming.cs @@ -0,0 +1,119 @@ +using System.Linq; +using Avalonia.Controls.Primitives; +using Avalonia.Controls.Templates; +using Avalonia.Media; +using Avalonia.Styling; +using Avalonia.UnitTests; +using Avalonia.VisualTree; +using Xunit; + +#nullable enable + +namespace Avalonia.Controls.UnitTests.Primitives +{ + public class TemplatedControlTests_Theming + { + [Fact] + public void Theme_Is_Applied_When_Attached_To_Logical_Tree() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(); + + Assert.Null(target.Template); + + var root = CreateRoot(target); + + Assert.NotNull(target.Template); + var border = Assert.IsType(target.VisualChild); + + Assert.Equal(border.Background, Brushes.Red); + + target.Classes.Add("foo"); + Assert.Equal(border.Background, Brushes.Green); + } + + [Fact] + public void Theme_Is_Detached_When_Theme_Property_Cleared() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(); + var root = CreateRoot(target); + + Assert.NotNull(target.Template); + + target.Theme = null; + Assert.Null(target.Template); + } + + [Fact] + public void Theme_Is_Applied_On_Layout_After_Theme_Property_Changes() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = new ThemedControl(); + var root = CreateRoot(target); + + Assert.Null(target.Template); + + target.Theme = CreateTheme(); + Assert.Null(target.Template); + + root.LayoutManager.ExecuteLayoutPass(); + + var border = Assert.IsType(target.VisualChild); + Assert.NotNull(target.Template); + Assert.Equal(border.Background, Brushes.Red); + } + + private static ThemedControl CreateTarget() + { + return new ThemedControl + { + Theme = CreateTheme(), + }; + } + + private static ControlTheme CreateTheme() + { + var template = new FuncControlTemplate((o, n) => + new Border { Name = "PART_Border" }); + + return new ControlTheme + { + TargetType = typeof(ThemedControl), + Setters = + { + new Setter(ThemedControl.TemplateProperty, template), + }, + Children = + { + new Style(x => x.Nesting().Template().OfType()) + { + Setters = + { + new Setter(Border.BackgroundProperty, Brushes.Red), + } + }, + new Style(x => x.Nesting().Class("foo").Template().OfType()) + { + Setters = + { + new Setter(Border.BackgroundProperty, Brushes.Green), + } + }, + } + }; + } + + private static TestRoot CreateRoot(IControl child) + { + var result = new TestRoot(child); + result.LayoutManager.ExecuteInitialLayoutPass(); + return result; + } + + private class ThemedControl : TemplatedControl + { + public IVisual? VisualChild => VisualChildren?.SingleOrDefault(); + } + } +} From dee353bb9640278ab2364b1d9b4624d5cbe7a215 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 1 Jun 2022 15:24:15 +0200 Subject: [PATCH 063/224] Support ControlTheme in XAML compiler. --- .../AvaloniaXamlIlCompiler.cs | 1 + .../AvaloniaXamlIlControlThemeTransformer.cs | 39 ++++++++++ .../AvaloniaXamlIlSetterTransformer.cs | 75 +++++++++++++------ .../Xaml/ControlThemeTests.cs | 53 +++++++++++++ .../Xaml/TestTemplatedControl.cs | 8 ++ 5 files changed, 155 insertions(+), 21 deletions(-) create mode 100644 src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlControlThemeTransformer.cs create mode 100644 tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlThemeTests.cs create mode 100644 tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TestTemplatedControl.cs diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlCompiler.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlCompiler.cs index 1ca7be67a7..20e035f8ff 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlCompiler.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlCompiler.cs @@ -48,6 +48,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions InsertBefore( new AvaloniaXamlIlBindingPathParser(), + new AvaloniaXamlIlControlThemeTransformer(), new AvaloniaXamlIlSelectorTransformer(), new AvaloniaXamlIlControlTemplateTargetTypeMetadataTransformer(), new AvaloniaXamlIlPropertyPathTransformer(), diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlControlThemeTransformer.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlControlThemeTransformer.cs new file mode 100644 index 0000000000..1338dc7248 --- /dev/null +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlControlThemeTransformer.cs @@ -0,0 +1,39 @@ +using System.Linq; +using XamlX; +using XamlX.Ast; +using XamlX.Transform; +using XamlX.Transform.Transformers; +using XamlX.TypeSystem; + +namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers +{ + class AvaloniaXamlIlControlThemeTransformer : IXamlAstTransformer + { + public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node) + { + if (!(node is XamlAstObjectNode on && on.Type.GetClrType().FullName == "Avalonia.Styling.ControlTheme")) + return node; + + // Check if we've already transformed this node. + if (context.ParentNodes().FirstOrDefault() is AvaloniaXamlIlTargetTypeMetadataNode) + return node; + + var targetTypeNode = on.Children.OfType() + .FirstOrDefault(p => p.Property.GetClrProperty().Name == "TargetType") ?? + throw new XamlParseException("ControlTheme must have a TargetType.", node); + + IXamlType targetType; + + if (targetTypeNode.Values[0] is XamlTypeExtensionNode extension) + targetType = extension.Value.GetClrType(); + else if (targetTypeNode.Values[0] is XamlAstTextNode text) + targetType = TypeReferenceResolver.ResolveType(context, text.Text, false, text, true).GetClrType(); + else + throw new XamlParseException("Could not determine TargetType for ControlTheme.", targetTypeNode); + + return new AvaloniaXamlIlTargetTypeMetadataNode(on, + new XamlAstClrTypeReference(targetTypeNode, targetType, false), + AvaloniaXamlIlTargetTypeMetadataNode.ScopeTypes.Style); + } + } +} diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlSetterTransformer.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlSetterTransformer.cs index e816265422..06e34a85a2 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlSetterTransformer.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlSetterTransformer.cs @@ -1,19 +1,14 @@ -using System; using System.Collections.Generic; using System.Linq; -using Avalonia.Data.Core; -using XamlX; using XamlX.Ast; using XamlX.Emit; using XamlX.IL; using XamlX.Transform; -using XamlX.Transform.Transformers; using XamlX.TypeSystem; namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers { using XamlParseException = XamlX.XamlParseException; - using XamlLoadException = XamlX.XamlLoadException; class AvaloniaXamlIlSetterTransformer : IXamlAstTransformer { public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node) @@ -22,21 +17,10 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers && on.Type.GetClrType().FullName == "Avalonia.Styling.Setter")) return node; - var parent = context.ParentNodes().OfType() - .FirstOrDefault(p => p.Type.GetClrType().FullName == "Avalonia.Styling.Style"); - - if (parent == null) - throw new XamlParseException( - "Avalonia.Styling.Setter is only valid inside Avalonia.Styling.Style", node); - var selectorProperty = parent.Children.OfType() - .FirstOrDefault(p => p.Property.GetClrProperty().Name == "Selector"); - if (selectorProperty == null) - throw new XamlParseException( - "Can not find parent Style Selector", node); - var selector = selectorProperty.Values.FirstOrDefault() as XamlIlSelectorNode; - if (selector?.TargetType == null) - throw new XamlParseException( - "Can not resolve parent Style Selector type", node); + var targetTypeNode = context.ParentNodes() + .OfType() + .FirstOrDefault(x => x.ScopeType == AvaloniaXamlIlTargetTypeMetadataNode.ScopeTypes.Style) ?? + throw new XamlParseException("Can not find parent Style Selector or ControlTemplate TargetType", node); IXamlType propType = null; var property = @on.Children.OfType() @@ -50,7 +34,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers var avaloniaPropertyNode = XamlIlAvaloniaPropertyHelper.CreateNode(context, propertyName, - new XamlAstClrTypeReference(selector, selector.TargetType, false), property.Values[0]); + new XamlAstClrTypeReference(targetTypeNode, targetTypeNode.TargetType.GetClrType(), false), property.Values[0]); property.Values = new List {avaloniaPropertyNode}; propType = avaloniaPropertyNode.AvaloniaPropertyType; } @@ -84,6 +68,55 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers return node; } + private (IXamlLineInfo, IXamlType) GetTargetType(AstTransformationContext context, IXamlAstNode node) + { + foreach (var n in context.ParentNodes()) + { + if (n is XamlAstObjectNode parent) + { + switch (parent.Type.GetClrType().FullName) + { + case "Avalonia.Styling.Style": + var selectorProperty = parent.Children.OfType() + .FirstOrDefault(p => p.Property.GetClrProperty().Name == "Selector"); + if (selectorProperty == null) + throw new XamlParseException("Can not find parent Style Selector.", node); + var selector = selectorProperty.Values.FirstOrDefault() as XamlIlSelectorNode; + if (selector?.TargetType != null) + return (selector, selector.TargetType); + throw new XamlParseException( + "Can not resolve parent Style Selector type", node); + + case "Avalonia.Styling.ControlTheme": + var targetTypeProperty = parent.Children.OfType() + .FirstOrDefault(p => p.Property.GetClrProperty().Name == "TargetType"); + if (targetTypeProperty == null) + throw new XamlParseException("ControlTemplate has no TargetType.", parent); + break; + } + } + } + + throw new XamlParseException("'Setter' is only valid inside a 'Style' or 'ControlTheme'.", node); + //var parent = context.ParentNodes().OfType() + // .FirstOrDefault(p => p.Type.GetClrType().FullName == "Avalonia.Styling.Style" || + // p.Type.GetClrType().FullName == "Avalonia.Styling.ControlTheme"); + + //if (parent == null) + // throw new XamlParseException( + // "Avalonia.Styling.Setter is only valid inside Avalonia.Styling.Style", node); + //var selectorProperty = parent.Children.OfType() + // .FirstOrDefault(p => p.Property.GetClrProperty().Name == "Selector" || + // p.Property.GetClrProperty().Name == "TargetType"); + //if (selectorProperty == null) + // throw new XamlParseException( + // "Can not find parent Style Selector or ControlTemplate TargetType", node); + //var selector = selectorProperty.Values.FirstOrDefault() as XamlIlSelectorNode; + //if (selector?.TargetType == null) + // throw new XamlParseException( + // "Can not resolve parent Style Selector type", node); + } + class SetterValueProperty : XamlAstClrProperty { public SetterValueProperty(IXamlLineInfo line, IXamlType setterType, IXamlType targetType, diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlThemeTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlThemeTests.cs new file mode 100644 index 0000000000..05083537cd --- /dev/null +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlThemeTests.cs @@ -0,0 +1,53 @@ +using Avalonia.Controls; +using Avalonia.Media; +using Avalonia.UnitTests; +using Avalonia.VisualTree; +using Xunit; + +namespace Avalonia.Markup.Xaml.UnitTests.Xaml +{ + public class ControlThemeTests : XamlTestBase + { + [Fact] + public void ControlTheme_Can_Be_StaticResource() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = $@" + + + {ControlThemeXaml} + + + +"; + + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var button = Assert.IsType(window.Content); + + window.LayoutManager.ExecuteInitialLayoutPass(); + + Assert.NotNull(button.Template); + + var child = Assert.Single(button.GetVisualChildren()); + var border = Assert.IsType(child); + + Assert.Equal(Brushes.Red, border.Background); + } + } + + private const string ControlThemeXaml = @" + + + + + + + +"; + } +} diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TestTemplatedControl.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TestTemplatedControl.cs new file mode 100644 index 0000000000..0c862bb66a --- /dev/null +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TestTemplatedControl.cs @@ -0,0 +1,8 @@ +using Avalonia.Controls.Primitives; + +namespace Avalonia.Markup.Xaml.UnitTests.Xaml +{ + public class TestTemplatedControl : TemplatedControl + { + } +} From a6dc6b1c887c8a5139be7bf1abba1315c26af0d7 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 1 Jun 2022 21:45:45 +0200 Subject: [PATCH 064/224] Prevent ControlTheme as a nested style. --- src/Avalonia.Base/Styling/ControlTheme.cs | 16 +++++++++++ .../Styling/ControlThemeTests.cs | 28 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/Avalonia.Base.UnitTests/Styling/ControlThemeTests.cs diff --git a/src/Avalonia.Base/Styling/ControlTheme.cs b/src/Avalonia.Base/Styling/ControlTheme.cs index 54fc972c31..9dcbd7d2c4 100644 --- a/src/Avalonia.Base/Styling/ControlTheme.cs +++ b/src/Avalonia.Base/Styling/ControlTheme.cs @@ -7,6 +7,17 @@ namespace Avalonia.Styling /// public class ControlTheme : StyleBase { + /// + /// Initializes a new instance of the class. + /// + public ControlTheme() { } + + /// + /// Initializes a new instance of the class. + /// + /// The value for . + public ControlTheme(Type targetType) => TargetType = targetType; + /// /// Gets or sets the type for which this control theme is intended. /// @@ -23,5 +34,10 @@ namespace Avalonia.Styling SelectorMatch.AlwaysThisType : SelectorMatch.NeverThisType; } + + internal override void SetParent(StyleBase? parent) + { + throw new InvalidOperationException("ControlThemes cannot be added as a nested style."); + } } } diff --git a/tests/Avalonia.Base.UnitTests/Styling/ControlThemeTests.cs b/tests/Avalonia.Base.UnitTests/Styling/ControlThemeTests.cs new file mode 100644 index 0000000000..93a0e6c2fd --- /dev/null +++ b/tests/Avalonia.Base.UnitTests/Styling/ControlThemeTests.cs @@ -0,0 +1,28 @@ +using System; +using Avalonia.Controls; +using Avalonia.Styling; +using Xunit; + +namespace Avalonia.Base.UnitTests.Styling +{ + public class ControlThemeTests + { + [Fact] + public void ControlTheme_Cannot_Be_Added_To_Style_Children() + { + var target = new ControlTheme(typeof(Button)); + var style = new Style(); + + Assert.Throws(() => style.Children.Add(target)); + } + + [Fact] + public void ControlTheme_Cannot_Be_Added_To_ControlTheme_Children() + { + var target = new ControlTheme(typeof(Button)); + var other = new ControlTheme(typeof(CheckBox)); + + Assert.Throws(() => other.Children.Add(target)); + } + } +} From fc3c036b02afce41d8faca7e4c1e8219fb0c4ceb Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 1 Jun 2022 22:48:34 +0200 Subject: [PATCH 065/224] Move Theme to StyledElement. The WPF equivalent (`Style`) is in `FrameworkElement` so it would make sense. Will also make stuff a lot easier and removes the need for an `IThemed` interface. --- src/Avalonia.Base/StyledElement.cs | 25 +++++++++++++++++-- src/Avalonia.Base/Styling/IStyleable.cs | 7 ++++-- src/Avalonia.Base/Styling/IThemed.cs | 13 ---------- src/Avalonia.Base/Styling/Styler.cs | 23 ++++------------- .../Primitives/TemplatedControl.cs | 25 +------------------ .../AvaloniaPropertyConverterTest.cs | 5 ++++ 6 files changed, 39 insertions(+), 59 deletions(-) delete mode 100644 src/Avalonia.Base/Styling/IThemed.cs diff --git a/src/Avalonia.Base/StyledElement.cs b/src/Avalonia.Base/StyledElement.cs index f98d2cdbcc..4ead2470d7 100644 --- a/src/Avalonia.Base/StyledElement.cs +++ b/src/Avalonia.Base/StyledElement.cs @@ -12,8 +12,6 @@ using Avalonia.Logging; using Avalonia.LogicalTree; using Avalonia.Styling; -#nullable enable - namespace Avalonia { /// @@ -55,6 +53,12 @@ namespace Avalonia nameof(TemplatedParent), o => o.TemplatedParent, (o ,v) => o.TemplatedParent = v); + + /// + /// Defines the property. + /// + public static readonly StyledProperty ThemeProperty = + AvaloniaProperty.Register(nameof(Theme)); private int _initCount; private string? _name; @@ -230,6 +234,15 @@ namespace Avalonia internal set => SetAndRaise(TemplatedParentProperty, ref _templatedParent, value); } + /// + /// Gets or sets the theme to be applied to the element. + /// + public ControlTheme? Theme + { + get { return GetValue(ThemeProperty); } + set { SetValue(ThemeProperty, value); } + } + /// /// Gets the styled element's logical children. /// @@ -590,6 +603,14 @@ namespace Avalonia { } + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + + if (change.Property == ThemeProperty) + InvalidateStyles(); + } + private static void DataContextNotifying(IAvaloniaObject o, bool updateStarted) { if (o is StyledElement element) diff --git a/src/Avalonia.Base/Styling/IStyleable.cs b/src/Avalonia.Base/Styling/IStyleable.cs index 5bc972e7ab..61fcbdf850 100644 --- a/src/Avalonia.Base/Styling/IStyleable.cs +++ b/src/Avalonia.Base/Styling/IStyleable.cs @@ -3,8 +3,6 @@ using System.Collections.Generic; using Avalonia.Collections; using Avalonia.Metadata; -#nullable enable - namespace Avalonia.Styling { /// @@ -28,6 +26,11 @@ namespace Avalonia.Styling /// ITemplatedControl? TemplatedParent { get; } + /// + /// Gets the theme to be applied to the control. + /// + public ControlTheme? Theme { get; } + /// /// Notifies the element that a style has been applied. /// diff --git a/src/Avalonia.Base/Styling/IThemed.cs b/src/Avalonia.Base/Styling/IThemed.cs deleted file mode 100644 index 32ae515bcb..0000000000 --- a/src/Avalonia.Base/Styling/IThemed.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Avalonia.Styling -{ - /// - /// Represents a themed element. - /// - public interface IThemed - { - /// - /// Gets the theme style for the element. - /// - public ControlTheme? Theme { get; } - } -} diff --git a/src/Avalonia.Base/Styling/Styler.cs b/src/Avalonia.Base/Styling/Styler.cs index b9359b3329..c9ea123bdc 100644 --- a/src/Avalonia.Base/Styling/Styler.cs +++ b/src/Avalonia.Base/Styling/Styler.cs @@ -1,33 +1,24 @@ using System; -#nullable enable - namespace Avalonia.Styling { public class Styler : IStyler { public void ApplyStyles(IStyleable target) { - target = target ?? throw new ArgumentNullException(nameof(target)); + _ = target ?? throw new ArgumentNullException(nameof(target)); // If the control has a themed templated parent then first apply the styles from // the templated parent theme. - if (target.TemplatedParent is IThemed themedTemplatedParent) - { - themedTemplatedParent.Theme?.TryAttach(target, themedTemplatedParent); - } + if (target.TemplatedParent is IStyleable styleableParent) + styleableParent.Theme?.TryAttach(target, styleableParent); - // If the control itself is themed, then next apply the control theme. - if (target is IThemed themed) - { - themed.Theme?.TryAttach(target, target); - } + // Next apply the control theme. + target.Theme?.TryAttach(target, target); // Apply styles from the rest of the tree. if (target is IStyleHost styleHost) - { ApplyStyles(target, styleHost); - } } private void ApplyStyles(IStyleable target, IStyleHost host) @@ -35,14 +26,10 @@ namespace Avalonia.Styling var parent = host.StylingParent; if (parent != null) - { ApplyStyles(target, parent); - } if (host.IsStylesInitialized) - { host.Styles.TryAttach(target, host); - } } } } diff --git a/src/Avalonia.Controls/Primitives/TemplatedControl.cs b/src/Avalonia.Controls/Primitives/TemplatedControl.cs index e1f42b6eb0..db029d38c0 100644 --- a/src/Avalonia.Controls/Primitives/TemplatedControl.cs +++ b/src/Avalonia.Controls/Primitives/TemplatedControl.cs @@ -12,7 +12,7 @@ namespace Avalonia.Controls.Primitives /// /// A lookless control whose visual appearance is defined by its . /// - public class TemplatedControl : Control, IThemed, ITemplatedControl + public class TemplatedControl : Control, ITemplatedControl { /// /// Defines the property. @@ -86,12 +86,6 @@ namespace Avalonia.Controls.Primitives public static readonly StyledProperty TemplateProperty = AvaloniaProperty.Register(nameof(Template)); - /// - /// Defines the property. - /// - public static readonly StyledProperty ThemeProperty = - AvaloniaProperty.Register(nameof(Theme)); - /// /// Defines the IsTemplateFocusTarget attached property. /// @@ -234,15 +228,6 @@ namespace Avalonia.Controls.Primitives set { SetValue(TemplateProperty, value); } } - /// - /// Gets or sets the theme to be applied to the control. - /// - public ControlTheme? Theme - { - get { return GetValue(ThemeProperty); } - set { SetValue(ThemeProperty, value); } - } - /// /// Gets the value of the IsTemplateFocusTargetProperty attached property on a control. /// @@ -380,14 +365,6 @@ namespace Avalonia.Controls.Primitives { } - protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) - { - base.OnPropertyChanged(change); - - if (change.Property == ThemeProperty) - InvalidateStyles(); - } - /// /// Called when the control's template is applied. /// diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Converters/AvaloniaPropertyConverterTest.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Converters/AvaloniaPropertyConverterTest.cs index 33bf72014c..ca59fe8480 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Converters/AvaloniaPropertyConverterTest.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Converters/AvaloniaPropertyConverterTest.cs @@ -137,6 +137,11 @@ namespace Avalonia.Markup.Xaml.UnitTests.Converters get { throw new NotImplementedException(); } } + public ControlTheme Theme + { + get { throw new NotImplementedException(); } + } + public void DetachStyles() { throw new NotImplementedException(); From 8c61f25188afe50b0785150e2e1fbf605c4652c5 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 2 Jun 2022 09:23:17 +0200 Subject: [PATCH 066/224] Promote theme to LocalValue if applied from style. --- src/Avalonia.Base/StyledElement.cs | 25 ++- .../TemplatedControlTests_Theming.cs | 146 ++++++++++++------ .../Xaml/ControlThemeTests.cs | 36 +++++ 3 files changed, 157 insertions(+), 50 deletions(-) diff --git a/src/Avalonia.Base/StyledElement.cs b/src/Avalonia.Base/StyledElement.cs index 4ead2470d7..75c4b94174 100644 --- a/src/Avalonia.Base/StyledElement.cs +++ b/src/Avalonia.Base/StyledElement.cs @@ -71,6 +71,7 @@ namespace Avalonia private List? _appliedStyles; private ITemplatedControl? _templatedParent; private bool _dataContextUpdating; + private bool _hasPromotedTheme; /// /// Initializes static members of the class. @@ -239,8 +240,8 @@ namespace Avalonia /// public ControlTheme? Theme { - get { return GetValue(ThemeProperty); } - set { SetValue(ThemeProperty, value); } + get => GetValue(ThemeProperty); + set => SetValue(ThemeProperty, value); } /// @@ -315,6 +316,7 @@ namespace Avalonia /// IStyleHost? IStyleHost.StylingParent => (IStyleHost?)InheritanceParent; + /// public virtual void BeginInit() { @@ -354,10 +356,15 @@ namespace Avalonia } finally { + _styled = true; EndBatchUpdate(); } - _styled = true; + if (_hasPromotedTheme) + { + _hasPromotedTheme = false; + ClearValue(ThemeProperty); + } } return _styled; @@ -608,7 +615,19 @@ namespace Avalonia base.OnPropertyChanged(change); if (change.Property == ThemeProperty) + { + // Changing the theme detaches all styles, meaning that if the theme property was + // set via a style, it will get cleared! To work around this, if the value was + // applied at less than local value priority then promote the value to local value + // priority until styling is re-applied. + if (change.Priority > BindingPriority.LocalValue) + { + Theme = change.GetNewValue(); + _hasPromotedTheme = true; + } + InvalidateStyles(); + } } private static void DataContextNotifying(IAvaloniaObject o, bool updateStarted) diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests_Theming.cs b/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests_Theming.cs index b24adfe7ab..74d75ff056 100644 --- a/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests_Theming.cs +++ b/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests_Theming.cs @@ -13,63 +13,122 @@ namespace Avalonia.Controls.UnitTests.Primitives { public class TemplatedControlTests_Theming { - [Fact] - public void Theme_Is_Applied_When_Attached_To_Logical_Tree() + public class InlineTheme { - using var app = UnitTestApplication.Start(TestServices.RealStyler); - var target = CreateTarget(); + [Fact] + public void Theme_Is_Applied_When_Attached_To_Logical_Tree() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(); - Assert.Null(target.Template); + Assert.Null(target.Template); - var root = CreateRoot(target); + var root = CreateRoot(target); + Assert.NotNull(target.Template); - Assert.NotNull(target.Template); - var border = Assert.IsType(target.VisualChild); - - Assert.Equal(border.Background, Brushes.Red); + var border = Assert.IsType(target.VisualChild); + Assert.Equal(border.Background, Brushes.Red); - target.Classes.Add("foo"); - Assert.Equal(border.Background, Brushes.Green); - } + target.Classes.Add("foo"); + Assert.Equal(border.Background, Brushes.Green); + } - [Fact] - public void Theme_Is_Detached_When_Theme_Property_Cleared() - { - using var app = UnitTestApplication.Start(TestServices.RealStyler); - var target = CreateTarget(); - var root = CreateRoot(target); + [Fact] + public void Theme_Is_Detached_When_Theme_Property_Cleared() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(); + var root = CreateRoot(target); - Assert.NotNull(target.Template); + Assert.NotNull(target.Template); - target.Theme = null; - Assert.Null(target.Template); - } + target.Theme = null; + Assert.Null(target.Template); + } - [Fact] - public void Theme_Is_Applied_On_Layout_After_Theme_Property_Changes() - { - using var app = UnitTestApplication.Start(TestServices.RealStyler); - var target = new ThemedControl(); - var root = CreateRoot(target); + [Fact] + public void Theme_Is_Applied_On_Layout_After_Theme_Property_Changes() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = new ThemedControl(); + var root = CreateRoot(target); + + Assert.Null(target.Template); - Assert.Null(target.Template); + target.Theme = CreateTheme(); + Assert.Null(target.Template); - target.Theme = CreateTheme(); - Assert.Null(target.Template); + root.LayoutManager.ExecuteLayoutPass(); - root.LayoutManager.ExecuteLayoutPass(); + var border = Assert.IsType(target.VisualChild); + Assert.NotNull(target.Template); + Assert.Equal(border.Background, Brushes.Red); + } - var border = Assert.IsType(target.VisualChild); - Assert.NotNull(target.Template); - Assert.Equal(border.Background, Brushes.Red); + private static ThemedControl CreateTarget() + { + return new ThemedControl + { + Theme = CreateTheme(), + }; + } + + private static TestRoot CreateRoot(IControl child) + { + var result = new TestRoot(child); + result.LayoutManager.ExecuteInitialLayoutPass(); + return result; + } } - private static ThemedControl CreateTarget() + public class ThemeFromStyle { - return new ThemedControl + [Fact] + public void Theme_Is_Applied_When_Attached_To_Logical_Tree() { - Theme = CreateTheme(), - }; + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(); + + Assert.Null(target.Theme); + Assert.Null(target.Template); + + var root = CreateRoot(target); + + Assert.NotNull(target.Theme); + Assert.NotNull(target.Template); + + var border = Assert.IsType(target.VisualChild); + Assert.Equal(border.Background, Brushes.Red); + + target.Classes.Add("foo"); + Assert.Equal(border.Background, Brushes.Green); + } + + private static ThemedControl CreateTarget() + { + return new ThemedControl(); + } + + private static TestRoot CreateRoot(IControl child) + { + var result = new TestRoot() + { + Styles = + { + new Style(x => x.OfType()) + { + Setters = + { + new Setter(TemplatedControl.ThemeProperty, CreateTheme()) + } + } + } + }; + + result.Child = child; + result.LayoutManager.ExecuteInitialLayoutPass(); + return result; + } } private static ControlTheme CreateTheme() @@ -104,13 +163,6 @@ namespace Avalonia.Controls.UnitTests.Primitives }; } - private static TestRoot CreateRoot(IControl child) - { - var result = new TestRoot(child); - result.LayoutManager.ExecuteInitialLayoutPass(); - return result; - } - private class ThemedControl : TemplatedControl { public IVisual? VisualChild => VisualChildren?.SingleOrDefault(); diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlThemeTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlThemeTests.cs index 05083537cd..9eb48311df 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlThemeTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlThemeTests.cs @@ -38,6 +38,42 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml } } + [Fact] + public void ControlTheme_Can_Be_Set_In_Style() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = $@" + + + {ControlThemeXaml} + + + + + + + +"; + + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var button = Assert.IsType(window.Content); + + window.LayoutManager.ExecuteInitialLayoutPass(); + + Assert.NotNull(button.Template); + + var child = Assert.Single(button.GetVisualChildren()); + var border = Assert.IsType(child); + + Assert.Equal(Brushes.Red, border.Background); + } + } + private const string ControlThemeXaml = @" From 5cd95320128fa97fff7af8223cf55b9d043086f8 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 2 Jun 2022 09:36:53 +0200 Subject: [PATCH 067/224] Move tests to correct place. --- .../Styling/StyledElementTests_Theming.cs | 169 +++++++++++++++++ .../TemplatedControlTests_Theming.cs | 171 ------------------ 2 files changed, 169 insertions(+), 171 deletions(-) create mode 100644 tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs delete mode 100644 tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests_Theming.cs diff --git a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs new file mode 100644 index 0000000000..539f9e6576 --- /dev/null +++ b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs @@ -0,0 +1,169 @@ +using System.Linq; +using Avalonia.Controls; +using Avalonia.Controls.Primitives; +using Avalonia.Controls.Templates; +using Avalonia.Media; +using Avalonia.Styling; +using Avalonia.UnitTests; +using Avalonia.VisualTree; +using Xunit; + +namespace Avalonia.Base.UnitTests.Styling; + +public class StyledElementTests_Theming +{ + public class InlineTheme + { + [Fact] + public void Theme_Is_Applied_When_Attached_To_Logical_Tree() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(); + + Assert.Null(target.Template); + + var root = CreateRoot(target); + Assert.NotNull(target.Template); + + var border = Assert.IsType(target.VisualChild); + Assert.Equal(border.Background, Brushes.Red); + + target.Classes.Add("foo"); + Assert.Equal(border.Background, Brushes.Green); + } + + [Fact] + public void Theme_Is_Detached_When_Theme_Property_Cleared() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(); + var root = CreateRoot(target); + + Assert.NotNull(target.Template); + + target.Theme = null; + Assert.Null(target.Template); + } + + [Fact] + public void Theme_Is_Applied_On_Layout_After_Theme_Property_Changes() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = new ThemedControl(); + var root = CreateRoot(target); + + Assert.Null(target.Template); + + target.Theme = CreateTheme(); + Assert.Null(target.Template); + + root.LayoutManager.ExecuteLayoutPass(); + + var border = Assert.IsType(target.VisualChild); + Assert.NotNull(target.Template); + Assert.Equal(border.Background, Brushes.Red); + } + + private static ThemedControl CreateTarget() + { + return new ThemedControl + { + Theme = CreateTheme(), + }; + } + + private static TestRoot CreateRoot(IControl child) + { + var result = new TestRoot(child); + result.LayoutManager.ExecuteInitialLayoutPass(); + return result; + } + } + + public class ThemeFromStyle + { + [Fact] + public void Theme_Is_Applied_When_Attached_To_Logical_Tree() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(); + + Assert.Null(target.Theme); + Assert.Null(target.Template); + + var root = CreateRoot(target); + + Assert.NotNull(target.Theme); + Assert.NotNull(target.Template); + + var border = Assert.IsType(target.VisualChild); + Assert.Equal(border.Background, Brushes.Red); + + target.Classes.Add("foo"); + Assert.Equal(border.Background, Brushes.Green); + } + + private static ThemedControl CreateTarget() + { + return new ThemedControl(); + } + + private static TestRoot CreateRoot(IControl child) + { + var result = new TestRoot() + { + Styles = + { + new Style(x => x.OfType()) + { + Setters = + { + new Setter(TemplatedControl.ThemeProperty, CreateTheme()) + } + } + } + }; + + result.Child = child; + result.LayoutManager.ExecuteInitialLayoutPass(); + return result; + } + } + + private static ControlTheme CreateTheme() + { + var template = new FuncControlTemplate((o, n) => + new Border { Name = "PART_Border" }); + + return new ControlTheme + { + TargetType = typeof(ThemedControl), + Setters = + { + new Setter(ThemedControl.TemplateProperty, template), + }, + Children = + { + new Style(x => x.Nesting().Template().OfType()) + { + Setters = + { + new Setter(Border.BackgroundProperty, Brushes.Red), + } + }, + new Style(x => x.Nesting().Class("foo").Template().OfType()) + { + Setters = + { + new Setter(Border.BackgroundProperty, Brushes.Green), + } + }, + } + }; + } + + private class ThemedControl : TemplatedControl + { + public IVisual? VisualChild => VisualChildren?.SingleOrDefault(); + } +} diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests_Theming.cs b/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests_Theming.cs deleted file mode 100644 index 74d75ff056..0000000000 --- a/tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests_Theming.cs +++ /dev/null @@ -1,171 +0,0 @@ -using System.Linq; -using Avalonia.Controls.Primitives; -using Avalonia.Controls.Templates; -using Avalonia.Media; -using Avalonia.Styling; -using Avalonia.UnitTests; -using Avalonia.VisualTree; -using Xunit; - -#nullable enable - -namespace Avalonia.Controls.UnitTests.Primitives -{ - public class TemplatedControlTests_Theming - { - public class InlineTheme - { - [Fact] - public void Theme_Is_Applied_When_Attached_To_Logical_Tree() - { - using var app = UnitTestApplication.Start(TestServices.RealStyler); - var target = CreateTarget(); - - Assert.Null(target.Template); - - var root = CreateRoot(target); - Assert.NotNull(target.Template); - - var border = Assert.IsType(target.VisualChild); - Assert.Equal(border.Background, Brushes.Red); - - target.Classes.Add("foo"); - Assert.Equal(border.Background, Brushes.Green); - } - - [Fact] - public void Theme_Is_Detached_When_Theme_Property_Cleared() - { - using var app = UnitTestApplication.Start(TestServices.RealStyler); - var target = CreateTarget(); - var root = CreateRoot(target); - - Assert.NotNull(target.Template); - - target.Theme = null; - Assert.Null(target.Template); - } - - [Fact] - public void Theme_Is_Applied_On_Layout_After_Theme_Property_Changes() - { - using var app = UnitTestApplication.Start(TestServices.RealStyler); - var target = new ThemedControl(); - var root = CreateRoot(target); - - Assert.Null(target.Template); - - target.Theme = CreateTheme(); - Assert.Null(target.Template); - - root.LayoutManager.ExecuteLayoutPass(); - - var border = Assert.IsType(target.VisualChild); - Assert.NotNull(target.Template); - Assert.Equal(border.Background, Brushes.Red); - } - - private static ThemedControl CreateTarget() - { - return new ThemedControl - { - Theme = CreateTheme(), - }; - } - - private static TestRoot CreateRoot(IControl child) - { - var result = new TestRoot(child); - result.LayoutManager.ExecuteInitialLayoutPass(); - return result; - } - } - - public class ThemeFromStyle - { - [Fact] - public void Theme_Is_Applied_When_Attached_To_Logical_Tree() - { - using var app = UnitTestApplication.Start(TestServices.RealStyler); - var target = CreateTarget(); - - Assert.Null(target.Theme); - Assert.Null(target.Template); - - var root = CreateRoot(target); - - Assert.NotNull(target.Theme); - Assert.NotNull(target.Template); - - var border = Assert.IsType(target.VisualChild); - Assert.Equal(border.Background, Brushes.Red); - - target.Classes.Add("foo"); - Assert.Equal(border.Background, Brushes.Green); - } - - private static ThemedControl CreateTarget() - { - return new ThemedControl(); - } - - private static TestRoot CreateRoot(IControl child) - { - var result = new TestRoot() - { - Styles = - { - new Style(x => x.OfType()) - { - Setters = - { - new Setter(TemplatedControl.ThemeProperty, CreateTheme()) - } - } - } - }; - - result.Child = child; - result.LayoutManager.ExecuteInitialLayoutPass(); - return result; - } - } - - private static ControlTheme CreateTheme() - { - var template = new FuncControlTemplate((o, n) => - new Border { Name = "PART_Border" }); - - return new ControlTheme - { - TargetType = typeof(ThemedControl), - Setters = - { - new Setter(ThemedControl.TemplateProperty, template), - }, - Children = - { - new Style(x => x.Nesting().Template().OfType()) - { - Setters = - { - new Setter(Border.BackgroundProperty, Brushes.Red), - } - }, - new Style(x => x.Nesting().Class("foo").Template().OfType()) - { - Setters = - { - new Setter(Border.BackgroundProperty, Brushes.Green), - } - }, - } - }; - } - - private class ThemedControl : TemplatedControl - { - public IVisual? VisualChild => VisualChildren?.SingleOrDefault(); - } - } -} From 4bdcb8eeeaab2f790245ddb70a5cfca3df7886f8 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 2 Jun 2022 10:34:34 +0200 Subject: [PATCH 068/224] Invalidate template control styles when Theme changes. --- .../Primitives/TemplatedControl.cs | 11 +++ .../Styling/StyledElementTests_Theming.cs | 75 ++++++++++++++----- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/TemplatedControl.cs b/src/Avalonia.Controls/Primitives/TemplatedControl.cs index db029d38c0..a07dd9ae27 100644 --- a/src/Avalonia.Controls/Primitives/TemplatedControl.cs +++ b/src/Avalonia.Controls/Primitives/TemplatedControl.cs @@ -365,6 +365,17 @@ namespace Avalonia.Controls.Primitives { } + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + + if (change.Property == ThemeProperty) + { + foreach (var child in this.GetTemplateChildren()) + child.InvalidateStyles(); + } + } + /// /// Called when the control's template is applied. /// diff --git a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs index 539f9e6576..0c0808987a 100644 --- a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs +++ b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs @@ -8,6 +8,8 @@ using Avalonia.UnitTests; using Avalonia.VisualTree; using Xunit; +#nullable enable + namespace Avalonia.Base.UnitTests.Styling; public class StyledElementTests_Theming @@ -45,6 +47,40 @@ public class StyledElementTests_Theming Assert.Null(target.Template); } + [Fact] + public void Theme_Is_Detached_From_Template_Controls_When_Theme_Property_Cleared() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + + var theme = new ControlTheme + { + TargetType = typeof(ThemedControl), + Children = + { + new Style(x => x.Nesting().Template().OfType()) + { + Setters = + { + new Setter(Canvas.BackgroundProperty, Brushes.Red), + } + }, + } + }; + + var target = CreateTarget(theme); + target.Template = new FuncControlTemplate((o, n) => new Canvas()); + + var root = CreateRoot(target); + + var canvas = Assert.IsType(target.VisualChild); + Assert.Equal(canvas.Background, Brushes.Red); + + target.Theme = null; + + Assert.IsType(target.VisualChild); + Assert.Null(canvas.Background); + } + [Fact] public void Theme_Is_Applied_On_Layout_After_Theme_Property_Changes() { @@ -64,11 +100,11 @@ public class StyledElementTests_Theming Assert.Equal(border.Background, Brushes.Red); } - private static ThemedControl CreateTarget() + private static ThemedControl CreateTarget(ControlTheme? theme = null) { return new ThemedControl { - Theme = CreateTheme(), + Theme = theme ?? CreateTheme(), }; } @@ -132,33 +168,32 @@ public class StyledElementTests_Theming private static ControlTheme CreateTheme() { - var template = new FuncControlTemplate((o, n) => - new Border { Name = "PART_Border" }); + var template = new FuncControlTemplate((o, n) => new Border()); return new ControlTheme { TargetType = typeof(ThemedControl), Setters = - { - new Setter(ThemedControl.TemplateProperty, template), - }, - Children = - { - new Style(x => x.Nesting().Template().OfType()) { - Setters = - { - new Setter(Border.BackgroundProperty, Brushes.Red), - } + new Setter(ThemedControl.TemplateProperty, template), }, - new Style(x => x.Nesting().Class("foo").Template().OfType()) + Children = { - Setters = + new Style(x => x.Nesting().Template().OfType()) { - new Setter(Border.BackgroundProperty, Brushes.Green), - } - }, - } + Setters = + { + new Setter(Border.BackgroundProperty, Brushes.Red), + } + }, + new Style(x => x.Nesting().Class("foo").Template().OfType()) + { + Setters = + { + new Setter(Border.BackgroundProperty, Brushes.Green), + } + }, + } }; } From 827692fa272aaa841d352d5aef3d5b5f9c5614dd Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 2 Jun 2022 22:36:22 +0100 Subject: [PATCH 069/224] add test for osx modal dialog window order when clicking resize grip of parent. --- .../WindowTests.cs | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs index b027c17ff3..59e705bd9b 100644 --- a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs @@ -3,9 +3,11 @@ using System.Collections.Generic; using System.Linq; using System.Reactive.Disposables; using System.Runtime.InteropServices; +using System.Threading.Tasks; using Avalonia.Controls; using OpenQA.Selenium; using OpenQA.Selenium.Appium; +using OpenQA.Selenium.Interactions; using SeleniumExtras.PageObjects; using Xunit; using Xunit.Sdk; @@ -50,7 +52,7 @@ namespace Avalonia.IntegrationTests.Appium { SwitchToNewWindowHack(mainWindow); var closeButton = _session.FindElementByAccessibilityId("CloseWindow"); - closeButton.SendClick(); + closeButton.Click(); } catch { } }); @@ -85,6 +87,43 @@ namespace Avalonia.IntegrationTests.Appium } } + [PlatformFact(SkipOnWindows = true)] + public void OSX_WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_Clicking_Resize_Grip() + { + var mainWindowHandle = GetCurrentWindowHandleHack(); + + var mainWindow = + _session.FindWindowOuter("MainWindow"); + + try + { + using (OpenWindow(ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) + { + new Actions(_session) + .MoveToElement(mainWindow, 100, 1) + .ClickAndHold() + .Perform(); + + var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); + + int mainWindowIndex = windows.GetWindowOrder("MainWindow"); + int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); + + new Actions(_session) + .MoveToElement(mainWindow, 100, 1) + .Release() + .Perform(); + + Assert.Equal(0, secondaryWindowIndex); + Assert.Equal(1, mainWindowIndex); + } + } + finally + { + SwitchToMainWindowHack(mainWindowHandle); + } + } + [PlatformFact(SkipOnWindows = true)] public void OSX_WindowOrder_Owned_Dialog_Stays_InFront_Of_Parent() { From a0af269d36b6300e941b1d9196746c08668528ec Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 2 Jun 2022 23:02:37 +0100 Subject: [PATCH 070/224] reset app after most tests, add test for fullscreen mode with modal osx. --- .../WindowTests.cs | 72 +++++++++++++++---- 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs index 59e705bd9b..8bf429c2ed 100644 --- a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs @@ -84,6 +84,7 @@ namespace Avalonia.IntegrationTests.Appium finally { SwitchToMainWindowHack(mainWindowHandle); + _session.ResetApp(); } } @@ -121,6 +122,41 @@ namespace Avalonia.IntegrationTests.Appium finally { SwitchToMainWindowHack(mainWindowHandle); + _session.ResetApp(); + } + } + + [PlatformFact(SkipOnWindows = true)] + public void OSX_WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_In_Fullscreen() + { + var mainWindowHandle = GetCurrentWindowHandleHack(); + + var mainWindow = + _session.FindWindowOuter("MainWindow"); + + var buttons = mainWindow.GetChromeButtons(); + + buttons.zoomButton.Click(); + + Task.Delay(500).Wait(); + + try + { + using (OpenWindow(ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) + { + var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); + + int mainWindowIndex = windows.GetWindowOrder("MainWindow"); + int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); + + Assert.Equal(0, secondaryWindowIndex); + Assert.Equal(1, mainWindowIndex); + } + } + finally + { + SwitchToMainWindowHack(mainWindowHandle); + _session.ResetApp(); } } @@ -150,31 +186,39 @@ namespace Avalonia.IntegrationTests.Appium finally { SwitchToMainWindowHack(mainWindowHandle); + _session.ResetApp(); } } - + [PlatformFact(SkipOnWindows = true)] public void OSX_WindowOrder_NonOwned_Window_Does_Not_Stay_InFront_Of_Parent() { var mainWindow = _session.FindElementByAccessibilityId("MainWindow"); - using (OpenWindow(ShowWindowMode.NonOwned, WindowStartupLocation.CenterOwner, 1400)) + try { - mainWindow.Click(); - - var secondaryWindow = - _session.FindElementByAccessibilityId("SecondaryWindow"); + using (OpenWindow(ShowWindowMode.NonOwned, WindowStartupLocation.CenterOwner, 1400)) + { + mainWindow.Click(); - var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); + var secondaryWindow = + _session.FindElementByAccessibilityId("SecondaryWindow"); - int mainWindowIndex = windows.GetWindowOrder("MainWindow"); - int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); + var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); - Assert.Equal(1, secondaryWindowIndex); - Assert.Equal(0, mainWindowIndex); - - secondaryWindow.SendClick(); + int mainWindowIndex = windows.GetWindowOrder("MainWindow"); + int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); + + Assert.Equal(1, secondaryWindowIndex); + Assert.Equal(0, mainWindowIndex); + + secondaryWindow.SendClick(); + } + } + finally + { + _session.ResetApp(); } } @@ -206,6 +250,7 @@ namespace Avalonia.IntegrationTests.Appium finally { SwitchToMainWindowHack(mainWindowHandle); + _session.ResetApp(); } } @@ -233,6 +278,7 @@ namespace Avalonia.IntegrationTests.Appium finally { SwitchToMainWindowHack(mainWindowHandle); + _session.ResetApp(); } } From 49613c7bceb5244444df97862b59285915a8e4ee Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 3 Jun 2022 12:04:37 +0200 Subject: [PATCH 071/224] Add accent button to control catalog. --- samples/ControlCatalog/Pages/ButtonsPage.xaml | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/ControlCatalog/Pages/ButtonsPage.xaml b/samples/ControlCatalog/Pages/ButtonsPage.xaml index 059b4d9788..8a474a203d 100644 --- a/samples/ControlCatalog/Pages/ButtonsPage.xaml +++ b/samples/ControlCatalog/Pages/ButtonsPage.xaml @@ -90,6 +90,7 @@ + Date: Fri, 3 Jun 2022 14:32:19 +0200 Subject: [PATCH 072/224] Fix nested :not selector. --- src/Avalonia.Base/Styling/NotSelector.cs | 2 +- .../Styling/SelectorTests_Nesting.cs | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Styling/NotSelector.cs b/src/Avalonia.Base/Styling/NotSelector.cs index cdc3254d38..76a0690e96 100644 --- a/src/Avalonia.Base/Styling/NotSelector.cs +++ b/src/Avalonia.Base/Styling/NotSelector.cs @@ -67,6 +67,6 @@ namespace Avalonia.Styling } protected override Selector? MovePrevious() => _previous; - internal override bool HasValidNestingSelector() => _argument.HasValidNestingSelector(); + internal override bool HasValidNestingSelector() => _previous?.HasValidNestingSelector() ?? false; } } diff --git a/tests/Avalonia.Base.UnitTests/Styling/SelectorTests_Nesting.cs b/tests/Avalonia.Base.UnitTests/Styling/SelectorTests_Nesting.cs index d49fcf03a2..1520dc329d 100644 --- a/tests/Avalonia.Base.UnitTests/Styling/SelectorTests_Nesting.cs +++ b/tests/Avalonia.Base.UnitTests/Styling/SelectorTests_Nesting.cs @@ -257,6 +257,30 @@ namespace Avalonia.Base.UnitTests.Styling parent.Children.Add(child); } + + [Fact] + public void Nesting_Not_Class_Matches() + { + var control = new Control1 { Classes = { "foo" } }; + Style nested; + var parent = new Style(x => x.OfType()) + { + Children = + { + (nested = new Style(x => x.Nesting().Not(y => y.Class("foo")))), + } + }; + + var match = nested.Selector.Match(control, parent); + Assert.Equal(SelectorMatchResult.Sometimes, match.Result); + + var sink = new ActivatorSink(match.Activator); + + Assert.False(sink.Active); + control.Classes.Clear(); + Assert.True(sink.Active); + } + public class Control1 : Control { } From 9a82e65f5323bd3a3ff04b11a7e7dba0d6399f50 Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Fri, 3 Jun 2022 23:53:01 +0300 Subject: [PATCH 073/224] [X11] Improve _NET_WM_SYNC_REQUEST handling --- src/Avalonia.X11/X11Window.cs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.X11/X11Window.cs b/src/Avalonia.X11/X11Window.cs index 6d0ca9422b..e7c4a37ea2 100644 --- a/src/Avalonia.X11/X11Window.cs +++ b/src/Avalonia.X11/X11Window.cs @@ -47,6 +47,7 @@ namespace Avalonia.X11 private IntPtr _renderHandle; private IntPtr _xSyncCounter; private XSyncValue _xSyncValue; + private XSyncState _xSyncState = 0; private bool _mapped; private bool _wasMappedAtLeastOnce = false; private double? _scalingOverride; @@ -54,6 +55,14 @@ namespace Avalonia.X11 private TransparencyHelper _transparencyHelper; private RawEventGrouper _rawEventGrouper; private bool _useRenderWindow = false; + + enum XSyncState + { + None, + WaitConfigure, + WaitPaint + } + public X11Window(AvaloniaX11Platform platform, IWindowImpl popupParent) { _platform = platform; @@ -507,7 +516,11 @@ namespace Avalonia.X11 if (_useRenderWindow) XConfigureResizeWindow(_x11.Display, _renderHandle, ev.ConfigureEvent.width, ev.ConfigureEvent.height); - EnqueuePaint(); + if (_xSyncState == XSyncState.WaitConfigure) + { + _xSyncState = XSyncState.WaitPaint; + EnqueuePaint(); + } } else if (ev.type == XEventName.DestroyNotify && ev.DestroyWindowEvent.window == _handle) @@ -527,6 +540,7 @@ namespace Avalonia.X11 { _xSyncValue.Lo = new UIntPtr(ev.ClientMessageEvent.ptr3.ToPointer()).ToUInt32(); _xSyncValue.Hi = ev.ClientMessageEvent.ptr4.ToInt32(); + _xSyncState = XSyncState.WaitConfigure; } } } @@ -755,8 +769,11 @@ namespace Avalonia.X11 void DoPaint() { Paint?.Invoke(new Rect()); - if (_xSyncCounter != IntPtr.Zero) + if (_xSyncCounter != IntPtr.Zero && _xSyncState == XSyncState.WaitPaint) + { + _xSyncState = XSyncState.None; XSyncSetCounter(_x11.Display, _xSyncCounter, _xSyncValue); + } } public void Invalidate(Rect rect) @@ -802,6 +819,12 @@ namespace Avalonia.X11 XDestroyIC(_xic); _xic = IntPtr.Zero; } + + if (_xSyncCounter != IntPtr.Zero) + { + XSyncDestroyCounter(_x11.Display, _xSyncCounter); + _xSyncCounter = IntPtr.Zero; + } if (_handle != IntPtr.Zero) { From 05fdc0446416a285c9067b56d858c018e4f7104d Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 3 Jun 2022 23:43:55 +0200 Subject: [PATCH 074/224] Add ControlTheme.BasedOn. --- src/Avalonia.Base/Styling/ControlTheme.cs | 26 ++++++-- src/Avalonia.Base/Styling/NestingSelector.cs | 12 +++- src/Avalonia.Base/Styling/Style.cs | 35 ++++++++--- src/Avalonia.Base/Styling/StyleBase.cs | 61 ++++++------------- .../Styling/StyledElementTests_Theming.cs | 59 ++++++++++++++++-- 5 files changed, 129 insertions(+), 64 deletions(-) diff --git a/src/Avalonia.Base/Styling/ControlTheme.cs b/src/Avalonia.Base/Styling/ControlTheme.cs index 9dcbd7d2c4..aff6fad990 100644 --- a/src/Avalonia.Base/Styling/ControlTheme.cs +++ b/src/Avalonia.Base/Styling/ControlTheme.cs @@ -23,16 +23,32 @@ namespace Avalonia.Styling /// public Type? TargetType { get; set; } - internal override bool HasSelector => TargetType is not null; + /// + /// Gets or sets a control theme that is the basis of the current theme. + /// + public ControlTheme? BasedOn { get; set; } - internal override SelectorMatch Match(IStyleable control, object? host, bool subscribe) + public override SelectorMatchResult TryAttach(IStyleable target, object? host) { + _ = target ?? throw new ArgumentNullException(nameof(target)); + if (TargetType is null) throw new InvalidOperationException("ControlTheme has no TargetType."); - return control.StyleKey == TargetType ? - SelectorMatch.AlwaysThisType : - SelectorMatch.NeverThisType; + var result = BasedOn?.TryAttach(target, host) ?? SelectorMatchResult.NeverThisType; + + if (HasSettersOrAnimations && target.StyleKey == TargetType) + { + Attach(target, null); + result = SelectorMatchResult.AlwaysThisType; + } + + var childResult = TryAttachChildren(target, host); + + if (childResult > result) + result = childResult; + + return result; } internal override void SetParent(StyleBase? parent) diff --git a/src/Avalonia.Base/Styling/NestingSelector.cs b/src/Avalonia.Base/Styling/NestingSelector.cs index 6d31f7cb18..c8945a713d 100644 --- a/src/Avalonia.Base/Styling/NestingSelector.cs +++ b/src/Avalonia.Base/Styling/NestingSelector.cs @@ -15,9 +15,17 @@ namespace Avalonia.Styling protected override SelectorMatch Evaluate(IStyleable control, IStyle? parent, bool subscribe) { - if (parent is StyleBase s && s.HasSelector) + if (parent is Style s && s.Selector is not null) { - return s.Match(control, null, subscribe); + return s.Selector.Match(control, s.Parent, subscribe); + } + else if (parent is ControlTheme theme) + { + if (theme.TargetType is null) + throw new InvalidOperationException("ControlTheme has no TargetType."); + return control.StyleKey == theme.TargetType ? + SelectorMatch.AlwaysThisType : + SelectorMatch.NeverThisType; } throw new InvalidOperationException( diff --git a/src/Avalonia.Base/Styling/Style.cs b/src/Avalonia.Base/Styling/Style.cs index ca20ff2b4b..7a6b746488 100644 --- a/src/Avalonia.Base/Styling/Style.cs +++ b/src/Avalonia.Base/Styling/Style.cs @@ -28,7 +28,32 @@ namespace Avalonia.Styling /// public Selector? Selector { get; set; } - internal override bool HasSelector => Selector is not null; + public override SelectorMatchResult TryAttach(IStyleable target, object? host) + { + _ = target ?? throw new ArgumentNullException(nameof(target)); + + var result = SelectorMatchResult.NeverThisType; + + if (HasSettersOrAnimations) + { + var match = Selector?.Match(target, Parent, true) ?? + (target == host ? + SelectorMatch.AlwaysThisInstance : + SelectorMatch.NeverThisInstance); + + if (match.IsMatch) + Attach(target, match.Activator); + + result = match.Result; + } + + var childResult = TryAttachChildren(target, host); + + if (childResult > result) + result = childResult; + + return result; + } /// /// Returns a string representation of the style. @@ -46,14 +71,6 @@ namespace Avalonia.Styling } } - internal override SelectorMatch Match(IStyleable control, object? host, bool subscribe) - { - return Selector?.Match(control, Parent, subscribe) ?? - (control == host ? - SelectorMatch.AlwaysThisInstance : - SelectorMatch.NeverThisInstance); - } - internal override void SetParent(StyleBase? parent) { if (parent is Style parentStyle && parentStyle.Selector is not null) diff --git a/src/Avalonia.Base/Styling/StyleBase.cs b/src/Avalonia.Base/Styling/StyleBase.cs index b6bfec62bd..306a4cf010 100644 --- a/src/Avalonia.Base/Styling/StyleBase.cs +++ b/src/Avalonia.Base/Styling/StyleBase.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using Avalonia.Animation; using Avalonia.Controls; using Avalonia.Metadata; +using Avalonia.Styling.Activators; namespace Avalonia.Styling { @@ -64,43 +65,14 @@ namespace Avalonia.Styling bool IResourceNode.HasResources => _resources?.Count > 0; IReadOnlyList IStyle.Children => (IReadOnlyList?)_children ?? Array.Empty(); - internal abstract bool HasSelector { get; } + internal bool HasSettersOrAnimations => _setters?.Count > 0 || _animations?.Count > 0; public void Add(ISetter setter) => Setters.Add(setter); public void Add(IStyle style) => Children.Add(style); public event EventHandler? OwnerChanged; - public SelectorMatchResult TryAttach(IStyleable target, object? host) - { - target = target ?? throw new ArgumentNullException(nameof(target)); - - var result = SelectorMatchResult.NeverThisType; - - if (_setters?.Count > 0 || _animations?.Count > 0) - { - var match = Match(target, host, subscribe: true); - - if (match.IsMatch) - { - var instance = new StyleInstance(this, target, _setters, _animations, match.Activator); - target.StyleApplied(instance); - instance.Start(); - } - - result = match.Result; - } - - if (_children is not null) - { - _childCache ??= new StyleCache(); - var childResult = _childCache.TryAttach(_children, target, host); - if (childResult > result) - result = childResult; - } - - return result; - } + public abstract SelectorMatchResult TryAttach(IStyleable target, object? host); public bool TryGetResource(object key, out object? result) { @@ -108,19 +80,20 @@ namespace Avalonia.Styling return _resources?.TryGetResource(key, out result) ?? false; } - /// - /// Evaluates the style's selector against the specified target element. - /// - /// The control. - /// The element that hosts the style. - /// - /// Whether the match should subscribe to changes in order to track the match over time, - /// or simply return an immediate result. - /// - /// - /// A describing how the style matches the control. - /// - internal abstract SelectorMatch Match(IStyleable control, object? host, bool subscribe); + internal void Attach(IStyleable target, IStyleActivator? activator) + { + var instance = new StyleInstance(this, target, _setters, _animations, activator); + target.StyleApplied(instance); + instance.Start(); + } + + internal SelectorMatchResult TryAttachChildren(IStyleable target, object? host) + { + if (_children is null || _children.Count == 0) + return SelectorMatchResult.NeverThisType; + _childCache ??= new StyleCache(); + return _childCache.TryAttach(_children, target, host); + } internal virtual void SetParent(StyleBase? parent) => Parent = parent; diff --git a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs index 0c0808987a..737cf1e048 100644 --- a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs +++ b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs @@ -28,10 +28,10 @@ public class StyledElementTests_Theming Assert.NotNull(target.Template); var border = Assert.IsType(target.VisualChild); - Assert.Equal(border.Background, Brushes.Red); + Assert.Equal(Brushes.Red, border.Background); target.Classes.Add("foo"); - Assert.Equal(border.Background, Brushes.Green); + Assert.Equal(Brushes.Green, border.Background); } [Fact] @@ -73,7 +73,7 @@ public class StyledElementTests_Theming var root = CreateRoot(target); var canvas = Assert.IsType(target.VisualChild); - Assert.Equal(canvas.Background, Brushes.Red); + Assert.Equal(Brushes.Red, canvas.Background); target.Theme = null; @@ -97,7 +97,28 @@ public class StyledElementTests_Theming var border = Assert.IsType(target.VisualChild); Assert.NotNull(target.Template); - Assert.Equal(border.Background, Brushes.Red); + Assert.Equal(Brushes.Red, border.Background); + } + + [Fact] + public void BasedOn_Theme_Is_Applied_When_Attached_To_Logical_Tree() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(CreateDerivedTheme()); + + Assert.Null(target.Template); + + var root = CreateRoot(target); + Assert.NotNull(target.Template); + Assert.Equal(Brushes.Blue, target.BorderBrush); + + var border = Assert.IsType(target.VisualChild); + Assert.Equal(Brushes.Red, border.Background); + Assert.Equal(Brushes.Yellow, border.BorderBrush); + + target.Classes.Add("foo"); + Assert.Equal(Brushes.Green, border.Background); + Assert.Equal(Brushes.Cyan, border.BorderBrush); } private static ThemedControl CreateTarget(ControlTheme? theme = null) @@ -197,6 +218,36 @@ public class StyledElementTests_Theming }; } + private static ControlTheme CreateDerivedTheme() + { + return new ControlTheme + { + TargetType = typeof(ThemedControl), + BasedOn = CreateTheme(), + Setters = + { + new Setter(Border.BorderBrushProperty, Brushes.Blue), + }, + Children = + { + new Style(x => x.Nesting().Template().OfType()) + { + Setters = + { + new Setter(Border.BorderBrushProperty, Brushes.Yellow), + } + }, + new Style(x => x.Nesting().Class("foo").Template().OfType()) + { + Setters = + { + new Setter(Border.BorderBrushProperty, Brushes.Cyan), + } + }, + } + }; + } + private class ThemedControl : TemplatedControl { public IVisual? VisualChild => VisualChildren?.SingleOrDefault(); From a34e03d7f5ee0a67b00ddf8f2dac4d42a497df58 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Fri, 3 Jun 2022 19:12:12 -0400 Subject: [PATCH 075/224] Fix android service initialization order --- src/Android/Avalonia.Android/AndroidPlatform.cs | 2 -- src/Android/Avalonia.Android/AvaloniaActivity.cs | 7 ++++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Android/Avalonia.Android/AndroidPlatform.cs b/src/Android/Avalonia.Android/AndroidPlatform.cs index 61aa6ce946..c7dd896cb8 100644 --- a/src/Android/Avalonia.Android/AndroidPlatform.cs +++ b/src/Android/Avalonia.Android/AndroidPlatform.cs @@ -61,8 +61,6 @@ namespace Avalonia.Android .Bind().ToConstant(new RenderLoop()) .Bind().ToSingleton(); - SkiaPlatform.Initialize(); - if (options.UseGpu) { EglPlatformOpenGlInterface.TryInitialize(); diff --git a/src/Android/Avalonia.Android/AvaloniaActivity.cs b/src/Android/Avalonia.Android/AvaloniaActivity.cs index f5d620a97a..f3692d33d7 100644 --- a/src/Android/Avalonia.Android/AvaloniaActivity.cs +++ b/src/Android/Avalonia.Android/AvaloniaActivity.cs @@ -31,21 +31,22 @@ namespace Avalonia.Android CustomizeAppBuilder(builder); - View = new AvaloniaView(this); - SetContentView(View); var lifetime = new SingleViewLifetime(); - lifetime.View = View; builder.AfterSetup(x => { _viewModel = new ViewModelProvider(this).Get(Java.Lang.Class.FromType(typeof(AvaloniaViewModel))) as AvaloniaViewModel; + View = new AvaloniaView(this); if (_viewModel.Content != null) { View.Content = _viewModel.Content; } + SetContentView(View); + lifetime.View = View; + View.Prepare(); }); From b277db43bd88c860ff998796570fd5540a8b4342 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Fri, 3 Jun 2022 21:02:09 -0400 Subject: [PATCH 076/224] Update release build parameters --- .../ControlCatalog.Android/ControlCatalog.Android.csproj | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/samples/ControlCatalog.Android/ControlCatalog.Android.csproj b/samples/ControlCatalog.Android/ControlCatalog.Android.csproj index 04c67e84e8..ec88852feb 100644 --- a/samples/ControlCatalog.Android/ControlCatalog.Android.csproj +++ b/samples/ControlCatalog.Android/ControlCatalog.Android.csproj @@ -20,10 +20,13 @@ - True + False + False True True - True + no-write-symbols,nodebug + Hybrid + True From 1d1ef5ca9fdb42d2fbf029f122530dcca9b51a59 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 4 Jun 2022 12:45:19 +0200 Subject: [PATCH 077/224] Display control themes in devtools. --- src/Avalonia.Base/Styling/ControlTheme.cs | 8 ++++++++ .../Diagnostics/ViewModels/ControlDetailsViewModel.cs | 11 +++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Base/Styling/ControlTheme.cs b/src/Avalonia.Base/Styling/ControlTheme.cs index aff6fad990..399eb9ae59 100644 --- a/src/Avalonia.Base/Styling/ControlTheme.cs +++ b/src/Avalonia.Base/Styling/ControlTheme.cs @@ -51,6 +51,14 @@ namespace Avalonia.Styling return result; } + public override string ToString() + { + if (TargetType is not null) + return "ControlTheme: " + TargetType.Name; + else + return "ControlTheme"; + } + internal override void SetParent(StyleBase? parent) { throw new InvalidOperationException("ControlThemes cannot be added as a nested style."); diff --git a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlDetailsViewModel.cs b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlDetailsViewModel.cs index e383c160e3..795826e4f6 100644 --- a/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlDetailsViewModel.cs +++ b/src/Avalonia.Diagnostics/Diagnostics/ViewModels/ControlDetailsViewModel.cs @@ -67,8 +67,15 @@ namespace Avalonia.Diagnostics.ViewModels var setters = new List(); - if (styleSource is Style style) + if (styleSource is StyleBase style) { + var selector = style switch + { + Style s => s.Selector?.ToString(), + ControlTheme t => t.TargetType?.Name.ToString(), + _ => null, + }; + foreach (var setter in style.Setters) { if (setter is Setter regularSetter @@ -105,7 +112,7 @@ namespace Avalonia.Diagnostics.ViewModels } } - AppliedStyles.Add(new StyleViewModel(appliedStyle, style.Selector?.ToString() ?? "No selector", setters)); + AppliedStyles.Add(new StyleViewModel(appliedStyle, selector ?? "No selector", setters)); } } From 95f70143ca319e5e6f49601d1e2de60bdbb39759 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 4 Jun 2022 14:53:31 +0200 Subject: [PATCH 078/224] Can apply control theme to derived types. --- src/Avalonia.Base/Styling/ControlTheme.cs | 2 +- src/Avalonia.Base/Styling/NestingSelector.cs | 2 +- .../Styling/StyledElementTests_Theming.cs | 25 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Base/Styling/ControlTheme.cs b/src/Avalonia.Base/Styling/ControlTheme.cs index 399eb9ae59..644e8b32d4 100644 --- a/src/Avalonia.Base/Styling/ControlTheme.cs +++ b/src/Avalonia.Base/Styling/ControlTheme.cs @@ -37,7 +37,7 @@ namespace Avalonia.Styling var result = BasedOn?.TryAttach(target, host) ?? SelectorMatchResult.NeverThisType; - if (HasSettersOrAnimations && target.StyleKey == TargetType) + if (HasSettersOrAnimations && TargetType.IsAssignableFrom(target.StyleKey)) { Attach(target, null); result = SelectorMatchResult.AlwaysThisType; diff --git a/src/Avalonia.Base/Styling/NestingSelector.cs b/src/Avalonia.Base/Styling/NestingSelector.cs index c8945a713d..4393d3239f 100644 --- a/src/Avalonia.Base/Styling/NestingSelector.cs +++ b/src/Avalonia.Base/Styling/NestingSelector.cs @@ -23,7 +23,7 @@ namespace Avalonia.Styling { if (theme.TargetType is null) throw new InvalidOperationException("ControlTheme has no TargetType."); - return control.StyleKey == theme.TargetType ? + return theme.TargetType.IsAssignableFrom(control.StyleKey) ? SelectorMatch.AlwaysThisType : SelectorMatch.NeverThisType; } diff --git a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs index 737cf1e048..ab6c239393 100644 --- a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs +++ b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs @@ -34,6 +34,27 @@ public class StyledElementTests_Theming Assert.Equal(Brushes.Green, border.Background); } + [Fact] + public void Theme_Is_Applied_To_Derived_Class_When_Attached_To_Logical_Tree() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = new DerivedThemedControl + { + Theme = CreateTheme(), + }; + + Assert.Null(target.Template); + + var root = CreateRoot(target); + Assert.NotNull(target.Template); + + var border = Assert.IsType(target.VisualChild); + Assert.Equal(Brushes.Red, border.Background); + + target.Classes.Add("foo"); + Assert.Equal(Brushes.Green, border.Background); + } + [Fact] public void Theme_Is_Detached_When_Theme_Property_Cleared() { @@ -252,4 +273,8 @@ public class StyledElementTests_Theming { public IVisual? VisualChild => VisualChildren?.SingleOrDefault(); } + + private class DerivedThemedControl : ThemedControl + { + } } From 51d158dc1b1c3f27f6868407307a0ff1ce5aea14 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 8 Jun 2022 01:39:25 +0100 Subject: [PATCH 079/224] clear layout jobs instead of explicit layout call. --- src/Avalonia.Controls/TopLevel.cs | 7 ++++++- src/Avalonia.Controls/WindowBase.cs | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 57fb82485c..31217c70a9 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -13,6 +13,7 @@ using Avalonia.Media; using Avalonia.Platform; using Avalonia.Rendering; using Avalonia.Styling; +using Avalonia.Threading; using Avalonia.Utilities; using Avalonia.VisualTree; @@ -412,7 +413,11 @@ namespace Avalonia.Controls FrameSize = PlatformImpl!.FrameSize; Width = clientSize.Width; Height = clientSize.Height; - LayoutManager.ExecuteLayoutPass(); + + // Setting ClientSize and Width / Height above caused ExecuteLayoutPass to be queued. + // Instead of explicitly calling LayoutManager.ExecuteLayoutPass here, we clear the job queue. + Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout); + Renderer?.Resized(clientSize); } diff --git a/src/Avalonia.Controls/WindowBase.cs b/src/Avalonia.Controls/WindowBase.cs index 12ba143c8a..5e827cc08f 100644 --- a/src/Avalonia.Controls/WindowBase.cs +++ b/src/Avalonia.Controls/WindowBase.cs @@ -8,6 +8,7 @@ using Avalonia.Controls.Primitives; using Avalonia.Input; using Avalonia.Layout; using Avalonia.Platform; +using Avalonia.Threading; using JetBrains.Annotations; namespace Avalonia.Controls @@ -219,7 +220,11 @@ namespace Avalonia.Controls { ClientSize = clientSize; FrameSize = PlatformImpl?.FrameSize; - LayoutManager.ExecuteLayoutPass(); + + // Setting ClientSize and Width / Height above caused ExecuteLayoutPass to be queued. + // Instead of explicitly calling LayoutManager.ExecuteLayoutPass here, we clear the job queue. + Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout); + Renderer?.Resized(clientSize); } From 33c61fdac5272f6f7912e9a39d8a10c4530ba8a5 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 8 Jun 2022 01:41:18 +0100 Subject: [PATCH 080/224] reduce work done on osx native side. --- .../Avalonia.Native/src/OSX/WindowBaseImpl.mm | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm b/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm index 7f2bb128da..f133fa34f6 100644 --- a/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm +++ b/native/Avalonia.Native/src/OSX/WindowBaseImpl.mm @@ -298,14 +298,15 @@ HRESULT WindowBaseImpl::Resize(double x, double y, AvnPlatformResizeReason reaso } @try { - lastSize = NSSize {x, y}; - - if (!_shown) { - BaseEvents->Resized(AvnSize{x, y}, reason); - } - else if(Window != nullptr) { - [Window setContentSize:lastSize]; - [Window invalidateShadow]; + if(x != lastSize.width || y != lastSize.height) { + lastSize = NSSize{x, y}; + + if (!_shown) { + BaseEvents->Resized(AvnSize{x, y}, reason); + } else if (Window != nullptr) { + [Window setContentSize:lastSize]; + [Window invalidateShadow]; + } } } @finally { From 3b9a34274a31ebccf7fb3fa9f7e280203b01c0cc Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Wed, 8 Jun 2022 09:35:02 +0200 Subject: [PATCH 081/224] feat(IDispatcher): Add void Post(Action action,object arg, DispatcherPriority priority = default); --- src/Avalonia.Base/Threading/Dispatcher.cs | 2 +- src/Avalonia.Base/Threading/IDispatcher.cs | 9 +++++++++ src/Avalonia.Base/Threading/JobRunner.cs | 14 +++++++------- tests/Avalonia.UnitTests/ImmediateDispatcher.cs | 3 ++- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Avalonia.Base/Threading/Dispatcher.cs b/src/Avalonia.Base/Threading/Dispatcher.cs index 2eb2e7c01f..571f782813 100644 --- a/src/Avalonia.Base/Threading/Dispatcher.cs +++ b/src/Avalonia.Base/Threading/Dispatcher.cs @@ -118,7 +118,7 @@ namespace Avalonia.Threading } /// - public void Post(Action action, T arg, DispatcherPriority priority = default) + public void Post(SendOrPostCallback action, object? arg, DispatcherPriority priority = default) { _ = action ?? throw new ArgumentNullException(nameof(action)); _jobRunner.Post(action, arg, priority); diff --git a/src/Avalonia.Base/Threading/IDispatcher.cs b/src/Avalonia.Base/Threading/IDispatcher.cs index 713a7ac4d7..1c700132b7 100644 --- a/src/Avalonia.Base/Threading/IDispatcher.cs +++ b/src/Avalonia.Base/Threading/IDispatcher.cs @@ -1,4 +1,5 @@ using System; +using System.Threading; using System.Threading.Tasks; namespace Avalonia.Threading @@ -26,6 +27,14 @@ namespace Avalonia.Threading /// The priority with which to invoke the method. void Post(Action action, DispatcherPriority priority = default); + /// + /// Posts an action that will be invoked on the dispatcher thread. + /// + /// The method. + /// The argument of method to call. + /// The priority with which to invoke the method. + void Post(SendOrPostCallback action, object? arg, DispatcherPriority priority = default); + /// /// Invokes a action on the dispatcher thread. /// diff --git a/src/Avalonia.Base/Threading/JobRunner.cs b/src/Avalonia.Base/Threading/JobRunner.cs index 4b304d44f6..ced3423c27 100644 --- a/src/Avalonia.Base/Threading/JobRunner.cs +++ b/src/Avalonia.Base/Threading/JobRunner.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Avalonia.Platform; @@ -81,9 +82,9 @@ namespace Avalonia.Threading /// The method to call. /// The parameter of method to call. /// The priority with which to invoke the method. - internal void Post(Action action, T parameter, DispatcherPriority priority) + internal void Post(SendOrPostCallback action, object? parameter, DispatcherPriority priority) { - AddJob(new Job(action, parameter, priority, true)); + AddJob(new JobWithArg(action, parameter, priority, true)); } /// @@ -207,11 +208,10 @@ namespace Avalonia.Threading /// /// A typed job to run. /// - /// Type of job parameter - private sealed class Job : IJob + private sealed class JobWithArg : IJob { - private readonly Action _action; - private readonly T _parameter; + private readonly SendOrPostCallback _action; + private readonly object? _parameter; private readonly TaskCompletionSource? _taskCompletionSource; /// @@ -222,7 +222,7 @@ namespace Avalonia.Threading /// The job priority. /// Do not wrap exception in TaskCompletionSource - public Job(Action action, T parameter, DispatcherPriority priority, bool throwOnUiThread) + public JobWithArg(SendOrPostCallback action, object? parameter, DispatcherPriority priority, bool throwOnUiThread) { _action = action; _parameter = parameter; diff --git a/tests/Avalonia.UnitTests/ImmediateDispatcher.cs b/tests/Avalonia.UnitTests/ImmediateDispatcher.cs index 03c89732f3..f6f3de3bc6 100644 --- a/tests/Avalonia.UnitTests/ImmediateDispatcher.cs +++ b/tests/Avalonia.UnitTests/ImmediateDispatcher.cs @@ -1,4 +1,5 @@ using System; +using System.Threading; using System.Threading.Tasks; using Avalonia.Threading; @@ -22,7 +23,7 @@ namespace Avalonia.UnitTests } /// - public void Post(Action action, T arg, DispatcherPriority priority) + public void Post(SendOrPostCallback action, object arg, DispatcherPriority priority) { action(arg); } From 44ac72ce223a3c7cd56da551940d000d30dd9b76 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Wed, 8 Jun 2022 11:13:38 +0200 Subject: [PATCH 082/224] fix(AvaloniaSynchronizationContext): avoid clousure --- src/Avalonia.Base/Threading/AvaloniaSynchronizationContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Threading/AvaloniaSynchronizationContext.cs b/src/Avalonia.Base/Threading/AvaloniaSynchronizationContext.cs index 21649306cb..2cade55f32 100644 --- a/src/Avalonia.Base/Threading/AvaloniaSynchronizationContext.cs +++ b/src/Avalonia.Base/Threading/AvaloniaSynchronizationContext.cs @@ -30,7 +30,7 @@ namespace Avalonia.Threading /// public override void Post(SendOrPostCallback d, object? state) { - Dispatcher.UIThread.Post(() => d(state), DispatcherPriority.Background); + Dispatcher.UIThread.Post(d, state, DispatcherPriority.Background); } /// From d21e634ab308c1b63d1e2f2105de29c8af236b2a Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 8 Jun 2022 12:00:14 +0200 Subject: [PATCH 083/224] Added support for implicit themes. If no `Theme` property is provided, try to look up a resource keyed with the control's `StyleKey`. --- src/Avalonia.Base/StyledElement.cs | 28 ++++++++++++ src/Avalonia.Base/Styling/IStyleable.cs | 4 +- src/Avalonia.Base/Styling/Styler.cs | 4 +- .../Styling/StyledElementTests_Theming.cs | 43 +++++++++++++++++++ .../AvaloniaPropertyConverterTest.cs | 4 +- .../DynamicResourceExtensionTests.cs | 6 ++- 6 files changed, 81 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.Base/StyledElement.cs b/src/Avalonia.Base/StyledElement.cs index 75c4b94174..f377eb848c 100644 --- a/src/Avalonia.Base/StyledElement.cs +++ b/src/Avalonia.Base/StyledElement.cs @@ -60,6 +60,7 @@ namespace Avalonia public static readonly StyledProperty ThemeProperty = AvaloniaProperty.Register(nameof(Theme)); + private static readonly ControlTheme s_invalidTheme = new ControlTheme(); private int _initCount; private string? _name; private readonly Classes _classes = new Classes(); @@ -72,6 +73,7 @@ namespace Avalonia private ITemplatedControl? _templatedParent; private bool _dataContextUpdating; private bool _hasPromotedTheme; + private ControlTheme? _implicitTheme; /// /// Initializes static members of the class. @@ -495,6 +497,31 @@ namespace Avalonia }; } + ControlTheme? IStyleable.GetEffectiveTheme() + { + var theme = Theme; + + // Explitly set Theme property takes precedence. + if (theme is not null) + return theme; + + // If the Theme property is not set, try to find a ControlTheme resource with our StyleKey. + if (_implicitTheme is null) + { + var key = ((IStyleable)this).StyleKey; + + if (this.TryFindResource(key, out var value) && value is ControlTheme t) + _implicitTheme = t; + else + _implicitTheme = s_invalidTheme; + } + + if (_implicitTheme != s_invalidTheme) + return _implicitTheme; + + return null; + } + void IStyleable.StyleApplied(IStyleInstance instance) { instance = instance ?? throw new ArgumentNullException(nameof(instance)); @@ -736,6 +763,7 @@ namespace Avalonia if (_logicalRoot != null) { _logicalRoot = null; + _implicitTheme = null; DetachStyles(); OnDetachedFromLogicalTree(e); DetachedFromLogicalTree?.Invoke(this, e); diff --git a/src/Avalonia.Base/Styling/IStyleable.cs b/src/Avalonia.Base/Styling/IStyleable.cs index 61fcbdf850..254da4d85c 100644 --- a/src/Avalonia.Base/Styling/IStyleable.cs +++ b/src/Avalonia.Base/Styling/IStyleable.cs @@ -27,9 +27,9 @@ namespace Avalonia.Styling ITemplatedControl? TemplatedParent { get; } /// - /// Gets the theme to be applied to the control. + /// Gets the effective theme for the control as used by the syling system. /// - public ControlTheme? Theme { get; } + ControlTheme? GetEffectiveTheme(); /// /// Notifies the element that a style has been applied. diff --git a/src/Avalonia.Base/Styling/Styler.cs b/src/Avalonia.Base/Styling/Styler.cs index c9ea123bdc..6ac2e8d372 100644 --- a/src/Avalonia.Base/Styling/Styler.cs +++ b/src/Avalonia.Base/Styling/Styler.cs @@ -11,10 +11,10 @@ namespace Avalonia.Styling // If the control has a themed templated parent then first apply the styles from // the templated parent theme. if (target.TemplatedParent is IStyleable styleableParent) - styleableParent.Theme?.TryAttach(target, styleableParent); + styleableParent.GetEffectiveTheme()?.TryAttach(target, styleableParent); // Next apply the control theme. - target.Theme?.TryAttach(target, target); + target.GetEffectiveTheme()?.TryAttach(target, target); // Apply styles from the rest of the tree. if (target is IStyleHost styleHost) diff --git a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs index ab6c239393..522937b669 100644 --- a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs +++ b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs @@ -158,6 +158,49 @@ public class StyledElementTests_Theming } } + public class ImplicitTheme + { + [Fact] + public void Implicit_Theme_Is_Applied_When_Attached_To_Logical_Tree() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(); + var root = CreateRoot(target); + Assert.NotNull(target.Template); + + var border = Assert.IsType(target.VisualChild); + Assert.Equal(Brushes.Red, border.Background); + + target.Classes.Add("foo"); + Assert.Equal(Brushes.Green, border.Background); + } + + [Fact] + public void Implicit_Theme_Is_Cleared_When_Removed_From_Logical_Tree() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(); + var root = CreateRoot(target); + + Assert.NotNull(((IStyleable)target).GetEffectiveTheme()); + + root.Child = null; + + Assert.Null(((IStyleable)target).GetEffectiveTheme()); + } + + private static ThemedControl CreateTarget() => new ThemedControl(); + + private static TestRoot CreateRoot(IControl child) + { + var result = new TestRoot(); + result.Resources.Add(typeof(ThemedControl), CreateTheme()); + result.Child = child; + result.LayoutManager.ExecuteInitialLayoutPass(); + return result; + } + } + public class ThemeFromStyle { [Fact] diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Converters/AvaloniaPropertyConverterTest.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Converters/AvaloniaPropertyConverterTest.cs index ca59fe8480..75e21a7138 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Converters/AvaloniaPropertyConverterTest.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Converters/AvaloniaPropertyConverterTest.cs @@ -137,9 +137,9 @@ namespace Avalonia.Markup.Xaml.UnitTests.Converters get { throw new NotImplementedException(); } } - public ControlTheme Theme + public ControlTheme GetEffectiveTheme() { - get { throw new NotImplementedException(); } + throw new NotImplementedException(); } public void DetachStyles() diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs index 592dbfc0d1..987725c314 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs @@ -845,7 +845,8 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions Assert.Equal("bar", border.Tag); var resourceProvider = (TrackingResourceProvider)window.Resources.MergedDictionaries[0]; - Assert.Equal(new[] { "bar" }, resourceProvider.RequestedResources); + Assert.Contains("bar", resourceProvider.RequestedResources); + Assert.DoesNotContain("foo", resourceProvider.RequestedResources); } [Fact] @@ -883,7 +884,8 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions Assert.Equal("bar", border.Tag); var resourceProvider = (TrackingResourceProvider)window.Resources.MergedDictionaries[0]; - Assert.Equal(new[] { "bar" }, resourceProvider.RequestedResources); + Assert.Contains("bar", resourceProvider.RequestedResources); + Assert.DoesNotContain("foo", resourceProvider.RequestedResources); } private IDisposable StyledWindow(params (string, string)[] assets) From 96931e2203840f8e99679cffd2722e4958f44e0a Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 8 Jun 2022 11:29:55 +0100 Subject: [PATCH 084/224] prevent excess layout passes in layout manager to catch other scenarios causing excessive layout passes. --- src/Avalonia.Base/Layout/LayoutManager.cs | 14 ++++++++++++-- src/Avalonia.Controls/TopLevel.cs | 6 +----- src/Avalonia.Controls/WindowBase.cs | 6 +----- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Avalonia.Base/Layout/LayoutManager.cs b/src/Avalonia.Base/Layout/LayoutManager.cs index fc988a8d6c..446f135c83 100644 --- a/src/Avalonia.Base/Layout/LayoutManager.cs +++ b/src/Avalonia.Base/Layout/LayoutManager.cs @@ -28,7 +28,7 @@ namespace Avalonia.Layout public LayoutManager(ILayoutRoot owner) { _owner = owner ?? throw new ArgumentNullException(nameof(owner)); - _executeLayoutPass = ExecuteLayoutPass; + _executeLayoutPass = ExecuteQueuedLayoutPass; } public virtual event EventHandler? LayoutUpdated; @@ -94,6 +94,16 @@ namespace Avalonia.Layout QueueLayoutPass(); } + private void ExecuteQueuedLayoutPass() + { + if (!_queued) + { + return; + } + + ExecuteLayoutPass(); + } + /// public virtual void ExecuteLayoutPass() { @@ -319,8 +329,8 @@ namespace Avalonia.Layout { if (!_queued && !_running) { - Dispatcher.UIThread.Post(_executeLayoutPass, DispatcherPriority.Layout); _queued = true; + Dispatcher.UIThread.Post(_executeLayoutPass, DispatcherPriority.Layout); } } diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 31217c70a9..0bc4adf1b5 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -413,11 +413,7 @@ namespace Avalonia.Controls FrameSize = PlatformImpl!.FrameSize; Width = clientSize.Width; Height = clientSize.Height; - - // Setting ClientSize and Width / Height above caused ExecuteLayoutPass to be queued. - // Instead of explicitly calling LayoutManager.ExecuteLayoutPass here, we clear the job queue. - Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout); - + LayoutManager.ExecuteLayoutPass(); Renderer?.Resized(clientSize); } diff --git a/src/Avalonia.Controls/WindowBase.cs b/src/Avalonia.Controls/WindowBase.cs index 5e827cc08f..224aeea9e9 100644 --- a/src/Avalonia.Controls/WindowBase.cs +++ b/src/Avalonia.Controls/WindowBase.cs @@ -220,11 +220,7 @@ namespace Avalonia.Controls { ClientSize = clientSize; FrameSize = PlatformImpl?.FrameSize; - - // Setting ClientSize and Width / Height above caused ExecuteLayoutPass to be queued. - // Instead of explicitly calling LayoutManager.ExecuteLayoutPass here, we clear the job queue. - Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout); - + LayoutManager.ExecuteLayoutPass(); Renderer?.Resized(clientSize); } From 726ac748ed1136156e564dfec23a65b172e71a47 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 8 Jun 2022 11:30:50 +0100 Subject: [PATCH 085/224] restore changes. --- src/Avalonia.Controls/TopLevel.cs | 1 - src/Avalonia.Controls/WindowBase.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 0bc4adf1b5..57fb82485c 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -13,7 +13,6 @@ using Avalonia.Media; using Avalonia.Platform; using Avalonia.Rendering; using Avalonia.Styling; -using Avalonia.Threading; using Avalonia.Utilities; using Avalonia.VisualTree; diff --git a/src/Avalonia.Controls/WindowBase.cs b/src/Avalonia.Controls/WindowBase.cs index 224aeea9e9..12ba143c8a 100644 --- a/src/Avalonia.Controls/WindowBase.cs +++ b/src/Avalonia.Controls/WindowBase.cs @@ -8,7 +8,6 @@ using Avalonia.Controls.Primitives; using Avalonia.Input; using Avalonia.Layout; using Avalonia.Platform; -using Avalonia.Threading; using JetBrains.Annotations; namespace Avalonia.Controls From 0b4ea2b1eb09115951546e4992657e8b396e9316 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 8 Jun 2022 11:47:40 +0100 Subject: [PATCH 086/224] add unit test. --- .../Layout/LayoutManagerTests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Avalonia.Base.UnitTests/Layout/LayoutManagerTests.cs b/tests/Avalonia.Base.UnitTests/Layout/LayoutManagerTests.cs index f0e8e1cd11..a097d395d8 100644 --- a/tests/Avalonia.Base.UnitTests/Layout/LayoutManagerTests.cs +++ b/tests/Avalonia.Base.UnitTests/Layout/LayoutManagerTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using Avalonia.Controls; using Avalonia.Layout; +using Avalonia.Threading; using Xunit; namespace Avalonia.Base.UnitTests.Layout @@ -421,5 +422,22 @@ namespace Avalonia.Base.UnitTests.Layout Assert.Equal(new Size(200, 200), control.Bounds.Size); Assert.Equal(new Size(200, 200), control.DesiredSize); } + + [Fact] + public void LayoutManager_Execute_Layout_Pass_Should_Clear_Queued_LayoutPasses() + { + var control = new LayoutTestControl(); + var root = new LayoutTestRoot { Child = control }; + + int layoutCount = 0; + root.LayoutUpdated += (sender, args) => layoutCount++; + + root.LayoutManager.InvalidateArrange(control); + root.LayoutManager.ExecuteInitialLayoutPass(); + + Dispatcher.UIThread.RunJobs(DispatcherPriority.Layout); + + Assert.Equal(1, layoutCount); + } } } From 236d10bf64b03e52a93183f6adbb26fcdf09ac04 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 8 Jun 2022 11:48:44 +0100 Subject: [PATCH 087/224] discard unused params --- tests/Avalonia.Base.UnitTests/Layout/LayoutManagerTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Avalonia.Base.UnitTests/Layout/LayoutManagerTests.cs b/tests/Avalonia.Base.UnitTests/Layout/LayoutManagerTests.cs index a097d395d8..37e07c244e 100644 --- a/tests/Avalonia.Base.UnitTests/Layout/LayoutManagerTests.cs +++ b/tests/Avalonia.Base.UnitTests/Layout/LayoutManagerTests.cs @@ -430,7 +430,7 @@ namespace Avalonia.Base.UnitTests.Layout var root = new LayoutTestRoot { Child = control }; int layoutCount = 0; - root.LayoutUpdated += (sender, args) => layoutCount++; + root.LayoutUpdated += (_, _) => layoutCount++; root.LayoutManager.InvalidateArrange(control); root.LayoutManager.ExecuteInitialLayoutPass(); From 7a533700d0bcb54be20ec448e5b7802ad6ac677b Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Thu, 9 Jun 2022 12:24:39 +0200 Subject: [PATCH 088/224] Implement GlyphRun.BuildGeometry --- samples/RenderDemo/Pages/GlyphRunPage.xaml | 12 +- samples/RenderDemo/Pages/GlyphRunPage.xaml.cs | 113 +++++++++++++----- src/Avalonia.Base/Media/GlyphRun.cs | 19 +++ src/Avalonia.Base/Media/PlatformGeometry.cs | 24 ++++ .../Platform/IPlatformRenderInterface.cs | 8 ++ .../Avalonia.Skia/PlatformRenderInterface.cs | 32 +++++ .../Avalonia.Direct2D1/Direct2D1Platform.cs | 28 +++++ .../Media/GlyphRunTests.cs | 66 ++++++++++ ...ould_Render_GlyphRun_Geometry.expected.png | Bin 0 -> 1476 bytes ...ould_Render_GlyphRun_Geometry.expected.png | Bin 0 -> 1170 bytes 10 files changed, 269 insertions(+), 33 deletions(-) create mode 100644 src/Avalonia.Base/Media/PlatformGeometry.cs create mode 100644 tests/Avalonia.RenderTests/Media/GlyphRunTests.cs create mode 100644 tests/TestFiles/Direct2D1/Media/GlyphRun/Should_Render_GlyphRun_Geometry.expected.png create mode 100644 tests/TestFiles/Skia/Media/GlyphRun/Should_Render_GlyphRun_Geometry.expected.png diff --git a/samples/RenderDemo/Pages/GlyphRunPage.xaml b/samples/RenderDemo/Pages/GlyphRunPage.xaml index c2914e8847..7db58e5286 100644 --- a/samples/RenderDemo/Pages/GlyphRunPage.xaml +++ b/samples/RenderDemo/Pages/GlyphRunPage.xaml @@ -2,13 +2,13 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:local="clr-namespace:RenderDemo.Pages" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="RenderDemo.Pages.GlyphRunPage"> - - - - + + + diff --git a/samples/RenderDemo/Pages/GlyphRunPage.xaml.cs b/samples/RenderDemo/Pages/GlyphRunPage.xaml.cs index 7f85606957..674ed8e61f 100644 --- a/samples/RenderDemo/Pages/GlyphRunPage.xaml.cs +++ b/samples/RenderDemo/Pages/GlyphRunPage.xaml.cs @@ -9,14 +9,6 @@ namespace RenderDemo.Pages { public class GlyphRunPage : UserControl { - private Image _imageControl; - private GlyphTypeface _glyphTypeface = Typeface.Default.GlyphTypeface; - private readonly Random _rand = new Random(); - private ushort[] _glyphIndices = new ushort[1]; - private char[] _characters = new char[1]; - private float _fontSize = 20; - private int _direction = 10; - public GlyphRunPage() { this.InitializeComponent(); @@ -25,19 +17,43 @@ namespace RenderDemo.Pages private void InitializeComponent() { AvaloniaXamlLoader.Load(this); + } + } + + public class GlyphRunControl : Control + { + private GlyphTypeface _glyphTypeface = Typeface.Default.GlyphTypeface; + private readonly Random _rand = new Random(); + private ushort[] _glyphIndices = new ushort[1]; + private char[] _characters = new char[1]; + private float _fontSize = 20; + private int _direction = 10; - _imageControl = this.FindControl("imageControl"); - _imageControl.Source = new DrawingImage(); + private DispatcherTimer _timer; - DispatcherTimer.Run(() => + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + _timer = new DispatcherTimer + { + Interval = TimeSpan.FromSeconds(1) + }; + + _timer.Tick += (s,e) => { - UpdateGlyphRun(); + InvalidateVisual(); + }; + + _timer.Start(); + } + + protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) + { + _timer.Stop(); - return true; - }, TimeSpan.FromSeconds(1)); + _timer = null; } - private void UpdateGlyphRun() + public override void Render(DrawingContext context) { var c = (char)_rand.Next(65, 90); @@ -57,27 +73,70 @@ namespace RenderDemo.Pages _characters[0] = c; - var scale = (double)_fontSize / _glyphTypeface.DesignEmHeight; + var glyphRun = new GlyphRun(_glyphTypeface, _fontSize, _characters, _glyphIndices); - var drawingGroup = new DrawingGroup(); + context.DrawGlyphRun(Brushes.Black, glyphRun); + } + } - var glyphRunDrawing = new GlyphRunDrawing + public class GlyphRunGeometryControl : Control + { + private GlyphTypeface _glyphTypeface = Typeface.Default.GlyphTypeface; + private readonly Random _rand = new Random(); + private ushort[] _glyphIndices = new ushort[1]; + private char[] _characters = new char[1]; + private float _fontSize = 20; + private int _direction = 10; + + private DispatcherTimer _timer; + + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + _timer = new DispatcherTimer { - Foreground = Brushes.Black, - GlyphRun = new GlyphRun(_glyphTypeface, _fontSize, _characters, _glyphIndices) + Interval = TimeSpan.FromSeconds(1) }; - drawingGroup.Children.Add(glyphRunDrawing); - - var geometryDrawing = new GeometryDrawing + _timer.Tick += (s, e) => { - Pen = new Pen(Brushes.Black), - Geometry = new RectangleGeometry { Rect = new Rect(glyphRunDrawing.GlyphRun.Size) } + InvalidateVisual(); }; - drawingGroup.Children.Add(geometryDrawing); + _timer.Start(); + } + + protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) + { + _timer.Stop(); + + _timer = null; + } + + public override void Render(DrawingContext context) + { + var c = (char)_rand.Next(65, 90); + + if (_fontSize + _direction > 200) + { + _direction = -10; + } + + if (_fontSize + _direction < 20) + { + _direction = 10; + } + + _fontSize += _direction; + + _glyphIndices[0] = _glyphTypeface.GetGlyph(c); + + _characters[0] = c; + + var glyphRun = new GlyphRun(_glyphTypeface, _fontSize, _characters, _glyphIndices); + + var geometry = glyphRun.BuildGeometry(); - (_imageControl.Source as DrawingImage).Drawing = drawingGroup; + context.DrawGeometry(Brushes.Green, null, geometry); } } } diff --git a/src/Avalonia.Base/Media/GlyphRun.cs b/src/Avalonia.Base/Media/GlyphRun.cs index 22be8d8865..25c35a28e5 100644 --- a/src/Avalonia.Base/Media/GlyphRun.cs +++ b/src/Avalonia.Base/Media/GlyphRun.cs @@ -194,6 +194,25 @@ namespace Avalonia.Media } } + /// + /// Obtains geometry for the glyph run. + /// + /// The geometry returned contains the combined geometry of all glyphs in the glyph run. + public Geometry BuildGeometry() + { + var platformRenderInterface = AvaloniaLocator.Current.GetRequiredService(); + + var geometryImpl = platformRenderInterface.BuildGlyphRunGeometry(this, out var scale); + + var geometry = new PlatformGeometry(geometryImpl); + + var transform = new MatrixTransform(Matrix.CreateTranslation(geometry.Bounds.Left, -geometry.Bounds.Top) * scale); + + geometry.Transform = transform; + + return geometry; + } + /// /// Retrieves the offset from the leading edge of the /// to the leading or trailing edge of a caret stop containing the specified character hit. diff --git a/src/Avalonia.Base/Media/PlatformGeometry.cs b/src/Avalonia.Base/Media/PlatformGeometry.cs new file mode 100644 index 0000000000..f25a14540f --- /dev/null +++ b/src/Avalonia.Base/Media/PlatformGeometry.cs @@ -0,0 +1,24 @@ +using Avalonia.Platform; + +namespace Avalonia.Media +{ + internal class PlatformGeometry : Geometry + { + private readonly IGeometryImpl _geometryImpl; + + public PlatformGeometry(IGeometryImpl geometryImpl) + { + _geometryImpl = geometryImpl; + } + + public override Geometry Clone() + { + return new PlatformGeometry(_geometryImpl); + } + + protected override IGeometryImpl? CreateDefiningGeometry() + { + return _geometryImpl; + } + } +} diff --git a/src/Avalonia.Base/Platform/IPlatformRenderInterface.cs b/src/Avalonia.Base/Platform/IPlatformRenderInterface.cs index 0eeefddf0b..bfa9e70fce 100644 --- a/src/Avalonia.Base/Platform/IPlatformRenderInterface.cs +++ b/src/Avalonia.Base/Platform/IPlatformRenderInterface.cs @@ -58,6 +58,14 @@ namespace Avalonia.Platform /// A combined geometry. IGeometryImpl CreateCombinedGeometry(GeometryCombineMode combineMode, Geometry g1, Geometry g2); + /// + /// Created a geometry implementation for the glyph run. + /// + /// The glyph run to build a geometry from. + /// The scaling of the produces geometry. + /// The geometry returned contains the combined geometry of all glyphs in the glyph run. + IGeometryImpl BuildGlyphRunGeometry(GlyphRun glyphRun, out Matrix scale); + /// /// Creates a renderer. /// diff --git a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs index d3c3585cd0..dc1b7785e2 100644 --- a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs +++ b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs @@ -62,6 +62,38 @@ namespace Avalonia.Skia return new CombinedGeometryImpl(combineMode, g1, g2); } + public IGeometryImpl BuildGlyphRunGeometry(GlyphRun glyphRun, out Matrix scale) + { + if (glyphRun.GlyphTypeface.PlatformImpl is not GlyphTypefaceImpl glyphTypeface) + { + throw new InvalidOperationException("PlatformImpl can't be null."); + } + + var fontRenderingEmSize = (float)glyphRun.FontRenderingEmSize; + var glyphs = glyphRun.GlyphIndices.ToArray(); + var skFont = new SKFont(glyphTypeface.Typeface, fontRenderingEmSize) + { + Size = fontRenderingEmSize, + Edging = SKFontEdging.Antialias, + Hinting = SKFontHinting.None, + LinearMetrics = true + }; + + SKPath path = null; + var matrix = SKMatrix.Identity; + + skFont.GetGlyphPaths(glyphs, (p, m) => + { + matrix = m; + + path = p; + }); + + scale = Matrix.CreateScale(matrix.ScaleX, matrix.ScaleY); + + return new StreamGeometryImpl(path); + } + /// public IBitmapImpl LoadBitmap(string fileName) { diff --git a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs index d9e992bb80..04025f92e4 100644 --- a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs +++ b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs @@ -159,6 +159,34 @@ namespace Avalonia.Direct2D1 public IGeometryImpl CreateGeometryGroup(FillRule fillRule, IReadOnlyList children) => new GeometryGroupImpl(fillRule, children); public IGeometryImpl CreateCombinedGeometry(GeometryCombineMode combineMode, Geometry g1, Geometry g2) => new CombinedGeometryImpl(combineMode, g1, g2); + public IGeometryImpl BuildGlyphRunGeometry(GlyphRun glyphRun, out Matrix scale) + { + if (glyphRun.GlyphTypeface.PlatformImpl is not GlyphTypefaceImpl glyphTypeface) + { + throw new InvalidOperationException("PlatformImpl can't be null."); + } + + var pathGeometry = new SharpDX.Direct2D1.PathGeometry(Direct2D1Factory); + + using (var sink = pathGeometry.Open()) + { + var glyphs = new short[glyphRun.GlyphIndices.Count]; + + for (int i = 0; i < glyphRun.GlyphIndices.Count; i++) + { + glyphs[i] = (short)glyphRun.GlyphIndices[i]; + } + + glyphTypeface.FontFace.GetGlyphRunOutline((float)glyphRun.FontRenderingEmSize, glyphs, null, null, false, !glyphRun.IsLeftToRight, sink); + + sink.Close(); + } + + scale = Matrix.Identity; + + return new StreamGeometryImpl(pathGeometry); + } + /// public IBitmapImpl LoadBitmap(string fileName) { diff --git a/tests/Avalonia.RenderTests/Media/GlyphRunTests.cs b/tests/Avalonia.RenderTests/Media/GlyphRunTests.cs new file mode 100644 index 0000000000..734e4d5012 --- /dev/null +++ b/tests/Avalonia.RenderTests/Media/GlyphRunTests.cs @@ -0,0 +1,66 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Avalonia.Controls; +using Avalonia.Controls.Shapes; +using Avalonia.Media; +using Avalonia.Media.Imaging; +using Xunit; + +#if AVALONIA_SKIA +namespace Avalonia.Skia.RenderTests +#else +namespace Avalonia.Direct2D1.RenderTests.Media +#endif +{ + public class GlyphRunTests : TestBase + { + public GlyphRunTests() + : base(@"Media\GlyphRun") + { + } + + [Fact] + public async Task Should_Render_GlyphRun_Geometry() + { + Decorator target = new Decorator + { + Padding = new Thickness(8), + Width = 80, + Height = 90, + Child = new GlyphRunGeometryControl() + }; + + await RenderToFile(target); + + CompareImages(); + } + + public class GlyphRunGeometryControl : Control + { + private readonly Geometry _geometry; + + public GlyphRunGeometryControl() + { + var glyphTypeface = Typeface.Default.GlyphTypeface; + + var glyphIndices = new[] { glyphTypeface.GetGlyph('A') }; + + var characters = new[] { 'A' }; + + var glyphRun = new GlyphRun(glyphTypeface, 100, characters, glyphIndices); + + _geometry = glyphRun.BuildGeometry(); + } + + protected override Size MeasureOverride(Size availableSize) + { + return _geometry.Bounds.Size; + } + + public override void Render(DrawingContext context) + { + context.DrawGeometry(Brushes.Green, null, _geometry); + } + } + } +} diff --git a/tests/TestFiles/Direct2D1/Media/GlyphRun/Should_Render_GlyphRun_Geometry.expected.png b/tests/TestFiles/Direct2D1/Media/GlyphRun/Should_Render_GlyphRun_Geometry.expected.png new file mode 100644 index 0000000000000000000000000000000000000000..0b40e667a408be1bda12d7749d0496a85a20ce3e GIT binary patch literal 1476 zcmV;#1v~nQP)Px#1ZP1_K>z@;j|==^1pojAZb?KzRCr$PoWE;SK@i9H#KOYD!a^ZJiWDg zks?A0g%l|w1Po~5fAQ}A0!vF3B18yiks^hKMT8V7QYr+rNMVt}!oos0XKwb)C3!nD z@4fvMvLCqjb|K*1cW2+ue0QIwP$(1%g+iflCF$9FK2&BJzSVczbFpLk=CN%j<(j?c zL(e?3EskXn`i^ZUB`*Oha~-Z5)M3&7U2t-Xz$N%hQB$C2p4wJY@)A%teaFl~#6Ga? zWaJ}YWp2au5p|R_6FdkxMc^8IZctOrDhPkmwv&*LfbF6Q#Y5qfk%xelnTP8fb!s`X z_lqQ+#(4A4mE38fB~4b?dYTzuo^x0)~Qp=z8RWtwp~xcD9j-OSKxDtnl%i~ z7r5?I$H_Vj1SZ$fNiU!rJ-cRL4&i=G-CD4$!-0rSS^?2Gpz6cLA&B?Gwm;f?J&7ul z)Dg(nvqSUFwy^!ywp^gWpeqACl2$-IYW~7!i<-c|9KiJ}b!x$9Ah#F=A)SELXzJn= zcJUcRJNw*|qAO!h)yb@o)YGfLqdEnw0 zqR%}|2af4INyKR!UqE@KlokppLJ>3=?%Q@y;tEK{0q@_*&p3=ij8Lw$ix!2kBM>cn zwC-ITfH+*TJjok-cd-Ej7TjQnE1;bFNmKI_`EPNcf*TC+1f)8ZGY+tHlb?TMC{lTw zD?()wGXl{#;HK0Lh;m}vI^-x^vI7^4cmn#IzNetsP@ixU!A~&65s>Or&N%o5A8y~H z9*Wpq>Np5IjpGO?{|I;KCLyOi({=!Z5MDqs4pyn-K$H8q!N}#GodKgTMg&SH4vrxD z_H4U3gLB4fJ$Qn_y>Zon@B(%-4s^&7ELT7+2`eDgqYKn=qR;(Y$aa6QX&hESxgOnv zAMZZ4(F8dP=k0)cC7gho)22SA?d$5BIPg1t$4GX+W?&l;Dgtp&f-?><@oiqZNoeeT&nFnd z2q-_SH{r)(0_-1MWIwm}&iEIQPR`Fz$3d9sOGLK&{lDtfH?QHE#Y-?OL-Wvz-3h-D zNE_O`agYn1azfuz80*cXuN^dC1qQsc!QfZGfVNQpyqsY0Cm@}amyCnF@UWQp6Yz3H z-xxF)xc5^;{0K_iO!S?DILegOV9~h5E&%A^yw^*FOce@AQ zuGOB59|8L#@wg%#zvSIu)dxr4yn^cs>NpshC-B``IYZs2Fui&&y{5XAL_9>ZVbx(8 z`*;RQ#sU6__qG+E!RYvhbYKZjFmU_UPPz*yk6yQ3Hy`M>O{+AauE+ol%?;}_yj{c0jrVYEd8G?_^#Ve+s=ak zIbk~iFU#U_9$ij22LZyp$8v_|0_uzdoG$ze(y8#gn6!yNT=^g<6^kNK3qQG(tYNoEnddNz(|FUR6NSIWKxNIpHD%jA$@4 z6Y#Q6-!ZzJP!)xG0m(R!4k)H~UbN`aEb)Ciy@0x5S`erhdK?hyU`shen>jF$BlgVTpXTyZ)boGd%}JK za^v9c&-6VJNUzQJggrBmWlIo$_Lk-bLmvT|ad5y3{0EDM$0Op#I)y7ASU$ZnFr9s? zWTxFD+JMFeLoWeYtqO@Yww-BeFtis?F%Ep<=oS~2Tk@17xYSQfoM>j?w?bQv2mICU=fsXP!Ri|+q0X002J$^RqAKfNQMqEfH~r1tMAr$Xt#m_)b$iPSMo(qQOvjv|K)g`xyr+ zL5Ojsom40Jdz7Ks{lx}@g@A1O?hS4GQi2duv?p99U~l6t7_!fO`^Ebc3N>K>nI45MP#gEPjS9L5Oj=oi52h zqFoD?42E0*mFXKY`|N{OryUE>1=Ng#{U?~duPAkzCQMESDm5Ww^w$?{PdLOMd7Mlp klgVT Date: Thu, 9 Jun 2022 12:44:22 +0200 Subject: [PATCH 089/224] Skia - Correctly translate current position when multiple glyphs are added to a path --- .../Avalonia.Skia/PlatformRenderInterface.cs | 15 ++++++---- .../Media/GlyphRunTests.cs | 27 ++++++++++++++---- ...ould_Render_GlyphRun_Geometry.expected.png | Bin 1476 -> 5566 bytes ...ould_Render_GlyphRun_Geometry.expected.png | Bin 1170 -> 4372 bytes 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs index dc1b7785e2..727677c82e 100644 --- a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs +++ b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs @@ -70,7 +70,6 @@ namespace Avalonia.Skia } var fontRenderingEmSize = (float)glyphRun.FontRenderingEmSize; - var glyphs = glyphRun.GlyphIndices.ToArray(); var skFont = new SKFont(glyphTypeface.Typeface, fontRenderingEmSize) { Size = fontRenderingEmSize, @@ -79,15 +78,19 @@ namespace Avalonia.Skia LinearMetrics = true }; - SKPath path = null; + SKPath path = new SKPath(); var matrix = SKMatrix.Identity; - skFont.GetGlyphPaths(glyphs, (p, m) => + var currentX = 0f; + + foreach (var glyph in glyphRun.GlyphIndices) { - matrix = m; + var p = skFont.GetGlyphPath(glyph); + + path.AddPath(p, currentX, 0); - path = p; - }); + currentX += p.Bounds.Right; + } scale = Matrix.CreateScale(matrix.ScaleX, matrix.ScaleY); diff --git a/tests/Avalonia.RenderTests/Media/GlyphRunTests.cs b/tests/Avalonia.RenderTests/Media/GlyphRunTests.cs index 734e4d5012..2de2dda29b 100644 --- a/tests/Avalonia.RenderTests/Media/GlyphRunTests.cs +++ b/tests/Avalonia.RenderTests/Media/GlyphRunTests.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Avalonia.Controls; +using Avalonia.Controls.Documents; using Avalonia.Controls.Shapes; using Avalonia.Media; using Avalonia.Media.Imaging; @@ -25,9 +26,21 @@ namespace Avalonia.Direct2D1.RenderTests.Media Decorator target = new Decorator { Padding = new Thickness(8), - Width = 80, - Height = 90, - Child = new GlyphRunGeometryControl() + Width = 200, + Height = 100, + Child = new GlyphRunGeometryControl + { + [TextElement.ForegroundProperty] = new LinearGradientBrush + { + StartPoint = new RelativePoint(0, 0.5, RelativeUnit.Relative), + EndPoint = new RelativePoint(1, 0.5, RelativeUnit.Relative), + GradientStops = + { + new GradientStop { Color = Colors.Red, Offset = 0 }, + new GradientStop { Color = Colors.Blue, Offset = 1 } + } + } + } }; await RenderToFile(target); @@ -43,9 +56,9 @@ namespace Avalonia.Direct2D1.RenderTests.Media { var glyphTypeface = Typeface.Default.GlyphTypeface; - var glyphIndices = new[] { glyphTypeface.GetGlyph('A') }; + var glyphIndices = new[] { glyphTypeface.GetGlyph('A'), glyphTypeface.GetGlyph('B'), glyphTypeface.GetGlyph('C') }; - var characters = new[] { 'A' }; + var characters = new[] { 'A', 'B', 'C' }; var glyphRun = new GlyphRun(glyphTypeface, 100, characters, glyphIndices); @@ -59,7 +72,9 @@ namespace Avalonia.Direct2D1.RenderTests.Media public override void Render(DrawingContext context) { - context.DrawGeometry(Brushes.Green, null, _geometry); + var foreground = TextElement.GetForeground(this); + + context.DrawGeometry(foreground, null, _geometry); } } } diff --git a/tests/TestFiles/Direct2D1/Media/GlyphRun/Should_Render_GlyphRun_Geometry.expected.png b/tests/TestFiles/Direct2D1/Media/GlyphRun/Should_Render_GlyphRun_Geometry.expected.png index 0b40e667a408be1bda12d7749d0496a85a20ce3e..40ccc9fdcddbfd3af4673109e9558f2a30f958e3 100644 GIT binary patch literal 5566 zcma)=^*GsRlum8 zpx^x$upQ*J}=D)ynLojv^#`x;g)lFT zO;Gth^*_Mpit-lumMny?HFb5IFpRIH@*7l60g86_ZNh072^Y*ZO?53C!ChPQ4SfPU zjdlYMq?TR#15x2iuc8;IP_Y56vbmk^Vb|JnzumQc1PVHounWOtWL(|D#6($iDCn=X z8Jb6HHmVt5j8vY#S5rux+uDnfuRkiJRw>>Lv-(QfXloa9q14`%$pk$3Q}>u z!;!Agx4F7jms{>@gVK0>x77_hQ23nq3=R}FmyS5y1fGEp-Ms_eqMWBdZw`5B3Z|rKJN7Dc zoAMOuvUd&HNN@N$nu zgiw0rL0$>*R3Vqji4T3OR%MirJ;34O_i7Qg^pDO>?Ow zfz%-@ZCU6+Q^^fB3l9mI@_Tv?xF9LbsI%5u(_dh8UDuU{olh2~!yLPgXRH8rM8er> zVCq9Av%n@;B111R)_WKI8c<`&FJLINh&N>62dcU5A|?R^7eHyA8GB?+@XZN47i#zH z*!op!#Uo(&8kzs8I&#O!zRVWs?O)s|3us7q>dqS+*ph|XmUMo7HlNDwZ-4yT*gZ?z zCO_>sv^6JPJo6^xvOGF|rlc6RgE2Dl2t>i3mFIERkrv43i8g-Eve%jqE) zdc=h3MaFAjiD^Gr3LOxl{#~n#fc?TC3Vlop^?Alg6mMPE!VX{qg5J?TSfRx1? zvb17uc|x|C%_E1^)lw%xFXYG%VFg_2uml%DWPYRnO2dbQ@B4iSAm8ugunI7~Zs459w8;}U^5d>Y&t+jrQlDUq$latJ zh^JEAEQ$H!QqSvwc-3KSTZS(2w22IZ9g_^aj_&w5QJM(d2HnobK+|@3Q`JuL;J^q%WHDs{;n>dh(Uw78CLW&``>^}DK;k( z`zIWsolt}ynBOTiOirI1Ko%A2vLRlo__323ftsPBwgIzP%x(q*iq6dfalpx114kP^ z2C%R_D^#oDPX<4AL)h4V5?`AFK4%6_o7{_!?v$^Z#Mk=QIPk4`DuL;1arJ1~ge(cj zN#|Y}YBqcl${@=(n>v{_*iqn$4Xq7YH5n0RswW7;&k~8xX<9{jRZz$Q(h7TFIB^Bh zBZq;nj6)?Pbg zve(N0_QDAZ`MQj&SsN!YhGY+CcRZf6o#XgbBFh&1EJ5=4pR8YR&EVRBn2}w5Ygh9hDgWUDH=`uBBKrVd?6`9#5W&4S_l>V&KAT1FG-DjK6gT^3Fg?p zG#0c;B8rFuRLdJ^+=^9b2MJnBe#`y5`p#Xglo+^RL=n#vbW>E+-74X^^iWR!hJ%(67*Gb(*G(uI)BMq$=~#@fBySqse_l5 zK=gtWMTWXe=m|gQn_$%f~Elh^b*{Kdkwj#1&>z^n@#In zzeYlEkbi1BoyZ=2@S&ap>)aJ=Riw|mt{QO{SJaB_dPU^@g_$J)ee(gS_Hx)8j1#1n zdVOMoEbxs(0{Zb!&A{r#Yom-q{!`UzOg+6qEl>e1611h8K}vxP#e8?kK@B z)oV0#c#okdIx06&C=+JQIV1e8vIoeGrP55?Tt{CMMBg$a6P6YKbDUOQfveD97uSVf zor|+Q6vSi*WpLK_nmCJ|ML_NcN@c<+;+oI3xU8#$jVeF+2)_!tR;e^|XURQwQU5?& z9>$M7fOlWW$q~oRc(nf7Xew%*lUGCj7Yw#wt>9|+wrs3TQB-H3lu|)qvI^dhG9`VP z9nArDKO0NV{AE~fV(%%QbNto-z@^xj8=K)X&MIp3M`b01a4u`p+q`b5j>6|DOu{bw z*$bLUVUvRHz?ZpHgvQkO@9_w7JjsZfj}+{dm-Cn_OeX%maLe^w@01l~9&wJ<2~l&K zw}GLZZUl2pnfq_nAk1lChS75#Ws1+R>u)<>JoXiJdb~fF6@)j%WF%IAGI$QW&C`Y^ zPp?c@i$&{xcg%C7fz{iDI&-TX{k$69-E>O{*_@8*YizGAp!CngJ)^G-%XM6aqMMDV zYVG--zbtkA?|$~`2>kSOaL#SbLMMI8t#3fNQZ4NYVf=J=np&cb>TVcc;~O+zvKYI$ zrc7ngQc^iJORx9PHXNa}iW&FIYy5*kw^GSYe9GQ^d*7A2Eqacye{xP4%{sxN-t)vs zDVSkoSoNL?^?rS_auQCrje=AgrZyX4bgTOa945#{NSYb1lq8VYOx@S43cxbu2irSc z|F|`5t+WLc%2I!|Xoc%c229>7>0u9O@~><;X3PfxIX*p3(UqGju<63ffWo3Hs$rtm zl$d8g*QI-(K&j;@kvze2gGdv`hkI@*4qR4?es6=1tF)EFh2XE0%5o<4WkeKos;lD# z=Xv@7cb7`x?*nf!=>sGFtL*D5DK*lz&F1k>fpZEsfWG(=8*+jzTJtRrXT1P7*d>V& zcJFNKey4?oTIgNQ9=o4=CxEjkyavcgd z&1-dl_|Pap{0yAz+b|zea5f~PHfe0&h-ySt04R$Wv*KUrYO~sd$$Zz*U@vyTHs*an zMn^E@ELBtd+i>?8CHyGTqVrJ&Nn%^zj3{d`x5x>7bCYpRBUj>q@*2I@P1}pu73k;C()Sb4Q)SaJ@v;d5_1weqR4}>Lwn%|(GmfiG7xn__{x};Dfq=j zyK!ZcYx=ToZ!33p_kW(@VM7demoBzD7k=D;bu6Y7NZ#OCL>9r-MSu_Qi)NeUVXeR< z*0vh7aBTHDRzs~`pkfE({m*zd(yhbJJilDt$?%}=-t2}=bgFqcS|{JLw&=)5$Rg{{ zKn{?hT$x@_bfnjM&Qr@5Cr z$0?yR>$?ce-+BwVk-K#V%45~3A-;oX8;#R%yL-0nqJ&uq9iZT1DK<)0FI6VIexBJ~sd6+2EXXBiu|5Ao-=kxrLz!aCu-Q*HS|Yk)XF)n3?% z{=g#x5O3YKmq;*Rp^0j8sO-v`QUpd>=53m%EZS;(m+!dYV@e~CGM`M(y=;B@gW9uuHq8_Tx+u56tH7BJ+ z_L0B)j)l%ehpbn)-pI))ZoW~8g>1UL7W#RbLAL$UK-?UtitCp9hTSCGiVtC0XR~#I z_>9e5;nz%e;etd%eCL{AFeQ1+K6uH@5+AkKV7%7L$A*NWt>-jjc(aLuPfjMcr}Xl!+sjN-eci-sW=how85*#|ku^NRiI8(}tf4Z+2d1 zC#3}z-`$2|8t`hAHR$8$R~d>}j}2%o4iRt0$`-N}TkhJUfzo zPrLyDF9K1GlJeR2eYuKE9<$Aba!U^{@5^j}m+7^~Wa#7;uj=tr^3T;@Z3@;2f-9r4 zCVNmacdijx%Jnd4&(OE;%b`u_G*+{Rj2{EU{iW_Sg(H=Kg6)>GIg=(ancqetQ>T9&nba5)mZ-|kPvsoA3n*9dLD?_q;GsNdny^1i%)jW z4<;+I26nHc86ON@kLUEqAO`gZX|>?j0$RjBg9%kSsW)NKE7lWOZwI&R%r7zWPO7^m z|Dc;ftt=9#gSm7(BI)!gTu)(PW%Xd8TN5s7#|FPCJ}q3Bn&Z33678tpW09u0P)=qmOIS?luH+|` z?Pp?VJzUp~+);Lqgl<$qlox~G#kR$4+`%zg zBZ6cqTR&jH%4^Zqr`3SRX*s-TYYzS{R5+i1%V{}IMETGMDns)rL#^4VWrdc=@Y6>c zF-PN-os#pT63zn=8_|>b(ZzufhZ7gOXUbh0tc-y-Kyaj5)RJ=SxO3r03_Z~~(^Kq^ zwmI)v)t5&@681vaeTnfp1cR%Hoy&)?JdIa|)#UtjMc=(?C$jII_rsOB^ugA9;V#Gy z(IF`Su%Mmw#n;fTJ;ZFL0#`n8G*Ze_)VZeQnir>$>t4Q^JFPwo&X(>rTk#sA8DXlX zH%cQo7ZGL}rsrLuKbUz#v}N_k>dO6odx#&t=hz#0#>I)Y|8d4#Wpwm7HkN`dL~zQl7}2Z?Fx&E=eH zP@zGMv^|OI?nPCr05Bxl2vS#(N(u`H%8JMIir@qFT{5Wkb6&t2W~rEm*`U))jqkzJ zGK4D6mV&(BEmQ|`00OS`3`B+Bl@v^4nG?pS2!rOQY#bTXnMx0}o+qafLlQ{A*7Too yRH>IHJkPy@;lc{j?~4ETru_e;Yx0#RK--#oh=w1Q+rJMMLrqy*sanB0{Qm&=`o4Dn delta 1465 zcmV;q1xEV5E5r*SiBL{Q4GJ0x0000DNk~Le0000`000152nGNE056Ks%aI`;e+6zy zL_t(|UhSN}Yg9oH$M?j-!otErAwr52DJ)W?NMVs8LJEZxDIx?6XyJeH?*0NxOBEtS z2xyTag@r|g6e&_F1hhzDk;1~lLO5q`_RJ-DJ2UUS{S~qwxc7D;;N5p;-_Cq@pQcbK z6bgkxp>QSX*?T@zW*WZLciVHZe`EUQv27>in!V>k&pfj&j%5(~j%_C;F99oa9j+VH zVbT6waB_>lCHPEHQ=n&_+E!BX5>PjN$IL>+KCta%H1afB~4b z?dYTzuo^x0)~Qp=z8RWtwp~xcD9j-OSKxDtnl%i~7r5?I$H_Vj1SZ$fNiU!rJ-cRL z4&i=G-CD4$!-0rSS^?2Gpz6cLA&B?Gwm;f?J&7ul)Dg(nvqSUFwy^!ywp^gWpeqAC zl2$-IYW~7!i<-c|9KiJ}e|2iXXCSv21tFb))oAMC6n60$L_7Q3lcF%eYMf3$>BrWK zwQ27zW?*0zZ958S1gu7B3%6?ihR-%NC!mP@L>&i_7n4*Gh|em{IM@cU{@C_er^A!1SaiD@54Dke{I+ZgHuym83e`6?8d7CRjWfC(2(Kz6y)DDPpV%s|8 zC|t4w7mRoU`kcO}f1ue=pKuewPcXz0km^&;IQRu0Zr`IGir8K1I0!tA;|M7K2zTiw zA*Vglb^wDAUO+MqR;lAall!^B$mO4%0i!TR1WG3kjv)H>Y`ZywbH-~uc!I&ban*tF z0(LVFbjT4bS3oTZDd$5BIPg1t$4GX+W?&l;Dgtp&f-?><@oiqZNoeeT&nFnd2q-_SH{r)(0_-1MWIwm} z&iEIQPR`Fzf5$`y=tVA|1cv z-C)%RN8h}H>kH~Q7@8;W-C8+A-KQ|UdM~}Ex|KvcM6+SlVH*2*21>>O{)qRs6`#TA z_=j|02~RL^`_@jn3n-6Xw_P_M=(bH;m7m=NtVVgC^roag*SS4t_w$dCmeaVKfSP|) zH~L}Gf7$(05D>fxTtj;S$vBYqTJgZ7pIx`E=oXX4l}VQfMAwBk4nq2V%|W|g+JixZ zf!heXKzjk@>8m+%5f4g~rSH@D1VcLktC8a@{huxPuG>%B&Vv9sVLJgY%i?h!T~0U$ z0m8kWl-NF8mA9sqnm*w244m>ZNT#e>^bB@6V<~&IvI=^g<6^kNK3qQG(tYNoEnddNz(|FUR6NSIWKxNIpHD%jA$@46Y#Q6-!ZzJP!)xG z0m(R!4k)H~UbN`aEb)Ciy@0xmsf-ItLbRoiDU!7<{wCKHt z1y> z^(DTgx~mY+z8P|Jgvf_2kD}#n&?|Q<7v=}WtD(nIPEN<6=wJnOXs>;vIUmj2f&T?Z z*$kz?-lk?RDI<(JgjZM;o*~Ad+j^`0u}O0-9olCe;-n8h`+wMX6fKB zX>n8+8r1sY>qkXZQZ;9Alu8^q3s<|*pCFfjUf8Nb8osQZeVwk&;<2X-n2R)71?RJm zGB&>Dw&j8uHBw!&28c!u1~u%Gsm)uD7lImWGVJ3vJ%LdkJ!Y0RoJt~*o(V4$|K{5i zq~zmJ|?iQRA(a zkz6-yMZ`t&ZKta#!7*A90p6P99@79$|6 z9@F@35-{{^1uMxr*k2fk)~UaTe4u*n&}gIlt8r_`!UhL!)x7v4gV_&XTynxuX({wW zrJ@B(JI>ksFh5>)WeBqOpUA&;?t`~r7$Bhz(?maNZpUqMz9)xd7}q^~cp`A&;F35s zip>Ds%Bp`W@F)5GmR~!q+z4Sjwc2+gbrtu=$l=(@kkp<#!t224lBgT`JBtv}pq2hT z#Wz~IqXyDca!7qCM9!LBQ_lHup9eZPIfPK&3+#A-d`5^FoK@2eI9r05nPba$J(_Tq zx-2nR;$e#rp!7eQ1;0C~ z;eDx3lMe;>{9Ddc(X4V5Ic1YdALzDU*gZoXG4KvW4+xvi?*_MBvx7*Pw*%jj_+<4C zf4t$zZaN(03~HPeS>K_Tr*!FWD;STcE7~)Iu#cQgf^O zKYzQ{qkx22MIMVeaWBk_oqOT5^Ox1s1tFT9(kp5nXU&HT4Hq~X%DN3JnN%U80k?b| zq^a?71BmYA-MJq!*&BkzLi(6eu?PQgmk{!XBIhj?On7~i7-Hg&q>cGsI`m%FxTvl0G5Lm9%1;CAr{S};Df|C6f(c)GL<3UZ9wX|qJ61R|n-RE!r!W9)gwzgeI z?`Cs~X?p3(>X16ct1jJp|1GN6UORd)wy&nTJewu9pQJ{0hOctbm^~Pn=A)6+8RtPw)ym2VsX=9!frONFsNdsS&kQ zdV=*q5|4@HXEsqop)bdKK=Apio-q*_OQ|d3zy(9&y+N3<-;stjsPdEN)?Q821A~H9 z%5nkn_ql%;9F>Bae(XoHZi0G)6cC*Rm+`3b@YgFj;B9Y-1CNR|h$eZU0mT@IcBDXA z&+GlBxPnV~xvCJYliZcCPXez zX`?(aDdyuE$E)87>8uy1EZ`=Gp9vr@OniE&fK5H*L>aOdR3SJ;EUMdQ4OdL_RX{Q*ac zaE`ASHJb9ORm!X+mgKhw75&zpPH1hPu7B?FhnTLTrSNJN>**d>L9YuDukhu9sYFe? zeMTD44&ny;W!$EQrUAJM9uP)}x)qD)%xw9*9E2Xw51oz8t2ZbW#QFgDbqH>9cYcHP zDCXpliU$8|ZC~w-b;56v)o=TYxQ!jaSzKKO47zi?G8MJ+@Qyc1ri{~BkUp?mjkkVH zVDvJ|yJ8p1Cr5b$7VC3dYFBk75mbM8x|wjuL@(5frgxa=%v z=2N_9+DB=W>rD8k`yQrUg-6v{kM|f$e~wJv=h>OeY9zEZmgN3DJF~JI@h-HJhLlA@ zJRzlauRSV{F@Qtcn1bR~m2hq6x^y`p0XG^unXx;!ek?qviY&13t(pF2%aZIBQ+xkD zGOA1WTWa$H_Lj3B)A9xMC6}AdJS7HG&r#b~FeD~{bZRlGJ_k!HqMp7+=u5fmZ-z@_ z#kf)Xg~wz8J;NHunF1&x1Dj}N2R<`@%A#31^velAE3G$eyQqc8H9srNEz%jUPiquN z3`>WFet!cVkiCK=N`lzBHBuQq1_;PC+dtAO7#OR_vnO(mRd;jq%=I|B;wC{e1QVkd zYnODi3J#A6LC$3+9~4s%oTN(Hf!<;NAS=jcAQL%lfH03Amv z#bF%K294Abfaa>#d}-ej+ZUmOfT~Y)t(#bhv|MSZ)qhYBm%=d6Y${QgVX)x$N^#h8 zj{88Tkm0OJO3W$wTlu90ceLnGUS8Pyor{ZU*$f_2P0Nyp7Hz$X#?71;&|zyuwBad^ zH_1R>?ec1`3tUw=wfa@_@+tRmjrvxYkrZYR9pr5u6lLRZIAjg=WYb;=xU}&`sE=cG zk7IlVuqazbhfWS(?YPYm(UiNN(Qt*}b0Y?(Ssgm)Ppc5N*rfe}1zN8;E%$ zCDm`9iHs_$(068&;YIh{VtysB<;?9me&~=QY*r|y)YX{%2$!FV-vSxp~kZphU%3Fny93L`SS#ffa)glFjBs%q?) zW%b#dB5fzThcX@D$4CAI(1us~lkO)>P9l`)l_%d(^}%gP>5UC=cXE;9{+QMyVjBrG zSO&tzKKjgIYyJWYpN6X=_E>4(uWr%L>%ZRY+L(L8O2^%m@^H)) zS3kz&yDnF zpp44YP9vVYW?s;>dBGz0Ga3kxe8$Cq&`n`5vOi~!dg9CtB+T3aJ|>8m=_&Lyxt~Xt z*f+7dTCGh43lMPzMbfAUZF!)_h16rU7-52qRD|9dQmtC?t1UC!1*Jy@#I}c2N=s=E z)Rm^}O7b>p%uC!InhRLx*aBo@75h(e?l*E2Hke0HA`Gy_0dV=dzYBd2Q^y4M=P?9X z4=F+eiM@3@&LIyIr7F_OM$x`HDP6-7!@@s+2JFWn@kkRjME*YT)MGugc{o&}z>giQ zu;rtdGIA&6ASUXRnhR7AU@0{wzc38^JXTC?1S8>qXQ8-4U=WJ@f2`o~1&Zq?(Coha7*36Sf^%?7KWUx6>)5zwi--f@fdWC7g^(naD z@@sOSYjH~kd*zD7xCs5_SI$LB{qs!5uSm%^Hyf&3aA8uO@mm2FOGY!6J#)pMVsM=} zHtoWQMs#Nxa?E)kQ|yjWFGLxYSNhvBTUoK7BMAo@z$YHBnk$?0Bp(Pu7+>Sqo~+y&v#AHQUBnR3t507ieXEgpNMS9)mVU zec$gkHpiejdLVOrr(whcnB+F5YDIe^ycMtQwC|D&1_qpHax>y6lP0 z_P2Ev{9acqiU4a`kXRX64f0OF=PLUWdxlO<>TNCq>LF9E2Yn|k!AD|4)KpijjAZn6 zFPmyafHP}bGqAfd9-YtkeD^2M9nkeKbLEBk4}Wz(KK-av z)clhhXF#YXKX`Z1B!vQpgLL`aH5T9mijC6r#mF14)^#bNG67}-@ZIHpC9jiD$qEgA zW4Z6WBC2}xF%m#k9Fd@6M-!>S>r&K%pj9!Xhv3M@-^uo|dCpPs0PhM|P1H;ewt^FD zUbtufuD>+VzEC;U0SfyuD;&m?GCg(*H=` zvmKeBP=ATE$}bmfenP)(LQ{OE_QE8#Jw4sQK@y61F*U>-QZJ`X0+3%vTrFsFZCV{RhL6X}#c+Zv zw5Y?F39H-a*SeRMD(7F#&UML?)NFs?m;Zt_eJu;btXEyvr6i4=6vQ@Bt0DO?Vk9d` z9NrPf6UrOalehpYGkCp2a%@tE1vB{gK~sv=j0OJOT!M`zoq+zrpUs$F5tNuDO-r1v z3t?BeDH{L+sd1~wftbNK7118SM|5kq(sw*UbJe~FkA)mo8168HKt~I#Jt-+TqCWG{ zW;OKZ`Vzga%8+cYSXd8T^qR;`M?c61#ne&TKxoX0!h5kT%UtcaxEsJGgZyc~hiP`+ zQdo67AD!CR@%4@gYy;a)F1xI<_&+|t|8@obpH3$3A3mSp)p4D88pD&#fdN%}p<1VG G8}mQ#oHS5S`erhdK?hyU`shen>jF$BlgVTpXTyZ)boGd%}JK za^v9c&-6VJNUzQJggrBmWlIo$_Lk-bLmvT|ad5y3{0EDM$0Op#I)y7ASU$ZnFr9s? zWTxFD+JMFeLoWeYtqO@Yww-BeFtis?F%Ep<=oS~2Tk@17xYSQfoM>j?w?bQv2mICU=fsXP!Ri|+q0X002J$^RqAKfNQMqEfH~r1tMAr$Xt#m_)b$iPSMo(qQOvjv|K)g`xyr+ zL5Ojsom40Jdz7Ks{lx}@g@A1O?hS4GQi2duv?p99U~l6t7_!fO`^Ebc3N>K>nI45MP#gEPjS9L5Oj=oi52h zqFoD?42E0*mFXKY`|N{OryUE>1=Ng#{U?~duPAkzCQMESDm5Ww^w$?{PdLOMd7Mlp klgVT Date: Thu, 9 Jun 2022 13:53:16 +0200 Subject: [PATCH 090/224] Fix mocks --- src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs | 7 +++++++ .../VisualTree/MockRenderInterface.cs | 5 +++++ tests/Avalonia.Benchmarks/NullRenderingPlatform.cs | 5 +++++ tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs | 7 +++++++ 4 files changed, 24 insertions(+) diff --git a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs index addc248d58..6471b87bfd 100644 --- a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs +++ b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs @@ -114,6 +114,13 @@ namespace Avalonia.Headless return new HeadlessGlyphRunStub(); } + public IGeometryImpl BuildGlyphRunGeometry(GlyphRun glyphRun, out Matrix scale) + { + scale = Matrix.Identity; + + return new HeadlessGeometryStub(new Rect(glyphRun.Size)); + } + class HeadlessGeometryStub : IGeometryImpl { public HeadlessGeometryStub(Rect bounds) diff --git a/tests/Avalonia.Base.UnitTests/VisualTree/MockRenderInterface.cs b/tests/Avalonia.Base.UnitTests/VisualTree/MockRenderInterface.cs index add8f7fd73..183177495a 100644 --- a/tests/Avalonia.Base.UnitTests/VisualTree/MockRenderInterface.cs +++ b/tests/Avalonia.Base.UnitTests/VisualTree/MockRenderInterface.cs @@ -121,6 +121,11 @@ namespace Avalonia.Base.UnitTests.VisualTree throw new NotImplementedException(); } + public IGeometryImpl BuildGlyphRunGeometry(GlyphRun glyphRun, out Matrix scale) + { + throw new NotImplementedException(); + } + class MockStreamGeometry : IStreamGeometryImpl { private MockStreamGeometryContext _impl = new MockStreamGeometryContext(); diff --git a/tests/Avalonia.Benchmarks/NullRenderingPlatform.cs b/tests/Avalonia.Benchmarks/NullRenderingPlatform.cs index 5cbb3b2c49..51e75b6611 100644 --- a/tests/Avalonia.Benchmarks/NullRenderingPlatform.cs +++ b/tests/Avalonia.Benchmarks/NullRenderingPlatform.cs @@ -117,6 +117,11 @@ namespace Avalonia.Benchmarks return new NullGlyphRun(); } + public IGeometryImpl BuildGlyphRunGeometry(GlyphRun glyphRun, out Matrix scale) + { + throw new NotImplementedException(); + } + public bool SupportsIndividualRoundRects => true; public AlphaFormat DefaultAlphaFormat => AlphaFormat.Premul; diff --git a/tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs b/tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs index 376121c269..c385e1c3eb 100644 --- a/tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs +++ b/tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs @@ -122,6 +122,13 @@ namespace Avalonia.UnitTests return Mock.Of(); } + public IGeometryImpl BuildGlyphRunGeometry(GlyphRun glyphRun, out Matrix scale) + { + scale = Matrix.Identity; + + return Mock.Of(); + } + public bool SupportsIndividualRoundRects { get; set; } public AlphaFormat DefaultAlphaFormat => AlphaFormat.Premul; From e48c984443e60664db79752bb5f058e6df2b0bf3 Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Thu, 9 Jun 2022 14:23:02 +0200 Subject: [PATCH 091/224] Update tests --- .../Media/GlyphRunTests.cs | 2 +- ...ould_Render_GlyphRun_Geometry.expected.png | Bin 5566 -> 5326 bytes ...ould_Render_GlyphRun_Geometry.expected.png | Bin 4372 -> 4228 bytes 3 files changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Avalonia.RenderTests/Media/GlyphRunTests.cs b/tests/Avalonia.RenderTests/Media/GlyphRunTests.cs index 2de2dda29b..6a8884a33a 100644 --- a/tests/Avalonia.RenderTests/Media/GlyphRunTests.cs +++ b/tests/Avalonia.RenderTests/Media/GlyphRunTests.cs @@ -54,7 +54,7 @@ namespace Avalonia.Direct2D1.RenderTests.Media public GlyphRunGeometryControl() { - var glyphTypeface = Typeface.Default.GlyphTypeface; + var glyphTypeface = new Typeface(TestFontFamily).GlyphTypeface; var glyphIndices = new[] { glyphTypeface.GetGlyph('A'), glyphTypeface.GetGlyph('B'), glyphTypeface.GetGlyph('C') }; diff --git a/tests/TestFiles/Direct2D1/Media/GlyphRun/Should_Render_GlyphRun_Geometry.expected.png b/tests/TestFiles/Direct2D1/Media/GlyphRun/Should_Render_GlyphRun_Geometry.expected.png index 40ccc9fdcddbfd3af4673109e9558f2a30f958e3..7f1e0d29a1940266121b7f610f3763d3e08e4268 100644 GIT binary patch delta 5310 zcmZWtXE2-r!wk_&lsG+V5S(6u=o}I?qL<@{9wkb&<55ra-lB^xx)bG&IGxjb3DF~Z z2%_bC-=FV%`)6loc4q(W>})6h7;lmyK;!pPRms2)dYDI%L}M5{&?P4ZCOz`s|IV1{ zNvzrS%uWiHex`VVYY?jZ)F(*;my3WnH{^A9il(QbnYDjLTOrP?^R4l`)#&aQ5;l>f zl!7?JXl74Jwu2{u^~8Zxv2x$Dv>P#)F|FMj`_;y#rs=y??77x>!DZzy|6`v^z`HB> zF8KH|5MUw*@3t1EC;MOGis71kr(Et<%CzBWn2TXLMit%?~fFC z{)pVyMg2RrR-?SZU>{(~8t!0-Wxug5^=#GG2F~T3D=p-n3HncTxyVisQ+FBAOQ@SpkCn~K6o&VDGso~}$)=Lx zyxe7G7mw>FF6&9mh6Y-7_e5=q@V|3|jNr-l@Y!!kzs7T+F5v&m@TQF+*DFo=ihvRx zznII&BSxaEI96DcKyhBQ5n9(ywIFtknJgIW+68stKmE&05;Gy&QA~tBv4o?w)tame zt;GT143}*CSnvFSeXmyH^@Xu7_qbo1wa-GlgCbLc3(lRX6n`X^em#H@8@qIyDQzB{ z>%Ut*p=_BMROu9D8K`#e>2f3DfXAGgKlQx{F&uRzpWbEu+B{vxh8MBIZz*76GbR^O zd1}_dPJj<78TbXo8yD>Mo#yjE?L1<>)64+Ycc0N$Gc>(wTXtW4Yavr=#k7VWk$%S5 zx#A>Rfj?fy5HsR*R}QbQa{6^>5oeq&eKumiO!Pg0Q>gJlpiZyKk>G=OuiXPBk8~;4 z@SlDhwBWX1TKa}?#exY-Bo|K}bQU~kr^39a>haZgk>&6OQNPHV6{{)`5xCn1yDg0a zB(r2uBy)v9LA8#f?O{d!?xR!HCFV6`zN%ZnZ9+q=TLkjzNnH&=NLeG^mS0;*Ouez22}T{3X-*1M61x70$S% z3IziZ^e?|vzk3JfMD48($~&&;`Iqg$2Q5NzQ8%6l9!ofg5(aIc_6=p=NV{GqIM(zY z!ghE_)zz5F;8G)yZ^# z-{Mb09GS|!U#@=r?cVS+{mKxfl4WSD*HCatpT;ueR$TB2nOe5+eBAorgFmk>J!1S< zh0}f9UQ@Bq+6nsY7dbrnc_lCak;CY7S(_s5(H{+W+iEsPSV!43)#S2Z`AJY+*pR?4 zsik{9I58-_lK=) zz5C^s15D&zM@UXR1u3V1I*Ex6UFPTbJ8xT}rPK_`B3-MdluL)E{bb+|<)MRVQ;a#Y zu@9z~nN&-23AS}QnY0gGeNR^Qx-hQl+X=Ymv+(md@i;A3Z1&A5vU|aaKvhy$I1T;tUd4sCI-p&0HS1(K;s%E51(k z#c87vt6Nd`69x2dN}j9X*Xo7$668u_Trz61K@r=4ZY zd^0L~`%^Nc&y6`)y3i>TmW*SO6If|Cyn@w#tSD})M8cuGu9rBmeY1bXeDB^wMl0tJ zN^14D>-c2cTNEWFtXNH^mSy^8WC|PeZLZ4vp!d#c9Ej4eZu15$EPF{DjZpmNV>8xc zmm#zqCn2HG`EKbg|vBB%C=rnxugo_;dkxE>Csa8C3fniLQ@Ib{oG!GU&wsW#?(97mPC)6jDITCv#cG7F@SA+0UWlgn9^agW6 za(kW?0n!#xFHGt$6Z-3HH~+m+!ts1kIH0AMVNW{^v1|FwtO+ZY>A5VXQN+PVmNZAP zx#LpzrkiRhM%KJ*liZ!s^3x4?!uwUHC+Z~Sr?-zp!!t!!I$@4Yf3e%H#);&mwaj#T z&zCIPV4wbY4$6Hw-wl4L;Iul7VdNPkJw3bc8W=F!^NGMce^&-_m{Z`ZRN@{iGvKJQqB-V1@TV~>Xc2DkD2EhxF zbDk&ODP8$ZrAD;wXDkx^?{9Q_+U`nSQ*W_D42 zY_Yz3$_F(DJG>e2a6Ca@-wnCph0xo{8Ow5inGd5xAlOtuNZPYj zDGSahwun2tv#crz%c4j9T(IJ>=Iu}n2x2qB=-M*3MnlLWUQ7rj4BRz9D5w(#E_6$} z^355=xDEyF=iK8kfMfs{GxdBj1@7we-Egiuj&fW~N~F+&(G20i7%6@79L8R}DZ-;7 z0KxRC8|$e~L*1egMHuj(1_i=SQCSEkg&lHu)Nr&B7_W(8({N(TKruLcjk>hR=1~~( zMd~b-X}hyGW5((N7mDE@te9Bzk|UfvpJUW+Z604v>8_@G4;j^cY4Ax$$|~b4{+C3{i)g&Mc9kW zDuIc6Q}zqVWUSM3v$^)U9{&_&j} zXg#z5|7K?N7h7ulq;Mk7&H9zf+tO5)tmFNo3kRf{3+rDqQ~>#Xd&WF%p0};Pc0>D( z->U7S=kt*2o6X1j=1z|=`Qs$;{;=l{Z^lmZZPNWS1J{4R0Ih7nfBO1cs^PbEorU6c zSrt)DphwaAr1&ut*Z>w__4K*P^swtZf$V9Gwx3Nfy-KYE!h&N;Cx3@zw3gdBwZN`T z(B0G}DVK;yQ@^jVSY)BVgZ)8GD>gjGS>`1t zF=U=y8{PL0;73y6&!s~4NRo52OjtK#S@i|x+bZoyCW+fEt6+5DQ7&Y%##f(-bToq! zTlppPb;b5s8zq}=Z{;;aDQvx5@j@S>E*A{Ob6XR+moA<+JUeu+#P-S$2!;G|U zL<<&dv~$18Q(JD!8YpD>FnO#SRC_XQh?zI>tTEjH#E6ts%VWv-e5EnX=8~*0rrdtF zfF94_(WHE#E#ZGFOPBPqf8qmB?~{oI^F<`p*PHN$*WytcjLBp1R=b=z61JLqri46S z6Y@^**3Cs+dicJ)pm^68g9h*UAX>)n9-@l=E{0X-rqzaNyi5o@Gx%?(8PhjZ$EdOS(44Ac4o%7Mw}eaw8V@#cO%Jn?v@V~X;`7$ z0i|~RSnkP=el>@+QJbSTtcHfjFp#9{1F;`tuJfcUJgu&OYbkKbgym_c&_utw66<1m)weSLKhXu|0bWei&7ky7$%*fgV@lus-b;T z=AwEh==C@7`!Df|55>TbXByr(t-5cbE-um2+Yic&)L=mvzmM*0+}>}=V+OM7fc0ZB zv%}9xG9;;{F|Pac1<2DYvVfgx!Ni1nc3Qq0o$IVnAb0TJ{mNdzU9k6~fzo90kyrM9 zF+7Z0U8nvc3f;3Ubf7_^Fb1K*FX6c{)y+Mo92r&FTnIWp9U(;hBO4d^@=Dd7n&Hbo z&;8%pKqRoVftkyg8EChsmb70oVCPu*$<(au4OhSv<9B{>TvDl;Sq9Uq6(KZNsId=a zJhf@wd%3Vb%x2O@X^t)#bBsoE*;p%wC%RtSA6#@e6VkO(-RE$p;@8FNz!3YUKmfPyJ0k=Umx z3s@!ck^r7EkS_Fv^;IGhmz}*pkCDjLn{r~~4$u6!_>fxK?1e}dSZUknqEmYO~9^n&Pik0swsMQ>`FyuOM6xQb0fhqTj53u1)Jf#v6poI*_v z)kBrVs#AsZ+n53R&=eD3%M42jL(K3smAgQKaRn4=i?2BGv3ABgs zWUw|k?>cJiQWnKi>|if}4)=$+5`YJM=O!j^zbQ1!i>wnMi2!Y|rptYF$fp`7A_PiT zyZDycojs)?!~!!xAdlLAfb{cn5w6=YWNmt{0Y-inqeeO^EVkh06qGc{vv@`w7w?M- zD)5J!PR6Z$AKiCKq_I{KpHn`+Gxy-D2~CMq?8hyh*K%e>QPIK8Gg5@qoa`4N3r<(h zBxf5|8w$oRfw+~@h{(mSYdzF8BrT8FRaqQ_$F}GH%5>Advv_Fhp`IVef{|`CI`bpnSzyNa@12Y&Gv^5@0ePJopHN?mrxf;hXa3hjXM8c= zOuu%!P~l;yBGj%bmGAm#=P{yN56-SQILe-@%AJf_XP;TZ^`gGFlQ?e3 zKPBG-`Wy;Jyq(v@jyAlGZz!KT4K$GaGw-R6xs*xvca^Oh5O2&<8@eQy9lhJ%TASkL z#A*9D%;SINkit35T4}=VR14WP-nN3<3LOO_PW`8NYv0q2o}K!dy`R0o7*qwevW|>O z)1Ku?-1vaU_MRbLoKD}o!}I+O4evxq2+RKhY_SK$oK3NjI1)5Vd%sPvj1?ta88^;9 z_0M`cG24E5_d$!q8VK%;xtUqihetA05QER4nk(}reYVc+R^fRJyHpsjdu*V{S${f= z_C$u`EViGup(NEYFEGhIdx7>O%jDXL`8(=QO^E;-29-kbJ5W z8I($k6Dy@L!1Z}g}pn&$b8s>@>&E7JEIW8xabo)mE5$%m* zBo38vqe~Jr==eHYCn!M_w<%BqEBV49+UCsvJG8A zEjJpME;>!tom4E^R5B{G^LKnDVcY7Og=P3g+=@r1`1D8D`h{9RpYyrA>0?ZDOB176 zYDNg&WOZt&&Ps@`=%?d6{$6$EzbR?X1-mrO*&sS1bMRJ6^2I&s!9lWlJ>a=22_El| zE*;^G18SHGm>x&Ur7~{&_jehx3=?Z{iiFv4?ps?SsThaA`G#`Xlv>(M%G`dYInuTm zVP-3s+$!1p*Ti+BIfbd~FJAHsu^mhpB8k3D=6#osZ5+&rfH3X9NWT#z%_^dF)(P3kxZ_!tzl`x|F~N3!)3rUW{&#M6_rjI?+3$GrEWp5{zD= z_s%(A&s}$a*=zrSXYKWjakuk+ln0(Vsw*oP_*w2{k$yDj%R+Y?jPC9iziIg@+8qj_ zG=M>taP4BqW+}%CdT}(Qb9yHMi+Z+%+xc#wZqBt5D~3mQpdxPX7Xwa<{%QMmKo6{ZAAlBM;ch4R zr{fE{c-8L{|5F6qk=~+TKMNCRPh1?v4-zP={(+QILZaM#>v3y`!h|wSlU?(N@K)x1 zLmog6!yVu~=|$I`Ky=u`^Qbv$bWA{#Ty~p#=%tSQA9o!e!Ms*woO~!bId{h(32_EJ z8vc7_iWa!HW~ZJ4M@#4UTOMU{qp4g^;6Ix^l`ox`{CS`kMV z8d&E>-`Y$Ug&hJpncQG&Eba5hyMlt|-*@nJ7y$FQJ&WVI&nFPq^brT=q1!f{gU{na z-uge!A#D}JC}(5X5%U$eeN(#biAHA{dL)3hk#6(i^j}(aqG^zfTSsIB$=yD=N@nHK z@H`Xh2{`$wxa1qW1SKRGaB7m7N7NvoE7g(oN4*1U%sIkz{DzpW#Q>LCH+slEe2_02 z074+D?l-tHW%@Q3mm2bm-OX@XkMFjxLiZFuCOk%hMa*U5Qc`w~dOx>%_!>Mv#O?V> zCB?`ew-UYUaOf)QZ1?XaYoIiopb#MgZTw0biq?Cm=8P`DpVd zWawIV%3jsyC{|}~8?uw)5@PTNf{7^sb{ichN>MF~xKkx61`Z1C0xF6I5gs|Ebv!~B zYy>;ajN)N@2!l!=pQJ>xuuJ*ayKXkC5~}-7@L=Iv^>AATOZs4}CmsFWt}XM)kKc1n zzGrNFM;zUM#^XeD5gQ*|Ez|uVJ-iyf0ua-XP>bJxBXNMvK{z-6D0S@(#T}Ue-`lhX z-6!MhKw1V95gCiA$8nyq1loY|ty)uFM@|NJ1GzL5vp@dB*?qy_C=k0#Wmd0e6VrJ? zyLPB$noT1GrU90kGcbLoQfurM9+I-9w+x&}Au`%wXYG~xf8eO<_6torpA0OA84g{~ z7(txy_><+p*EyCDa+Eo%Gncs7~C z-~RBAv3rJ&O>WAkoRCY!zF4O0dj76oOOLYROI~s8ws4rgFu+qB)VKx8gK+m9&13#n z+6t3y2$V9w9_f(~r4|^kKvni6O1sM9h||W8=9DjLiFS2jhq{?B4{w9S9ey-+GecVV z49e?Y$tEcVh#fSBy|KhDYbPV)c#k@Lj?EebE!%te)I|)Q;2PQ(lHVdngpsZ`WY7xg zmn88%*0}0WSij`eoLm7Qpa(-Vl{pWB@N>GiDX#yRYtM&77quUp8%$&1J-#rSNtvWj zHT|1;eNWH_dQ2%CF;;>%I~cvde@B}e^2`@ynh0AK>;L=0S2X68Xj>7UHmiw}j4reH zgdWWI5TL~L7E3L5;5{-tM0!&&<&IM5V3|VxE6mZ#5{%I(haUwj9b9l+4 z2i{ble$cLk_YCr{&8U_mw?sI00B;eaGX8y`|G{E5_CS)_J2*cdi-nkS+xEwOi6R3S zAy+>GlhRZVNSe~>L2U3{~Jo$sN;VGh}+S#&gW85;04ZjOqx9K^uOQs z=seFaO6(Sj7QGp_gYcG1m?g5jU+8?%8>cpiW6Rhs0i=wj8El!P;kS0g&4|&4zp4dq zC){0?t0ZWO#?@&yZoMjKO~TE9`wC2!NMSkad~{BD!A?m=NnmJypLc#qA=1;{4i+`2 zH8;bKdTjp(lrGKgL~8$lE4&qg@`DOEC5OuEQ-H`LV_eoGik05C(V);%)HF6wR*UKN zfIzVsU>b}I{jA-4u;ycc2;H$lHyQq7^wTgzjC_;)QXlX!J#fWq zf5lT7%20);N5?K~Nk~C9^W0FY_JeR5d9K;S(X_#qB6mzkRnW4@kO*@PVJJa{Xk1qP zGTN(*QXZ6&--W=9&5Ih^4}5M6Ok@v7ajnDw;MN3GIP&JK6;h#ur_>cfG;jkJ{u19N zNjnk}w8h3S_UD}hbtRF$V_#bI2lM#y+Rk(~lqn>9&Tp~mJo;FmanRaG%j81S%@OPl zsV)0@qJ+Ca2R9*_bO(2PG>)r<^YD2B>jv^9Ug{9I z%NrZz!exI|ojJ`b`#GLpKqZN1Aivm$(X#O-P3pf^u!tc|UCKA>$igwE(aB0~eX%ri0w!#}&k$r!D9`IQA(hrAp4f#ocU=!_sJbEy zEd-<&?=I41$TKN)xNO*9TJ8EJ0*2fFx2nyF{N9HE?kTv+Q^x*->`D7Y9p3zsdZAsH zsDi%;izKLfHXzwv9%qedj0`x}>lPPcMXv1=GK_v``0cmrvN@M?!p?k--`h$Qk$d3F8XSWj}TI8+_~SkM57^E`K#i( ztc+(o+JZwfwY%O7Cbo*#dDP1Rv6iB){kE}+zXZ?E&Q8G2e{GGM{x&2PD>xg&6@H|4 zGe_sXk(eEFkmW<=<^x+;-4u?JtP@>E1N*m+l*ESR$MR)Et+}Q|-jsKOd9YO*NE)gc zDuWmrr({Dj;(m?NDJXL18*Jmb2xxF~wS+*J4dIN=`d(uvQPU{cZEvw`7-ek3sW!Lu z4-upC4?ZH#gDzFe&D>eD4_!3g(Upb@;Pm3(=5ukz@-Q8&z5t9SB3HTiG!=d$5Odaw zu6D0WMw*qxbbE`b6cxvRAbZfJWY03AIHB$*BcIcM8T>^+^GSzAP|fbzKIc8jPr`>;;}YD|Y>MI}KbMKp%ef&A6>t=%#MCb@!^2 ztEXI`j2~`}lZ$lF9krt?{C(yN79&@eRLQK`%Bsg^sWl!thC@_V(W8Djb$`*ACTh8{ z51HGqZ`-ps#ZFQ756(%$8AsSOJD#{nc~eY`%ia?q-Y<@pj>70S(Xa}`mDlyg8c}l%8on+K+NHC$`g6P2* zh`+kNo{S7CJ!x`7Le44CuhAV%2Q&hWMD{D}{8gVVD^T~CP8 zMD()r)()!Y#=NuYay&3cJRGr=mJt-Xlt|ha8X$>*VZyj6B>C4t{(hlpAgwBKr1yY& zNKFtdhabJ`pLz{{KHXUqOeHuMxceR9p~KIH?qNI-MFT${QirN}14k6@K=LFxGj)u3tv`Bm*%82Y^`6Q|MKa*q zhq2K-{<^(m+agAk5#I_4E|g}cV)IgC#_v&1nM|;fwySXDi*V0>mH0=*LrJt|Y`1L7 zsqrLj&Gs(t)HcHD97$9yvU9AXkZ%o=0IAuFI5F&bq=DkBn|Bfjdo8rk^$z7dnXkYL z3nA02Y0neh5!>5B#Q-BK=1|Y0WtMd;4+D%PLwt%nJ>tB;8f5c@t3_iETj|h7&uGD2 z$w>iUNl0ioT4jPh_9-}~rfebduHv+XqrSiLsF>J3;!pRX@Tu5<^%D0>d0D0Pm#Q(a zb+;G7zmC($H=h|un1j{u+_GPCn1osJqfD!9HqKBVaahXy8UXq;mwpV)ccvbJP*%X| zMixyi@Y8tp#c4mguZ=I*ctSgZKb;`-5Xp=6Lrxz>!8ob+)Fgn;!_d-syM}t(Ui!Y4 zSW;2JLXxf(OD-g>F$98@H_w13^9#hh3fxL1E#tzSCU%G4ar%YtJ=p=#ju47mx)neL z8{k8PmP&K0fSSV76WxX?8Hz2B!ZzGBh)y}2jf)Pk_`~cqmrV4JqD~(FU&v;XCNXzB zBvrzYrE+5xwYRyvLYv%!{-GjmTfb;q%5m*4#g|(zG80n*3vaH&unhP#OKSCT^?w+O zT95QbL)CU(CbsW z9wS0aYS>#l4rIUoQLT#&yOYVbf-`^4&VL0%!^DMCext&!+^8$1>JIK+n_6hy3wsGy51PsY zPgu?1Ogx1)lBnB_$)mv+A7U#npA{yqouH-qYiM-ww$umJ&Bx*=o!pmoJdyi6gzy5~ zvOn_S72La{Chj2-K5XCjO#kFmhOCtXFqQcrWT9;#J5O-5_K*;{>c&q5xcpMA>0vqG zeo`Jk%9>N49UaE+-*{Y#8(uo_j@rNzQ1%^hbM=q$uj5_DPf21ciV|tA9-Ztwko5tcW5WfQe9KIy@ ztwO;+NSsRtusw~J2G!;LUx~eW*+OjJHS32bd+vjy_tag81Ex!w2V%uI>r1R)TsukF zO$9G}keGgHPci4pqDwy9I__JA3Z9gj3?zH1+jQ9rm{z!{y52CY&`fx!X{erenSNjT z74e4EJ)0}f+sy$1V_s=}FY20oN$h%@?DRM6U@rih#(Yrm1WWcE0bk3zBOCVq z7=`-r{Hl%nW|H4nFqVCm+SwgbC!9Pp8QPrYG)@@O4B;GT+sTy;eG5GH5foW4=sznd zbKjYQ1@%tyayqZ-2 z6c%L!t1e3>Lxh3l0Ey@>Q39~OOB#)S)>B07G&Rd0JA87X?k#jumPqySLXh{Hxr#te zP{4(rftbjfqP$6LbE0Tf5yjU%H5bMe0Rlg}w6uy``4HN!_-HJXJn&r|PUq=@3= po5KI~ng8G7{y)xh;*w(N0o1(W4ruyeyFL1E)KzqpD-^B6{sVGw$pioZ diff --git a/tests/TestFiles/Skia/Media/GlyphRun/Should_Render_GlyphRun_Geometry.expected.png b/tests/TestFiles/Skia/Media/GlyphRun/Should_Render_GlyphRun_Geometry.expected.png index 5bd64a6ac79e49c34a25802415074442dd911e9b..a8f3aa927782575db62b7a370e9656a504898c29 100644 GIT binary patch delta 4215 zcmZvfXEfUn`^Hh!s8xH%Xl<%i{fs8WuePE_)T%93QM=7IcF>|#dvBVcc9l@IVvh=n zQWa6uNYmEl>GSgWpa1>lKKIM(oX>Uc>v}C3Cz7rVXc#~t55fy}3j=)J@F5?7&G8%u z!yB|Bq|!GZm#j?~=)b|XuV1u$X}?~#@TZU2;uEd@f{5&zncXAw?xxD>(ts zPTPq}b<+=ad$$4{YZ+)!T<50$trLPxy>jA4i{13i7JxuAK{wDf)b z@=I-)VPvWH6NRi$Z^u@}pcUD8IXxM^(~_3mLbTJU&|QhE%enoU)G<39E#Qyy63%jA zNF(%Dny7akelVLBj#3qfG@DSix@~QsB@<}qk`^o=BhKcCx5sKP zjG4rIVnMz%LU0jVj*nMsq`X;pF8C_Sk&-9&n}@l40dtIJdRq|}%V|@`@(8&zfy>!; zrSjBvkb_sGUOUILJuUyk4A#`N;AgE9A;C;d2x>Bj(R;Us#RRN+5X|F$07JA)b#UkY z0|d-@7r`tDkSV$RKQVtQD}l6qB2C0x8}?%)R5d)!OyQSOy`V+x*mZsB#VZMtAg1O& zwrNg<`?LOhYD{KiHv>77WJHTE4sMFV99nZYTq2#AR@d9$4X&wty2_3JB#O~l&`wne zuGDVK=D>Ela2bpY`BkA*kRqFNp+p#vgf-y$9!blD7U1hRdW+ym{)^J4apzi$HHBs@ zlI{p?>EY4JD!JG)`s#P*t_DGaW-;al6NWL4(?i1Wj&nF)zZbU}?lpx+?Q2(sf$nfO zM*49d3~P&wo&D7j8BK_j?_<&*5Cw%hr5CL*=#`lA$j*L zV*`F(dMtI!@xjbBd$4bp4ai1|*Tih3Dwor=GS@veR0;&|Xs>w7yC(PRc6Hz*I=+fW z$_<)jGx$|kS3{1-!s$EZQ`4jKaw6#Nx^-P=7ZWs>E^GS4ii}>n_PANS#pmV+aGT}s z)7zOITwd)Wl7ho;UC~+-mq?&*Lwra`k5(GQJq!zPa5M8`E9Tqje~C3sYIzn)qj4qH9#iLCY=%m2-ia|h+<$D-xS#Zb@``Gs zX2v0WCv@eDC3vsQ!n|dDFvmDIH{_L4Y3+-${8fUP9M(AW>U@=6)CYiqh9Ih5erUM< zg?zp+c?S1c1ALOlWLr_q*ZWlbhMJdoIFwr#s?A-UseVRhMz}8@N#~vBq%L5NZ0pQj z|Ap!;X>+Q(OvA}khY$Qt1;S`=f?n!L=fv9YNrJ*irURnMA<|YgG|F11?O?%9-7cl$kK=^8YKWa*N zwruseZPLIQMD>&M>Wf8Px%FgURi;y|D~cnU1B|7tb#|J&o1NS<DY9Xd^Dgoo&nX- zCE=1nY$a|0Djhs+FQYZ3%{$c~7-d8vPLKv%;DEzO?(DsnlwPc#h@fx>e@#I`(3UQu za@~CDPT>}k1{&1gc9xlMS0DMz9r2^;vU<0*4LeWcNkep8o7VRKs%5!ltX10pnPrWt z{7#fO_O@Z{Il|y}3LhH$SJjta8m9e2ew$)lfbQti+Nl63YJTnp8s__Y|+2 zX8{F7Qg)W``bPKi;?)k#X@2y1dLHsDCZ9}vt%%LD>EtkBdV<8KCO|1A3rpUSBt4_> zIe0>q60D*IGPWY~BXwraC?SD#i0}Th_HO;kfWpDZc3;7cp;*^&BV1Z3k7CBYf*;>K z&a{#g_3r+;d`~BRQ-IDUWdL8%rObsYU|d$N&ZV1|tM}Bee#1AzI#H+PBw}K`XHaZF zoOpY@LKM_PAU^GLc9ep17I=v=JO1E#H}91PbKMxXbLk%Ayw|_1^i-_G+Z0AXzbXKY z7H?WPu9v!41Y4XU2a`lN*+&PX-1(#2MSTKSN|*Rhvb$lfduUxRK8At7IEH<7OD2{5r3hA zLlq6AJG+;uGUcP(^QNx7NZs{i(fKldsGA>}p;JZ`^s-=p`1Jty%ZWXZ5T2BAqm!mB z)ELJ)uyOM_w#2eh?>Z+d|33cKRypAp%9Z^k2vWVc#XH(e49yjgNCcoX&<~=1V+X9_ zwj%p1Q9=f;!UwR`Ln0!>)yh&Zi)YmzGYnFs?9i8|AZ2VTXpMc|7)pD-k$|2)YgKGc zrN+F#@D?f?ypLey@|%!;A?ij#)dUYD@vn(U8pb-VrtPi|n6k5Wi4-UcFm+W$v^2D_ zNuuL9A0yKkjE7Z>0SoZ+6`3@zE%q}rc%r{Gy$n+^Mw^`Jp`|8CV*wf&}IkZXgYmg58iSIvCbmCgD9stkBTaOkE+~9>Ij_Jn3)2j?J zs(zfRMsx9+`LEUO`bM7h7-{QRKT{q28M$|FlIo|nl69j^RTJDh@VOdVbtOm8W6(6^ zzh7VxPfrQ%tWn0B=9b?WeKF5N@Bj52zjQpA^cng?H`a}jsCsf@GMkWf5I)ld3zj7o z5rAVoz4xrl6PJv@99b8hh+CYEA!p#I?j)q)(Y!_aFJS1(v1*VCw1dH;F9oYQN&KNj;8E!d)Lfi)RczOFK;t zI|PZ#QPa2ZK2Q&n!OyKarG8Sw^SMbgbbmVzq^Y+jNBZwOvSXzEZWx(SiJ}zuGp~K_ zf{S<=Fx|+wq1r4|q5LG;6x9qgkRFR{nJPSXBKdc-XfX^6#rd@(x*aHH=B~9p!P0ij zF7x~{+4V^9b!@#enD3siM|4lIz^^0A$XlGGIL2km2L#CIdWWmoJe8=xtrdNSopNW# z)~Ua4+Kn&U2`T#=teJNLscJv=t(`EQDr5}T-0DqnuU{0mUMcT=#0|)*pR8c8(y;ewA_QUxoJ^MTa#*3#0lEfi3lpDo zd91Cu$;(YU`f`L_@9|w@@8}w_ugp{w&=&mS#v8X8XG9itCA8&{;XJzCEdcI$Y2)c7J z;3;M!!A}8o3km`0z<>DXQMsX2{NFJ9r2d~`{=YT-|GBD?F0cBm$9G5-M{#w%(7 delta 4361 zcmZvgRaDdsw8iPJp(Ta^>4u@Z8|h{wq>=9Y(bI-3pET!9;>BX9FLv8B4J5YMq0dUFIXfGv-rv04+ zOc5Q?>)2??Py1%ze=TRZOy!W?reEI7paZDE)-1_Xx2PHK!bvHhreah2RFeOvi*xA5e^U zkA<}zm$E3}lk`mKPk|jG%{TqOZXQynE7hWRE4o2JlaIK!ly;|#TeQ$W>#dK`Q>vgZ z6Ls0FGpc9cm(Jw%_i;>y{_5&0&wGSEorsr`0CFl9K?_n=rda~v-%V9A`lN&Deoqy$ z4$z5>HAn9}FF_D<)8MU^kzO}!MJE9K+ZpQ0E*P!w@y=cn3MaD-L&70XTN50=?3}cW zFDys|gx0IShrXwN>eOhb^0RSk$I1@J z#in`jdnQXDzJ%0-v+`2J`${D%)^?n;`C$RPoXT)y?_bfs>pTZ0|HbSs-7a4Yyb<=s|bJDvOpQ6i1{H)0J{uSZ}welk3* z=Z@$)WV$5gM&ZsXTr6~@e^2SPj{c~T47EJcP#T`Q=FpUTe%$Ac4oe9qQt*X1Um%|l zVTNVbbQ7E{!7MDX6}leHxJuoZ7_D(UfFU|p+4hhmX-SnR^%Tf!76N6kO#c~hda#m> z^s#3VLc4o>oiPE){H0w8yptZ@m;N~UfWQ~na-)uAlc&rrn^b;Jzx~YN3F?TEZzy&^ z#C(1?tnHcuOvbVu@`ltuyLb4*4R21<;V4&Vmf3L7`?{oG>p1$Adv$Dd= z3?W`oLZ;D<#4-ws*p^Yg*lKz}?t?UEaDu@WVfrk1^DCQh@gdpS@mA<^fi$V)#p)An zw+iDx^~}A6c63y{C!ZC{N5cbxDXblkG@KAiJXH9gVsCg^O=eUZb13$1h>D32SD&5z z*+to2icRR(5PLywEA>1;YpeUeAcxkY;G|h4UaL6?U(C#%dy(|>=hZX-B3!FeW<}lm ztod-E;Q~ifMZaMsi#mKX_?EweEG;o%0N$OlJNI2SXG5r1*bq}X{@_2J5+c3`13*C#bB6jO2eb!lucP+U|PY?0(l4YcV1)Xe>*V z8P+ZlOyGzjo0H8~_udY(qx>b50)I99lnJJRmqu!++*;-DPCiu3Nuj}nD5QQk&ZxJhQr8FJX zVqHs*C3wXnY~=9i>)$9PCGV|mkI}o?++x~Z`m#EtUh%4X_uhYts`l5;-c0SQY3@&E zNgO9>P+fV}g_%5vwLdq7HJZ&183Z(`2|U=f_!RQK_#e^u4=EZ6Hm@&bL6UGA9>%)5~2rKj_OANg3euUb?Z z;2!!@miPrO>+)cle)_Y*m70f|$U&-pIA3|gIpy!Z4?*cNQFIwecN^je0jhina=BLQ zoU7kqD;%6e9qRcgh2$a0ylLh})KcjQ)_W;DX4W4$#0`Z30Ovb`u=5u^W1_Ow(pMxQ z3&y5>Kx@Q5oQabuMAiL9hAonz6j437*GW+Nc0}75CvPjj~g^6(W*EmiYOVtqx#F`551L z6Y;Xb2WuMQdLC8Ol`HAtzEAy4R5#<>CUx&E20Wbn^cSTD?CY7yJ~+SDqSOz^SHA>Rv8->7B8RI--CJ`*i?2Y!S7JYiE)%ZNf14+Nt^-HJzdX0?1; z4n+?bM$E?N*Bg}zVZC<=a0+Yka(j*RF6QEtj)nYaZC~w-cg1g!Gi>_5)qq);X3Eyx@=tR~vNBs6&*<5#hZ<)5pv0gLxPE_JB7k_@dsJl#w> zWM&ZV#RbUTjB4}V-<2-}VhG$|Euk5(Y0Q0A; zTBSojT?y!945sZDb>MlHXN7r1dgJx!je^OM8L)_NuOS0+SI}fBFnhOV8smpxLHTCK zM><6#Q&k0yWbU!*ZXVvb9%m2SSK66E$+3&IOL{tp!($?_TbbE=rBpZ`rz@76I@Xrza6uzwal2(J1_ z-@1vFOvjy$TK%gH=2jdASxhDCGY%qttrSN-<-8AZ4Ij>)q{5t1xK&tM@Is3X<>yDf z+qt-ymdoTd*Rn2oXwfyOXxz+wraf${gf>3K@gp7Rt6g60b$3w{NvnR*ynM=YT%)lS zX(El;Ll1qEuZ^;EIvlbEw0+ohSAs9?{NNhn7~SI-UkF%LtYaf4hp%=#=ZI;`z0PR4 z!|{3G1Ji6y9So;caC?de+lywxmOnqfT8R(Dy_S~lx6DGu6jd0yvCHzIdu}nmP}Fke zbsax+NE5ZZy$?)n5*~R8VD2PzI;f3L( z*9o($C{;$~$v0GeSlg?N#s(KJ3en>JxYi>QJ4rM|7Rt^s`ow8#{sIf1mb)YVSW|II zVsZW5ZhAEjoAo--t6%iv`i~!nF6Q2_@^N>if(vG@A%C_t+!yycA^ z7uJZ^VS))YQWN=UO1J7HuC~nZAWDynNbC=(m6y^VXe!M)lojmMS(bP@v=*?=u?5M; zD)#^3J#geGaxkB;M1;T|hk!fC>utn4mH7yUMD9BoB zN^xNv@@cG?#so&n>5`4&8Jc1YT{JKbHr?~bGb%rMx*WAz?1w*LPLsQ-`#PjZ^(m7M zXq=)DAkoTKK=qmGZ)CDN)6mN0YTQP>sd|A47;%3L>$m=r65>(ZlF3oIVl^(xaQTI6 zk;?Eqi|Gqe>h;Zr+Lnt58SnV5AgeW#1?!%r(hqSLy##jM!steHXBl$LZ6Hhhj>#Zg z1(jd=%Q{Cz3DNNiM?08bB2g_*F83cw;6yO~#<4v)q0PweVULUc86(vywdXvBQs^?E z)Bv`Rm^k}TWhhE+iZS*&m@l|o>!JKgQamwF0q=Gjo8!=&J0o=x$lY1_P2E*eyuANMMJc#NNr4P z2Kgpj<|_M=dxlO<>g_Ir>!DK~2Yn|kVMpRaG}KpYOymr8&zovQK{IPxGce$;j92f| zJ^%g5Q|F9+Sz_EXng`F&@@lCKnWAbx{X7K`fy1BOkN=V@HI2aJ#u-RkO8~OFXqHNe z!%4P$?h#Mm3XYG_3c$!8uhw@jp*92M5a4^s|4dn@n35A7{K|UYdqrIJ`U5~fT^yaH z>Oc#q^0^oF!0A+t8K5|FiNKv)AG^;SH81E6!Dgmzey|mmT=UE;=P%EnWgAz=r{G$O zjZn}}p3sqqLVS`dV_}JZ|Fye56E56iZ$P$}?2J?8TS)sK8G5#XB*pqmq)kEj&?R(* z$^~}f?oH_ZGmumm@wT;m&obA3bILq{iL~ygq9i^MuD_TBkUMV(Op(Wa*WqPWD}!hp zysdj=|0|2a!pdNW2f&SniDP^L#I(!akklQ9nP*6uwEo63WUL84xkH^N%c!DdusK=BAtJ~<;xtEbH z7g){7bI+I3YJU(=_>47uEeFD^S6kPoB8#6C!ZuT{A$<>wn8-Lr0RK59Lt3DH{NRXmG12 zK$u}T6|vr7NAzpAGIzYfbJYO{kA<8z7+x^AU`GqA{ne|m==!WjyVZyv>q`v!szY+U z;*mY{v1@?nO-Dc2AI02J+dyQ>hQfQcEyq&ryto_8E{pu(xQA(R-cnd~JRh6Z*zx6# v8Da<9PAR*rvid(g!T)Xz{+~-W;V(YF(A9CBL^>nJztL8IrdFqdWgquH-9bKv From 49ee55fca4a0ae93862cf67f296454579739fd0a Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 9 Jun 2022 17:04:36 +0200 Subject: [PATCH 092/224] Make GridSplitter scaling aware. When comparing sizes in `GridSplitter.MoveSplitter`, account for `UseLayoutRounding` and DPI scaling by using an epsilon that is the size of a device pixel. --- src/Avalonia.Controls/GridSplitter.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/GridSplitter.cs b/src/Avalonia.Controls/GridSplitter.cs index 216e43e1f0..00ab856c31 100644 --- a/src/Avalonia.Controls/GridSplitter.cs +++ b/src/Avalonia.Controls/GridSplitter.cs @@ -60,6 +60,7 @@ namespace Avalonia.Controls private static readonly Cursor s_rowSplitterCursor = new Cursor(StandardCursorType.SizeNorthSouth); private ResizeData? _resizeData; + private double _scaling = 1; /// /// Indicates whether the Splitter resizes the Columns, Rows, or Both. @@ -348,6 +349,12 @@ namespace Avalonia.Controls } } + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) + { + base.OnAttachedToVisualTree(e); + _scaling = e.Root.RenderScaling; + } + protected override void OnPointerEnter(PointerEventArgs e) { base.OnPointerEnter(e); @@ -630,13 +637,17 @@ namespace Avalonia.Controls { double actualLength1 = GetActualLength(definition1); double actualLength2 = GetActualLength(definition2); + double pixelLength = 1 / _scaling; + double epsilon = pixelLength + LayoutHelper.LayoutEpsilon; // When splitting, Check to see if the total pixels spanned by the definitions - // is the same as before starting resize. If not cancel the drag. + // is the same as before starting resize. If not cancel the drag. We need to account for + // layout rounding here, so ignore differences of less than a device pixel to avoid problems + // that WPF has, such as https://stackoverflow.com/questions/28464843. if (_resizeData.SplitBehavior == SplitBehavior.Split && !MathUtilities.AreClose( actualLength1 + actualLength2, - _resizeData.OriginalDefinition1ActualLength + _resizeData.OriginalDefinition2ActualLength, LayoutHelper.LayoutEpsilon)) + _resizeData.OriginalDefinition1ActualLength + _resizeData.OriginalDefinition2ActualLength, epsilon)) { CancelResize(); From 553c4bc114470d81b6ac91584f1787e56f2c7c53 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 9 Jun 2022 18:45:37 +0200 Subject: [PATCH 093/224] Read scaling when starting a drag operation. Previous implementation didn't update the scaling when moving between monitors. --- src/Avalonia.Controls/GridSplitter.cs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Avalonia.Controls/GridSplitter.cs b/src/Avalonia.Controls/GridSplitter.cs index 00ab856c31..784d33ed58 100644 --- a/src/Avalonia.Controls/GridSplitter.cs +++ b/src/Avalonia.Controls/GridSplitter.cs @@ -60,7 +60,6 @@ namespace Avalonia.Controls private static readonly Cursor s_rowSplitterCursor = new Cursor(StandardCursorType.SizeNorthSouth); private ResizeData? _resizeData; - private double _scaling = 1; /// /// Indicates whether the Splitter resizes the Columns, Rows, or Both. @@ -222,7 +221,8 @@ namespace Avalonia.Controls ShowsPreview = showsPreview, ResizeDirection = resizeDirection, SplitterLength = Math.Min(Bounds.Width, Bounds.Height), - ResizeBehavior = GetEffectiveResizeBehavior(resizeDirection) + ResizeBehavior = GetEffectiveResizeBehavior(resizeDirection), + Scaling = (VisualRoot as ILayoutRoot)?.LayoutScaling ?? 1, }; // Store the rows and columns to resize on drag events. @@ -349,12 +349,6 @@ namespace Avalonia.Controls } } - protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) - { - base.OnAttachedToVisualTree(e); - _scaling = e.Root.RenderScaling; - } - protected override void OnPointerEnter(PointerEventArgs e) { base.OnPointerEnter(e); @@ -637,7 +631,7 @@ namespace Avalonia.Controls { double actualLength1 = GetActualLength(definition1); double actualLength2 = GetActualLength(definition2); - double pixelLength = 1 / _scaling; + double pixelLength = 1 / _resizeData.Scaling; double epsilon = pixelLength + LayoutHelper.LayoutEpsilon; // When splitting, Check to see if the total pixels spanned by the definitions @@ -809,6 +803,9 @@ namespace Avalonia.Controls // The minimum of Width/Height of Splitter. Used to ensure splitter // isn't hidden by resizing a row/column smaller than the splitter. public double SplitterLength; + + // The current layout scaling factor. + public double Scaling; } } From de3720ce77d181cab10be89f2f022c335b666dff Mon Sep 17 00:00:00 2001 From: ahmedmohammedfawzy <42243982+ahmedmohammedfawzy@users.noreply.github.com> Date: Thu, 9 Jun 2022 20:53:26 +0200 Subject: [PATCH 094/224] Update TextBox.cs Added the option to determine whether to ignore changes while the user is inputting or not --- src/Avalonia.Controls/TextBox.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index 7652b23162..52e5da95b3 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -53,6 +53,9 @@ namespace Avalonia.Controls public static readonly StyledProperty PasswordCharProperty = AvaloniaProperty.Register(nameof(PasswordChar)); + + public static readonly StyledProperty IgnoreChangesWhileEditingProperty = + AvaloniaProperty.Register(nameof(IgnoreChangesWhileEditing)); public static readonly StyledProperty SelectionBrushProperty = AvaloniaProperty.Register(nameof(SelectionBrush)); @@ -276,6 +279,12 @@ namespace Avalonia.Controls get => GetValue(IsReadOnlyProperty); set => SetValue(IsReadOnlyProperty, value); } + + public bool IgnoreChangesWhileEditing + { + get => GetValue(IgnoreChangesWhileEditingProperty); + set => SetValue(IgnoreChangesWhileEditingProperty, value); + } public char PasswordChar { @@ -1501,7 +1510,9 @@ namespace Avalonia.Controls { try { - _ignoreTextChanges = true; + if (IgnoreChangesWhileEditing == true) + _ignoreTextChanges = true; + SetAndRaise(TextProperty, ref _text, value); } finally From 0868442ec92aa4d427a8e750e9b1b0232c9662ce Mon Sep 17 00:00:00 2001 From: ahmedmohammedfawzy <42243982+ahmedmohammedfawzy@users.noreply.github.com> Date: Thu, 9 Jun 2022 21:04:12 +0200 Subject: [PATCH 095/224] Update TextBox.cs --- src/Avalonia.Controls/TextBox.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index 52e5da95b3..77be6bd9ee 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -55,7 +55,7 @@ namespace Avalonia.Controls AvaloniaProperty.Register(nameof(PasswordChar)); public static readonly StyledProperty IgnoreChangesWhileEditingProperty = - AvaloniaProperty.Register(nameof(IgnoreChangesWhileEditing)); + AvaloniaProperty.Register(nameof(IgnoreChangesWhileEditing), true); public static readonly StyledProperty SelectionBrushProperty = AvaloniaProperty.Register(nameof(SelectionBrush)); From 5ebba8e68c960016c7054e80073ff952a0bc77c1 Mon Sep 17 00:00:00 2001 From: ahmedmohammedfawzy <42243982+ahmedmohammedfawzy@users.noreply.github.com> Date: Fri, 10 Jun 2022 08:55:08 +0200 Subject: [PATCH 096/224] Update TextBox.cs --- src/Avalonia.Controls/TextBox.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/TextBox.cs b/src/Avalonia.Controls/TextBox.cs index 77be6bd9ee..52e5da95b3 100644 --- a/src/Avalonia.Controls/TextBox.cs +++ b/src/Avalonia.Controls/TextBox.cs @@ -55,7 +55,7 @@ namespace Avalonia.Controls AvaloniaProperty.Register(nameof(PasswordChar)); public static readonly StyledProperty IgnoreChangesWhileEditingProperty = - AvaloniaProperty.Register(nameof(IgnoreChangesWhileEditing), true); + AvaloniaProperty.Register(nameof(IgnoreChangesWhileEditing)); public static readonly StyledProperty SelectionBrushProperty = AvaloniaProperty.Register(nameof(SelectionBrush)); From 8b4cf63be3ffbf29427bb16d15ff514c595d348b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 10 Jun 2022 11:18:22 +0200 Subject: [PATCH 097/224] Additional validation for ControlTheme children. --- src/Avalonia.Base/Styling/ChildSelector.cs | 2 +- .../Styling/DescendentSelector.cs | 2 +- src/Avalonia.Base/Styling/NestingSelector.cs | 2 +- src/Avalonia.Base/Styling/NotSelector.cs | 2 +- src/Avalonia.Base/Styling/NthChildSelector.cs | 2 +- src/Avalonia.Base/Styling/OrSelector.cs | 12 +--- .../Styling/PropertyEqualsSelector.cs | 2 +- src/Avalonia.Base/Styling/Selector.cs | 31 ++++++++- src/Avalonia.Base/Styling/Style.cs | 9 ++- src/Avalonia.Base/Styling/Styles.cs | 5 ++ src/Avalonia.Base/Styling/TemplateSelector.cs | 2 +- .../Styling/TypeNameAndClassSelector.cs | 2 +- .../Styling/ControlThemeTests.cs | 64 +++++++++++++++++++ 13 files changed, 117 insertions(+), 20 deletions(-) diff --git a/src/Avalonia.Base/Styling/ChildSelector.cs b/src/Avalonia.Base/Styling/ChildSelector.cs index 34f3a76b61..9512dc34df 100644 --- a/src/Avalonia.Base/Styling/ChildSelector.cs +++ b/src/Avalonia.Base/Styling/ChildSelector.cs @@ -65,6 +65,6 @@ namespace Avalonia.Styling } protected override Selector? MovePrevious() => null; - internal override bool HasValidNestingSelector() => _parent.HasValidNestingSelector(); + protected override Selector? MovePreviousOrParent() => _parent; } } diff --git a/src/Avalonia.Base/Styling/DescendentSelector.cs b/src/Avalonia.Base/Styling/DescendentSelector.cs index 4ffaff6861..677a924189 100644 --- a/src/Avalonia.Base/Styling/DescendentSelector.cs +++ b/src/Avalonia.Base/Styling/DescendentSelector.cs @@ -70,6 +70,6 @@ namespace Avalonia.Styling } protected override Selector? MovePrevious() => null; - internal override bool HasValidNestingSelector() => _parent.HasValidNestingSelector(); + protected override Selector? MovePreviousOrParent() => _parent; } } diff --git a/src/Avalonia.Base/Styling/NestingSelector.cs b/src/Avalonia.Base/Styling/NestingSelector.cs index 4393d3239f..77c5b719c6 100644 --- a/src/Avalonia.Base/Styling/NestingSelector.cs +++ b/src/Avalonia.Base/Styling/NestingSelector.cs @@ -33,6 +33,6 @@ namespace Avalonia.Styling } protected override Selector? MovePrevious() => null; - internal override bool HasValidNestingSelector() => true; + protected override Selector? MovePreviousOrParent() => null; } } diff --git a/src/Avalonia.Base/Styling/NotSelector.cs b/src/Avalonia.Base/Styling/NotSelector.cs index 76a0690e96..c7727bb6b8 100644 --- a/src/Avalonia.Base/Styling/NotSelector.cs +++ b/src/Avalonia.Base/Styling/NotSelector.cs @@ -67,6 +67,6 @@ namespace Avalonia.Styling } protected override Selector? MovePrevious() => _previous; - internal override bool HasValidNestingSelector() => _previous?.HasValidNestingSelector() ?? false; + protected override Selector? MovePreviousOrParent() => _previous; } } diff --git a/src/Avalonia.Base/Styling/NthChildSelector.cs b/src/Avalonia.Base/Styling/NthChildSelector.cs index 047bf434da..f473791664 100644 --- a/src/Avalonia.Base/Styling/NthChildSelector.cs +++ b/src/Avalonia.Base/Styling/NthChildSelector.cs @@ -105,7 +105,7 @@ namespace Avalonia.Styling } protected override Selector? MovePrevious() => _previous; - internal override bool HasValidNestingSelector() => _previous?.HasValidNestingSelector() ?? false; + protected override Selector? MovePreviousOrParent() => _previous; public override string ToString() { diff --git a/src/Avalonia.Base/Styling/OrSelector.cs b/src/Avalonia.Base/Styling/OrSelector.cs index 913c27bf0c..af9249864f 100644 --- a/src/Avalonia.Base/Styling/OrSelector.cs +++ b/src/Avalonia.Base/Styling/OrSelector.cs @@ -103,18 +103,12 @@ namespace Avalonia.Styling } protected override Selector? MovePrevious() => null; + protected override Selector? MovePreviousOrParent() => null; - internal override bool HasValidNestingSelector() + internal override void ValidateNestingSelector(bool inControlTheme) { foreach (var selector in _selectors) - { - if (!selector.HasValidNestingSelector()) - { - return false; - } - } - - return true; + selector.ValidateNestingSelector(inControlTheme); } private Type? EvaluateTargetType() diff --git a/src/Avalonia.Base/Styling/PropertyEqualsSelector.cs b/src/Avalonia.Base/Styling/PropertyEqualsSelector.cs index 7a37daf087..48136ba2de 100644 --- a/src/Avalonia.Base/Styling/PropertyEqualsSelector.cs +++ b/src/Avalonia.Base/Styling/PropertyEqualsSelector.cs @@ -90,7 +90,7 @@ namespace Avalonia.Styling } protected override Selector? MovePrevious() => _previous; - internal override bool HasValidNestingSelector() => _previous?.HasValidNestingSelector() ?? false; + protected override Selector? MovePreviousOrParent() => _previous; internal static bool Compare(Type propertyType, object? propertyValue, object? value) { diff --git a/src/Avalonia.Base/Styling/Selector.cs b/src/Avalonia.Base/Styling/Selector.cs index 1e06f3d375..7ce17518dd 100644 --- a/src/Avalonia.Base/Styling/Selector.cs +++ b/src/Avalonia.Base/Styling/Selector.cs @@ -86,7 +86,36 @@ namespace Avalonia.Styling /// protected abstract Selector? MovePrevious(); - internal abstract bool HasValidNestingSelector(); + /// + /// Moves to the previous selector or the parent selector. + /// + protected abstract Selector? MovePreviousOrParent(); + + internal virtual void ValidateNestingSelector(bool inControlTheme) + { + var s = this; + var templateCount = 0; + + do + { + if (inControlTheme) + { + if (!s.InTemplate && s.IsCombinator) + throw new InvalidOperationException( + "ControlTheme style may not directly contain a child or descendent selector."); + if (s is TemplateSelector && templateCount++ > 0) + throw new InvalidOperationException( + "ControlTemplate styles cannot contain multiple template selectors."); + } + + var previous = s.MovePreviousOrParent(); + + if (previous is null && s is not NestingSelector) + throw new InvalidOperationException("Child styles must have a nesting selector."); + + s = previous; + } while (s is not null); + } private static SelectorMatch MatchUntilCombinator( IStyleable control, diff --git a/src/Avalonia.Base/Styling/Style.cs b/src/Avalonia.Base/Styling/Style.cs index 7a6b746488..77c4e62d29 100644 --- a/src/Avalonia.Base/Styling/Style.cs +++ b/src/Avalonia.Base/Styling/Style.cs @@ -77,8 +77,13 @@ namespace Avalonia.Styling { if (Selector is null) throw new InvalidOperationException("Child styles must have a selector."); - if (!Selector.HasValidNestingSelector()) - throw new InvalidOperationException("Child styles must have a nesting selector."); + Selector.ValidateNestingSelector(false); + } + else if (parent is ControlTheme) + { + if (Selector is null) + throw new InvalidOperationException("Child styles must have a selector."); + Selector.ValidateNestingSelector(true); } base.SetParent(parent); diff --git a/src/Avalonia.Base/Styling/Styles.cs b/src/Avalonia.Base/Styling/Styles.cs index 4c011f1b0d..3a27275438 100644 --- a/src/Avalonia.Base/Styling/Styles.cs +++ b/src/Avalonia.Base/Styling/Styles.cs @@ -26,6 +26,11 @@ namespace Avalonia.Styling { _styles.ResetBehavior = ResetBehavior.Remove; _styles.CollectionChanged += OnCollectionChanged; + _styles.Validate = i => + { + if (i is ControlTheme) + throw new InvalidOperationException("ControlThemes cannot be added to a Styles collection."); + }; } public Styles(IResourceHost owner) diff --git a/src/Avalonia.Base/Styling/TemplateSelector.cs b/src/Avalonia.Base/Styling/TemplateSelector.cs index b0a2dae8d6..278e24a203 100644 --- a/src/Avalonia.Base/Styling/TemplateSelector.cs +++ b/src/Avalonia.Base/Styling/TemplateSelector.cs @@ -49,6 +49,6 @@ namespace Avalonia.Styling } protected override Selector? MovePrevious() => null; - internal override bool HasValidNestingSelector() => _parent?.HasValidNestingSelector() ?? false; + protected override Selector? MovePreviousOrParent() => _parent; } } diff --git a/src/Avalonia.Base/Styling/TypeNameAndClassSelector.cs b/src/Avalonia.Base/Styling/TypeNameAndClassSelector.cs index 24d5d6bbbf..6681a7da36 100644 --- a/src/Avalonia.Base/Styling/TypeNameAndClassSelector.cs +++ b/src/Avalonia.Base/Styling/TypeNameAndClassSelector.cs @@ -140,7 +140,7 @@ namespace Avalonia.Styling } protected override Selector? MovePrevious() => _previous; - internal override bool HasValidNestingSelector() => _previous?.HasValidNestingSelector() ?? false; + protected override Selector? MovePreviousOrParent() => _previous; private string BuildSelectorString() { diff --git a/tests/Avalonia.Base.UnitTests/Styling/ControlThemeTests.cs b/tests/Avalonia.Base.UnitTests/Styling/ControlThemeTests.cs index 93a0e6c2fd..7a27a02fc4 100644 --- a/tests/Avalonia.Base.UnitTests/Styling/ControlThemeTests.cs +++ b/tests/Avalonia.Base.UnitTests/Styling/ControlThemeTests.cs @@ -1,5 +1,6 @@ using System; using Avalonia.Controls; +using Avalonia.Controls.Primitives; using Avalonia.Styling; using Xunit; @@ -7,6 +8,15 @@ namespace Avalonia.Base.UnitTests.Styling { public class ControlThemeTests { + [Fact] + public void ControlTheme_Cannot_Be_Added_To_Styles() + { + var target = new ControlTheme(typeof(Button)); + var styles = new Styles(); + + Assert.Throws(() => styles.Add(target)); + } + [Fact] public void ControlTheme_Cannot_Be_Added_To_Style_Children() { @@ -24,5 +34,59 @@ namespace Avalonia.Base.UnitTests.Styling Assert.Throws(() => other.Children.Add(target)); } + + [Fact] + public void Style_Without_Selector_Cannot_Be_Added_To_Children() + { + var target = new ControlTheme(typeof(Button)); + var child = new Style(); + + Assert.Throws(() => target.Children.Add(child)); + } + + [Fact] + public void Style_Without_Nesting_Selector_Cannot_Be_Added_To_Children() + { + var target = new ControlTheme(typeof(Button)); + var child = new Style(x => x.OfType + + diff --git a/samples/IntegrationTestApp/MainWindow.axaml.cs b/samples/IntegrationTestApp/MainWindow.axaml.cs index 580548a433..1aba10ec30 100644 --- a/samples/IntegrationTestApp/MainWindow.axaml.cs +++ b/samples/IntegrationTestApp/MainWindow.axaml.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using Avalonia; using Avalonia.Controls; +using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; using Avalonia.VisualTree; @@ -82,6 +83,16 @@ namespace IntegrationTestApp } } + private void SendToBack() + { + var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!; + + foreach (var window in lifetime.Windows) + { + window.Activate(); + } + } + private void MenuClicked(object? sender, RoutedEventArgs e) { var clickedMenuItemTextBlock = this.FindControl("ClickedMenuItem"); @@ -102,6 +113,10 @@ namespace IntegrationTestApp this.FindControl("ClickedMenuItem").Text = "None"; if (source?.Name == "ShowWindow") ShowWindow(); + if (source?.Name == "SendToBack") + SendToBack(); + if (source?.Name == "ExitFullscreen") + WindowState = WindowState.Normal; } } } diff --git a/samples/IntegrationTestApp/ShowWindowTest.axaml b/samples/IntegrationTestApp/ShowWindowTest.axaml index 2525bfc866..a263d8ab46 100644 --- a/samples/IntegrationTestApp/ShowWindowTest.axaml +++ b/samples/IntegrationTestApp/ShowWindowTest.axaml @@ -4,8 +4,6 @@ Name="SecondaryWindow" Title="Show Window Test"> - - diff --git a/samples/IntegrationTestApp/ShowWindowTest.axaml.cs b/samples/IntegrationTestApp/ShowWindowTest.axaml.cs index 3f45f1c5ad..720f7b1c12 100644 --- a/samples/IntegrationTestApp/ShowWindowTest.axaml.cs +++ b/samples/IntegrationTestApp/ShowWindowTest.axaml.cs @@ -34,7 +34,5 @@ namespace IntegrationTestApp ownerRect.Text = $"{owner.Position}, {owner.FrameSize}"; } } - - private void CloseWindow_Click(object sender, RoutedEventArgs e) => Close(); } } diff --git a/tests/Avalonia.IntegrationTests.Appium/ElementExtensions.cs b/tests/Avalonia.IntegrationTests.Appium/ElementExtensions.cs index 07fea24a93..16d37e4beb 100644 --- a/tests/Avalonia.IntegrationTests.Appium/ElementExtensions.cs +++ b/tests/Avalonia.IntegrationTests.Appium/ElementExtensions.cs @@ -15,6 +15,19 @@ namespace Avalonia.IntegrationTests.Appium public static IReadOnlyList GetChildren(this AppiumWebElement element) => element.FindElementsByXPath("*/*"); + public static (AppiumWebElement close, AppiumWebElement minimize, AppiumWebElement maximize) GetChromeButtons(this AppiumWebElement window) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + var closeButton = window.FindElementByXPath("//XCUIElementTypeButton[1]"); + var fullscreenButton = window.FindElementByXPath("//XCUIElementTypeButton[2]"); + var minimizeButton = window.FindElementByXPath("//XCUIElementTypeButton[3]"); + return (closeButton, minimizeButton, fullscreenButton); + } + + throw new NotSupportedException("GetChromeButtons not supported on this platform."); + } + public static string GetComboBoxValue(this AppiumWebElement element) { return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @@ -57,14 +70,15 @@ namespace Avalonia.IntegrationTests.Appium public static IDisposable OpenWindowWithClick(this AppiumWebElement element) { var session = element.WrappedDriver; - var oldHandle = session.CurrentWindowHandle; - var oldHandles = session.WindowHandles.ToList(); - var oldChildWindows = session.FindElements(By.XPath("//Window")); - - element.Click(); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { + var oldHandle = session.CurrentWindowHandle; + var oldHandles = session.WindowHandles.ToList(); + var oldChildWindows = session.FindElements(By.XPath("//Window")); + + element.Click(); + var newHandle = session.WindowHandles.Except(oldHandles).SingleOrDefault(); if (newHandle is not null) @@ -94,14 +108,22 @@ namespace Avalonia.IntegrationTests.Appium } else { - var newHandle = session.CurrentWindowHandle; - - Assert.NotEqual(oldHandle, newHandle); + var oldWindows = session.FindElements(By.XPath("/XCUIElementTypeApplication/XCUIElementTypeWindow")); + var oldWindowTitles = oldWindows.ToDictionary(x => x.Text); + + element.Click(); + var newWindows = session.FindElements(By.XPath("/XCUIElementTypeApplication/XCUIElementTypeWindow")); + var newWindowTitles = newWindows.ToDictionary(x => x.Text); + var newWindowTitle = Assert.Single(newWindowTitles.Keys.Except(oldWindowTitles.Keys)); + var newWindow = (AppiumWebElement)newWindowTitles[newWindowTitle]; + return Disposable.Create(() => { - session.Close(); - session.SwitchTo().Window(oldHandle); + // TODO: We should be able to use Cmd+W here but Avalonia apps don't seem to have this shortcut + // set up by default. + var (close, _, _) = newWindow.GetChromeButtons(); + close!.Click(); }); } } diff --git a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs index 38d0d5ba1c..e887a2afe8 100644 --- a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs @@ -57,82 +57,56 @@ namespace Avalonia.IntegrationTests.Appium [PlatformFact(SkipOnWindows = true)] public void OSX_WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent() { - var mainWindowHandle = GetCurrentWindowHandleHack(); - - var mainWindow = - _session.FindElementByAccessibilityId("MainWindow"); + var mainWindow = _session.FindElementByAccessibilityId("MainWindow"); - try + using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) { - using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) - { - mainWindow.Click(); + mainWindow.Click(); - var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); + var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); - int mainWindowIndex = windows.GetWindowOrder("MainWindow"); - int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); + int mainWindowIndex = windows.GetWindowOrder("MainWindow"); + int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); - Assert.Equal(0, secondaryWindowIndex); - Assert.Equal(1, mainWindowIndex); - } - } - finally - { - SwitchToMainWindowHack(mainWindowHandle); - _session.ResetApp(); + Assert.Equal(0, secondaryWindowIndex); + Assert.Equal(1, mainWindowIndex); } } [PlatformFact(SkipOnWindows = true)] public void OSX_WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_Clicking_Resize_Grip() { - var mainWindowHandle = GetCurrentWindowHandleHack(); - - var mainWindow = - _session.FindWindowOuter("MainWindow"); + var mainWindow = _session.FindWindowOuter("MainWindow"); - try + using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) { - using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) - { - new Actions(_session) - .MoveToElement(mainWindow, 100, 1) - .ClickAndHold() - .Perform(); + new Actions(_session) + .MoveToElement(mainWindow, 100, 1) + .ClickAndHold() + .Perform(); - var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); + var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); - int mainWindowIndex = windows.GetWindowOrder("MainWindow"); - int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); + int mainWindowIndex = windows.GetWindowOrder("MainWindow"); + int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); - new Actions(_session) - .MoveToElement(mainWindow, 100, 1) - .Release() - .Perform(); + new Actions(_session) + .MoveToElement(mainWindow, 100, 1) + .Release() + .Perform(); - Assert.Equal(0, secondaryWindowIndex); - Assert.Equal(1, mainWindowIndex); - } - } - finally - { - SwitchToMainWindowHack(mainWindowHandle); - _session.ResetApp(); + Assert.Equal(0, secondaryWindowIndex); + Assert.Equal(1, mainWindowIndex); } } [PlatformFact(SkipOnWindows = true)] public void OSX_WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_In_Fullscreen() { - var mainWindowHandle = GetCurrentWindowHandleHack(); - - var mainWindow = - _session.FindWindowOuter("MainWindow"); - + var mainWindow = _session.FindWindowOuter("MainWindow"); var buttons = mainWindow.GetChromeButtons(); - buttons.zoomButton.Click(); + buttons.maximize.Click(); Task.Delay(500).Wait(); @@ -151,70 +125,51 @@ namespace Avalonia.IntegrationTests.Appium } finally { - SwitchToMainWindowHack(mainWindowHandle); - _session.ResetApp(); + _session.FindElementByAccessibilityId("ExitFullscreen").Click(); } } [PlatformFact(SkipOnWindows = true)] public void OSX_WindowOrder_Owned_Dialog_Stays_InFront_Of_Parent() { - var mainWindowHandle = GetCurrentWindowHandleHack(); - - var mainWindow = - _session.FindElementByAccessibilityId("MainWindow"); + var mainWindow = _session.FindElementByAccessibilityId("MainWindow"); - try + using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Owned, WindowStartupLocation.CenterOwner)) { - using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Owned, WindowStartupLocation.CenterOwner)) - { - mainWindow.Click(); + mainWindow.Click(); - var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); + var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); - int mainWindowIndex = windows.GetWindowOrder("MainWindow"); - int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); + int mainWindowIndex = windows.GetWindowOrder("MainWindow"); + int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); - Assert.Equal(0, secondaryWindowIndex); - Assert.Equal(1, mainWindowIndex); - } - } - finally - { - SwitchToMainWindowHack(mainWindowHandle); - _session.ResetApp(); + Assert.Equal(0, secondaryWindowIndex); + Assert.Equal(1, mainWindowIndex); } } [PlatformFact(SkipOnWindows = true)] public void OSX_WindowOrder_NonOwned_Window_Does_Not_Stay_InFront_Of_Parent() { - var mainWindow = - _session.FindElementByAccessibilityId("MainWindow"); + var mainWindow = _session.FindElementByAccessibilityId("MainWindow"); - try + using (OpenWindow(new PixelSize(1400, 100), ShowWindowMode.NonOwned, WindowStartupLocation.CenterOwner)) { - using (OpenWindow(new PixelSize(1400, 100), ShowWindowMode.NonOwned, WindowStartupLocation.CenterOwner)) - { - mainWindow.Click(); + mainWindow.Click(); - var secondaryWindow = - _session.FindElementByAccessibilityId("SecondaryWindow"); + var secondaryWindow = + _session.FindElementByAccessibilityId("SecondaryWindow"); - var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); + var windows = _session.FindElements(By.XPath("XCUIElementTypeWindow")); - int mainWindowIndex = windows.GetWindowOrder("MainWindow"); - int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); + int mainWindowIndex = windows.GetWindowOrder("MainWindow"); + int secondaryWindowIndex = windows.GetWindowOrder("SecondaryWindow"); - Assert.Equal(1, secondaryWindowIndex); - Assert.Equal(0, mainWindowIndex); + Assert.Equal(1, secondaryWindowIndex); + Assert.Equal(0, mainWindowIndex); - secondaryWindow.SendClick(); - } - } - finally - { - _session.ResetApp(); + var sendToBack = _session.FindElementByAccessibilityId("SendToBack"); + sendToBack.Click(); } } @@ -222,31 +177,22 @@ namespace Avalonia.IntegrationTests.Appium public void OSX_Parent_Window_Has_Disabled_ChromeButtons_When_Modal_Dialog_Shown() { var mainWindowHandle = GetCurrentWindowHandleHack(); - - try - { - var window = _session.FindWindowOuter("MainWindow"); + var window = _session.FindWindowOuter("MainWindow"); - var (closeButton, zoomButton, miniturizeButton) - = window.GetChromeButtons(); + var (closeButton, zoomButton, miniturizeButton) + = window.GetChromeButtons(); - Assert.True(closeButton.Enabled); - Assert.True(zoomButton.Enabled); - Assert.True(miniturizeButton.Enabled); + Assert.True(closeButton.Enabled); + Assert.True(zoomButton.Enabled); + Assert.True(miniturizeButton.Enabled); - using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) - { - SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle); - - Assert.False(closeButton.Enabled); - Assert.False(zoomButton.Enabled); - Assert.False(miniturizeButton.Enabled); - } - } - finally + using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) { - SwitchToMainWindowHack(mainWindowHandle); - _session.ResetApp(); + SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle); + + Assert.False(closeButton.Enabled); + Assert.False(zoomButton.Enabled); + Assert.False(miniturizeButton.Enabled); } } @@ -255,26 +201,18 @@ namespace Avalonia.IntegrationTests.Appium { var mainWindowHandle = GetCurrentWindowHandleHack(); - try + using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) { - using (OpenWindow(new PixelSize(200, 100), ShowWindowMode.Modal, WindowStartupLocation.CenterOwner)) - { - var secondaryWindow = _session.FindWindowOuter("SecondaryWindow"); + var secondaryWindow = _session.FindWindowOuter("SecondaryWindow"); - var (closeButton, zoomButton, miniturizeButton) - = secondaryWindow.GetChromeButtons(); - - Assert.True(closeButton.Enabled); - Assert.True(zoomButton.Enabled); - Assert.False(miniturizeButton.Enabled); + var (closeButton, zoomButton, miniaturizeButton) + = secondaryWindow.GetChromeButtons(); - SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle); - } - } - finally - { - SwitchToMainWindowHack(mainWindowHandle); - _session.ResetApp(); + Assert.True(closeButton.Enabled); + Assert.True(zoomButton.Enabled); + Assert.False(miniaturizeButton.Enabled); + + SwitchToNewWindowHack(oldWindowHandle: mainWindowHandle); } } @@ -409,19 +347,5 @@ namespace Avalonia.IntegrationTests.Appium return window; } - - public static (AppiumWebElement? closeButton, AppiumWebElement? zoomButton, AppiumWebElement? miniturizeButton) GetChromeButtons (this AppiumWebElement outerWindow) - { - var closeButton = - outerWindow.FindElementByXPath("XCUIElementTypeButton[1]"); - - var zoomButton = - outerWindow.FindElementByXPath("XCUIElementTypeButton[2]"); - - var miniturizeButton = - outerWindow.FindElementByXPath("XCUIElementTypeButton[3]"); - - return (closeButton, zoomButton, miniturizeButton); - } } } From 8b9e675bd4a8ee174c78d54062c1c0b04a22b845 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 22 Jun 2022 10:55:19 +0200 Subject: [PATCH 124/224] Refactor PlatformFact. To make it inclusive rather than exclusive. --- .../ButtonTests.cs | 2 +- .../ComboBoxTests.cs | 6 ++-- .../ListBoxTests.cs | 2 +- .../MenuTests.cs | 16 +++++----- .../NativeMenuTests.cs | 2 +- .../PlatformFactAttribute.cs | 32 +++++++++++++------ .../WindowTests.cs | 14 ++++---- 7 files changed, 43 insertions(+), 31 deletions(-) diff --git a/tests/Avalonia.IntegrationTests.Appium/ButtonTests.cs b/tests/Avalonia.IntegrationTests.Appium/ButtonTests.cs index 2ac859e091..6c630ae782 100644 --- a/tests/Avalonia.IntegrationTests.Appium/ButtonTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/ButtonTests.cs @@ -44,7 +44,7 @@ namespace Avalonia.IntegrationTests.Appium Assert.Equal("Button with TextBlock", button.Text); } - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void ButtonWithAcceleratorKey() { var button = _session.FindElementByAccessibilityId("ButtonWithAcceleratorKey"); diff --git a/tests/Avalonia.IntegrationTests.Appium/ComboBoxTests.cs b/tests/Avalonia.IntegrationTests.Appium/ComboBoxTests.cs index fad3e1eb9f..abdb4e2dd8 100644 --- a/tests/Avalonia.IntegrationTests.Appium/ComboBoxTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/ComboBoxTests.cs @@ -46,7 +46,7 @@ namespace Avalonia.IntegrationTests.Appium Assert.Equal("Item 0", comboBox.GetComboBoxValue()); } - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void Can_Change_Selection_With_Keyboard() { var comboBox = _session.FindElementByAccessibilityId("BasicComboBox"); @@ -63,7 +63,7 @@ namespace Avalonia.IntegrationTests.Appium Assert.Equal("Item 1", comboBox.GetComboBoxValue()); } - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void Can_Change_Selection_With_Keyboard_From_Unselected() { var comboBox = _session.FindElementByAccessibilityId("BasicComboBox"); @@ -80,7 +80,7 @@ namespace Avalonia.IntegrationTests.Appium Assert.Equal("Item 0", comboBox.GetComboBoxValue()); } - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void Can_Cancel_Keyboard_Selection_With_Escape() { var comboBox = _session.FindElementByAccessibilityId("BasicComboBox"); diff --git a/tests/Avalonia.IntegrationTests.Appium/ListBoxTests.cs b/tests/Avalonia.IntegrationTests.Appium/ListBoxTests.cs index 625742ac20..e2943b3349 100644 --- a/tests/Avalonia.IntegrationTests.Appium/ListBoxTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/ListBoxTests.cs @@ -61,7 +61,7 @@ namespace Avalonia.IntegrationTests.Appium } // appium-mac2-driver just hangs - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void Can_Select_Range_By_Shift_Clicking() { var listBox = GetTarget(); diff --git a/tests/Avalonia.IntegrationTests.Appium/MenuTests.cs b/tests/Avalonia.IntegrationTests.Appium/MenuTests.cs index 98fb335061..d1d231466f 100644 --- a/tests/Avalonia.IntegrationTests.Appium/MenuTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/MenuTests.cs @@ -57,7 +57,7 @@ namespace Avalonia.IntegrationTests.Appium Assert.Equal("_Grandchild", clickedMenuItem.Text); } - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void Select_Child_With_Alt_Arrow_Keys() { new Actions(_session) @@ -69,7 +69,7 @@ namespace Avalonia.IntegrationTests.Appium Assert.Equal("_Child 1", clickedMenuItem.Text); } - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void Select_Grandchild_With_Alt_Arrow_Keys() { new Actions(_session) @@ -81,7 +81,7 @@ namespace Avalonia.IntegrationTests.Appium Assert.Equal("_Grandchild", clickedMenuItem.Text); } - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void Select_Child_With_Alt_Access_Keys() { new Actions(_session) @@ -93,7 +93,7 @@ namespace Avalonia.IntegrationTests.Appium Assert.Equal("_Child 1", clickedMenuItem.Text); } - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void Select_Grandchild_With_Alt_Access_Keys() { new Actions(_session) @@ -105,7 +105,7 @@ namespace Avalonia.IntegrationTests.Appium Assert.Equal("_Grandchild", clickedMenuItem.Text); } - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void Select_Child_With_Click_Arrow_Keys() { var rootMenuItem = _session.FindElementByAccessibilityId("RootMenuItem"); @@ -119,7 +119,7 @@ namespace Avalonia.IntegrationTests.Appium Assert.Equal("_Child 1", clickedMenuItem.Text); } - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void Select_Grandchild_With_Click_Arrow_Keys() { var rootMenuItem = _session.FindElementByAccessibilityId("RootMenuItem"); @@ -133,7 +133,7 @@ namespace Avalonia.IntegrationTests.Appium Assert.Equal("_Grandchild", clickedMenuItem.Text); } - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void Child_AcceleratorKey() { var rootMenuItem = _session.FindElementByAccessibilityId("RootMenuItem"); @@ -145,7 +145,7 @@ namespace Avalonia.IntegrationTests.Appium Assert.Equal("Ctrl+O", childMenuItem.GetAttribute("AcceleratorKey")); } - [PlatformFact(SkipOnOSX = true)] + [PlatformFact(TestPlatforms.Windows)] public void PointerOver_Does_Not_Steal_Focus() { // Issue #7906 diff --git a/tests/Avalonia.IntegrationTests.Appium/NativeMenuTests.cs b/tests/Avalonia.IntegrationTests.Appium/NativeMenuTests.cs index fde01f0e41..7858c4cc81 100644 --- a/tests/Avalonia.IntegrationTests.Appium/NativeMenuTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/NativeMenuTests.cs @@ -17,7 +17,7 @@ namespace Avalonia.IntegrationTests.Appium tab.Click(); } - [PlatformFact(SkipOnWindows = true)] + [PlatformFact(TestPlatforms.MacOS)] public void View_Menu_Select_Button_Tab() { var tabs = _session.FindElementByAccessibilityId("MainTabs"); diff --git a/tests/Avalonia.IntegrationTests.Appium/PlatformFactAttribute.cs b/tests/Avalonia.IntegrationTests.Appium/PlatformFactAttribute.cs index 60338b92c2..53ae5d924f 100644 --- a/tests/Avalonia.IntegrationTests.Appium/PlatformFactAttribute.cs +++ b/tests/Avalonia.IntegrationTests.Appium/PlatformFactAttribute.cs @@ -5,21 +5,33 @@ using Xunit; namespace Avalonia.IntegrationTests.Appium { + [Flags] + internal enum TestPlatforms + { + Windows = 0x01, + MacOS = 0x02, + All = Windows | MacOS, + } + internal class PlatformFactAttribute : FactAttribute { + public PlatformFactAttribute(TestPlatforms platforms = TestPlatforms.All) => Platforms = platforms; + + public TestPlatforms Platforms { get; } + public override string? Skip { - get - { - if (SkipOnWindows && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - return "Ignored on Windows"; - if (SkipOnOSX && RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - return "Ignored on MacOS"; - return null; - } + get => IsSupported() ? null : $"Ignored on {RuntimeInformation.OSDescription}"; set => throw new NotSupportedException(); } - public bool SkipOnOSX { get; set; } - public bool SkipOnWindows { get; set; } + + private bool IsSupported() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + return Platforms.HasAnyFlag(TestPlatforms.Windows); + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + return Platforms.HasAnyFlag(TestPlatforms.MacOS); + return false; + } } } diff --git a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs index e887a2afe8..b759dafbeb 100644 --- a/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs +++ b/tests/Avalonia.IntegrationTests.Appium/WindowTests.cs @@ -54,7 +54,7 @@ namespace Avalonia.IntegrationTests.Appium } } - [PlatformFact(SkipOnWindows = true)] + [PlatformFact(TestPlatforms.MacOS)] public void OSX_WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent() { var mainWindow = _session.FindElementByAccessibilityId("MainWindow"); @@ -73,7 +73,7 @@ namespace Avalonia.IntegrationTests.Appium } } - [PlatformFact(SkipOnWindows = true)] + [PlatformFact(TestPlatforms.MacOS)] public void OSX_WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_Clicking_Resize_Grip() { var mainWindow = _session.FindWindowOuter("MainWindow"); @@ -100,7 +100,7 @@ namespace Avalonia.IntegrationTests.Appium } } - [PlatformFact(SkipOnWindows = true)] + [PlatformFact(TestPlatforms.MacOS)] public void OSX_WindowOrder_Modal_Dialog_Stays_InFront_Of_Parent_When_In_Fullscreen() { var mainWindow = _session.FindWindowOuter("MainWindow"); @@ -129,7 +129,7 @@ namespace Avalonia.IntegrationTests.Appium } } - [PlatformFact(SkipOnWindows = true)] + [PlatformFact(TestPlatforms.MacOS)] public void OSX_WindowOrder_Owned_Dialog_Stays_InFront_Of_Parent() { var mainWindow = _session.FindElementByAccessibilityId("MainWindow"); @@ -148,7 +148,7 @@ namespace Avalonia.IntegrationTests.Appium } } - [PlatformFact(SkipOnWindows = true)] + [PlatformFact(TestPlatforms.MacOS)] public void OSX_WindowOrder_NonOwned_Window_Does_Not_Stay_InFront_Of_Parent() { var mainWindow = _session.FindElementByAccessibilityId("MainWindow"); @@ -173,7 +173,7 @@ namespace Avalonia.IntegrationTests.Appium } } - [PlatformFact(SkipOnWindows = true)] + [PlatformFact(TestPlatforms.MacOS)] public void OSX_Parent_Window_Has_Disabled_ChromeButtons_When_Modal_Dialog_Shown() { var mainWindowHandle = GetCurrentWindowHandleHack(); @@ -196,7 +196,7 @@ namespace Avalonia.IntegrationTests.Appium } } - [PlatformFact(SkipOnWindows = true)] + [PlatformFact(TestPlatforms.MacOS)] public void OSX_Minimize_Button_Disabled_Modal_Dialog() { var mainWindowHandle = GetCurrentWindowHandleHack(); From 64518efc5184d67208a7d0c3b7df9c66a364a562 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 22 Jun 2022 12:59:40 +0200 Subject: [PATCH 125/224] Add failing test for #8372. --- .../AvaloniaObjectTests_SetValue.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_SetValue.cs b/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_SetValue.cs index 954a609315..72162a4d8e 100644 --- a/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_SetValue.cs +++ b/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_SetValue.cs @@ -17,6 +17,21 @@ namespace Avalonia.Base.UnitTests Assert.Equal("foodefault", target.GetValue(Class1.FooProperty)); } + [Fact] + public void ClearValue_Resets_Value_To_Style_value() + { + Class1 target = new Class1(); + + target.SetValue(Class1.FooProperty, "style", BindingPriority.Style); + target.SetValue(Class1.FooProperty, "local"); + + Assert.Equal("local", target.GetValue(Class1.FooProperty)); + + target.ClearValue(Class1.FooProperty); + + Assert.Equal("style", target.GetValue(Class1.FooProperty)); + } + [Fact] public void ClearValue_Raises_PropertyChanged() { From f33d4e881f3b4fb4db9a13279ad8bf1c648b3151 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 22 Jun 2022 13:01:37 +0200 Subject: [PATCH 126/224] Correctly clear local value in PriorityValue. --- src/Avalonia.Base/PropertyStore/PriorityValue.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Avalonia.Base/PropertyStore/PriorityValue.cs b/src/Avalonia.Base/PropertyStore/PriorityValue.cs index 112cf6619f..182b2638c4 100644 --- a/src/Avalonia.Base/PropertyStore/PriorityValue.cs +++ b/src/Avalonia.Base/PropertyStore/PriorityValue.cs @@ -121,6 +121,7 @@ namespace Avalonia.PropertyStore public void ClearLocalValue() { + _localValue = default; UpdateEffectiveValue(new AvaloniaPropertyChangedEventArgs( _owner, Property, From 7b7d6581253ab244b22455ff3b8fdae35dab52a9 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 22 Jun 2022 13:38:49 +0200 Subject: [PATCH 127/224] Added a new failing test. --- .../Styling/StyledElementTests_Theming.cs | 64 ++++++++++++------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs index 522937b669..c2692c30ab 100644 --- a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs +++ b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs @@ -82,7 +82,7 @@ public class StyledElementTests_Theming { Setters = { - new Setter(Canvas.BackgroundProperty, Brushes.Red), + new Setter(Panel.BackgroundProperty, Brushes.Red), } }, } @@ -224,6 +224,39 @@ public class StyledElementTests_Theming Assert.Equal(border.Background, Brushes.Green); } + [Fact] + public void Theme_Can_Be_Changed_By_Style_Class() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(); + var theme1 = CreateTheme(); + var theme2 = new ControlTheme(typeof(ThemedControl)); + var root = new TestRoot() + { + Styles = + { + new Style(x => x.OfType()) + { + Setters = { new Setter(StyledElement.ThemeProperty, theme1) } + }, + new Style(x => x.OfType().Class("bar")) + { + Setters = { new Setter(StyledElement.ThemeProperty, theme2) } + }, + } + }; + + root.Child = target; + root.LayoutManager.ExecuteInitialLayoutPass(); + + Assert.Same(theme1, target.Theme); + Assert.NotNull(target.Template); + + target.Classes.Add("bar"); + Assert.Same(theme2, target.Theme); + Assert.Null(target.Template); + } + private static ThemedControl CreateTarget() { return new ThemedControl(); @@ -234,15 +267,12 @@ public class StyledElementTests_Theming var result = new TestRoot() { Styles = - { - new Style(x => x.OfType()) { - Setters = + new Style(x => x.OfType()) { - new Setter(TemplatedControl.ThemeProperty, CreateTheme()) + Setters = { new Setter(StyledElement.ThemeProperty, CreateTheme()) } } } - } }; result.Child = child; @@ -260,23 +290,17 @@ public class StyledElementTests_Theming TargetType = typeof(ThemedControl), Setters = { - new Setter(ThemedControl.TemplateProperty, template), + new Setter(TemplatedControl.TemplateProperty, template), }, Children = { new Style(x => x.Nesting().Template().OfType()) { - Setters = - { - new Setter(Border.BackgroundProperty, Brushes.Red), - } + Setters = { new Setter(Border.BackgroundProperty, Brushes.Red) } }, new Style(x => x.Nesting().Class("foo").Template().OfType()) { - Setters = - { - new Setter(Border.BackgroundProperty, Brushes.Green), - } + Setters = { new Setter(Border.BackgroundProperty, Brushes.Green) } }, } }; @@ -296,17 +320,11 @@ public class StyledElementTests_Theming { new Style(x => x.Nesting().Template().OfType()) { - Setters = - { - new Setter(Border.BorderBrushProperty, Brushes.Yellow), - } + Setters = { new Setter(Border.BorderBrushProperty, Brushes.Yellow) } }, new Style(x => x.Nesting().Class("foo").Template().OfType()) { - Setters = - { - new Setter(Border.BorderBrushProperty, Brushes.Cyan), - } + Setters = { new Setter(Border.BorderBrushProperty, Brushes.Cyan) } }, } }; From c9e10f0d2f88346caeb461b900199b6fb571653d Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 22 Jun 2022 15:53:16 +0200 Subject: [PATCH 128/224] Added additional failing test. Exposed by the previous fix for #8372: re-entrancy in `PropertySetterInstance.Dispose()` is causing detaching a style to call `ClearValue` on the property. Previously this wasn't a problem as `ClearValue` didn't work, but now it is. (Also added one passing test which tests the same scenario in `PropertySetterBindingInstance` for future coverage) --- .../Styling/SetterTests.cs | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/tests/Avalonia.Base.UnitTests/Styling/SetterTests.cs b/tests/Avalonia.Base.UnitTests/Styling/SetterTests.cs index ed4c78aa3e..c684466200 100644 --- a/tests/Avalonia.Base.UnitTests/Styling/SetterTests.cs +++ b/tests/Avalonia.Base.UnitTests/Styling/SetterTests.cs @@ -150,13 +150,43 @@ namespace Avalonia.Base.UnitTests.Styling Assert.Equal(BindingPriority.StyleTrigger, control.GetDiagnostic(TextBlock.TagProperty).Priority); } - private IBinding CreateMockBinding(AvaloniaProperty property) + [Fact] + public void Disposing_Setter_Should_Preserve_LocalValue() { - var subject = new Subject(); - var descriptor = InstancedBinding.OneWay(subject); - var binding = Mock.Of(x => - x.Initiate(It.IsAny(), property, null, false) == descriptor); - return binding; + var control = new Canvas(); + var setter = new Setter(TextBlock.TagProperty, "foo"); + + var instance = setter.Instance(control); + instance.Start(true); + instance.Activate(); + + control.Tag = "bar"; + + instance.Dispose(); + + Assert.Equal("bar", control.Tag); + } + + [Fact] + public void Disposing_Binding_Setter_Should_Preserve_LocalValue() + { + var control = new Canvas(); + var source = new { Foo = "foo" }; + var setter = new Setter(TextBlock.TagProperty, new Binding + { + Source = source, + Path = nameof(source.Foo), + }); + + var instance = setter.Instance(control); + instance.Start(true); + instance.Activate(); + + control.Tag = "bar"; + + instance.Dispose(); + + Assert.Equal("bar", control.Tag); } private class TestConverter : IValueConverter From 857bfb5bd2b863825c09fbfb780dc379fba4d345 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 22 Jun 2022 16:00:29 +0200 Subject: [PATCH 129/224] Prevent re-entrancy in PropertySetterInstance.Dispose. The call to `_subscription.Dispose()` causes `BindingEntry.Dispose()` to call `_subscription.Dispose()`, but in this case the `BindingEntry._subscription` instance is the `PropertySetterInstance`! Except now `PropertySetterInstance._subscription` is null, and so `PropertySetterInstance.Dispose` called `ClearValue`, which is obviously wrong. --- .../Styling/PropertySetterInstance.cs | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Avalonia.Base/Styling/PropertySetterInstance.cs b/src/Avalonia.Base/Styling/PropertySetterInstance.cs index c4e8f47e67..9028224cc1 100644 --- a/src/Avalonia.Base/Styling/PropertySetterInstance.cs +++ b/src/Avalonia.Base/Styling/PropertySetterInstance.cs @@ -18,7 +18,7 @@ namespace Avalonia.Styling private readonly DirectPropertyBase? _directProperty; private readonly T _value; private IDisposable? _subscription; - private bool _isActive; + private State _state; public PropertySetterInstance( IStyleable target, @@ -40,6 +40,8 @@ namespace Avalonia.Styling _value = value; } + private bool IsActive => _state == State.Active; + public void Start(bool hasActivator) { if (hasActivator) @@ -70,31 +72,35 @@ namespace Avalonia.Styling public void Activate() { - if (!_isActive) + if (!IsActive) { - _isActive = true; + _state = State.Active; PublishNext(); } } public void Deactivate() { - if (_isActive) + if (IsActive) { - _isActive = false; + _state = State.Inactive; PublishNext(); } } public override void Dispose() { + if (_state == State.Disposed) + return; + _state = State.Disposed; + if (_subscription is object) { var sub = _subscription; _subscription = null; sub.Dispose(); } - else if (_isActive) + else if (IsActive) { if (_styledProperty is object) { @@ -114,7 +120,14 @@ namespace Avalonia.Styling private void PublishNext() { - PublishNext(_isActive ? new BindingValue(_value) : default); + PublishNext(IsActive ? new BindingValue(_value) : default); + } + + private enum State + { + Inactive, + Active, + Disposed, } } } From 421546609a68933dee4e427968a74733f929fe04 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 22 Jun 2022 16:13:43 +0200 Subject: [PATCH 130/224] Add test/fix for promoted themes. A bit of an edge case here, but we should deal with it. --- src/Avalonia.Base/StyledElement.cs | 4 ++ .../Styling/StyledElementTests_Theming.cs | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/Avalonia.Base/StyledElement.cs b/src/Avalonia.Base/StyledElement.cs index f377eb848c..189d73c502 100644 --- a/src/Avalonia.Base/StyledElement.cs +++ b/src/Avalonia.Base/StyledElement.cs @@ -652,6 +652,10 @@ namespace Avalonia Theme = change.GetNewValue(); _hasPromotedTheme = true; } + else if (_hasPromotedTheme && change.Priority == BindingPriority.LocalValue) + { + _hasPromotedTheme = false; + } InvalidateStyles(); } diff --git a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs index c2692c30ab..9b133035f4 100644 --- a/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs +++ b/tests/Avalonia.Base.UnitTests/Styling/StyledElementTests_Theming.cs @@ -257,6 +257,47 @@ public class StyledElementTests_Theming Assert.Null(target.Template); } + [Fact] + public void Theme_Can_Be_Set_To_LocalValue_While_Updating_Due_To_Style_Class() + { + using var app = UnitTestApplication.Start(TestServices.RealStyler); + var target = CreateTarget(); + var theme1 = CreateTheme(); + var theme2 = new ControlTheme(typeof(ThemedControl)); + var theme3 = new ControlTheme(typeof(ThemedControl)); + var root = new TestRoot() + { + Styles = + { + new Style(x => x.OfType()) + { + Setters = { new Setter(StyledElement.ThemeProperty, theme1) } + }, + new Style(x => x.OfType().Class("bar")) + { + Setters = { new Setter(StyledElement.ThemeProperty, theme2) } + }, + } + }; + + root.Child = target; + root.LayoutManager.ExecuteInitialLayoutPass(); + + Assert.Same(theme1, target.Theme); + Assert.NotNull(target.Template); + + target.Classes.Add("bar"); + + // At this point, theme2 has been promoted to a local value internally in StyledElement; + // make sure that setting a new local value here doesn't cause it to be cleared when we + // do a layout pass because StyledElement thinks its clearing the promoted theme. + target.Theme = theme3; + + root.LayoutManager.ExecuteLayoutPass(); + + Assert.Same(target.Theme, theme3); + } + private static ThemedControl CreateTarget() { return new ThemedControl(); From a408ea10d79ac357d968a1ff96a3664506a6058b Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Wed, 22 Jun 2022 16:45:38 +0200 Subject: [PATCH 131/224] Some Progress --- samples/Sandbox/MainWindow.axaml | 14 + .../Media/TextFormatting/TextFormatterImpl.cs | 2 +- .../TextFormatting/Unicode/BiDiAlgorithm.cs | 6 - .../Documents/InlineCollection.cs | 2 - src/Avalonia.Controls/Documents/Span.cs | 20 +- .../Presenters/TextPresenter.cs | 8 +- src/Avalonia.Controls/RichTextBlock.cs | 326 +++++++++++++++++- src/Avalonia.Controls/TextBlock.cs | 12 +- .../TextFormatting/BiDiAlgorithmTests.cs | 2 +- .../RichTextBlockTests.cs | 52 +++ .../TextBlockTests.cs | 42 --- 11 files changed, 397 insertions(+), 89 deletions(-) create mode 100644 tests/Avalonia.Controls.UnitTests/RichTextBlockTests.cs diff --git a/samples/Sandbox/MainWindow.axaml b/samples/Sandbox/MainWindow.axaml index 6929f192c7..806f6d37da 100644 --- a/samples/Sandbox/MainWindow.axaml +++ b/samples/Sandbox/MainWindow.axaml @@ -1,4 +1,18 @@ + + + + This is a + TextBlock + with several + Span elements, + + using a variety of styles + . + + + + diff --git a/src/Avalonia.Base/Media/TextFormatting/TextFormatterImpl.cs b/src/Avalonia.Base/Media/TextFormatting/TextFormatterImpl.cs index 4205268bc6..cd764be43f 100644 --- a/src/Avalonia.Base/Media/TextFormatting/TextFormatterImpl.cs +++ b/src/Avalonia.Base/Media/TextFormatting/TextFormatterImpl.cs @@ -177,7 +177,7 @@ namespace Avalonia.Media.TextFormatting } - var biDi = BidiAlgorithm.Instance.Value!; + var biDi = new BidiAlgorithm(); biDi.Process(biDiData); diff --git a/src/Avalonia.Base/Media/TextFormatting/Unicode/BiDiAlgorithm.cs b/src/Avalonia.Base/Media/TextFormatting/Unicode/BiDiAlgorithm.cs index 2511807d9c..3c510ff484 100644 --- a/src/Avalonia.Base/Media/TextFormatting/Unicode/BiDiAlgorithm.cs +++ b/src/Avalonia.Base/Media/TextFormatting/Unicode/BiDiAlgorithm.cs @@ -188,12 +188,6 @@ namespace Avalonia.Media.TextFormatting.Unicode { } - /// - /// Gets a per-thread instance that can be re-used as often - /// as necessary. - /// - public static ThreadLocal Instance { get; } = new ThreadLocal(() => new BidiAlgorithm()); - /// /// Gets the resolved levels. /// diff --git a/src/Avalonia.Controls/Documents/InlineCollection.cs b/src/Avalonia.Controls/Documents/InlineCollection.cs index 0cbf272297..2f27ca72d0 100644 --- a/src/Avalonia.Controls/Documents/InlineCollection.cs +++ b/src/Avalonia.Controls/Documents/InlineCollection.cs @@ -73,8 +73,6 @@ namespace Avalonia.Controls.Documents { get { - return _text; - if (!HasComplexContent) { return _text; diff --git a/src/Avalonia.Controls/Documents/Span.cs b/src/Avalonia.Controls/Documents/Span.cs index c2576ec231..98851726da 100644 --- a/src/Avalonia.Controls/Documents/Span.cs +++ b/src/Avalonia.Controls/Documents/Span.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Text; using Avalonia.Media.TextFormatting; +using Avalonia.Metadata; namespace Avalonia.Controls.Documents { @@ -27,29 +28,14 @@ namespace Avalonia.Controls.Documents /// /// Gets or sets the inlines. - /// + /// + [Content] public InlineCollection Inlines { get => GetValue(InlinesProperty); set => SetValue(InlinesProperty, value); } - public void Add(Inline inline) - { - if (Inlines is not null) - { - Inlines.Add(inline); - } - } - - public void Add(string text) - { - if (Inlines is not null) - { - Inlines.Add(text); - } - } - internal override void BuildTextRun(IList textRuns) { if (Inlines.HasComplexContent) diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index 3523cd5214..e463bc5731 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -543,9 +543,11 @@ namespace Avalonia.Controls.Presenters protected override Size ArrangeOverride(Size finalSize) { - if (finalSize.Width < TextLayout.Bounds.Width) + var textWidth = Math.Ceiling(TextLayout.Bounds.Width); + + if (finalSize.Width < textWidth) { - finalSize = finalSize.WithWidth(TextLayout.Bounds.Width); + finalSize = finalSize.WithWidth(textWidth); } if (MathUtilities.AreClose(_constraint.Width, finalSize.Width)) @@ -553,7 +555,7 @@ namespace Avalonia.Controls.Presenters return finalSize; } - _constraint = new Size(finalSize.Width, double.PositiveInfinity); + _constraint = new Size(Math.Ceiling(finalSize.Width), double.PositiveInfinity); _textLayout = null; diff --git a/src/Avalonia.Controls/RichTextBlock.cs b/src/Avalonia.Controls/RichTextBlock.cs index 16d0254f4a..859503e693 100644 --- a/src/Avalonia.Controls/RichTextBlock.cs +++ b/src/Avalonia.Controls/RichTextBlock.cs @@ -1,8 +1,14 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using Avalonia.Controls.Documents; +using Avalonia.Controls.Utils; +using Avalonia.Input; +using Avalonia.Interactivity; using Avalonia.Media; using Avalonia.Media.TextFormatting; +using Avalonia.Metadata; +using Avalonia.Utilities; namespace Avalonia.Controls { @@ -11,6 +17,38 @@ namespace Avalonia.Controls /// public class RichTextBlock : TextBlock, IInlineHost { + public static readonly StyledProperty IsTextSelectionEnabledProperty = + AvaloniaProperty.Register(nameof(IsTextSelectionEnabled), false); + + public static readonly DirectProperty CaretIndexProperty = + AvaloniaProperty.RegisterDirect( + nameof(CaretIndex), + o => o.CaretIndex, + (o, v) => o.CaretIndex = v); + + public static readonly DirectProperty SelectionStartProperty = + AvaloniaProperty.RegisterDirect( + nameof(SelectionStart), + o => o.SelectionStart, + (o, v) => o.SelectionStart = v); + + public static readonly DirectProperty SelectionEndProperty = + AvaloniaProperty.RegisterDirect( + nameof(SelectionEnd), + o => o.SelectionEnd, + (o, v) => o.SelectionEnd = v); + + public static readonly DirectProperty SelectedTextProperty = + AvaloniaProperty.RegisterDirect( + nameof(SelectedText), + o => o.SelectedText); + + public static readonly StyledProperty SelectionBrushProperty = + AvaloniaProperty.Register(nameof(SelectionBrush), Brushes.Blue); + + public static readonly StyledProperty SelectionForegroundBrushProperty = + AvaloniaProperty.Register(nameof(SelectionForegroundBrush)); + /// /// Defines the property. /// @@ -18,6 +56,17 @@ namespace Avalonia.Controls AvaloniaProperty.Register( nameof(Inlines)); + private int _caretIndex; + private int _selectionStart; + private int _selectionEnd; + + static RichTextBlock() + { + FocusableProperty.OverrideDefaultValue(typeof(RichTextBlock), true); + + AffectsRender(SelectionStartProperty, SelectionEndProperty, SelectionForegroundBrushProperty, SelectionBrushProperty); + } + public RichTextBlock() { Inlines = new InlineCollection @@ -27,31 +76,85 @@ namespace Avalonia.Controls }; } - /// - /// Gets or sets the inlines. - /// - public InlineCollection Inlines + public IBrush? SelectionBrush { - get => GetValue(InlinesProperty); - set => SetValue(InlinesProperty, value); + get => GetValue(SelectionBrushProperty); + set => SetValue(SelectionBrushProperty, value); } - public void Add(Inline inline) + public IBrush? SelectionForegroundBrush { - if (Inlines is not null) + get => GetValue(SelectionForegroundBrushProperty); + set => SetValue(SelectionForegroundBrushProperty, value); + } + + public int CaretIndex + { + get => _caretIndex; + set { - Inlines.Add(inline); + if(SetAndRaise(CaretIndexProperty, ref _caretIndex, value)) + { + SelectionStart = SelectionEnd = value; + } } } - public new void Add(string text) + public int SelectionStart { - if (Inlines is not null) + get => _selectionStart; + set { - Inlines.Add(text); + if (SetAndRaise(SelectionStartProperty, ref _selectionStart, value)) + { + RaisePropertyChanged(SelectedTextProperty, "", ""); + + if (SelectionEnd == value && CaretIndex != value) + { + CaretIndex = value; + } + } } } + public int SelectionEnd + { + get => _selectionEnd; + set + { + if(SetAndRaise(SelectionEndProperty, ref _selectionEnd, value)) + { + RaisePropertyChanged(SelectedTextProperty, "", ""); + + if (SelectionStart == value && CaretIndex != value) + { + CaretIndex = value; + } + } + } + } + + public string SelectedText + { + get => GetSelection(); + } + + public bool IsTextSelectionEnabled + { + get => GetValue(IsTextSelectionEnabledProperty); + set => SetValue(IsTextSelectionEnabledProperty, value); + } + + /// + /// Gets or sets the inlines. + /// + [Content] + public InlineCollection Inlines + { + get => GetValue(InlinesProperty); + set => SetValue(InlinesProperty, value); + } + /// /// Creates the used to render the text. /// @@ -99,6 +202,179 @@ namespace Avalonia.Controls lineHeight: LineHeight); } + public override void Render(DrawingContext context) + { + var selectionStart = SelectionStart; + var selectionEnd = SelectionEnd; + var selectionBrush = SelectionBrush; + + var selectionEnabled = IsTextSelectionEnabled; + + if (selectionEnabled && selectionStart != selectionEnd && selectionBrush != null) + { + var start = Math.Min(selectionStart, selectionEnd); + var length = Math.Max(selectionStart, selectionEnd) - start; + + var rects = TextLayout.HitTestTextRange(start, length); + + foreach (var rect in rects) + { + context.FillRectangle(selectionBrush, PixelRect.FromRect(rect, 1).ToRect(1)); + } + } + + base.Render(context); + } + + /// + /// Select all text in the TextBox + /// + public void SelectAll() + { + if (!IsTextSelectionEnabled) + { + return; + } + + var text = Inlines.Text ?? Text; + + SelectionStart = 0; + SelectionEnd = text?.Length ?? 0; + } + + /// + /// Clears the current selection/> + /// + public void ClearSelection() + { + if (!IsTextSelectionEnabled) + { + return; + } + + SelectionEnd = SelectionStart; + } + + protected override void OnLostFocus(RoutedEventArgs e) + { + base.OnLostFocus(e); + + ClearSelection(); + } + + protected override void OnPointerPressed(PointerPressedEventArgs e) + { + if (!IsTextSelectionEnabled) + { + return; + } + + var text = Inlines.Text; + var clickInfo = e.GetCurrentPoint(this); + + if (text != null && clickInfo.Properties.IsLeftButtonPressed) + { + var point = e.GetPosition(this); + + var clickToSelect = e.KeyModifiers.HasFlag(KeyModifiers.Shift); + + var hit = TextLayout.HitTestPoint(point); + + var oldIndex = CaretIndex; + var index = hit.TextPosition; + CaretIndex = index; + +#pragma warning disable CS0618 // Type or member is obsolete + switch (e.ClickCount) +#pragma warning restore CS0618 // Type or member is obsolete + { + case 1: + if (clickToSelect) + { + SelectionStart = Math.Min(oldIndex, index); + SelectionEnd = Math.Max(oldIndex, index); + } + else + { + SelectionStart = SelectionEnd = index; + } + + break; + case 2: + if (!StringUtils.IsStartOfWord(text, index)) + { + SelectionStart = StringUtils.PreviousWord(text, index); + } + + SelectionEnd = StringUtils.NextWord(text, index); + break; + case 3: + SelectAll(); + break; + } + } + + e.Pointer.Capture(this); + e.Handled = true; + } + + protected override void OnPointerMoved(PointerEventArgs e) + { + if (!IsTextSelectionEnabled) + { + return; + } + + // selection should not change during pointer move if the user right clicks + if (e.Pointer.Captured == this && e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) + { + var point = e.GetPosition(this); + + point = new Point( + MathUtilities.Clamp(point.X, 0, Math.Max(Bounds.Width - 1, 0)), + MathUtilities.Clamp(point.Y, 0, Math.Max(Bounds.Height - 1, 0))); + + var hit = TextLayout.HitTestPoint(point); + + SelectionEnd = hit.TextPosition; + } + } + + protected override void OnPointerReleased(PointerReleasedEventArgs e) + { + if (!IsTextSelectionEnabled) + { + return; + } + + if (e.Pointer.Captured != this) + { + return; + } + + if (e.InitialPressMouseButton == MouseButton.Right) + { + var point = e.GetPosition(this); + + var hit = TextLayout.HitTestPoint(point); + + var caretIndex = hit.TextPosition; + + // see if mouse clicked inside current selection + // if it did not, we change the selection to where the user clicked + var firstSelection = Math.Min(SelectionStart, SelectionEnd); + var lastSelection = Math.Max(SelectionStart, SelectionEnd); + var didClickInSelection = SelectionStart != SelectionEnd && + caretIndex >= firstSelection && caretIndex <= lastSelection; + if (!didClickInSelection) + { + _caretIndex = SelectionEnd = SelectionStart = caretIndex; + } + } + + e.Pointer.Capture(null); + } + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { base.OnPropertyChanged(change); @@ -134,6 +410,32 @@ namespace Avalonia.Controls Inlines.Text = newValue; } + private string GetSelection() + { + var text = Inlines.Text ?? Text; + + if (string.IsNullOrEmpty(text)) + { + return ""; + } + + var selectionStart = SelectionStart; + var selectionEnd = SelectionEnd; + var start = Math.Min(selectionStart, selectionEnd); + var end = Math.Max(selectionStart, selectionEnd); + + if (start == end || text.Length < end) + { + return ""; + } + + var length = Math.Max(0, end - start); + + var selectedText = text.Substring(start, length); + + return selectedText; + } + private void OnInlinesChanged(InlineCollection? oldValue, InlineCollection? newValue) { if (oldValue is not null) diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 87966e9a6f..1f891b092f 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -128,8 +128,8 @@ namespace Avalonia.Controls public static readonly StyledProperty TextDecorationsProperty = AvaloniaProperty.Register(nameof(TextDecorations)); - private string? _text; - private TextLayout? _textLayout; + protected string? _text; + protected TextLayout? _textLayout; private Size _constraint; /// @@ -572,9 +572,11 @@ namespace Avalonia.Controls protected override Size ArrangeOverride(Size finalSize) { - if(finalSize.Width < TextLayout.Bounds.Width) + var textWidth = Math.Ceiling(TextLayout.Bounds.Width); + + if(finalSize.Width < textWidth) { - finalSize = finalSize.WithWidth(TextLayout.Bounds.Width); + finalSize = finalSize.WithWidth(textWidth); } if (MathUtilities.AreClose(_constraint.Width, finalSize.Width)) @@ -586,7 +588,7 @@ namespace Avalonia.Controls var padding = LayoutHelper.RoundLayoutThickness(Padding, scale, scale); - _constraint = new Size(finalSize.Deflate(padding).Width, double.PositiveInfinity); + _constraint = new Size(Math.Ceiling(finalSize.Deflate(padding).Width), double.PositiveInfinity); _textLayout = null; diff --git a/tests/Avalonia.Base.UnitTests/Media/TextFormatting/BiDiAlgorithmTests.cs b/tests/Avalonia.Base.UnitTests/Media/TextFormatting/BiDiAlgorithmTests.cs index f8a2abc716..5ff2c0e07b 100644 --- a/tests/Avalonia.Base.UnitTests/Media/TextFormatting/BiDiAlgorithmTests.cs +++ b/tests/Avalonia.Base.UnitTests/Media/TextFormatting/BiDiAlgorithmTests.cs @@ -27,7 +27,7 @@ namespace Avalonia.Visuals.UnitTests.Media.TextFormatting private bool Run(BiDiTestData testData) { - var bidi = BidiAlgorithm.Instance.Value; + var bidi = new BidiAlgorithm(); // Run the algorithm... ArraySlice resultLevels; diff --git a/tests/Avalonia.Controls.UnitTests/RichTextBlockTests.cs b/tests/Avalonia.Controls.UnitTests/RichTextBlockTests.cs new file mode 100644 index 0000000000..eb4b88956d --- /dev/null +++ b/tests/Avalonia.Controls.UnitTests/RichTextBlockTests.cs @@ -0,0 +1,52 @@ +using Avalonia.Controls.Documents; +using Avalonia.Media; +using Avalonia.UnitTests; +using Xunit; + +namespace Avalonia.Controls.UnitTests +{ + public class RichTextBlockTests + { + [Fact] + public void Changing_InlinesCollection_Should_Invalidate_Measure() + { + using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface)) + { + var target = new RichTextBlock(); + + target.Measure(Size.Infinity); + + Assert.True(target.IsMeasureValid); + + target.Inlines.Add(new Run("Hello")); + + Assert.False(target.IsMeasureValid); + + target.Measure(Size.Infinity); + + Assert.True(target.IsMeasureValid); + } + } + + [Fact] + public void Changing_Inlines_Properties_Should_Invalidate_Measure() + { + using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface)) + { + var target = new RichTextBlock(); + + var inline = new Run("Hello"); + + target.Inlines.Add(inline); + + target.Measure(Size.Infinity); + + Assert.True(target.IsMeasureValid); + + inline.Foreground = Brushes.Green; + + Assert.False(target.IsMeasureValid); + } + } + } +} diff --git a/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs b/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs index 0ed1f8d2d0..6da011f062 100644 --- a/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs @@ -62,47 +62,5 @@ namespace Avalonia.Controls.UnitTests renderer.Verify(x => x.AddDirty(target), Times.Once); } - - [Fact] - public void Changing_InlinesCollection_Should_Invalidate_Measure() - { - using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface)) - { - var target = new TextBlock(); - - target.Measure(Size.Infinity); - - Assert.True(target.IsMeasureValid); - - target.Inlines.Add(new Run("Hello")); - - Assert.False(target.IsMeasureValid); - - target.Measure(Size.Infinity); - - Assert.True(target.IsMeasureValid); - } - } - - [Fact] - public void Changing_Inlines_Properties_Should_Invalidate_Measure() - { - using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface)) - { - var target = new TextBlock(); - - var inline = new Run("Hello"); - - target.Inlines.Add(inline); - - target.Measure(Size.Infinity); - - Assert.True(target.IsMeasureValid); - - inline.Text = "1337"; - - Assert.False(target.IsMeasureValid); - } - } } } From 8abeb76235a259b4b03ebb9a2c008231479128bc Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 22 Jun 2022 17:13:20 +0200 Subject: [PATCH 132/224] Added support for Design.PreviewWith in resource dictionaries. --- src/Avalonia.Controls/Design.cs | 22 +++++++++++++++---- .../DesignWindowLoader.cs | 22 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Controls/Design.cs b/src/Avalonia.Controls/Design.cs index 07d2918a88..80600b2276 100644 --- a/src/Avalonia.Controls/Design.cs +++ b/src/Avalonia.Controls/Design.cs @@ -1,4 +1,5 @@ +using System.Collections.Generic; using System.Runtime.CompilerServices; using Avalonia.Styling; @@ -6,6 +7,8 @@ namespace Avalonia.Controls { public static class Design { + private static Dictionary? _previewWith; + public static bool IsDesignMode { get; internal set; } public static readonly AttachedProperty HeightProperty = AvaloniaProperty @@ -47,19 +50,30 @@ namespace Avalonia.Controls return control.GetValue(DataContextProperty); } - public static readonly AttachedProperty PreviewWithProperty = AvaloniaProperty - .RegisterAttached("PreviewWith", typeof (Design)); + public static readonly AttachedProperty PreviewWithProperty = AvaloniaProperty + .RegisterAttached("PreviewWith", typeof (Design)); - public static void SetPreviewWith(AvaloniaObject target, Control control) + public static void SetPreviewWith(AvaloniaObject target, Control? control) { target.SetValue(PreviewWithProperty, control); } - public static Control GetPreviewWith(AvaloniaObject target) + public static void SetPreviewWith(ResourceDictionary target, Control? control) + { + _previewWith ??= new(); + _previewWith[target] = control; + } + + public static Control? GetPreviewWith(AvaloniaObject target) { return target.GetValue(PreviewWithProperty); } + public static Control? GetPreviewWith(ResourceDictionary target) + { + return _previewWith?[target]; + } + public static readonly AttachedProperty DesignStyleProperty = AvaloniaProperty .RegisterAttached("DesignStyle", typeof(Design)); diff --git a/src/Avalonia.DesignerSupport/DesignWindowLoader.cs b/src/Avalonia.DesignerSupport/DesignWindowLoader.cs index b009778f97..811f9c7baa 100644 --- a/src/Avalonia.DesignerSupport/DesignWindowLoader.cs +++ b/src/Avalonia.DesignerSupport/DesignWindowLoader.cs @@ -37,6 +37,7 @@ namespace Avalonia.DesignerSupport var localAsm = assemblyPath != null ? Assembly.LoadFile(Path.GetFullPath(assemblyPath)) : null; var loaded = loader.Load(stream, localAsm, null, baseUri, true); var style = loaded as IStyle; + var resources = loaded as ResourceDictionary; if (style != null) { var substitute = Design.GetPreviewWith((AvaloniaObject)style); @@ -58,6 +59,27 @@ namespace Avalonia.DesignerSupport } }; } + else if (resources != null) + { + var substitute = Design.GetPreviewWith(resources); + if (substitute != null) + { + substitute.Resources.MergedDictionaries.Add(resources); + control = substitute; + } + else + control = new StackPanel + { + Children = + { + new TextBlock {Text = "ResourceDictionaries can't be previewed without Design.PreviewWith. Add"}, + new TextBlock {Text = ""}, + new TextBlock {Text = " "}, + new TextBlock {Text = ""}, + new TextBlock {Text = "in your resource dictionary"} + } + }; + } else if (loaded is Application) control = new TextBlock {Text = "Application can't be previewed in design view"}; else From 90e0dcc9e3161cb9659ca7381c6ff98ee7e84f10 Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Thu, 23 Jun 2022 15:18:36 +0200 Subject: [PATCH 133/224] Add Copy geasture --- samples/Sandbox/MainWindow.axaml | 1 + .../Documents/InlineCollection.cs | 4 +- src/Avalonia.Controls/Documents/Span.cs | 12 +- .../Documents/TextElement.cs | 4 +- .../Primitives/AccessText.cs | 4 +- src/Avalonia.Controls/RichTextBlock.cs | 194 +++++++++++++----- src/Avalonia.Controls/TextBlock.cs | 38 ++-- .../Media/TextFormatting/BiDiClassTests.cs | 2 +- .../Styling/SetterTests.cs | 2 +- 9 files changed, 174 insertions(+), 87 deletions(-) diff --git a/samples/Sandbox/MainWindow.axaml b/samples/Sandbox/MainWindow.axaml index 806f6d37da..a834e3fef3 100644 --- a/samples/Sandbox/MainWindow.axaml +++ b/samples/Sandbox/MainWindow.axaml @@ -13,6 +13,7 @@ . + diff --git a/src/Avalonia.Controls/Documents/InlineCollection.cs b/src/Avalonia.Controls/Documents/InlineCollection.cs index 2f27ca72d0..dc688fc359 100644 --- a/src/Avalonia.Controls/Documents/InlineCollection.cs +++ b/src/Avalonia.Controls/Documents/InlineCollection.cs @@ -136,7 +136,7 @@ namespace Avalonia.Controls.Documents base.Add(new Run(_text)); } - _text = string.Empty; + _text = null; } base.Add(item); @@ -160,8 +160,6 @@ namespace Avalonia.Controls.Documents Invalidated?.Invoke(this, EventArgs.Empty); } - private void Invalidate(object? sender, EventArgs e) => Invalidate(); - private void OnParentChanged(ILogical? parent) { foreach(var child in this) diff --git a/src/Avalonia.Controls/Documents/Span.cs b/src/Avalonia.Controls/Documents/Span.cs index 98851726da..c7289dbc3f 100644 --- a/src/Avalonia.Controls/Documents/Span.cs +++ b/src/Avalonia.Controls/Documents/Span.cs @@ -67,10 +67,12 @@ namespace Avalonia.Controls.Documents inline.AppendText(stringBuilder); } } - - if (Inlines.Text is string text) + else { - stringBuilder.Append(text); + if (Inlines.Text is string text) + { + stringBuilder.Append(text); + } } } @@ -87,9 +89,9 @@ namespace Avalonia.Controls.Documents } } - internal override void OnInlinesHostChanged(IInlineHost? oldValue, IInlineHost? newValue) + internal override void OnInlineHostChanged(IInlineHost? oldValue, IInlineHost? newValue) { - base.OnInlinesHostChanged(oldValue, newValue); + base.OnInlineHostChanged(oldValue, newValue); if(Inlines is not null) { diff --git a/src/Avalonia.Controls/Documents/TextElement.cs b/src/Avalonia.Controls/Documents/TextElement.cs index e75fd87615..5bac3642ed 100644 --- a/src/Avalonia.Controls/Documents/TextElement.cs +++ b/src/Avalonia.Controls/Documents/TextElement.cs @@ -259,11 +259,11 @@ namespace Avalonia.Controls.Documents { var oldValue = _inlineHost; _inlineHost = value; - OnInlinesHostChanged(oldValue, value); + OnInlineHostChanged(oldValue, value); } } - internal virtual void OnInlinesHostChanged(IInlineHost? oldValue, IInlineHost? newValue) + internal virtual void OnInlineHostChanged(IInlineHost? oldValue, IInlineHost? newValue) { } diff --git a/src/Avalonia.Controls/Primitives/AccessText.cs b/src/Avalonia.Controls/Primitives/AccessText.cs index 87cf660cad..7e5b34acd9 100644 --- a/src/Avalonia.Controls/Primitives/AccessText.cs +++ b/src/Avalonia.Controls/Primitives/AccessText.cs @@ -79,9 +79,9 @@ namespace Avalonia.Controls.Primitives } /// - protected override TextLayout CreateTextLayout(Size constraint, string? text) + protected override TextLayout CreateTextLayout(string? text) { - return base.CreateTextLayout(constraint, RemoveAccessKeyMarker(text)); + return base.CreateTextLayout(RemoveAccessKeyMarker(text)); } /// diff --git a/src/Avalonia.Controls/RichTextBlock.cs b/src/Avalonia.Controls/RichTextBlock.cs index 859503e693..1411d715ec 100644 --- a/src/Avalonia.Controls/RichTextBlock.cs +++ b/src/Avalonia.Controls/RichTextBlock.cs @@ -1,9 +1,10 @@ using System; using System.Collections.Generic; -using System.Diagnostics; +using System.Linq; using Avalonia.Controls.Documents; using Avalonia.Controls.Utils; using Avalonia.Input; +using Avalonia.Input.Platform; using Avalonia.Interactivity; using Avalonia.Media; using Avalonia.Media.TextFormatting; @@ -56,6 +57,16 @@ namespace Avalonia.Controls AvaloniaProperty.Register( nameof(Inlines)); + public static readonly DirectProperty CanCopyProperty = + AvaloniaProperty.RegisterDirect( + nameof(CanCopy), + o => o.CanCopy); + + public static readonly RoutedEvent CopyingToClipboardEvent = + RoutedEvent.Register( + nameof(CopyingToClipboard), RoutingStrategies.Bubble); + + private bool _canCopy; private int _caretIndex; private int _selectionStart; private int _selectionEnd; @@ -75,7 +86,7 @@ namespace Avalonia.Controls InlineHost = this }; } - + public IBrush? SelectionBrush { get => GetValue(SelectionBrushProperty); @@ -156,50 +167,43 @@ namespace Avalonia.Controls } /// - /// Creates the used to render the text. + /// Property for determining if the Copy command can be executed. /// - /// The constraint of the text. - /// The text to format. - /// A object. - protected override TextLayout CreateTextLayout(Size constraint, string? text) + public bool CanCopy { - var defaultProperties = new GenericTextRunProperties( - new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), - FontSize, - TextDecorations, - Foreground); + get => _canCopy; + private set => SetAndRaise(CanCopyProperty, ref _canCopy, value); + } - var paragraphProperties = new GenericTextParagraphProperties(FlowDirection, TextAlignment, true, false, - defaultProperties, TextWrapping, LineHeight, 0); + public event EventHandler? CopyingToClipboard + { + add => AddHandler(CopyingToClipboardEvent, value); + remove => RemoveHandler(CopyingToClipboardEvent, value); + } - ITextSource textSource; + public async void Copy() + { + if (_canCopy || !IsTextSelectionEnabled) + { + return; + } - var inlines = Inlines; + var text = GetSelection(); - if (inlines is not null && inlines.HasComplexContent) + if (string.IsNullOrEmpty(text)) { - var textRuns = new List(); + return; + } - foreach (var inline in inlines) - { - inline.BuildTextRun(textRuns); - } + var eventArgs = new RoutedEventArgs(CopyingToClipboardEvent); - textSource = new InlinesTextSource(textRuns); - } - else + RaiseEvent(eventArgs); + + if (!eventArgs.Handled) { - textSource = new SimpleTextSource((text ?? "").AsMemory(), defaultProperties); + await ((IClipboard)AvaloniaLocator.Current.GetRequiredService(typeof(IClipboard))) + .SetTextAsync(text); } - - return new TextLayout( - textSource, - paragraphProperties, - TextTrimming, - constraint.Width, - constraint.Height, - maxLines: MaxLines, - lineHeight: LineHeight); } public override void Render(DrawingContext context) @@ -236,7 +240,7 @@ namespace Avalonia.Controls return; } - var text = Inlines.Text ?? Text; + var text = Text; SelectionStart = 0; SelectionEnd = text?.Length ?? 0; @@ -255,6 +259,75 @@ namespace Avalonia.Controls SelectionEnd = SelectionStart; } + + protected override string? GetText() + { + return _text ?? Inlines.Text; + } + + protected override void SetText(string? text) + { + var oldValue = _text ?? Inlines?.Text; + + if (Inlines is not null && Inlines.HasComplexContent) + { + Inlines.Text = text; + + _text = null; + } + else + { + _text = text; + } + + RaisePropertyChanged(TextProperty, oldValue, text); + } + + /// + /// Creates the used to render the text. + /// + /// A object. + protected override TextLayout CreateTextLayout(string? text) + { + var defaultProperties = new GenericTextRunProperties( + new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), + FontSize, + TextDecorations, + Foreground); + + var paragraphProperties = new GenericTextParagraphProperties(FlowDirection, TextAlignment, true, false, + defaultProperties, TextWrapping, LineHeight, 0); + + ITextSource textSource; + + var inlines = Inlines; + + if (inlines is not null && inlines.HasComplexContent) + { + var textRuns = new List(); + + foreach (var inline in inlines) + { + inline.BuildTextRun(textRuns); + } + + textSource = new InlinesTextSource(textRuns); + } + else + { + textSource = new SimpleTextSource((text ?? "").AsMemory(), defaultProperties); + } + + return new TextLayout( + textSource, + paragraphProperties, + TextTrimming, + _constraint.Width, + _constraint.Height, + maxLines: MaxLines, + lineHeight: LineHeight); + } + protected override void OnLostFocus(RoutedEventArgs e) { base.OnLostFocus(e); @@ -262,6 +335,24 @@ namespace Avalonia.Controls ClearSelection(); } + protected override void OnKeyDown(KeyEventArgs e) + { + var handled = false; + var modifiers = e.KeyModifiers; + var keymap = AvaloniaLocator.Current.GetRequiredService(); + + bool Match(List gestures) => gestures.Any(g => g.Matches(e)); + + if (Match(keymap.Copy)) + { + Copy(); + + handled = true; + } + + e.Handled = handled; + } + protected override void OnPointerPressed(PointerPressedEventArgs e) { if (!IsTextSelectionEnabled) @@ -269,20 +360,21 @@ namespace Avalonia.Controls return; } - var text = Inlines.Text; + var text = Text; var clickInfo = e.GetCurrentPoint(this); if (text != null && clickInfo.Properties.IsLeftButtonPressed) { var point = e.GetPosition(this); - var clickToSelect = e.KeyModifiers.HasFlag(KeyModifiers.Shift); - - var hit = TextLayout.HitTestPoint(point); + var clickToSelect = e.KeyModifiers.HasFlag(KeyModifiers.Shift); var oldIndex = CaretIndex; + + var hit = TextLayout.HitTestPoint(point); var index = hit.TextPosition; - CaretIndex = index; + + SetAndRaise(CaretIndexProperty, ref _caretIndex, index); #pragma warning disable CS0618 // Type or member is obsolete switch (e.ClickCount) @@ -368,7 +460,7 @@ namespace Avalonia.Controls caretIndex >= firstSelection && caretIndex <= lastSelection; if (!didClickInSelection) { - _caretIndex = SelectionEnd = SelectionStart = caretIndex; + CaretIndex = SelectionEnd = SelectionStart = caretIndex; } } @@ -389,29 +481,19 @@ namespace Avalonia.Controls } case nameof(TextProperty): { - OnTextChanged(change.OldValue as string, change.NewValue as string); + InvalidateTextLayout(); break; } } } - private void OnTextChanged(string? oldValue, string? newValue) + private string GetSelection() { - if (oldValue == newValue) - { - return; - } - - if (Inlines is null) + if (!IsTextSelectionEnabled) { - return; + return ""; } - Inlines.Text = newValue; - } - - private string GetSelection() - { var text = Inlines.Text ?? Text; if (string.IsNullOrEmpty(text)) diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 1f891b092f..2f83ee1002 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -130,7 +130,7 @@ namespace Avalonia.Controls protected string? _text; protected TextLayout? _textLayout; - private Size _constraint; + protected Size _constraint; /// /// Initializes static members of the class. @@ -149,7 +149,7 @@ namespace Avalonia.Controls { get { - return _textLayout ??= CreateTextLayout(_constraint, Text); + return _textLayout ??= CreateTextLayout(_text); } } @@ -176,11 +176,8 @@ namespace Avalonia.Controls /// public string? Text { - get => _text; - set - { - SetAndRaise(TextProperty, ref _text, value); - } + get => GetText(); + set => SetText(value); } /// @@ -302,11 +299,6 @@ namespace Avalonia.Controls set { SetValue(BaselineOffsetProperty, value); } } - public void Add(string text) - { - Text = text; - } - /// /// Reads the attached property from the given element /// @@ -481,6 +473,10 @@ namespace Avalonia.Controls control.SetValue(MaxLinesProperty, maxLines); } + public void Add(string text) + { + _text = text; + } /// /// Renders the to a drawing context. @@ -516,13 +512,21 @@ namespace Avalonia.Controls TextLayout.Draw(context, new Point(padding.Left, top)); } + protected virtual string? GetText() + { + return _text; + } + + protected virtual void SetText(string? text) + { + SetAndRaise(TextProperty, ref _text, text); + } + /// /// Creates the used to render the text. /// - /// The constraint of the text. - /// The text to format. /// A object. - protected virtual TextLayout CreateTextLayout(Size constraint, string? text) + protected virtual TextLayout CreateTextLayout(string? text) { var defaultProperties = new GenericTextRunProperties( new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), @@ -537,8 +541,8 @@ namespace Avalonia.Controls new SimpleTextSource((text ?? "").AsMemory(), defaultProperties), paragraphProperties, TextTrimming, - constraint.Width, - constraint.Height, + _constraint.Width, + _constraint.Height, maxLines: MaxLines, lineHeight: LineHeight); } diff --git a/tests/Avalonia.Base.UnitTests/Media/TextFormatting/BiDiClassTests.cs b/tests/Avalonia.Base.UnitTests/Media/TextFormatting/BiDiClassTests.cs index 1ed33e6132..f29420ff87 100644 --- a/tests/Avalonia.Base.UnitTests/Media/TextFormatting/BiDiClassTests.cs +++ b/tests/Avalonia.Base.UnitTests/Media/TextFormatting/BiDiClassTests.cs @@ -30,7 +30,7 @@ namespace Avalonia.Visuals.UnitTests.Media.TextFormatting private bool Run(BiDiClassData t) { - var bidi = BidiAlgorithm.Instance.Value; + var bidi = new BidiAlgorithm(); var bidiData = new BidiData(t.ParagraphLevel); var text = Encoding.UTF32.GetString(MemoryMarshal.Cast(t.CodePoints).ToArray()); diff --git a/tests/Avalonia.Base.UnitTests/Styling/SetterTests.cs b/tests/Avalonia.Base.UnitTests/Styling/SetterTests.cs index ed4c78aa3e..99dfc93a68 100644 --- a/tests/Avalonia.Base.UnitTests/Styling/SetterTests.cs +++ b/tests/Avalonia.Base.UnitTests/Styling/SetterTests.cs @@ -49,7 +49,7 @@ namespace Avalonia.Base.UnitTests.Styling setter.Instance(control).Start(false); - Assert.Equal("", control.Text); + Assert.Equal(null, control.Text); } [Fact] From 0289a515b38984601d36186a260c44edf5acd8be Mon Sep 17 00:00:00 2001 From: Max Katz Date: Fri, 24 Jun 2022 00:59:16 -0400 Subject: [PATCH 134/224] Add file picker interface definitions --- src/Avalonia.Base/Logging/LogArea.cs | 10 ++ .../Platform/Storage/FileIO/BclStorageFile.cs | 107 ++++++++++++++++++ .../Storage/FileIO/BclStorageFolder.cs | 88 ++++++++++++++ .../Storage/FileIO/BclStorageProvider.cs | 35 ++++++ .../Storage/FileIO/StorageProviderHelpers.cs | 40 +++++++ .../Platform/Storage/FilePickerFileType.cs | 44 +++++++ .../Platform/Storage/FilePickerFileTypes.cs | 48 ++++++++ .../Platform/Storage/FilePickerOpenOptions.cs | 29 +++++ .../Platform/Storage/FilePickerSaveOptions.cs | 39 +++++++ .../Storage/FolderPickerOpenOptions.cs | 22 ++++ .../Platform/Storage/IStorageBookmarkItem.cs | 21 ++++ .../Platform/Storage/IStorageFile.cs | 32 ++++++ .../Platform/Storage/IStorageFolder.cs | 12 ++ .../Platform/Storage/IStorageItem.cs | 53 +++++++++ .../Platform/Storage/IStorageProvider.cs | 56 +++++++++ .../Platform/Storage/StorageItemProperties.cs | 43 +++++++ .../Dialogs/IStorageProviderFactory.cs | 12 ++ .../{ => Dialogs}/ISystemDialogImpl.cs | 2 + .../Platform/Dialogs/SystemDialogImpl.cs | 74 ++++++++++++ .../ITopLevelImplWithStorageProvider.cs | 11 ++ src/Avalonia.Controls/SystemDialog.cs | 57 ++++++++++ src/Avalonia.Controls/TopLevel.cs | 9 +- 22 files changed, 843 insertions(+), 1 deletion(-) create mode 100644 src/Avalonia.Base/Platform/Storage/FileIO/BclStorageFile.cs create mode 100644 src/Avalonia.Base/Platform/Storage/FileIO/BclStorageFolder.cs create mode 100644 src/Avalonia.Base/Platform/Storage/FileIO/BclStorageProvider.cs create mode 100644 src/Avalonia.Base/Platform/Storage/FileIO/StorageProviderHelpers.cs create mode 100644 src/Avalonia.Base/Platform/Storage/FilePickerFileType.cs create mode 100644 src/Avalonia.Base/Platform/Storage/FilePickerFileTypes.cs create mode 100644 src/Avalonia.Base/Platform/Storage/FilePickerOpenOptions.cs create mode 100644 src/Avalonia.Base/Platform/Storage/FilePickerSaveOptions.cs create mode 100644 src/Avalonia.Base/Platform/Storage/FolderPickerOpenOptions.cs create mode 100644 src/Avalonia.Base/Platform/Storage/IStorageBookmarkItem.cs create mode 100644 src/Avalonia.Base/Platform/Storage/IStorageFile.cs create mode 100644 src/Avalonia.Base/Platform/Storage/IStorageFolder.cs create mode 100644 src/Avalonia.Base/Platform/Storage/IStorageItem.cs create mode 100644 src/Avalonia.Base/Platform/Storage/IStorageProvider.cs create mode 100644 src/Avalonia.Base/Platform/Storage/StorageItemProperties.cs create mode 100644 src/Avalonia.Controls/Platform/Dialogs/IStorageProviderFactory.cs rename src/Avalonia.Controls/Platform/{ => Dialogs}/ISystemDialogImpl.cs (88%) create mode 100644 src/Avalonia.Controls/Platform/Dialogs/SystemDialogImpl.cs create mode 100644 src/Avalonia.Controls/Platform/ITopLevelImplWithStorageProvider.cs diff --git a/src/Avalonia.Base/Logging/LogArea.cs b/src/Avalonia.Base/Logging/LogArea.cs index c049f9e763..98ef6d2530 100644 --- a/src/Avalonia.Base/Logging/LogArea.cs +++ b/src/Avalonia.Base/Logging/LogArea.cs @@ -44,5 +44,15 @@ namespace Avalonia.Logging /// The log event comes from X11Platform. /// public const string X11Platform = nameof(X11Platform); + + /// + /// The log event comes from AndroidPlatform. + /// + public const string AndroidPlatform = nameof(AndroidPlatform); + + /// + /// The log event comes from IOSPlatform. + /// + public const string IOSPlatform = nameof(IOSPlatform); } } diff --git a/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageFile.cs b/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageFile.cs new file mode 100644 index 0000000000..5af02219ce --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageFile.cs @@ -0,0 +1,107 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Security; +using System.Threading.Tasks; +using Avalonia.Metadata; + +namespace Avalonia.Platform.Storage.FileIO; + +[Unstable] +public class BclStorageFile : IStorageBookmarkFile +{ + private readonly FileInfo _fileInfo; + + public BclStorageFile(FileInfo fileInfo) + { + _fileInfo = fileInfo ?? throw new ArgumentNullException(nameof(fileInfo)); + } + + public bool CanOpenRead => true; + + public bool CanOpenWrite => true; + + public string Name => _fileInfo.Name; + + public virtual bool CanBookmark => true; + + public Task GetBasicPropertiesAsync() + { + var props = new StorageItemProperties(); + if (_fileInfo.Exists) + { + props = new StorageItemProperties( + (ulong)_fileInfo.Length, + _fileInfo.CreationTimeUtc, + _fileInfo.LastAccessTimeUtc); + } + return Task.FromResult(props); + } + + public Task GetParentAsync() + { + if (_fileInfo.Directory is { } directory) + { + return Task.FromResult(new BclStorageFolder(directory)); + } + return Task.FromResult(null); + } + + public Task OpenRead() + { + return Task.FromResult(_fileInfo.OpenRead()); + } + + public Task OpenWrite() + { + return Task.FromResult(_fileInfo.OpenWrite()); + } + + public virtual Task SaveBookmark() + { + return Task.FromResult(_fileInfo.FullName); + } + + public Task ReleaseBookmark() + { + // No-op + return Task.CompletedTask; + } + + public bool TryGetUri([NotNullWhen(true)] out Uri? uri) + { + try + { + if (_fileInfo.Directory is not null) + { + uri = Path.IsPathRooted(_fileInfo.FullName) ? + new Uri(new Uri("file://"), _fileInfo.FullName) : + new Uri(_fileInfo.FullName, UriKind.Relative); + return true; + } + + uri = null; + return false; + } + catch (SecurityException) + { + uri = null; + return false; + } + } + + protected virtual void Dispose(bool disposing) + { + } + + ~BclStorageFile() + { + Dispose(disposing: false); + } + + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } +} diff --git a/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageFolder.cs b/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageFolder.cs new file mode 100644 index 0000000000..7267017eaf --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageFolder.cs @@ -0,0 +1,88 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Security; +using System.Threading.Tasks; +using Avalonia.Metadata; + +namespace Avalonia.Platform.Storage.FileIO; + +[Unstable] +public class BclStorageFolder : IStorageBookmarkFolder +{ + private readonly DirectoryInfo _directoryInfo; + + public BclStorageFolder(DirectoryInfo directoryInfo) + { + _directoryInfo = directoryInfo ?? throw new ArgumentNullException(nameof(directoryInfo)); + if (!_directoryInfo.Exists) + { + throw new ArgumentException("Directory must exist", nameof(directoryInfo)); + } + } + + public string Name => _directoryInfo.Name; + + public bool CanBookmark => true; + + public Task GetBasicPropertiesAsync() + { + var props = new StorageItemProperties( + null, + _directoryInfo.CreationTimeUtc, + _directoryInfo.LastAccessTimeUtc); + return Task.FromResult(props); + } + + public Task GetParentAsync() + { + if (_directoryInfo.Parent is { } directory) + { + return Task.FromResult(new BclStorageFolder(directory)); + } + return Task.FromResult(null); + } + + public virtual Task SaveBookmark() + { + return Task.FromResult(_directoryInfo.FullName); + } + + public Task ReleaseBookmark() + { + // No-op + return Task.CompletedTask; + } + + public bool TryGetUri([NotNullWhen(true)] out Uri? uri) + { + try + { + uri = Path.IsPathRooted(_directoryInfo.FullName) ? + new Uri(new Uri("file://"), _directoryInfo.FullName) : + new Uri(_directoryInfo.FullName, UriKind.Relative); + + return true; + } + catch (SecurityException) + { + uri = null; + return false; + } + } + + protected virtual void Dispose(bool disposing) + { + } + + ~BclStorageFolder() + { + Dispose(disposing: false); + } + + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } +} diff --git a/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageProvider.cs b/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageProvider.cs new file mode 100644 index 0000000000..469388021e --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/FileIO/BclStorageProvider.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using Avalonia.Metadata; + +namespace Avalonia.Platform.Storage.FileIO; + +[Unstable] +public abstract class BclStorageProvider : IStorageProvider +{ + public abstract bool CanOpen { get; } + public abstract Task> OpenFilePickerAsync(FilePickerOpenOptions options); + + public abstract bool CanSave { get; } + public abstract Task SaveFilePickerAsync(FilePickerSaveOptions options); + + public abstract bool CanPickFolder { get; } + public abstract Task> OpenFolderPickerAsync(FolderPickerOpenOptions options); + + public virtual Task OpenFileBookmarkAsync(string bookmark) + { + var file = new FileInfo(bookmark); + return file.Exists + ? Task.FromResult(new BclStorageFile(file)) + : Task.FromResult(null); + } + + public virtual Task OpenFolderBookmarkAsync(string bookmark) + { + var folder = new DirectoryInfo(bookmark); + return folder.Exists + ? Task.FromResult(new BclStorageFolder(folder)) + : Task.FromResult(null); + } +} diff --git a/src/Avalonia.Base/Platform/Storage/FileIO/StorageProviderHelpers.cs b/src/Avalonia.Base/Platform/Storage/FileIO/StorageProviderHelpers.cs new file mode 100644 index 0000000000..f90d0a5a2f --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/FileIO/StorageProviderHelpers.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; +using System.Linq; +using Avalonia.Metadata; + +namespace Avalonia.Platform.Storage.FileIO; + +[Unstable] +public static class StorageProviderHelpers +{ + public static string NameWithExtension(string path, string? defaultExtension, FilePickerFileType? filter) + { + var name = Path.GetFileName(path); + if (name != null && !Path.HasExtension(name)) + { + if (filter?.Patterns?.Count > 0) + { + if (defaultExtension != null + && filter.Patterns.Contains(defaultExtension)) + { + return Path.ChangeExtension(path, defaultExtension.TrimStart('.')); + } + + var ext = filter.Patterns.FirstOrDefault(x => x != "*.*"); + ext = ext?.Split(new[] { "*." }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault(); + if (ext != null) + { + return Path.ChangeExtension(path, ext); + } + } + + if (defaultExtension != null) + { + return Path.ChangeExtension(path, defaultExtension); + } + } + + return path; + } +} diff --git a/src/Avalonia.Base/Platform/Storage/FilePickerFileType.cs b/src/Avalonia.Base/Platform/Storage/FilePickerFileType.cs new file mode 100644 index 0000000000..98848ac9f7 --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/FilePickerFileType.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; + +namespace Avalonia.Platform.Storage; + +/// +/// Represents a name mapped to the associated file types (extensions). +/// +public class FilePickerFileType +{ + public FilePickerFileType(string name) + { + Name = name; + } + + /// + /// File type name. + /// + public string Name { get; } + + /// + /// List of extensions in GLOB format. I.e. "*.png" or "*.*". + /// + /// + /// Used on Windows and Linux systems. + /// + public IReadOnlyList? Patterns { get; set; } + + /// + /// List of extensions in MIME format. + /// + /// + /// Used on Android, Browser and Linux systems. + /// + public IReadOnlyList? MimeTypes { get; set; } + + /// + /// List of extensions in Apple uniform format. + /// + /// + /// Used only on Apple devices. + /// See https://developer.apple.com/documentation/uniformtypeidentifiers/system_declared_uniform_type_identifiers. + /// + public IReadOnlyList? AppleUniformTypeIdentifiers { get; set; } +} diff --git a/src/Avalonia.Base/Platform/Storage/FilePickerFileTypes.cs b/src/Avalonia.Base/Platform/Storage/FilePickerFileTypes.cs new file mode 100644 index 0000000000..5da037999a --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/FilePickerFileTypes.cs @@ -0,0 +1,48 @@ +namespace Avalonia.Platform.Storage; + +/// +/// Dictionary of well known file types. +/// +public static class FilePickerFileTypes +{ + public static FilePickerFileType All { get; } = new("All") + { + Patterns = new[] { "*.*" }, + MimeTypes = new[] { "*/*" } + }; + + public static FilePickerFileType TextPlain { get; } = new("Plain Text") + { + Patterns = new[] { "*.txt" }, + AppleUniformTypeIdentifiers = new[] { "public.plain-text" }, + MimeTypes = new[] { "text/plain" } + }; + + public static FilePickerFileType ImageAll { get; } = new("All Images") + { + Patterns = new[] { "*.png", "*.jpg", "*.jpeg", "*.gif", "*.bmp" }, + AppleUniformTypeIdentifiers = new[] { "public.image" }, + MimeTypes = new[] { "image/*" } + }; + + public static FilePickerFileType ImageJpg { get; } = new("JPEG image") + { + Patterns = new[] { "*.jpg", "*.jpeg" }, + AppleUniformTypeIdentifiers = new[] { "public.jpeg" }, + MimeTypes = new[] { "image/jpeg" } + }; + + public static FilePickerFileType ImagePng { get; } = new("PNG image") + { + Patterns = new[] { "*.png" }, + AppleUniformTypeIdentifiers = new[] { "public.png" }, + MimeTypes = new[] { "image/png" } + }; + + public static FilePickerFileType Pdf { get; } = new("PDF document") + { + Patterns = new[] { "*.pdf" }, + AppleUniformTypeIdentifiers = new[] { "com.adobe.pdf" }, + MimeTypes = new[] { "application/pdf" } + }; +} diff --git a/src/Avalonia.Base/Platform/Storage/FilePickerOpenOptions.cs b/src/Avalonia.Base/Platform/Storage/FilePickerOpenOptions.cs new file mode 100644 index 0000000000..1f9202b0e7 --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/FilePickerOpenOptions.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; + +namespace Avalonia.Platform.Storage; + +/// +/// Options class for method. +/// +public class FilePickerOpenOptions +{ + /// + /// Gets or sets the text that appears in the title bar of a file dialog. + /// + public string? Title { get; set; } + + /// + /// Gets or sets the initial location where the file open picker looks for files to present to the user. + /// + public IStorageFolder? SuggestedStartLocation { get; set; } + + /// + /// Gets or sets an option indicating whether open picker allows users to select multiple files. + /// + public bool AllowMultiple { get; set; } + + /// + /// Gets or sets the collection of file types that the file open picker displays. + /// + public IReadOnlyList? FileTypeFilter { get; set; } +} \ No newline at end of file diff --git a/src/Avalonia.Base/Platform/Storage/FilePickerSaveOptions.cs b/src/Avalonia.Base/Platform/Storage/FilePickerSaveOptions.cs new file mode 100644 index 0000000000..0f4d690f7a --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/FilePickerSaveOptions.cs @@ -0,0 +1,39 @@ +using System.Collections.Generic; + +namespace Avalonia.Platform.Storage; + +/// +/// Options class for method. +/// +public class FilePickerSaveOptions +{ + /// + /// Gets or sets the text that appears in the title bar of a file dialog. + /// + public string? Title { get; set; } + + /// + /// Gets or sets the file name that the file save picker suggests to the user. + /// + public string? SuggestedFileName { get; set; } + + /// + /// Gets or sets the default extension to be used to save the file. + /// + public string? DefaultExtension { get; set; } + + /// + /// Gets or sets the initial location where the file open picker looks for files to present to the user. + /// + public IStorageFolder? SuggestedStartLocation { get; set; } + + /// + /// Gets or sets the collection of valid file types that the user can choose to assign to a file. + /// + public IReadOnlyList? FileTypeChoices { get; set; } + + /// + /// Gets or sets a value indicating whether file open picker displays a warning if the user specifies the name of a file that already exists. + /// + public bool? ShowOverwritePrompt { get; set; } +} diff --git a/src/Avalonia.Base/Platform/Storage/FolderPickerOpenOptions.cs b/src/Avalonia.Base/Platform/Storage/FolderPickerOpenOptions.cs new file mode 100644 index 0000000000..de90da30b2 --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/FolderPickerOpenOptions.cs @@ -0,0 +1,22 @@ +namespace Avalonia.Platform.Storage; + +/// +/// Options class for method. +/// +public class FolderPickerOpenOptions +{ + /// + /// Gets or sets the text that appears in the title bar of a folder dialog. + /// + public string? Title { get; set; } + + /// + /// Gets or sets an option indicating whether open picker allows users to select multiple folders. + /// + public bool AllowMultiple { get; set; } + + /// + /// Gets or sets the initial location where the file open picker looks for files to present to the user. + /// + public IStorageFolder? SuggestedStartLocation { get; set; } +} diff --git a/src/Avalonia.Base/Platform/Storage/IStorageBookmarkItem.cs b/src/Avalonia.Base/Platform/Storage/IStorageBookmarkItem.cs new file mode 100644 index 0000000000..65811b7fbd --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/IStorageBookmarkItem.cs @@ -0,0 +1,21 @@ +using System.Threading.Tasks; +using Avalonia.Metadata; + +namespace Avalonia.Platform.Storage; + +[NotClientImplementable] +public interface IStorageBookmarkItem : IStorageItem +{ + Task ReleaseBookmark(); +} + +[NotClientImplementable] +public interface IStorageBookmarkFile : IStorageFile, IStorageBookmarkItem +{ +} + +[NotClientImplementable] +public interface IStorageBookmarkFolder : IStorageFolder, IStorageBookmarkItem +{ + +} diff --git a/src/Avalonia.Base/Platform/Storage/IStorageFile.cs b/src/Avalonia.Base/Platform/Storage/IStorageFile.cs new file mode 100644 index 0000000000..2f12514e50 --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/IStorageFile.cs @@ -0,0 +1,32 @@ +using System.IO; +using System.Threading.Tasks; +using Avalonia.Metadata; + +namespace Avalonia.Platform.Storage; + +/// +/// Represents a file. Provides information about the file and its contents, and ways to manipulate them. +/// +[NotClientImplementable] +public interface IStorageFile : IStorageItem +{ + /// + /// Returns true, if file is readable. + /// + bool CanOpenRead { get; } + + /// + /// Opens a stream for read access. + /// + Task OpenRead(); + + /// + /// Returns true, if file is writeable. + /// + bool CanOpenWrite { get; } + + /// + /// Opens stream for writing to the file. + /// + Task OpenWrite(); +} \ No newline at end of file diff --git a/src/Avalonia.Base/Platform/Storage/IStorageFolder.cs b/src/Avalonia.Base/Platform/Storage/IStorageFolder.cs new file mode 100644 index 0000000000..83b316bc3b --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/IStorageFolder.cs @@ -0,0 +1,12 @@ +using Avalonia.Metadata; + +namespace Avalonia.Platform.Storage; + +/// +/// Manipulates folders and their contents, and provides information about them. +/// +[NotClientImplementable] +public interface IStorageFolder : IStorageItem +{ + +} \ No newline at end of file diff --git a/src/Avalonia.Base/Platform/Storage/IStorageItem.cs b/src/Avalonia.Base/Platform/Storage/IStorageItem.cs new file mode 100644 index 0000000000..078311a286 --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/IStorageItem.cs @@ -0,0 +1,53 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Threading.Tasks; +using Avalonia.Metadata; + +namespace Avalonia.Platform.Storage; + +/// +/// Manipulates storage items (files and folders) and their contents, and provides information about them +/// +/// +/// This interface inherits . It's recommended to dispose when it's not used anymore. +/// +[NotClientImplementable] +public interface IStorageItem : IDisposable +{ + /// + /// Gets the name of the item including the file name extension if there is one. + /// + string Name { get; } + + /// + /// Gets the full file-system path of the item, if the item has a path. + /// + /// + /// Android backend might return file path with "content:" scheme. + /// Browser and iOS backends might return relative uris. + /// + bool TryGetUri([NotNullWhen(true)] out Uri? uri); + + /// + /// Gets the basic properties of the current item. + /// + Task GetBasicPropertiesAsync(); + + /// + /// Returns true is item can be bookmarked and reused later. + /// + bool CanBookmark { get; } + + /// + /// Saves items to a bookmark. + /// + /// + /// Returns identifier of a bookmark. Can be null if OS denied request. + /// + Task SaveBookmark(); + + /// + /// Gets the parent folder of the current storage item. + /// + Task GetParentAsync(); +} diff --git a/src/Avalonia.Base/Platform/Storage/IStorageProvider.cs b/src/Avalonia.Base/Platform/Storage/IStorageProvider.cs new file mode 100644 index 0000000000..32fb148790 --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/IStorageProvider.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Avalonia.Metadata; + +namespace Avalonia.Platform.Storage; + +[NotClientImplementable] +public interface IStorageProvider +{ + /// + /// Returns true if it's possible to open file picker on the current platform. + /// + bool CanOpen { get; } + + /// + /// Opens file picker dialog. + /// + /// Array of selected or empty collection if user canceled the dialog. + Task> OpenFilePickerAsync(FilePickerOpenOptions options); + + /// + /// Returns true if it's possible to open save file picker on the current platform. + /// + bool CanSave { get; } + + /// + /// Opens save file picker dialog. + /// + /// Saved or null if user canceled the dialog. + Task SaveFilePickerAsync(FilePickerSaveOptions options); + + /// + /// Returns true if it's possible to open folder picker on the current platform. + /// + bool CanPickFolder { get; } + + /// + /// Opens folder picker dialog. + /// + /// Array of selected or empty collection if user canceled the dialog. + Task> OpenFolderPickerAsync(FolderPickerOpenOptions options); + + /// + /// Open from the bookmark ID. + /// + /// Bookmark ID. + /// Bookmarked file or null if OS denied request. + Task OpenFileBookmarkAsync(string bookmark); + + /// + /// Open from the bookmark ID. + /// + /// Bookmark ID. + /// Bookmarked folder or null if OS denied request. + Task OpenFolderBookmarkAsync(string bookmark); +} diff --git a/src/Avalonia.Base/Platform/Storage/StorageItemProperties.cs b/src/Avalonia.Base/Platform/Storage/StorageItemProperties.cs new file mode 100644 index 0000000000..41b9bfa941 --- /dev/null +++ b/src/Avalonia.Base/Platform/Storage/StorageItemProperties.cs @@ -0,0 +1,43 @@ +using System; + +namespace Avalonia.Platform.Storage; + +/// +/// Provides access to the content-related properties of an item (like a file or folder). +/// +public class StorageItemProperties +{ + public StorageItemProperties( + ulong? size = null, + DateTimeOffset? dateCreated = null, + DateTimeOffset? dateModified = null) + { + Size = size; + DateCreated = dateCreated; + DateModified = dateModified; + } + + /// + /// Gets the size of the file in bytes. + /// + /// + /// Can be null if property is not available. + /// + public ulong? Size { get; } + + /// + /// Gets the date and time that the current folder was created. + /// + /// + /// Can be null if property is not available. + /// + public DateTimeOffset? DateCreated { get; } + + /// + /// Gets the date and time of the last time the file was modified. + /// + /// + /// Can be null if property is not available. + /// + public DateTimeOffset? DateModified { get; } +} \ No newline at end of file diff --git a/src/Avalonia.Controls/Platform/Dialogs/IStorageProviderFactory.cs b/src/Avalonia.Controls/Platform/Dialogs/IStorageProviderFactory.cs new file mode 100644 index 0000000000..3eee8e848e --- /dev/null +++ b/src/Avalonia.Controls/Platform/Dialogs/IStorageProviderFactory.cs @@ -0,0 +1,12 @@ +#nullable enable +using Avalonia.Platform.Storage; + +namespace Avalonia.Controls.Platform; + +/// +/// Factory allows to register custom storage provider instead of native implementation. +/// +public interface IStorageProviderFactory +{ + IStorageProvider CreateProvider(TopLevel topLevel); +} diff --git a/src/Avalonia.Controls/Platform/ISystemDialogImpl.cs b/src/Avalonia.Controls/Platform/Dialogs/ISystemDialogImpl.cs similarity index 88% rename from src/Avalonia.Controls/Platform/ISystemDialogImpl.cs rename to src/Avalonia.Controls/Platform/Dialogs/ISystemDialogImpl.cs index 715eda5cfa..996fff6775 100644 --- a/src/Avalonia.Controls/Platform/ISystemDialogImpl.cs +++ b/src/Avalonia.Controls/Platform/Dialogs/ISystemDialogImpl.cs @@ -1,3 +1,4 @@ +using System; using System.Threading.Tasks; using Avalonia.Metadata; @@ -6,6 +7,7 @@ namespace Avalonia.Controls.Platform /// /// Defines a platform-specific system dialog implementation. /// + [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] [Unstable] public interface ISystemDialogImpl { diff --git a/src/Avalonia.Controls/Platform/Dialogs/SystemDialogImpl.cs b/src/Avalonia.Controls/Platform/Dialogs/SystemDialogImpl.cs new file mode 100644 index 0000000000..2775c53803 --- /dev/null +++ b/src/Avalonia.Controls/Platform/Dialogs/SystemDialogImpl.cs @@ -0,0 +1,74 @@ +using System; +using System.Linq; +using System.Threading.Tasks; + +#nullable enable + +namespace Avalonia.Controls.Platform +{ + /// + /// Defines a platform-specific system dialog implementation. + /// + [Obsolete] + internal class SystemDialogImpl : ISystemDialogImpl + { + public async Task ShowFileDialogAsync(FileDialog dialog, Window parent) + { + if (dialog is OpenFileDialog openDialog) + { + var filePicker = parent.StorageProvider; + if (!filePicker.CanOpen) + { + return null; + } + + var options = openDialog.ToFilePickerOpenOptions(); + + var files = await filePicker.OpenFilePickerAsync(options); + return files + .Select(file => file.TryGetUri(out var fullPath) + ? fullPath.LocalPath + : file.Name) + .ToArray(); + } + else if (dialog is SaveFileDialog saveDialog) + { + var filePicker = parent.StorageProvider; + if (!filePicker.CanSave) + { + return null; + } + + var options = saveDialog.ToFilePickerSaveOptions(); + + var file = await filePicker.SaveFilePickerAsync(options); + if (file is null) + { + return null; + } + + var filePath = file.TryGetUri(out var fullPath) + ? fullPath.LocalPath + : file.Name; + return new[] { filePath }; + } + return null; + } + + public async Task ShowFolderDialogAsync(OpenFolderDialog dialog, Window parent) + { + var filePicker = parent.StorageProvider; + if (!filePicker.CanPickFolder) + { + return null; + } + + var options = dialog.ToFolderPickerOpenOptions(); + + var folders = await filePicker.OpenFolderPickerAsync(options); + return folders + .Select(f => f.TryGetUri(out var uri) ? uri.LocalPath : null) + .FirstOrDefault(u => u is not null); + } + } +} diff --git a/src/Avalonia.Controls/Platform/ITopLevelImplWithStorageProvider.cs b/src/Avalonia.Controls/Platform/ITopLevelImplWithStorageProvider.cs new file mode 100644 index 0000000000..b42040f3c3 --- /dev/null +++ b/src/Avalonia.Controls/Platform/ITopLevelImplWithStorageProvider.cs @@ -0,0 +1,11 @@ +using Avalonia.Metadata; +using Avalonia.Platform; +using Avalonia.Platform.Storage; + +namespace Avalonia.Controls.Platform; + +[Unstable] +public interface ITopLevelImplWithStorageProvider : ITopLevelImpl +{ + public IStorageProvider StorageProvider { get; } +} diff --git a/src/Avalonia.Controls/SystemDialog.cs b/src/Avalonia.Controls/SystemDialog.cs index 093f10be51..f3fb4d9a6d 100644 --- a/src/Avalonia.Controls/SystemDialog.cs +++ b/src/Avalonia.Controls/SystemDialog.cs @@ -3,12 +3,15 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Avalonia.Controls.Platform; +using Avalonia.Platform.Storage; +using Avalonia.Platform.Storage.FileIO; namespace Avalonia.Controls { /// /// Base class for system file dialogs. /// + [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public abstract class FileDialog : FileSystemDialog { /// @@ -26,6 +29,7 @@ namespace Avalonia.Controls /// /// Base class for system file and directory dialogs. /// + [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public abstract class FileSystemDialog : SystemDialog { [Obsolete("Use Directory")] @@ -45,6 +49,7 @@ namespace Avalonia.Controls /// /// Represents a system dialog that prompts the user to select a location for saving a file. /// + [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public class SaveFileDialog : FileDialog { /// @@ -73,11 +78,27 @@ namespace Avalonia.Controls return (await service.ShowFileDialogAsync(this, parent) ?? Array.Empty()).FirstOrDefault(); } + + public FilePickerSaveOptions ToFilePickerSaveOptions() + { + return new FilePickerSaveOptions + { + SuggestedFileName = InitialFileName, + DefaultExtension = DefaultExtension, + FileTypeChoices = Filters?.Select(f => new FilePickerFileType(f.Name!) { Patterns = f.Extensions.Select(e => $"*.{e}").ToArray() }).ToArray(), + Title = Title, + SuggestedStartLocation = InitialDirectory is { } directory + ? new BclStorageFolder(new System.IO.DirectoryInfo(directory)) + : null, + ShowOverwritePrompt = ShowOverwritePrompt + }; + } } /// /// Represents a system dialog that allows the user to select one or more files to open. /// + [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public class OpenFileDialog : FileDialog { /// @@ -100,11 +121,25 @@ namespace Avalonia.Controls var service = AvaloniaLocator.Current.GetRequiredService(); return service.ShowFileDialogAsync(this, parent); } + + public FilePickerOpenOptions ToFilePickerOpenOptions() + { + return new FilePickerOpenOptions + { + AllowMultiple = AllowMultiple, + FileTypeFilter = Filters?.Select(f => new FilePickerFileType(f.Name!) { Patterns = f.Extensions.Select(e => $"*.{e}").ToArray() }).ToArray(), + Title = Title, + SuggestedStartLocation = InitialDirectory is { } directory + ? new BclStorageFolder(new System.IO.DirectoryInfo(directory)) + : null + }; + } } /// /// Represents a system dialog that allows the user to select a directory. /// + [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public class OpenFolderDialog : FileSystemDialog { [Obsolete("Use Directory")] @@ -129,14 +164,35 @@ namespace Avalonia.Controls var service = AvaloniaLocator.Current.GetRequiredService(); return service.ShowFolderDialogAsync(this, parent); } + + public FolderPickerOpenOptions ToFolderPickerOpenOptions() + { + return new FolderPickerOpenOptions + { + Title = Title, + SuggestedStartLocation = InitialDirectory is { } directory + ? new BclStorageFolder(new System.IO.DirectoryInfo(directory)) + : null + }; + } } /// /// Base class for system dialogs. /// + [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public abstract class SystemDialog { + static SystemDialog() + { + if (AvaloniaLocator.Current.GetService() is null) + { + // Register default implementation. + AvaloniaLocator.CurrentMutable.Bind().ToSingleton(); + } + } + /// /// Gets or sets the dialog title. /// @@ -146,6 +202,7 @@ namespace Avalonia.Controls /// /// Represents a filter in an or an . /// + [Obsolete("Use Window.StorageProvider API or TopLevel.StorageProvider API")] public class FileDialogFilter { /// diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 57fb82485c..d09b824958 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -11,6 +11,7 @@ using Avalonia.Logging; using Avalonia.LogicalTree; using Avalonia.Media; using Avalonia.Platform; +using Avalonia.Platform.Storage; using Avalonia.Rendering; using Avalonia.Styling; using Avalonia.Utilities; @@ -93,7 +94,8 @@ namespace Avalonia.Controls private ILayoutManager? _layoutManager; private Border? _transparencyFallbackBorder; private TargetWeakEventSubscriber? _resourcesChangesSubscriber; - + private IStorageProvider? _storageProvider; + /// /// Initializes static members of the class. /// @@ -319,6 +321,11 @@ namespace Avalonia.Controls double IRenderRoot.RenderScaling => PlatformImpl?.RenderScaling ?? 1; IStyleHost IStyleHost.StylingParent => _globalStyles!; + + public IStorageProvider StorageProvider => _storageProvider + ??= AvaloniaLocator.Current.GetService()?.CreateProvider(this) + ?? (PlatformImpl as ITopLevelImplWithStorageProvider)?.StorageProvider + ?? throw new InvalidOperationException("StorageProvider platform implementation is not available."); IRenderTarget IRenderRoot.CreateRenderTarget() => CreateRenderTarget(); From e717cce7e822f25cc4290150597410489fcbdc26 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Fri, 24 Jun 2022 01:00:07 -0400 Subject: [PATCH 135/224] Update headless implementations, managed and samples --- samples/ControlCatalog/ControlCatalog.csproj | 2 +- samples/ControlCatalog/MainView.xaml | 3 + samples/ControlCatalog/MainView.xaml.cs | 5 - samples/ControlCatalog/Pages/DialogsPage.xaml | 76 ++++-- .../ControlCatalog/Pages/DialogsPage.xaml.cs | 234 ++++++++++++++++-- .../Pages/NumericUpDownPage.xaml | 6 +- .../Pages/NumericUpDownPage.xaml.cs | 13 +- .../Remote/PreviewerWindowImpl.cs | 6 +- .../Remote/PreviewerWindowingPlatform.cs | 1 - src/Avalonia.DesignerSupport/Remote/Stubs.cs | 32 ++- src/Avalonia.Dialogs/Avalonia.Dialogs.csproj | 4 - .../ManagedFileChooserFilterViewModel.cs | 35 +-- .../ManagedFileChooserViewModel.cs | 97 +++++--- .../ManagedFileDialogExtensions.cs | 142 ++--------- .../ManagedStorageProvider.cs | 147 +++++++++++ .../AvaloniaHeadlessPlatform.cs | 1 - .../HeadlessPlatformStubs.cs | 36 ++- src/Avalonia.Headless/HeadlessWindowImpl.cs | 6 +- 18 files changed, 576 insertions(+), 270 deletions(-) create mode 100644 src/Avalonia.Dialogs/ManagedStorageProvider.cs diff --git a/samples/ControlCatalog/ControlCatalog.csproj b/samples/ControlCatalog/ControlCatalog.csproj index 903c849834..8358fb3cd4 100644 --- a/samples/ControlCatalog/ControlCatalog.csproj +++ b/samples/ControlCatalog/ControlCatalog.csproj @@ -1,6 +1,6 @@  - netstandard2.0 + netstandard2.0;net6.0 true enable diff --git a/samples/ControlCatalog/MainView.xaml b/samples/ControlCatalog/MainView.xaml index d8dc3bad2d..b6ce59f15b 100644 --- a/samples/ControlCatalog/MainView.xaml +++ b/samples/ControlCatalog/MainView.xaml @@ -69,6 +69,9 @@ + + + diff --git a/samples/ControlCatalog/MainView.xaml.cs b/samples/ControlCatalog/MainView.xaml.cs index d675324d9f..47d11738bc 100644 --- a/samples/ControlCatalog/MainView.xaml.cs +++ b/samples/ControlCatalog/MainView.xaml.cs @@ -24,11 +24,6 @@ namespace ControlCatalog { IList tabItems = ((IList)sideBar.Items); tabItems.Add(new TabItem() - { - Header = "Dialogs", - Content = new DialogsPage() - }); - tabItems.Add(new TabItem() { Header = "Screens", Content = new ScreenPage() diff --git a/samples/ControlCatalog/Pages/DialogsPage.xaml b/samples/ControlCatalog/Pages/DialogsPage.xaml index 8a835867b3..cc23ef796a 100644 --- a/samples/ControlCatalog/Pages/DialogsPage.xaml +++ b/samples/ControlCatalog/Pages/DialogsPage.xaml @@ -1,29 +1,57 @@ - - - Use filters - - - - - + + - - + - - - - - - - + + + + + + + + + + + + + + Use filters + + + Force managed dialog + Open multiple + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/ControlCatalog/Pages/DialogsPage.xaml.cs b/samples/ControlCatalog/Pages/DialogsPage.xaml.cs index efa30c2741..f7b6db1255 100644 --- a/samples/ControlCatalog/Pages/DialogsPage.xaml.cs +++ b/samples/ControlCatalog/Pages/DialogsPage.xaml.cs @@ -1,13 +1,21 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Threading.Tasks; +using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Presenters; using Avalonia.Dialogs; using Avalonia.Layout; using Avalonia.Markup.Xaml; -#pragma warning disable 4014 +using Avalonia.Platform.Storage; +using Avalonia.Platform.Storage.FileIO; + +#pragma warning disable CS0618 // Type or member is obsolete +#nullable enable + namespace ControlCatalog.Pages { public class DialogsPage : UserControl @@ -18,13 +26,16 @@ namespace ControlCatalog.Pages var results = this.Get("PickerLastResults"); var resultsVisible = this.Get("PickerLastResultsVisible"); + var bookmarkContainer = this.Get("BookmarkContainer"); + var openedFileContent = this.Get("OpenedFileContent"); + var openMultiple = this.Get("OpenMultiple"); - string? lastSelectedDirectory = null; + IStorageFolder? lastSelectedDirectory = null; - List? GetFilters() + List GetFilters() { if (this.Get("UseFilters").IsChecked != true) - return null; + return new List(); return new List { new FileDialogFilter @@ -39,12 +50,23 @@ namespace ControlCatalog.Pages }; } + List? GetFileTypes() + { + if (this.Get("UseFilters").IsChecked != true) + return null; + return new List + { + FilePickerFileTypes.All, + FilePickerFileTypes.TextPlain + }; + } + this.Get