GTPCorrgir/Chat.xaml.cs

176 lines
6.3 KiB
C#
Raw Normal View History

2024-04-29 05:56:48 -03:00
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;
2024-04-29 06:32:37 -03:00
using System.Windows.Forms;
2024-04-29 05:56:48 -03:00
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
{
/// <summary>
/// Interaction logic for Chat.xaml
/// </summary>
public partial class Chat : Window
{
gtpask AI_API;
2024-04-29 06:32:37 -03:00
string respuestas;
2024-04-29 05:56:48 -03:00
public Chat(gtpask GTP)
{
InitializeComponent();
2024-04-29 06:32:37 -03:00
PositionWindow();
2024-04-29 05:56:48 -03:00
// Inicializar componentes de la UI, por ejemplo, llenar el ComboBox
AI_API = GTP;
questionArea.Text = ""; //GTP.TextoACorregir;
2024-04-29 06:32:37 -03:00
respuestas = "";
2024-04-29 05:56:48 -03:00
foreach (KeyValuePair<LLM_a_Usar, string> 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";
2024-04-29 06:32:37 -03:00
AddMarkdownContent(AI_API.TextoCorregido + "\r\n");
2024-04-29 05:56:48 -03:00
Mouse.OverrideCursor = null; // Restaurar el cursor normal
sendButton.IsEnabled = true; // Habilitar el botón de envío
}
});
}
});
}
}
2024-04-29 06:32:37 -03:00
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;
}
2024-04-29 05:56:48 -03:00
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
}
}
2024-04-29 06:32:37 -03:00
private void clearButton_Click(object sender, RoutedEventArgs e)
{
respuestas = "";
AddMarkdownContent("");
}
2024-04-29 05:56:48 -03:00
}
}