55using System . Runtime . InteropServices ;
66using System . Text ;
77using System . Windows . Input ;
8- using Windows . Storage . Pickers ;
98using Files . Shared . Helpers ;
109
1110namespace Files . App . ViewModels . Dialogs
@@ -36,6 +35,20 @@ public sealed class CreateShortcutDialogViewModel : ObservableObject
3635 // Previous path of the destination item
3736 private string _previousShortcutTargetPath ;
3837
38+ private string _shortcutName ;
39+ public string ShortcutName
40+ {
41+ get => _shortcutName ;
42+ set
43+ {
44+ if ( SetProperty ( ref _shortcutName , value ) )
45+ {
46+ OnPropertyChanged ( nameof ( ShowNameWarningTip ) ) ;
47+ OnPropertyChanged ( nameof ( IsShortcutValid ) ) ;
48+ }
49+ }
50+ }
51+
3952 // Destination of the shortcut chosen by the user (can be a path, a command or a URL)
4053 private string _shortcutTarget ;
4154 public string ShortcutTarget
@@ -167,6 +180,10 @@ public string ShortcutTarget
167180 Arguments = string . Empty ;
168181 _previousShortcutTargetPath = string . Empty ;
169182 }
183+ finally
184+ {
185+ AutoFillName ( ) ;
186+ }
170187 }
171188 }
172189
@@ -178,12 +195,19 @@ public bool IsLocationValid
178195 set
179196 {
180197 if ( SetProperty ( ref _isLocationValid , value ) )
198+ {
181199 OnPropertyChanged ( nameof ( ShowWarningTip ) ) ;
200+ OnPropertyChanged ( nameof ( IsShortcutValid ) ) ;
201+ }
182202 }
183203 }
184204
185205 public bool ShowWarningTip => ! string . IsNullOrEmpty ( ShortcutTarget ) && ! _isLocationValid ;
186206
207+ public bool ShowNameWarningTip => ! string . IsNullOrEmpty ( _shortcutTarget ) && ! FilesystemHelpers . IsValidForFilename ( _shortcutName ) ;
208+
209+ public bool IsShortcutValid => _isLocationValid && ! ShowNameWarningTip && ! string . IsNullOrEmpty ( _shortcutTarget ) ;
210+
187211 // Command invoked when the user clicks the 'Browse' button
188212 public ICommand SelectDestinationCommand { get ; private set ; }
189213
@@ -225,31 +249,9 @@ private Task SelectDestination()
225249
226250 private async Task CreateShortcutAsync ( )
227251 {
228- string ? destinationName ;
229252 var extension = DestinationPathExists ? ".lnk" : ".url" ;
230253
231- if ( DestinationPathExists )
232- {
233- destinationName = Path . GetFileName ( FullPath ) ;
234-
235- if ( string . IsNullOrEmpty ( FullPath ) )
236- {
237-
238- var destinationPath = FullPath . Replace ( '/' , '\\ ' ) ;
239-
240- if ( destinationPath . EndsWith ( '\\ ' ) )
241- destinationPath = destinationPath . Substring ( 0 , destinationPath . Length - 1 ) ;
242-
243- destinationName = destinationPath . Substring ( destinationPath . LastIndexOf ( '\\ ' ) + 1 ) ;
244- }
245- }
246- else
247- {
248- var uri = new Uri ( FullPath ) ;
249- destinationName = uri . Host ;
250- }
251-
252- var shortcutName = FilesystemHelpers . GetShortcutNamingPreference ( destinationName ) ;
254+ var shortcutName = FilesystemHelpers . GetShortcutNamingPreference ( _shortcutName ) ;
253255 ShortcutCompleteName = shortcutName + extension ;
254256 var filePath = Path . Combine ( WorkingDirectory , ShortcutCompleteName ) ;
255257
@@ -262,5 +264,34 @@ private async Task CreateShortcutAsync()
262264
263265 ShortcutCreatedSuccessfully = await FileOperationsHelpers . CreateOrUpdateLinkAsync ( filePath , FullPath , Arguments ) ;
264266 }
267+
268+ private void AutoFillName ( )
269+ {
270+ if ( DestinationPathExists )
271+ {
272+ var destinationName = Path . GetFileName ( FullPath ) ;
273+ if ( DestinationPathExists )
274+ {
275+ destinationName = Path . GetFileName ( FullPath ) ;
276+
277+ if ( string . IsNullOrEmpty ( FullPath ) )
278+ {
279+
280+ var destinationPath = FullPath . Replace ( '/' , '\\ ' ) ;
281+
282+ if ( destinationPath . EndsWith ( '\\ ' ) )
283+ destinationPath = destinationPath . Substring ( 0 , destinationPath . Length - 1 ) ;
284+
285+ destinationName = destinationPath . Substring ( destinationPath . LastIndexOf ( '\\ ' ) + 1 ) ;
286+ }
287+ }
288+ ShortcutName = destinationName ;
289+ }
290+ else if ( ! string . IsNullOrEmpty ( FullPath ) )
291+ {
292+ var uri = new Uri ( FullPath ) ;
293+ ShortcutName = uri . Host ;
294+ }
295+ }
265296 }
266297}
0 commit comments