1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . ComponentModel ;
4+ using System . Diagnostics ;
35using System . IO ;
46using System . Runtime . CompilerServices ;
57using System . Windows ;
68using System . Windows . Controls ;
79using System . Windows . Data ;
810using Microsoft . Win32 ;
9- using WslToolbox . Gui . Configurations ;
1011using WslToolbox . Gui . Handlers ;
1112using WslToolbox . Gui . Helpers . Ui ;
1213using 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}
0 commit comments