From 5e5e4f30c23fe0bfd97012779742a3f55fc84fb7 Mon Sep 17 00:00:00 2001 From: Filippo Ferrario <102259289+ferrariofilippo@users.noreply.github.com> Date: Wed, 27 Nov 2024 15:52:31 +0100 Subject: [PATCH] Feature: Added support for assigning a custom name when creating a new shortcut (#16506) --- .../Dialogs/CreateShortcutDialog.xaml | 26 +++++- .../Dialogs/CreateShortcutDialogViewModel.cs | 79 +++++++++++++------ 2 files changed, 80 insertions(+), 25 deletions(-) diff --git a/src/Files.App/Dialogs/CreateShortcutDialog.xaml b/src/Files.App/Dialogs/CreateShortcutDialog.xaml index d800512c8584..7dbe4ad5f0bf 100644 --- a/src/Files.App/Dialogs/CreateShortcutDialog.xaml +++ b/src/Files.App/Dialogs/CreateShortcutDialog.xaml @@ -9,7 +9,7 @@ Title="{helpers:ResourceString Name=NewShortcutDialogTitle}" DefaultButton="Primary" HighContrastAdjustment="None" - IsPrimaryButtonEnabled="{x:Bind ViewModel.IsLocationValid, Mode=OneWay}" + IsPrimaryButtonEnabled="{x:Bind ViewModel.IsShortcutValid, Mode=OneWay}" PrimaryButtonCommand="{x:Bind ViewModel.PrimaryButtonCommand}" PrimaryButtonText="{helpers:ResourceString Name=Create}" RequestedTheme="{x:Bind RootAppElement.RequestedTheme, Mode=OneWay}" @@ -30,6 +30,8 @@ + + @@ -68,6 +70,28 @@ Command="{x:Bind ViewModel.SelectDestinationCommand}" Content="{helpers:ResourceString Name=Browse}" /> + + + + + + + + diff --git a/src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs b/src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs index 6d5ab229095f..290ea485f152 100644 --- a/src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs +++ b/src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs @@ -5,7 +5,6 @@ using System.Runtime.InteropServices; using System.Text; using System.Windows.Input; -using Windows.Storage.Pickers; using Files.Shared.Helpers; namespace Files.App.ViewModels.Dialogs @@ -36,6 +35,20 @@ public sealed class CreateShortcutDialogViewModel : ObservableObject // Previous path of the destination item private string _previousShortcutTargetPath; + private string _shortcutName; + public string ShortcutName + { + get => _shortcutName; + set + { + if (SetProperty(ref _shortcutName, value)) + { + OnPropertyChanged(nameof(ShowNameWarningTip)); + OnPropertyChanged(nameof(IsShortcutValid)); + } + } + } + // Destination of the shortcut chosen by the user (can be a path, a command or a URL) private string _shortcutTarget; public string ShortcutTarget @@ -167,6 +180,10 @@ public string ShortcutTarget Arguments = string.Empty; _previousShortcutTargetPath = string.Empty; } + finally + { + AutoFillName(); + } } } @@ -178,12 +195,19 @@ public bool IsLocationValid set { if (SetProperty(ref _isLocationValid, value)) + { OnPropertyChanged(nameof(ShowWarningTip)); + OnPropertyChanged(nameof(IsShortcutValid)); + } } } public bool ShowWarningTip => !string.IsNullOrEmpty(ShortcutTarget) && !_isLocationValid; + public bool ShowNameWarningTip => !string.IsNullOrEmpty(_shortcutTarget) && !FilesystemHelpers.IsValidForFilename(_shortcutName); + + public bool IsShortcutValid => _isLocationValid && !ShowNameWarningTip && !string.IsNullOrEmpty(_shortcutTarget); + // Command invoked when the user clicks the 'Browse' button public ICommand SelectDestinationCommand { get; private set; } @@ -225,31 +249,9 @@ private Task SelectDestination() private async Task CreateShortcutAsync() { - string? destinationName; var extension = DestinationPathExists ? ".lnk" : ".url"; - if (DestinationPathExists) - { - destinationName = Path.GetFileName(FullPath); - - if(string.IsNullOrEmpty(FullPath)) - { - - var destinationPath = FullPath.Replace('/', '\\'); - - if (destinationPath.EndsWith('\\')) - destinationPath = destinationPath.Substring(0, destinationPath.Length - 1); - - destinationName = destinationPath.Substring(destinationPath.LastIndexOf('\\') + 1); - } - } - else - { - var uri = new Uri(FullPath); - destinationName = uri.Host; - } - - var shortcutName = FilesystemHelpers.GetShortcutNamingPreference(destinationName); + var shortcutName = FilesystemHelpers.GetShortcutNamingPreference(_shortcutName); ShortcutCompleteName = shortcutName + extension; var filePath = Path.Combine(WorkingDirectory, ShortcutCompleteName); @@ -262,5 +264,34 @@ private async Task CreateShortcutAsync() ShortcutCreatedSuccessfully = await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, FullPath, Arguments); } + + private void AutoFillName() + { + if (DestinationPathExists) + { + var destinationName = Path.GetFileName(FullPath); + if (DestinationPathExists) + { + destinationName = Path.GetFileName(FullPath); + + if (string.IsNullOrEmpty(FullPath)) + { + + var destinationPath = FullPath.Replace('/', '\\'); + + if (destinationPath.EndsWith('\\')) + destinationPath = destinationPath.Substring(0, destinationPath.Length - 1); + + destinationName = destinationPath.Substring(destinationPath.LastIndexOf('\\') + 1); + } + } + ShortcutName = destinationName; + } + else if (!string.IsNullOrEmpty(FullPath)) + { + var uri = new Uri(FullPath); + ShortcutName = uri.Host; + } + } } } \ No newline at end of file