using System; using System.Collections.Generic; using System.Diagnostics; 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 static GTPCorrgir.Opciones; using static System.Net.WebRequestMethods; using ComboBox = System.Windows.Controls.ComboBox; using Cursors = System.Windows.Input.Cursors; using KeyEventArgs = System.Windows.Input.KeyEventArgs; using MouseEventArgs = System.Windows.Input.MouseEventArgs; namespace GTPCorrgir { /// /// Interaction logic for Chat.xaml /// public partial class Chat : Window { gtpask AI_API; string respuestas; public Chat(gtpask GTP) { InitializeComponent(); PositionWindow(); // Inicializar componentes de la UI, por ejemplo, llenar el ComboBox AI_API = GTP; questionArea.Text = ""; //GTP.TextoACorregir; respuestas = ""; foreach (KeyValuePair kvp in Opciones.Instance.nombreLLM) { ComboBoxItem item = new ComboBoxItem(); item.Content = kvp.Value; // El texto que se mostrará item.Tag = kvp.Key; // Guarda el valor enum en el Tag para acceso posterior modelSelector.Items.Add(item); // Verifica si este ítem debe ser el seleccionado if (kvp.Key == Opciones.Instance.LLM) { modelSelector.SelectedItem = item; } } } private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Iniciar movimiento de la ventana si se presiona el botón izquierdo del ratón if (e.LeftButton == MouseButtonState.Pressed) { this.DragMove(); } } private void Window_MouseEnter(object sender, MouseEventArgs e) { // Hacer la ventana opaca cuando el ratón esté sobre ella this.Opacity = 1.0; } private void Window_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Escape) { this.Close(); } } private void QuestionArea_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter && !e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Shift)) { SendButton_Click(this, new RoutedEventArgs()); e.Handled = true; // Prevenir el salto de línea en el TextBox } } private void Window_MouseLeave(object sender, MouseEventArgs e) { // Hacer la ventana transparente cuando el ratón no esté sobre ella this.Opacity = 0.2; // Ajusta este valor a tu preferencia } private void SendButton_Click(object sender, RoutedEventArgs e) { // Aquí lógica para enviar pregunta y recibir respuesta de OpenAI AI_API.TextoACorregir = questionArea.Text; if (AI_API.TextoACorregir.Length > 3) { sendButton.IsEnabled = false; // Deshabilitar el botón de envío Mouse.OverrideCursor = Cursors.Wait; // Cambiar el cursor a espera Task.Run(async () => { try { await AI_API.CorregirTexto(); } catch (Exception ex) { Console.WriteLine("Error durante la corrección de texto: " + ex.Message); } finally { Dispatcher.Invoke(async () => // Nota el 'async' aquí para permitir 'await' { if (AI_API.TextoCorregido != null) { System.Windows.Clipboard.SetText(AI_API.TextoCorregido); //responseArea. .Text += AI_API.TextoCorregido + "\r\n"; AddMarkdownContent(AI_API.TextoCorregido + "\r\n"); Mouse.OverrideCursor = null; // Restaurar el cursor normal sendButton.IsEnabled = true; // Habilitar el botón de envío } }); } }); } } public void AddMarkdownContent(string markdownText) { // Transforma el texto Markdown a un FlowDocument var markdown = new Markdown.Xaml.Markdown(); respuestas += markdownText + "\r\n"; responseArea.Document = markdown.Transform(respuestas); } private void PositionWindow() { // Obtener la posición del cursor var cursorPosition = System.Windows.Forms.Cursor.Position; // Determinar en qué pantalla está el cursor var screen = Screen.FromPoint(cursorPosition); // Calcular la ubicación central en la pantalla actual this.Left = (screen.WorkingArea.Width - this.Width) / 2 + screen.WorkingArea.Left; this.Top = (screen.WorkingArea.Height - this.Height) / 2 + screen.WorkingArea.Top; } private void CambiarModelo(object sender, SelectionChangedEventArgs e) { ComboBox comboBox = sender as ComboBox; ComboBoxItem selectedItem = comboBox.SelectedItem as ComboBoxItem; if (selectedItem != null) { LLM_a_Usar selectedEnum = (LLM_a_Usar)selectedItem.Tag; Opciones.Instance.LLM = selectedEnum; // Suponiendo que hay una propiedad para establecerlo } } private void clearButton_Click(object sender, RoutedEventArgs e) { respuestas = ""; AddMarkdownContent(""); } } }