189 lines
6.7 KiB
C#
189 lines
6.7 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;
|
|
using System.Globalization;
|
|
using System.Windows.Data;
|
|
using System.Runtime.InteropServices;
|
|
|
|
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;
|
|
|
|
KeyboardHelper pasteLogic = new KeyboardHelper();
|
|
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
base.OnStartup(e);
|
|
|
|
pasteLogic.SaveCurrentWindow();
|
|
|
|
if (System.Windows.Clipboard.ContainsText())
|
|
{
|
|
GTP.TextoACorregir = System.Windows.Clipboard.GetText();
|
|
}
|
|
|
|
if (Opciones.Instance.modo == Opciones.modoDeUso.Corregir || Opciones.Instance.modo == Opciones.modoDeUso.Ortografia)
|
|
{
|
|
|
|
stopwatch.Start();
|
|
|
|
// Muestra notificación inicial y comienza el cronómetro en el hilo de la UI
|
|
ShowCustomNotification("Espera", $"Corrigiendo texto con .{Opciones.Instance.nombreDeLLM()} ..");
|
|
IniciarCronometro();
|
|
|
|
|
|
// Ejecuta la tarea de corrección en un hilo secundario
|
|
Task.Run(async () =>
|
|
{
|
|
try
|
|
{
|
|
await GTP.CorregirTexto();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Error durante la corrección de texto: " + ex.Message);
|
|
}
|
|
finally
|
|
{
|
|
await 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");
|
|
|
|
if (Opciones.Instance.modo == Opciones.modoDeUso.Corregir)
|
|
{
|
|
var resultadoWindow = new VentanaResultado(GTP.TextoCorregido);
|
|
resultadoWindow.Show();
|
|
await Task.Delay(1000); // Asíncrono espera 5 segundos
|
|
}
|
|
else if (Opciones.Instance.modo == Opciones.modoDeUso.Ortografia)
|
|
pasteLogic.RestoreAndSimulatePaste();
|
|
|
|
Application.Current.Shutdown();
|
|
}
|
|
else
|
|
{
|
|
MostrarNotificacion("Error", "No se pudo corregir el texto.");
|
|
Application.Current.Shutdown();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
} else if (Opciones.Instance.modo == Opciones.modoDeUso.Chat)
|
|
{
|
|
var chatWindows = new Chat(GTP);
|
|
chatWindows.Show();
|
|
}
|
|
|
|
}
|
|
|
|
private void ShowCustomNotification(string title, string message)
|
|
{
|
|
if (notificationWindow == null)
|
|
{
|
|
notificationWindow = new notificacion();
|
|
notificationWindow.Show();
|
|
}
|
|
|
|
notificationWindow.UpdateNotification(title, message);
|
|
}
|
|
|
|
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 = 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($"{Opciones.Instance.nombreDeLLM()} Trabajando..", $"Texto en {GTP.IdiomaDetectado} pasados: {Math.Round(stopwatch.ElapsedMilliseconds / 1000.0, 1)} s");
|
|
}
|
|
}
|
|
|
|
private void DetenerCronometro()
|
|
{
|
|
timer.Stop();
|
|
timer.Dispose();
|
|
}
|
|
}
|
|
|
|
|
|
public class KeyboardHelper
|
|
{
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
private static extern IntPtr GetForegroundWindow();
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
|
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
|
|
|
|
public const int KEYEVENTF_KEYUP = 0x0002; // Key up flag
|
|
public const int VK_CONTROL = 0x11; // Control key code
|
|
public const int V = 0x56; // V key code
|
|
|
|
private IntPtr previousWindow;
|
|
|
|
public void SaveCurrentWindow()
|
|
{
|
|
previousWindow = GetForegroundWindow();
|
|
}
|
|
|
|
public void RestoreAndSimulatePaste()
|
|
{
|
|
// Restore focus to the previous window
|
|
SetForegroundWindow(previousWindow);
|
|
|
|
// Simulating Ctrl+V
|
|
keybd_event((byte)VK_CONTROL, 0x9d, 0, UIntPtr.Zero); // Ctrl Press
|
|
keybd_event((byte)V, 0x9e, 0, UIntPtr.Zero); // V Press
|
|
keybd_event((byte)V, 0x9e, KEYEVENTF_KEYUP, UIntPtr.Zero); // V Release
|
|
keybd_event((byte)VK_CONTROL, 0x9d, KEYEVENTF_KEYUP, UIntPtr.Zero); // Ctrl Release
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|