From 340fe9e3babf5c5a40bf72b37ccbe2a29b27e79e Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 20 Mar 2024 18:27:48 +0800 Subject: [PATCH] enhance: leave the current state until we got the new diff result to reduce flickering --- src/SourceGit/Models/TextMateHelper.cs | 59 +++++++ src/SourceGit/ViewModels/CommitDetail.cs | 2 +- src/SourceGit/ViewModels/DiffContext.cs | 5 +- src/SourceGit/ViewModels/FileHistories.cs | 2 +- src/SourceGit/ViewModels/RevisionCompare.cs | 2 +- src/SourceGit/ViewModels/StashesPage.cs | 2 +- src/SourceGit/ViewModels/WorkingCopy.cs | 9 +- src/SourceGit/Views/Blame.axaml.cs | 76 +++------ src/SourceGit/Views/RevisionFiles.axaml.cs | 62 ++------ src/SourceGit/Views/TextDiffView.axaml.cs | 166 ++++++-------------- 10 files changed, 158 insertions(+), 227 deletions(-) create mode 100644 src/SourceGit/Models/TextMateHelper.cs diff --git a/src/SourceGit/Models/TextMateHelper.cs b/src/SourceGit/Models/TextMateHelper.cs new file mode 100644 index 00000000..d4f2c7aa --- /dev/null +++ b/src/SourceGit/Models/TextMateHelper.cs @@ -0,0 +1,59 @@ +using System.IO; + +using Avalonia.Styling; + +using AvaloniaEdit; +using AvaloniaEdit.TextMate; + +using TextMateSharp.Grammars; + +namespace SourceGit.Models +{ + public static class TextMateHelper + { + public static TextMate.Installation CreateForEditor(TextEditor editor) + { + if (App.Current?.ActualThemeVariant == ThemeVariant.Dark) + { + return editor.InstallTextMate(new RegistryOptions(ThemeName.DarkPlus)); + } + else + { + return editor.InstallTextMate(new RegistryOptions(ThemeName.LightPlus)); + } + } + + public static void SetThemeByApp(TextMate.Installation installation) + { + if (installation == null) return; + + var reg = installation.RegistryOptions as RegistryOptions; + if (App.Current?.ActualThemeVariant == ThemeVariant.Dark) + { + installation.SetTheme(reg.LoadTheme(ThemeName.DarkPlus)); + } + else + { + installation.SetTheme(reg.LoadTheme(ThemeName.LightPlus)); + } + } + + public static void SetGrammarByFileName(TextMate.Installation installation, string filePath) + { + if (installation == null) return; + + var ext = Path.GetExtension(filePath); + if (ext == ".h") + { + ext = ".cpp"; + } + else if (ext == ".resx") + { + ext = ".xml"; + } + + var reg = installation.RegistryOptions as RegistryOptions; + installation.SetGrammar(reg.GetScopeByExtension(ext)); + } + } +} diff --git a/src/SourceGit/ViewModels/CommitDetail.cs b/src/SourceGit/ViewModels/CommitDetail.cs index 38c63574..55e0d43b 100644 --- a/src/SourceGit/ViewModels/CommitDetail.cs +++ b/src/SourceGit/ViewModels/CommitDetail.cs @@ -67,7 +67,7 @@ namespace SourceGit.ViewModels else { SelectedChangeNode = FileTreeNode.SelectByPath(_changeTree, value.Path); - DiffContext = new DiffContext(_repo, new Models.DiffOption(_commit, value)); + DiffContext = new DiffContext(_repo, new Models.DiffOption(_commit, value), _diffContext); } } } diff --git a/src/SourceGit/ViewModels/DiffContext.cs b/src/SourceGit/ViewModels/DiffContext.cs index d3cf3a6b..5e474ff8 100644 --- a/src/SourceGit/ViewModels/DiffContext.cs +++ b/src/SourceGit/ViewModels/DiffContext.cs @@ -70,10 +70,11 @@ namespace SourceGit.ViewModels set => SetProperty(ref _syncScrollOffset, value); } - public DiffContext(string repo, Models.DiffOption option) + public DiffContext(string repo, Models.DiffOption option, DiffContext previous = null) { _repo = repo; _option = option; + _content = previous != null ? previous._content : null; OnPropertyChanged(nameof(FilePath)); OnPropertyChanged(nameof(IsOrgFilePathVisible)); @@ -101,7 +102,7 @@ namespace SourceGit.ViewModels } } - Dispatcher.UIThread.InvokeAsync(() => + Dispatcher.UIThread.Post(() => { if (latest.IsBinary) { diff --git a/src/SourceGit/ViewModels/FileHistories.cs b/src/SourceGit/ViewModels/FileHistories.cs index f2e84c7e..0b5653f5 100644 --- a/src/SourceGit/ViewModels/FileHistories.cs +++ b/src/SourceGit/ViewModels/FileHistories.cs @@ -39,7 +39,7 @@ namespace SourceGit.ViewModels } else { - DiffContext = new DiffContext(_repo, new Models.DiffOption(value, _file)); + DiffContext = new DiffContext(_repo, new Models.DiffOption(value, _file), _diffContext); } } } diff --git a/src/SourceGit/ViewModels/RevisionCompare.cs b/src/SourceGit/ViewModels/RevisionCompare.cs index 4fab38eb..585e9f48 100644 --- a/src/SourceGit/ViewModels/RevisionCompare.cs +++ b/src/SourceGit/ViewModels/RevisionCompare.cs @@ -51,7 +51,7 @@ namespace SourceGit.ViewModels else { SelectedNode = FileTreeNode.SelectByPath(_changeTree, value.Path); - DiffContext = new DiffContext(_repo, new Models.DiffOption(StartPoint.SHA, EndPoint.SHA, value)); + DiffContext = new DiffContext(_repo, new Models.DiffOption(StartPoint.SHA, EndPoint.SHA, value), _diffContext); } } } diff --git a/src/SourceGit/ViewModels/StashesPage.cs b/src/SourceGit/ViewModels/StashesPage.cs index a5fc75be..22a7262e 100644 --- a/src/SourceGit/ViewModels/StashesPage.cs +++ b/src/SourceGit/ViewModels/StashesPage.cs @@ -77,7 +77,7 @@ namespace SourceGit.ViewModels } else { - DiffContext = new DiffContext(_repo.FullPath, new Models.DiffOption($"{_selectedStash.SHA}^", _selectedStash.SHA, value)); + DiffContext = new DiffContext(_repo.FullPath, new Models.DiffOption($"{_selectedStash.SHA}^", _selectedStash.SHA, value), _diffContext); } } } diff --git a/src/SourceGit/ViewModels/WorkingCopy.cs b/src/SourceGit/ViewModels/WorkingCopy.cs index cacaab5a..737876ec 100644 --- a/src/SourceGit/ViewModels/WorkingCopy.cs +++ b/src/SourceGit/ViewModels/WorkingCopy.cs @@ -287,7 +287,14 @@ namespace SourceGit.ViewModels } else { - DetailContext = new DiffContext(_repo.FullPath, new Models.DiffOption(change, isUnstaged)); + if (_detailContext is DiffContext previous) + { + DetailContext = new DiffContext(_repo.FullPath, new Models.DiffOption(change, isUnstaged), previous); + } + else + { + DetailContext = new DiffContext(_repo.FullPath, new Models.DiffOption(change, isUnstaged)); + } } } diff --git a/src/SourceGit/Views/Blame.axaml.cs b/src/SourceGit/Views/Blame.axaml.cs index c98f6d9a..54592ed5 100644 --- a/src/SourceGit/Views/Blame.axaml.cs +++ b/src/SourceGit/Views/Blame.axaml.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Globalization; -using System.IO; using Avalonia; using Avalonia.Controls; @@ -9,7 +8,6 @@ using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Media; -using Avalonia.Styling; using AvaloniaEdit; using AvaloniaEdit.Document; @@ -18,8 +16,6 @@ using AvaloniaEdit.Rendering; using AvaloniaEdit.TextMate; using AvaloniaEdit.Utils; -using TextMateSharp.Grammars; - namespace SourceGit.Views { public class BlameTextEditor : TextEditor @@ -214,6 +210,16 @@ namespace SourceGit.Views IsReadOnly = true; ShowLineNumbers = false; WordWrap = false; + + _textMate = Models.TextMateHelper.CreateForEditor(this); + + TextArea.LeftMargins.Add(new LineNumberMargin() { Margin = new Thickness(8, 0) }); + TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this)); + TextArea.LeftMargins.Add(new CommitInfoMargin(this) { Margin = new Thickness(8, 0) }); + TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this)); + TextArea.TextView.ContextRequested += OnTextViewContextRequested; + TextArea.TextView.VisualLinesChanged += OnTextViewVisualLinesChanged; + TextArea.TextView.Margin = new Thickness(4, 0); } public void OnCommitSHAClicked(string sha) @@ -224,31 +230,6 @@ namespace SourceGit.Views } } - protected override void OnLoaded(RoutedEventArgs e) - { - base.OnLoaded(e); - - TextArea.LeftMargins.Add(new LineNumberMargin() { Margin = new Thickness(8, 0) }); - TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this)); - TextArea.LeftMargins.Add(new CommitInfoMargin(this) { Margin = new Thickness(8, 0) }); - TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this)); - TextArea.TextView.ContextRequested += OnTextViewContextRequested; - TextArea.TextView.VisualLinesChanged += OnTextViewVisualLinesChanged; - TextArea.TextView.Margin = new Thickness(4, 0); - - if (App.Current?.ActualThemeVariant == ThemeVariant.Dark) - { - _registryOptions = new RegistryOptions(ThemeName.DarkPlus); - } - else - { - _registryOptions = new RegistryOptions(ThemeName.LightPlus); - } - - _textMate = this.InstallTextMate(_registryOptions); - UpdateGrammar(); - } - protected override void OnUnloaded(RoutedEventArgs e) { base.OnUnloaded(e); @@ -257,9 +238,11 @@ namespace SourceGit.Views TextArea.TextView.ContextRequested -= OnTextViewContextRequested; TextArea.TextView.VisualLinesChanged -= OnTextViewVisualLinesChanged; - _registryOptions = null; - _textMate.Dispose(); - _textMate = null; + if (_textMate != null) + { + _textMate.Dispose(); + _textMate = null; + } } protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) @@ -270,24 +253,17 @@ namespace SourceGit.Views { if (BlameData != null) { + Models.TextMateHelper.SetGrammarByFileName(_textMate, BlameData.File); Text = BlameData.Content; - UpdateGrammar(); } else { Text = string.Empty; } } - else if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null && _textMate != null) + else if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null) { - if (App.Current?.ActualThemeVariant == ThemeVariant.Dark) - { - _textMate.SetTheme(_registryOptions.LoadTheme(ThemeName.DarkPlus)); - } - else - { - _textMate.SetTheme(_registryOptions.LoadTheme(ThemeName.LightPlus)); - } + Models.TextMateHelper.SetThemeByApp(_textMate); } } @@ -329,22 +305,6 @@ namespace SourceGit.Views } } - private void UpdateGrammar() - { - if (_textMate == null || BlameData == null) return; - - var ext = Path.GetExtension(BlameData.File); - if (ext == ".h") - { - _textMate.SetGrammar(_registryOptions.GetScopeByLanguageId("cpp")); - } - else - { - _textMate.SetGrammar(_registryOptions.GetScopeByExtension(ext)); - } - } - - private RegistryOptions _registryOptions = null; private TextMate.Installation _textMate = null; } diff --git a/src/SourceGit/Views/RevisionFiles.axaml.cs b/src/SourceGit/Views/RevisionFiles.axaml.cs index 1c458729..82748c5f 100644 --- a/src/SourceGit/Views/RevisionFiles.axaml.cs +++ b/src/SourceGit/Views/RevisionFiles.axaml.cs @@ -1,23 +1,18 @@ using System; -using System.IO; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Interactivity; using Avalonia.Media; -using Avalonia.Styling; using AvaloniaEdit; using AvaloniaEdit.Document; using AvaloniaEdit.Editing; using AvaloniaEdit.TextMate; -using TextMateSharp.Grammars; - namespace SourceGit.Views { - public class RevisionTextFileView : TextEditor { protected override Type StyleKeyOverride => typeof(TextEditor); @@ -39,17 +34,12 @@ namespace SourceGit.Views base.OnLoaded(e); TextArea.TextView.ContextRequested += OnTextViewContextRequested; - if (App.Current?.ActualThemeVariant == ThemeVariant.Dark) - { - _registryOptions = new RegistryOptions(ThemeName.DarkPlus); - } - else - { - _registryOptions = new RegistryOptions(ThemeName.LightPlus); - } - _textMate = this.InstallTextMate(_registryOptions); - UpdateGrammar(); + _textMate = Models.TextMateHelper.CreateForEditor(this); + if (DataContext is Models.RevisionTextFile source) + { + Models.TextMateHelper.SetGrammarByFileName(_textMate, source.FileName); + } } protected override void OnUnloaded(RoutedEventArgs e) @@ -57,9 +47,13 @@ namespace SourceGit.Views base.OnUnloaded(e); TextArea.TextView.ContextRequested -= OnTextViewContextRequested; - _registryOptions = null; - _textMate.Dispose(); - _textMate = null; + + if (_textMate != null) + { + _textMate.Dispose(); + _textMate = null; + } + GC.Collect(); } @@ -70,7 +64,7 @@ namespace SourceGit.Views var source = DataContext as Models.RevisionTextFile; if (source != null) { - UpdateGrammar(); + Models.TextMateHelper.SetGrammarByFileName(_textMate, source.FileName); Text = source.Content; } } @@ -79,16 +73,9 @@ namespace SourceGit.Views { base.OnPropertyChanged(change); - if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null && _textMate != null) + if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null) { - if (App.Current?.ActualThemeVariant == ThemeVariant.Dark) - { - _textMate.SetTheme(_registryOptions.LoadTheme(ThemeName.DarkPlus)); - } - else - { - _textMate.SetTheme(_registryOptions.LoadTheme(ThemeName.LightPlus)); - } + Models.TextMateHelper.SetThemeByApp(_textMate); } } @@ -118,25 +105,6 @@ namespace SourceGit.Views e.Handled = true; } - private void UpdateGrammar() - { - if (_textMate == null) return; - - var src = DataContext as Models.RevisionTextFile; - if (src == null) return; - - var ext = Path.GetExtension(src.FileName); - if (ext == ".h") - { - _textMate.SetGrammar(_registryOptions.GetScopeByLanguageId("cpp")); - } - else - { - _textMate.SetGrammar(_registryOptions.GetScopeByExtension(ext)); - } - } - - private RegistryOptions _registryOptions = null; private TextMate.Installation _textMate = null; } diff --git a/src/SourceGit/Views/TextDiffView.axaml.cs b/src/SourceGit/Views/TextDiffView.axaml.cs index 34002d84..420f6a59 100644 --- a/src/SourceGit/Views/TextDiffView.axaml.cs +++ b/src/SourceGit/Views/TextDiffView.axaml.cs @@ -9,7 +9,6 @@ using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Interactivity; using Avalonia.Media; -using Avalonia.Styling; using Avalonia.VisualTree; using AvaloniaEdit; @@ -19,8 +18,6 @@ using AvaloniaEdit.Rendering; using AvaloniaEdit.TextMate; using AvaloniaEdit.Utils; -using TextMateSharp.Grammars; - namespace SourceGit.Views { public class CombinedTextDiffPresenter : TextEditor @@ -160,10 +157,9 @@ namespace SourceGit.Views private static readonly Brush HL_ADDED = new SolidColorBrush(Color.FromArgb(90, 0, 255, 0)); private static readonly Brush HL_DELETED = new SolidColorBrush(Color.FromArgb(80, 255, 0, 0)); - public LineStyleTransformer(CombinedTextDiffPresenter editor, IBrush indicatorFG) + public LineStyleTransformer(CombinedTextDiffPresenter editor) { _editor = editor; - _indicatorFG = indicatorFG; _indicatorTypeface = new Typeface("fonts:SourceGit#JetBrains Mono", FontStyle.Italic); } @@ -177,7 +173,7 @@ namespace SourceGit.Views { ChangeLinePart(line.Offset, line.EndOffset, v => { - v.TextRunProperties.SetForegroundBrush(_indicatorFG); + v.TextRunProperties.SetForegroundBrush(_editor.SecondaryFG); v.TextRunProperties.SetTypeface(_indicatorTypeface); }); @@ -198,7 +194,6 @@ namespace SourceGit.Views } private readonly CombinedTextDiffPresenter _editor; - private readonly IBrush _indicatorFG = Brushes.DarkGray; private readonly Typeface _indicatorTypeface = Typeface.Default; } @@ -233,14 +228,11 @@ namespace SourceGit.Views public CombinedTextDiffPresenter() : base(new TextArea(), new TextDocument()) { + _lineStyleTransformer = new LineStyleTransformer(this); + IsReadOnly = true; ShowLineNumbers = false; WordWrap = false; - } - - protected override void OnLoaded(RoutedEventArgs e) - { - base.OnLoaded(e); TextArea.LeftMargins.Add(new LineNumberMargin(this, true) { Margin = new Thickness(8, 0) }); TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this)); @@ -249,37 +241,34 @@ namespace SourceGit.Views TextArea.TextView.Margin = new Thickness(4, 0); TextArea.TextView.BackgroundRenderers.Add(new LineBackgroundRenderer(this)); + } + + protected override void OnLoaded(RoutedEventArgs e) + { + base.OnLoaded(e); + + _textMate = Models.TextMateHelper.CreateForEditor(this); + if (DiffData != null) Models.TextMateHelper.SetGrammarByFileName(_textMate, DiffData.File); + + TextArea.TextView.LineTransformers.Add(_lineStyleTransformer); TextArea.TextView.ContextRequested += OnTextViewContextRequested; TextArea.TextView.ScrollOffsetChanged += OnTextViewScrollOffsetChanged; - - if (App.Current?.ActualThemeVariant == ThemeVariant.Dark) - { - _registryOptions = new RegistryOptions(ThemeName.DarkPlus); - } - else - { - _registryOptions = new RegistryOptions(ThemeName.LightPlus); - } - - _textMate = this.InstallTextMate(_registryOptions); - UpdateGrammar(); - - // This line must after InstallTextMate. - TextArea.TextView.LineTransformers.Add(new LineStyleTransformer(this, SecondaryFG)); } protected override void OnUnloaded(RoutedEventArgs e) { base.OnUnloaded(e); - TextArea.LeftMargins.Clear(); - TextArea.TextView.BackgroundRenderers.Clear(); - TextArea.TextView.LineTransformers.Clear(); + TextArea.TextView.LineTransformers.Remove(_lineStyleTransformer); TextArea.TextView.ContextRequested -= OnTextViewContextRequested; TextArea.TextView.ScrollOffsetChanged -= OnTextViewScrollOffsetChanged; - _registryOptions = null; - _textMate.Dispose(); - _textMate = null; + + if (_textMate != null) + { + _textMate.Dispose(); + _textMate = null; + } + GC.Collect(); } @@ -328,7 +317,7 @@ namespace SourceGit.Views builder.AppendLine(line.Content); } - UpdateGrammar(); + Models.TextMateHelper.SetGrammarByFileName(_textMate, DiffData.File); Text = builder.ToString(); } else @@ -344,36 +333,14 @@ namespace SourceGit.Views scrollable.Offset = SyncScrollOffset; } } - else if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null && _textMate != null) + else if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null) { - if (App.Current?.ActualThemeVariant == ThemeVariant.Dark) - { - _textMate.SetTheme(_registryOptions.LoadTheme(ThemeName.DarkPlus)); - } - else - { - _textMate.SetTheme(_registryOptions.LoadTheme(ThemeName.LightPlus)); - } + Models.TextMateHelper.SetThemeByApp(_textMate); } } - private void UpdateGrammar() - { - if (_textMate == null || DiffData == null) return; - - var ext = Path.GetExtension(DiffData.File); - if (ext == ".h") - { - _textMate.SetGrammar(_registryOptions.GetScopeByLanguageId("cpp")); - } - else - { - _textMate.SetGrammar(_registryOptions.GetScopeByExtension(ext)); - } - } - - private RegistryOptions _registryOptions; private TextMate.Installation _textMate; + private LineStyleTransformer _lineStyleTransformer = null; } public class SingleSideTextDiffPresenter : TextEditor @@ -513,10 +480,9 @@ namespace SourceGit.Views private static readonly Brush HL_ADDED = new SolidColorBrush(Color.FromArgb(90, 0, 255, 0)); private static readonly Brush HL_DELETED = new SolidColorBrush(Color.FromArgb(80, 255, 0, 0)); - public LineStyleTransformer(SingleSideTextDiffPresenter editor, IBrush indicatorFG) + public LineStyleTransformer(SingleSideTextDiffPresenter editor) { _editor = editor; - _indicatorFG = indicatorFG; _indicatorTypeface = new Typeface("fonts:SourceGit#JetBrains Mono", FontStyle.Italic); } @@ -531,7 +497,7 @@ namespace SourceGit.Views { ChangeLinePart(line.Offset, line.EndOffset, v => { - v.TextRunProperties.SetForegroundBrush(_indicatorFG); + v.TextRunProperties.SetForegroundBrush(_editor.SecondaryFG); v.TextRunProperties.SetTypeface(_indicatorTypeface); }); @@ -552,7 +518,6 @@ namespace SourceGit.Views } private readonly SingleSideTextDiffPresenter _editor; - private readonly IBrush _indicatorFG = Brushes.DarkGray; private readonly Typeface _indicatorTypeface = Typeface.Default; } @@ -596,9 +561,16 @@ namespace SourceGit.Views public SingleSideTextDiffPresenter() : base(new TextArea(), new TextDocument()) { + _lineStyleTransformer = new LineStyleTransformer(this); + IsReadOnly = true; ShowLineNumbers = false; WordWrap = false; + + TextArea.LeftMargins.Add(new LineNumberMargin(this) { Margin = new Thickness(8, 0) }); + TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this)); + TextArea.TextView.Margin = new Thickness(4, 0); + TextArea.TextView.BackgroundRenderers.Add(new LineBackgroundRenderer(this)); } protected override void OnLoaded(RoutedEventArgs e) @@ -612,26 +584,11 @@ namespace SourceGit.Views _scrollViewer.ScrollChanged += OnTextViewScrollChanged; } - TextArea.LeftMargins.Add(new LineNumberMargin(this) { Margin = new Thickness(8, 0) }); - TextArea.LeftMargins.Add(new VerticalSeperatorMargin(this)); - TextArea.TextView.Margin = new Thickness(4, 0); - TextArea.TextView.BackgroundRenderers.Add(new LineBackgroundRenderer(this)); + _textMate = Models.TextMateHelper.CreateForEditor(this); + if (DiffData != null) Models.TextMateHelper.SetGrammarByFileName(_textMate, DiffData.File); + + TextArea.TextView.LineTransformers.Add(_lineStyleTransformer); TextArea.TextView.ContextRequested += OnTextViewContextRequested; - - if (App.Current?.ActualThemeVariant == ThemeVariant.Dark) - { - _registryOptions = new RegistryOptions(ThemeName.DarkPlus); - } - else - { - _registryOptions = new RegistryOptions(ThemeName.LightPlus); - } - - _textMate = this.InstallTextMate(_registryOptions); - UpdateGrammar(); - - // This line must after InstallTextMate - TextArea.TextView.LineTransformers.Add(new LineStyleTransformer(this, SecondaryFG)); } protected override void OnUnloaded(RoutedEventArgs e) @@ -644,13 +601,15 @@ namespace SourceGit.Views _scrollViewer = null; } - TextArea.LeftMargins.Clear(); - TextArea.TextView.BackgroundRenderers.Clear(); - TextArea.TextView.LineTransformers.Clear(); + if (_textMate != null) + { + _textMate.Dispose(); + _textMate = null; + } + + TextArea.TextView.LineTransformers.Remove(_lineStyleTransformer); TextArea.TextView.ContextRequested -= OnTextViewContextRequested; - _registryOptions = null; - _textMate.Dispose(); - _textMate = null; + GC.Collect(); } @@ -716,7 +675,7 @@ namespace SourceGit.Views } } - UpdateGrammar(); + Models.TextMateHelper.SetGrammarByFileName(_textMate, DiffData.File); Text = builder.ToString(); } else @@ -744,37 +703,14 @@ namespace SourceGit.Views } } } - else if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null && _textMate != null) + else if (change.Property.Name == "ActualThemeVariant" && change.NewValue != null) { - if (App.Current?.ActualThemeVariant == ThemeVariant.Dark) - { - _textMate.SetTheme(_registryOptions.LoadTheme(ThemeName.DarkPlus)); - } - else - { - _textMate.SetTheme(_registryOptions.LoadTheme(ThemeName.LightPlus)); - } + Models.TextMateHelper.SetThemeByApp(_textMate); } } - private void UpdateGrammar() - { - if (_textMate == null || DiffData == null) return; - - var ext = Path.GetExtension(DiffData.File); - if (ext == ".h") - { - _textMate.SetGrammar(_registryOptions.GetScopeByLanguageId("cpp")); - } - else - { - _textMate.SetGrammar(_registryOptions.GetScopeByExtension(ext)); - } - } - - private RegistryOptions _registryOptions; private TextMate.Installation _textMate; - + private LineStyleTransformer _lineStyleTransformer = null; private ScrollViewer _scrollViewer = null; private bool _syncScrollingByOthers = false; }