GTPCorrgir/App.xaml.cs

126 lines
4.1 KiB
C#
Raw Normal View History

2024-04-24 03:33:57 -03:00
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;
2024-04-24 03:33:57 -03:00
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;
bool CorreccionFinalizada = false;
Stopwatch stopwatch = new Stopwatch();
private notificacion notificationWindow;
2024-04-24 03:33:57 -03:00
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
stopwatch.Start();
2024-04-24 03:33:57 -03:00
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
ShowCustomNotification("Espera", "Corrigiendo texto...");
IniciarCronometro();
// Ejecuta la tarea de corrección en un hilo secundario
2024-04-24 03:33:57 -03:00
Task.Run(async () =>
{
try
{
await GTP.CorregirTexto();
}
catch (Exception ex)
{
Console.WriteLine("Error durante la corrección de texto: " + ex.Message);
}
finally
2024-04-24 03:33:57 -03:00
{
Dispatcher.Invoke(async () => // Nota el 'async' aquí para permitir 'await'
{
CorreccionFinalizada = true;
DetenerCronometro();
if (GTP.TextoCorregido != null)
{
System.Windows.Clipboard.SetText(GTP.TextoCorregido);
ShowCustomNotification("Se puede pegar", $"Corrección en : {Math.Round(stopwatch.ElapsedMilliseconds / 1000.0, 1)} s");
await Task.Delay(5000); // Asíncrono espera 5 segundos
}
else
{
MostrarNotificacion("Error", "No se pudo corregir el texto.");
}
Application.Current.Shutdown();
});
}
2024-04-24 03:33:57 -03:00
});
2024-04-24 03:33:57 -03:00
}
private void ShowCustomNotification(string title, string message)
{
if (notificationWindow == null)
{
notificationWindow = new notificacion();
notificationWindow.Show();
}
notificationWindow.UpdateNotification(title, message);
}
2024-04-24 03:33:57 -03:00
private void MostrarNotificacion(string titulo, string mensaje)
{
notificacion = new NotifyIcon
2024-04-24 03:33:57 -03:00
{
Icon = SystemIcons.Information,
BalloonTipTitle = titulo,
BalloonTipText = mensaje,
Visible = true
};
notificacion.ShowBalloonTip(1000);
}
private void IniciarCronometro()
{
timer = new System.Windows.Forms.Timer();
timer.Interval = 100; // 1000 milisegundos (1 segundo)
timer.Tick += ActualizarCronometro;
timer.Start();
}
private void ActualizarCronometro(object sender, EventArgs e)
{
if (!CorreccionFinalizada) {
//notificacion.BalloonTipText = $"Texto en {GTP.IdiomaDetectado} pasados: {Math.Round(stopwatch.ElapsedMilliseconds/1000.0,1)} s";
//notificacion.ShowBalloonTip(1000);
ShowCustomNotification("Trabajando..", $"Texto en {GTP.IdiomaDetectado} pasados: {Math.Round(stopwatch.ElapsedMilliseconds / 1000.0, 1)} s");
}
}
private void DetenerCronometro()
{
timer.Stop();
timer.Dispose();
2024-04-24 03:33:57 -03:00
}
}
2024-04-24 03:33:57 -03:00
}