diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml b/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml index 7d0a553a486..c4d6321e4e5 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml @@ -22,6 +22,8 @@ Error occurred during search: {0} Could not open folder Could not open file + Select type + Do you want to select a file?{0}{0}Click 'Yes' for file, 'No' for folder. Delete diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Views/QuickAccessLinkSettings.xaml.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Views/QuickAccessLinkSettings.xaml.cs index 36a00e9e59f..1016282ecb7 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Views/QuickAccessLinkSettings.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Views/QuickAccessLinkSettings.xaml.cs @@ -1,11 +1,13 @@ using System; using System.Collections.ObjectModel; using System.ComponentModel; +using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Forms; using Flow.Launcher.Plugin.Explorer.Helper; +using Flow.Launcher.Plugin.Explorer.Search; using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks; namespace Flow.Launcher.Plugin.Explorer.Views; @@ -49,7 +51,8 @@ public string SelectedName private bool IsEdit { get; } private AccessLink SelectedAccessLink { get; } - + private ResultType AccessLinkType { get; set; } = ResultType.Folder; + public ObservableCollection QuickAccessLinks { get; } public QuickAccessLinkSettings(ObservableCollection quickAccessLinks) @@ -65,6 +68,7 @@ public QuickAccessLinkSettings(ObservableCollection quickAccessLinks _selectedName = selectedAccessLink.Name; _selectedPath = selectedAccessLink.Path; SelectedAccessLink = selectedAccessLink; + AccessLinkType = selectedAccessLink.Type; QuickAccessLinks = quickAccessLinks; InitializeComponent(); } @@ -96,30 +100,42 @@ private void OnDoneButtonClick(object sender, RoutedEventArgs e) } // If editing, update the existing link - if (IsEdit) + if (IsEdit) { - if (SelectedAccessLink == null) return; - - var index = QuickAccessLinks.IndexOf(SelectedAccessLink); - if (index >= 0) + if (SelectedAccessLink != null) { - var updatedLink = new AccessLink + var index = QuickAccessLinks.IndexOf(SelectedAccessLink); + if (index >= 0) { - Name = SelectedName, - Type = SelectedAccessLink.Type, - Path = SelectedPath - }; - QuickAccessLinks[index] = updatedLink; + var updatedLink = new AccessLink + { + Name = SelectedName, + Type = AccessLinkType, + Path = SelectedPath + }; + QuickAccessLinks[index] = updatedLink; + } + DialogResult = true; + Close(); + } + // Add a new one if the selected access link is null (should not happen in edit mode, but just in case) + else + { + AddNewAccessLink(); } - DialogResult = true; - Close(); } // Otherwise, add a new one else + { + AddNewAccessLink(); + } + + void AddNewAccessLink() { var newAccessLink = new AccessLink { Name = SelectedName, + Type = AccessLinkType, Path = SelectedPath }; QuickAccessLinks.Add(newAccessLink); @@ -130,12 +146,79 @@ private void OnDoneButtonClick(object sender, RoutedEventArgs e) private void SelectPath_OnClick(object commandParameter, RoutedEventArgs e) { - var folderBrowserDialog = new FolderBrowserDialog(); + // If we are editing an existing access link, we should keep the file/folder type consistent. + ResultType type; + if (SelectedAccessLink != null) + { + type = SelectedAccessLink.Type; + } + // Else we should allow the user to select any file or folder. + else + { + var result = Main.Context.API.ShowMsgBox( + string.Format( + Main.Context.API.GetTranslation("plugin_explorer_quickAccessLinkFileOrFolderSubtitle"), + Environment.NewLine), + Main.Context.API.GetTranslation("plugin_explorer_quickAccessLinkFileOrFolderTitle"), + MessageBoxButton.YesNo, MessageBoxImage.Question); + type = result == MessageBoxResult.Yes ? ResultType.File : ResultType.Folder; + } - if (folderBrowserDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) - return; + if (type == ResultType.File) + { + var dialog = new OpenFileDialog + { + Multiselect = false, + CheckFileExists = true, + CheckPathExists = true + }; + + if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK || + string.IsNullOrEmpty(dialog.FileName)) + return; + + SelectedPath = dialog.FileName; + } + else + { + var dialog = new FolderBrowserDialog + { + ShowNewFolderButton = true + }; + + if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK || + string.IsNullOrEmpty(dialog.SelectedPath)) + return; - SelectedPath = folderBrowserDialog.SelectedPath; + SelectedPath = dialog.SelectedPath; + } + + AccessLinkType = GetResultType(SelectedPath); + } + + private static ResultType GetResultType(string path) + { + // Check if the path is a file or folder + if (File.Exists(path)) + { + return ResultType.File; + } + else if (Directory.Exists(path)) + { + if (string.Equals(Path.GetPathRoot(path), path, StringComparison.OrdinalIgnoreCase)) + { + return ResultType.Volume; + } + else + { + return ResultType.Folder; + } + } + else + { + // This should not happen, but just in case, we assume it's a folder + return ResultType.Folder; + } } public event PropertyChangedEventHandler PropertyChanged;