5
5
using System . Runtime . InteropServices ;
6
6
using System . Text ;
7
7
using System . Windows . Input ;
8
- using Windows . Storage . Pickers ;
9
8
using Files . Shared . Helpers ;
10
9
11
10
namespace Files . App . ViewModels . Dialogs
@@ -36,6 +35,20 @@ public sealed class CreateShortcutDialogViewModel : ObservableObject
36
35
// Previous path of the destination item
37
36
private string _previousShortcutTargetPath ;
38
37
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
+
39
52
// Destination of the shortcut chosen by the user (can be a path, a command or a URL)
40
53
private string _shortcutTarget ;
41
54
public string ShortcutTarget
@@ -167,6 +180,10 @@ public string ShortcutTarget
167
180
Arguments = string . Empty ;
168
181
_previousShortcutTargetPath = string . Empty ;
169
182
}
183
+ finally
184
+ {
185
+ AutoFillName ( ) ;
186
+ }
170
187
}
171
188
}
172
189
@@ -178,12 +195,19 @@ public bool IsLocationValid
178
195
set
179
196
{
180
197
if ( SetProperty ( ref _isLocationValid , value ) )
198
+ {
181
199
OnPropertyChanged ( nameof ( ShowWarningTip ) ) ;
200
+ OnPropertyChanged ( nameof ( IsShortcutValid ) ) ;
201
+ }
182
202
}
183
203
}
184
204
185
205
public bool ShowWarningTip => ! string . IsNullOrEmpty ( ShortcutTarget ) && ! _isLocationValid ;
186
206
207
+ public bool ShowNameWarningTip => ! string . IsNullOrEmpty ( _shortcutTarget ) && ! FilesystemHelpers . IsValidForFilename ( _shortcutName ) ;
208
+
209
+ public bool IsShortcutValid => _isLocationValid && ! ShowNameWarningTip && ! string . IsNullOrEmpty ( _shortcutTarget ) ;
210
+
187
211
// Command invoked when the user clicks the 'Browse' button
188
212
public ICommand SelectDestinationCommand { get ; private set ; }
189
213
@@ -225,31 +249,9 @@ private Task SelectDestination()
225
249
226
250
private async Task CreateShortcutAsync ( )
227
251
{
228
- string ? destinationName ;
229
252
var extension = DestinationPathExists ? ".lnk" : ".url" ;
230
253
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 ) ;
253
255
ShortcutCompleteName = shortcutName + extension ;
254
256
var filePath = Path . Combine ( WorkingDirectory , ShortcutCompleteName ) ;
255
257
@@ -262,5 +264,34 @@ private async Task CreateShortcutAsync()
262
264
263
265
ShortcutCreatedSuccessfully = await FileOperationsHelpers . CreateOrUpdateLinkAsync ( filePath , FullPath , Arguments ) ;
264
266
}
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
+ }
265
296
}
266
297
}
0 commit comments