GTPCorrgir/Chat.xaml.cs

258 lines
8.4 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;
2024-11-14 12:37:08 -03:00
using Brushes = System.Windows.Media.Brushes;
using Color = System.Windows.Media.Color;
2024-04-29 05:56:48 -03:00
using ComboBox = System.Windows.Controls.ComboBox;
using Cursors = System.Windows.Input.Cursors;
2024-11-14 12:37:08 -03:00
using FontFamily = System.Windows.Media.FontFamily;
2024-04-29 05:56:48 -03:00
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;
2024-11-14 12:37:08 -03:00
using Timer = System.Windows.Forms.Timer;
2024-04-29 05:56:48 -03:00
namespace GTPCorrgir
{
public partial class Chat : Window
{
gtpask AI_API;
2024-04-29 06:32:37 -03:00
string respuestas;
2024-11-14 12:37:08 -03:00
private Timer opacityTimer;
private bool isMouseOver = false;
private const double OPACITY_ACTIVE = 1.0;
private const double OPACITY_INACTIVE = 0.2;
private const int OPACITY_DELAY_MS = 3000; // 10 segundos
2024-04-29 05:56:48 -03:00
public Chat(gtpask GTP)
{
InitializeComponent();
2024-11-14 12:37:08 -03:00
InitializeOpacityTimer();
2024-04-29 06:32:37 -03:00
PositionWindow();
2024-11-14 12:37:08 -03:00
// Inicializar componentes de la UI
2024-04-29 05:56:48 -03:00
AI_API = GTP;
2024-11-14 12:37:08 -03:00
questionArea.Text = "";
2024-04-29 06:32:37 -03:00
respuestas = "";
2024-11-14 12:37:08 -03:00
this.Opacity = OPACITY_ACTIVE;
InitializeModelSelector();
}
private void InitializeOpacityTimer()
{
opacityTimer = new Timer();
opacityTimer.Interval = OPACITY_DELAY_MS;
opacityTimer.Tick += (s, e) =>
{
if (!isMouseOver)
{
this.Dispatcher.Invoke(() =>
{
this.Opacity = OPACITY_INACTIVE;
});
}
opacityTimer.Stop();
};
}
private void InitializeModelSelector()
{
2024-04-29 05:56:48 -03:00
foreach (KeyValuePair<LLM_a_Usar, string> kvp in Opciones.Instance.nombreLLM)
{
ComboBoxItem item = new ComboBoxItem();
2024-11-14 12:37:08 -03:00
item.Content = kvp.Value;
item.Tag = kvp.Key;
2024-04-29 05:56:48 -03:00
modelSelector.Items.Add(item);
2024-11-14 12:37:08 -03:00
2024-04-29 05:56:48 -03:00
if (kvp.Key == Opciones.Instance.LLM)
{
modelSelector.SelectedItem = item;
}
2024-11-14 12:37:08 -03:00
}
}
private void SettingsButton_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new SettingsWindow
{
Owner = this,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
if (settingsWindow.ShowDialog() == true)
{
// Aplicar cambios de configuración
ApplySettings();
}
}
private void ApplySettings()
{
var settings = UserSettings.Instance;
// Aplicar opacidad
opacityTimer.Stop();
opacityTimer.Interval = ((int)TimeSpan.FromMilliseconds(settings.Appearance.OpacityDelay).TotalMilliseconds);
// Aplicar fuente
questionArea.FontFamily = new FontFamily(settings.Appearance.FontFamily);
questionArea.FontSize = settings.Appearance.FontSize;
responseArea.FontFamily = new FontFamily(settings.Appearance.FontFamily);
responseArea.FontSize = settings.Appearance.FontSize;
// Aplicar tema
if (settings.Appearance.Theme == "Dark")
{
// Aplicar tema oscuro
this.Background = new SolidColorBrush(Color.FromArgb(200, 30, 30, 30));
responseArea.Background = new SolidColorBrush(Color.FromArgb(255, 40, 40, 40));
responseArea.Foreground = Brushes.White;
questionArea.Background = new SolidColorBrush(Color.FromArgb(255, 50, 50, 50));
questionArea.Foreground = Brushes.White;
}
else
{
// Aplicar tema claro
this.Background = new SolidColorBrush(Color.FromArgb(200, 240, 240, 240));
responseArea.Background = Brushes.White;
responseArea.Foreground = Brushes.Black;
questionArea.Background = Brushes.White;
questionArea.Foreground = Brushes.Black;
}
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
ApplySettings(); // Aplicar configuración inicial
2024-04-29 05:56:48 -03:00
}
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
this.DragMove();
}
}
private void Window_MouseEnter(object sender, MouseEventArgs e)
{
2024-11-14 12:37:08 -03:00
isMouseOver = true;
opacityTimer.Stop();
this.Opacity = OPACITY_ACTIVE;
}
private void Window_MouseLeave(object sender, MouseEventArgs e)
{
isMouseOver = false;
opacityTimer.Start();
2024-04-29 05:56:48 -03:00
}
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());
2024-11-14 12:37:08 -03:00
e.Handled = true;
2024-04-29 05:56:48 -03:00
}
}
2024-11-14 12:37:08 -03:00
private async void SendButton_Click(object sender, RoutedEventArgs e)
2024-04-29 05:56:48 -03:00
{
AI_API.TextoACorregir = questionArea.Text;
if (AI_API.TextoACorregir.Length > 3)
{
2024-11-14 12:37:08 -03:00
try
2024-04-29 05:56:48 -03:00
{
2024-11-14 12:37:08 -03:00
sendButton.IsEnabled = false;
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () =>
2024-04-29 05:56:48 -03:00
{
2024-11-14 12:37:08 -03:00
try
2024-04-29 05:56:48 -03:00
{
2024-11-14 12:37:08 -03:00
await AI_API.CorregirTexto();
}
catch (Exception ex)
{
Console.WriteLine($"Error durante la corrección de texto: {ex.Message}");
throw;
}
});
if (AI_API.TextoCorregido != null)
{
System.Windows.Clipboard.SetText(AI_API.TextoCorregido);
AddMarkdownContent(AI_API.TextoCorregido + "\r\n");
2024-04-29 05:56:48 -03:00
}
2024-11-14 12:37:08 -03:00
}
catch (Exception ex)
{
AddMarkdownContent($"Error: {ex.Message}\r\n");
}
finally
{
Mouse.OverrideCursor = null;
sendButton.IsEnabled = true;
}
2024-04-29 05:56:48 -03:00
}
}
2024-04-29 06:32:37 -03:00
public void AddMarkdownContent(string markdownText)
{
var markdown = new Markdown.Xaml.Markdown();
respuestas += markdownText + "\r\n";
responseArea.Document = markdown.Transform(respuestas);
}
private void PositionWindow()
{
var cursorPosition = System.Windows.Forms.Cursor.Position;
var screen = Screen.FromPoint(cursorPosition);
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)
{
2024-11-14 12:37:08 -03:00
if (sender is ComboBox comboBox && comboBox.SelectedItem is ComboBoxItem selectedItem)
2024-04-29 05:56:48 -03:00
{
2024-11-14 12:37:08 -03:00
Opciones.Instance.LLM = (LLM_a_Usar)selectedItem.Tag;
2024-04-29 05:56:48 -03:00
}
}
2024-04-29 06:32:37 -03:00
private void clearButton_Click(object sender, RoutedEventArgs e)
{
respuestas = "";
AddMarkdownContent("");
}
2024-11-14 12:37:08 -03:00
protected override void OnClosed(EventArgs e)
{
opacityTimer?.Dispose();
base.OnClosed(e);
}
2024-04-29 05:56:48 -03:00
}
2024-11-14 12:37:08 -03:00
}