From c360ac3e06a0d3518da5d241d546d000505298a3 Mon Sep 17 00:00:00 2001 From: bcssov Date: Tue, 19 Mar 2024 22:43:52 +0100 Subject: [PATCH] Apply colors to diff viewer --- .../AvaloniaEdit/DiffBackgroundRenderer.cs | 59 +++++-------------- .../Implementation/AvaloniaEdit/DiffMargin.cs | 51 ++++------------ .../AvaloniaEdit/EditorColorConverter.cs | 2 +- .../Controls/MergeViewerControlViewModel.cs | 13 +++- .../Controls/OptionsControlViewModel.cs | 15 +++++ .../Controls/MergeViewerControlView.xaml.cs | 18 ++++-- 6 files changed, 67 insertions(+), 91 deletions(-) diff --git a/src/IronyModManager/Implementation/AvaloniaEdit/DiffBackgroundRenderer.cs b/src/IronyModManager/Implementation/AvaloniaEdit/DiffBackgroundRenderer.cs index 2106820a..55a68db3 100644 --- a/src/IronyModManager/Implementation/AvaloniaEdit/DiffBackgroundRenderer.cs +++ b/src/IronyModManager/Implementation/AvaloniaEdit/DiffBackgroundRenderer.cs @@ -4,7 +4,7 @@ // Created : 02-19-2024 // // Last Modified By : Mario -// Last Modified On : 02-28-2024 +// Last Modified On : 03-19-2024 // *********************************************************************** // // Mario @@ -19,9 +19,6 @@ using Avalonia.Media; using AvaloniaEdit.Document; using AvaloniaEdit.Rendering; -using IronyModManager.DI; -using IronyModManager.Platform.Themes; -using IronyModManager.Services.Common; using IronyModManager.ViewModels.Controls; namespace IronyModManager.Implementation.AvaloniaEdit @@ -32,26 +29,13 @@ namespace IronyModManager.Implementation.AvaloniaEdit /// public class DiffBackgroundRenderer : IBackgroundRenderer { - #region Fields - - /// - /// A private bool? named isLightTheme. - /// - private bool? isLightTheme; - - /// - /// A private IThemeManager named themeManager. - /// - private IThemeManager themeManager; + #region Properties /// - /// A private IThemeService named themeService. + /// Gets or sets a value representing the color converter. /// - private IThemeService themeService; - - #endregion Fields - - #region Properties + /// The color converter. + public EditorColorConverter ColorConverter { get; set; } /// /// Gets a value representing the layer. @@ -91,13 +75,14 @@ public void Draw(TextView textView, DrawingContext drawingContext) } var diff = Lines[num]; + ColorConverter ??= new EditorColorConverter(null); Brush brush = diff.Type switch { - DiffPlex.DiffBuilder.Model.ChangeType.Deleted => IsLightTheme() ? Constants.LightDiffDeletedLine : Constants.DarkDiffDeletedLine, - DiffPlex.DiffBuilder.Model.ChangeType.Inserted => IsLightTheme() ? Constants.LightDiffInsertedLine : Constants.DarkDiffInsertedLine, - DiffPlex.DiffBuilder.Model.ChangeType.Imaginary => IsLightTheme() ? Constants.LightDiffImaginaryLine : Constants.DarkDiffImaginaryLine, - DiffPlex.DiffBuilder.Model.ChangeType.Modified => IsLightTheme() ? Constants.LightDiffModifiedLine : Constants.DarkDiffModifiedLine, + DiffPlex.DiffBuilder.Model.ChangeType.Deleted => ColorConverter.GetDeletedLineBrush(), + DiffPlex.DiffBuilder.Model.ChangeType.Inserted => ColorConverter.GetInsertedLineBrush(), + DiffPlex.DiffBuilder.Model.ChangeType.Imaginary => ColorConverter.GetImaginaryLineBrush(), + DiffPlex.DiffBuilder.Model.ChangeType.Modified => ColorConverter.GetEditedLineBrush(), _ => default }; @@ -116,10 +101,10 @@ public void Draw(TextView textView, DrawingContext drawingContext) { var subPieceBrush = piece.Type switch { - DiffPlex.DiffBuilder.Model.ChangeType.Deleted => IsLightTheme() ? Constants.LightDiffDeletedPieces : Constants.DarkDiffDeletedPieces, - DiffPlex.DiffBuilder.Model.ChangeType.Inserted => IsLightTheme() ? Constants.LightDiffInsertedPieces : Constants.DarkDiffInsertedPieces, - DiffPlex.DiffBuilder.Model.ChangeType.Modified => IsLightTheme() ? Constants.LightDiffModifiedPieces : Constants.DarkDiffModifiedPieces, - DiffPlex.DiffBuilder.Model.ChangeType.Unchanged => IsLightTheme() ? Constants.LightDiffUnchangedPieces : Constants.DarkDiffUnchangedPieces, + DiffPlex.DiffBuilder.Model.ChangeType.Deleted => ColorConverter.GetDeletedLineBrush(), + DiffPlex.DiffBuilder.Model.ChangeType.Inserted => ColorConverter.GetInsertedLineBrush(), + DiffPlex.DiffBuilder.Model.ChangeType.Modified => ColorConverter.GetEditedLineBrush(), + DiffPlex.DiffBuilder.Model.ChangeType.Unchanged => ColorConverter.GetEditedLineBrush(), _ => default(Brush) }; var text = piece.Text ?? string.Empty; @@ -142,22 +127,6 @@ public void Draw(TextView textView, DrawingContext drawingContext) } } - /// - /// Is light theme. - /// - /// A bool. - private bool IsLightTheme() - { - if (!isLightTheme.HasValue) - { - themeManager ??= DIResolver.Get(); - themeService ??= DIResolver.Get(); - isLightTheme = themeManager.IsLightTheme(themeService.GetSelected().Type); - } - - return isLightTheme.GetValueOrDefault(); - } - #endregion Methods #region Classes diff --git a/src/IronyModManager/Implementation/AvaloniaEdit/DiffMargin.cs b/src/IronyModManager/Implementation/AvaloniaEdit/DiffMargin.cs index fd9f6eb8..0a60f782 100644 --- a/src/IronyModManager/Implementation/AvaloniaEdit/DiffMargin.cs +++ b/src/IronyModManager/Implementation/AvaloniaEdit/DiffMargin.cs @@ -4,7 +4,7 @@ // Created : 02-19-2024 // // Last Modified By : Mario -// Last Modified On : 02-23-2024 +// Last Modified On : 03-19-2024 // *********************************************************************** // // Mario @@ -25,9 +25,6 @@ using AvaloniaEdit.Editing; using AvaloniaEdit.Rendering; using AvaloniaEdit.Utils; -using IronyModManager.DI; -using IronyModManager.Platform.Themes; -using IronyModManager.Services.Common; using IronyModManager.ViewModels.Controls; namespace IronyModManager.Implementation.AvaloniaEdit @@ -55,11 +52,6 @@ public class DiffMargin : AbstractMargin /// private double fontSize; - /// - /// A private bool? named isLightTheme. - /// - private bool? isLightTheme; - /// /// The maximum line number length /// @@ -75,20 +67,16 @@ public class DiffMargin : AbstractMargin /// private AnchorSegment selectionStart; - /// - /// A private IThemeManager named themeManager. - /// - private IThemeManager themeManager; - - /// - /// A private IThemeService named themeService. - /// - private IThemeService themeService; - #endregion Fields #region Properties + /// + /// Gets or sets the color converter. + /// + /// The color converter. + public EditorColorConverter ColorConverter { get; set; } + /// /// Gets or sets a value representing the lines. /// @@ -124,13 +112,14 @@ public override void Render(DrawingContext context) } var diff = Lines[ln]; + ColorConverter ??= new EditorColorConverter(null); Brush brush = diff.Type switch { - DiffPlex.DiffBuilder.Model.ChangeType.Deleted => IsLightTheme() ? Constants.LightDiffDeletedLine : Constants.DarkDiffDeletedLine, - DiffPlex.DiffBuilder.Model.ChangeType.Inserted => IsLightTheme() ? Constants.LightDiffInsertedLine : Constants.DarkDiffInsertedLine, - DiffPlex.DiffBuilder.Model.ChangeType.Imaginary => IsLightTheme() ? Constants.LightDiffImaginaryLine : Constants.DarkDiffImaginaryLine, - DiffPlex.DiffBuilder.Model.ChangeType.Modified => IsLightTheme() ? Constants.LightDiffModifiedPieces : Constants.DarkDiffModifiedPieces, + DiffPlex.DiffBuilder.Model.ChangeType.Deleted => ColorConverter.GetDeletedLineBrush(), + DiffPlex.DiffBuilder.Model.ChangeType.Inserted => ColorConverter.GetInsertedLineBrush(), + DiffPlex.DiffBuilder.Model.ChangeType.Imaginary => ColorConverter.GetImaginaryLineBrush(), + DiffPlex.DiffBuilder.Model.ChangeType.Modified => ColorConverter.GetEditedLineBrush(), _ => default }; @@ -349,22 +338,6 @@ private SimpleSegment GetTextLineSegment(PointerEventArgs e) return new SimpleSegment(startOffset, endOffset - startOffset); } - /// - /// Is light theme. - /// - /// A bool. - private bool IsLightTheme() - { - if (!isLightTheme.HasValue) - { - themeManager ??= DIResolver.Get(); - themeService ??= DIResolver.Get(); - isLightTheme = themeManager.IsLightTheme(themeService.GetSelected().Type); - } - - return isLightTheme.GetValueOrDefault(); - } - /// /// Handles the event. /// diff --git a/src/IronyModManager/Implementation/AvaloniaEdit/EditorColorConverter.cs b/src/IronyModManager/Implementation/AvaloniaEdit/EditorColorConverter.cs index d8f1ecfe..7bbd2864 100644 --- a/src/IronyModManager/Implementation/AvaloniaEdit/EditorColorConverter.cs +++ b/src/IronyModManager/Implementation/AvaloniaEdit/EditorColorConverter.cs @@ -27,7 +27,7 @@ namespace IronyModManager.Implementation.AvaloniaEdit /// /// Class EditorColorConverter. /// - internal class EditorColorConverter(IConflictSolverColors color) + public class EditorColorConverter(IConflictSolverColors color) { #region Fields diff --git a/src/IronyModManager/ViewModels/Controls/MergeViewerControlViewModel.cs b/src/IronyModManager/ViewModels/Controls/MergeViewerControlViewModel.cs index c9329feb..4fe575b1 100644 --- a/src/IronyModManager/ViewModels/Controls/MergeViewerControlViewModel.cs +++ b/src/IronyModManager/ViewModels/Controls/MergeViewerControlViewModel.cs @@ -4,7 +4,7 @@ // Created : 03-20-2020 // // Last Modified By : Mario -// Last Modified On : 03-07-2024 +// Last Modified On : 03-19-2024 // *********************************************************************** // // Mario @@ -27,8 +27,10 @@ using DiffPlex.DiffBuilder.Model; using IronyModManager.Common; using IronyModManager.Common.ViewModels; +using IronyModManager.DI; using IronyModManager.Implementation.Actions; using IronyModManager.Implementation.AppState; +using IronyModManager.Implementation.AvaloniaEdit; using IronyModManager.Implementation.Hotkey; using IronyModManager.Localization; using IronyModManager.Localization.Attributes; @@ -196,6 +198,14 @@ public class MergeViewerControlViewModel( /// true if this instance can perform hot key actions; otherwise, false. public virtual bool CanPerformHotKeyActions { get; set; } + /// + /// Gets or sets a value representing the color converter. + /// + /// + /// The color converter. + /// + public virtual EditorColorConverter ColorConverter { get; protected set; } + /// /// Gets or sets the copy all. /// @@ -645,6 +655,7 @@ public virtual void InitParameters() EditorAvailable = true; } + ColorConverter = new EditorColorConverter(DIResolver.Get().Get()); EvaluateMergeType(); } diff --git a/src/IronyModManager/ViewModels/Controls/OptionsControlViewModel.cs b/src/IronyModManager/ViewModels/Controls/OptionsControlViewModel.cs index a195dc15..0637a83d 100644 --- a/src/IronyModManager/ViewModels/Controls/OptionsControlViewModel.cs +++ b/src/IronyModManager/ViewModels/Controls/OptionsControlViewModel.cs @@ -1509,6 +1509,21 @@ public virtual Color ConflictSolverModifiedLineColor /// public void Dispose() { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Releases unmanaged and - optionally - managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected virtual void Dispose(bool disposing) + { + if (!disposing) + { + return; + } + ResetInsertedLineCommand?.Dispose(); ResetInsertedLineCommand = null; ResetModifiedLineCommand?.Dispose(); diff --git a/src/IronyModManager/Views/Controls/MergeViewerControlView.xaml.cs b/src/IronyModManager/Views/Controls/MergeViewerControlView.xaml.cs index 8f516e6e..e5b1c43b 100644 --- a/src/IronyModManager/Views/Controls/MergeViewerControlView.xaml.cs +++ b/src/IronyModManager/Views/Controls/MergeViewerControlView.xaml.cs @@ -4,7 +4,7 @@ // Created : 03-20-2020 // // Last Modified By : Mario -// Last Modified On : 03-17-2024 +// Last Modified On : 03-19-2024 // *********************************************************************** // // Mario @@ -345,10 +345,10 @@ protected override void OnActivated(CompositeDisposable disposables) HandleEditorContextMenu(diffRight, diffSearchPanelRight, false); diffLeft.ScrollInitialized += (_, _) => HandleTextEditorPropertyChanged(diffLeft, diffRight); diffRight.ScrollInitialized += (_, _) => HandleTextEditorPropertyChanged(diffRight, diffLeft); - var diffLeftMargin = new DiffMargin { Lines = ViewModel!.LeftDiff }; - var diffRightMargin = new DiffMargin { Lines = ViewModel.RightDiff }; - var diffLeftRenderer = new DiffBackgroundRenderer { Lines = ViewModel.LeftDiff }; - var diffRightRenderer = new DiffBackgroundRenderer { Lines = ViewModel.RightDiff }; + var diffLeftMargin = new DiffMargin { Lines = ViewModel!.LeftDiff, ColorConverter = ViewModel.ColorConverter }; + var diffRightMargin = new DiffMargin { Lines = ViewModel.RightDiff, ColorConverter = ViewModel.ColorConverter }; + var diffLeftRenderer = new DiffBackgroundRenderer { Lines = ViewModel.LeftDiff, ColorConverter = ViewModel.ColorConverter }; + var diffRightRenderer = new DiffBackgroundRenderer { Lines = ViewModel.RightDiff, ColorConverter = ViewModel.ColorConverter }; diffLeft.TextArea.LeftMargins.Add(diffLeftMargin); diffLeft.TextArea.TextView.BackgroundRenderers.Add(diffLeftRenderer); diffRight.TextArea.LeftMargins.Add(diffRightMargin); @@ -618,6 +618,14 @@ void evalKey() RedrawEditorDiffs(); }; + this.WhenAnyValue(v => v.ViewModel.ColorConverter).Subscribe(s => + { + diffLeftMargin.ColorConverter = s; + diffRightMargin.ColorConverter = s; + diffLeftRenderer.ColorConverter = s; + diffRightRenderer.ColorConverter = s; + Dispatcher.UIThread.SafeInvoke(RedrawEditorDiffs); + }); base.OnActivated(disposables); }