From fe2624e91eafe7eeafd59a687fd319917dfc5284 Mon Sep 17 00:00:00 2001 From: Miguel Date: Fri, 26 Apr 2024 17:57:18 +0200 Subject: [PATCH] Creada Ventada de Resultado para ver rapidamente el resultado antes de pegar. --- App.xaml | 4 +- App.xaml.cs | 11 +++- GTPCorrgir.csproj | 9 +++ HalfValueConverter.cs | 27 +++++++++ VentanaResultado.xaml | 14 +++++ VentanaResultado.xaml.cs | 118 +++++++++++++++++++++++++++++++++++++++ gtpask.cs | 22 +++++++- 7 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 HalfValueConverter.cs create mode 100644 VentanaResultado.xaml create mode 100644 VentanaResultado.xaml.cs diff --git a/App.xaml b/App.xaml index c6f9ce0..ac9da6d 100644 --- a/App.xaml +++ b/App.xaml @@ -3,6 +3,8 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:GTPCorrgir"> + + + - diff --git a/App.xaml.cs b/App.xaml.cs index 84634a3..2ed46dd 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -7,12 +7,16 @@ using System.Windows.Forms; // Necesitas agregar una referencia a System.Windows 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; namespace GTPCorrgir { /// /// Interaction logic for App.xaml /// + /// + public partial class App : Application { gtpask GTP = new gtpask(); @@ -27,7 +31,6 @@ namespace GTPCorrgir { base.OnStartup(e); - stopwatch.Start(); if (System.Windows.Clipboard.ContainsText()) @@ -39,6 +42,7 @@ namespace GTPCorrgir ShowCustomNotification("Espera", "Corrigiendo texto..."); IniciarCronometro(); + // Ejecuta la tarea de corrección en un hilo secundario Task.Run(async () => { @@ -60,13 +64,16 @@ namespace GTPCorrgir { System.Windows.Clipboard.SetText(GTP.TextoCorregido); ShowCustomNotification("Se puede pegar", $"Corrección en : {Math.Round(stopwatch.ElapsedMilliseconds / 1000.0, 1)} s"); + + var resultadoWindow = new VentanaResultado(GTP.TextoCorregido); + resultadoWindow.Show(); + await Task.Delay(5000); // Asíncrono espera 5 segundos } else { MostrarNotificacion("Error", "No se pudo corregir el texto."); } - Application.Current.Shutdown(); }); } }); diff --git a/GTPCorrgir.csproj b/GTPCorrgir.csproj index e668e1b..b3a4eae 100644 --- a/GTPCorrgir.csproj +++ b/GTPCorrgir.csproj @@ -10,6 +10,15 @@ + + + tlbimp + 4 + 2 + 215d64d2-031c-33c7-96e3-61794cd1ee61 + + + diff --git a/HalfValueConverter.cs b/HalfValueConverter.cs new file mode 100644 index 0000000..1fe8667 --- /dev/null +++ b/HalfValueConverter.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Data; + +namespace GTPCorrgir +{ + public class HalfValueConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is double) + { + return ((double)value) / 2; + } + return value; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/VentanaResultado.xaml b/VentanaResultado.xaml new file mode 100644 index 0000000..da86d1b --- /dev/null +++ b/VentanaResultado.xaml @@ -0,0 +1,14 @@ + + + + + + + + diff --git a/VentanaResultado.xaml.cs b/VentanaResultado.xaml.cs new file mode 100644 index 0000000..b75d3ea --- /dev/null +++ b/VentanaResultado.xaml.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Forms; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Shapes; +using Brushes = System.Windows.Media.Brushes; +using Size = System.Windows.Size; +using Application = System.Windows.Application; +using System.Runtime.InteropServices; +using MouseEventArgs = System.Windows.Input.MouseEventArgs; +using FlowDirection = System.Windows.FlowDirection; + + +namespace GTPCorrgir +{ + /// + /// Interaction logic for VentanaResultado.xaml + /// + public partial class VentanaResultado : Window + { + [StructLayout(LayoutKind.Sequential)] + public struct POINT + { + public int X; + public int Y; + } + + [DllImport("user32.dll")] + public static extern bool GetCursorPos(out POINT lpPoint); + + public VentanaResultado(string message) + { + InitializeComponent(); + MessageText.Text = message; + + // Medir el tamaño del texto + System.Windows.Size textSize = MeasureTextSize(message); + + // Ajustar el tamaño del TextBlock + MessageText.Width = textSize.Width; // Establece el ancho real del TextBlock al ancho medido + MessageText.MaxWidth = SystemParameters.PrimaryScreenWidth * 2 / 3; // Límite máximo si necesario + MessageText.MaxHeight = SystemParameters.WorkArea.Height / 2; // Límite máximo de altura + + // Configurar el tamaño de la ventana basado en el tamaño del texto + this.Width = MessageText.Width + 20; // Considera los paddings del borde + this.Height = textSize.Height + 20; + + // Posiciona la ventana + POINT cursorPoint; + if (GetCursorPos(out cursorPoint)) + { + this.Left = cursorPoint.X - this.Width / 2; + this.Top = cursorPoint.Y - this.Height / 2; + } + + // Eventos para cerrar y mover la ventana + this.MouseLeftButtonDown += (s, e) => this.Close(); + this.MouseMove += MainWindow_MouseMove; + this.MouseLeave += MainWindow_MouseLeave; + } + + private void MainWindow_MouseLeave(object sender, MouseEventArgs e) + { + this.Close(); // Cierra la ventana cuando el ratón sale de su área + Application.Current.Shutdown(); // Terminar la aplicación + } + + private void MainWindow_MouseMove(object sender, MouseEventArgs e) + { + POINT cursorPoint; + if (GetCursorPos(out cursorPoint)) + { + this.Left = cursorPoint.X - this.Width / 2; + this.Top = cursorPoint.Y - this.Height / 2; + } + } + + private Size MeasureTextSize(string message) + { + // Establece el ancho máximo del texto como dos tercios del ancho de la pantalla + double maxWidth = SystemParameters.PrimaryScreenWidth * 2 / 3; + + // Configurando FormattedText con el ancho máximo + var formattedText = new FormattedText( + message, + CultureInfo.CurrentCulture, + FlowDirection.LeftToRight, + new Typeface(MessageText.FontFamily, MessageText.FontStyle, MessageText.FontWeight, MessageText.FontStretch), + MessageText.FontSize, + Brushes.Black, // Asegúrate de que estás usando System.Windows.Media.Brushes + new NumberSubstitution(), + TextFormattingMode.Display); + + // Aplicar el ancho máximo para que el texto se ajuste dentro de este límite + formattedText.MaxTextWidth = maxWidth; + + // Calcular la altura necesaria para todo el texto dentro del ancho máximo + return new Size(formattedText.Width, formattedText.Height); + } + + + private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) + { + this.Close(); // Cerrar la ventana al hacer clic + Application.Current.Shutdown(); // Terminar la aplicación + } + } +} diff --git a/gtpask.cs b/gtpask.cs index 09a69c0..00ba1d0 100644 --- a/gtpask.cs +++ b/gtpask.cs @@ -12,6 +12,11 @@ using System.Diagnostics; namespace GTPCorrgir { + public enum LLM_a_Usar + { + OpenAI, + Ollama + } internal class gtpask { private readonly string openAiApiKey = "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"; @@ -29,6 +34,8 @@ namespace GTPCorrgir public string TextoACorregir; public string TextoCorregido; public string TextodeSistema; + private const bool Simulacion = false; + private const LLM_a_Usar LLM = LLM_a_Usar.OpenAI; private string CrearMensajeDeSistema() @@ -70,6 +77,9 @@ namespace GTPCorrgir public async Task CorregirTexto() { + if (Simulacion) + TextoACorregir = "La FB482 puo gestire un divider di 7 uscite a 3 punte di scambio pero su questa macchina abbiamo un divider a 3 uscite e solo un punto di scambio."; + Log.Log(""); Log.Log("Texto a corregir: " + TextoACorregir); if (DetectarIdioma()) { @@ -87,8 +97,16 @@ namespace GTPCorrgir Log.Log("Texto marcado: " + TextoACorregir); - string RespuestaLLM = await CallOpenAiApi(TextoACorregir); - //string RespuestaLLM = await CallOllamaApi(TextoACorregir); + string RespuestaLLM; + if (!Simulacion) + { + if (LLM == LLM_a_Usar.OpenAI) RespuestaLLM = await CallOpenAiApi(TextoACorregir); + if (LLM == LLM_a_Usar.Ollama) RespuestaLLM = await CallOllamaApi(TextoACorregir); + } else + { + await Task.Delay(1000); + RespuestaLLM = "{ 'Rewritten_text': 'Movimiento Continuo: Este enfoque hará que la ventana se mueva continuamente mientras el ratón se mueva sobre ella. Esto podría ser deseable para tu aplicación, pero ten en cuenta que puede parecer un comportamiento inusual desde la perspectiva del usuario.\r\nRendimiento: Mover la ventana de esta manera puede ser demandante en términos de recursos del sistema, especialmente si se realiza con mucha frecuencia (por ejemplo, durante un movimiento rápido del ratón). Si observas problemas de rendimiento, podrías necesitar optimizar cómo y cuándo se actualiza la posición de la ventana.' }"; + } Log.Log("Respuesta: " + RespuestaLLM);