Cryptura/Cryptura/MainWindow.axaml.cs
WeeXnes 2156aff43c
All checks were successful
Java CI / test (push) Successful in 1m23s
Fixed some styling
2025-05-06 20:49:41 +02:00

157 lines
No EOL
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
namespace Cryptura;
public partial class MainWindow : Window
{
public static bool censorPasswords = true;
public static string MasterPassword = "";
private void AdjustThemeToPlatform()
{
if (Core.IsRunningOnGnome())
{
this.TransparencyLevelHint = new[] { WindowTransparencyLevel.None };
this.Background = new SolidColorBrush(Color.Parse(ColorScheme.Surface.Color_1));
this.ContentBorder.Background = new SolidColorBrush(Color.Parse(ColorScheme.Surface.Color_2));
this.AddPasswordBorder.Background = new SolidColorBrush(Color.Parse(ColorScheme.Surface.Color_2));
this.MiddleSeperator.IsVisible = true;
this.AcrylicBorderObject.IsVisible = false;
}
else
{
this.TransparencyLevelHint = new[] { WindowTransparencyLevel.AcrylicBlur };
this.Background = Brushes.Transparent;
this.AcrylicBorderObject.IsVisible = true;
}
}
public MainWindow(string masterPassword)
{
InitializeComponent();
MasterPassword = masterPassword;
AdjustThemeToPlatform();
FetchPasswords();
}
private void FetchPasswords(){
List<Password> passwords = new List<Password>();
string[] WXFiles = Directory.GetFiles(Globals.GetKeyDirectory());
foreach (string file in WXFiles)
{
if (Path.GetExtension(file).ToLower() == ".wx")
{
Password pw = new Password(file);
if(!String.IsNullOrEmpty(pw.Value))
passwords.Add(pw);
}
else
{
Console.WriteLine("Skipped non-key file: " + file);
}
}
PasswordList.ItemsSource = passwords;
}
private void Control_OnLoaded(object? sender, RoutedEventArgs e)
{
if (sender is TextBox textBox)
{
Console.WriteLine("called");
textBox.Bind(TextBlock.TextProperty, new Binding("Value"));
}
}
private void PasswordList_OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
{
if (PasswordList.SelectedItem is Password password) Core.SetClipboardText(password.Value, this);
}
private void CreatePassword_OnClick(object? sender, RoutedEventArgs e)
{
AddPasswordContainer.IsVisible = true;
}
private void DeletePassword_OnClick(object? sender, RoutedEventArgs e)
{
if (PasswordList.SelectedItem is Password password)
{
File.Delete(password.WxFile.Path);
FetchPasswords();
}
}
private void ToggleHide_OnClick(object? sender, RoutedEventArgs e)
{
censorPasswords = !censorPasswords;
FetchPasswords();
}
private void ErrorMessage(string message)
{
ErrorTextBlock.Text = message;
ErrorTextBlock.IsVisible = true;
}
private void SavePassword()
{
if (String.IsNullOrEmpty(PasswordName.Text))
{
ErrorMessage("Name cannot be empty");
return;
}
if(String.IsNullOrEmpty(PasswordValue.Text))
{
ErrorMessage("Password cannot be empty");
return;
}
if (PasswordValue.Text != PasswordRepeat.Text)
{
ErrorMessage("Password doesn't match");
return;
}
Password newPassword = new Password(PasswordName.Text, PasswordValue.Text);
FetchPasswords();
ResetPasswordModal();
}
private void ResetPasswordModal()
{
AddPasswordContainer.IsVisible = false;
PasswordName.Text = "";
PasswordValue.Text = "";
PasswordRepeat.Text = "";
ErrorTextBlock.IsVisible = false;
}
private void PasswordSave_OnClick(object? sender, RoutedEventArgs e)
{
SavePassword();
}
private void DialogKeyDown(object? sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
SavePassword();
}
}
private void OuterBorder_OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
ResetPasswordModal();
}
private void InnerBorder_OnPointerPressed(object sender, PointerPressedEventArgs e)
{
e.Handled = true;
}
}