Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions src/LogExpert.Configuration/ConfigManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Reflection;
Expand Down Expand Up @@ -259,6 +260,9 @@ public ImportResult Import (FileInfo fileInfo, ExportImportFlags importFlags)

// Proceed with import - Use Settings property to ensure _settings is initialized
_settings = Instance.Import(Instance.Settings, fileInfo, importFlags);
// Re-apply defaults and materialize runtime-only fields (e.g. Preferences.Font from FontString)
// since the import path bypasses Load/InitializeSettings.
_settings = InitializeSettings(_settings);
Save(SettingsFlags.All);

_logger.Info("Import completed successfully");
Expand Down Expand Up @@ -602,17 +606,7 @@ private static Settings InitializeSettings (Settings settings)

settings.FileColors ??= [];

try
{
using var fontFamily = new FontFamily(settings.Preferences.FontName);
settings.Preferences.FontName = fontFamily.Name;
}
catch (ArgumentException)
{
string genericMonospaceFont = FontFamily.GenericMonospace.Name;
_logger.Warn($"Specified font '{settings.Preferences.FontName}' not found. Falling back to default: '{genericMonospaceFont}'.");
settings.Preferences.FontName = genericMonospaceFont;
}
InitializeFont(settings);

if (settings.Preferences.ShowTailColor == Color.Empty)
{
Expand Down Expand Up @@ -680,6 +674,42 @@ private static Settings InitializeSettings (Settings settings)
return settings;
}

/// <summary>
/// Materializes the persisted <see cref="Preferences.FontString"/> into a live <see cref="Font"/>
/// instance <see cref="Preferences.FontString"/> is the source of truth in settings.json so
/// that family, size, style and unit round-trip through the FontDialog.
/// </summary>
private static void InitializeFont (Settings settings)
{
var converter = TypeDescriptor.GetConverter(typeof(Font));
var fallbackFamily = FontFamily.GenericMonospace.Name;

Font font = TryDeserializeFont(converter, settings.Preferences.FontString, "FontString")
?? new Font(fallbackFamily, 9f);

settings.Preferences.Font?.Dispose();
settings.Preferences.Font = font;
settings.Preferences.FontString = converter.ConvertToInvariantString(font);
}

private static Font? TryDeserializeFont (TypeConverter converter, string? value, string sourceLabel)
{
if (string.IsNullOrWhiteSpace(value))
{
return null;
}

try
{
return converter.ConvertFromInvariantString(value) as Font;
}
catch (Exception ex) when (ex is NotSupportedException or ArgumentException or FormatException)
{
_logger.Warn(ex, $"Could not deserialize font from {sourceLabel}='{value}'.");
return null;
}
}

/// <summary>
/// Saves the Settings to file, fires OnConfigChanged Event so LogTabWindow is updated
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/LogExpert.Core/Config/Preferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,16 @@ public List<HighlightGroup> HilightGroupList

public bool FollowTail { get; set; } = true;

[Obsolete("This setting is no longer used and will be removed in a future version. The 'FontString' will be used for Importing / Exporting the Font")]
public string FontName { get; set; } = "Courier New";

public string FontString { get; set; } = "Courier New, 9pt, style=Regular";

[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
public Font Font { get; set; }

[Obsolete("This setting is no longer used and will be removed in a future version. The 'FontString' will be used for Importing / Exporting the Font")]
public float FontSize { get => field; set => field = MathF.Round(value, 1); } = 9.0f;

public List<HighlightMaskEntry> HighlightMaskList { get; set; } = [];
Expand Down
4 changes: 3 additions & 1 deletion src/LogExpert.Core/Interfaces/ISharedToolWindow.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Drawing;

using ColumnizerLib;

using LogExpert.Core.Config;
Expand Down Expand Up @@ -26,7 +28,7 @@ public interface ISharedToolWindow

void SetColumnizer (ILogLineMemoryColumnizer columnizer);

void PreferencesChanged (string fontName, float fontSize, bool setLastColumnWidth, int lastColumnWidth, SettingsFlags flags);
void PreferencesChanged (Font font, bool setLastColumnWidth, int lastColumnWidth, SettingsFlags flags);

#endregion
}
6 changes: 3 additions & 3 deletions src/LogExpert.Tests/ConfigManagerTests/ConfigManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -678,12 +678,12 @@ public void Import_WithOtherFlag_ShouldMergePreferences ()
// Arrange
// Set up current settings
Settings currentSettings = _configManager.Settings;
currentSettings.Preferences.FontSize = 10;
currentSettings.Preferences.FontString = "Courier New, 9pt, style=Regular";
currentSettings.Preferences.ColumnizerMaskList.Clear();

// Create import settings with different preferences
Settings importSettings = CreateTestSettings();
importSettings.Preferences.FontSize = 12;
importSettings.Preferences.FontString = "Arial, 12pt, style=Bold";
importSettings.Preferences.ShowBubbles = true;

string importFilePath = Path.Join(_testDir, "import_other.json");
Expand All @@ -698,7 +698,7 @@ public void Import_WithOtherFlag_ShouldMergePreferences ()
Assert.That(result.Success, Is.True);

Settings updatedSettings = _configManager.Settings;
Assert.That(updatedSettings.Preferences.FontSize, Is.EqualTo(12), "Preferences should be merged from import file");
Assert.That(updatedSettings.Preferences.FontString, Is.EqualTo("Arial, 12pt, style=Bold"), "Preferences should be merged from import file");
}

[Test]
Expand Down
6 changes: 2 additions & 4 deletions src/LogExpert.Tests/Services/ToolWindowCoordinatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ public void ApplyPreferences_DoesNotThrow_WhenInitialized ()
_coordinator.Initialize();

// Act & Assert
Assert.DoesNotThrow(() =>
_coordinator.ApplyPreferences("Courier New", 10f, true, 500, SettingsFlags.All));
Assert.DoesNotThrow(() => _coordinator.ApplyPreferences(new Font("Courier New", 10f), true, 500, SettingsFlags.All));
}

[Test]
Expand Down Expand Up @@ -193,8 +192,7 @@ public void GetDockContent_BeforeInitialize_ReturnsNull ()
public void ApplyPreferences_BeforeInitialize_DoesNotThrow ()
{
// Act & Assert
Assert.DoesNotThrow(() =>
_coordinator.ApplyPreferences("Courier New", 10f, true, 500, SettingsFlags.All));
Assert.DoesNotThrow(() => _coordinator.ApplyPreferences(new Font("Courier New", 10f), true, 500, SettingsFlags.All));
}

[Test]
Expand Down
20 changes: 9 additions & 11 deletions src/LogExpert.UI/Controls/LogWindow/LogWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -821,10 +821,8 @@ private void OnLogWindowLoad (object sender, EventArgs e)
{
var setLastColumnWidth = Preferences.SetLastColumnWidth;
var lastColumnWidth = Preferences.LastColumnWidth;
var fontName = Preferences.FontName;
var fontSize = Preferences.FontSize;

PreferencesChanged(fontName, fontSize, setLastColumnWidth, lastColumnWidth, true, SettingsFlags.GuiOrColors);
PreferencesChanged(Preferences.Font, setLastColumnWidth, lastColumnWidth, true, SettingsFlags.GuiOrColors);
}

[SupportedOSPlatform("windows")]
Expand Down Expand Up @@ -2948,10 +2946,8 @@ private void LoadingFinished ()

var setLastColumnWidth = Preferences.SetLastColumnWidth;
var lastColumnWidth = Preferences.LastColumnWidth;
var fontName = Preferences.FontName;
var fontSize = Preferences.FontSize;

PreferencesChanged(fontName, fontSize, setLastColumnWidth, lastColumnWidth, true, SettingsFlags.All);
PreferencesChanged(Preferences.Font, setLastColumnWidth, lastColumnWidth, true, SettingsFlags.All);
//LoadPersistenceData();
}

Expand Down Expand Up @@ -5433,7 +5429,7 @@ private void InitPatternWindow ()
_patternWindow = new PatternWindow(this);
_patternWindow.SetColumnizer(CurrentColumnizer);
//this.patternWindow.SetBlockList(blockList);
_patternWindow.SetFont(Preferences.FontName, Preferences.FontSize);
_patternWindow.SetFont(Preferences.Font);
_patternWindow.Fuzzy = _patternArgs.Fuzzy;
_patternWindow.MaxDiff = _patternArgs.MaxDiffInBlock;
_patternWindow.MaxMisses = _patternArgs.MaxMisses;
Expand Down Expand Up @@ -6755,7 +6751,7 @@ public void CellPainting (bool focused, int rowIndex, int columnIndex, bool isFi
var stringToDraw = isFilteredGridView ? "!" : "i";

using var brush2 = new SolidBrush(Color.FromArgb(255, 190, 100, 0)); //dark orange
using var font = new Font(fontName, Preferences.FontSize, FontStyle.Bold);
using var font = new Font(fontName, Preferences.Font.Size, FontStyle.Bold);
e.Graphics.DrawString(stringToDraw, font, brush2, new RectangleF(rect.Left, rect.Top, rect.Width, rect.Height), _format);
}
}
Expand Down Expand Up @@ -7777,13 +7773,15 @@ public void Reload ()
}
}

public void PreferencesChanged (string fontName, float fontSize, bool setLastColumnWidth, int lastColumnWidth, bool isLoadTime, SettingsFlags flags)
public void PreferencesChanged (Font font, bool setLastColumnWidth, int lastColumnWidth, bool isLoadTime, SettingsFlags flags)
{
if ((flags & SettingsFlags.GuiOrColors) == SettingsFlags.GuiOrColors)
{
NormalFont = new Font(new FontFamily(fontName), fontSize);
font ??= Preferences.Font ?? new Font(FontFamily.GenericMonospace, 9f);

NormalFont = font;
BoldFont = new Font(NormalFont, FontStyle.Bold);
MonospacedFont = new Font(FONT_COURIER_NEW, Preferences.FontSize, FontStyle.Bold);
MonospacedFont = new Font(FONT_COURIER_NEW, NormalFont.Size, FontStyle.Bold);

var lineSpacing = NormalFont.FontFamily.GetLineSpacing(FontStyle.Regular);
var lineSpacingPixel = NormalFont.Size * lineSpacing / NormalFont.FontFamily.GetEmHeight(FontStyle.Regular);
Expand Down
3 changes: 1 addition & 2 deletions src/LogExpert.UI/Controls/LogWindow/PatternWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,8 @@ public void SetColumnizer (ILogLineMemoryColumnizer columnizer)
contentDataGridView.Columns.Insert(1, contentInfoColumn);
}

public void SetFont (string fontName, float fontSize)
public void SetFont (Font font)
{
Font font = new(new FontFamily(fontName), fontSize);
//var lineSpacing = font.FontFamily.GetLineSpacing(FontStyle.Regular);
//var lineSpacingPixel = font.Size * lineSpacing / font.FontFamily.GetEmHeight(FontStyle.Regular);

Expand Down
7 changes: 3 additions & 4 deletions src/LogExpert.UI/Dialogs/BookmarkWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ public void SetBookmarkData (IBookmarkData bookmarkData)
HideIfNeeded();
}

public void PreferencesChanged (string fontName, float fontSize, bool setLastColumnWidth, int lastColumnWidth, SettingsFlags flags)
public void PreferencesChanged (Font font, bool setLastColumnWidth, int lastColumnWidth, SettingsFlags flags)
{
if ((flags & SettingsFlags.GuiOrColors) == SettingsFlags.GuiOrColors)
{
SetFont(fontName, fontSize);
SetFont(font);
if (bookmarkDataGridView.Columns.Count > 1 && setLastColumnWidth)
{
bookmarkDataGridView.Columns[bookmarkDataGridView.Columns.Count - 1].MinimumWidth = lastColumnWidth;
Expand Down Expand Up @@ -269,9 +269,8 @@ protected override void OnPaint (PaintEventArgs e)

#region Private Methods

private void SetFont (string fontName, float fontSize)
private void SetFont (Font font)
{
Font font = new(new FontFamily(fontName), fontSize);
bookmarkDataGridView.DefaultCellStyle.Font = font;
bookmarkDataGridView.RowTemplate.Height = font.Height + 4;
bookmarkDataGridView.Refresh();
Expand Down
6 changes: 2 additions & 4 deletions src/LogExpert.UI/Dialogs/LogTabWindow/LogTabWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,15 +1226,13 @@ private void NotifyWindowsForChangedPrefs (SettingsFlags flags)

var setLastColumnWidth = ConfigManager.Settings.Preferences.SetLastColumnWidth;
var lastColumnWidth = ConfigManager.Settings.Preferences.LastColumnWidth;
var fontName = ConfigManager.Settings.Preferences.FontName;
var fontSize = ConfigManager.Settings.Preferences.FontSize;

foreach (var logWindow in _tabController.GetAllWindows())
{
logWindow.PreferencesChanged(fontName, fontSize, setLastColumnWidth, lastColumnWidth, false, flags);
logWindow.PreferencesChanged(ConfigManager.Settings.Preferences.Font, setLastColumnWidth, lastColumnWidth, false, flags);
}

_toolWindowCoordinator.ApplyPreferences(fontName, fontSize, setLastColumnWidth, lastColumnWidth, flags);
_toolWindowCoordinator.ApplyPreferences(ConfigManager.Settings.Preferences.Font, setLastColumnWidth, lastColumnWidth, flags);

HighlightGroupList = ConfigManager.Settings.Preferences.HighlightGroupList;
if ((flags & SettingsFlags.HighlightSettings) == SettingsFlags.HighlightSettings)
Expand Down
35 changes: 17 additions & 18 deletions src/LogExpert.UI/Dialogs/SettingsDialog.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel;
using System.Globalization;
using System.Runtime.Versioning;
using System.Security;
Expand Down Expand Up @@ -25,7 +26,7 @@ internal partial class SettingsDialog : Form

private readonly Image _emptyImage = new Bitmap(16, 16);
private readonly LogTabWindow _logTabWin;
private const string DEFAULT_FONT_NAME = "Courier New";
private const float DEFAULT_FONT_SIZE = 9.0f;

private ILogExpertPluginConfigurator _selectedPlugin;
private ToolEntry _selectedTool;
Expand Down Expand Up @@ -119,16 +120,6 @@ private void FillDialog ()
{
Preferences ??= new Preferences();

if (Preferences.FontName == null)
{
Preferences.FontName = DEFAULT_FONT_NAME;
}

if (Math.Abs(Preferences.FontSize) <= 0.1)
{
Preferences.FontSize = 9.0f;
}

FillPortableMode();

checkBoxDarkMode.Checked = Preferences.DarkMode;
Expand Down Expand Up @@ -266,8 +257,10 @@ private void FillPortableMode ()

private void DisplayFontName ()
{
labelFont.Text = $"{Preferences.FontName} {Preferences.FontSize}";
labelFont.Font = new Font(new FontFamily(Preferences.FontName), Preferences.FontSize);
var font = Preferences.Font ?? new Font(FontFamily.GenericMonospace, DEFAULT_FONT_SIZE);
var style = font.Style == FontStyle.Regular ? string.Empty : $" {font.Style}";
labelFont.Text = $"{font.Name} {font.Size}{style}";
labelFont.Font = font;
}

private void SaveMultifileData ()
Expand Down Expand Up @@ -714,18 +707,24 @@ private void OnSettingsDialogLoad (object sender, EventArgs e)

private void OnBtnChangeFontClick (object sender, EventArgs e)
{
FontDialog dlg = new()
var currentFont = Preferences.Font ?? new Font(FontFamily.GenericMonospace, DEFAULT_FONT_SIZE);

using FontDialog dlg = new()
{
ShowEffects = false,
ShowEffects = true,
AllowVerticalFonts = false,
AllowScriptChange = false,
Font = new Font(new FontFamily(Preferences.FontName), Preferences.FontSize)
Font = currentFont
};

if (dlg.ShowDialog() == DialogResult.OK)
{
Preferences.FontSize = dlg.Font.Size;
Preferences.FontName = dlg.Font.FontFamily.Name;
var converter = TypeDescriptor.GetConverter(typeof(Font));
var selected = (Font)dlg.Font.Clone();

Preferences.Font?.Dispose();
Preferences.Font = selected;
Preferences.FontString = converter.ConvertToInvariantString(selected);
}

DisplayFontName();
Expand Down
2 changes: 1 addition & 1 deletion src/LogExpert.UI/Interface/ILogPaintContextUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal interface ILogPaintContextUI : ILogPaintContext
{
#region Properties

Font MonospacedFont { get; } // Font font = new Font("Courier New", this.Preferences.fontSize, FontStyle.Bold);
Font MonospacedFont { get; }

Font NormalFont { get; }

Expand Down
2 changes: 1 addition & 1 deletion src/LogExpert.UI/Interface/IToolWindowCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal interface IToolWindowCoordinator : IDisposable
/// <summary>
/// Forwards preference changes to the BookmarkWindow.
/// </summary>
void ApplyPreferences (string fontName, float fontSize, bool setLastColumnWidth, int lastColumnWidth, SettingsFlags flags);
void ApplyPreferences (Font font, bool setLastColumnWidth, int lastColumnWidth, SettingsFlags flags);

/// <summary>
/// Sets the line column visibility on the BookmarkWindow.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@
using LogExpert.UI.Controls.LogWindow;
using LogExpert.UI.Interface;

using NLog;

using WeifenLuo.WinFormsUI.Docking;

namespace LogExpert.UI.Services.ToolWindowCoordinatorService;

[SupportedOSPlatform("windows")]
internal sealed class ToolWindowCoordinator (IConfigManager configManager) : IToolWindowCoordinator
{
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

private readonly IConfigManager _configManager = configManager;

private BookmarkWindow? _bookmarkWindow;
Expand All @@ -36,7 +32,7 @@ public void Initialize ()
};

var prefs = _configManager.Settings.Preferences;
_bookmarkWindow.PreferencesChanged(prefs.FontName, prefs.FontSize, prefs.SetLastColumnWidth, prefs.LastColumnWidth, SettingsFlags.All);
_bookmarkWindow.PreferencesChanged(prefs.Font, prefs.SetLastColumnWidth, prefs.LastColumnWidth, SettingsFlags.All);
_bookmarkWindow.VisibleChanged += OnBookmarkWindowVisibleChanged;
_firstBookmarkWindowShow = true;
}
Expand Down Expand Up @@ -119,9 +115,9 @@ public void ToggleBookmarkVisibility (DockPanel dockPanel)
: (IDockContent?)null;
}

public void ApplyPreferences (string fontName, float fontSize, bool setLastColumnWidth, int lastColumnWidth, SettingsFlags flags)
public void ApplyPreferences (Font font, bool setLastColumnWidth, int lastColumnWidth, SettingsFlags flags)
{
_bookmarkWindow?.PreferencesChanged(fontName, fontSize, setLastColumnWidth, lastColumnWidth, flags);
_bookmarkWindow?.PreferencesChanged(font, setLastColumnWidth, lastColumnWidth, flags);
}

public void SetLineColumnVisible (bool visible)
Expand Down
Loading