From e5ea852c3bce6634b11927998eb7f82b647a892a Mon Sep 17 00:00:00 2001 From: BornToBeRoot <16019165+BornToBeRoot@users.noreply.github.com> Date: Sat, 8 Nov 2025 23:56:41 +0100 Subject: [PATCH 01/10] Feature: Migrate password dialogs to childwindow --- Source/NETworkManager/DialogHelper.cs | 36 +++++++++++++ .../ViewModels/ProfileFileViewModel.cs | 6 +-- .../ViewModels/SettingsProfilesViewModel.cs | 52 +++++++++---------- .../CredentialsChangePasswordDialog.xaml | 2 +- .../CredentialsChangePasswordDialog.xaml.cs | 11 ++-- ...ml => CredentialsPasswordChildWindow.xaml} | 21 ++++---- .../CredentialsPasswordChildWindow.xaml.cs | 21 ++++++++ .../Views/CredentialsPasswordDialog.xaml.cs | 16 ------ .../Views/ProfileFileChildWindow.xaml | 2 +- 9 files changed, 108 insertions(+), 59 deletions(-) create mode 100644 Source/NETworkManager/DialogHelper.cs rename Source/NETworkManager/Views/{CredentialsPasswordDialog.xaml => CredentialsPasswordChildWindow.xaml} (77%) create mode 100644 Source/NETworkManager/Views/CredentialsPasswordChildWindow.xaml.cs delete mode 100644 Source/NETworkManager/Views/CredentialsPasswordDialog.xaml.cs diff --git a/Source/NETworkManager/DialogHelper.cs b/Source/NETworkManager/DialogHelper.cs new file mode 100644 index 0000000000..83062f7dad --- /dev/null +++ b/Source/NETworkManager/DialogHelper.cs @@ -0,0 +1,36 @@ +using MahApps.Metro.SimpleChildWindow; +using NETworkManager.Localization.Resources; +using NETworkManager.Settings; +using NETworkManager.Utilities; +using NETworkManager.ViewModels; +using NETworkManager.Views; +using System.Threading.Tasks; +using System.Windows; + +namespace NETworkManager +{ + public static class DialogHelper + { + public static Task ShowOKMessageAsync(Window parentWindow, string title, string message, ChildWindowIcon icon = ChildWindowIcon.Info, string buttonOK = null) + { + if (string.IsNullOrEmpty(buttonOK)) + buttonOK = Strings.OK; + + var childWindow = new OKMessageChildWindow(); + + var childWindowViewModel = new OKMessageViewModel(_ => + { + childWindow.IsOpen = false; + ConfigurationManager.Current.IsChildWindowOpen = false; + }, message, buttonOK, icon); + + childWindow.Title = title; + + childWindow.DataContext = childWindowViewModel; + + ConfigurationManager.Current.IsChildWindowOpen = true; + + return parentWindow.ShowChildWindowAsync(childWindow); + } + } +} diff --git a/Source/NETworkManager/ViewModels/ProfileFileViewModel.cs b/Source/NETworkManager/ViewModels/ProfileFileViewModel.cs index 0474c56cab..f5a9c24ae3 100644 --- a/Source/NETworkManager/ViewModels/ProfileFileViewModel.cs +++ b/Source/NETworkManager/ViewModels/ProfileFileViewModel.cs @@ -11,10 +11,10 @@ public class ProfileFileViewModel : ViewModelBase private string _name; - public ProfileFileViewModel(Action addCommand, Action cancelHandler, + public ProfileFileViewModel(Action okCommand, Action cancelHandler, ProfileFileInfo info = null) { - AcceptCommand = new RelayCommand(_ => addCommand(this)); + OKCommand = new RelayCommand(_ => okCommand(this)); CancelCommand = new RelayCommand(_ => cancelHandler(this)); if (info == null) @@ -25,7 +25,7 @@ public ProfileFileViewModel(Action addCommand, Action - new RelayCommand(_ => DeleteProfileFileAction().ConfigureAwait(false), DeleteProfileFile_CanExecute); + new RelayCommand(async _ => await DeleteProfileFileAction().ConfigureAwait(false), DeleteProfileFile_CanExecute); private bool DeleteProfileFile_CanExecute(object obj) { @@ -361,18 +361,16 @@ await _dialogCoordinator.ShowMessageAsync(this, Strings.DecryptionError, await _dialogCoordinator.ShowMetroDialogAsync(this, customDialog); } - public ICommand DisableEncryptionCommand => new RelayCommand(_ => DisableEncryptionAction()); + public ICommand DisableEncryptionCommand => new RelayCommand(async _ => await DisableEncryptionAction().ConfigureAwait(false)); - private async void DisableEncryptionAction() + private Task DisableEncryptionAction() { - var customDialog = new CustomDialog - { - Title = Strings.MasterPassword - }; + var childWindow = new CredentialsPasswordChildWindow(); - var credentialsPasswordViewModel = new CredentialsPasswordViewModel(async instance => + var childWindowViewModel = new CredentialsPasswordViewModel(async instance => { - await _dialogCoordinator.HideMetroDialogAsync(this, customDialog); + childWindow.IsOpen = false; + ConfigurationManager.Current.IsChildWindowOpen = false; try { @@ -380,31 +378,33 @@ private async void DisableEncryptionAction() } catch (CryptographicException) { - var settings = AppearanceManager.MetroDialog; - settings.AffirmativeButtonText = Strings.OK; - - await _dialogCoordinator.ShowMessageAsync(this, Strings.WrongPassword, - Strings.WrongPasswordDecryptionFailedMessage, MessageDialogStyle.Affirmative, - settings); + await DialogHelper.ShowOKMessageAsync(Application.Current.MainWindow, + Strings.WrongPassword, + Strings.WrongPasswordDecryptionFailedMessage, + ChildWindowIcon.Error).ConfigureAwait(false); } catch (Exception ex) { - var settings = AppearanceManager.MetroDialog; - settings.AffirmativeButtonText = Strings.OK; - - await _dialogCoordinator.ShowMessageAsync(this, Strings.DecryptionError, + await DialogHelper.ShowOKMessageAsync(Application.Current.MainWindow, + Strings.DecryptionError, $"{Strings.DecryptionErrorMessage}\n\n{ex.Message}", - MessageDialogStyle.Affirmative, settings); + ChildWindowIcon.Error).ConfigureAwait(false); } - }, async _1 => { await _dialogCoordinator.HideMetroDialogAsync(this, customDialog); }); - customDialog.Content = new CredentialsPasswordDialog + }, _ => { - DataContext = credentialsPasswordViewModel - }; + childWindow.IsOpen = false; + ConfigurationManager.Current.IsChildWindowOpen = false; + }); - await _dialogCoordinator.ShowMetroDialogAsync(this, customDialog); - } + childWindow.Title = Strings.MasterPassword; + + childWindow.DataContext = childWindowViewModel; + + ConfigurationManager.Current.IsChildWindowOpen = true; + + return (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow); + } #endregion } \ No newline at end of file diff --git a/Source/NETworkManager/Views/CredentialsChangePasswordDialog.xaml b/Source/NETworkManager/Views/CredentialsChangePasswordDialog.xaml index 5e176c0cc4..57ab8188d1 100644 --- a/Source/NETworkManager/Views/CredentialsChangePasswordDialog.xaml +++ b/Source/NETworkManager/Views/CredentialsChangePasswordDialog.xaml @@ -7,7 +7,7 @@ xmlns:interactivity="http://schemas.microsoft.com/xaml/behaviors" xmlns:viewModels="clr-namespace:NETworkManager.ViewModels" xmlns:localization="clr-namespace:NETworkManager.Localization.Resources;assembly=NETworkManager.Localization" - mc:Ignorable="d" Loaded="UserControl_Loaded" + mc:Ignorable="d" Loaded="ChildWindow_OnLoaded" d:DataContext="{d:DesignInstance viewModels:CredentialsChangePasswordViewModel}"> diff --git a/Source/NETworkManager/Views/CredentialsChangePasswordDialog.xaml.cs b/Source/NETworkManager/Views/CredentialsChangePasswordDialog.xaml.cs index 088f45d549..f9bb4c192c 100644 --- a/Source/NETworkManager/Views/CredentialsChangePasswordDialog.xaml.cs +++ b/Source/NETworkManager/Views/CredentialsChangePasswordDialog.xaml.cs @@ -1,4 +1,6 @@ -using System.Windows; +using System; +using System.Windows; +using System.Windows.Threading; namespace NETworkManager.Views; @@ -9,8 +11,11 @@ public CredentialsChangePasswordDialog() InitializeComponent(); } - private void UserControl_Loaded(object sender, RoutedEventArgs e) + private void ChildWindow_OnLoaded(object sender, RoutedEventArgs e) { - PasswordBoxPassword.Focus(); + Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(delegate + { + PasswordBoxPassword.Focus(); + })); } } \ No newline at end of file diff --git a/Source/NETworkManager/Views/CredentialsPasswordDialog.xaml b/Source/NETworkManager/Views/CredentialsPasswordChildWindow.xaml similarity index 77% rename from Source/NETworkManager/Views/CredentialsPasswordDialog.xaml rename to Source/NETworkManager/Views/CredentialsPasswordChildWindow.xaml index 045a3e74b3..73f66d1797 100644 --- a/Source/NETworkManager/Views/CredentialsPasswordDialog.xaml +++ b/Source/NETworkManager/Views/CredentialsPasswordChildWindow.xaml @@ -1,4 +1,4 @@ - - + xmlns:simpleChildWindow="clr-namespace:MahApps.Metro.SimpleChildWindow;assembly=MahApps.Metro.SimpleChildWindow" + CloseButtonCommand="{Binding Path=CancelCommand}" + Style="{StaticResource DefaultChildWindow}" + Loaded="ChildWindow_OnLoaded" + mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:CredentialsPasswordViewModel}"> + @@ -30,16 +33,16 @@ + Style="{StaticResource DefaultPasswordBox}"> + Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />