Skip to content

Commit dab4e23

Browse files
committed
Fixed import dialog
1 parent c24a15f commit dab4e23

15 files changed

+170
-80
lines changed

WslToolbox.Core/DistributionClass.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public class DistributionClass
2828

2929
public long Size { get; set; }
3030

31-
public static async Task<List<DistributionClass>> ListAvailableDistributions(List<DistributionClass> currentDistributions)
31+
public static async Task<List<DistributionClass>> ListAvailableDistributions(
32+
List<DistributionClass> currentDistributions)
3233
{
3334
return await DistributionFetcherHelper.ReadOnlineDistributions(currentDistributions);
3435
}

WslToolbox.Core/Helpers/DistributionFetcherHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public static async Task<List<DistributionClass>> ReadOnlineDistributions(
4343
if (WebRequest.Create(Url) is not HttpWebRequest request) return distros;
4444
var response = await request.GetResponseAsync();
4545
var encoding = Encoding.ASCII;
46-
var jsonResponse = new StreamReader(response.GetResponseStream(), encoding);
46+
var jsonResponse =
47+
new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException(), encoding);
4748
var onlineDistributions =
4849
JsonSerializer.Deserialize<OnlineDistributions>(await jsonResponse.ReadToEndAsync());
4950

WslToolbox.Gui/Collections/Dialogs/AboutDialogCollection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ private static string UsedPackages()
4343
{"AutoUpdater.NET", "https://github.com/ravibpatel/AutoUpdater.NET"},
4444
{"Command Line Parser Library", "https://github.com/commandlineparser/commandline"},
4545
{"Hardcodet NotifyIcon for WPF", "https://github.com/hardcodet/wpf-notifyicon"},
46-
{"ModernWPF UI Library", "https://github.com/Kinnara/ModernWpf"}
46+
{"ModernWPF UI Library", "https://github.com/Kinnara/ModernWpf"},
47+
{"Serilog", "https://github.com/serilog/serilog"}
4748
};
4849

4950
return packages.Aggregate<KeyValuePair<string, string>, string>(null,
Lines changed: 96 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
4+
using System.Diagnostics;
35
using System.IO;
46
using System.Runtime.CompilerServices;
57
using System.Windows;
68
using System.Windows.Controls;
79
using System.Windows.Data;
810
using Microsoft.Win32;
9-
using WslToolbox.Gui.Configurations;
1011
using WslToolbox.Gui.Handlers;
1112
using WslToolbox.Gui.Helpers.Ui;
1213
using WslToolbox.Gui.Validators;
@@ -16,11 +17,25 @@ namespace WslToolbox.Gui.Collections.Dialogs
1617
{
1718
public sealed class ImportDistributionDialogCollection : INotifyPropertyChanged
1819
{
20+
private string _distributionName;
21+
1922
private bool _distributionNameIsValid;
20-
public string DistributionName;
21-
public string SelectedBasePath;
23+
private bool _runAfterImport;
24+
25+
private string _selectedBasePath;
2226

23-
public string SelectedFilePath;
27+
private string _selectedFilePath;
28+
29+
public string DistributionName
30+
{
31+
get => _distributionName;
32+
set
33+
{
34+
if (value == _distributionName) return;
35+
_distributionName = value;
36+
OnPropertyChanged(nameof(DistributionName));
37+
}
38+
}
2439

2540
public bool DistributionNameIsValid
2641
{
@@ -33,71 +48,96 @@ public bool DistributionNameIsValid
3348
}
3449
}
3550

36-
public event PropertyChangedEventHandler PropertyChanged;
51+
public string SelectedBasePath
52+
{
53+
get => _selectedBasePath;
54+
set
55+
{
56+
if (value == _selectedBasePath) return;
57+
_selectedBasePath = value;
58+
OnPropertyChanged(nameof(SelectedBasePath));
59+
}
60+
}
3761

38-
public IEnumerable<Control> Items(MainViewModel viewModel)
62+
public string SelectedFilePath
3963
{
40-
var distributionFile = new TextBox
41-
{IsEnabled = false, IsReadOnly = true, Margin = new Thickness(0, 0, 0, 2)};
42-
var distributionFileBrowse = new Button {Content = "Browse...", Margin = new Thickness(0, 0, 0, 10)};
64+
get => _selectedFilePath;
65+
set
66+
{
67+
if (value == _selectedFilePath) return;
68+
_selectedFilePath = value;
69+
OnPropertyChanged(nameof(SelectedFilePath));
70+
}
71+
}
4372

44-
var distributionBasePath = ElementHelper.AddTextBox(nameof(DefaultConfiguration.UserBasePath),
45-
null, "Configuration.UserBasePath", viewModel.Config, width: 400, bindingMode: BindingMode.OneWay);
73+
public bool RunAfterImport
74+
{
75+
get => _runAfterImport;
76+
set
77+
{
78+
if (value == _runAfterImport) return;
79+
_runAfterImport = value;
80+
OnPropertyChanged(nameof(RunAfterImport));
81+
}
82+
}
4683

47-
var distributionBasePathBrowse = new Button {Content = "Browse...", Margin = new Thickness(0, 0, 0, 10)};
48-
var distributionName = new TextBox {Margin = new Thickness(0, 0, 0, 10)};
84+
public event PropertyChangedEventHandler PropertyChanged;
4985

50-
distributionFileBrowse.Click += (_, _) => { distributionFile.Text = SelectDistributionFile(); };
51-
distributionBasePathBrowse.Click += (_, _) => { distributionBasePath.Text = SelectDistributionBasePath(); };
52-
distributionName.TextChanged += (_, _) =>
53-
{
54-
DistributionNameIsValid =
55-
ValidateImportValues(distributionFile.Text, distributionBasePath.Text, distributionName.Text);
86+
public IEnumerable<Control> Items(MainViewModel viewModel)
87+
{
88+
var userBasePath = viewModel.Config.Configuration.UserBasePath;
89+
var distributionFileBrowse = new Button {Content = "Browse..."};
90+
var distributionBasePathBrowse = new Button {Content = "Browse..."};
5691

57-
SelectedFilePath = DistributionNameIsValid ? distributionFile.Text : null;
58-
SelectedBasePath = DistributionNameIsValid ? distributionBasePath.Text : null;
59-
SelectedBasePath = DistributionNameIsValid ? distributionBasePath.Text : null;
60-
DistributionName = DistributionNameIsValid ? distributionName.Text : null;
61-
};
92+
SelectedBasePath = Directory.Exists(userBasePath) ? userBasePath : null;
93+
distributionFileBrowse.Click += (_, _) => { SelectDistributionFile(); };
94+
distributionBasePathBrowse.Click += (_, _) => { SelectDistributionBasePath(); };
6295

6396
Control[] items =
6497
{
6598
new Label {Content = "Name:", Margin = new Thickness(0, 0, 0, 2), FontWeight = FontWeights.Bold},
6699
new Label
67100
{
68101
Content = "- Only alphanumeric characters are allowed.\n" +
69-
"- Name must contain atleast 3 characters.",
70-
Margin = new Thickness(0, 0, 0, 5)
71-
},
72-
distributionName,
73-
new Label {Content = "Filename:", Margin = new Thickness(0, 0, 0, 2), FontWeight = FontWeights.Bold},
74-
new Label
75-
{
76-
Content = "Select the file which needs to be imported\n"
102+
"- Name must contain at least 3 characters.",
103+
Margin = new Thickness(0, 0, 0, 10)
77104
},
78-
distributionFile,
105+
ElementHelper.AddTextBox(nameof(DistributionName), bind: "DistributionName", width: 400, source: this,
106+
isReadonly: false, isEnabled: true, updateSourceTrigger: UpdateSourceTrigger.PropertyChanged,
107+
placeholder: "Name your distribution"),
79108
ElementHelper.Separator(),
109+
110+
new Label {Content = "Filename:", Margin = new Thickness(0, 0, 0, 2), FontWeight = FontWeights.Bold},
111+
ElementHelper.AddTextBox(nameof(SelectedFilePath),
112+
null, "SelectedFilePath", this, width: 400,
113+
bindingMode: BindingMode.TwoWay, updateSourceTrigger: UpdateSourceTrigger.PropertyChanged,
114+
placeholder: "Select an exported distribution file."),
115+
ElementHelper.Separator(0),
80116
distributionFileBrowse,
81117

82-
new Label {Content = "Base path:", Margin = new Thickness(0, 0, 0, 2), FontWeight = FontWeights.Bold},
83-
distributionBasePath,
84118
ElementHelper.Separator(),
119+
new Label {Content = "Base path:", Margin = new Thickness(0, 0, 0, 2), FontWeight = FontWeights.Bold},
120+
ElementHelper.AddTextBox(nameof(SelectedBasePath),
121+
bind: "SelectedBasePath", source: this, width: 400, bindingMode: BindingMode.TwoWay,
122+
updateSourceTrigger: UpdateSourceTrigger.PropertyChanged,
123+
placeholder: "Select an installation directory"),
124+
ElementHelper.Separator(0),
85125
distributionBasePathBrowse
86126
};
87127

88128
return items;
89129
}
90130

91-
private static string SelectDistributionFile()
131+
private void SelectDistributionFile()
92132
{
93133
var distributionFilePathDialog = FileDialogHandler.OpenFileDialog();
94134

95-
return distributionFilePathDialog.ShowDialog() == null
135+
SelectedFilePath = distributionFilePathDialog.ShowDialog() == null
96136
? null
97137
: distributionFilePathDialog.FileName;
98138
}
99139

100-
private static string SelectDistributionBasePath()
140+
private void SelectDistributionBasePath()
101141
{
102142
OpenFileDialog openLocation = new()
103143
{
@@ -110,21 +150,31 @@ private static string SelectDistributionBasePath()
110150
RestoreDirectory = true
111151
};
112152

113-
return openLocation.ShowDialog() == null ? null : Path.GetDirectoryName(openLocation.FileName);
153+
SelectedBasePath = openLocation.ShowDialog() == null ? null : Path.GetDirectoryName(openLocation.FileName);
114154
}
115155

116-
private static bool ValidateImportValues(string distributionFile, string distributionBasePath,
117-
string distributionName)
156+
private bool ValidateImportValues()
118157
{
119-
return
120-
distributionFile.Length >= 1 && distributionBasePath.Length >= 1 &&
121-
File.Exists(distributionFile) && Directory.Exists(distributionBasePath) &&
122-
DistributionNameValidator.ValidateName(distributionName);
158+
return DistributionName != null
159+
&& DistributionNameValidator.ValidateName(DistributionName)
160+
&& SelectedBasePath is {Length: > 1}
161+
&& SelectedFilePath is {Length: > 1}
162+
&& Directory.Exists(SelectedBasePath)
163+
&& File.Exists(SelectedFilePath);
123164
}
124165

125166
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
126167
{
127168
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
169+
Debug.WriteLine($"{propertyName} changed.");
170+
try
171+
{
172+
DistributionNameIsValid = ValidateImportValues();
173+
}
174+
catch (Exception e)
175+
{
176+
Debug.WriteLine(e);
177+
}
128178
}
129179
}
130180
}

WslToolbox.Gui/Collections/Dialogs/InstallDistributionDialogCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.Collections.Generic;
22
using System.Windows;
33
using System.Windows.Controls;
4-
using WslToolbox.Core;
54
using ModernWpf.Controls.Primitives;
5+
using WslToolbox.Core;
66

77
namespace WslToolbox.Gui.Collections.Dialogs
88
{
@@ -38,7 +38,7 @@ private static ComboBox DistributionListControl(List<DistributionClass> distribu
3838
Name = "InstallDistributionList",
3939
ItemsSource = distributionClass.FindAll(x => !x.IsInstalled),
4040
DisplayMemberPath = "Name",
41-
MinWidth = 200,
41+
MinWidth = 200
4242
};
4343

4444
ControlHelper.SetHeader(installDistributionList, "Select an online distribution to install");

WslToolbox.Gui/Collections/Settings/GeneralSettingsGenericCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private CompositeCollection GenericControls()
3232
"Hide export warning", "Configuration.HideExportWarning", Source, header: null),
3333
ElementHelper.AddToggleSwitch(nameof(DefaultConfiguration.AutoCheckUpdates),
3434
"Automatically check for updates on startup", "Configuration.AutoCheckUpdates", Source,
35-
header: null),
35+
header: null)
3636
};
3737
}
3838

@@ -68,7 +68,7 @@ private CompositeCollection EnableSystemTraySettings()
6868
ElementHelper.AddToggleSwitch(nameof(DefaultConfiguration.MinimizeToTray),
6969
"Minimize to tray", "Configuration.MinimizeToTray", Source),
7070
ElementHelper.AddToggleSwitch(nameof(DefaultConfiguration.MinimizeOnStartup),
71-
"Minimize on startup", "Configuration.MinimizeOnStartup", Source),
71+
"Minimize on startup", "Configuration.MinimizeOnStartup", Source)
7272
};
7373
}
7474
}

WslToolbox.Gui/Collections/Settings/KeyboardShortcutSettingsGenericCollection.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public CompositeCollection Items()
2828
"Enable keyboard shortcuts", "Configuration.KeyboardShortcutConfiguration.Enabled", Source,
2929
header: null),
3030
new Separator(),
31-
ElementHelper.ItemsControlGroup(ShortcutControls(),
32-
source: Source,
31+
ElementHelper.ItemsControlGroup(ShortcutControls(), source: Source,
3332
requires: "Configuration.KeyboardShortcutConfiguration.Enabled")
3433
};
3534
}
@@ -47,11 +46,20 @@ private CompositeCollection ShortcutControls()
4746

4847
shortCutKey = $"{shortCutKey}{shortcut.Key}";
4948

50-
keyboardChecks.Add(ElementHelper.AddToggleSwitch(shortcut.Configuration,
51-
$"{shortcut.Name}\t\t[{shortCutKey}]",
49+
var shortcutLine = new StackPanel {Orientation = Orientation.Horizontal};
50+
shortcutLine.Children.Add(ElementHelper.AddToggleSwitch(shortcut.Configuration,
51+
$"{shortcut.Name}",
5252
$"Configuration.KeyboardShortcutConfiguration.{shortcut.Configuration}", Source,
53-
enabled: shortcut.Modifiable, header: null)
54-
);
53+
header: null));
54+
shortcutLine.Children.Add(ElementHelper.Separator(marginLeft: 10));
55+
shortcutLine.Children.Add(new TextBox
56+
{
57+
Text = shortCutKey,
58+
IsReadOnly = true,
59+
IsEnabled = false
60+
});
61+
62+
keyboardChecks.Add(shortcutLine);
5563
}
5664

5765
return keyboardChecks;

WslToolbox.Gui/Collections/Settings/OtherSettingsGenericCollection.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Windows;
1+
using System;
2+
using System.Windows;
23
using System.Windows.Controls;
34
using System.Windows.Data;
45
using ModernWpf.Controls;
@@ -88,7 +89,13 @@ private CompositeCollection AdvancedControls()
8889
LogConfiguration.GetValues(),
8990
"Configuration.MinimumLogLevel",
9091
Source
91-
)
92+
),
93+
new Label {Margin = new Thickness(0, 5, 0, 0), Content = "Update manifest:"},
94+
new HyperlinkButton
95+
{
96+
Content = AppConfiguration.AppConfigurationUpdateXml,
97+
NavigateUri = new Uri(AppConfiguration.AppConfigurationUpdateXml)
98+
}
9299
};
93100
}
94101
}

WslToolbox.Gui/Commands/Distribution/ExportDistributionCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Diagnostics;
32
using ModernWpf.Controls;
43
using WslToolbox.Core;
54
using WslToolbox.Gui.Handlers;

WslToolbox.Gui/Commands/Distribution/InstallDistributionCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using WslToolbox.Core.Helpers;
77
using WslToolbox.Gui.Collections.Dialogs;
88
using WslToolbox.Gui.Handlers;
9-
using WslToolbox.Gui.Helpers;
109
using WslToolbox.Gui.Helpers.Ui;
1110
using WslToolbox.Gui.ViewModels;
1211

0 commit comments

Comments
 (0)