From b60a854de006d8aa33b1322f4273d06ba5f55ed5 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Mon, 16 May 2022 13:34:49 -0400 Subject: [PATCH] Formattable UI labels --- Source/LibationWinForms/Form1.BackupCounts.cs | 104 ++++++++++-------- Source/LibationWinForms/Form1.Designer.cs | 32 +++--- Source/LibationWinForms/Form1.VisibleBooks.cs | 24 ++-- Source/LibationWinForms/FormattableLabel.cs | 32 ++++++ .../FormattableToolStripMenuItem.cs | 39 +++++++ .../FormattableToolStripStatusLabel.cs | 37 +++++++ 6 files changed, 195 insertions(+), 73 deletions(-) create mode 100644 Source/LibationWinForms/FormattableLabel.cs create mode 100644 Source/LibationWinForms/FormattableToolStripMenuItem.cs create mode 100644 Source/LibationWinForms/FormattableToolStripStatusLabel.cs diff --git a/Source/LibationWinForms/Form1.BackupCounts.cs b/Source/LibationWinForms/Form1.BackupCounts.cs index 4885bafb..8c25d699 100644 --- a/Source/LibationWinForms/Form1.BackupCounts.cs +++ b/Source/LibationWinForms/Form1.BackupCounts.cs @@ -6,16 +6,14 @@ namespace LibationWinForms { public partial class Form1 { - private string beginBookBackupsToolStripMenuItem_format; - private string beginPdfBackupsToolStripMenuItem_format; - protected void Configure_BackupCounts() - { - // back up string formats - beginBookBackupsToolStripMenuItem_format = beginBookBackupsToolStripMenuItem.Text; - beginPdfBackupsToolStripMenuItem_format = beginPdfBackupsToolStripMenuItem.Text; + { + // init formattable + beginBookBackupsToolStripMenuItem.Format(0); + beginPdfBackupsToolStripMenuItem.Format(0); + pdfsCountsLbl.Text = "| [Calculating backed up PDFs]"; - Load += setBackupCounts; + Load += setBackupCounts; LibraryCommands.LibrarySizeChanged += setBackupCounts; LibraryCommands.BookUserDefinedItemCommitted += setBackupCounts; } @@ -56,54 +54,72 @@ namespace LibationWinForms setPdfBackupCounts(libraryStats); } + // this cannot be cleanly be FormattableToolStripMenuItem because of the optional "Errors" text + private const string backupsCountsLbl_Format = "BACKUPS: No progress: {0} In process: {1} Fully backed up: {2}"; + private void setBookBackupCounts(LibraryCommands.LibraryStats libraryStats) { - var backupsCountsLbl_Format = "BACKUPS: No progress: {0} In process: {1} Fully backed up: {2}"; + var pending = libraryStats.booksNoProgress + libraryStats.booksDownloadedOnly; + var hasResults = 0 < (libraryStats.booksFullyBackedUp + libraryStats.booksDownloadedOnly + libraryStats.booksNoProgress + libraryStats.booksError); // enable/disable export - var hasResults = 0 < (libraryStats.booksFullyBackedUp + libraryStats.booksDownloadedOnly + libraryStats.booksNoProgress + libraryStats.booksError); - exportLibraryToolStripMenuItem.Enabled = hasResults; + { + exportLibraryToolStripMenuItem.Enabled = hasResults; + } // update bottom numbers - var pending = libraryStats.booksNoProgress + libraryStats.booksDownloadedOnly; - var statusStripText - = !hasResults ? "No books. Begin by importing your library" - : libraryStats.booksError > 0 ? string.Format(backupsCountsLbl_Format + " Errors: {3}", libraryStats.booksNoProgress, libraryStats.booksDownloadedOnly, libraryStats.booksFullyBackedUp, libraryStats.booksError) - : pending > 0 ? string.Format(backupsCountsLbl_Format, libraryStats.booksNoProgress, libraryStats.booksDownloadedOnly, libraryStats.booksFullyBackedUp) - : $"All {"book".PluralizeWithCount(libraryStats.booksFullyBackedUp)} backed up"; + { + var formatString + = !hasResults ? "No books. Begin by importing your library" + : libraryStats.booksError > 0 ? backupsCountsLbl_Format + " Errors: {3}" + : pending > 0 ? backupsCountsLbl_Format + : $"All {"book".PluralizeWithCount(libraryStats.booksFullyBackedUp)} backed up"; + var statusStripText = string.Format(formatString, + libraryStats.booksNoProgress, + libraryStats.booksDownloadedOnly, + libraryStats.booksFullyBackedUp, + libraryStats.booksError); + statusStrip1.UIThreadAsync(() => backupsCountsLbl.Text = statusStripText); + } - // update menu item - var menuItemText - = pending > 0 - ? $"{pending} remaining" - : "All books have been liberated"; - - // update UI - statusStrip1.UIThreadAsync(() => backupsCountsLbl.Text = statusStripText); - menuStrip1.UIThreadAsync(() => beginBookBackupsToolStripMenuItem.Enabled = pending > 0); - menuStrip1.UIThreadAsync(() => beginBookBackupsToolStripMenuItem.Text = string.Format(beginBookBackupsToolStripMenuItem_format, menuItemText)); + // update 'begin book backups' menu item + { + var menuItemText + = pending > 0 + ? $"{pending} remaining" + : "All books have been liberated"; + menuStrip1.UIThreadAsync(() => + { + beginBookBackupsToolStripMenuItem.Format(menuItemText); + beginBookBackupsToolStripMenuItem.Enabled = pending > 0; + }); + } } private void setPdfBackupCounts(LibraryCommands.LibraryStats libraryStats) { - var pdfsCountsLbl_Format = "| PDFs: NOT d/l\'ed: {0} Downloaded: {1}"; - // update bottom numbers - var hasResults = 0 < (libraryStats.pdfsNotDownloaded + libraryStats.pdfsDownloaded); - var statusStripText - = !hasResults ? "" - : libraryStats.pdfsNotDownloaded > 0 ? string.Format(pdfsCountsLbl_Format, libraryStats.pdfsNotDownloaded, libraryStats.pdfsDownloaded) - : $"| All {libraryStats.pdfsDownloaded} PDFs downloaded"; + { + var hasResults = 0 < (libraryStats.pdfsNotDownloaded + libraryStats.pdfsDownloaded); + // don't need to assign the output of Format(). It just makes this logic cleaner + var statusStripText + = !hasResults ? "" + : libraryStats.pdfsNotDownloaded > 0 ? pdfsCountsLbl.Format(libraryStats.pdfsNotDownloaded, libraryStats.pdfsDownloaded) + : $"| All {libraryStats.pdfsDownloaded} PDFs downloaded"; + statusStrip1.UIThreadAsync(() => pdfsCountsLbl.Text = statusStripText); + } - // update menu item - var menuItemText - = libraryStats.pdfsNotDownloaded > 0 - ? $"{libraryStats.pdfsNotDownloaded} remaining" - : "All PDFs have been downloaded"; - - // update UI - statusStrip1.UIThreadAsync(() => pdfsCountsLbl.Text = statusStripText); - menuStrip1.UIThreadAsync(() => beginPdfBackupsToolStripMenuItem.Enabled = libraryStats.pdfsNotDownloaded > 0); - menuStrip1.UIThreadAsync(() => beginPdfBackupsToolStripMenuItem.Text = string.Format(beginPdfBackupsToolStripMenuItem_format, menuItemText)); + // update 'begin pdf only backups' menu item + { + var menuItemText + = libraryStats.pdfsNotDownloaded > 0 + ? $"{libraryStats.pdfsNotDownloaded} remaining" + : "All PDFs have been downloaded"; + menuStrip1.UIThreadAsync(() => + { + beginPdfBackupsToolStripMenuItem.Format(menuItemText); + beginPdfBackupsToolStripMenuItem.Enabled = libraryStats.pdfsNotDownloaded > 0; + }); + } } } } diff --git a/Source/LibationWinForms/Form1.Designer.cs b/Source/LibationWinForms/Form1.Designer.cs index 70aae01c..b6be6147 100644 --- a/Source/LibationWinForms/Form1.Designer.cs +++ b/Source/LibationWinForms/Form1.Designer.cs @@ -44,10 +44,10 @@ this.removeAllAccountsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.removeSomeAccountsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.liberateToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.beginBookBackupsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.beginPdfBackupsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.beginBookBackupsToolStripMenuItem = new LibationWinForms.FormattableToolStripMenuItem(); + this.beginPdfBackupsToolStripMenuItem = new LibationWinForms.FormattableToolStripMenuItem(); this.convertAllM4bToMp3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.liberateVisible2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.liberateVisible2ToolStripMenuItem = new LibationWinForms.FormattableToolStripMenuItem(); this.exportToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.exportLibraryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.quickFiltersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -55,8 +55,8 @@ this.editQuickFiltersToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.scanningToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.visibleBooksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.liberateVisibleToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.visibleBooksToolStripMenuItem = new LibationWinForms.FormattableToolStripMenuItem(); + this.liberateVisibleToolStripMenuItem = new LibationWinForms.FormattableToolStripMenuItem(); this.replaceTagsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.setDownloadedToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.removeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -66,10 +66,10 @@ this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator(); this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.statusStrip1 = new System.Windows.Forms.StatusStrip(); - this.visibleCountLbl = new System.Windows.Forms.ToolStripStatusLabel(); + this.visibleCountLbl = new LibationWinForms.FormattableToolStripStatusLabel(); this.springLbl = new System.Windows.Forms.ToolStripStatusLabel(); this.backupsCountsLbl = new System.Windows.Forms.ToolStripStatusLabel(); - this.pdfsCountsLbl = new System.Windows.Forms.ToolStripStatusLabel(); + this.pdfsCountsLbl = new LibationWinForms.FormattableToolStripStatusLabel(); this.addQuickFilterBtn = new System.Windows.Forms.Button(); this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.panel1 = new System.Windows.Forms.Panel(); @@ -407,7 +407,7 @@ // this.visibleCountLbl.Name = "visibleCountLbl"; this.visibleCountLbl.Size = new System.Drawing.Size(68, 20); - this.visibleCountLbl.Text = "Visible: 0"; + this.visibleCountLbl.Text = "Visible: {0}"; // // springLbl // @@ -425,7 +425,7 @@ // this.pdfsCountsLbl.Name = "pdfsCountsLbl"; this.pdfsCountsLbl.Size = new System.Drawing.Size(214, 20); - this.pdfsCountsLbl.Text = "| [Calculating backed up PDFs]"; + this.pdfsCountsLbl.Text = "| PDFs: NOT d/l\'ed: {0} Downloaded: {1}"; // // addQuickFilterBtn // @@ -531,12 +531,12 @@ private System.Windows.Forms.ToolStripMenuItem importToolStripMenuItem; private System.Windows.Forms.StatusStrip statusStrip1; private System.Windows.Forms.ToolStripStatusLabel springLbl; - private System.Windows.Forms.ToolStripStatusLabel visibleCountLbl; + private LibationWinForms.FormattableToolStripStatusLabel visibleCountLbl; private System.Windows.Forms.ToolStripMenuItem liberateToolStripMenuItem; private System.Windows.Forms.ToolStripStatusLabel backupsCountsLbl; - private System.Windows.Forms.ToolStripMenuItem beginBookBackupsToolStripMenuItem; - private System.Windows.Forms.ToolStripStatusLabel pdfsCountsLbl; - private System.Windows.Forms.ToolStripMenuItem beginPdfBackupsToolStripMenuItem; + private LibationWinForms.FormattableToolStripMenuItem beginBookBackupsToolStripMenuItem; + private LibationWinForms.FormattableToolStripStatusLabel pdfsCountsLbl; + private LibationWinForms.FormattableToolStripMenuItem beginPdfBackupsToolStripMenuItem; private System.Windows.Forms.TextBox filterSearchTb; private System.Windows.Forms.Button filterBtn; private System.Windows.Forms.Button filterHelpBtn; @@ -562,12 +562,12 @@ private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem scanningToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem autoScanLibraryToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem visibleBooksToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem liberateVisibleToolStripMenuItem; + private LibationWinForms.FormattableToolStripMenuItem visibleBooksToolStripMenuItem; + private LibationWinForms.FormattableToolStripMenuItem liberateVisibleToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem replaceTagsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem setDownloadedToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem removeToolStripMenuItem; - private System.Windows.Forms.ToolStripMenuItem liberateVisible2ToolStripMenuItem; + private LibationWinForms.FormattableToolStripMenuItem liberateVisible2ToolStripMenuItem; private System.Windows.Forms.SplitContainer splitContainer1; private LibationWinForms.ProcessQueue.ProcessQueueControl processBookQueue1; private System.Windows.Forms.Panel panel1; diff --git a/Source/LibationWinForms/Form1.VisibleBooks.cs b/Source/LibationWinForms/Form1.VisibleBooks.cs index 3f7a9a43..0890ddb3 100644 --- a/Source/LibationWinForms/Form1.VisibleBooks.cs +++ b/Source/LibationWinForms/Form1.VisibleBooks.cs @@ -10,22 +10,20 @@ namespace LibationWinForms { public partial class Form1 { - private string visibleBooksToolStripMenuItem_format; - private string liberateVisibleToolStripMenuItem_format; - private string liberateVisible2ToolStripMenuItem_format; - protected void Configure_VisibleBooks() { + // init formattable + visibleCountLbl.Format(0); + liberateVisibleToolStripMenuItem.Format(0); + liberateVisible2ToolStripMenuItem.Format(0); + // bottom-left visible count - productsGrid.VisibleCountChanged += (_, qty) => visibleCountLbl.Text = string.Format("Visible: {0}", qty); - - // back up string formats - visibleBooksToolStripMenuItem_format = visibleBooksToolStripMenuItem.Text; - liberateVisibleToolStripMenuItem_format = liberateVisibleToolStripMenuItem.Text; - liberateVisible2ToolStripMenuItem_format = liberateVisible2ToolStripMenuItem.Text; + productsGrid.VisibleCountChanged += (_, qty) => visibleCountLbl.Format(qty); + // top menu strip + visibleBooksToolStripMenuItem.Format(0); productsGrid.VisibleCountChanged += (_, qty) => { - visibleBooksToolStripMenuItem.Text = string.Format(visibleBooksToolStripMenuItem_format, qty); + visibleBooksToolStripMenuItem.Format(qty); visibleBooksToolStripMenuItem.Enabled = qty > 0; var notLiberatedCount = productsGrid.GetVisible().Count(lb => lb.Book.UserDefinedItem.BookStatus == DataLayer.LiberatedStatus.NotLiberated); @@ -45,10 +43,10 @@ namespace LibationWinForms { if (notLiberated > 0) { - liberateVisibleToolStripMenuItem.Text = string.Format(liberateVisibleToolStripMenuItem_format, notLiberated); + liberateVisibleToolStripMenuItem.Format(notLiberated); liberateVisibleToolStripMenuItem.Enabled = true; - liberateVisible2ToolStripMenuItem.Text = string.Format(liberateVisible2ToolStripMenuItem_format, notLiberated); + liberateVisible2ToolStripMenuItem.Format(notLiberated); liberateVisible2ToolStripMenuItem.Enabled = true; } else diff --git a/Source/LibationWinForms/FormattableLabel.cs b/Source/LibationWinForms/FormattableLabel.cs new file mode 100644 index 00000000..fb4474a4 --- /dev/null +++ b/Source/LibationWinForms/FormattableLabel.cs @@ -0,0 +1,32 @@ +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace LibationWinForms +{ + public class FormattableLabel : Label + { + public string FormatText { get; set; } + + /// Text set: first non-null, non-whitespace set is also saved as + public override string Text + { + get => base.Text; + set + { + if (string.IsNullOrWhiteSpace(FormatText)) + FormatText = value; + + base.Text = value; + } + } + + #region ctor.s + public FormattableLabel() : base() { } + #endregion + + /// Replaces the format item in a specified string with the string representation of a corresponding object in a specified array. Returns for convenience. + /// An object array that contains zero or more objects to format. + public string Format(params object[] args) => Text = string.Format(FormatText, args); + } +} diff --git a/Source/LibationWinForms/FormattableToolStripMenuItem.cs b/Source/LibationWinForms/FormattableToolStripMenuItem.cs new file mode 100644 index 00000000..a1032b8d --- /dev/null +++ b/Source/LibationWinForms/FormattableToolStripMenuItem.cs @@ -0,0 +1,39 @@ +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace LibationWinForms +{ + public class FormattableToolStripMenuItem : ToolStripMenuItem + { + public string FormatText { get; set; } + + /// Text set: first non-null, non-whitespace set is also saved as + public override string Text + { + get => base.Text; + set + { + if (string.IsNullOrWhiteSpace(FormatText)) + FormatText = value; + + base.Text = value; + } + } + + #region ctor.s + public FormattableToolStripMenuItem() : base() { } + public FormattableToolStripMenuItem(string text) : base(text) => FormatText = text; + public FormattableToolStripMenuItem(Image image) : base(image) { } + public FormattableToolStripMenuItem(string text, Image image) : base(text, image) => FormatText = text; + public FormattableToolStripMenuItem(string text, Image image, EventHandler onClick) : base(text, image, onClick) => FormatText = text; + public FormattableToolStripMenuItem(string text, Image image, params ToolStripItem[] dropDownItems) : base(text, image, dropDownItems) => FormatText = text; + public FormattableToolStripMenuItem(string text, Image image, EventHandler onClick, Keys shortcutKeys) : base(text, image, onClick, shortcutKeys) => FormatText = text; + public FormattableToolStripMenuItem(string text, Image image, EventHandler onClick, string name) : base(text, image, onClick, name) => FormatText = text; + #endregion + + /// Replaces the format item in a specified string with the string representation of a corresponding object in a specified array. Returns for convenience. + /// An object array that contains zero or more objects to format. + public string Format(params object[] args) => Text = string.Format(FormatText, args); + } +} diff --git a/Source/LibationWinForms/FormattableToolStripStatusLabel.cs b/Source/LibationWinForms/FormattableToolStripStatusLabel.cs new file mode 100644 index 00000000..a3fb88d5 --- /dev/null +++ b/Source/LibationWinForms/FormattableToolStripStatusLabel.cs @@ -0,0 +1,37 @@ +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace LibationWinForms +{ + public class FormattableToolStripStatusLabel : ToolStripStatusLabel + { + public string FormatText { get; set; } + + /// Text set: first non-null, non-whitespace set is also saved as + public override string Text + { + get => base.Text; + set + { + if (string.IsNullOrWhiteSpace(FormatText)) + FormatText = value; + + base.Text = value; + } + } + + #region ctor.s + public FormattableToolStripStatusLabel() : base() { } + public FormattableToolStripStatusLabel(string text) : base(text) => FormatText = text; + public FormattableToolStripStatusLabel(Image image) : base(image) { } + public FormattableToolStripStatusLabel(string text, Image image) : base(text, image) => FormatText = text; + public FormattableToolStripStatusLabel(string text, Image image, EventHandler onClick) : base(text, image, onClick) => FormatText = text; + public FormattableToolStripStatusLabel(string text, Image image, EventHandler onClick, string name) : base(text, image, onClick, name) => FormatText = text; + #endregion + + /// Replaces the format item in a specified string with the string representation of a corresponding object in a specified array. Returns for convenience. + /// An object array that contains zero or more objects to format. + public string Format(params object[] args) => Text = string.Format(FormatText, args); + } +}