268 lines
9.1 KiB
C#
268 lines
9.1 KiB
C#
using System;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
using System.Windows.Media;
|
|
using ShortcutsHelper.Models;
|
|
using ShortcutsHelper.ViewModels;
|
|
|
|
namespace ShortcutsHelper
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for MainWindow.xaml
|
|
/// </summary>
|
|
public partial class MainWindow : Window
|
|
{
|
|
private MainViewModel ViewModel => (MainViewModel)DataContext;
|
|
private bool _isInitialized = false;
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
this.Closing += MainWindow_Closing;
|
|
this.Loaded += MainWindow_Loaded;
|
|
this.SizeChanged += MainWindow_SizeChanged;
|
|
this.LocationChanged += MainWindow_LocationChanged;
|
|
this.Deactivated += MainWindow_Deactivated;
|
|
this.Activated += MainWindow_Activated;
|
|
|
|
// Suscribirse al evento de cambio de visibilidad
|
|
ViewModel.VisibilityChanged += OnVisibilityChanged;
|
|
}
|
|
|
|
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
|
|
{
|
|
LoadWindowSettings();
|
|
this.Opacity = 1.0; // Inicializar con opacidad completa
|
|
_isInitialized = true;
|
|
}
|
|
|
|
private void OnVisibilityChanged(object? sender, bool shouldBeVisible)
|
|
{
|
|
// Ejecutar en el hilo de UI
|
|
Dispatcher.BeginInvoke(() =>
|
|
{
|
|
if (shouldBeVisible)
|
|
{
|
|
// Mostrar ventana con opacidad completa
|
|
if (this.Visibility == Visibility.Hidden)
|
|
{
|
|
this.Show();
|
|
}
|
|
if (this.WindowState == WindowState.Minimized)
|
|
{
|
|
this.WindowState = WindowState.Normal;
|
|
}
|
|
this.Opacity = 1.0; // 100% opacidad
|
|
}
|
|
else
|
|
{
|
|
// En lugar de ocultar, usar transparencia del 20%
|
|
this.Opacity = 0.2; // 20% opacidad
|
|
// Mantener la ventana visible pero semi-transparente
|
|
if (this.Visibility == Visibility.Hidden)
|
|
{
|
|
this.Show();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void PinButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
ViewModel.TogglePinMode();
|
|
}
|
|
|
|
private void LoadWindowSettings()
|
|
{
|
|
try
|
|
{
|
|
var settings = ViewModel.GetCurrentApplicationSettings();
|
|
|
|
this.Left = settings.Left;
|
|
this.Top = settings.Top;
|
|
this.Width = settings.Width;
|
|
this.Height = settings.Height;
|
|
|
|
// Aplicar anchos de columna
|
|
if (ShortcutsDataGrid != null)
|
|
{
|
|
FavoriteColumn.Width = settings.FavoriteColumnWidth;
|
|
ShortcutColumn.Width = settings.ShortcutColumnWidth;
|
|
// Para DataGridTemplateColumn necesitamos manejar el ancho diferente
|
|
DescriptionColumn.Width = new DataGridLength(settings.DescriptionColumnWidth, DataGridLengthUnitType.Pixel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error al cargar configuración de ventana: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
|
|
{
|
|
if (_isInitialized)
|
|
{
|
|
SaveWindowSettings();
|
|
}
|
|
}
|
|
|
|
private void MainWindow_LocationChanged(object? sender, EventArgs e)
|
|
{
|
|
if (_isInitialized)
|
|
{
|
|
SaveWindowSettings();
|
|
}
|
|
}
|
|
|
|
private void SaveWindowSettings()
|
|
{
|
|
try
|
|
{
|
|
ViewModel.UpdateWindowSettings(this.Left, this.Top, this.Width, this.Height);
|
|
|
|
if (ShortcutsDataGrid != null)
|
|
{
|
|
// Para DataGridTemplateColumn obtenemos el ancho diferente
|
|
double descriptionWidth = DescriptionColumn.ActualWidth;
|
|
|
|
ViewModel.UpdateColumnWidths(
|
|
FavoriteColumn.Width.Value,
|
|
ShortcutColumn.Width.Value,
|
|
descriptionWidth);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error al guardar configuración de ventana: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private void MainWindow_Deactivated(object? sender, EventArgs e)
|
|
{
|
|
// Confirmar cualquier edición pendiente en el DataGrid
|
|
CommitDataGridEdits();
|
|
|
|
// Guardar cambios cuando pierde el foco
|
|
ViewModel.SavePendingChanges();
|
|
}
|
|
|
|
private void MainWindow_Activated(object? sender, EventArgs e)
|
|
{
|
|
// Cuando la ventana se activa, asegurar opacidad completa
|
|
this.Opacity = 1.0;
|
|
}
|
|
|
|
private void MainWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
// Confirmar cualquier edición pendiente en el DataGrid
|
|
CommitDataGridEdits();
|
|
|
|
SaveWindowSettings();
|
|
|
|
// Desuscribirse del evento
|
|
ViewModel.VisibilityChanged -= OnVisibilityChanged;
|
|
|
|
ViewModel.Dispose();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error al cerrar aplicación: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
private void MenuItem_Delete_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
ViewModel.DeleteSelectedShortcut();
|
|
}
|
|
|
|
private void MenuItem_CaptureKeys_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
ViewModel.StartKeyCapture();
|
|
}
|
|
|
|
private void ShortcutsDataGrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
|
|
{
|
|
// Verificar si se hizo doble clic en una celda (no en el header)
|
|
if (e.OriginalSource is FrameworkElement element)
|
|
{
|
|
// Buscar si el clic fue en una celda del DataGrid
|
|
var cell = element.GetVisualParent<DataGridCell>();
|
|
if (cell != null && cell.Column == DescriptionColumn)
|
|
{
|
|
// Iniciar edición de la celda de descripción
|
|
ShortcutsDataGrid.BeginEdit();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ShortcutsDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
|
|
{
|
|
// Este evento se dispara cuando termina la edición de una celda
|
|
// No cancelamos la edición aquí, solo nos aseguramos de que se procese
|
|
if (!e.Cancel)
|
|
{
|
|
// Forzar la actualización del binding
|
|
Dispatcher.BeginInvoke(() =>
|
|
{
|
|
if (e.EditingElement is System.Windows.Controls.TextBox textBox)
|
|
{
|
|
var bindingExpression = textBox.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty);
|
|
bindingExpression?.UpdateSource();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
private void ShortcutsDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
|
|
{
|
|
// Este evento se dispara cuando termina la edición de una fila
|
|
// Aquí podrían guardarse los cambios si fuera necesario
|
|
if (!e.Cancel && e.Row.Item is ShortcutRecord shortcut)
|
|
{
|
|
// Forzar guardar cambios después de que termine la edición de la fila
|
|
Dispatcher.BeginInvoke(() =>
|
|
{
|
|
if (shortcut.IsModified)
|
|
{
|
|
ViewModel.SavePendingChanges();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
private void CommitDataGridEdits()
|
|
{
|
|
try
|
|
{
|
|
if (ShortcutsDataGrid != null)
|
|
{
|
|
// Terminar cualquier edición activa
|
|
ShortcutsDataGrid.CommitEdit(DataGridEditingUnit.Row, true);
|
|
ShortcutsDataGrid.CommitEdit(DataGridEditingUnit.Cell, true);
|
|
|
|
// Actualizar el binding
|
|
ShortcutsDataGrid.UpdateLayout();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error al confirmar ediciones del DataGrid: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Método de extensión para buscar elementos padre en el árbol visual
|
|
public static class VisualTreeHelperExtensions
|
|
{
|
|
public static T? GetVisualParent<T>(this DependencyObject child) where T : DependencyObject
|
|
{
|
|
var parent = VisualTreeHelper.GetParent(child);
|
|
if (parent == null) return null;
|
|
if (parent is T) return (T)parent;
|
|
return parent.GetVisualParent<T>();
|
|
}
|
|
}
|
|
} |