add a progress bar and timeout text

This commit is contained in:
mshafer1
2024-12-20 17:08:53 -06:00
parent f908e9c4b0
commit 65f27c7c42
2 changed files with 147 additions and 17 deletions

View File

@@ -2,6 +2,9 @@
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.ComponentModel;
using Microsoft.Win32;
using System.Diagnostics;
namespace ntfysh_client
@@ -13,8 +16,21 @@ namespace ntfysh_client
private const int ScreenMargin = 20;
private System.Timers.Timer? timer = null;
private int _timeout = 0;
private System.Timers.Timer? displayTimeoutTimer = null;
private System.Windows.Forms.Timer? updateTimer = null;
private Stopwatch? shownStopwatch = null;
private ToolTipIcon? _icon;
private int _progress_value = 0;
private int progress
{
get { return this._progress_value; }
set
{
this._progress_value = value;
this.progressBar1.Value = value;
}
}
public bool IsVisible
{
@@ -34,30 +50,69 @@ namespace ntfysh_client
{
if (this.IsVisible)
{
// close the current notification
this.handleTimeout(null, null);
}
// setup data
this._icon = icon;
if (this._icon != null)
{
this.iconBox.Image = ConvertToolTipIconToImage(_icon.Value);
}
if (this.timer != null)
this.tbTitle.Text = title;
this.tbMessage.Text = message;
// setup timers
if (this.displayTimeoutTimer != null)
{
this.timer.Stop();
this.timer.Dispose();
this.displayTimeoutTimer.Stop();
this.displayTimeoutTimer.Dispose();
}
if (this.updateTimer != null)
{
this.updateTimer.Stop();
this.updateTimer.Dispose();
}
if (timeout_ms > 0)
{
this.timer = new System.Timers.Timer(timeout_ms);
timer.Elapsed += handleTimeout;
this.timer.Start();
this.displayTimeoutTimer = new System.Timers.Timer(timeout_ms);
displayTimeoutTimer.Elapsed += handleTimeout;
this.displayTimeoutTimer.Start();
this.progress = 100;
this.updateTimer = new System.Windows.Forms.Timer();
updateTimer.Interval = 100;
this.updateTimer.Tick += this.UpdateProgress;
this.updateTimer.Start();
this.shownStopwatch = new Stopwatch();
this.shownStopwatch.Start();
this.progressBar1.Visible = true;
this.lbTimeout.Visible = true;
this._timeout = timeout_ms;
}
this.tbTitle.Text = title;
this.tbMessage.Text = message;
else
{
this.progressBar1.Visible = false;
this.lbTimeout.Visible = false;
}
// ok, show the window
this.Show();
this.SetWindowPosition();
}
private void UpdateProgress(object? sender, EventArgs e)
{
if (this.shownStopwatch == null)
{
return;
}
this.progress = (int)((this._timeout - this.shownStopwatch.ElapsedMilliseconds) * 100 / this._timeout);
this.lbTimeout.Text = $"{(int)(this._timeout - this.shownStopwatch.ElapsedMilliseconds) / 1000}";
}
protected override void SetVisibleCore(bool value)
{
this.SetWindowPosition();
@@ -99,12 +154,7 @@ namespace ntfysh_client
private void handleTimeout(object? sender, EventArgs? e)
{
if (this.timer != null) // check if the timer has already been disposed
{
this.timer.Stop();
this.timer.Dispose();
this.timer = null;
}
this.cancelTimer();
if (this.InvokeRequired)
{
// on a background thread, so invoke on the UI thread
@@ -159,5 +209,47 @@ namespace ntfysh_client
public const int AW_SLIDE = 0x00040000;
public const int AW_BLEND = 0x00080000;
}
private void window_MouseDown(object sender, EventArgs e)
{
this.cancelTimer();
}
private void cancelTimer()
{
if (this.InvokeRequired)
{
// on a background thread, so invoke on the UI thread
this.Invoke(new Action(() =>
{
this.lbTimeout.Visible = false;
this.progressBar1.Visible = false;
}));
}
else
{
// in the UI thread, invoke directly
this.lbTimeout.Visible = false;
this.progressBar1.Visible = false;
}
if (this.displayTimeoutTimer != null) // check if the timer has already been disposed
{
this.displayTimeoutTimer.Stop();
this.displayTimeoutTimer.Dispose();
this.displayTimeoutTimer = null;
}
if (this.updateTimer != null)
{
this.updateTimer.Stop();
this.updateTimer.Dispose();
this.updateTimer = null;
}
if (this.shownStopwatch != null)
{
this.shownStopwatch.Stop();
this.shownStopwatch = null;
}
}
}
}