GTPCorrgir/notificacion.xaml.cs

114 lines
3.5 KiB
C#

using System;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Threading;
namespace GTPCorrgir
{
public partial class notificacion : Window
{
private readonly DispatcherTimer autoCloseTimer;
private readonly DispatcherTimer progressTimer;
private double progressValue = 0;
private const int AUTO_CLOSE_SECONDS = 5;
private const int PROGRESS_UPDATE_INTERVAL = 50; // milisegundos
public notificacion()
{
InitializeComponent();
PositionWindow();
// Configurar el timer para auto-cierre
autoCloseTimer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(AUTO_CLOSE_SECONDS)
};
autoCloseTimer.Tick += AutoCloseTimer_Tick;
// Configurar el timer para la barra de progreso
progressTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(PROGRESS_UPDATE_INTERVAL)
};
progressTimer.Tick += ProgressTimer_Tick;
// Iniciar animación de entrada
Loaded += (s, e) =>
{
var storyboard = (Storyboard)FindResource("FadeIn");
storyboard.Begin(this);
autoCloseTimer.Start();
progressTimer.Start();
};
}
private void PositionWindow()
{
var cursorPosition = System.Windows.Forms.Cursor.Position;
var screen = System.Windows.Forms.Screen.FromPoint(cursorPosition);
this.Left = screen.WorkingArea.Right - this.Width - 20;
this.Top = screen.WorkingArea.Bottom - this.Height - 20;
}
public void UpdateNotification(string title, string message)
{
TitleText.Text = title;
MessageText.Text = message;
// Reiniciar timers y progreso
progressValue = 0;
AutoCloseProgress.Value = 0;
autoCloseTimer.Stop();
progressTimer.Stop();
autoCloseTimer.Start();
progressTimer.Start();
}
private async void AutoCloseTimer_Tick(object sender, EventArgs e)
{
autoCloseTimer.Stop();
progressTimer.Stop();
var storyboard = (Storyboard)FindResource("FadeOut");
storyboard.Completed += (s, _) => Close();
storyboard.Begin(this);
}
private void ProgressTimer_Tick(object sender, EventArgs e)
{
progressValue += (PROGRESS_UPDATE_INTERVAL / (AUTO_CLOSE_SECONDS * 1000.0)) * 100;
AutoCloseProgress.Value = progressValue;
if (progressValue >= 100)
{
progressTimer.Stop();
}
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
autoCloseTimer.Stop();
progressTimer.Stop();
var storyboard = (Storyboard)FindResource("FadeOut");
storyboard.Completed += (s, _) => Close();
storyboard.Begin(this);
}
protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
{
base.OnMouseEnter(e);
autoCloseTimer.Stop();
progressTimer.Stop();
}
protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
{
base.OnMouseLeave(e);
autoCloseTimer.Start();
progressTimer.Start();
}
}
}