2020-08-11 04:28:22 +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 ;
2020-05-06 00:02:28 +08:00
using System.Reflection ;
2020-08-11 04:28:22 +08:00
using System.Windows ;
2020-06-02 03:35:37 +08:00
using System.Windows.Input ;
2020-07-09 00:56:26 +08:00
using Wox.Infrastructure ;
2020-08-11 04:28:22 +08:00
using Wox.Infrastructure.Logger ;
using Wox.Plugin ;
namespace Microsoft.Plugin.Folder
{
internal class ContextMenuLoader : IContextMenu
{
private readonly PluginInitContext _context ;
2020-05-06 00:02:28 +08:00
2020-08-11 04:28:22 +08:00
public ContextMenuLoader ( PluginInitContext context )
{
_context = context ;
2020-07-24 07:05:36 +08:00
}
[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")]
2020-08-11 04:28:22 +08:00
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 ,
Title = _context . API . GetTranslation ( "Microsoft_plugin_folder_copy_path" ) ,
Glyph = "\xE8C8" ,
FontFamily = "Segoe MDL2 Assets" ,
2020-06-02 03:35:37 +08:00
AcceleratorKey = Key . C ,
2020-08-11 04:28:22 +08:00
AcceleratorModifiers = ModifierKeys . Control ,
Action = ( context ) = >
{
try
{
Clipboard . SetText ( record . FullPath ) ;
return true ;
}
catch ( Exception e )
{
var message = "Fail to set text in clipboard" ;
LogException ( message , e ) ;
_context . API . ShowMsg ( message ) ;
return false ;
}
} ,
} ) ;
contextMenus . Add ( new ContextMenuResult
{
PluginName = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ,
Title = _context . API . GetTranslation ( "Microsoft_plugin_folder_open_in_console" ) ,
Glyph = "\xE756" ,
FontFamily = "Segoe MDL2 Assets" ,
2020-07-09 00:56:26 +08:00
AcceleratorKey = Key . C ,
2020-08-11 04:28:22 +08:00
AcceleratorModifiers = ModifierKeys . Control | ModifierKeys . Shift ,
Action = ( context ) = >
{
try
{
if ( record . Type = = ResultType . File )
{
2020-07-09 00:56:26 +08:00
Helper . OpenInConsole ( Path . GetDirectoryName ( record . FullPath ) ) ;
2020-08-11 04:28:22 +08:00
}
else
{
2020-07-09 00:56:26 +08:00
Helper . OpenInConsole ( record . FullPath ) ;
2020-08-11 04:28:22 +08:00
}
return true ;
}
catch ( Exception e )
{
Log . Exception ( $"|Microsoft.Plugin.Folder.ContextMenuLoader.LoadContextMenus| Failed to open {record.FullPath} in console, {e.Message}" , e ) ;
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 ,
Title = _context . API . GetTranslation ( "Microsoft_plugin_folder_open_containing_folder" ) ,
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 )
{
var message = $"Fail to open file at {record.FullPath}" ;
LogException ( message , e ) ;
_context . API . ShowMsg ( message ) ;
return false ;
}
return true ;
} ,
} ;
}
public static void LogException ( string message , Exception e )
{
Log . Exception ( $"|Microsoft.Plugin.Folder.ContextMenu|{message}" , e ) ;
}
}
public enum ResultType
{
Volume ,
Folder ,
File ,
}
}