[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
This commit is contained in:
Divyansh Srivastava 2020-09-04 15:12:04 -07:00 committed by GitHub
parent 726f94e2a2
commit a0eaf077de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 21 deletions

View File

@ -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);
},
};
}

View File

@ -177,6 +177,24 @@ namespace Microsoft.Plugin.Folder.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Use &gt; to search within the directory. Use * to search for file extensions. Or use both &gt;*.
/// </summary>
public static string wox_plugin_folder_select_folder_first_result_subtitle {
get {
return ResourceManager.GetString("wox_plugin_folder_select_folder_first_result_subtitle", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Open.
/// </summary>
public static string wox_plugin_folder_select_folder_first_result_title {
get {
return ResourceManager.GetString("wox_plugin_folder_select_folder_first_result_title", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Please select a folder link.
/// </summary>
@ -185,5 +203,14 @@ namespace Microsoft.Plugin.Folder.Properties {
return ResourceManager.GetString("wox_plugin_folder_select_folder_link_warning", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Could not start.
/// </summary>
public static string wox_plugin_folder_select_folder_OpenFileOrFolder_error_message {
get {
return ResourceManager.GetString("wox_plugin_folder_select_folder_OpenFileOrFolder_error_message", resourceCulture);
}
}
}
}

View File

@ -159,4 +159,13 @@
<data name="Microsoft_plugin_folder_clipboard_failed" xml:space="preserve">
<value>Fail to set text in clipboard</value>
</data>
<data name="wox_plugin_folder_select_folder_first_result_subtitle" xml:space="preserve">
<value>Use &gt; to search within the directory. Use * to search for file extensions. Or use both &gt;*</value>
</data>
<data name="wox_plugin_folder_select_folder_first_result_title" xml:space="preserve">
<value>Open</value>
</data>
<data name="wox_plugin_folder_select_folder_OpenFileOrFolder_error_message" xml:space="preserve">
<value>Could not start</value>
</data>
</root>

View File

@ -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)

View File

@ -599,6 +599,7 @@ namespace PowerLauncher.ViewModel
if (!isDelayedInvoke)
{
Results.SelectedIndex = 0;
Results.SelectedItem = Results.Results[0];
}
}
else