-
Notifications
You must be signed in to change notification settings - Fork 688
/
PointerRoutedEventArgs.macOS.cs
203 lines (177 loc) · 6.28 KB
/
PointerRoutedEventArgs.macOS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Windows.Devices.Input;
using AppKit;
using Windows.System;
using Foundation;
using Windows.UI.Input;
using CoreGraphics;
namespace Windows.UI.Xaml.Input
{
partial class PointerRoutedEventArgs
{
private const int TabletPointEventSubtype = 1;
private const int TabletProximityEventSubtype = 2;
private const int LeftMouseButtonMask = 1;
private const int RightMouseButtonMask = 2;
private readonly NSEvent _nativeEvent;
private readonly NSSet _nativeTouches;
internal PointerRoutedEventArgs(NSSet touches, NSEvent nativeEvent, UIElement source) : this()
{
_nativeEvent = nativeEvent;
_nativeTouches = touches;
var pointerDeviceType = GetPointerDeviceType(nativeEvent);
var pointerId = (uint)0;
if (pointerDeviceType == PointerDeviceType.Pen)
{
pointerId = (uint)nativeEvent.PointingDeviceID();
}
var isInContact = GetIsInContact(nativeEvent);
FrameId = ToFrameId(_nativeEvent.Timestamp);
Pointer = new Pointer(pointerId, pointerDeviceType, isInContact, isInRange: true);
KeyModifiers = GetVirtualKeyModifiers(nativeEvent);
OriginalSource = source;
CanBubbleNatively = true;
}
public PointerPoint GetCurrentPoint(UIElement relativeTo)
{
var device = PointerDevice.For(PointerDeviceType.Mouse);
var rawPosition = _nativeEvent.LocationInWindow;
var position = relativeTo != null ?
relativeTo.ConvertPointFromView(_nativeEvent.LocationInWindow, null) :
new CGPoint(rawPosition.X, Window.Current.Bounds.Height - rawPosition.Y); //flip Y - uses lower-left as origin and IsFlipped does not apply here
var properties = new PointerPointProperties()
{
IsInRange = true,
IsPrimary = true,
IsLeftButtonPressed = ((int)NSEvent.CurrentPressedMouseButtons & LeftMouseButtonMask) == LeftMouseButtonMask,
IsRightButtonPressed = ((int)NSEvent.CurrentPressedMouseButtons & RightMouseButtonMask) == RightMouseButtonMask,
};
if (Pointer.PointerDeviceType == PointerDeviceType.Pen)
{
properties.XTilt = (float)_nativeEvent.Tilt.X;
properties.YTilt = (float)_nativeEvent.Tilt.Y;
properties.Pressure = (float)_nativeEvent.Pressure;
}
return new PointerPoint(
FrameId,
ToTimeStamp(_nativeEvent.Timestamp),
device,
Pointer.PointerId,
rawPosition,
position,
Pointer.IsInContact,
properties);
}
#region Misc static helpers
private static long? _bootTime;
private static bool GetIsInContact(NSEvent nativeEvent)
{
return
nativeEvent.Type == NSEventType.LeftMouseDown ||
nativeEvent.Type == NSEventType.LeftMouseDragged ||
nativeEvent.Type == NSEventType.RightMouseDown ||
nativeEvent.Type == NSEventType.RightMouseDragged ||
nativeEvent.Type == NSEventType.OtherMouseDown ||
nativeEvent.Type == NSEventType.OtherMouseDragged;
}
private static PointerDeviceType GetPointerDeviceType(NSEvent nativeEvent)
{
if (nativeEvent.Type == NSEventType.DirectTouch)
{
return PointerDeviceType.Touch;
}
if (IsTabletPointingEvent(nativeEvent))
{
return PointerDeviceType.Pen;
}
return PointerDeviceType.Mouse;
}
private VirtualKeyModifiers GetVirtualKeyModifiers(NSEvent nativeEvent)
{
var modifiers = VirtualKeyModifiers.None;
if (nativeEvent.ModifierFlags.HasFlag(NSEventModifierMask.AlphaShiftKeyMask) ||
nativeEvent.ModifierFlags.HasFlag(NSEventModifierMask.ShiftKeyMask))
{
modifiers |= VirtualKeyModifiers.Shift;
}
if (nativeEvent.ModifierFlags.HasFlag(NSEventModifierMask.AlternateKeyMask))
{
modifiers |= VirtualKeyModifiers.Menu;
}
if(nativeEvent.ModifierFlags.HasFlag(NSEventModifierMask.CommandKeyMask))
{
modifiers |= VirtualKeyModifiers.Windows;
}
if (nativeEvent.ModifierFlags.HasFlag(NSEventModifierMask.ControlKeyMask))
{
modifiers |= VirtualKeyModifiers.Control;
}
return modifiers;
}
private static ulong ToTimeStamp(double timestamp)
{
if (!_bootTime.HasValue)
{
_bootTime = DateTime.UtcNow.Ticks - (long)(TimeSpan.TicksPerSecond * new NSProcessInfo().SystemUptime);
}
return (ulong)_bootTime.Value + (ulong)(TimeSpan.TicksPerSecond * timestamp);
}
private static uint ToFrameId(double timestamp)
{
// The precision of the frameId is 10 frame per ms ... which should be enough
return (uint)(timestamp * 1000.0 * 10.0);
}
/// <summary>
/// Taken from <see cref="https://github.com/xamarin/xamarin-macios/blob/bc492585d137d8c3d3a2ffc827db3cdaae3cc869/src/AppKit/NSEvent.cs#L127" />
/// </summary>
/// <param name="nativeEvent">Native event</param>
/// <returns>Value indicating whether the event is recognized as a "mouse" event.</returns>
private static bool IsMouseEvent(NSEvent nativeEvent)
{
switch (nativeEvent.Type)
{
case NSEventType.LeftMouseDown:
case NSEventType.LeftMouseUp:
case NSEventType.RightMouseDown:
case NSEventType.RightMouseUp:
case NSEventType.MouseMoved:
case NSEventType.LeftMouseDragged:
case NSEventType.RightMouseDragged:
case NSEventType.MouseEntered:
case NSEventType.MouseExited:
case NSEventType.OtherMouseDown:
case NSEventType.OtherMouseUp:
case NSEventType.OtherMouseDragged:
return true;
default:
return false;
}
}
/// <summary>
/// Inspiration from <see cref="https://github.com/xamarin/xamarin-macios/blob/bc492585d137d8c3d3a2ffc827db3cdaae3cc869/src/AppKit/NSEvent.cs#L148"/>
/// with some modifications.
/// </summary>
/// <param name="nativeEvent">Native event</param>
/// <returns>Value indicating whether the event is in fact coming from a tablet device.</returns>
private static bool IsTabletPointingEvent(NSEvent nativeEvent)
{
//limitation - mouse entered event currently throws for Subtype
//(selector not working, although it should, according to docs)
if (IsMouseEvent(nativeEvent) &&
nativeEvent.Type != NSEventType.MouseEntered &&
nativeEvent.Type != NSEventType.MouseExited)
{
//Xamarin debugger proxy for NSEvent incorrectly says Subtype
//works only for Custom events, but that is not the case
return
nativeEvent.Subtype == TabletPointEventSubtype ||
nativeEvent.Subtype == TabletProximityEventSubtype;
}
return nativeEvent.Type == NSEventType.TabletPoint;
}
#endregion
}
}