Skip to content

Commit

Permalink
Apply colors to diff viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
bcssov committed Mar 19, 2024
1 parent c0f28dc commit c360ac3
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 02-19-2024
//
// Last Modified By : Mario
// Last Modified On : 02-28-2024
// Last Modified On : 03-19-2024
// ***********************************************************************
// <copyright file="DiffBackgroundRenderer.cs" company="Mario">
// Mario
Expand All @@ -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
Expand All @@ -32,26 +29,13 @@ namespace IronyModManager.Implementation.AvaloniaEdit
/// <seealso cref="IBackgroundRenderer" />
public class DiffBackgroundRenderer : IBackgroundRenderer
{
#region Fields

/// <summary>
/// A private bool? named isLightTheme.
/// </summary>
private bool? isLightTheme;

/// <summary>
/// A private IThemeManager named themeManager.
/// </summary>
private IThemeManager themeManager;
#region Properties

/// <summary>
/// A private IThemeService named themeService.
/// Gets or sets a value representing the color converter.<see cref="IronyModManager.Implementation.AvaloniaEdit.EditorColorConverter" />
/// </summary>
private IThemeService themeService;

#endregion Fields

#region Properties
/// <value>The color converter.</value>
public EditorColorConverter ColorConverter { get; set; }

/// <summary>
/// Gets a value representing the layer.<see cref="KnownLayer" />
Expand Down Expand Up @@ -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
};

Expand All @@ -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;
Expand All @@ -142,22 +127,6 @@ public void Draw(TextView textView, DrawingContext drawingContext)
}
}

/// <summary>
/// Is light theme.
/// </summary>
/// <returns>A bool.</returns>
private bool IsLightTheme()
{
if (!isLightTheme.HasValue)
{
themeManager ??= DIResolver.Get<IThemeManager>();
themeService ??= DIResolver.Get<IThemeService>();
isLightTheme = themeManager.IsLightTheme(themeService.GetSelected().Type);
}

return isLightTheme.GetValueOrDefault();
}

#endregion Methods

#region Classes
Expand Down
51 changes: 12 additions & 39 deletions src/IronyModManager/Implementation/AvaloniaEdit/DiffMargin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 02-19-2024
//
// Last Modified By : Mario
// Last Modified On : 02-23-2024
// Last Modified On : 03-19-2024
// ***********************************************************************
// <copyright file="DiffMargin.cs" company="Mario">
// Mario
Expand All @@ -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
Expand Down Expand Up @@ -55,11 +52,6 @@ public class DiffMargin : AbstractMargin
/// </summary>
private double fontSize;

/// <summary>
/// A private bool? named isLightTheme.
/// </summary>
private bool? isLightTheme;

/// <summary>
/// The maximum line number length
/// </summary>
Expand All @@ -75,20 +67,16 @@ public class DiffMargin : AbstractMargin
/// </summary>
private AnchorSegment selectionStart;

/// <summary>
/// A private IThemeManager named themeManager.
/// </summary>
private IThemeManager themeManager;

/// <summary>
/// A private IThemeService named themeService.
/// </summary>
private IThemeService themeService;

#endregion Fields

#region Properties

/// <summary>
/// Gets or sets the color converter.
/// </summary>
/// <value>The color converter.</value>
public EditorColorConverter ColorConverter { get; set; }

/// <summary>
/// Gets or sets a value representing the lines.<see cref="System.Collections.Generic.IList{IronyModManager.ViewModels.Controls.MergeViewerControlViewModel.DiffPieceWithIndex}" />
/// </summary>
Expand Down Expand Up @@ -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
};

Expand Down Expand Up @@ -349,22 +338,6 @@ private SimpleSegment GetTextLineSegment(PointerEventArgs e)
return new SimpleSegment(startOffset, endOffset - startOffset);
}

/// <summary>
/// Is light theme.
/// </summary>
/// <returns>A bool.</returns>
private bool IsLightTheme()
{
if (!isLightTheme.HasValue)
{
themeManager ??= DIResolver.Get<IThemeManager>();
themeService ??= DIResolver.Get<IThemeService>();
isLightTheme = themeManager.IsLightTheme(themeService.GetSelected().Type);
}

return isLightTheme.GetValueOrDefault();
}

/// <summary>
/// Handles the <see cref="E:DocumentLineCountChanged" /> event.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace IronyModManager.Implementation.AvaloniaEdit
/// <summary>
/// Class EditorColorConverter.
/// </summary>
internal class EditorColorConverter(IConflictSolverColors color)
public class EditorColorConverter(IConflictSolverColors color)
{
#region Fields

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 03-20-2020
//
// Last Modified By : Mario
// Last Modified On : 03-07-2024
// Last Modified On : 03-19-2024
// ***********************************************************************
// <copyright file="MergeViewerControlViewModel.cs" company="Mario">
// Mario
Expand All @@ -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;
Expand Down Expand Up @@ -196,6 +198,14 @@ public class MergeViewerControlViewModel(
/// <value><c>true</c> if this instance can perform hot key actions; otherwise, <c>false</c>.</value>
public virtual bool CanPerformHotKeyActions { get; set; }

/// <summary>
/// Gets or sets a value representing the color converter.<see cref="IronyModManager.Implementation.AvaloniaEdit.EditorColorConverter"/>
/// </summary>
/// <value>
/// The color converter.
/// </value>
public virtual EditorColorConverter ColorConverter { get; protected set; }

/// <summary>
/// Gets or sets the copy all.
/// </summary>
Expand Down Expand Up @@ -645,6 +655,7 @@ public virtual void InitParameters()
EditorAvailable = true;
}

ColorConverter = new EditorColorConverter(DIResolver.Get<IConflictSolverColorsService>().Get());
EvaluateMergeType();
}

Expand Down
15 changes: 15 additions & 0 deletions src/IronyModManager/ViewModels/Controls/OptionsControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,21 @@ public virtual Color ConflictSolverModifiedLineColor
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!disposing)
{
return;
}

ResetInsertedLineCommand?.Dispose();
ResetInsertedLineCommand = null;
ResetModifiedLineCommand?.Dispose();
Expand Down
18 changes: 13 additions & 5 deletions src/IronyModManager/Views/Controls/MergeViewerControlView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 03-20-2020
//
// Last Modified By : Mario
// Last Modified On : 03-17-2024
// Last Modified On : 03-19-2024
// ***********************************************************************
// <copyright file="MergeViewerControlView.xaml.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit c360ac3

Please sign in to comment.