-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathModelSystemCanvas.ParamEditing.cs
More file actions
769 lines (700 loc) · 32 KB
/
Copy pathModelSystemCanvas.ParamEditing.cs
File metadata and controls
769 lines (700 loc) · 32 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
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.Collections.Generic;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Styling;
using Avalonia.VisualTree;
using XTMF2.GUI.ViewModels;
namespace XTMF2.GUI.Controls;
partial class ModelSystemCanvas
{
private void UnsubscribeInlineEditorScroll()
{
if (_inlineEditorSv is not null && _inlineEditorSvHandler is not null)
_inlineEditorSv.PropertyChanged -= _inlineEditorSvHandler;
_inlineEditorSv = null;
_inlineEditorSvHandler = null;
_scriptOverlay.HorizontalScrollOffset = 0;
}
/// <summary>
/// Returns the effective enum <see cref="Type"/> for a parameter node, or <c>null</c> if the
/// node's inner type is not (or does not implement) an enum-returning function.
/// </summary>
private static Type? GetEffectiveEnumType(NodeViewModel node)
{
var nodeType = node.UnderlyingNode.Type;
if (nodeType is null || !nodeType.IsGenericType) return null;
var innerType = nodeType.GetGenericArguments().FirstOrDefault();
if (innerType is null) return null;
if (innerType.IsEnum) return innerType;
// Walk interfaces: look for IFunction<EnumT>.
var candidates = innerType.IsInterface
? new[] { innerType }.Concat(innerType.GetInterfaces())
: (IEnumerable<Type>)innerType.GetInterfaces();
foreach (var iface in candidates)
{
if (iface.IsGenericType && iface.GetGenericTypeDefinition() == typeof(IFunction<>))
{
var arg = iface.GetGenericArguments()[0];
if (arg.IsEnum) return arg;
}
}
return null;
}
private void BeginParamEdit(NodeViewModel node, double rowX = -1, double rowY = -1, double rowW = -1, ICanvasElement? parentElement = null, object? hook = null)
{
HideVarDropdown();
_editingParamNode = node;
_editingParamEditorX = rowX >= 0 ? rowX : node.X;
_editingParamEditorY = rowY >= 0 ? rowY : node.Y + NodeHeaderHeight;
// Width must be positive for ArrangeOverride to position the inline editor.
_editingParamEditorW = rowW > 0 ? rowW : NodeRenderWidth(node);
_editingParamParentElement = parentElement;
_editingParamHook = hook;
var enumType = GetEffectiveEnumType(node);
_editingParamIsEnum = enumType is not null && !node.IsScriptedParameter;
if (_editingParamIsEnum)
{
// Populate the ComboBox with enum member names and pre-select the current value.
// Guard with _enumEditorLoading so SelectionChanged / DropDownClosed don't fire
// prematurely while we're setting ItemsSource and SelectedItem.
_enumEditorLoading = true;
var names = Enum.GetNames(enumType!);
_inlineEnumEditor.ItemsSource = names;
var current = node.ParameterValueRepresentation;
_inlineEnumEditor.SelectedItem = names.Contains(current) ? current
: (names.Length > 0 ? names[0] : null);
_enumEditorLoading = false;
_inlineEditor.IsVisible = false;
_inlineEnumEditor.IsVisible = true;
InvalidateMeasure();
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
{
_inlineEnumEditor.Focus();
_inlineEnumEditor.IsDropDownOpen = true;
}, Avalonia.Threading.DispatcherPriority.Render);
return;
}
_inlineEnumEditor.IsVisible = false;
_inlineEditor.Text = node.ParameterValueRepresentation;
bool isLight = Application.Current?.ActualThemeVariant == ThemeVariant.Light;
// For scripted parameters the text is rendered by Render() with syntax colours;
// make the TextBox itself transparent so the coloured tokens show through.
if (node.IsScriptedParameter)
{
_inlineEditor.Foreground = Brushes.Transparent;
// Set background to light grey for light mode, dark blue for dark mode
_inlineEditor.Background = isLight
? new SolidColorBrush(Color.FromRgb(0xE8, 0xE8, 0xE8))
: new SolidColorBrush(Color.FromRgb(0x18, 0x28, 0x38));
// Use a dark caret in light theme and bright yellow in dark theme.
_inlineEditor.CaretBrush = isLight
? new SolidColorBrush(Color.FromRgb(0x1C, 0x24, 0x33))
: new SolidColorBrush(Color.FromRgb(0xFF, 0xFF, 0x00));
_scriptTokens = TokenizeScript(node.ParameterValueRepresentation);
_scriptOverlay.Tokens = _scriptTokens;
_scriptOverlay.IsVisible = true;
// Subscribe to the TextBox's internal ScrollViewer so the overlay shifts
// horizontally in lockstep with the TextBox after every caret move or
// text change (the scroll happens during layout, after TextChanged fires).
UnsubscribeInlineEditorScroll();
// The internal SV may not exist until after the first layout pass, so
// we post the subscription to run once the visual tree is populated.
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
{
var sv = _inlineEditor.GetVisualDescendants()
.OfType<ScrollViewer>()
.FirstOrDefault();
if (sv is not null)
{
_inlineEditorSvHandler = (_, args) =>
{
if (args.Property == ScrollViewer.OffsetProperty)
{
_scriptOverlay.HorizontalScrollOffset = sv.Offset.X;
_scriptOverlay.InvalidateVisual();
}
};
_inlineEditorSv = sv;
sv.PropertyChanged += _inlineEditorSvHandler;
}
}, Avalonia.Threading.DispatcherPriority.Loaded);
}
else
{
_inlineEditor.Foreground = isLight ? ParamValueTextBrushL : ParamValueTextBrush;
// Set background to light grey for light mode, dark blue for dark mode
_inlineEditor.Background = isLight
? new SolidColorBrush(Color.FromRgb(0xE8, 0xE8, 0xE8))
: new SolidColorBrush(Color.FromRgb(0x18, 0x28, 0x38));
// Use bright yellow caret for both themes for maximum visibility
// Use a dark caret in light theme and bright yellow in dark theme.
_inlineEditor.CaretBrush = isLight
? new SolidColorBrush(Color.FromRgb(0x1C, 0x24, 0x33))
: new SolidColorBrush(Color.FromRgb(0xFF, 0xFF, 0x00));
_scriptTokens = Array.Empty<(string, IBrush)>();
_scriptOverlay.Tokens = _scriptTokens;
_scriptOverlay.IsVisible = false;
}
_inlineEditor.IsVisible = true;
// Re-layout so ArrangeOverride positions the TextBox at the right row.
InvalidateMeasure();
// Focus + select-all after the layout pass completes.
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
{
_inlineEditor.Focus();
_inlineEditor.SelectAll();
}, Avalonia.Threading.DispatcherPriority.Render);
}
/// <summary>Commits the current editor text as the new parameter value.</summary>
/// <remarks>
/// For ScriptedParameter nodes the value is validated before the editor is closed.
/// If the save fails the editor remains open so the user can correct the expression,
/// and an error toast is shown instead.
/// </remarks>
private void CommitParamEdit()
{
if (_commitParamEditInProgress) return;
_commitParamEditInProgress = true;
try
{
HideVarDropdown();
if (_editingParamNode is null) return;
var node = _editingParamNode;
// Clear tracking fields after capturing node
_editingParamParentElement = null;
_editingParamHook = null;
var value = _editingParamIsEnum
? (_inlineEnumEditor.SelectedItem as string ?? string.Empty)
: (_inlineEditor.Text ?? string.Empty);
// Attempt to save. For ScriptedParameter this validates the expression first.
if (!node.SetParameterValue(value, out var error))
{
// Save failed – keep the editor open, restore focus, show the error.
_vm?.ShowToast(error?.Message ?? "Failed to set parameter value.",
isError: true, durationMs: 5000);
Avalonia.Threading.Dispatcher.UIThread.Post(
() => _inlineEditor.Focus(),
Avalonia.Threading.DispatcherPriority.Input);
return;
}
// Save succeeded – close the editor.
UnsubscribeInlineEditorScroll();
_editingParamNode = null;
_editingParamIsEnum = false;
_enumEditorLoading = false;
_inlineEditor.IsVisible = false;
_inlineEnumEditor.IsVisible = false;
_inlineEditor.Foreground = ParamValueTextBrush;
_inlineEditor.Background = new SolidColorBrush(Color.FromRgb(0x18, 0x28, 0x38));
_inlineEditor.CaretBrush = null;
_scriptTokens = Array.Empty<(string, IBrush)>();
_scriptOverlay.Tokens = _scriptTokens;
_scriptOverlay.IsVisible = false;
InvalidateAndMeasure();
}
finally
{
_commitParamEditInProgress = false;
}
}
/// <summary>Discards the current edit without saving.</summary>
private void CancelParamEdit()
{
HideVarDropdown();
UnsubscribeInlineEditorScroll();
_editingParamNode = null;
_editingParamParentElement = null;
_editingParamHook = null;
_editingParamIsEnum = false;
_enumEditorLoading = false;
_inlineEditor.IsVisible = false;
_inlineEnumEditor.IsVisible = false;
_inlineEditor.Foreground = ParamValueTextBrush;
_inlineEditor.Background = new SolidColorBrush(Color.FromRgb(0x18, 0x28, 0x38));
_inlineEditor.CaretBrush = null;
_scriptTokens = Array.Empty<(string, IBrush)>();
_scriptOverlay.Tokens = _scriptTokens;
_scriptOverlay.IsVisible = false;
InvalidateAndMeasure();
Focus();
}
// ── Inline enum editor handlers ───────────────────────────────────────
private void OnInlineEnumEditorDropDownClosed(object? sender, EventArgs e)
{
// Fires when the user picks an item or presses Escape to dismiss.
// Escape is handled by OnInlineEnumEditorKeyDown first (which sets _enumEditorLoading
// before closing the dropdown), so the guard below prevents a commit on cancel.
if (_enumEditorLoading || _commitParamEditInProgress) return;
if (_editingParamNode is not null && _editingParamIsEnum
&& _inlineEnumEditor.SelectedItem is not null)
{
CommitParamEdit();
}
}
private void OnInlineEnumEditorLostFocus(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
// Don't commit if loading, committing, or if the dropdown popup just took focus.
if (_enumEditorLoading || _commitParamEditInProgress) return;
if (_editingParamNode is not null && _editingParamIsEnum && !_inlineEnumEditor.IsDropDownOpen)
CommitParamEdit();
}
private void OnInlineEnumEditorKeyDown(object? sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
// Set loading flag so the DropDownClosed event that fires when we close
// the dropdown programmatically does not trigger a commit.
_enumEditorLoading = true;
_inlineEnumEditor.IsDropDownOpen = false;
_enumEditorLoading = false;
CancelParamEdit();
e.Handled = true;
}
}
private void OnInlineEditorKeyDown(object? sender, KeyEventArgs e)
{
// ── Variable autocomplete dropdown navigation ─────────────────────
if (_varDropdownVisible)
{
int count = _varDropdownStack.Children.Count;
if (e.Key == Key.Down)
{
_varSelectedIndex = Math.Min(_varSelectedIndex + 1, count - 1);
UpdateDropdownHighlight();
e.Handled = true;
return;
}
if (e.Key == Key.Up)
{
_varSelectedIndex = Math.Max(_varSelectedIndex - 1, 0);
UpdateDropdownHighlight();
e.Handled = true;
return;
}
if (e.Key == Key.Tab)
{
SelectCurrentDropdownItem();
e.Handled = true;
return;
}
if (e.Key is Key.Return or Key.Enter)
{
// Complete with the highlighted item; do NOT commit the whole edit.
SelectCurrentDropdownItem();
e.Handled = true;
return;
}
if (e.Key == Key.Escape)
{
HideVarDropdown();
e.Handled = true;
return;
}
}
// ── Standard inline-editor keys ───────────────────────────────────
if (e.Key is Key.Return or Key.Enter)
{
CommitParamEdit();
Focus();
e.Handled = true;
}
else if (e.Key == Key.Escape)
{
CancelParamEdit();
e.Handled = true;
}
}
// ── Variable autocomplete helpers ─────────────────────────────────────
// ── Script syntax tokenizer ───────────────────────────────────────────
/// <summary>
/// Breaks <paramref name="text"/> into coloured segments for display in the
/// scripted-parameter inline editor.
/// <list type="bullet">
/// <item>Known model-system variables → <see cref="ScriptVarKnownBrush"/> (green)</item>
/// <item>Unrecognised identifiers → <see cref="ScriptVarUnknownBrush"/> (red)</item>
/// <item>Numeric literals → <see cref="ScriptNumberBrush"/> (light-blue)</item>
/// <item>String literals → <see cref="ScriptStringBrush"/> (orange)</item>
/// <item><c>true</c> / <c>false</c> → <see cref="ScriptKeywordBrush"/> (gold)</item>
/// <item>Operators & punctuation → <see cref="ScriptOperatorBrush"/> (steel-blue)</item>
/// <item>Whitespace → <see cref="ParamValueTextBrush"/> (neutral)</item>
/// </list>
/// </summary>
private (string text, IBrush brush)[] TokenizeScript(string text)
{
if (_vm is null || string.IsNullOrEmpty(text))
return [];
var knownNames = new HashSet<string>(
_vm.ModelSystemVariables.Select(v => v.Name)
.Concat(_vm.LocalVariables.Select(v => v.Name)),
StringComparer.OrdinalIgnoreCase);
List<(string, IBrush)> tokens = [];
int i = 0;
while (i < text.Length)
{
char c = text[i];
// ── String literal ─────────────────────────────────────────────
if (c == '"')
{
int start = i++;
while (i < text.Length && text[i] != '"') i++;
if (i < text.Length) i++; // consume closing quote
tokens.Add((text[start..i], _isLight ? ScriptStringBrushL : ScriptStringBrush));
continue;
}
// ── Whitespace ─────────────────────────────────────────────────
// Emit whitespace as its own invisible token so it does not bleed
// into the operator brush and the overlay advances correctly.
if (c == ' ' || c == '\t')
{
int start = i;
while (i < text.Length && (text[i] == ' ' || text[i] == '\t')) i++;
tokens.Add((text[start..i], Brushes.Transparent));
continue;
}
// ── Identifier / keyword ───────────────────────────────────────
if (char.IsLetter(c) || c == '_')
{
int start = i;
while (i < text.Length && (char.IsLetterOrDigit(text[i]) || text[i] == '_')) i++;
// Greedily extend the token by consuming "<spaces><word>" segments
// when the resulting substring matches a known variable name that
// contains spaces (e.g. "Home Based Work").
int extended = i;
while (extended < text.Length && text[extended] == ' ')
{
int spaceEnd = extended;
while (spaceEnd < text.Length && text[spaceEnd] == ' ') spaceEnd++;
if (spaceEnd >= text.Length ||
(!char.IsLetterOrDigit(text[spaceEnd]) && text[spaceEnd] != '_'))
break;
int wordEnd = spaceEnd;
while (wordEnd < text.Length &&
(char.IsLetterOrDigit(text[wordEnd]) || text[wordEnd] == '_'))
wordEnd++;
if (knownNames.Contains(text[start..wordEnd]))
{
i = wordEnd;
extended = wordEnd;
}
else
{
break;
}
}
var word = text[start..i];
IBrush brush = word switch
{
"true" or "false" => _isLight ? ScriptKeywordBrushL : ScriptKeywordBrush,
_ => knownNames.Contains(word)
? (_isLight ? ScriptVarKnownBrushL : ScriptVarKnownBrush)
: (_isLight ? ScriptVarUnknownBrushL : ScriptVarUnknownBrush),
};
tokens.Add((word, brush));
continue;
}
// ── Numeric literal ──────────────────────────────────────────
if (char.IsDigit(c))
{
int start = i;
while (i < text.Length && (char.IsDigit(text[i]) || text[i] == '.')) i++;
tokens.Add((text[start..i], _isLight ? ScriptNumberBrushL : ScriptNumberBrush));
continue;
}
// ── Operator / punctuation (single or double char) ────────────────
{
int start = i++;
// Absorb two-char operators: &&, ||, ==, !=, >=, <=
if (i < text.Length && (
(c == '&' && text[i] == '&') ||
(c == '|' && text[i] == '|') ||
(c == '=' && text[i] == '=') ||
(c == '!' && text[i] == '=') ||
(c == '>' && text[i] == '=') ||
(c == '<' && text[i] == '=')))
{
i++;
}
tokens.Add((text[start..i], _isLight ? ScriptOperatorBrushL : ScriptOperatorBrush));
}
}
return tokens.ToArray();
}
/// <summary>
/// Returns <c>true</c> for characters that terminate a variable token
/// in a scripted-parameter expression.
/// </summary>
private static bool IsExpressionSpecialChar(char c) =>
c is '+' or '-' or '*' or '/' or '^' or '?' or ':'
or '&' or '|' or '<' or '>' or '=' or '!' or '(' or ')' or '"';
/// <summary>
/// Called whenever the inline-editor text changes. When editing a
/// <see cref="NodeViewModel.IsScriptedParameter"/> node, extracts the
/// token at the caret and populates (or hides) the autocomplete dropdown.
/// </summary>
private void OnInlineEditorTextChanged(object? sender, TextChangedEventArgs e)
{
// ── Syntax-highlight tokens for scripted params ─────────────────────────
if (_editingParamNode is { IsScriptedParameter: true })
{
_scriptTokens = TokenizeScript(_inlineEditor.Text ?? string.Empty);
_scriptOverlay.Tokens = _scriptTokens;
// Offset is kept current by the _inlineEditorScrollSub observable subscription;
// just redraw with the already-known offset.
_scriptOverlay.InvalidateVisual();
}
else
{
_scriptTokens = [];
_scriptOverlay.Tokens = _scriptTokens;
}
// ── Variable autocomplete dropdown ──────────────────────────────────
if (_editingParamNode is null || !_editingParamNode.IsScriptedParameter || _vm is null)
{
HideVarDropdown();
return;
}
var text = _inlineEditor.Text ?? string.Empty;
var caret = Math.Clamp(_inlineEditor.CaretIndex, 0, text.Length);
// Walk backwards from the caret to find the start of the current token.
int tokenStart = caret;
while (tokenStart > 0)
{
char ch = text[tokenStart - 1];
if (IsExpressionSpecialChar(ch))
{
break;
}
tokenStart--;
}
var token = text[tokenStart..caret];
// Strip any leading whitespace that the backward walk included (e.g. a
// space immediately after an operator). Adjust _varTokenStart so that
// CompleteVariable() replaces only the real identifier text.
int leadingSpaces = token.Length - token.TrimStart().Length;
tokenStart += leadingSpaces;
token = token.TrimStart();
// Also strip trailing whitespace (caret resting just after a space).
token = token.TrimEnd();
_varTokenStart = tokenStart;
if (token.Length == 0)
{
HideVarDropdown();
return;
}
bool isLight = Application.Current?.ActualThemeVariant == ThemeVariant.Light;
var matches = _vm.ModelSystemVariables
.Concat(_vm.LocalVariables)
.Where(v => v.Name.Contains(token, StringComparison.OrdinalIgnoreCase))
.Select(v => v.Name)
.Distinct(StringComparer.OrdinalIgnoreCase)
.OrderBy(n => n, StringComparer.OrdinalIgnoreCase)
.Take(MaxVarDropdownItems)
.ToList();
if (matches.Count == 0)
{
HideVarDropdown();
return;
}
var normalBg = isLight
? new SolidColorBrush(Color.FromRgb(0xF8, 0xF9, 0xFF))
: new SolidColorBrush(Color.FromRgb(0x1E, 0x2E, 0x3E));
IBrush normalFg = isLight ? Brushes.Black : Brushes.White;
_varDropdownBorder.Background = normalBg;
_varDropdownBorder.BorderBrush = isLight
? new SolidColorBrush(Color.FromRgb(0x88, 0xAA, 0xCC))
: new SolidColorBrush(Color.FromRgb(0x44, 0x88, 0xCC));
_varDropdownStack.Children.Clear();
foreach (var name in matches)
{
var captured = name;
var tb = new TextBlock
{
Text = captured,
Padding = new Thickness(8, 3, 8, 3),
Foreground = normalFg,
Background = normalBg,
FontSize = HookFontSize,
FontFamily = new Avalonia.Media.FontFamily("Segoe UI, Arial, sans-serif"),
};
tb.PointerEntered += (_, _) =>
{
tb.Background = new SolidColorBrush(Color.FromRgb(0x20, 0x60, 0xA0));
tb.Foreground = Brushes.White;
};
tb.PointerExited += (_, _) =>
{
// UpdateDropdownHighlight will repaint based on _varSelectedIndex.
UpdateDropdownHighlight();
};
tb.PointerPressed += (_, pe) =>
{
CompleteVariable(captured);
pe.Handled = true;
};
_varDropdownStack.Children.Add(tb);
}
_varSelectedIndex = 0;
UpdateDropdownHighlight();
_varDropdownBorder.IsVisible = true;
_varDropdownVisible = true;
InvalidateMeasure();
}
/// <summary>Repaints the selection highlight so only the row at <see cref="_varSelectedIndex"/> is highlighted.</summary>
private void UpdateDropdownHighlight()
{
bool isLight = Application.Current?.ActualThemeVariant == ThemeVariant.Light;
var normalBg = isLight
? new SolidColorBrush(Color.FromRgb(0xF8, 0xF9, 0xFF))
: new SolidColorBrush(Color.FromRgb(0x1E, 0x2E, 0x3E));
var selBg = new SolidColorBrush(Color.FromRgb(0x20, 0x60, 0xA0));
IBrush normalFg = isLight ? Brushes.Black : Brushes.White;
for (int i = 0; i < _varDropdownStack.Children.Count; i++)
{
if (_varDropdownStack.Children[i] is not TextBlock tb) continue;
bool sel = i == _varSelectedIndex;
tb.Background = sel ? selBg : normalBg;
tb.Foreground = sel ? Brushes.White : normalFg;
}
}
/// <summary>Completes the current token with the currently highlighted dropdown item.</summary>
private void SelectCurrentDropdownItem()
{
int count = _varDropdownStack.Children.Count;
if (_varSelectedIndex < 0 || _varSelectedIndex >= count)
{
return;
}
if (_varDropdownStack.Children[_varSelectedIndex] is TextBlock tb && tb.Text is { } name)
{
CompleteVariable(name);
}
}
/// <summary>
/// Replaces the token starting at <see cref="_varTokenStart"/> up to the current
/// caret position with <paramref name="name"/>, then closes the dropdown.
/// The editor remains open so the user can continue typing.
/// </summary>
private void CompleteVariable(string name)
{
var text = _inlineEditor.Text ?? string.Empty;
var caret = Math.Clamp(_inlineEditor.CaretIndex, 0, text.Length);
_inlineEditor.Text = text[.._varTokenStart] + name + text[caret..];
_inlineEditor.CaretIndex = _varTokenStart + name.Length;
HideVarDropdown();
// Clicking a TextBlock item shifted focus to the canvas; return it to the
// inline editor so the user can keep typing without clicking again.
Avalonia.Threading.Dispatcher.UIThread.Post(
() => _inlineEditor.Focus(),
Avalonia.Threading.DispatcherPriority.Input);
}
/// <summary>Hides and clears the variable autocomplete dropdown.</summary>
private void HideVarDropdown()
{
if (!_varDropdownVisible) return;
_varDropdownVisible = false;
_varDropdownBorder.IsVisible = false;
_varDropdownStack.Children.Clear();
InvalidateMeasure();
}
private void OnInlineEditorLostFocus(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
// When the variable autocomplete dropdown is visible the user may have clicked
// a suggestion item. TextBlock items are non-focusable, so focus falls to the
// canvas. We must NOT commit here; CompleteVariable() keeps the session open
// and will immediately return focus to the inline editor.
if (_varDropdownVisible) return;
// Don't commit if we're in the middle of a drag/resize operation (focus naturally shifts to canvas during pointer operations).
if (_inDragOrResize) return;
// Commit on focus loss (e.g. user clicks away to another element).
if (_editingParamNode is not null)
CommitParamEdit();
}
/// <summary>
/// Updates the stored inline editor position and size fields to match the current
/// position and size of the element being edited. This is called during element
/// move/resize operations to keep the inline editor textbox synchronized.
/// </summary>
private void SyncEditingElementPositions()
{
// Sync parameter editor position/size if editing
if (_editingParamNode is not null)
{
_editingParamEditorX = _editingParamNode.X;
_editingParamEditorY = _editingParamNode.Y + NodeHeaderHeight;
_editingParamEditorW = NodeRenderWidth(_editingParamNode);
}
// Sync name editor position/size if editing
if (_editingNameElement is not null)
{
if (_editingNameElement is NodeViewModel nvm)
{
_nameEditorX = nvm.X;
_nameEditorY = nvm.Y;
_nameEditorW = NodeRenderWidth(nvm);
_nameEditorH = NodeHeaderHeight;
}
else if (_editingNameElement is StartViewModel svm)
{
_nameEditorX = svm.X - StartViewModel.Radius;
_nameEditorY = svm.Y + StartViewModel.Radius + 2;
_nameEditorW = svm.Diameter + 20;
_nameEditorH = NodeHeaderHeight;
}
else if (_editingNameElement is FunctionTemplateViewModel ftvm)
{
_nameEditorX = ftvm.X;
_nameEditorY = ftvm.Y;
_nameEditorW = ftvm.Width;
_nameEditorH = FtHeaderHeight;
}
else if (_editingNameElement is FunctionInstanceViewModel fivm)
{
_nameEditorX = fivm.X;
_nameEditorY = fivm.Y;
_nameEditorW = fivm.Width;
_nameEditorH = FtHeaderHeight;
}
else if (_editingNameElement is FunctionParameterViewModel fpvm)
{
_nameEditorX = fpvm.X;
_nameEditorY = fpvm.Y;
_nameEditorW = fpvm.Width;
_nameEditorH = FtHeaderHeight;
}
}
// Sync comment editor position/size if editing
if (_editingCommentBlock is not null)
{
_editingCommentEditorX = _editingCommentBlock.X;
_editingCommentEditorY = _editingCommentBlock.Y + CommentHeaderHeight;
_editingCommentEditorW = _editingCommentBlock.Width;
_editingCommentEditorH = _editingCommentBlock.Height - CommentHeaderHeight;
}
// Sync comment header editor position/size if editing
if (_editingCommentHeaderBlock is not null)
{
_editingCommentHeaderEditorX = _editingCommentHeaderBlock.X;
_editingCommentHeaderEditorY = _editingCommentHeaderBlock.Y;
_editingCommentHeaderEditorW = _editingCommentHeaderBlock.Width - CommentFoldSize;
_editingCommentHeaderEditorH = CommentHeaderHeight;
}
}
}