From d1ebbbfd73b2d541483156fc4d799d4852d12904 Mon Sep 17 00:00:00 2001 From: Raphael Noeldner Date: Tue, 6 Jun 2023 19:12:54 +0200 Subject: [PATCH] Passing on FontConfig to Progress --- Application/FindSkipRows.cs | 2 +- Application/FormEditSettings.cs | 2 +- Application/FormMain.cs | 2 +- Library/ClassLibraryCSV/IFontConfig.cs | 14 ++++++++++++++ Library/WinFormControls/DetailControl.cs | 4 ++-- Library/WinFormControls/Extensions.cs | 2 +- Library/WinFormControls/FormColumnUIRead.cs | 6 +++--- Library/WinFormControls/FormCsvTextDisplay.cs | 4 ++-- .../WinFormControls/FormDuplicatesDisplay.cs | 2 +- Library/WinFormControls/FormHierarchyDisplay.cs | 4 ++-- Library/WinFormControls/FormProcessDisplay.cs | 7 +++---- Library/WinFormControls/FormUniqueDisplay.cs | 2 +- Library/WinFormControls/ResizeForm.cs | 17 ++++++++++------- .../FormProcessDisplayTests.cs | 12 ++++++------ 14 files changed, 48 insertions(+), 32 deletions(-) diff --git a/Application/FindSkipRows.cs b/Application/FindSkipRows.cs index ed81ccb5..dce3f033 100644 --- a/Application/FindSkipRows.cs +++ b/Application/FindSkipRows.cs @@ -51,7 +51,7 @@ private void HighlightVisibleRange(int skipRows) private void ButtonSkipLine_Click(object? sender, EventArgs e) { - using var formProgress = new FormProgress("Check", true, CancellationToken.None); + using var formProgress = new FormProgress("Check", true, FontConfig, CancellationToken.None); formProgress.ShowWithFont(this); formProgress.Maximum = 0; using var stream = new ImprovedStream(new SourceAccess(m_CsvFile)); diff --git a/Application/FormEditSettings.cs b/Application/FormEditSettings.cs index f9030154..5a3bf7fd 100644 --- a/Application/FormEditSettings.cs +++ b/Application/FormEditSettings.cs @@ -120,7 +120,7 @@ private async void BtnOpenFile_Click(object? sender, EventArgs e) if (m_FileSetting == null) { SetDefaultInspectionResult(); - using var formProgress = new FormProgress("Examining file", false, m_CancellationTokenSource.Token); + using var formProgress = new FormProgress("Examining file", false, FontConfig, m_CancellationTokenSource.Token); formProgress.Maximum = 0; formProgress.ShowWithFont(this); diff --git a/Application/FormMain.cs b/Application/FormMain.cs index 7fff5481..0d5931bb 100644 --- a/Application/FormMain.cs +++ b/Application/FormMain.cs @@ -594,7 +594,7 @@ private async Task OpenDataReaderAsync(CancellationToken cancellationToken) }); Logger.Debug("Loading Batch"); - using (var formProgress = new FormProgress(fileNameShort, false, cancellationToken)) + using (var formProgress = new FormProgress(fileNameShort, false, FontConfig, cancellationToken)) { formProgress.ShowWithFont(this); noError = await detailControl.LoadSettingAsync(m_FileSetting, false, true, m_ViewSettings.DurationTimeSpan, diff --git a/Library/ClassLibraryCSV/IFontConfig.cs b/Library/ClassLibraryCSV/IFontConfig.cs index 6985bc65..66f3027a 100644 --- a/Library/ClassLibraryCSV/IFontConfig.cs +++ b/Library/ClassLibraryCSV/IFontConfig.cs @@ -21,4 +21,18 @@ public interface IFontConfig : INotifyPropertyChanged string Font { get; } float FontSize { get; } } + + public class FontConfig : ObservableObject, IFontConfig + { + private readonly string m_Font; + private readonly float m_FontSize; + + public FontConfig(string? font = null, float? fontSize = null) + { + m_Font=font ?? "Segoe UI"; + m_FontSize=fontSize ?? 8.25F; + } + public string Font => m_Font; + public float FontSize => m_FontSize; + } } \ No newline at end of file diff --git a/Library/WinFormControls/DetailControl.cs b/Library/WinFormControls/DetailControl.cs index 4c0a6fb4..f372255d 100644 --- a/Library/WinFormControls/DetailControl.cs +++ b/Library/WinFormControls/DetailControl.cs @@ -750,7 +750,7 @@ public async Task SafeCurrentFile(string fileName) for (var i = 0; i < physSource.SkipRows; i++) skippedLines.AppendLine(await sr.ReadLineAsync()); } - using var formProgress = new FormProgress("Writing file", true, m_CancellationToken); + using var formProgress = new FormProgress("Writing file", true, new FontConfig(Font.Name, Font.Size), m_CancellationToken); try { formProgress.ShowWithFont(this); @@ -851,7 +851,7 @@ private async void ToolStripButtonLoadRemaining_Click(object? sender, EventArgs // ReSharper disable once LocalizableElement m_ToolStripLabelCount.Text = " loading..."; - using var formProgress = new FormProgress("Load more...", false, m_CancellationToken); + using var formProgress = new FormProgress("Load more...", false, new FontConfig(Font.Name, Font.Size), m_CancellationToken); formProgress.ShowWithFont(this); formProgress.Maximum = 100; diff --git a/Library/WinFormControls/Extensions.cs b/Library/WinFormControls/Extensions.cs index 823dd253..5fedb72a 100644 --- a/Library/WinFormControls/Extensions.cs +++ b/Library/WinFormControls/Extensions.cs @@ -127,7 +127,7 @@ public static void CtrlA(this Form frm, object? sender, KeyEventArgs e) { if (!fileSetting.ShowProgress) return null; - var formProgress = new FormProgress(fileSetting.ToString(), withLogger, cancellationToken); + var formProgress = new FormProgress(fileSetting.ToString(), withLogger, new FontConfig(owner?.Font.Name, owner?.Font.Size), cancellationToken); formProgress.ShowWithFont(owner); return formProgress; } diff --git a/Library/WinFormControls/FormColumnUIRead.cs b/Library/WinFormControls/FormColumnUIRead.cs index 39c8fcb2..cf05db1f 100644 --- a/Library/WinFormControls/FormColumnUIRead.cs +++ b/Library/WinFormControls/FormColumnUIRead.cs @@ -126,7 +126,7 @@ private void AddDateFormat(string format) private async Task DisplayValues() { - using var formProgress = new FormProgress("Display Values", true, m_CancellationTokenSource.Token); + using var formProgress = new FormProgress("Display Values", true, FontConfig, m_CancellationTokenSource.Token); formProgress.ShowWithFont(this); var values = await GetSampleValuesAsync(comboBoxColumnName.Text, formProgress, formProgress.CancellationToken); @@ -165,7 +165,7 @@ private async Task Guess() await buttonGuess.RunWithHourglassAsync(async () => { - using var formProgress = new FormProgress("Guess Value", true, m_CancellationTokenSource.Token); + using var formProgress = new FormProgress("Guess Value", true, FontConfig, m_CancellationTokenSource.Token); formProgress.ShowWithFont(this); { var samples = await GetSampleValuesAsync(columnName, formProgress, formProgress.CancellationToken); @@ -533,7 +533,7 @@ private async void ColumnFormatUI_Load(object? sender, EventArgs e) columnBindingSource.DataSource = m_ColumnEdit; SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged; - using var formProgress = new FormProgress("Getting column headers", false, m_CancellationTokenSource.Token); + using var formProgress = new FormProgress("Getting column headers", false, FontConfig, m_CancellationTokenSource.Token); formProgress.ShowWithFont(this); formProgress.SetProcess("Getting columns from source"); HashSet allColumns = new HashSet(StringComparer.OrdinalIgnoreCase); diff --git a/Library/WinFormControls/FormCsvTextDisplay.cs b/Library/WinFormControls/FormCsvTextDisplay.cs index d80b36d1..7c05a137 100644 --- a/Library/WinFormControls/FormCsvTextDisplay.cs +++ b/Library/WinFormControls/FormCsvTextDisplay.cs @@ -100,7 +100,7 @@ private async Task OriginalStream(CancellationToken cancellationToken) { await textBox.RunWithHourglassAsync(async () => { - using var formProgress = new FormProgress("Display Source", false, cancellationToken); + using var formProgress = new FormProgress("Display Source", false, FontConfig, cancellationToken); formProgress.ShowWithFont(this); textBox.ClearUndo(); formProgress.Report(new ProgressInfo("Display of read file")); @@ -121,7 +121,7 @@ private void PrettyPrintStream(CancellationToken cancellationToken) { textBox.RunWithHourglass(() => { - using var formProgress = new FormProgress("Pretty Print Source", false, cancellationToken); + using var formProgress = new FormProgress("Pretty Print Source", false, FontConfig, cancellationToken); formProgress.ShowWithFont(this); formProgress.Maximum = 0; formProgress.Report(new ProgressInfo("Parsing Text as Json")); diff --git a/Library/WinFormControls/FormDuplicatesDisplay.cs b/Library/WinFormControls/FormDuplicatesDisplay.cs index a2db99a8..fd84ffd0 100644 --- a/Library/WinFormControls/FormDuplicatesDisplay.cs +++ b/Library/WinFormControls/FormDuplicatesDisplay.cs @@ -121,7 +121,7 @@ private void Work(string dataColumnName, bool ignoreNull) return; this.SafeInvoke(() => Text = $@"Duplicate Display - {dataColumnName}"); - using var formProgress = new FormProgress($"Processing {dataColumnName}", false, m_CancellationTokenSource.Token) + using var formProgress = new FormProgress($"Processing {dataColumnName}", false, FontConfig, m_CancellationTokenSource.Token) { Maximum = m_DataRow.Length }; formProgress.ShowWithFont(this); var intervalAction = new IntervalAction(); diff --git a/Library/WinFormControls/FormHierarchyDisplay.cs b/Library/WinFormControls/FormHierarchyDisplay.cs index b8bbb16b..bb5a2a68 100644 --- a/Library/WinFormControls/FormHierarchyDisplay.cs +++ b/Library/WinFormControls/FormHierarchyDisplay.cs @@ -91,7 +91,7 @@ public void BuildTree(string parent, string id, string? display1 = null, string? { this.RunWithHourglass(() => { - using var formProgress = new FormProgress("Building Tree", false, m_CancellationTokenSource.Token); + using var formProgress = new FormProgress("Building Tree", false, FontConfig, m_CancellationTokenSource.Token); formProgress.ShowWithFont(this); formProgress.Maximum = m_DataRow.GetLength(0) * 2; @@ -317,7 +317,7 @@ private void FilterValueChangedElapsed(object? sender, ElapsedEventArgs e) { try { - using var formProgress = new FormProgress("Searching", false, m_CancellationTokenSource.Token); + using var formProgress = new FormProgress("Searching", false, FontConfig, m_CancellationTokenSource.Token); formProgress.ShowWithFont(this); Search(m_TextBoxValue!.Text, m_TreeView.Nodes, formProgress.CancellationToken); } diff --git a/Library/WinFormControls/FormProcessDisplay.cs b/Library/WinFormControls/FormProcessDisplay.cs index f6ccc894..8edd320a 100644 --- a/Library/WinFormControls/FormProcessDisplay.cs +++ b/Library/WinFormControls/FormProcessDisplay.cs @@ -21,7 +21,6 @@ using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; -using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrayNotify; // ReSharper disable RedundantDelegateCreation // ReSharper disable RedundantNameQualifier @@ -48,7 +47,7 @@ public sealed class FormProgress : ResizeForm, IProgressTime, ILogger public event EventHandler? ProgressChanged; public FormProgress(in string windowTitle) - : this(windowTitle, true, CancellationToken.None) + : this(windowTitle, true, new FontConfig(), CancellationToken.None) { } @@ -58,7 +57,7 @@ public FormProgress(in string windowTitle) /// The description / form title /// True if a debug logging windows should be shown /// Cancellation token to stop a possibly long running process - public FormProgress(in string? windowTitle, bool withLoggerDisplay, in CancellationToken cancellationToken) + public FormProgress(in string? windowTitle, bool withLoggerDisplay, in IFontConfig fontConfig, in CancellationToken cancellationToken) : base(fontConfig) { CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); InitializeComponent(); @@ -90,7 +89,7 @@ public FormProgress(in string? windowTitle, bool withLoggerDisplay, in Cancellat } public FormProgress() - : this(string.Empty, true, CancellationToken.None) + : this(string.Empty, true, new FontConfig(), CancellationToken.None) { } diff --git a/Library/WinFormControls/FormUniqueDisplay.cs b/Library/WinFormControls/FormUniqueDisplay.cs index 3fef2672..116902bd 100644 --- a/Library/WinFormControls/FormUniqueDisplay.cs +++ b/Library/WinFormControls/FormUniqueDisplay.cs @@ -119,7 +119,7 @@ private void Work(string dataColumnName, bool ignoreNull) var dictIDToRow = new Dictionary(StringComparer.OrdinalIgnoreCase); using var formProgress = - new FormProgress($"Processing {dataColumnName}", false, m_CancellationTokenSource.Token) + new FormProgress($"Processing {dataColumnName}", false, FontConfig, m_CancellationTokenSource.Token) { Maximum = m_DataRow.Length }; diff --git a/Library/WinFormControls/ResizeForm.cs b/Library/WinFormControls/ResizeForm.cs index 95bba745..68d3ebec 100644 --- a/Library/WinFormControls/ResizeForm.cs +++ b/Library/WinFormControls/ResizeForm.cs @@ -8,31 +8,34 @@ namespace CsvTools public class ResizeForm : Form { - private IFontConfig? m_FontConfig; + private IFontConfig m_FontConfig; - public ResizeForm() : this(null) + public ResizeForm() : this(new FontConfig()) { } [Browsable(false)] [Bindable(false)] - public IFontConfig? FontConfig + public IFontConfig FontConfig { get => m_FontConfig; set { if (m_FontConfig != null) m_FontConfig.PropertyChanged -= FontSettingChanged; - m_FontConfig = value; + if (m_FontConfig != null) { + m_FontConfig = value; m_FontConfig.PropertyChanged += FontSettingChanged; FontSettingChanged(value, new PropertyChangedEventArgs(nameof(IFontConfig.Font))); } } } - protected ResizeForm(in IFontConfig? fontConfig) +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. + protected ResizeForm(in IFontConfig fontConfig) +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. { InitializeComponent(); FontConfig = fontConfig; @@ -59,8 +62,7 @@ private void FontSettingChanged(object? sender, PropertyChangedEventArgs e) } } - public void SetFont(Font newFont) => - SetFonts(this, newFont); + public void SetFont(Font newFont) => SetFonts(this, newFont); /// /// Recursively change the font of all controls, needed on Windows 8 / 2012 @@ -75,6 +77,7 @@ private static void SetFonts(in Control container, in Font newFont) foreach (Control ctrl in container.Controls) SetFonts(ctrl, newFont); } + #pragma warning disable CS8600 private void InitializeComponent() { diff --git a/UnitTest/WinFormControlsUnitTest/FormProcessDisplayTests.cs b/UnitTest/WinFormControlsUnitTest/FormProcessDisplayTests.cs index 0e128c40..4f0e57e0 100644 --- a/UnitTest/WinFormControlsUnitTest/FormProcessDisplayTests.cs +++ b/UnitTest/WinFormControlsUnitTest/FormProcessDisplayTests.cs @@ -26,7 +26,7 @@ public class FormProgressTests [Timeout(1000)] public void FormProcessCancel() { - using var formProgress = new FormProgress("Test Logger", true, UnitTestStatic.Token); + using var formProgress = new FormProgress("Test Logger", true, new FontConfig(), UnitTestStatic.Token); formProgress.ShowInTaskbar = true; formProgress.Show(); UnitTestStaticForms.WaitSomeTime(.2, UnitTestStatic.Token); @@ -39,7 +39,7 @@ public void FormProcessCancel() public void FormProgress() { // Log - using (var formProgress = new FormProgress("Test Logger", true, UnitTestStatic.Token)) + using (var formProgress = new FormProgress("Test Logger", true, new FontConfig(),UnitTestStatic.Token)) { formProgress.ShowInTaskbar = false; formProgress.Show(); @@ -66,7 +66,7 @@ public void FormProgress() } // marquee - using (var formProgress = new FormProgress("Test Marquee", false, UnitTestStatic.Token)) + using (var formProgress = new FormProgress("Test Marquee", false, new FontConfig(),UnitTestStatic.Token)) { formProgress.ShowInTaskbar = false; formProgress.Show(); @@ -81,7 +81,7 @@ public void FormProgress() } // NoLog - using (var formProgress = new FormProgress("Test", false, UnitTestStatic.Token)) + using (var formProgress = new FormProgress("Test", false,new FontConfig(), UnitTestStatic.Token)) { formProgress.ShowInTaskbar = false; formProgress.Show(); @@ -109,7 +109,7 @@ public void FormprogressTest() public void FormprogressTest1() { using var tokenSrc = new CancellationTokenSource(); - using var formProgress = new FormProgress("Title", false, tokenSrc.Token); + using var formProgress = new FormProgress("Title", false,new FontConfig(), tokenSrc.Token); Assert.AreEqual("Title", formProgress.Text); Assert.AreEqual(false, formProgress.CancellationToken.IsCancellationRequested); tokenSrc.Cancel(); @@ -121,7 +121,7 @@ public void FormprogressTest1() public void CancelTest() { using var tokenSrc = new CancellationTokenSource(); - using var formProgress = new FormProgress("Title", true, tokenSrc.Token); + using var formProgress = new FormProgress("Title", true, new FontConfig(),tokenSrc.Token); Assert.AreEqual(false, formProgress.CancellationToken.IsCancellationRequested); formProgress.Close(); Assert.AreEqual(true, formProgress.CancellationToken.IsCancellationRequested);