Flow.Launcher/Plugins/Flow.Launcher.Plugin.Explorer/Search/ResultManager.cs

211 lines
7.8 KiB
C#
Raw Normal View History

using Flow.Launcher.Infrastructure;
using Flow.Launcher.Plugin.SharedCommands;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace Flow.Launcher.Plugin.Explorer.Search
{
public static class ResultManager
{
private static PluginInitContext Context;
2021-07-02 03:38:07 +00:00
private static Settings Settings { get; set; }
2021-07-02 03:38:07 +00:00
public static void Init(PluginInitContext context, Settings settings)
{
Context = context;
2021-07-02 03:38:07 +00:00
Settings = settings;
2021-12-06 21:57:06 +00:00
}
2021-12-07 21:01:16 +00:00
private static string GetPathWithActionKeyword(string path, ResultType type)
2021-12-06 21:57:06 +00:00
{
2021-12-07 21:01:16 +00:00
// one of it is enabled
var keyword = Settings.SearchActionKeywordEnabled ? Settings.SearchActionKeyword : Settings.PathSearchActionKeyword;
keyword = keyword == Query.GlobalPluginWildcardSign ? string.Empty : keyword + " ";
var formatted_path = path;
if (type == ResultType.Folder)
formatted_path = path.EndsWith(Constants.DirectorySeperator) ? path : path + Constants.DirectorySeperator;
return $"{keyword}{formatted_path}";
}
2021-04-13 11:40:04 +00:00
internal static Result CreateFolderResult(string title, string subtitle, string path, Query query, int score = 0, bool showIndexState = false, bool windowsIndexed = false)
{
return new Result
{
Title = title,
IcoPath = path,
SubTitle = subtitle,
2021-12-07 21:01:16 +00:00
AutoCompleteText = GetPathWithActionKeyword(path, ResultType.Folder),
TitleHighlightData = StringMatcher.FuzzySearch(query.Search, title).MatchData,
Action = c =>
{
2021-07-03 04:45:00 +00:00
if (c.SpecialKeyState.CtrlPressed || (!Settings.PathSearchKeywordEnabled && !Settings.SearchActionKeywordEnabled))
{
try
{
Context.API.OpenDirectory(path);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + path);
return false;
}
}
2021-12-07 21:01:16 +00:00
Context.API.ChangeQuery(GetPathWithActionKeyword(path, ResultType.Folder));
return false;
},
2021-04-13 11:40:04 +00:00
Score = score,
TitleToolTip = Constants.ToolTipOpenDirectory,
SubTitleToolTip = path,
2021-07-02 03:38:07 +00:00
ContextData = new SearchResult
{
Type = ResultType.Folder,
FullPath = path,
ShowIndexState = showIndexState,
WindowsIndexed = windowsIndexed
}
};
}
internal static Result CreateOpenCurrentFolderResult(string path, bool windowsIndexed = false)
{
2020-06-02 10:21:28 +00:00
var retrievedDirectoryPath = FilesFolders.ReturnPreviousDirectoryIfIncompleteString(path);
2021-07-02 03:38:07 +00:00
var folderName = retrievedDirectoryPath.TrimEnd(Constants.DirectorySeperator).Split(new[]
{
Path.DirectorySeparatorChar
}, StringSplitOptions.None).Last();
if (retrievedDirectoryPath.EndsWith(":\\"))
{
var driveLetter = path.Substring(0, 1).ToUpper();
folderName = driveLetter + " drive";
}
var title = "Open current directory";
if (retrievedDirectoryPath != path)
title = "Open " + folderName;
2021-04-13 11:40:04 +00:00
var subtitleFolderName = folderName;
2021-04-13 11:40:04 +00:00
// ie. max characters can be displayed without subtitle cutting off: "Program Files (x86)"
if (folderName.Length > 19)
2020-06-02 03:26:33 +00:00
subtitleFolderName = "the directory";
return new Result
{
Title = title,
SubTitle = $"Use > to search within {subtitleFolderName}, " +
2021-07-02 03:38:07 +00:00
$"* to search for file extensions or >* to combine both searches.",
2021-12-07 21:01:16 +00:00
AutoCompleteText = GetPathWithActionKeyword(retrievedDirectoryPath, ResultType.Folder),
IcoPath = retrievedDirectoryPath,
Score = 500,
Action = c =>
{
Context.API.OpenDirectory(retrievedDirectoryPath);
return true;
},
TitleToolTip = retrievedDirectoryPath,
SubTitleToolTip = retrievedDirectoryPath,
2021-07-02 03:38:07 +00:00
ContextData = new SearchResult
{
Type = ResultType.Folder,
FullPath = retrievedDirectoryPath,
ShowIndexState = true,
WindowsIndexed = windowsIndexed
}
};
}
2021-04-13 11:40:04 +00:00
internal static Result CreateFileResult(string filePath, Query query, int score = 0, bool showIndexState = false, bool windowsIndexed = false)
{
var result = new Result
{
Title = Path.GetFileName(filePath),
SubTitle = filePath,
IcoPath = filePath,
2021-12-07 21:01:16 +00:00
AutoCompleteText = GetPathWithActionKeyword(filePath, ResultType.File),
TitleHighlightData = StringMatcher.FuzzySearch(query.Search, Path.GetFileName(filePath)).MatchData,
2021-04-13 11:40:04 +00:00
Score = score,
Action = c =>
{
try
{
if (File.Exists(filePath) && c.SpecialKeyState.CtrlPressed && c.SpecialKeyState.ShiftPressed)
{
Task.Run(() =>
{
try
{
Process.Start(new ProcessStartInfo
{
FileName = filePath,
UseShellExecute = true,
Verb = "runas",
});
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Could not start " + filePath);
}
});
}
else if (c.SpecialKeyState.CtrlPressed)
{
Context.API.OpenDirectory(Path.GetDirectoryName(filePath), filePath);
}
else
{
FilesFolders.OpenPath(filePath);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + filePath);
}
return true;
},
TitleToolTip = Constants.ToolTipOpenContainingFolder,
SubTitleToolTip = filePath,
2021-07-02 03:38:07 +00:00
ContextData = new SearchResult
{
Type = ResultType.File,
FullPath = filePath,
ShowIndexState = showIndexState,
WindowsIndexed = windowsIndexed
}
};
return result;
}
}
2020-05-25 08:55:50 +00:00
internal class SearchResult
{
public string FullPath { get; set; }
public ResultType Type { get; set; }
public bool WindowsIndexed { get; set; }
public bool ShowIndexState { get; set; }
}
2021-01-26 09:01:12 +00:00
public enum ResultType
{
Volume,
Folder,
File
}
2021-07-02 03:38:07 +00:00
}