GTPCorrgir/App.xaml.cs

112 lines
3.6 KiB
C#

using System.Configuration;
using System.Data;
using System.Windows;
using System;
using System.Windows;
using System.Windows.Forms; // Necesitas agregar una referencia a System.Windows.Forms
using Application = System.Windows.Application;
using System.Diagnostics; // Asegúrate de incluir esta directiva para usar Stopwatch
using System.Threading.Tasks;
namespace GTPCorrgir
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
gtpask GTP = new gtpask();
NotifyIcon notificacion;
System.Windows.Forms.Timer timer;
int segundos = 0;
bool CorreccionFinalizada = false;
Stopwatch stopwatch = new Stopwatch();
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
stopwatch.Start();
if (System.Windows.Clipboard.ContainsText())
{
GTP.TextoACorregir = System.Windows.Clipboard.GetText();
}
// Muestra notificación inicial y comienza el cronómetro en el hilo de la UI
MostrarNotificacion("Espera", "Corrigiendo texto...");
IniciarCronometro();
// Ejecuta la tarea de corrección en un hilo secundario
Task.Run(async () =>
{
try
{
await GTP.CorregirTexto();
}
catch (Exception ex)
{
// Manejo de errores (puedes mostrar un mensaje de error si es necesario)
Console.WriteLine("Error durante la corrección de texto: " + ex.Message);
}
finally
{
// Asegura que el timer se detenga y la notificación se actualice en el hilo de la UI
Dispatcher.Invoke(() =>
{
CorreccionFinalizada = true;
DetenerCronometro();
if (GTP.TextoCorregido != null)
{
System.Windows.Clipboard.SetText(GTP.TextoCorregido);
MostrarNotificacion("Corrección Lista", GTP.TextoCorregido);
}
else
{
MostrarNotificacion("Error", "No se pudo corregir el texto.");
}
Application.Current.Shutdown();
});
}
});
}
private void MostrarNotificacion(string titulo, string mensaje)
{
notificacion = new NotifyIcon
{
Icon = SystemIcons.Information,
BalloonTipTitle = titulo,
BalloonTipText = mensaje,
Visible = true
};
notificacion.ShowBalloonTip(1000);
}
private void IniciarCronometro()
{
timer = new System.Windows.Forms.Timer();
timer.Interval = 1000; // 1000 milisegundos (1 segundo)
timer.Tick += ActualizarCronometro;
timer.Start();
}
private void ActualizarCronometro(object sender, EventArgs e)
{
if (!CorreccionFinalizada) {
segundos++;
notificacion.BalloonTipText = $"Texto en {GTP.IdiomaDetectado} pasados: {Math.Round(stopwatch.ElapsedMilliseconds/1000.0,1)} s";
notificacion.ShowBalloonTip(1000);
}
}
private void DetenerCronometro()
{
timer.Stop();
timer.Dispose();
}
}
}