2020-08-18 01:00:56 +08:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.IO ;
using System.Reflection ;
using System.Windows ;
using System.Windows.Input ;
using Wox.Infrastructure ;
using Wox.Plugin ;
2020-10-24 04:06:22 +08:00
using Wox.Plugin.Logger ;
2020-08-18 01:00:56 +08:00
namespace Microsoft.Plugin.Folder
{
internal class ContextMenuLoader : IContextMenu
{
private readonly PluginInitContext _context ;
public ContextMenuLoader ( PluginInitContext context )
{
_context = context ;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive, and instead log the exception")]
public List < ContextMenuResult > LoadContextMenus ( Result selectedResult )
{
var contextMenus = new List < ContextMenuResult > ( ) ;
if ( selectedResult . ContextData is SearchResult record )
{
if ( record . Type = = ResultType . File )
{
contextMenus . Add ( CreateOpenContainingFolderResult ( record ) ) ;
}
var icoPath = ( record . Type = = ResultType . File ) ? Main . FileImagePath : Main . FolderImagePath ;
contextMenus . Add ( new ContextMenuResult
{
PluginName = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ,
2020-09-03 06:24:59 +08:00
Title = Properties . Resources . Microsoft_plugin_folder_copy_path ,
2020-08-18 01:00:56 +08:00
Glyph = "\xE8C8" ,
FontFamily = "Segoe MDL2 Assets" ,
AcceleratorKey = Key . C ,
AcceleratorModifiers = ModifierKeys . Control ,
Action = ( context ) = >
{
try
{
Clipboard . SetText ( record . FullPath ) ;
return true ;
}
catch ( Exception e )
{
2020-09-03 06:24:59 +08:00
var message = Properties . Resources . Microsoft_plugin_folder_clipboard_failed ;
2020-09-24 07:32:06 +08:00
Log . Exception ( message , e , GetType ( ) ) ;
2020-08-18 01:00:56 +08:00
_context . API . ShowMsg ( message ) ;
return false ;
}
} ,
} ) ;
contextMenus . Add ( new ContextMenuResult
{
PluginName = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ,
2020-09-03 06:24:59 +08:00
Title = Properties . Resources . Microsoft_plugin_folder_open_in_console ,
2020-08-18 01:00:56 +08:00
Glyph = "\xE756" ,
FontFamily = "Segoe MDL2 Assets" ,
AcceleratorKey = Key . C ,
AcceleratorModifiers = ModifierKeys . Control | ModifierKeys . Shift ,
Action = ( context ) = >
{
try
{
if ( record . Type = = ResultType . File )
{
Helper . OpenInConsole ( Path . GetDirectoryName ( record . FullPath ) ) ;
}
else
{
Helper . OpenInConsole ( record . FullPath ) ;
}
return true ;
}
catch ( Exception e )
{
2020-09-24 07:32:06 +08:00
Log . Exception ( $"Failed to open {record.FullPath} in console, {e.Message}" , e , GetType ( ) ) ;
2020-08-18 01:00:56 +08:00
return false ;
}
} ,
} ) ;
}
return contextMenus ;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive, and instead log the exception")]
private ContextMenuResult CreateOpenContainingFolderResult ( SearchResult record )
{
return new ContextMenuResult
{
PluginName = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ,
2020-09-03 06:24:59 +08:00
Title = Properties . Resources . Microsoft_plugin_folder_open_containing_folder ,
2020-08-18 01:00:56 +08:00
Glyph = "\xE838" ,
FontFamily = "Segoe MDL2 Assets" ,
AcceleratorKey = Key . E ,
AcceleratorModifiers = ModifierKeys . Control | ModifierKeys . Shift ,
Action = _ = >
{
try
{
Process . Start ( "explorer.exe" , $" /select,\" { record . FullPath } \ "" ) ;
}
catch ( Exception e )
{
2020-09-09 02:51:24 +08:00
var message = $"{Properties.Resources.Microsoft_plugin_folder_file_open_failed} {record.FullPath}" ;
2020-09-24 07:32:06 +08:00
Log . Exception ( message , e , GetType ( ) ) ;
2020-08-18 01:00:56 +08:00
_context . API . ShowMsg ( message ) ;
2020-09-24 07:32:06 +08:00
2020-08-18 01:00:56 +08:00
return false ;
}
return true ;
} ,
} ;
}
}
public enum ResultType
{
Volume ,
Folder ,
File ,
}
}