Skip to content

Commit c0d4c62

Browse files
committed
Feature: Migrate password dialogs to childwindow / code cleanup
1 parent e5ea852 commit c0d4c62

17 files changed

+128
-85
lines changed

Source/NETworkManager/DialogHelper.cs

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,34 @@
99

1010
namespace NETworkManager
1111
{
12+
/// <summary>
13+
/// Helper class for showing dialog messages.
14+
/// </summary>
1215
public static class DialogHelper
1316
{
14-
public static Task ShowOKMessageAsync(Window parentWindow, string title, string message, ChildWindowIcon icon = ChildWindowIcon.Info, string buttonOK = null)
17+
/// <summary>
18+
/// Displays a modal message dialog with an OK button, allowing the user to acknowledge the message before
19+
/// continuing.
20+
/// </summary>
21+
/// <remarks>The dialog is shown as a child window of the specified parent. The method is
22+
/// asynchronous and returns when the dialog is dismissed by the user.</remarks>
23+
/// <param name="parentWindow">The parent window that will host the message dialog. Cannot be null.</param>
24+
/// <param name="title">The title text displayed in the message dialog window. Cannot be null.</param>
25+
/// <param name="message">The main message content shown in the dialog. Cannot be null.</param>
26+
/// <param name="icon">The icon to display in the dialog, indicating the message type. Defaults to Info if not specified.</param>
27+
/// <param name="buttonOKText">The text to display on the OK button. If null, a default value is used.</param>
28+
/// <returns>A task that completes when the user closes the message dialog.</returns>
29+
public static Task ShowOKMessageAsync(Window parentWindow, string title, string message, ChildWindowIcon icon = ChildWindowIcon.Info, string buttonOKText = null)
1530
{
16-
if (string.IsNullOrEmpty(buttonOK))
17-
buttonOK = Strings.OK;
31+
buttonOKText ??= Strings.OK;
1832

1933
var childWindow = new OKMessageChildWindow();
2034

2135
var childWindowViewModel = new OKMessageViewModel(_ =>
2236
{
2337
childWindow.IsOpen = false;
2438
ConfigurationManager.Current.IsChildWindowOpen = false;
25-
}, message, buttonOK, icon);
39+
}, message, icon, buttonOKText);
2640

2741
childWindow.Title = title;
2842

@@ -32,5 +46,53 @@ public static Task ShowOKMessageAsync(Window parentWindow, string title, string
3246

3347
return parentWindow.ShowChildWindowAsync(childWindow);
3448
}
49+
50+
/// <summary>
51+
/// Displays an asynchronous modal dialog with OK and Cancel buttons, allowing the user to confirm or cancel an
52+
/// action.
53+
/// </summary>
54+
/// <remarks>The dialog is shown as a child window of the specified parent. The method does not
55+
/// return until the user closes the dialog. Custom button text and icon can be provided to tailor the dialog to
56+
/// specific scenarios.</remarks>
57+
/// <param name="parentWindow">The parent window that hosts the child dialog. Cannot be null.</param>
58+
/// <param name="title">The title text displayed in the dialog window.</param>
59+
/// <param name="message">The message content shown to the user in the dialog.</param>
60+
/// <param name="icon">The icon displayed in the dialog to indicate the message type. Defaults to Info.</param>
61+
/// <param name="buttonOKText">The text label for the OK button. If null, a default value is used.</param>
62+
/// <param name="buttonCancelText">The text label for the Cancel button. If null, a default value is used.</param>
63+
/// <returns>A task that represents the asynchronous operation. The task result is <see langword="true"/> if the user
64+
/// clicks OK; otherwise, <see langword="false"/>.</returns>
65+
public static async Task<bool> ShowOKCancelMessageAsync(Window parentWindow, string title, string message, ChildWindowIcon icon = ChildWindowIcon.Info, string buttonOKText = null, string buttonCancelText = null)
66+
{
67+
buttonOKText ??= Strings.OK;
68+
buttonCancelText ??= Strings.Cancel;
69+
70+
var result = false;
71+
72+
var childWindow = new OKCancelMessageChildWindow();
73+
74+
var childWindowViewModel = new OKCancelMessageViewModel(_ =>
75+
{
76+
childWindow.IsOpen = false;
77+
ConfigurationManager.Current.IsChildWindowOpen = false;
78+
79+
result = true;
80+
},
81+
_ =>
82+
{
83+
childWindow.IsOpen = false;
84+
ConfigurationManager.Current.IsChildWindowOpen = false;
85+
},
86+
message, icon, buttonOKText, buttonCancelText);
87+
88+
childWindow.Title = title;
89+
childWindow.DataContext = childWindowViewModel;
90+
91+
ConfigurationManager.Current.IsChildWindowOpen = true;
92+
93+
await parentWindow.ShowChildWindowAsync(childWindow);
94+
95+
return result;
96+
}
3597
}
3698
}

Source/NETworkManager/NETworkManager.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
<XamlRuntime>Wpf</XamlRuntime>
142142
<SubType>Designer</SubType>
143143
</Page>
144-
<Page Update="Views\OKCancelInfoMessageChildWindow.xaml">
144+
<Page Update="Views\OKCancelMessageChildWindow.xaml">
145145
<Generator>MSBuild:Compile</Generator>
146146
<XamlRuntime>Wpf</XamlRuntime>
147147
<SubType>Designer</SubType>

Source/NETworkManager/ProfileDialogManager.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NETworkManager.Models.PuTTY;
77
using NETworkManager.Models.RemoteDesktop;
88
using NETworkManager.Profiles;
9+
using NETworkManager.Utilities;
910
using NETworkManager.ViewModels;
1011
using NETworkManager.Views;
1112
using System;
@@ -563,9 +564,9 @@ public static Task ShowCopyAsProfileDialog(Window parentWindow, IProfileManagerM
563564
public static Task ShowDeleteProfileDialog(Window parentWindow, IProfileManagerMinimal viewModel,
564565
IList<ProfileInfo> profiles)
565566
{
566-
var childWindow = new OKCancelInfoMessageChildWindow();
567+
var childWindow = new OKCancelMessageChildWindow();
567568

568-
OKCancelInfoMessageViewModel childWindowViewModel = new(_ =>
569+
OKCancelMessageViewModel childWindowViewModel = new(_ =>
569570
{
570571
childWindow.IsOpen = false;
571572
Settings.ConfigurationManager.Current.IsChildWindowOpen = false;
@@ -581,6 +582,7 @@ public static Task ShowDeleteProfileDialog(Window parentWindow, IProfileManagerM
581582
viewModel.OnProfileManagerDialogClose();
582583
},
583584
profiles.Count == 1 ? Strings.DeleteProfileMessage : Strings.DeleteProfilesMessage,
585+
Utilities.ChildWindowIcon.Info,
584586
Strings.Delete
585587
);
586588

@@ -665,9 +667,9 @@ public static Task ShowEditGroupDialog(Window parentWindow, IProfileManagerMinim
665667
public static Task ShowDeleteGroupDialog(Window parentWindow, IProfileManagerMinimal viewModel,
666668
GroupInfo group)
667669
{
668-
var childWindow = new OKCancelInfoMessageChildWindow();
670+
var childWindow = new OKCancelMessageChildWindow();
669671

670-
OKCancelInfoMessageViewModel childWindowViewModel = new(_ =>
672+
OKCancelMessageViewModel childWindowViewModel = new(_ =>
671673
{
672674
childWindow.IsOpen = false;
673675
Settings.ConfigurationManager.Current.IsChildWindowOpen = false;
@@ -683,6 +685,7 @@ public static Task ShowDeleteGroupDialog(Window parentWindow, IProfileManagerMin
683685
viewModel.OnProfileManagerDialogClose();
684686
},
685687
Strings.DeleteGroupMessage,
688+
ChildWindowIcon.Info,
686689
Strings.Delete
687690
);
688691

Source/NETworkManager/ViewModels/AWSSessionManagerSettingsViewModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,9 @@ public async Task EditAWSProfile()
277277

278278
private Task DeleteAWSProfile()
279279
{
280-
var childWindow = new OKCancelInfoMessageChildWindow();
280+
var childWindow = new OKCancelMessageChildWindow();
281281

282-
var childWindowViewModel = new OKCancelInfoMessageViewModel(_ =>
282+
var childWindowViewModel = new OKCancelMessageViewModel(_ =>
283283
{
284284
childWindow.IsOpen = false;
285285
ConfigurationManager.Current.IsChildWindowOpen = false;
@@ -291,6 +291,7 @@ private Task DeleteAWSProfile()
291291
ConfigurationManager.Current.IsChildWindowOpen = false;
292292
},
293293
Strings.DeleteAWSProfileMessage,
294+
ChildWindowIcon.Info,
294295
Strings.Delete
295296
);
296297

Source/NETworkManager/ViewModels/DNSLookupSettingsViewModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ public async Task EditDNSServer()
359359

360360
private Task DeleteDNSServer()
361361
{
362-
var childWindow = new OKCancelInfoMessageChildWindow();
362+
var childWindow = new OKCancelMessageChildWindow();
363363

364-
var childWindowViewModel = new OKCancelInfoMessageViewModel(_ =>
364+
var childWindowViewModel = new OKCancelMessageViewModel(_ =>
365365
{
366366
childWindow.IsOpen = false;
367367
ConfigurationManager.Current.IsChildWindowOpen = false;
@@ -373,6 +373,7 @@ private Task DeleteDNSServer()
373373
ConfigurationManager.Current.IsChildWindowOpen = false;
374374
},
375375
Strings.DeleteDNSServerMessage,
376+
ChildWindowIcon.Info,
376377
Strings.Delete
377378
);
378379

Source/NETworkManager/ViewModels/HostsFileEditorViewModel.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,9 @@ private async Task DeleteEntryAction()
391391
{
392392
IsModifying = true;
393393

394-
var childWindow = new OKCancelInfoMessageChildWindow();
394+
var childWindow = new OKCancelMessageChildWindow();
395395

396-
var childWindowViewModel = new OKCancelInfoMessageViewModel(async _ =>
396+
var childWindowViewModel = new OKCancelMessageViewModel(async _ =>
397397
{
398398
childWindow.IsOpen = false;
399399
ConfigurationManager.Current.IsChildWindowOpen = false;
@@ -413,6 +413,7 @@ private async Task DeleteEntryAction()
413413
},
414414
string.Format(Strings.DeleteHostsFileEntryMessage, SelectedResult.IPAddress, SelectedResult.Hostname,
415415
string.IsNullOrEmpty(SelectedResult.Comment) ? "" : $"# {SelectedResult.Comment}"),
416+
ChildWindowIcon.Info,
416417
Strings.Delete
417418
);
418419

@@ -452,7 +453,7 @@ private async Task ShowErrorMessageAsync(HostsFileEntryModifyResult result)
452453
{
453454
childWindow.IsOpen = false;
454455
ConfigurationManager.Current.IsChildWindowOpen = false;
455-
}, message, Strings.OK, ChildWindowIcon.Error);
456+
}, message, ChildWindowIcon.Error, Strings.OK);
456457

457458
childWindow.Title = Strings.Error;
458459

@@ -479,7 +480,7 @@ private async Task RestartAsAdminAction()
479480
{
480481
childWindow.IsOpen = false;
481482
ConfigurationManager.Current.IsChildWindowOpen = false;
482-
}, ex.Message, Strings.OK, ChildWindowIcon.Error);
483+
}, ex.Message, ChildWindowIcon.Error, Strings.OK);
483484

484485
childWindow.Title = Strings.Error;
485486

Source/NETworkManager/ViewModels/IPScannerSettingsViewModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ public async void EditCustomCommand()
387387

388388
private Task DeleteCustomCommand()
389389
{
390-
var childWindow = new OKCancelInfoMessageChildWindow();
390+
var childWindow = new OKCancelMessageChildWindow();
391391

392-
var childWindowViewModel = new OKCancelInfoMessageViewModel(_ =>
392+
var childWindowViewModel = new OKCancelMessageViewModel(_ =>
393393
{
394394
childWindow.IsOpen = false;
395395
ConfigurationManager.Current.IsChildWindowOpen = false;
@@ -401,6 +401,7 @@ private Task DeleteCustomCommand()
401401
ConfigurationManager.Current.IsChildWindowOpen = false;
402402
},
403403
Strings.DeleteCustomCommandMessage,
404+
ChildWindowIcon.Info,
404405
Strings.Delete
405406
);
406407

Source/NETworkManager/ViewModels/OKCancelInfoMessageViewModel.cs renamed to Source/NETworkManager/ViewModels/OKCancelMessageViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace NETworkManager.ViewModels;
66

7-
public class OKCancelInfoMessageViewModel : ViewModelBase
7+
public class OKCancelMessageViewModel : ViewModelBase
88
{
9-
public OKCancelInfoMessageViewModel(Action<OKCancelInfoMessageViewModel> okCommand,
10-
Action<OKCancelInfoMessageViewModel> cancelHandler, string message, string okButtonText = null, string cancelButtonText = null, ChildWindowIcon icon = ChildWindowIcon.Info)
9+
public OKCancelMessageViewModel(Action<OKCancelMessageViewModel> okCommand,
10+
Action<OKCancelMessageViewModel> cancelHandler, string message, ChildWindowIcon icon = ChildWindowIcon.Info, string okButtonText = null, string cancelButtonText = null)
1111
{
1212
OKCommand = new RelayCommand(_ => okCommand(this));
1313
CancelCommand = new RelayCommand(_ => cancelHandler(this));

Source/NETworkManager/ViewModels/OKMessageViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace NETworkManager.ViewModels;
66

77
public class OKMessageViewModel : ViewModelBase
88
{
9-
public OKMessageViewModel(Action<OKMessageViewModel> okCommand, string message, string okButtonText = null, ChildWindowIcon icon = ChildWindowIcon.Info)
9+
public OKMessageViewModel(Action<OKMessageViewModel> okCommand, string message, ChildWindowIcon icon = ChildWindowIcon.Info, string okButtonText = null)
1010
{
1111
OKCommand = new RelayCommand(_ => okCommand(this));
1212

Source/NETworkManager/ViewModels/PortScannerSettingsViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ public async Task EditPortProfile()
234234

235235
private Task DeletePortProfile()
236236
{
237-
var childWindow = new OKCancelInfoMessageChildWindow();
237+
var childWindow = new OKCancelMessageChildWindow();
238238

239-
var childWindowViewModel = new OKCancelInfoMessageViewModel(_ =>
239+
var childWindowViewModel = new OKCancelMessageViewModel(_ =>
240240
{
241241
childWindow.IsOpen = false;
242242
ConfigurationManager.Current.IsChildWindowOpen = false;
@@ -247,7 +247,7 @@ private Task DeletePortProfile()
247247
childWindow.IsOpen = false;
248248
ConfigurationManager.Current.IsChildWindowOpen = false;
249249
},
250-
Strings.DeletePortProfileMessage, Strings.Delete);
250+
Strings.DeletePortProfileMessage, ChildWindowIcon.Info, Strings.Delete);
251251

252252
childWindow.Title = Strings.DeletePortProfile;
253253

0 commit comments

Comments
 (0)