-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
RichSuggestBox.Helpers.cs
123 lines (109 loc) · 5.38 KB
/
RichSuggestBox.Helpers.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using Windows.Foundation;
using Windows.Graphics.Display;
using Windows.UI.Text;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// The RichSuggestBox control extends <see cref="RichEditBox"/> control that suggests and embeds custom data in a rich document.
/// </summary>
public partial class RichSuggestBox
{
private static bool IsElementOnScreen(FrameworkElement element, double offsetX = 0, double offsetY = 0)
{
// DisplayInformation only works in UWP. No alternative to get DisplayInformation.ScreenHeightInRawPixels
// Tracking issues:
// https://github.com/microsoft/WindowsAppSDK/issues/114
// https://github.com/microsoft/microsoft-ui-xaml/issues/4228
// TODO: Remove when DisplayInformation.ScreenHeightInRawPixels alternative is available
return true;
#pragma warning disable CS0162 // Unreachable code detected
if (Window.Current == null)
{
return !ControlHelpers.IsXamlRootAvailable || element.XamlRoot.IsHostVisible;
}
var toWindow = element.TransformToVisual(null);
var windowBounds = Window.Current.Bounds;
var elementBounds = new Rect(offsetX, offsetY, element.ActualWidth, element.ActualHeight);
elementBounds = toWindow.TransformBounds(elementBounds);
elementBounds.X += windowBounds.X;
elementBounds.Y += windowBounds.Y;
var displayInfo = DisplayInformation.GetForCurrentView();
var scaleFactor = displayInfo.RawPixelsPerViewPixel;
var displayHeight = displayInfo.ScreenHeightInRawPixels;
return elementBounds.Top * scaleFactor >= 0 && elementBounds.Bottom * scaleFactor <= displayHeight;
#pragma warning restore CS0162 // Unreachable code detected
}
private static bool IsElementInsideWindow(FrameworkElement element, double offsetX = 0, double offsetY = 0)
{
var toWindow = element.TransformToVisual(null);
var windowBounds = ControlHelpers.IsXamlRootAvailable
? element.XamlRoot.Size.ToRect()
: ApplicationView.GetForCurrentView().VisibleBounds;
windowBounds = new Rect(0, 0, windowBounds.Width, windowBounds.Height);
var elementBounds = new Rect(offsetX, offsetY, element.ActualWidth, element.ActualHeight);
elementBounds = toWindow.TransformBounds(elementBounds);
elementBounds.Intersect(windowBounds);
return elementBounds.Height >= element.ActualHeight;
}
private static string EnforcePrefixesRequirements(string value)
{
return string.IsNullOrEmpty(value) ? string.Empty : string.Concat(value.Where(char.IsPunctuation));
}
/// <summary>
/// Pad range with Zero-Width-Spaces.
/// </summary>
/// <param name="range">Range to pad.</param>
/// <param name="format">Character format to apply to the padding.</param>
private static void PadRange(ITextRange range, ITextCharacterFormat format)
{
var startPosition = range.StartPosition;
var endPosition = range.EndPosition + 1;
var clone = range.GetClone();
clone.Collapse(true);
clone.SetText(TextSetOptions.Unhide, "\u200B");
clone.CharacterFormat.SetClone(format);
clone.SetRange(endPosition, endPosition);
clone.SetText(TextSetOptions.Unhide, "\u200B");
clone.CharacterFormat.SetClone(format);
range.SetRange(startPosition, endPosition + 1);
}
private static void ForEachLinkInDocument(ITextDocument document, Action<ITextRange> action)
{
var range = document.GetRange(0, 0);
range.SetIndex(TextRangeUnit.Character, -1, false);
// Handle link at the very end of the document where GetIndex fails to detect
range.Expand(TextRangeUnit.Link);
if (!string.IsNullOrEmpty(range.Link))
{
action?.Invoke(range);
}
var nextIndex = range.GetIndex(TextRangeUnit.Link);
while (nextIndex != 0 && nextIndex != 1)
{
range.Move(TextRangeUnit.Link, -1);
var linkRange = range.GetClone();
linkRange.Expand(TextRangeUnit.Link);
// Adjacent links have the same index. Manually check each link with Collapse and Expand.
var previousStart = linkRange.StartPosition;
var hasAdjacentToken = true;
while (hasAdjacentToken)
{
action?.Invoke(linkRange);
linkRange.Collapse(false);
linkRange.Expand(TextRangeUnit.Link);
hasAdjacentToken = !string.IsNullOrEmpty(linkRange.Link) && linkRange.StartPosition != previousStart;
previousStart = linkRange.StartPosition;
}
nextIndex = range.GetIndex(TextRangeUnit.Link);
}
}
}
}