Flow.Launcher/Plugins/Flow.Launcher.Plugin.Explorer/Main.cs

133 lines
4.6 KiB
C#
Raw Permalink Normal View History

using Flow.Launcher.Plugin.Explorer.Helper;
2020-05-19 12:38:42 +00:00
using Flow.Launcher.Plugin.Explorer.Search;
using Flow.Launcher.Plugin.Explorer.Search.Everything;
using Flow.Launcher.Plugin.Explorer.ViewModels;
using Flow.Launcher.Plugin.Explorer.Views;
using System;
using System.Collections.Generic;
using System.IO;
2021-01-02 14:01:15 +00:00
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using Flow.Launcher.Plugin.Explorer.Exceptions;
using System.Linq;
using System.Globalization;
namespace Flow.Launcher.Plugin.Explorer
{
public class Main : ISettingProvider, IAsyncPlugin, IContextMenu, IPluginI18n, IAsyncDialogJump
{
internal static PluginInitContext Context { get; set; }
internal static Settings Settings { get; set; }
private SettingsViewModel viewModel;
private ContextMenu contextMenu;
2020-05-25 08:58:04 +00:00
2021-01-02 14:01:15 +00:00
private SearchManager searchManager;
private static readonly List<DialogJumpResult> _emptyDialogJumpResultList = new();
public Control CreateSettingPanel()
{
return new ExplorerSettings(viewModel);
}
public Task InitAsync(PluginInitContext context)
{
Context = context;
2022-08-17 01:32:49 +00:00
Settings = context.API.LoadSettingJsonStorage<Settings>();
2025-06-07 05:54:15 +00:00
FillQuickAccessLinkNames();
viewModel = new SettingsViewModel(context, Settings);
contextMenu = new ContextMenu(Context, Settings);
2021-01-02 14:01:15 +00:00
searchManager = new SearchManager(Settings, Context);
2021-07-02 03:38:07 +00:00
ResultManager.Init(Context, Settings);
2022-08-17 01:32:49 +00:00
EverythingApiDllImport.Load(Path.Combine(Context.CurrentPluginMetadata.PluginDirectory, "EverythingSDK",
Environment.Is64BitProcess ? "x64" : "x86"));
return Task.CompletedTask;
}
public List<Result> LoadContextMenus(Result selectedResult)
{
return contextMenu.LoadContextMenus(selectedResult);
}
2021-01-02 14:01:15 +00:00
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
{
try
{
return await searchManager.SearchAsync(query, token);
}
catch (Exception e) when (e is SearchException or EngineNotAvailableException)
{
return new List<Result>
{
new()
{
Title = e.Message,
SubTitle = e is EngineNotAvailableException { Resolution: { } resolution }
? resolution
: "Enter to copy the message to clipboard",
Score = 501,
IcoPath = e is EngineNotAvailableException { ErrorIcon: { } iconPath }
? iconPath
: Constants.GeneralSearchErrorImagePath,
AsyncAction = e is EngineNotAvailableException {Action: { } action}
? action
: _ =>
{
2023-06-09 09:59:49 +00:00
Context.API.CopyToClipboard(e.ToString());
return new ValueTask<bool>(true);
}
}
};
}
}
2020-06-08 10:31:48 +00:00
public string GetTranslatedPluginTitle()
{
return Localize.plugin_explorer_plugin_name();
2020-06-08 10:31:48 +00:00
}
public string GetTranslatedPluginDescription()
{
return Localize.plugin_explorer_plugin_description();
2020-06-08 10:31:48 +00:00
}
2025-05-25 17:45:44 +00:00
public void OnCultureInfoChanged(CultureInfo newCulture)
{
// Update labels for setting view model
EverythingSortOptionLocalized.UpdateLabels(viewModel.AllEverythingSortOptions);
}
2025-07-08 01:45:54 +00:00
private static void FillQuickAccessLinkNames()
2025-05-25 17:45:44 +00:00
{
2025-06-07 05:54:15 +00:00
// Legacy version does not have names for quick access links, so we fill them with the path name.
2025-05-25 17:45:44 +00:00
foreach (var link in Settings.QuickAccessLinks)
{
if (string.IsNullOrWhiteSpace(link.Name))
{
link.Name = link.Path.GetPathName();
}
}
}
public async Task<List<DialogJumpResult>> QueryDialogJumpAsync(Query query, CancellationToken token)
{
try
{
var results = await searchManager.SearchAsync(query, token);
return results.Select(r => DialogJumpResult.From(r, r.CopyText)).ToList();
}
catch (Exception e) when (e is SearchException or EngineNotAvailableException)
{
return _emptyDialogJumpResultList;
}
}
}
}