mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
Add 'src/modules/launcher/' from commit '28acd466b352a807910cebbc74477d3173b31511'
git-subtree-dir: src/modules/launcher git-subtree-mainline:852689b3dfgit-subtree-split:28acd466b3
This commit is contained in:
286
src/modules/launcher/Plugins/Wox.Plugin.Everything/Main.cs
Normal file
286
src/modules/launcher/Plugins/Wox.Plugin.Everything/Main.cs
Normal file
@@ -0,0 +1,286 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.Storage;
|
||||
using Wox.Plugin.Everything.Everything;
|
||||
|
||||
namespace Wox.Plugin.Everything
|
||||
{
|
||||
public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, ISavable
|
||||
{
|
||||
public const string DLL = "Everything.dll";
|
||||
private readonly IEverythingApi _api = new EverythingApi();
|
||||
|
||||
|
||||
|
||||
private PluginInitContext _context;
|
||||
|
||||
private Settings _settings;
|
||||
private PluginJsonStorage<Settings> _storage;
|
||||
private CancellationTokenSource _cancellationTokenSource;
|
||||
|
||||
public void Save()
|
||||
{
|
||||
_storage.Save();
|
||||
}
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
_cancellationTokenSource?.Cancel(); // cancel if already exist
|
||||
var cts = _cancellationTokenSource = new CancellationTokenSource();
|
||||
var results = new List<Result>();
|
||||
if (!string.IsNullOrEmpty(query.Search))
|
||||
{
|
||||
var keyword = query.Search;
|
||||
|
||||
try
|
||||
{
|
||||
var searchList = _api.Search(keyword, cts.Token, maxCount: _settings.MaxSearchCount);
|
||||
if (searchList == null)
|
||||
{
|
||||
return results;
|
||||
}
|
||||
|
||||
foreach (var searchResult in searchList)
|
||||
{
|
||||
var r = CreateResult(keyword, searchResult);
|
||||
results.Add(r);
|
||||
}
|
||||
}
|
||||
catch (IPCErrorException)
|
||||
{
|
||||
results.Add(new Result
|
||||
{
|
||||
Title = _context.API.GetTranslation("wox_plugin_everything_is_not_running"),
|
||||
IcoPath = "Images\\warning.png"
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Exception("EverythingPlugin", "Query Error", e);
|
||||
results.Add(new Result
|
||||
{
|
||||
Title = _context.API.GetTranslation("wox_plugin_everything_query_error"),
|
||||
SubTitle = e.Message,
|
||||
Action = _ =>
|
||||
{
|
||||
Clipboard.SetText(e.Message + "\r\n" + e.StackTrace);
|
||||
_context.API.ShowMsg(_context.API.GetTranslation("wox_plugin_everything_copied"), null, string.Empty);
|
||||
return false;
|
||||
},
|
||||
IcoPath = "Images\\error.png"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private Result CreateResult(string keyword, SearchResult searchResult)
|
||||
{
|
||||
var path = searchResult.FullPath;
|
||||
|
||||
string workingDir = null;
|
||||
if (_settings.UseLocationAsWorkingDir)
|
||||
workingDir = Path.GetDirectoryName(path);
|
||||
|
||||
var r = new Result
|
||||
{
|
||||
Title = Path.GetFileName(path),
|
||||
SubTitle = path,
|
||||
IcoPath = path,
|
||||
TitleHighlightData = StringMatcher.FuzzySearch(keyword, Path.GetFileName(path)).MatchData,
|
||||
Action = c =>
|
||||
{
|
||||
bool hide;
|
||||
try
|
||||
{
|
||||
Process.Start(new ProcessStartInfo
|
||||
{
|
||||
FileName = path, UseShellExecute = true, WorkingDirectory = workingDir
|
||||
});
|
||||
hide = true;
|
||||
}
|
||||
catch (Win32Exception)
|
||||
{
|
||||
var name = $"Plugin: {_context.CurrentPluginMetadata.Name}";
|
||||
var message = "Can't open this file";
|
||||
_context.API.ShowMsg(name, message, string.Empty);
|
||||
hide = false;
|
||||
}
|
||||
|
||||
return hide;
|
||||
},
|
||||
ContextData = searchResult,
|
||||
SubTitleHighlightData = StringMatcher.FuzzySearch(keyword, path).MatchData
|
||||
};
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private List<ContextMenu> GetDefaultContextMenu()
|
||||
{
|
||||
List<ContextMenu> defaultContextMenus = new List<ContextMenu>();
|
||||
ContextMenu openFolderContextMenu = new ContextMenu
|
||||
{
|
||||
Name = _context.API.GetTranslation("wox_plugin_everything_open_containing_folder"),
|
||||
Command = "explorer.exe",
|
||||
Argument = " /select,\"{path}\"",
|
||||
ImagePath = "Images\\folder.png"
|
||||
};
|
||||
|
||||
defaultContextMenus.Add(openFolderContextMenu);
|
||||
|
||||
string editorPath = string.IsNullOrEmpty(_settings.EditorPath) ? "notepad.exe" : _settings.EditorPath;
|
||||
|
||||
ContextMenu openWithEditorContextMenu = new ContextMenu
|
||||
{
|
||||
Name = string.Format(_context.API.GetTranslation("wox_plugin_everything_open_with_editor"), Path.GetFileNameWithoutExtension(editorPath)),
|
||||
Command = editorPath,
|
||||
Argument = " \"{path}\"",
|
||||
ImagePath = editorPath
|
||||
};
|
||||
|
||||
defaultContextMenus.Add(openWithEditorContextMenu);
|
||||
|
||||
return defaultContextMenus;
|
||||
}
|
||||
|
||||
public void Init(PluginInitContext context)
|
||||
{
|
||||
_context = context;
|
||||
_storage = new PluginJsonStorage<Settings>();
|
||||
_settings = _storage.Load();
|
||||
if (_settings.MaxSearchCount <= 0)
|
||||
{
|
||||
_settings.MaxSearchCount = Settings.DefaultMaxSearchCount;
|
||||
}
|
||||
|
||||
var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory;
|
||||
const string sdk = "EverythingSDK";
|
||||
var bundledSDKDirectory = Path.Combine(pluginDirectory, sdk, CpuType());
|
||||
var sdkDirectory = Path.Combine(_storage.DirectoryPath, sdk, CpuType());
|
||||
Helper.ValidateDataDirectory(bundledSDKDirectory, sdkDirectory);
|
||||
|
||||
var sdkPath = Path.Combine(sdkDirectory, DLL);
|
||||
Constant.EverythingSDKPath = sdkPath;
|
||||
_api.Load(sdkPath);
|
||||
}
|
||||
|
||||
private static string CpuType()
|
||||
{
|
||||
return Environment.Is64BitOperatingSystem ? "x64" : "x86";
|
||||
}
|
||||
|
||||
public string GetTranslatedPluginTitle()
|
||||
{
|
||||
return _context.API.GetTranslation("wox_plugin_everything_plugin_name");
|
||||
}
|
||||
|
||||
public string GetTranslatedPluginDescription()
|
||||
{
|
||||
return _context.API.GetTranslation("wox_plugin_everything_plugin_description");
|
||||
}
|
||||
|
||||
public List<Result> LoadContextMenus(Result selectedResult)
|
||||
{
|
||||
SearchResult record = selectedResult.ContextData as SearchResult;
|
||||
List<Result> contextMenus = new List<Result>();
|
||||
if (record == null) return contextMenus;
|
||||
|
||||
List<ContextMenu> availableContextMenus = new List<ContextMenu>();
|
||||
availableContextMenus.AddRange(GetDefaultContextMenu());
|
||||
availableContextMenus.AddRange(_settings.ContextMenus);
|
||||
|
||||
if (record.Type == ResultType.File)
|
||||
{
|
||||
foreach (ContextMenu contextMenu in availableContextMenus)
|
||||
{
|
||||
var menu = contextMenu;
|
||||
contextMenus.Add(new Result
|
||||
{
|
||||
Title = contextMenu.Name,
|
||||
Action = _ =>
|
||||
{
|
||||
string argument = menu.Argument.Replace("{path}", record.FullPath);
|
||||
try
|
||||
{
|
||||
Process.Start(menu.Command, argument);
|
||||
}
|
||||
catch
|
||||
{
|
||||
_context.API.ShowMsg(string.Format(_context.API.GetTranslation("wox_plugin_everything_canot_start"), record.FullPath), string.Empty, string.Empty);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
IcoPath = contextMenu.ImagePath
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var icoPath = (record.Type == ResultType.File) ? "Images\\file.png" : "Images\\folder.png";
|
||||
contextMenus.Add(new Result
|
||||
{
|
||||
Title = _context.API.GetTranslation("wox_plugin_everything_copy_path"),
|
||||
Action = (context) =>
|
||||
{
|
||||
Clipboard.SetText(record.FullPath);
|
||||
return true;
|
||||
},
|
||||
IcoPath = icoPath
|
||||
});
|
||||
|
||||
contextMenus.Add(new Result
|
||||
{
|
||||
Title = _context.API.GetTranslation("wox_plugin_everything_copy"),
|
||||
Action = (context) =>
|
||||
{
|
||||
Clipboard.SetFileDropList(new System.Collections.Specialized.StringCollection { record.FullPath });
|
||||
return true;
|
||||
},
|
||||
IcoPath = icoPath
|
||||
});
|
||||
|
||||
if (record.Type == ResultType.File || record.Type == ResultType.Folder)
|
||||
contextMenus.Add(new Result
|
||||
{
|
||||
Title = _context.API.GetTranslation("wox_plugin_everything_delete"),
|
||||
Action = (context) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (record.Type == ResultType.File)
|
||||
System.IO.File.Delete(record.FullPath);
|
||||
else
|
||||
System.IO.Directory.Delete(record.FullPath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
_context.API.ShowMsg(string.Format(_context.API.GetTranslation("wox_plugin_everything_canot_delete"), record.FullPath), string.Empty, string.Empty);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
IcoPath = icoPath
|
||||
});
|
||||
|
||||
return contextMenus;
|
||||
}
|
||||
|
||||
public Control CreateSettingPanel()
|
||||
{
|
||||
return new EverythingSettings(_settings);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user