From c765a9262d054b18a560581e2493a1b0fc77e0b9 Mon Sep 17 00:00:00 2001 From: Natestah Date: Thu, 4 Jul 2024 06:54:02 -0700 Subject: [PATCH] Avalonia.TextMate - Review Updates -Remove apply window updates bool. -Remove duplicated CurrentHighlightRenderer Background/border settings, this is replaced with a new call for but doesn't answer the review note fully. --- src/AvaloniaEdit.Demo/App.xaml.cs | 23 ++++- src/AvaloniaEdit.Demo/MainWindow.xaml.cs | 5 + src/AvaloniaEdit.TextMate/TextMate.cs | 94 +++++++------------ .../Rendering/CurrentLineHighlightRenderer.cs | 6 ++ src/AvaloniaEdit/Rendering/TextView.cs | 5 + 5 files changed, 67 insertions(+), 66 deletions(-) diff --git a/src/AvaloniaEdit.Demo/App.xaml.cs b/src/AvaloniaEdit.Demo/App.xaml.cs index 86e01cf..bd7bfb6 100644 --- a/src/AvaloniaEdit.Demo/App.xaml.cs +++ b/src/AvaloniaEdit.Demo/App.xaml.cs @@ -3,6 +3,9 @@ using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.Platform; using Avalonia.Markup.Xaml; using System; +using System.ComponentModel; +using Avalonia.Styling; +using AvaloniaEdit.Demo.ViewModels; namespace AvaloniaEdit.Demo { @@ -17,13 +20,12 @@ namespace AvaloniaEdit.Demo { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime) { - var window = new MainWindow(); - desktopLifetime.MainWindow = window; - //PlatformManager.CreateEmbeddableWindow().Crea - - + if (window.DataContext is MainWindowViewModel mainWindowViewModel) + { + mainWindowViewModel.PropertyChanged +=MainWindowViewModelOnPropertyChanged; + } } else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime) { @@ -40,5 +42,16 @@ namespace AvaloniaEdit.Demo base.OnFrameworkInitializationCompleted(); } + + private void MainWindowViewModelOnPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (DataContext is not MainWindowViewModel mainWindowViewModel) return; + if (e.PropertyName == nameof(MainWindowViewModel.SelectedTheme)) + { + RequestedThemeVariant = mainWindowViewModel.SelectedTheme.ThemeName.ToString().Contains("light") + ? ThemeVariant.Light + : ThemeVariant.Dark; + } + } } } diff --git a/src/AvaloniaEdit.Demo/MainWindow.xaml.cs b/src/AvaloniaEdit.Demo/MainWindow.xaml.cs index e78e1cf..987767a 100644 --- a/src/AvaloniaEdit.Demo/MainWindow.xaml.cs +++ b/src/AvaloniaEdit.Demo/MainWindow.xaml.cs @@ -154,6 +154,11 @@ namespace AvaloniaEdit.Demo { _customMargin.SetDefaultBackgroundBrush(); } + + //Applying the Editor background to the whole window for demo sake. + e.ApplyBrushAction("editor.background",brush => Background = brush); + e.ApplyBrushAction("editor.foreground",brush => Foreground = brush); + } private void Caret_PositionChanged(object sender, EventArgs e) diff --git a/src/AvaloniaEdit.TextMate/TextMate.cs b/src/AvaloniaEdit.TextMate/TextMate.cs index faf0d38..92bf614 100644 --- a/src/AvaloniaEdit.TextMate/TextMate.cs +++ b/src/AvaloniaEdit.TextMate/TextMate.cs @@ -82,7 +82,7 @@ namespace AvaloniaEdit.TextMate return true; } - public void SetTheme(IRawTheme theme, bool applyWindowProperties = true) + public void SetTheme(IRawTheme theme) { _textMateRegistry.SetTheme(theme); @@ -93,70 +93,42 @@ namespace AvaloniaEdit.TextMate _editorModel?.InvalidateViewPortLines(); - if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime - { - MainWindow: Window window - }) + _guiColorDictionary = registryTheme.GetGuiColorDictionary(); + + ApplyBrushAction("editor.background",brush =>_editor.Background = brush); + ApplyBrushAction("editor.foreground",brush =>_editor.Foreground = brush); + + if (_defaultSelectionBrush == null) { - _guiColorDictionary = registryTheme.GetGuiColorDictionary(); - var themeName = theme.GetName(); - - // Applies to the application on a whole.. Some might want to opt out of this for their own setups. - if (applyWindowProperties) - { - if (themeName != null && themeName.ToLower().Contains("light")) - { - Application.Current.RequestedThemeVariant = ThemeVariant.Light; - } - else - { - Application.Current.RequestedThemeVariant = ThemeVariant.Dark; - } - - ApplyBrushAction("editor.background",brush =>window.Background = brush); - ApplyBrushAction("editor.foreground",brush =>window.Foreground = brush); - } - - ApplyBrushAction("editor.background",brush =>_editor.Background = brush); - ApplyBrushAction("editor.foreground",brush =>_editor.Foreground = brush); - - if (_defaultSelectionBrush == null) - { - _defaultSelectionBrush = _editor.TextArea.SelectionBrush; - } - - if (!ApplyBrushAction("editor.selectionBackground", - brush => _editor.TextArea.SelectionBrush = brush)) - { - _editor.TextArea.SelectionBrush = _defaultSelectionBrush; - } - - if (!ApplyBrushAction("editor.lineHighlightBackground", - brush => - { - _editor.TextArea.TextView.CurrentLineBackground = brush; - _editor.TextArea.TextView.CurrentLineBorder = new Pen(brush); // Todo: VS Code didn't seem to have a border but it might be nice to have that option. For now just make it the same.. - })) - { - _editor.TextArea.TextView.CurrentLineBackground = new SolidColorBrush(CurrentHighlightRendererDefaultBackground); - _editor.TextArea.TextView.CurrentLineBorder = new Pen(new SolidColorBrush(CurrentHighlightRendererDefaultBorder)); - } - - - //Todo: looks like the margin doesn't have a active line highlight, would be a nice addition - if (!ApplyBrushAction("editorLineNumber.foreground", - brush => _editor.LineNumbersForeground = brush)) - { - _editor.LineNumbersForeground = _editor.Foreground; - } - - AppliedTheme?.Invoke(this,this); + _defaultSelectionBrush = _editor.TextArea.SelectionBrush; } + + if (!ApplyBrushAction("editor.selectionBackground", + brush => _editor.TextArea.SelectionBrush = brush)) + { + _editor.TextArea.SelectionBrush = _defaultSelectionBrush; + } + + if (!ApplyBrushAction("editor.lineHighlightBackground", + brush => + { + _editor.TextArea.TextView.CurrentLineBackground = brush; + _editor.TextArea.TextView.CurrentLineBorder = new Pen(brush); // Todo: VS Code didn't seem to have a border but it might be nice to have that option. For now just make it the same.. + })) + { + _editor.TextArea.TextView.SetDefaultHighlightLineColors(); + } + + //Todo: looks like the margin doesn't have a active line highlight, would be a nice addition + if (!ApplyBrushAction("editorLineNumber.foreground", + brush => _editor.LineNumbersForeground = brush)) + { + _editor.LineNumbersForeground = _editor.Foreground; + } + + AppliedTheme?.Invoke(this,this); } - //These come out of CurrentHighlightRenderer sealed class. - public static readonly Color CurrentHighlightRendererDefaultBackground = Color.FromArgb(22, 20, 220, 224); - public static readonly Color CurrentHighlightRendererDefaultBorder = Color.FromArgb(52, 0, 255, 110); public void Dispose() { diff --git a/src/AvaloniaEdit/Rendering/CurrentLineHighlightRenderer.cs b/src/AvaloniaEdit/Rendering/CurrentLineHighlightRenderer.cs index 1d193be..70d385e 100644 --- a/src/AvaloniaEdit/Rendering/CurrentLineHighlightRenderer.cs +++ b/src/AvaloniaEdit/Rendering/CurrentLineHighlightRenderer.cs @@ -37,6 +37,12 @@ namespace AvaloniaEdit.Rendering #region Properties + public void SetDefaultColors() + { + BorderPen = new ImmutablePen(new ImmutableSolidColorBrush(DefaultBorder), 1); + BackgroundBrush = new ImmutableSolidColorBrush(DefaultBackground); + } + public int Line { get { return _line; } set { diff --git a/src/AvaloniaEdit/Rendering/TextView.cs b/src/AvaloniaEdit/Rendering/TextView.cs index 58d14e3..10e47c8 100644 --- a/src/AvaloniaEdit/Rendering/TextView.cs +++ b/src/AvaloniaEdit/Rendering/TextView.cs @@ -2099,5 +2099,10 @@ namespace AvaloniaEdit.Rendering } Size IScrollable.Viewport => _scrollViewport; + + public void SetDefaultHighlightLineColors() + { + _currentLineHighlightRenderer?.SetDefaultColors(); + } } }