Skip to content

Commit a522034

Browse files
committed
Improve dialog handling
1 parent f1df43d commit a522034

File tree

8 files changed

+88
-56
lines changed

8 files changed

+88
-56
lines changed

WslToolbox.Gui/Collections/Dialogs/ImportDistributionDialogCollection.cs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Windows;
88
using System.Windows.Controls;
99
using System.Windows.Data;
10+
using WslToolbox.Core;
1011
using WslToolbox.Gui.Handlers;
1112
using WslToolbox.Gui.Helpers.Ui;
1213
using WslToolbox.Gui.Validators;
@@ -86,7 +87,7 @@ public IEnumerable<Control> Items(MainViewModel viewModel)
8687
{
8788
var createFolder = viewModel.Config.Configuration.ImportCreateFolder;
8889
var userBasePath = viewModel.Config.Configuration.UserBasePath;
89-
var distributionFileBrowse = new Button {Content = "Browse..."};
90+
var distributionFileBrowse = new Button {Content = "Browse...", TabIndex = 0};
9091
var distributionBasePathBrowse = new Button {Content = "Browse..."};
9192

9293
SelectedBasePath = Directory.Exists(userBasePath) ? userBasePath : null;
@@ -98,19 +99,6 @@ public IEnumerable<Control> Items(MainViewModel viewModel)
9899

99100
Control[] items =
100101
{
101-
new Label {Content = "Name:", Margin = new Thickness(0, 0, 0, 2), FontWeight = FontWeights.Bold},
102-
new Label
103-
{
104-
Content = "- Only alphanumeric characters are allowed.\n" +
105-
"- Name must contain at least 3 characters.",
106-
Margin = new Thickness(0, 0, 0, 10)
107-
},
108-
ElementHelper.TextBox(nameof(DistributionName), bind: "DistributionName", width: 400,
109-
source: this,
110-
isReadonly: false, isEnabled: true, updateSourceTrigger: UpdateSourceTrigger.PropertyChanged,
111-
placeholder: "Name your distribution"),
112-
ElementHelper.Separator(),
113-
114102
new Label {Content = "Filename:", Margin = new Thickness(0, 0, 0, 2), FontWeight = FontWeights.Bold},
115103
ElementHelper.TextBox(nameof(SelectedFilePath),
116104
null, "SelectedFilePath", this, width: 400,
@@ -126,7 +114,19 @@ public IEnumerable<Control> Items(MainViewModel viewModel)
126114
updateSourceTrigger: UpdateSourceTrigger.PropertyChanged,
127115
placeholder: "Select an installation directory"),
128116
ElementHelper.Separator(0),
129-
distributionBasePathBrowse
117+
distributionBasePathBrowse,
118+
new Label {Content = "Name:", Margin = new Thickness(0, 0, 0, 2), FontWeight = FontWeights.Bold},
119+
new Label
120+
{
121+
Content = "- Only alphanumeric characters are allowed.\n" +
122+
"- Name must contain at least 3 characters.",
123+
Margin = new Thickness(0, 0, 0, 10)
124+
},
125+
ElementHelper.TextBox(nameof(DistributionName), bind: "DistributionName", width: 400,
126+
source: this,
127+
isReadonly: false, isEnabled: true, updateSourceTrigger: UpdateSourceTrigger.PropertyChanged,
128+
placeholder: "Name your distribution"),
129+
ElementHelper.Separator()
130130
};
131131

132132
return items;
@@ -171,6 +171,24 @@ private void OnPropertyChanged([CallerMemberName] string propertyName = null)
171171
{
172172
Debug.WriteLine(e);
173173
}
174+
175+
if (propertyName == nameof(SelectedFilePath) && SelectedFilePath != null)
176+
OnSelectedFilePathChanged();
177+
}
178+
179+
private void OnSelectedFilePathChanged()
180+
{
181+
var fileName = Path.GetFileNameWithoutExtension(SelectedFilePath);
182+
var distroNameExists = ToolboxClass.DistributionByName(fileName) != null;
183+
var distroNum = 0;
184+
185+
while (distroNameExists)
186+
{
187+
distroNum++;
188+
distroNameExists = ToolboxClass.DistributionByName($"{fileName}{distroNum}") != null;
189+
}
190+
191+
DistributionName = Path.GetFileNameWithoutExtension($"{fileName}{distroNum}");
174192
}
175193
}
176194
}

WslToolbox.Gui/Collections/Dialogs/RenameDistributionDialogCollection.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
using System.Windows;
55
using System.Windows.Controls;
66
using WslToolbox.Core;
7+
using WslToolbox.Gui.Helpers;
78
using WslToolbox.Gui.Validators;
89

910
namespace WslToolbox.Gui.Collections.Dialogs
1011
{
1112
public class RenameDistributionDialogCollection : INotifyPropertyChanged
1213
{
14+
private string _distributionName;
1315
private bool _distributionNameIsValid;
1416

1517
public bool DistributionNameIsValid
@@ -23,12 +25,26 @@ public bool DistributionNameIsValid
2325
}
2426
}
2527

28+
public string DistributionName
29+
{
30+
get => _distributionName;
31+
set
32+
{
33+
if (value == _distributionName) return;
34+
_distributionName = value;
35+
OnPropertyChanged(nameof(DistributionName));
36+
}
37+
}
38+
2639
public event PropertyChangedEventHandler PropertyChanged;
2740

2841
public IEnumerable<Control> Items(DistributionClass distributionClass)
2942
{
30-
var distributionName = new TextBox {Text = distributionClass.Name};
43+
var distributionName = new TextBox();
44+
distributionName.SetBinding(TextBox.TextProperty,
45+
BindHelper.BindingObject(nameof(DistributionName), this));
3146

47+
DistributionName = distributionClass.Name;
3248
distributionName.TextChanged += (_, _) =>
3349
{
3450
DistributionNameIsValid = DistributionNameValidator.ValidateRename(
@@ -41,7 +57,7 @@ public IEnumerable<Control> Items(DistributionClass distributionClass)
4157
new Label
4258
{
4359
Content = "- Only alphanumeric characters are allowed.\n" +
44-
"- Name must contain atleast 3 characters.",
60+
"- Name must contain at least 3 characters.",
4561
Margin = new Thickness(0, 0, 0, 5)
4662
},
4763
distributionName

WslToolbox.Gui/Collections/TopMenuCollection.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Windows.Data;
33
using ModernWpf.Controls;
44
using WslToolbox.Gui.Collections.TopMenu;
5-
using WslToolbox.Gui.Configurations;
65
using WslToolbox.Gui.Helpers.Ui;
76
using WslToolbox.Gui.ViewModels;
87

@@ -42,7 +41,7 @@ public static CompositeCollection Items(MainViewModel viewModel)
4241
{
4342
Content = "Debug menu",
4443
Flyout = ElementHelper.MenuFlyoutItems(DebugTopMenuCollection.Items(viewModel)),
45-
Visibility = AppConfiguration.DebugMode ? Visibility.Visible : Visibility.Collapsed
44+
Visibility = viewModel.IsDebug ? Visibility.Visible : Visibility.Collapsed
4645
}
4746
};
4847
}

WslToolbox.Gui/Commands/Distribution/MoveBasePathDistributionCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.IO;
54
using System.Linq;
65
using System.Threading.Tasks;

WslToolbox.Gui/Commands/Distribution/RenameDistributionCommand.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
2-
using ModernWpf.Controls;
1+
using ModernWpf.Controls;
32
using WslToolbox.Core;
43
using WslToolbox.Gui.Collections.Dialogs;
4+
using WslToolbox.Gui.Handlers;
55
using WslToolbox.Gui.Helpers;
66
using WslToolbox.Gui.Helpers.Ui;
77

@@ -16,8 +16,6 @@ public RenameDistributionCommand(DistributionClass distributionClass) : base(
1616
IsExecutable = IsExecutableDefault;
1717
}
1818

19-
public static event EventHandler DistributionRenamed;
20-
2119
public override async void Execute(object parameter)
2220
{
2321
var distribution = (DistributionClass) parameter;
@@ -36,9 +34,21 @@ public override async void Execute(object parameter)
3634
var result = await renameDistribution.Dialog.ShowAsync();
3735

3836
if (result != ContentDialogResult.Primary) return;
39-
37+
var newName = renameDistributionDialogCollection.DistributionName;
38+
39+
if (ToolboxClass.DistributionByName(newName) != null)
40+
{
41+
ContentDialogHandler.ShowDialog(
42+
"Error",
43+
$"Renaming failed. The name {newName} already exists.",
44+
showCloseButton: true,
45+
closeButtonText: "Close",
46+
waitForUser: true);
47+
return;
48+
}
49+
50+
Core.Commands.Distribution.RenameDistributionCommand.Execute(distribution, newName);
4051
IsExecutable = _ => true;
41-
DistributionRenamed?.Invoke(this, EventArgs.Empty);
4252
}
4353
}
4454
}

WslToolbox.Gui/Handlers/ContentDialogHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,13 @@ public async void Show(string title = null,
5353
ProgressBarVisibility = progressBarVisibility;
5454

5555
if (_dialog.IsVisible)
56-
{
5756
if (waitForUser)
5857
{
5958
Debug.WriteLine("Queued dialog.");
6059

6160
while (_dialog.IsVisible)
6261
await Task.Delay(10);
6362
}
64-
}
6563

6664
if (waitForUser)
6765
await _dialog.ShowAsync();

WslToolbox.Gui/ViewModels/MainViewModel.cs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Diagnostics;
55
using System.Threading.Tasks;
66
using System.Windows;
7-
using System.Windows.Controls;
87
using System.Windows.Data;
98
using System.Windows.Input;
109
using CommandLine;
@@ -32,44 +31,51 @@ public class Options
3231
{
3332
[Option('r', "reset", Default = false, HelpText = "Reset configuration")]
3433
public bool ResetConfiguration { get; set; }
34+
35+
[Option('f', "release", Default = false, HelpText = "Force release version")]
36+
public bool Release { get; set; }
3537
}
3638

3739
public class MainViewModel : INotifyPropertyChanged
3840
{
41+
private readonly ContentDialogHandler _contentDialogHandler = new();
3942
private readonly UpdateHandler _updateHandler;
4043
private readonly MainView _view;
4144
public readonly ICommand CheckForUpdates;
4245
public readonly ConfigurationHandler Config = new();
4346

4447
public readonly ICommand EnableWindowsComponents = new EnableWindowsComponentsCommand();
48+
public readonly bool IsDebug;
4549
public readonly Logger Log;
4650

4751
private List<DistributionClass> _distributionList = new();
4852
private BindingList<DistributionClass> _gridList = new();
53+
private KeyboardShortcutHandler _keyboardShortcutHandler;
4954
private DistributionClass _selectedDistribution;
5055
private bool _updateAvailable;
5156
private Visibility _updateAvailableVisibility = Visibility.Collapsed;
52-
public ContentDialogHandler ContentDialogHandler = new();
5357
public List<DistributionClass> InstallableDistributions = null;
54-
public KeyboardShortcutHandler KeyboardShortcutHandler;
5558

5659

5760
public MainViewModel(MainView view)
5861
{
5962
var args = Environment.GetCommandLineArgs();
63+
var disableDebug = false;
6064

6165
Parser.Default.ParseArguments<Options>(args)
6266
.WithParsed(commandLineArgument =>
6367
{
6468
if (commandLineArgument.ResetConfiguration) Config.Reset();
69+
disableDebug = commandLineArgument.Release;
6570
});
6671

6772
Log = Log();
6873
_view = view;
6974
_updateHandler = new UpdateHandler(_view);
7075
CheckForUpdates = new CheckForUpdateCommand(_updateHandler);
76+
IsDebug = AppConfiguration.DebugMode && !disableDebug;
7177

72-
if (AppConfiguration.DebugMode) InitializeDebugMode();
78+
if (IsDebug) InitializeDebugMode();
7379
InitializeKeyboardShortcuts();
7480
InitializeEventHandlers();
7581
InitializeUpdater();
@@ -133,7 +139,7 @@ public DistributionClass SelectedDistribution
133139
public ICommand ShowApplication => new ShowApplicationCommand(_view);
134140
public ICommand ExitApplication => new ExitApplicationCommand();
135141
public ICommand Refresh => new RefreshDistributionsCommand(_view);
136-
public ICommand ShowSettings => new ShowSettingsCommand(Config, KeyboardShortcutHandler);
142+
public ICommand ShowSettings => new ShowSettingsCommand(Config, _keyboardShortcutHandler);
137143
public ICommand ShowExportDialog => new ExportDistributionCommand(SelectedDistribution, this);
138144
public ICommand ShowImportDialog => new ImportDistributionCommand(SelectedDistribution, this);
139145
public ICommand StartWslService => new StartWslServiceCommand();
@@ -159,24 +165,10 @@ public DistributionClass SelectedDistribution
159165

160166
public void InitializeKeyboardShortcuts()
161167
{
162-
KeyboardShortcutHandler =
168+
_keyboardShortcutHandler =
163169
new KeyboardShortcutHandler(Config.Configuration.KeyboardShortcutConfiguration, this);
164170
}
165171

166-
public void SetStatus(string status)
167-
{
168-
var topMenuBar = (ItemsControl) _view.FindName("TopMenu");
169-
if (topMenuBar == null) return;
170-
171-
foreach (var topMenuBarItem in topMenuBar.Items)
172-
{
173-
if (topMenuBarItem.GetType() != typeof(Label)) continue;
174-
var topMenuBarItemLabel = (Label) topMenuBarItem;
175-
176-
topMenuBarItemLabel.Content = status;
177-
}
178-
}
179-
180172
private void InitializeEventHandlers()
181173
{
182174
Config.ConfigurationUpdatedSuccessfully += OnSaveSuccessfully;
@@ -189,23 +181,23 @@ private void InitializeEventHandlers()
189181

190182
private async void OnHideContentDialogEvent(object? sender, ContentDialogEventArguments e)
191183
{
192-
if (ContentDialogHandler.GetType() != typeof(ContentDialogHandler)) return;
184+
if (_contentDialogHandler.GetType() != typeof(ContentDialogHandler)) return;
193185
if ((string) e.Owner != nameof(MainViewModel)) return;
194186

195187
if (e.CloseDelay > 0)
196188
await Task.Delay(e.CloseDelay);
197189

198-
ContentDialogHandler.Dispose();
190+
_contentDialogHandler.Dispose();
199191
}
200192

201193
private void OnUpdateContentDialogEvent(object? sender, ContentDialogEventArguments e)
202194
{
203-
if (ContentDialogHandler.GetType() != typeof(ContentDialogHandler)) return;
195+
if (_contentDialogHandler.GetType() != typeof(ContentDialogHandler)) return;
204196
if ((string) e.Owner != nameof(MainViewModel)) return;
205197

206-
ContentDialogHandler.ProgressBarVisibility = e.ProgressBarVisibility;
207-
ContentDialogHandler.ProgressValue = e.Progress;
208-
ContentDialogHandler.Show(
198+
_contentDialogHandler.ProgressBarVisibility = e.ProgressBarVisibility;
199+
_contentDialogHandler.ProgressValue = e.Progress;
200+
_contentDialogHandler.Show(
209201
e.Title,
210202
e.Content,
211203
e.ProgressBarVisibility,
@@ -262,7 +254,7 @@ private void ShortcutHandler()
262254
modifierKey = ModifierKeys.Shift;
263255

264256

265-
var shortcut = KeyboardShortcutHandler.ShortcutByKey(args.Key, modifierKey);
257+
var shortcut = _keyboardShortcutHandler.ShortcutByKey(args.Key, modifierKey);
266258

267259
if (shortcut is not {Enabled: true}) return;
268260
shortcut.Action?.Invoke();

WslToolbox.Gui/Views/MainView.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public MainView()
2828
WslIsEnabledCheck();
2929
InitializeComponent();
3030
ApplyTitle($"{AssemblyHelper.Version()} build {AssemblyHelper.Build()}");
31-
if (AppConfiguration.DebugMode) ApplyTitle("Dev Build");
31+
if (_viewModel.IsDebug) ApplyTitle("Dev Build");
3232
InitializeDataGrid();
3333
InitializeTopMenu();
3434
HandleConfiguration();

0 commit comments

Comments
 (0)