Skip to content

Commit e5ea852

Browse files
committed
Feature: Migrate password dialogs to childwindow
1 parent 16e3415 commit e5ea852

9 files changed

+108
-59
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using MahApps.Metro.SimpleChildWindow;
2+
using NETworkManager.Localization.Resources;
3+
using NETworkManager.Settings;
4+
using NETworkManager.Utilities;
5+
using NETworkManager.ViewModels;
6+
using NETworkManager.Views;
7+
using System.Threading.Tasks;
8+
using System.Windows;
9+
10+
namespace NETworkManager
11+
{
12+
public static class DialogHelper
13+
{
14+
public static Task ShowOKMessageAsync(Window parentWindow, string title, string message, ChildWindowIcon icon = ChildWindowIcon.Info, string buttonOK = null)
15+
{
16+
if (string.IsNullOrEmpty(buttonOK))
17+
buttonOK = Strings.OK;
18+
19+
var childWindow = new OKMessageChildWindow();
20+
21+
var childWindowViewModel = new OKMessageViewModel(_ =>
22+
{
23+
childWindow.IsOpen = false;
24+
ConfigurationManager.Current.IsChildWindowOpen = false;
25+
}, message, buttonOK, icon);
26+
27+
childWindow.Title = title;
28+
29+
childWindow.DataContext = childWindowViewModel;
30+
31+
ConfigurationManager.Current.IsChildWindowOpen = true;
32+
33+
return parentWindow.ShowChildWindowAsync(childWindow);
34+
}
35+
}
36+
}

Source/NETworkManager/ViewModels/ProfileFileViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public class ProfileFileViewModel : ViewModelBase
1111

1212
private string _name;
1313

14-
public ProfileFileViewModel(Action<ProfileFileViewModel> addCommand, Action<ProfileFileViewModel> cancelHandler,
14+
public ProfileFileViewModel(Action<ProfileFileViewModel> okCommand, Action<ProfileFileViewModel> cancelHandler,
1515
ProfileFileInfo info = null)
1616
{
17-
AcceptCommand = new RelayCommand(_ => addCommand(this));
17+
OKCommand = new RelayCommand(_ => okCommand(this));
1818
CancelCommand = new RelayCommand(_ => cancelHandler(this));
1919

2020
if (info == null)
@@ -25,7 +25,7 @@ public ProfileFileViewModel(Action<ProfileFileViewModel> addCommand, Action<Prof
2525
IsEdit = true;
2626
}
2727

28-
public ICommand AcceptCommand { get; }
28+
public ICommand OKCommand { get; }
2929

3030
public ICommand CancelCommand { get; }
3131

Source/NETworkManager/ViewModels/SettingsProfilesViewModel.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private async Task EditProfileFileAction()
213213
}
214214

215215
public ICommand DeleteProfileFileCommand =>
216-
new RelayCommand(_ => DeleteProfileFileAction().ConfigureAwait(false), DeleteProfileFile_CanExecute);
216+
new RelayCommand(async _ => await DeleteProfileFileAction().ConfigureAwait(false), DeleteProfileFile_CanExecute);
217217

218218
private bool DeleteProfileFile_CanExecute(object obj)
219219
{
@@ -361,50 +361,50 @@ await _dialogCoordinator.ShowMessageAsync(this, Strings.DecryptionError,
361361
await _dialogCoordinator.ShowMetroDialogAsync(this, customDialog);
362362
}
363363

364-
public ICommand DisableEncryptionCommand => new RelayCommand(_ => DisableEncryptionAction());
364+
public ICommand DisableEncryptionCommand => new RelayCommand(async _ => await DisableEncryptionAction().ConfigureAwait(false));
365365

366-
private async void DisableEncryptionAction()
366+
private Task DisableEncryptionAction()
367367
{
368-
var customDialog = new CustomDialog
369-
{
370-
Title = Strings.MasterPassword
371-
};
368+
var childWindow = new CredentialsPasswordChildWindow();
372369

373-
var credentialsPasswordViewModel = new CredentialsPasswordViewModel(async instance =>
370+
var childWindowViewModel = new CredentialsPasswordViewModel(async instance =>
374371
{
375-
await _dialogCoordinator.HideMetroDialogAsync(this, customDialog);
372+
childWindow.IsOpen = false;
373+
ConfigurationManager.Current.IsChildWindowOpen = false;
376374

377375
try
378376
{
379377
ProfileManager.DisableEncryption(SelectedProfileFile, instance.Password);
380378
}
381379
catch (CryptographicException)
382380
{
383-
var settings = AppearanceManager.MetroDialog;
384-
settings.AffirmativeButtonText = Strings.OK;
385-
386-
await _dialogCoordinator.ShowMessageAsync(this, Strings.WrongPassword,
387-
Strings.WrongPasswordDecryptionFailedMessage, MessageDialogStyle.Affirmative,
388-
settings);
381+
await DialogHelper.ShowOKMessageAsync(Application.Current.MainWindow,
382+
Strings.WrongPassword,
383+
Strings.WrongPasswordDecryptionFailedMessage,
384+
ChildWindowIcon.Error).ConfigureAwait(false);
389385
}
390386
catch (Exception ex)
391387
{
392-
var settings = AppearanceManager.MetroDialog;
393-
settings.AffirmativeButtonText = Strings.OK;
394-
395-
await _dialogCoordinator.ShowMessageAsync(this, Strings.DecryptionError,
388+
await DialogHelper.ShowOKMessageAsync(Application.Current.MainWindow,
389+
Strings.DecryptionError,
396390
$"{Strings.DecryptionErrorMessage}\n\n{ex.Message}",
397-
MessageDialogStyle.Affirmative, settings);
391+
ChildWindowIcon.Error).ConfigureAwait(false);
398392
}
399-
}, async _1 => { await _dialogCoordinator.HideMetroDialogAsync(this, customDialog); });
400393

401-
customDialog.Content = new CredentialsPasswordDialog
394+
}, _ =>
402395
{
403-
DataContext = credentialsPasswordViewModel
404-
};
396+
childWindow.IsOpen = false;
397+
ConfigurationManager.Current.IsChildWindowOpen = false;
398+
});
405399

406-
await _dialogCoordinator.ShowMetroDialogAsync(this, customDialog);
407-
}
400+
childWindow.Title = Strings.MasterPassword;
401+
402+
childWindow.DataContext = childWindowViewModel;
403+
404+
ConfigurationManager.Current.IsChildWindowOpen = true;
405+
406+
return (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
407+
}
408408

409409
#endregion
410410
}

Source/NETworkManager/Views/CredentialsChangePasswordDialog.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
xmlns:interactivity="http://schemas.microsoft.com/xaml/behaviors"
88
xmlns:viewModels="clr-namespace:NETworkManager.ViewModels"
99
xmlns:localization="clr-namespace:NETworkManager.Localization.Resources;assembly=NETworkManager.Localization"
10-
mc:Ignorable="d" Loaded="UserControl_Loaded"
10+
mc:Ignorable="d" Loaded="ChildWindow_OnLoaded"
1111
d:DataContext="{d:DesignInstance viewModels:CredentialsChangePasswordViewModel}">
1212
<Grid Margin="0,20">
1313
<Grid.RowDefinitions>
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Windows;
1+
using System;
2+
using System.Windows;
3+
using System.Windows.Threading;
24

35
namespace NETworkManager.Views;
46

@@ -9,8 +11,11 @@ public CredentialsChangePasswordDialog()
911
InitializeComponent();
1012
}
1113

12-
private void UserControl_Loaded(object sender, RoutedEventArgs e)
14+
private void ChildWindow_OnLoaded(object sender, RoutedEventArgs e)
1315
{
14-
PasswordBoxPassword.Focus();
16+
Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(delegate
17+
{
18+
PasswordBoxPassword.Focus();
19+
}));
1520
}
1621
}

Source/NETworkManager/Views/CredentialsPasswordDialog.xaml renamed to Source/NETworkManager/Views/CredentialsPasswordChildWindow.xaml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<UserControl x:Class="NETworkManager.Views.CredentialsPasswordDialog"
1+
<simpleChildWindow:ChildWindow x:Class="NETworkManager.Views.CredentialsPasswordChildWindow"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -7,9 +7,12 @@
77
xmlns:interactivity="http://schemas.microsoft.com/xaml/behaviors"
88
xmlns:viewModels="clr-namespace:NETworkManager.ViewModels"
99
xmlns:localization="clr-namespace:NETworkManager.Localization.Resources;assembly=NETworkManager.Localization"
10-
mc:Ignorable="d" Loaded="UserControl_Loaded"
11-
d:DataContext="{d:DesignInstance viewModels:CredentialsPasswordViewModel}">
12-
<Grid Margin="0,20">
10+
xmlns:simpleChildWindow="clr-namespace:MahApps.Metro.SimpleChildWindow;assembly=MahApps.Metro.SimpleChildWindow"
11+
CloseButtonCommand="{Binding Path=CancelCommand}"
12+
Style="{StaticResource DefaultChildWindow}"
13+
Loaded="ChildWindow_OnLoaded"
14+
mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:CredentialsPasswordViewModel}">
15+
<Grid Margin="10">
1316
<Grid.RowDefinitions>
1417
<RowDefinition Height="*" />
1518
<RowDefinition Height="10" />
@@ -30,16 +33,16 @@
3033
</Grid.ColumnDefinitions>
3134
<TextBlock Grid.Column="0" Grid.Row="0" Text="{x:Static localization:Strings.Password}" />
3235
<PasswordBox x:Name="PasswordBoxPassword" Grid.Column="2" Grid.Row="0"
33-
Style="{StaticResource DefaultPasswordBox}">
36+
Style="{StaticResource DefaultPasswordBox}">
3437
<interactivity:Interaction.Behaviors>
3538
<wpfHelpers:PasswordBoxBindingBehavior
36-
Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
39+
Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
3740
</interactivity:Interaction.Behaviors>
3841
</PasswordBox>
3942
</Grid>
4043
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right">
4144
<Button Content="{x:Static localization:Strings.OK}" Command="{Binding OKCommand}" IsDefault="True"
42-
Margin="0,0,10,0">
45+
Margin="0,0,10,0">
4346
<Button.Style>
4447
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource HighlightedButton}">
4548
<Setter Property="IsEnabled" Value="False" />
@@ -52,7 +55,7 @@
5255
</Button.Style>
5356
</Button>
5457
<Button Content="{x:Static localization:Strings.Cancel}" Command="{Binding CancelCommand}" IsCancel="True"
55-
Style="{StaticResource DefaultButton}" />
58+
Style="{StaticResource DefaultButton}" />
5659
</StackPanel>
5760
</Grid>
58-
</UserControl>
61+
</simpleChildWindow:ChildWindow>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Windows;
3+
using System.Windows.Threading;
4+
5+
namespace NETworkManager.Views;
6+
7+
public partial class CredentialsPasswordChildWindow
8+
{
9+
public CredentialsPasswordChildWindow()
10+
{
11+
InitializeComponent();
12+
}
13+
14+
private void ChildWindow_OnLoaded(object sender, RoutedEventArgs e)
15+
{
16+
Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(delegate
17+
{
18+
PasswordBoxPassword.Focus();
19+
}));
20+
}
21+
}

Source/NETworkManager/Views/CredentialsPasswordDialog.xaml.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

Source/NETworkManager/Views/ProfileFileChildWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
</TextBox>
4848
</Grid>
4949
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right">
50-
<Button Command="{Binding AcceptCommand}" IsDefault="True" Margin="0,0,10,0">
50+
<Button Command="{Binding OKCommand}" IsDefault="True" Margin="0,0,10,0">
5151
<Button.Style>
5252
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource HighlightedButton}">
5353
<Setter Property="IsEnabled" Value="False" />

0 commit comments

Comments
 (0)