CodeView.axaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.ApplicationLifetimes;
  4. using Avalonia.Data;
  5. using Avalonia.Layout;
  6. using Avalonia.Markup.Xaml;
  7. using Avalonia.Media;
  8. using Avalonia.Threading;
  9. using SukiUI.Content;
  10. using SukiUI.Extensions;
  11. using System;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace SukiUI.Controls;
  15. public partial class CodeView : UserControl
  16. {
  17. private void InitializeComponent()
  18. {
  19. AvaloniaXamlLoader.Load(this);
  20. }
  21. private string _text = "";
  22. private readonly TextBlock _textBlock;
  23. public CodeView()
  24. {
  25. InitializeComponent();
  26. var grid = new Grid();
  27. grid.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Auto));
  28. grid.ColumnDefinitions.Add(new ColumnDefinition());
  29. var lineNumberTextBlock = new TextBlock()
  30. {
  31. VerticalAlignment = Avalonia.Layout.VerticalAlignment.Top,
  32. Margin = new Thickness(10, 5, 5, 0),
  33. Foreground = Brushes.Gray
  34. };
  35. Grid.SetColumn(lineNumberTextBlock, 0);
  36. _textBlock = new TextBlock()
  37. {
  38. VerticalAlignment = Avalonia.Layout.VerticalAlignment.Top,
  39. Margin = new Thickness(10, 5, 5, 0),
  40. FontFamily = new FontFamily("Consolas")
  41. };
  42. Grid.SetColumn(_textBlock, 1);
  43. grid.Children.Add(lineNumberTextBlock);
  44. grid.Children.Add(_textBlock);
  45. var gridcontent = new Grid();
  46. gridcontent.Children.Add(
  47. new PathIcon()
  48. {
  49. Data = Icons.ChevronRight,
  50. Classes = { "Flippable" },
  51. Foreground =
  52. new Avalonia.Media.SolidColorBrush(
  53. (Avalonia.Media.Color)Application.Current.FindRequiredResource("SukiText")),
  54. Height = 15,
  55. Width = 15,
  56. });
  57. gridcontent.Children.Add(new TextBlock() { IsVisible = false, HorizontalAlignment = HorizontalAlignment.Right, VerticalAlignment = VerticalAlignment.Center, FontSize = 12, Text = "Copied !" });
  58. var button = new Button()
  59. {
  60. Classes = { "Accent" },
  61. Content = gridcontent,
  62. HorizontalAlignment = HorizontalAlignment.Right,
  63. VerticalAlignment = VerticalAlignment.Top,
  64. };
  65. button.Click += async (sender, args) =>
  66. {
  67. await TopLevel.GetTopLevel(((ClassicDesktopStyleApplicationLifetime)Application.Current.ApplicationLifetime)
  68. .MainWindow).Clipboard.SetTextAsync(Text);
  69. Dispatcher.UIThread.Invoke(() =>
  70. {
  71. gridcontent.Children[0].IsVisible = false;
  72. gridcontent.Children[1].IsVisible = true;
  73. });
  74. await Task.Delay(3000);
  75. Dispatcher.UIThread.Invoke(() =>
  76. {
  77. gridcontent.Children[0].IsVisible = true;
  78. gridcontent.Children[1].IsVisible = false;
  79. });
  80. };
  81. Grid.SetColumn(button, 1);
  82. grid.Children.Add(button);
  83. Content = grid;
  84. this.AttachedToVisualTree += (s, e) => UpdateText();
  85. }
  86. public string Text
  87. {
  88. get => _text;
  89. set
  90. {
  91. if (_text != value)
  92. {
  93. _text = value;
  94. UpdateText();
  95. }
  96. }
  97. }
  98. public static readonly DirectProperty<CodeView, string> TextProperty =
  99. AvaloniaProperty.RegisterDirect<CodeView, string>(
  100. nameof(Text),
  101. o => o.Text,
  102. (o, v) => o.Text = v,
  103. defaultBindingMode: BindingMode.OneWay,
  104. enableDataValidation: true);
  105. private void UpdateText()
  106. {
  107. try
  108. {
  109. var lines = _text.Split('\n');
  110. var lineNumberText = "";
  111. for (int i = 1; i <= lines.Length; i++)
  112. {
  113. lineNumberText += $"{i}\n";
  114. }
  115. (_textBlock.Parent as Grid).RowDefinitions.Clear();
  116. for (int i = 0; i < lines.Length; i++)
  117. {
  118. (_textBlock.Parent as Grid).RowDefinitions.Add(new RowDefinition());
  119. }
  120. (_textBlock.Parent as Grid).ColumnDefinitions[0].Width = new GridLength(0, GridUnitType.Auto);
  121. (_textBlock.Parent as Grid).ColumnDefinitions[1].Width = new GridLength(1, GridUnitType.Star);
  122. (_textBlock.Parent as Grid).Children[0].SetValue(Grid.RowSpanProperty, lines.Length);
  123. (_textBlock.Parent as Grid).Children[0].SetValue(TextBlock.TextProperty, lineNumberText);
  124. _textBlock.SetValue(Grid.ColumnProperty, 1);
  125. _textBlock.SetValue(Grid.RowProperty, 0);
  126. _textBlock.SetValue(Grid.RowSpanProperty, lines.Length);
  127. _textBlock.Text = _text.TrimEnd();
  128. }
  129. catch (Exception) { }
  130. }
  131. }