-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathScriptSyntaxOverlay.cs
More file actions
88 lines (73 loc) · 3.13 KB
/
ScriptSyntaxOverlay.cs
File metadata and controls
88 lines (73 loc) · 3.13 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
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
/*
Copyright 2026 University of Toronto
This file is part of XTMF2.
XTMF2 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
XTMF2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with XTMF2. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Globalization;
using System.Text;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
namespace XTMF2.GUI.Controls;
/// <summary>
/// A transparent, non-interactive overlay control that paints syntax-highlighted
/// tokens over the scripted-parameter <see cref="Avalonia.Controls.TextBox"/>. It is added to
/// <see cref="Control.VisualChildren"/> after the TextBox so it renders on top of it.
/// </summary>
internal sealed class ScriptSyntaxOverlay : Control
{
private static readonly Typeface OverlayTypeface = new("Segoe UI, Arial, sans-serif");
public (string text, IBrush brush)[] Tokens { get; set; } = Array.Empty<(string, IBrush)>();
/// <summary>
/// Scaled font size to use when drawing; updated by <see cref="ModelSystemCanvas.ArrangeOverride"/>
/// on every layout pass so the text matches the current zoom level.
/// </summary>
public double FontSize { get; set; } = 10.0;
public ScriptSyntaxOverlay()
{
IsHitTestVisible = false;
IsVisible = false;
}
public override void Render(DrawingContext ctx)
{
if (Tokens.Length == 0) return;
// 4 px left-pad matches the TextBox inner padding so the first character
// lines up with the native cursor position.
const double leftPad = 4.0;
// Build the full expression string from all token segments so that
// Avalonia measures it in one pass. Per-token width accumulation is
// unreliable: FormattedText.Width excludes trailing whitespace, so any
// whitespace-only token advances x by 0 and every subsequent token is
// shifted left. Drawing as a single FormattedText with SetForegroundBrush
// span colouring avoids the issue entirely.
var sb = new StringBuilder();
foreach (var (seg, _) in Tokens)
sb.Append(seg);
string fullText = sb.ToString();
var ft = new FormattedText(
fullText,
CultureInfo.InvariantCulture,
FlowDirection.LeftToRight,
OverlayTypeface,
FontSize,
Brushes.White); // default; overridden per span below
int offset = 0;
foreach (var (seg, brush) in Tokens)
{
ft.SetForegroundBrush(brush, offset, seg.Length);
offset += seg.Length;
}
double ty = (Bounds.Height - ft.Height) / 2.0;
ctx.DrawText(ft, new Point(leftPad, ty));
}
}