EscribePassword/MainWindow.xaml.cs

229 lines
7.1 KiB
C#
Raw Normal View History

2024-06-15 06:24:07 -03:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;
using System.Windows;
2024-06-15 14:33:52 -03:00
using Application = System.Windows.Application;
using System.Windows.Input;
using MouseButton = System.Windows.Input.MouseButton;
using Gma.System.MouseKeyHook;
using MouseEventArgs = System.Windows.Forms.MouseEventArgs;
using System.Diagnostics;
using System.Windows.Controls;
using Clipboard = System.Windows.Clipboard;
2024-06-15 06:24:07 -03:00
namespace EscribePassword
{
2024-06-15 14:33:52 -03:00
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private IKeyboardMouseEvents _globalHook;
2024-06-15 14:33:52 -03:00
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
this.MouseWheel += MainWindow_Wheel;
HookManager();
2024-06-15 14:33:52 -03:00
}
private void DataGrid_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
if (sender is DataGrid dataGrid && dataGrid.CurrentCell.Item is Passwords selectedPassword)
{
var cellContent = (dataGrid.CurrentCell.Column.GetCellContent(selectedPassword) as TextBlock)?.Text;
if (!string.IsNullOrEmpty(cellContent))
{
Clipboard.SetText(cellContent);
// Mostrar notificación de que el valor fue copiado
ShowNotification("Valor copiado al portapapeles.");
}
}
}
private void ShowNotification(string message)
{
NotifyIcon notifyIcon = new NotifyIcon();
notifyIcon.Visible = true;
notifyIcon.Icon = SystemIcons.Information;
notifyIcon.BalloonTipTitle = "Notificación";
notifyIcon.BalloonTipText = message;
notifyIcon.ShowBalloonTip(2000);
// Ocultar el icono después de 2 segundos
var timer = new System.Timers.Timer(2000);
timer.Elapsed += (s, e) =>
{
notifyIcon.Dispose();
timer.Dispose();
};
timer.Start();
}
2024-06-15 14:33:52 -03:00
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
((App)Application.Current).SetInitialWindowPosition();
}
private void MainWindow_Wheel(object sender, MouseWheelEventArgs e)
{
if (DataContext is MView mv)
mv.OnWheel(sender, e);
}
2024-06-15 14:33:52 -03:00
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
this.DragMove();
}
else if (e.ChangedButton == MouseButton.Middle)
{
if (DataContext is MView mv && mv.SelectedPassword != null)
{
mv.UtilizarCommand.Execute(mv.SelectedPassword);
e.Handled = true; // Suprimir el evento
}
}
}
private void HookManager()
{
_globalHook = Hook.GlobalEvents();
_globalHook.MouseWheel += GlobalHookMouseWheel;
_globalHook.MouseDownExt += GlobalHookMouseDown;
}
private void GlobalHookMouseDown(object sender, MouseEventExtArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Middle)
{
var mouseButtonEventArgs = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Middle)
{
RoutedEvent = UIElement.MouseDownEvent
};
Window_MouseDown(sender, mouseButtonEventArgs);
}
}
private void GlobalHookMouseWheel(object sender, MouseEventArgs e)
{
// Convierte MouseEventArgs a MouseWheelEventArgs
var wheelArgs = new MouseWheelEventArgs(Mouse.PrimaryDevice, 0, e.Delta)
{
RoutedEvent = UIElement.MouseWheelEvent
};
MainWindow_Wheel(sender, wheelArgs);
}
protected override void OnClosed(EventArgs e)
{
_globalHook.MouseWheel -= GlobalHookMouseWheel;
_globalHook.MouseDownExt -= GlobalHookMouseDown;
_globalHook.Dispose();
base.OnClosed(e);
}
2024-06-15 14:33:52 -03:00
}
2024-06-15 06:24:07 -03:00
public partial class MView : ObservableObject
{
[ObservableProperty]
private ObservableCollection<Passwords> passwords;
[ObservableProperty]
private ObservableCollection<Passwords> top_passwords;
2024-06-15 06:24:07 -03:00
[ObservableProperty]
private Passwords selectedPassword;
Stopwatch timeSteps = new Stopwatch();
public void OnWheel(object sender, MouseWheelEventArgs e)
{
if (Passwords.Count == 0) return;
if (timeSteps.ElapsedMilliseconds > 200)
{
var index = Passwords.IndexOf(SelectedPassword);
var max = Passwords.Count;
if (e.Delta < 0)
index++;
else if (e.Delta > 0) index--;
if (index < 0) index = max - 1;
if (index >= max) index = 0;
SelectedPassword = Passwords[index];
timeSteps.Restart();
}
}
partial void OnSelectedPasswordChanged(Passwords value)
{
Top_passwords.Clear();
Top_passwords.Add(value);
}
2024-06-15 06:24:07 -03:00
public MView()
{
// Suscribirse al evento de cierre de la aplicación
Application.Current.Exit += OnApplicationExit;
top_passwords = new ObservableCollection<Passwords>();
2024-06-15 06:24:07 -03:00
passwords = new ObservableCollection<Passwords>(EstadoPersistente.Instance.Passwords);
SelectedPassword = passwords.FirstOrDefault();
timeSteps.Start();
2024-06-15 06:24:07 -03:00
}
private void OnApplicationExit(object sender, ExitEventArgs e)
{
EstadoPersistente.Instance.Passwords = new List<Passwords>(passwords);
2024-06-16 12:39:45 -03:00
EstadoPersistente.Instance.GuardarEstado(TiposEstadosPersistentes.Obsidean);
2024-06-15 06:24:07 -03:00
}
2024-06-15 14:33:52 -03:00
[RelayCommand]
2024-06-15 06:24:07 -03:00
private void Agregar()
{
Passwords newPassword = new Passwords { Usuario = "NuevoUsuario", Password = "NuevaContraseña" };
passwords.Add(newPassword);
SelectedPassword = newPassword;
}
2024-06-15 14:33:52 -03:00
[RelayCommand]
private void Eliminar()
2024-06-15 06:24:07 -03:00
{
if (SelectedPassword != null)
2024-06-15 06:24:07 -03:00
{
Passwords.Remove(SelectedPassword);
2024-06-15 06:24:07 -03:00
SelectedPassword = null;
}
}
2024-06-15 14:33:52 -03:00
[RelayCommand]
private void Utilizar()
2024-06-15 14:33:52 -03:00
{
if (SelectedPassword != null)
2024-06-15 14:33:52 -03:00
{
((App)Application.Current).PasteLogic.RestoreAndSimulatePaste(SelectedPassword);
2024-06-15 14:33:52 -03:00
// Cerrar la aplicación después de la acción
2024-06-15 14:33:52 -03:00
Application.Current.Shutdown();
}
}
[RelayCommand]
private void Cerrar()
{
// Cerrar la aplicación después de la acción
Application.Current.Shutdown();
}
2024-06-15 06:24:07 -03:00
}
}