generated from jingkecn/MyScript.InteractiveInk.Starter.UWP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditorExtensions.cs
118 lines (107 loc) · 5.45 KB
/
EditorExtensions.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
using System;
using System.Diagnostics;
using System.Linq;
using Windows.ApplicationModel.DataTransfer;
using Windows.Devices.Input;
using Windows.Foundation;
using Windows.UI.Input;
using MyScript.IInk;
using MyScript.InteractiveInk.Annotations;
namespace MyScript.InteractiveInk.UI.Extensions
{
public static partial class EditorExtensions
{
public static void CopyToClipboard(this Editor source, ContentBlock block = null, MimeType type = MimeType.TEXT)
{
var data = new DataPackage {RequestedOperation = DataPackageOperation.Copy};
var content = source.Export_(block, type);
data.SetText(content);
Clipboard.SetContent(data);
}
public static void Typeset(this Editor source, [CanBeNull] ContentBlock block = null)
{
var states = source.GetSupportedTargetConversionStates(block);
if (!states.Any())
{
return;
}
source.Convert(block, states.First());
}
public static void Typeset(this Editor source, float x, float y)
{
source.Typeset(source.HitBlock(x, y));
}
public static void Typeset(this Editor source, Point position)
{
source.Typeset((float)position.X, (float)position.Y);
}
}
/// <summary>
/// Sends pointer events.
/// Please be careful with the timestamp: the editor accepts the pointer timestamp in milliseconds, whereas the UWP
/// pointer timestamp is in microseconds, therefore, a conversion between these two units is a must.
/// Otherwise, you would encounter the following error when handling pointer events on the text document part:
/// - ink rejected: stroke is too long.
/// This is because, on the text document part, the interactive-ink SDK checks the interval time between the first
/// point (pointer down) and the last point (pointer up / cancel) of a single stroke, and raises the error if the
/// interval time is too high (> 15s).
/// </summary>
public static partial class EditorExtensions
{
public static void PointerCancel(this Editor source, PointerPoint point)
{
Debug.WriteLine($"---------- {typeof(EditorExtensions).Name}.{nameof(PointerCancel)} ----------");
Debug.WriteLine($"{nameof(point)}: ({nameof(point.PointerId)}={point.PointerId};" +
$"{nameof(point.Position)}={point.Position};" +
$"{nameof(point.Timestamp)}={point.Timestamp.FromMicrosecondsToMilliseconds()})");
source.PointerCancel((int)point.PointerId);
}
public static void PointerDown(this Editor source, PointerPoint point)
{
Debug.WriteLine($"---------- {typeof(EditorExtensions).Name}.{nameof(PointerDown)} ----------");
Debug.WriteLine($"{nameof(point)}: ({nameof(point.PointerId)}={point.PointerId};" +
$"{nameof(point.Position)}={point.Position};" +
$"{nameof(point.Timestamp)}={point.Timestamp.FromMicrosecondsToMilliseconds()})");
source.PointerDown((float)point.Position.X, (float)point.Position.Y,
(long)point.Timestamp.FromMicrosecondsToMilliseconds(), point.Properties.Pressure,
point.PointerDevice.PointerDeviceType.ToNative(), (int)point.PointerId);
}
public static void PointerMove(this Editor source, PointerPoint point)
{
if (!point.IsInContact)
{
return;
}
Debug.WriteLine($"---------- {typeof(EditorExtensions).Name}.{nameof(PointerMove)} ----------");
Debug.WriteLine($"{nameof(point)}: ({nameof(point.PointerId)}={point.PointerId};" +
$"{nameof(point.Position)}={point.Position};" +
$"{nameof(point.Timestamp)}={point.Timestamp.FromMicrosecondsToMilliseconds()})");
source.PointerMove((float)point.Position.X, (float)point.Position.Y,
(long)point.Timestamp.FromMicrosecondsToMilliseconds(), point.Properties.Pressure,
point.PointerDevice.PointerDeviceType.ToNative(), (int)point.PointerId);
}
public static void PointerUp(this Editor source, PointerPoint point)
{
Debug.WriteLine($"---------- {typeof(EditorExtensions).Name}.{nameof(PointerUp)} ----------");
Debug.WriteLine($"{nameof(point)}: ({nameof(point.PointerId)}={point.PointerId};" +
$"{nameof(point.Position)}={point.Position};" +
$"{nameof(point.Timestamp)}={point.Timestamp.FromMicrosecondsToMilliseconds()})");
source.PointerUp((float)point.Position.X, (float)point.Position.Y,
(long)point.Timestamp.FromMicrosecondsToMilliseconds(), point.Properties.Pressure,
point.PointerDevice.PointerDeviceType.ToNative(), (int)point.PointerId);
}
}
public static partial class EditorExtensions
{
public static PointerType ToNative(this PointerDeviceType source)
{
return source switch
{
PointerDeviceType.Touch => PointerType.TOUCH,
PointerDeviceType.Pen => PointerType.PEN,
PointerDeviceType.Mouse => PointerType.TOUCH,
_ => throw new ArgumentOutOfRangeException(nameof(source), source, null)
};
}
}
}