From 676af0210b5bc260e1249ad2b4dfebe479dd28a9 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Sat, 14 May 2022 02:54:32 -0600 Subject: [PATCH] Finalized ProcessBookControl --- .../ProcessBookControl.Designer.cs | 37 +- .../ProcessQueue/ProcessBookControl.cs | 318 +++--------------- .../ProcessQueue/ProcessBookControl.resx | 36 ++ 3 files changed, 113 insertions(+), 278 deletions(-) diff --git a/Source/LibationWinForms/ProcessQueue/ProcessBookControl.Designer.cs b/Source/LibationWinForms/ProcessQueue/ProcessBookControl.Designer.cs index e77e0211..38cc2b67 100644 --- a/Source/LibationWinForms/ProcessQueue/ProcessBookControl.Designer.cs +++ b/Source/LibationWinForms/ProcessQueue/ProcessBookControl.Designer.cs @@ -98,7 +98,6 @@ this.cancelBtn.Size = new System.Drawing.Size(20, 20); this.cancelBtn.TabIndex = 4; this.cancelBtn.UseVisualStyleBackColor = false; - this.cancelBtn.Click += new System.EventHandler(this.cancelBtn_Click); // // statusLbl // @@ -128,53 +127,61 @@ // this.moveUpBtn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); + this.moveUpBtn.BackColor = System.Drawing.Color.Transparent; this.moveUpBtn.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("moveUpBtn.BackgroundImage"))); this.moveUpBtn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.moveUpBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.moveUpBtn.ForeColor = System.Drawing.SystemColors.Control; this.moveUpBtn.Location = new System.Drawing.Point(314, 24); this.moveUpBtn.Name = "moveUpBtn"; this.moveUpBtn.Size = new System.Drawing.Size(30, 17); this.moveUpBtn.TabIndex = 5; - this.moveUpBtn.UseVisualStyleBackColor = true; - this.moveUpBtn.Click += new System.EventHandler(this.moveUpBtn_Click_1); + this.moveUpBtn.UseVisualStyleBackColor = false; // // moveDownBtn // this.moveDownBtn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); + this.moveDownBtn.BackColor = System.Drawing.Color.Transparent; this.moveDownBtn.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("moveDownBtn.BackgroundImage"))); this.moveDownBtn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.moveDownBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.moveDownBtn.ForeColor = System.Drawing.SystemColors.Control; this.moveDownBtn.Location = new System.Drawing.Point(314, 40); this.moveDownBtn.Name = "moveDownBtn"; this.moveDownBtn.Size = new System.Drawing.Size(30, 17); this.moveDownBtn.TabIndex = 5; - this.moveDownBtn.UseVisualStyleBackColor = true; - this.moveDownBtn.Click += new System.EventHandler(this.moveDownBtn_Click); + this.moveDownBtn.UseVisualStyleBackColor = false; // // moveFirstBtn // this.moveFirstBtn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); + this.moveFirstBtn.BackColor = System.Drawing.Color.Transparent; this.moveFirstBtn.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("moveFirstBtn.BackgroundImage"))); this.moveFirstBtn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.moveFirstBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.moveFirstBtn.ForeColor = System.Drawing.SystemColors.Control; this.moveFirstBtn.Location = new System.Drawing.Point(314, 3); this.moveFirstBtn.Name = "moveFirstBtn"; this.moveFirstBtn.Size = new System.Drawing.Size(30, 17); this.moveFirstBtn.TabIndex = 5; - this.moveFirstBtn.UseVisualStyleBackColor = true; - this.moveFirstBtn.Click += new System.EventHandler(this.moveFirstBtn_Click); + this.moveFirstBtn.UseVisualStyleBackColor = false; // // moveLastBtn // this.moveLastBtn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Right))); + this.moveLastBtn.BackColor = System.Drawing.Color.Transparent; this.moveLastBtn.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("moveLastBtn.BackgroundImage"))); this.moveLastBtn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; + this.moveLastBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.moveLastBtn.ForeColor = System.Drawing.SystemColors.Control; this.moveLastBtn.Location = new System.Drawing.Point(314, 63); this.moveLastBtn.Name = "moveLastBtn"; this.moveLastBtn.Size = new System.Drawing.Size(30, 17); this.moveLastBtn.TabIndex = 5; - this.moveLastBtn.UseVisualStyleBackColor = true; - this.moveLastBtn.Click += new System.EventHandler(this.moveLastBtn_Click); + this.moveLastBtn.UseVisualStyleBackColor = false; // // ProcessBookControl // @@ -193,7 +200,7 @@ this.Controls.Add(this.progressBar1); this.Controls.Add(this.bookInfoLbl); this.Controls.Add(this.pictureBox1); - this.Margin = new System.Windows.Forms.Padding(2); + this.Margin = new System.Windows.Forms.Padding(4, 2, 4, 2); this.Name = "ProcessBookControl"; this.Size = new System.Drawing.Size(375, 86); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); @@ -208,12 +215,12 @@ private System.Windows.Forms.ProgressBar progressBar1; private System.Windows.Forms.Label remainingTimeLbl; private System.Windows.Forms.Label etaLbl; - private System.Windows.Forms.Button cancelBtn; private System.Windows.Forms.Label statusLbl; private System.Windows.Forms.Label bookInfoLbl; - private System.Windows.Forms.Button moveUpBtn; - private System.Windows.Forms.Button moveDownBtn; - private System.Windows.Forms.Button moveFirstBtn; - private System.Windows.Forms.Button moveLastBtn; + public System.Windows.Forms.Button cancelBtn; + public System.Windows.Forms.Button moveUpBtn; + public System.Windows.Forms.Button moveDownBtn; + public System.Windows.Forms.Button moveFirstBtn; + public System.Windows.Forms.Button moveLastBtn; } } diff --git a/Source/LibationWinForms/ProcessQueue/ProcessBookControl.cs b/Source/LibationWinForms/ProcessQueue/ProcessBookControl.cs index 76f73368..02764c26 100644 --- a/Source/LibationWinForms/ProcessQueue/ProcessBookControl.cs +++ b/Source/LibationWinForms/ProcessQueue/ProcessBookControl.cs @@ -1,40 +1,19 @@ using System; using System.Drawing; using System.Windows.Forms; -using System.Windows.Forms.Layout; -using DataLayer; -using Dinah.Core.Net.Http; -using Dinah.Core.Threading; -using FileLiberator; -using LibationFileManager; -using LibationWinForms.BookLiberation; -using LibationWinForms.ProcessQueue; namespace LibationWinForms.ProcessQueue { - internal interface ILiberatiofffnBaseForm - { - Action CancelAction { get; set; } - Func MoveUpAction { get; set; } - Func MoveDownAction { get; set; } - void SetResult(ProcessBookResult status); - void SetQueuePosition(QueuePosition status); - void RegisterFileLiberator(Processable streamable, LogMe logMe); - void Processable_Begin(object sender, LibraryBook libraryBook); - int Width { get; set; } - int Height { get; set; } - Padding Margin { get; set; } - } - internal partial class ProcessBookControl : UserControl { - public ProcessBookStatus Status { get; private set; } = ProcessBookStatus.Queued; - public Action CancelAction { get; set; } - public Func RequestMoveAction { get; set; } - public string DecodeActionName { get; } = "Decoding"; - private Func GetCoverArtDelegate; - protected Processable Processable { get; private set; } - protected LogMe LogMe { get; private set; } + private static int ControlNumberCounter = 0; + + /// + /// The contol's position within + /// + public int ControlNumber { get; } + private ProcessBookStatus Status { get; set; } = ProcessBookStatus.Queued; + private readonly int CancelBtnDistanceFromEdge; public ProcessBookControl() { InitializeComponent(); @@ -42,17 +21,27 @@ namespace LibationWinForms.ProcessQueue remainingTimeLbl.Visible = false; progressBar1.Visible = false; etaLbl.Visible = false; + + CancelBtnDistanceFromEdge = Width - cancelBtn.Location.X; + ControlNumber = ControlNumberCounter++; } public void SetCover(Image cover) { pictureBox1.Image = cover; } - public void SetTitle(string title) + public void SetBookInfo(string title) { bookInfoLbl.Text = title; } - + public void SetProgrss(int progress) + { + progressBar1.Value = progress; + } + public void SetRemainingTime(TimeSpan remaining) + { + remainingTimeLbl.Text = $"{remaining:mm\\:ss}"; + } public void SetResult(ProcessBookResult result) { string statusText = default; @@ -90,8 +79,7 @@ namespace LibationWinForms.ProcessQueue SetStatus(Status, statusText); } - - public void SetStatus(ProcessBookStatus status, string statusText) + public void SetStatus(ProcessBookStatus status, string statusText = null) { Color backColor = default; switch (status) @@ -118,245 +106,49 @@ namespace LibationWinForms.ProcessQueue break; } - this.UIThreadAsync(() => + SuspendLayout(); + + cancelBtn.Visible = Status is ProcessBookStatus.Queued or ProcessBookStatus.Working; + moveLastBtn.Visible = Status == ProcessBookStatus.Queued; + moveDownBtn.Visible = Status == ProcessBookStatus.Queued; + moveUpBtn.Visible = Status == ProcessBookStatus.Queued; + moveFirstBtn.Visible = Status == ProcessBookStatus.Queued; + remainingTimeLbl.Visible = Status == ProcessBookStatus.Working; + progressBar1.Visible = Status == ProcessBookStatus.Working; + etaLbl.Visible = Status == ProcessBookStatus.Working; + statusLbl.Visible = Status != ProcessBookStatus.Working; + statusLbl.Text = statusText ?? Status.ToString(); + BackColor = backColor; + + int deltaX = Width - cancelBtn.Location.X - CancelBtnDistanceFromEdge; + + if (Status is ProcessBookStatus.Queued or ProcessBookStatus.Working && deltaX != 0) { - SuspendLayout(); + //If the last book to occupy this control before resizing was not queued, + //the buttons were not Visible so the Anchor property was ignored. - cancelBtn.Visible = Status is ProcessBookStatus.Queued or ProcessBookStatus.Working; - moveLastBtn.Visible = Status == ProcessBookStatus.Queued; - moveDownBtn.Visible = Status == ProcessBookStatus.Queued; - moveUpBtn.Visible = Status == ProcessBookStatus.Queued; - moveFirstBtn.Visible = Status == ProcessBookStatus.Queued; - remainingTimeLbl.Visible = Status == ProcessBookStatus.Working; - progressBar1.Visible = Status == ProcessBookStatus.Working; - etaLbl.Visible = Status == ProcessBookStatus.Working; - statusLbl.Visible = Status != ProcessBookStatus.Working; - statusLbl.Text = statusText; - BackColor = backColor; + cancelBtn.Location = new Point(cancelBtn.Location.X + deltaX, cancelBtn.Location.Y); + moveFirstBtn.Location = new Point(moveFirstBtn.Location.X + deltaX, moveFirstBtn.Location.Y); + moveUpBtn.Location = new Point(moveUpBtn.Location.X + deltaX, moveUpBtn.Location.Y); + moveDownBtn.Location = new Point(moveDownBtn.Location.X + deltaX, moveDownBtn.Location.Y); + moveLastBtn.Location = new Point(moveLastBtn.Location.X + deltaX, moveLastBtn.Location.Y); + } - if (status == ProcessBookStatus.Working) - { - bookInfoLbl.Width += moveLastBtn.Width + moveLastBtn.Padding.Left + moveLastBtn.Padding.Right; - } - else - { - bookInfoLbl.Width -= moveLastBtn.Width + moveLastBtn.Padding.Left + moveLastBtn.Padding.Right; - - } - ResumeLayout(); - }); - } - - public ProcessBookControl(string title, Image cover) : this() - { - this.title = title; - pictureBox1.Image = cover; - bookInfoLbl.Text = title; - } - - public void RegisterFileLiberator(Processable processable, LogMe logMe = null) - { - if (processable is null) return; - - Processable = processable; - LogMe = logMe; - - Subscribe((Streamable)processable); - Subscribe(processable); - if (processable is AudioDecodable audioDecodable) - Subscribe(audioDecodable); - } - - - #region Event Subscribers and Unsubscribers - private void Subscribe(Streamable streamable) - { - UnsubscribeStreamable(this, EventArgs.Empty); - - streamable.StreamingProgressChanged += Streamable_StreamingProgressChanged; - streamable.StreamingTimeRemaining += Streamable_StreamingTimeRemaining; - - Disposed += UnsubscribeStreamable; - } - private void Subscribe(Processable processable) - { - UnsubscribeProcessable(this, null); - - processable.Begin += Processable_Begin; - processable.Completed += Processable_Completed; - - //Don't unsubscribe from Dispose because it fires when - //Streamable.StreamingCompleted closes the form, and - //the Processable events need to live past that event. - processable.Completed += UnsubscribeProcessable; - } - private void Subscribe(AudioDecodable audioDecodable) - { - UnsubscribeAudioDecodable(this, EventArgs.Empty); - - audioDecodable.RequestCoverArt += AudioDecodable_RequestCoverArt; - audioDecodable.TitleDiscovered += AudioDecodable_TitleDiscovered; - audioDecodable.AuthorsDiscovered += AudioDecodable_AuthorsDiscovered; - audioDecodable.NarratorsDiscovered += AudioDecodable_NarratorsDiscovered; - audioDecodable.CoverImageDiscovered += AudioDecodable_CoverImageDiscovered; - - Disposed += UnsubscribeAudioDecodable; - } - private void UnsubscribeStreamable(object sender, EventArgs e) - { - Disposed -= UnsubscribeStreamable; - - Processable.StreamingProgressChanged -= Streamable_StreamingProgressChanged; - Processable.StreamingTimeRemaining -= Streamable_StreamingTimeRemaining; - } - private void UnsubscribeProcessable(object sender, LibraryBook e) - { - Processable.Completed -= UnsubscribeProcessable; - Processable.Begin -= Processable_Begin; - Processable.Completed -= Processable_Completed; - } - private void UnsubscribeAudioDecodable(object sender, EventArgs e) - { - if (Processable is not AudioDecodable audioDecodable) - return; - - Disposed -= UnsubscribeAudioDecodable; - audioDecodable.RequestCoverArt -= AudioDecodable_RequestCoverArt; - audioDecodable.TitleDiscovered -= AudioDecodable_TitleDiscovered; - audioDecodable.AuthorsDiscovered -= AudioDecodable_AuthorsDiscovered; - audioDecodable.NarratorsDiscovered -= AudioDecodable_NarratorsDiscovered; - audioDecodable.CoverImageDiscovered -= AudioDecodable_CoverImageDiscovered; - - audioDecodable.Cancel(); - } - #endregion - - #region Streamable event handlers - public void Streamable_StreamingProgressChanged(object sender, DownloadProgress downloadProgress) - { - if (!downloadProgress.ProgressPercentage.HasValue) - return; - - if (downloadProgress.ProgressPercentage == 0) - updateRemainingTime(0); + if (status == ProcessBookStatus.Working) + { + bookInfoLbl.Width = cancelBtn.Location.X - bookInfoLbl.Location.X - bookInfoLbl.Padding.Left + cancelBtn.Padding.Right; + } else - progressBar1.UIThreadAsync(() => progressBar1.Value = (int)downloadProgress.ProgressPercentage); - } + { + bookInfoLbl.Width = moveLastBtn.Location.X - bookInfoLbl.Location.X - bookInfoLbl.Padding.Left + moveLastBtn.Padding.Right; + } - public void Streamable_StreamingTimeRemaining(object sender, TimeSpan timeRemaining) - { - updateRemainingTime((int)timeRemaining.TotalSeconds); - } - - private void updateRemainingTime(int remaining) - => remainingTimeLbl.UIThreadAsync(() => remainingTimeLbl.Text = formatTime(remaining)); - - private string formatTime(int seconds) - { - var timeSpan = TimeSpan.FromSeconds(seconds); - return $"{timeSpan:mm\\:ss}"; - } - - #endregion - - #region Processable event handlers - public void Processable_Begin(object sender, LibraryBook libraryBook) - { - Status = ProcessBookStatus.Working; - - LogMe.Info($"{Environment.NewLine}{Processable.Name} Step, Begin: {libraryBook.Book}"); - - SetStatus(ProcessBookStatus.Working, ""); - - GetCoverArtDelegate = () => PictureStorage.GetPictureSynchronously( - new PictureDefinition( - libraryBook.Book.PictureId, - PictureSize._500x500)); - - //Set default values from library - AudioDecodable_TitleDiscovered(sender, libraryBook.Book.Title); - AudioDecodable_AuthorsDiscovered(sender, libraryBook.Book.AuthorNames()); - AudioDecodable_NarratorsDiscovered(sender, libraryBook.Book.NarratorNames()); - AudioDecodable_CoverImageDiscovered(sender, - PictureStorage.GetPicture( - new PictureDefinition( - libraryBook.Book.PictureId, - PictureSize._80x80)).bytes); - } - - public void Processable_Completed(object sender, LibraryBook libraryBook) - { - LogMe.Info($"{Processable.Name} Step, Completed: {libraryBook.Book}"); - } - - #endregion - - #region AudioDecodable event handlers - - private string title; - private string authorNames; - private string narratorNames; - public void AudioDecodable_TitleDiscovered(object sender, string title) - { - this.UIThreadAsync(() => this.Text = DecodeActionName + " " + title); - this.title = title; - updateBookInfo(); - } - - public void AudioDecodable_AuthorsDiscovered(object sender, string authors) - { - authorNames = authors; - updateBookInfo(); - } - - public void AudioDecodable_NarratorsDiscovered(object sender, string narrators) - { - narratorNames = narrators; - updateBookInfo(); - } - - private void updateBookInfo() - => bookInfoLbl.UIThreadAsync(() => bookInfoLbl.Text = $"{title}\r\nBy {authorNames}\r\nNarrated by {narratorNames}"); - - public void AudioDecodable_RequestCoverArt(object sender, Action setCoverArtDelegate) - { - setCoverArtDelegate(GetCoverArtDelegate?.Invoke()); - } - - public void AudioDecodable_CoverImageDiscovered(object sender, byte[] coverArt) - { - pictureBox1.UIThreadAsync(() => pictureBox1.Image = Dinah.Core.Drawing.ImageReader.ToImage(coverArt)); - } - #endregion - - private void cancelBtn_Click(object sender, EventArgs e) - { - CancelAction?.Invoke(); - } - - private void moveLastBtn_Click(object sender, EventArgs e) - { - RequestMoveAction?.Invoke(QueuePositionRequest.Last); - } - - private void moveFirstBtn_Click(object sender, EventArgs e) - { - RequestMoveAction?.Invoke(QueuePositionRequest.Fisrt); - } - - private void moveUpBtn_Click_1(object sender, EventArgs e) - { - RequestMoveAction?.Invoke(QueuePositionRequest.OneUp); - } - - private void moveDownBtn_Click(object sender, EventArgs e) - { - RequestMoveAction?.Invoke(QueuePositionRequest.OneDown); + ResumeLayout(); } public override string ToString() { - return title ?? "NO TITLE"; + return bookInfoLbl.Text ?? "[NO TITLE]"; } } } diff --git a/Source/LibationWinForms/ProcessQueue/ProcessBookControl.resx b/Source/LibationWinForms/ProcessQueue/ProcessBookControl.resx index a91b8cad..83921ba7 100644 --- a/Source/LibationWinForms/ProcessQueue/ProcessBookControl.resx +++ b/Source/LibationWinForms/ProcessQueue/ProcessBookControl.resx @@ -57,6 +57,21 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + True + + + True + + + True + + + True + + + True + @@ -719,6 +734,15 @@ /x9W31o+WFcHNAAAAABJRU5ErkJggg== + + True + + + True + + + True + iVBORw0KGgoAAAANSUhEUgAAAMgAAABNCAYAAADjJSv1AAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1 @@ -746,6 +770,9 @@ t7PIdXRT9kCF1lo8u3IPfFOKqVljg2IAAAAASUVORK5CYII= + + True + iVBORw0KGgoAAAANSUhEUgAAAMgAAABNCAYAAADjJSv1AAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1 @@ -773,6 +800,9 @@ wxjjQQ1jjAc1jDEe1DDGiJcX/wKcO4zm90rrbQAAAABJRU5ErkJggg== + + True + iVBORw0KGgoAAAANSUhEUgAAAMgAAABZCAYAAAB7Ymt4AAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1 @@ -801,6 +831,9 @@ jfkqyMbMAAAAAElFTkSuQmCC + + True + iVBORw0KGgoAAAANSUhEUgAAAMgAAABZCAYAAAB7Ymt4AAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1 @@ -829,4 +862,7 @@ 6l8KOJAbKVKmPQAAAABJRU5ErkJggg== + + True + \ No newline at end of file