Skip to content

Commit bc3cdd5

Browse files
authored
Introduced RawPointerPoint for usage with IntermediatePoints (#7581)
Introduced RawPointerPoint for usage with IntermediatePoints
1 parent ec52383 commit bc3cdd5

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
@@ -273,7 +273,7 @@ private bool MouseDown(IMouseDevice device, ulong timestamp, IInputElement root,
273273
}
274274

275275
private bool MouseMove(IMouseDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties properties,
276-
KeyModifiers inputModifiers, IReadOnlyList<Point>? intermediatePoints)
276+
KeyModifiers inputModifiers, Lazy<IReadOnlyList<RawPointerPoint>?>? intermediatePoints)
277277
{
278278
device = device ?? throw new ArgumentNullException(nameof(device));
279279
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
@@ -33,6 +33,8 @@ public enum RawPointerEventType
3333
/// </summary>
3434
public class RawPointerEventArgs : RawInputEventArgs
3535
{
36+
private RawPointerPoint _point;
37+
3638
/// <summary>
3739
/// Initializes a new instance of the <see cref="RawPointerEventArgs"/> class.
3840
/// </summary>
@@ -58,11 +60,50 @@ public RawPointerEventArgs(
5860
Type = type;
5961
InputModifiers = inputModifiers;
6062
}
63+
64+
/// <summary>
65+
/// Initializes a new instance of the <see cref="RawPointerEventArgs"/> class.
66+
/// </summary>
67+
/// <param name="device">The associated device.</param>
68+
/// <param name="timestamp">The event timestamp.</param>
69+
/// <param name="root">The root from which the event originates.</param>
70+
/// <param name="type">The type of the event.</param>
71+
/// <param name="point">The point properties and position, in client DIPs.</param>
72+
/// <param name="inputModifiers">The input modifiers.</param>
73+
public RawPointerEventArgs(
74+
IInputDevice device,
75+
ulong timestamp,
76+
IInputRoot root,
77+
RawPointerEventType type,
78+
RawPointerPoint point,
79+
RawInputModifiers inputModifiers)
80+
: base(device, timestamp, root)
81+
{
82+
Contract.Requires<ArgumentNullException>(device != null);
83+
Contract.Requires<ArgumentNullException>(root != null);
84+
85+
Point = point;
86+
Type = type;
87+
InputModifiers = inputModifiers;
88+
}
89+
90+
/// <summary>
91+
/// Gets the pointer properties and position, in client DIPs.
92+
/// </summary>
93+
public RawPointerPoint Point
94+
{
95+
get => _point;
96+
set => _point = value;
97+
}
6198

6299
/// <summary>
63100
/// Gets the mouse position, in client DIPs.
64101
/// </summary>
65-
public Point Position { get; set; }
102+
public Point Position
103+
{
104+
get => _point.Position;
105+
set => _point.Position = value;
106+
}
66107

67108
/// <summary>
68109
/// Gets the type of the event.
@@ -78,6 +119,19 @@ public RawPointerEventArgs(
78119
/// Points that were traversed by a pointer since the previous relevant event,
79120
/// only valid for Move and TouchUpdate
80121
/// </summary>
81-
public IReadOnlyList<Point>? IntermediatePoints { get; set; }
122+
public Lazy<IReadOnlyList<RawPointerPoint>?>? IntermediatePoints { get; set; }
123+
}
124+
125+
public struct RawPointerPoint
126+
{
127+
/// <summary>
128+
/// Pointer position, in client DIPs.
129+
/// </summary>
130+
public Point Position { get; set; }
131+
132+
public RawPointerPoint()
133+
{
134+
Position = default;
135+
}
82136
}
83137
}

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)