forked from FrozenAssassine/TextControlBox-WinUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCursorHelper.cs
More file actions
52 lines (44 loc) · 2.29 KB
/
CursorHelper.cs
File metadata and controls
52 lines (44 loc) · 2.29 KB
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
using Microsoft.Graphics.Canvas.Text;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using TextControlBoxNS.Core;
using TextControlBoxNS.Core.Renderer;
using TextControlBoxNS.Core.Text;
using TextControlBoxNS.Extensions;
using Windows.Foundation;
namespace TextControlBoxNS.Helper;
internal class CursorHelper
{
public static int GetCursorLineFromPoint(TextRenderer textRenderer, Point point)
{
//Calculate the relative linenumber, where the pointer was pressed at
int linenumber = (int)((point.Y - textRenderer.VerticalDrawOffset) / textRenderer.SingleLineHeight);
linenumber += textRenderer.NumberOfStartLine;
return Math.Clamp(linenumber, 0, textRenderer.NumberOfStartLine + textRenderer.NumberOfRenderedLines - 1);
}
public static int GetCharacterPositionFromPoint(CurrentLineManager currentLineManager, CanvasTextLayout textLayout, Point cursorPosition, float marginLeft)
{
if (currentLineManager.GetCurrentLineText() == null || textLayout == null)
return 0;
textLayout.HitTest(
(float)cursorPosition.X - marginLeft, 0,
out var textLayoutRegion);
return textLayoutRegion.CharacterIndex;
}
//Return the position in pixels of the cursor in the current line
public static float GetCursorPositionInLine(CanvasTextLayout currentLineTextLayout, CursorPosition cursorPosition, float xOffset)
{
if (currentLineTextLayout == null)
return 0;
return currentLineTextLayout.GetCaretPosition(cursorPosition.CharacterPosition < 0 ? 0 : cursorPosition.CharacterPosition, false).X + xOffset;
}
public static void UpdateCursorPosFromPoint(CanvasControl canvasText, CurrentLineManager currentLineManager, TextRenderer textRenderer, ScrollManager scrollManager, Point point, CursorPosition cursorPos)
{
//Apply an offset to the cursorposition
point.X += textRenderer.SingleLineHeight / 4;
point.Y -= textRenderer.SingleLineHeight / 4;
cursorPos.LineNumber = GetCursorLineFromPoint(textRenderer, point);
textRenderer.UpdateCurrentLineTextLayout(canvasText);
cursorPos.CharacterPosition = GetCharacterPositionFromPoint(currentLineManager, textRenderer.CurrentLineTextLayout, point, (float)-scrollManager.HorizontalScroll);
}
}