Skip to content

Commit a9d683b

Browse files
kekekeksdanwalmsley
authored andcommitted
Introduced RawPointerPoint for usage with IntermediatePoints (#7581)
Introduced RawPointerPoint for usage with IntermediatePoints
1 parent 8b3b654 commit a9d683b

File tree

4 files changed

+72
-13
lines changed

4 files changed

+72
-13
lines changed

src/Avalonia.Input/MouseDevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ private bool MouseDown(IMouseDevice device, ulong timestamp, IInputElement root,
264264
}
265265

266266
private bool MouseMove(IMouseDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties properties,
267-
KeyModifiers inputModifiers, IReadOnlyList<Point>? intermediatePoints)
267+
KeyModifiers inputModifiers, Lazy<IReadOnlyList<RawPointerPoint>?>? intermediatePoints)
268268
{
269269
device = device ?? throw new ArgumentNullException(nameof(device));
270270
root = root ?? throw new ArgumentNullException(nameof(root));

src/Avalonia.Input/PointerEventArgs.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class PointerEventArgs : RoutedEventArgs
1111
private readonly IVisual? _rootVisual;
1212
private readonly Point _rootVisualPosition;
1313
private readonly PointerPointProperties _properties;
14-
private readonly IReadOnlyList<Point>? _previousPoints;
14+
private Lazy<IReadOnlyList<RawPointerPoint>?>? _previousPoints;
1515

1616
public PointerEventArgs(RoutedEvent routedEvent,
1717
IInteractive? source,
@@ -38,7 +38,7 @@ public PointerEventArgs(RoutedEvent routedEvent,
3838
ulong timestamp,
3939
PointerPointProperties properties,
4040
KeyModifiers modifiers,
41-
IReadOnlyList<Point>? previousPoints)
41+
Lazy<IReadOnlyList<RawPointerPoint>?>? previousPoints)
4242
: this(routedEvent, source, pointer, rootVisual, rootVisualPosition, timestamp, properties, modifiers)
4343
{
4444
_previousPoints = previousPoints;
@@ -121,13 +121,14 @@ public PointerPoint GetCurrentPoint(IVisual? relativeTo)
121121
/// <returns></returns>
122122
public IReadOnlyList<PointerPoint> GetIntermediatePoints(IVisual? relativeTo)
123123
{
124-
if (_previousPoints == null || _previousPoints.Count == 0)
124+
var previousPoints = _previousPoints?.Value;
125+
if (previousPoints == null || previousPoints.Count == 0)
125126
return new[] { GetCurrentPoint(relativeTo) };
126-
var points = new PointerPoint[_previousPoints.Count + 1];
127-
for (var c = 0; c < _previousPoints.Count; c++)
127+
var points = new PointerPoint[previousPoints.Count + 1];
128+
for (var c = 0; c < previousPoints.Count; c++)
128129
{
129-
var pt = _previousPoints[c];
130-
points[c] = new PointerPoint(Pointer, GetPosition(pt, relativeTo), _properties);
130+
var pt = previousPoints[c];
131+
points[c] = new PointerPoint(Pointer, GetPosition(pt.Position, relativeTo), _properties);
131132
}
132133

133134
points[points.Length - 1] = GetCurrentPoint(relativeTo);

src/Avalonia.Input/Raw/RawPointerEventArgs.cs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public enum RawPointerEventType
3030
/// </summary>
3131
public class RawPointerEventArgs : RawInputEventArgs
3232
{
33+
private RawPointerPoint _point;
34+
3335
/// <summary>
3436
/// Initializes a new instance of the <see cref="RawPointerEventArgs"/> class.
3537
/// </summary>
@@ -55,11 +57,50 @@ public RawPointerEventArgs(
5557
Type = type;
5658
InputModifiers = inputModifiers;
5759
}
60+
61+
/// <summary>
62+
/// Initializes a new instance of the <see cref="RawPointerEventArgs"/> class.
63+
/// </summary>
64+
/// <param name="device">The associated device.</param>
65+
/// <param name="timestamp">The event timestamp.</param>
66+
/// <param name="root">The root from which the event originates.</param>
67+
/// <param name="type">The type of the event.</param>
68+
/// <param name="point">The point properties and position, in client DIPs.</param>
69+
/// <param name="inputModifiers">The input modifiers.</param>
70+
public RawPointerEventArgs(
71+
IInputDevice device,
72+
ulong timestamp,
73+
IInputRoot root,
74+
RawPointerEventType type,
75+
RawPointerPoint point,
76+
RawInputModifiers inputModifiers)
77+
: base(device, timestamp, root)
78+
{
79+
Contract.Requires<ArgumentNullException>(device != null);
80+
Contract.Requires<ArgumentNullException>(root != null);
81+
82+
Point = point;
83+
Type = type;
84+
InputModifiers = inputModifiers;
85+
}
86+
87+
/// <summary>
88+
/// Gets the pointer properties and position, in client DIPs.
89+
/// </summary>
90+
public RawPointerPoint Point
91+
{
92+
get => _point;
93+
set => _point = value;
94+
}
5895

5996
/// <summary>
6097
/// Gets the mouse position, in client DIPs.
6198
/// </summary>
62-
public Point Position { get; set; }
99+
public Point Position
100+
{
101+
get => _point.Position;
102+
set => _point.Position = value;
103+
}
63104

64105
/// <summary>
65106
/// Gets the type of the event.
@@ -75,6 +116,19 @@ public RawPointerEventArgs(
75116
/// Points that were traversed by a pointer since the previous relevant event,
76117
/// only valid for Move and TouchUpdate
77118
/// </summary>
78-
public IReadOnlyList<Point>? IntermediatePoints { get; set; }
119+
public Lazy<IReadOnlyList<RawPointerPoint>?>? IntermediatePoints { get; set; }
120+
}
121+
122+
public struct RawPointerPoint
123+
{
124+
/// <summary>
125+
/// Pointer position, in client DIPs.
126+
/// </summary>
127+
public Point Position { get; set; }
128+
129+
public RawPointerPoint()
130+
{
131+
Position = default;
132+
}
79133
}
80134
}

src/Shared/RawEventGrouping.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void DispatchFromQueue()
5353

5454
_eventCallback?.Invoke(ev);
5555

56-
if (ev is RawPointerEventArgs { IntermediatePoints: PooledList<Point> list })
56+
if (ev is RawPointerEventArgs { IntermediatePoints.Value: PooledList<RawPointerPoint> list })
5757
list.Dispose();
5858

5959
if (Dispatcher.UIThread.HasJobsWithPriority(DispatcherPriority.Input + 1))
@@ -110,10 +110,14 @@ args is RawPointerEventArgs pointerEvent
110110
AddToQueue(args);
111111
}
112112

113+
private static IReadOnlyList<RawPointerPoint> GetPooledList() => new PooledList<RawPointerPoint>();
114+
private static readonly Func<IReadOnlyList<RawPointerPoint>> s_getPooledListDelegate = GetPooledList;
115+
113116
private static void MergeEvents(RawPointerEventArgs last, RawPointerEventArgs current)
114117
{
115-
last.IntermediatePoints ??= new PooledList<Point>();
116-
((PooledList<Point>)last.IntermediatePoints).Add(last.Position);
118+
119+
last.IntermediatePoints ??= new Lazy<IReadOnlyList<RawPointerPoint>?>(s_getPooledListDelegate);
120+
((PooledList<RawPointerPoint>)last.IntermediatePoints.Value!).Add(new RawPointerPoint { Position = last.Position });
117121
last.Position = current.Position;
118122
last.Timestamp = current.Timestamp;
119123
last.InputModifiers = current.InputModifiers;

0 commit comments

Comments
 (0)