-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextView.xaml.cs
630 lines (558 loc) · 21.5 KB
/
TextView.xaml.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
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
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Documents;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Trivial.Data;
using Trivial.IO;
using Trivial.Reflection;
using Trivial.Text;
using Windows.Foundation;
using Windows.Foundation.Collections;
namespace Trivial.UI;
using DependencyObjectProxy = DependencyObjectProxy<TextView>;
/// <summary>
/// The text view.
/// </summary>
public sealed partial class TextView : UserControl
{
/// <summary>
/// The event arguments of line event.
/// </summary>
public class LineEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the LineEventArgs class.
/// </summary>
public LineEventArgs()
{
}
/// <summary>
/// Initializes a new instance of the LineEventArgs class.
/// </summary>
/// <param name="line">The line number.</param>
/// <param name="text">The text.</param>
/// <param name="background">The background.</param>
/// <param name="textHighlighters">The text highligher.</param>
public LineEventArgs(int line, string text, Brush background = null, IList<TextHighlighter> textHighlighters = null)
{
LineNumber = line;
Text = text;
Background = background;
TextHighlighters = textHighlighters;
}
/// <summary>
/// Gets the line number.
/// </summary>
public int LineNumber { get; private set; }
/// <summary>
/// Gets the text.
/// </summary>
public string Text { get; }
/// <summary>
/// Gets the background
/// </summary>
public Brush Background { get; }
/// <summary>
/// Gets the text highlighters.
/// </summary>
public IList<TextHighlighter> TextHighlighters { get; }
}
/// <summary>
/// The dependency property of item container style.
/// </summary>
public static readonly DependencyProperty ItemContainerStyleProperty = DependencyObjectProxy.RegisterProperty<Style>(nameof(ItemContainerStyle));
/// <summary>
/// The dependency property of text style.
/// </summary>
public static readonly DependencyProperty TextStyleProperty = DependencyObjectProxy.RegisterProperty<Style>(nameof(TextStyle));
/// <summary>
/// The dependency property of prefix text style.
/// </summary>
public static readonly DependencyProperty PrefixStyleProperty = DependencyObjectProxy.RegisterProperty<Style>(nameof(PrefixStyle));
/// <summary>
/// The dependency property of line number style.
/// </summary>
public static readonly DependencyProperty LineNumberStyleProperty = DependencyObjectProxy.RegisterProperty<Style>(nameof(LineNumberStyle));
/// <summary>
/// The dependency property of line number width.
/// </summary>
public static readonly DependencyProperty LineNumberWidthProperty = DependencyObjectProxy.RegisterProperty(nameof(LineNumberWidth), new GridLength(50));
/// <summary>
/// The dependency property of text selection state.
/// </summary>
public static readonly DependencyProperty IsTextSelectionEnabledProperty = DependencyObjectProxy.RegisterProperty(nameof(IsTextSelectionEnabled), true);
/// <summary>
/// The dependency property of selection mode.
/// </summary>
public static readonly DependencyProperty SelectionModeProperty = DependencyObjectProxy.RegisterProperty(nameof(SelectionMode), ListViewSelectionMode.None);
/// <summary>
/// The text.
/// </summary>
private readonly ObservableCollection<TextViewModel> collection = new();
/// <summary>
/// Initializes a new instance of the TextView class.
/// </summary>
public TextView()
{
InitializeComponent();
TextElement.ItemsSource = collection;
}
/// <summary>
/// Adds or removes the click event on item.
/// </summary>
public event EventHandler<LineEventArgs> ItemClick;
/// <summary>
/// Adds or removes the change event on item.
/// </summary>
public event SelectionChangedEventHandler SelectionChanged;
/// <summary>
/// Gets the count of line.
/// </summary>
public int Count { get; private set; }
/// <summary>
/// Gets or sets the item container style.
/// </summary>
public Style ItemContainerStyle
{
get => (Style)GetValue(ItemContainerStyleProperty);
set => SetValue(ItemContainerStyleProperty, value);
}
/// <summary>
/// Gets or sets the text style.
/// </summary>
public Style TextStyle
{
get => (Style)GetValue(TextStyleProperty);
set => SetValue(TextStyleProperty, value);
}
/// <summary>
/// Gets or sets the prefix text style.
/// </summary>
public Style PrefixStyle
{
get => (Style)GetValue(PrefixStyleProperty);
set => SetValue(PrefixStyleProperty, value);
}
/// <summary>
/// Gets or sets the line number style.
/// </summary>
public Style LineNumberStyle
{
get => (Style)GetValue(LineNumberStyleProperty);
set => SetValue(LineNumberStyleProperty, value);
}
/// <summary>
/// Gets or sets the width of line number.
/// </summary>
public GridLength LineNumberWidth
{
get => (GridLength)GetValue(LineNumberWidthProperty);
set => SetValue(LineNumberWidthProperty, value);
}
/// <summary>
/// Gets or sets a value indicating whether text selection is enabled in each line.
/// </summary>
public bool IsTextSelectionEnabled
{
get => (bool)GetValue(IsTextSelectionEnabledProperty);
set => SetValue(IsTextSelectionEnabledProperty, value);
}
/// <summary>
/// Gets or sets the selection behavior for the element.
/// </summary>
public ListViewSelectionMode SelectionMode
{
get => (ListViewSelectionMode)GetValue(SelectionModeProperty);
set => SetValue(SelectionModeProperty, value);
}
/// <summary>
/// Gets the line number selected.
/// </summary>
public int SelectedLineNumber
{
get => TextElement.SelectedItem is TextViewModel m ? m.LineNumber : -1;
set => TextElement.SelectedItem = collection.FirstOrDefault(ele => ele?.LineNumber == value);
}
/// <summary>
/// Gets the line number selected.
/// </summary>
public string SelectedLine => TextElement.SelectedItem is TextViewModel m ? m.Text : null;
/// <summary>
/// Gets the line number selected.
/// </summary>
public IList<int> SelectedLineNumbers => TextElement.SelectedItems?.OfType<TextViewModel>()?.Select(ele => ele.LineNumber)?.ToList();
/// <summary>
/// Gets the line number selected.
/// </summary>
public IList<string> SelectedLines => TextElement.SelectedItems?.OfType<TextViewModel>()?.Select(ele => ele.Text)?.ToList();
/// <summary>
/// Gets a collection of item index range that describe the currently selected items in the list.
/// </summary>
public IReadOnlyList<ItemIndexRange> SelectedRanges => TextElement.SelectedRanges;
/// <summary>
/// Gets or sets the content of header.
/// </summary>
public object Header
{
get => TextElement.Header;
set => TextElement.Header = value;
}
/// <summary>
/// Gets or sets the content of footer.
/// </summary>
public object Footer
{
get => TextElement.Footer;
set => TextElement.Footer = value;
}
/// <summary>
/// Gets the text of the specific line.
/// </summary>
/// <param name="lineNumber">The line number.</param>
/// <returns>The text in the line.</returns>
public string GetLine(int lineNumber)
=> collection.FirstOrDefault(ele => ele?.LineNumber == lineNumber)?.Text;
/// <summary>
/// Gets line number by searching a keyword.
/// </summary>
/// <param name="q">The keyword to search.</param>
/// <param name="exactly">true if equaling; otherwise, false.</param>
/// <param name="start">The start position.</param>
/// <param name="comparison">One of the enumeration values that specifies the rules to use in the comparison.</param>
/// <returns>The line number.</returns>
public int GetLineNumber(string q, bool exactly = false, int start = 0, StringComparison? comparison = null)
{
if (string.IsNullOrEmpty(q)) return -1;
return SearchInternal(q, exactly, start, comparison).FirstOrDefault()?.LineNumber ?? -1;
}
/// <summary>
/// Gets line number by searching a keyword.
/// </summary>
/// <param name="q">The keyword to search.</param>
/// <param name="exactly">true if equaling; otherwise, false.</param>
/// <param name="afterSelection">true if find next; otherwise, false.</param>
/// <param name="comparison">One of the enumeration values that specifies the rules to use in the comparison.</param>
/// <returns>The line number.</returns>
public int GetLineNumber(string q, bool exactly, bool afterSelection, StringComparison? comparison = null)
{
if (string.IsNullOrEmpty(q)) return -1;
var i = afterSelection && TextElement.SelectedItem is TextViewModel m ? (m.LineNumber + 1) : 0;
return SearchInternal(q, exactly, i, comparison).FirstOrDefault()?.LineNumber ?? -1;
}
/// <summary>
/// Gets line number by searching a keyword.
/// </summary>
/// <param name="q">The keyword to search.</param>
/// <param name="exactly">true if equaling; otherwise, false.</param>
/// <param name="start">The start position.</param>
/// <param name="comparison">One of the enumeration values that specifies the rules to use in the comparison.</param>
/// <returns>The line number.</returns>
public int GetLastLineNumber(string q, bool exactly = false, int start = 0, StringComparison? comparison = null)
{
if (string.IsNullOrEmpty(q)) return -1;
return SearchLastInternal(q, exactly, start, comparison).FirstOrDefault()?.LineNumber ?? -1;
}
/// <summary>
/// Gets line number by searching a keyword.
/// </summary>
/// <param name="q">The keyword to search.</param>
/// <param name="exactly">true if equaling; otherwise, false.</param>
/// <param name="beforeSelection">true if find next; otherwise, false.</param>
/// <param name="comparison">One of the enumeration values that specifies the rules to use in the comparison.</param>
/// <returns>The line number.</returns>
public int GetLastLineNumber(string q, bool exactly, bool beforeSelection, StringComparison? comparison = null)
{
if (string.IsNullOrEmpty(q)) return -1;
var i = beforeSelection && TextElement.SelectedItem is TextViewModel m ? (m.LineNumber - 1) : -1;
return SearchLastInternal(q, exactly, i, comparison).FirstOrDefault()?.LineNumber ?? -1;
}
/// <summary>
/// Gets all line numbers by searching a keyword.
/// </summary>
/// <param name="q">The keyword to search.</param>
/// <param name="exactly">true if equaling; otherwise, false.</param>
/// <param name="start">The start position.</param>
/// <param name="comparison">One of the enumeration values that specifies the rules to use in the comparison.</param>
/// <returns>The collection of line number.</returns>
public IEnumerable<int> GetLineNumbers(string q, bool exactly = false, int start = 0, StringComparison? comparison = null)
{
if (string.IsNullOrEmpty(q)) return new List<int>();
return SearchInternal(q, exactly, start, comparison).Select(ele => ele.LineNumber).ToList();
}
/// <summary>
/// Scrolls the list to bring the item of the specific line number into view with the specified alignment.
/// </summary>
/// <param name="lineNumber">The line number.</param>
/// <param name="alignment">The alignment to scroll into view.</param>
public void ScrollIntoView(int lineNumber, ScrollIntoViewAlignment alignment = ScrollIntoViewAlignment.Default)
{
var line = collection.FirstOrDefault(ele => ele?.LineNumber == lineNumber);
if (line != null) TextElement.ScrollIntoView(line, alignment);
}
/// <summary>
/// Selects a block of items described by the item index range.
/// </summary>
/// <param name="itemIndexRange">Information about the range of items, including the index of the first and last items in the range, and the number of items.</param>
public void SelectRange(ItemIndexRange itemIndexRange)
=> TextElement.SelectRange(itemIndexRange);
/// <summary>
/// Selects a block of items described by the item index range.
/// </summary>
/// <param name="firstIndex">The index of the first item in the instance of the range to select.</param>
/// <param name="length">The number of items in the instance of the range to select.</param>
public void SelectRange(int firstIndex, uint length)
=> TextElement.SelectRange(new ItemIndexRange(firstIndex, length));
/// <summary>
/// Selects all the items in a view.
/// </summary>
public void SelectAll()
=> TextElement.SelectAll();
/// <summary>
/// Appends.
/// </summary>
/// <param name="text">The text to append.</param>
public void Append(string text)
=> Append(text == string.Empty ? new List<string>
{
string.Empty
} : new CharsReader(text).ReadLines());
/// <summary>
/// Appends.
/// </summary>
/// <param name="text">The text to append.</param>
public void Append(CharsReader text)
=> Append(text?.ReadLines());
/// <summary>
/// Appends.
/// </summary>
/// <param name="text">The text to append.</param>
public void Append(IEnumerable<string> text)
{
if (text == null) return;
foreach (var line in text)
{
Count++;
var item = new TextViewModel
{
Text = line,
LineNumber = Count
};
collection.Add(item);
}
}
/// <summary>
/// Appends.
/// </summary>
/// <param name="text">The text to append.</param>
public void Append(IEnumerable<TextLineBlockModel> text)
{
if (text == null) return;
foreach (var line in text)
{
Count++;
var s = line?.Text;
var item = new TextViewModel
{
Text = s,
LineNumber = Count
};
var background = line?.Background;
if (background != null) item.Background = background;
if (line.TextHighlighters.Count > 0) item.TextHighlighters = line.TextHighlighters;
collection.Add(item);
}
}
/// <summary>
/// Appends.
/// </summary>
/// <param name="text">The text to append.</param>
/// <param name="style">The optional style for JSON.</param>
public void Append(JsonObjectNode text, JsonTextStyle style = null)
{
if (text == null) return;
var i = Count;
var lines = VisualUtility.CreateTextViewModels(text, i + 1, style ?? VisualUtility.GetDefaultJsonTextStyle(this));
Count += lines.Count;
foreach (var line in lines)
{
collection.Add(line);
}
}
/// <summary>
/// Appends.
/// </summary>
/// <param name="text">The text to append.</param>
/// <param name="style">The optional style for JSON.</param>
public void Append(JsonArrayNode text, JsonTextStyle style = null)
{
if (text == null) return;
var i = Count;
var lines = VisualUtility.CreateTextViewModels(text, i + 1, style ?? VisualUtility.GetDefaultJsonTextStyle(this));
Count += lines.Count;
foreach (var line in lines)
{
collection.Add(line);
}
}
/// <summary>
/// Sets text.
/// </summary>
/// <param name="text">The text to set.</param>
public void SetData(string text)
=> SetData(text == string.Empty ? new List<string>
{
string.Empty
} : new CharsReader(text).ReadLines());
/// <summary>
/// Sets text.
/// </summary>
/// <param name="text">The text to set.</param>
public void SetData(CharsReader text)
=> SetData(text?.ReadLines());
/// <summary>
/// Sets text.
/// </summary>
/// <param name="text">The text to set.</param>
public void SetData(IEnumerable<string> text)
{
collection.Clear();
Count = 0;
if (text == null) return;
Append(text);
}
/// <summary>
/// Sets text.
/// </summary>
/// <param name="text">The text to set.</param>
/// <param name="style">The optional style for JSON.</param>
public void SetData(JsonObjectNode text, JsonTextStyle style = null)
{
collection.Clear();
Count = 0;
if (text == null) return;
Append(text, style);
}
/// <summary>
/// Sets text.
/// </summary>
/// <param name="text">The text to set.</param>
/// <param name="style">The optional style for JSON.</param>
public void SetData(JsonArrayNode text, JsonTextStyle style = null)
{
collection.Clear();
Count = 0;
if (text == null) return;
Append(text, style);
}
private IEnumerable<TextViewModel> SearchInternal(string q, bool exactly = false, int start = 0, StringComparison? comparison = null)
{
if (string.IsNullOrEmpty(q)) return new List<TextViewModel>();
var col = collection.Where(ele => ele != null && ele.LineNumber >= start);
if (exactly)
return comparison.HasValue ? col.Where(ele => q.Equals(ele.Text, comparison.Value)) : col.Where(ele => q.Equals(ele.Text));
return comparison.HasValue ? col.Where(ele => !string.IsNullOrEmpty(ele.Text) && ele.Text.Contains(q, comparison.Value)) : col.Where(ele => !string.IsNullOrEmpty(ele.Text) && ele.Text.Contains(q));
}
private IEnumerable<TextViewModel> SearchLastInternal(string q, bool exactly = false, int start = 0, StringComparison? comparison = null)
{
if (string.IsNullOrEmpty(q)) return new List<TextViewModel>();
var col = start == -1 ? collection.Where(ele => ele != null) : collection.Where(ele => ele != null && ele.LineNumber <= start);
col = col.OrderByDescending(ele => ele.LineNumber);
if (exactly)
return comparison.HasValue ? col.Where(ele => q.Equals(ele.Text, comparison.Value)) : col.Where(ele => q.Equals(ele.Text));
return comparison.HasValue ? col.Where(ele => !string.IsNullOrEmpty(ele.Text) && ele.Text.Contains(q, comparison.Value)) : col.Where(ele => !string.IsNullOrEmpty(ele.Text) && ele.Text.Contains(q));
}
private void TextElement_ItemClick(object sender, ItemClickEventArgs e)
{
if (e.ClickedItem is not TextViewModel model) return;
ItemClick?.Invoke(this, new LineEventArgs(model.LineNumber, model.Text, model.Background, model.TextHighlighters));
}
private void TextElement_SelectionChanged(object sender, SelectionChangedEventArgs e)
=> SelectionChanged?.Invoke(this, e);
}
internal class TextViewModel : ObservableProperties
{
public int LineNumber
{
get => GetCurrentProperty<int>();
internal set => SetCurrentProperty(value);
}
public string Text
{
get => GetCurrentProperty<string>();
internal set => SetCurrentProperty(value);
}
public Brush Background
{
get => GetCurrentProperty<Brush>();
internal set => SetCurrentProperty(value);
}
public Visibility Visibility
{
get => GetCurrentProperty<Visibility>();
internal set => SetCurrentProperty(value);
}
/// <summary>
/// Gets the text highlighters.
/// </summary>
public IList<TextHighlighter> TextHighlighters
{
get => GetCurrentProperty<IList<TextHighlighter>>();
internal set => SetCurrentProperty(value);
}
}
internal class TextViewModelFactory
{
private StringBuilder sb = new();
private List<TextHighlighter> highlighters = new();
public TextViewModelFactory(int start)
{
StartIndex = start;
}
public List<TextViewModel> Collection { get; } = new();
public int StartIndex { get; }
public void AppendToBuffer(string text, Brush foreground, Brush background = null)
{
var range = new TextRange(sb.Length, text.Length);
sb.Append(text);
AppendToBuffer(range, foreground, background);
}
public void AppendToBuffer(char c, Brush foreground, Brush background = null)
{
var range = new TextRange(sb.Length, 1);
sb.Append(c);
AppendToBuffer(range, foreground, background);
}
public void PushLine()
{
Collection.Add(new()
{
LineNumber = StartIndex + Collection.Count,
Text = sb.ToString(),
TextHighlighters = highlighters
});
sb = new();
highlighters = new();
}
private void AppendToBuffer(TextRange range, Brush foreground, Brush background)
{
var hl = new TextHighlighter
{
Foreground = foreground,
Background = background ?? VisualUtility.TransparentBrush
};
hl.Ranges.Add(range);
highlighters.Add(hl);
}
}