|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +using Avalonia; |
| 5 | +using Avalonia.Controls.Primitives; |
| 6 | +using Avalonia.Interactivity; |
| 7 | +using Avalonia.Media; |
| 8 | + |
| 9 | +using AvaloniaEdit; |
| 10 | +using AvaloniaEdit.Document; |
| 11 | +using AvaloniaEdit.Editing; |
| 12 | +using AvaloniaEdit.Rendering; |
| 13 | +using AvaloniaEdit.TextMate; |
| 14 | + |
| 15 | +namespace SourceGit.Views |
| 16 | +{ |
| 17 | + public class CommandLogContentPresenter : TextEditor |
| 18 | + { |
| 19 | + public class LineStyleTransformer : DocumentColorizingTransformer |
| 20 | + { |
| 21 | + protected override void ColorizeLine(DocumentLine line) |
| 22 | + { |
| 23 | + var content = CurrentContext.Document.GetText(line); |
| 24 | + if (content.StartsWith("$ git ", StringComparison.Ordinal)) |
| 25 | + { |
| 26 | + ChangeLinePart(line.Offset, line.Offset + 1, v => |
| 27 | + { |
| 28 | + v.TextRunProperties.SetForegroundBrush(Brushes.Orange); |
| 29 | + }); |
| 30 | + |
| 31 | + ChangeLinePart(line.Offset + 2, line.EndOffset, v => |
| 32 | + { |
| 33 | + var old = v.TextRunProperties.Typeface; |
| 34 | + v.TextRunProperties.SetTypeface(new Typeface(old.FontFamily, old.Style, FontWeight.Bold)); |
| 35 | + }); |
| 36 | + } |
| 37 | + else if (content.StartsWith("remote: ", StringComparison.Ordinal)) |
| 38 | + { |
| 39 | + ChangeLinePart(line.Offset, line.Offset + 7, v => |
| 40 | + { |
| 41 | + v.TextRunProperties.SetForegroundBrush(Brushes.SeaGreen); |
| 42 | + }); |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + foreach (var err in _errors) |
| 47 | + { |
| 48 | + var idx = content.IndexOf(err, StringComparison.Ordinal); |
| 49 | + if (idx >= 0) |
| 50 | + { |
| 51 | + ChangeLinePart(line.Offset + idx, line.Offset + err.Length + 1, v => |
| 52 | + { |
| 53 | + v.TextRunProperties.SetForegroundBrush(Brushes.Red); |
| 54 | + }); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private readonly List<string> _errors = ["! [rejected]", "! [remote rejected]"]; |
| 61 | + } |
| 62 | + |
| 63 | + public static readonly StyledProperty<ViewModels.CommandLog> LogProperty = |
| 64 | + AvaloniaProperty.Register<CommandLogContentPresenter, ViewModels.CommandLog>(nameof(Log)); |
| 65 | + |
| 66 | + public ViewModels.CommandLog Log |
| 67 | + { |
| 68 | + get => GetValue(LogProperty); |
| 69 | + set => SetValue(LogProperty, value); |
| 70 | + } |
| 71 | + |
| 72 | + public static readonly StyledProperty<string> PureTextProperty = |
| 73 | + AvaloniaProperty.Register<CommandLogContentPresenter, string>(nameof(PureText)); |
| 74 | + |
| 75 | + public string PureText |
| 76 | + { |
| 77 | + get => GetValue(PureTextProperty); |
| 78 | + set => SetValue(PureTextProperty, value); |
| 79 | + } |
| 80 | + |
| 81 | + protected override Type StyleKeyOverride => typeof(TextEditor); |
| 82 | + |
| 83 | + public CommandLogContentPresenter() : base(new TextArea(), new TextDocument()) |
| 84 | + { |
| 85 | + IsReadOnly = true; |
| 86 | + ShowLineNumbers = false; |
| 87 | + WordWrap = false; |
| 88 | + HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; |
| 89 | + VerticalScrollBarVisibility = ScrollBarVisibility.Auto; |
| 90 | + |
| 91 | + TextArea.TextView.Margin = new Thickness(4, 0); |
| 92 | + TextArea.TextView.Options.EnableHyperlinks = false; |
| 93 | + TextArea.TextView.Options.EnableEmailHyperlinks = false; |
| 94 | + } |
| 95 | + |
| 96 | + protected override void OnLoaded(RoutedEventArgs e) |
| 97 | + { |
| 98 | + base.OnLoaded(e); |
| 99 | + |
| 100 | + if (_textMate == null) |
| 101 | + { |
| 102 | + _textMate = Models.TextMateHelper.CreateForEditor(this); |
| 103 | + Models.TextMateHelper.SetGrammarByFileName(_textMate, "Log.log"); |
| 104 | + TextArea.TextView.LineTransformers.Add(new LineStyleTransformer()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + protected override void OnUnloaded(RoutedEventArgs e) |
| 109 | + { |
| 110 | + base.OnUnloaded(e); |
| 111 | + |
| 112 | + if (_textMate != null) |
| 113 | + { |
| 114 | + _textMate.Dispose(); |
| 115 | + _textMate = null; |
| 116 | + } |
| 117 | + |
| 118 | + GC.Collect(); |
| 119 | + } |
| 120 | + |
| 121 | + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) |
| 122 | + { |
| 123 | + base.OnPropertyChanged(change); |
| 124 | + |
| 125 | + if (change.Property == LogProperty) |
| 126 | + { |
| 127 | + if (change.NewValue is ViewModels.CommandLog log) |
| 128 | + { |
| 129 | + Text = log.Content; |
| 130 | + log.Register(OnLogLineReceived); |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + Text = string.Empty; |
| 135 | + } |
| 136 | + } |
| 137 | + else if (change.Property == PureTextProperty) |
| 138 | + { |
| 139 | + if (!string.IsNullOrEmpty(PureText)) |
| 140 | + Text = PureText; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private void OnLogLineReceived(string newline) |
| 145 | + { |
| 146 | + AppendText("\n"); |
| 147 | + AppendText(newline); |
| 148 | + } |
| 149 | + |
| 150 | + private TextMate.Installation _textMate = null; |
| 151 | + } |
| 152 | +} |
0 commit comments