From a0eaf077de936d64173d78f6715cde7e6ea5d6ac Mon Sep 17 00:00:00 2001 From: Divyansh Srivastava Date: Fri, 4 Sep 2020 15:12:04 -0700 Subject: [PATCH] [Pt Run] Show context menu for first folder plugin result (#6301) * Added context menu to first folder result * Added context menu to first folder result * Add localization for string in folder plugin * Fixed issue with context menu not showing on first selected item * Add exception logging --- .../Plugins/Microsoft.Plugin.Folder/Main.cs | 51 +++++++++++-------- .../Properties/Resources.Designer.cs | 27 ++++++++++ .../Properties/Resources.resx | 9 ++++ .../PowerLauncher/PublicAPIInstance.cs | 4 ++ .../PowerLauncher/ViewModel/MainViewModel.cs | 1 + 5 files changed, 71 insertions(+), 21 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Main.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Main.cs index b5b648985c..54ed650d24 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Main.cs @@ -11,8 +11,12 @@ using System.Linq; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Controls; +using System.Windows.Media; using Microsoft.PowerToys.Settings.UI.Lib; +using Microsoft.VisualBasic; +using Newtonsoft.Json; using Wox.Infrastructure; +using Wox.Infrastructure.Logger; using Wox.Infrastructure.Storage; using Wox.Plugin; @@ -106,6 +110,23 @@ namespace Microsoft.Plugin.Folder return results; } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive and instead inform the user of the error")] + private static bool OpenFileOrFolder(string program, string path) + { + try + { + Process.Start(program, path); + } + catch (Exception e) + { + string messageBoxTitle = string.Format(CultureInfo.InvariantCulture, "{0} {1}", Properties.Resources.wox_plugin_folder_select_folder_OpenFileOrFolder_error_message, path); + Log.Exception($"|Microsoft.Plugin.Folder.Main.OpenFileOrFolder| Failed to open {path} in explorer, {e.Message}", e); + _context.API.ShowMsg(messageBoxTitle, e.Message); + } + + return true; + } + private static bool IsDriveOrSharedFolder(string search) { if (search == null) @@ -137,14 +158,13 @@ namespace Microsoft.Plugin.Folder { Title = title, IcoPath = path, - SubTitle = "Folder: " + subtitle, + SubTitle = string.Format(CultureInfo.InvariantCulture, "{0}: {1}", Properties.Resources.wox_plugin_folder_plugin_name, subtitle), QueryTextDisplay = path, TitleHighlightData = StringMatcher.FuzzySearch(query.Search, title).MatchData, ContextData = new SearchResult { Type = ResultType.Folder, FullPath = path }, Action = c => { - Process.Start(_fileExplorerProgramName, path); - return true; + return OpenFileOrFolder(_fileExplorerProgramName, path); }, }; } @@ -297,37 +317,26 @@ namespace Microsoft.Plugin.Folder }; } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alve and instead inform the user of the error")] private static Result CreateFileResult(string filePath, Query query) { var result = new Result { Title = Path.GetFileName(filePath), - SubTitle = "Folder: " + filePath, + SubTitle = string.Format(CultureInfo.InvariantCulture, "{0}: {1}", Properties.Resources.wox_plugin_folder_plugin_name, filePath), IcoPath = filePath, + ContextData = new SearchResult { Type = ResultType.File, FullPath = filePath }, TitleHighlightData = StringMatcher.FuzzySearch(query.Search, Path.GetFileName(filePath)).MatchData, Action = c => { - try - { - Process.Start(_fileExplorerProgramName, filePath); - } - catch (Exception ex) - { - MessageBox.Show(ex.Message, "Could not start " + filePath); - } - - return true; + return OpenFileOrFolder(_fileExplorerProgramName, filePath); }, - ContextData = new SearchResult { Type = ResultType.File, FullPath = filePath }, }; return result; } private static Result CreateOpenCurrentFolderResult(string search) { - var firstResult = "Open " + search; - + var firstResult = string.Format(CultureInfo.InvariantCulture, "{0} {1}", Properties.Resources.wox_plugin_folder_select_folder_first_result_title, search); var folderName = search.TrimEnd('\\').Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.None).Last(); var sanitizedPath = Regex.Replace(search, @"[\/\\]+", "\\"); @@ -341,13 +350,13 @@ namespace Microsoft.Plugin.Folder { Title = firstResult, QueryTextDisplay = search, - SubTitle = $"Folder: Use > to search within the directory. Use * to search for file extensions. Or use both >*.", + SubTitle = string.Format(CultureInfo.InvariantCulture, "{0}: {1}", Properties.Resources.wox_plugin_folder_plugin_name, Properties.Resources.wox_plugin_folder_select_folder_first_result_subtitle), IcoPath = search, Score = 500, + ContextData = new SearchResult { Type = ResultType.Folder, FullPath = search }, Action = c => { - Process.Start(_fileExplorerProgramName, sanitizedPath); - return true; + return OpenFileOrFolder(_fileExplorerProgramName, search); }, }; } diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Properties/Resources.Designer.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Properties/Resources.Designer.cs index 1ea1ca1017..29233369cf 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Properties/Resources.Designer.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Properties/Resources.Designer.cs @@ -177,6 +177,24 @@ namespace Microsoft.Plugin.Folder.Properties { } } + /// + /// Looks up a localized string similar to Use > to search within the directory. Use * to search for file extensions. Or use both >*. + /// + public static string wox_plugin_folder_select_folder_first_result_subtitle { + get { + return ResourceManager.GetString("wox_plugin_folder_select_folder_first_result_subtitle", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Open. + /// + public static string wox_plugin_folder_select_folder_first_result_title { + get { + return ResourceManager.GetString("wox_plugin_folder_select_folder_first_result_title", resourceCulture); + } + } + /// /// Looks up a localized string similar to Please select a folder link. /// @@ -185,5 +203,14 @@ namespace Microsoft.Plugin.Folder.Properties { return ResourceManager.GetString("wox_plugin_folder_select_folder_link_warning", resourceCulture); } } + + /// + /// Looks up a localized string similar to Could not start. + /// + public static string wox_plugin_folder_select_folder_OpenFileOrFolder_error_message { + get { + return ResourceManager.GetString("wox_plugin_folder_select_folder_OpenFileOrFolder_error_message", resourceCulture); + } + } } } diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Properties/Resources.resx b/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Properties/Resources.resx index af9e4acd64..7a6d1f35ee 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Properties/Resources.resx +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Folder/Properties/Resources.resx @@ -159,4 +159,13 @@ Fail to set text in clipboard + + Use > to search within the directory. Use * to search for file extensions. Or use both >* + + + Open + + + Could not start + \ No newline at end of file diff --git a/src/modules/launcher/PowerLauncher/PublicAPIInstance.cs b/src/modules/launcher/PowerLauncher/PublicAPIInstance.cs index 4171d6f9d1..41f7a39314 100644 --- a/src/modules/launcher/PowerLauncher/PublicAPIInstance.cs +++ b/src/modules/launcher/PowerLauncher/PublicAPIInstance.cs @@ -75,6 +75,10 @@ namespace Wox public void ShowMsg(string title, string subTitle = "", string iconPath = "", bool useMainWindowAsOwner = true) { + Application.Current.Dispatcher.Invoke(() => + { + MessageBox.Show(subTitle, title); + }); } public void InstallPlugin(string path) diff --git a/src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs b/src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs index 01654c48c4..36130cd7e8 100644 --- a/src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs +++ b/src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs @@ -599,6 +599,7 @@ namespace PowerLauncher.ViewModel if (!isDelayedInvoke) { Results.SelectedIndex = 0; + Results.SelectedItem = Results.Results[0]; } } else