Skip to content

Commit 9debba9

Browse files
TheBestPessimistTheBestPessimist
authored andcommitted
centralize the 3 actions: open file, open folder, open file as admin, for simpler logic
1 parent 2be108d commit 9debba9

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

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

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ internal static Result CreateFolderResult(string title, string subtitle, string
7070
{
7171
return new Result
7272
{
73-
Title = title + addScoreInDebug(score),
73+
Title = title + _addScoreInDebug(score),
7474
IcoPath = path,
7575
SubTitle = subtitle,
7676
AutoCompleteText = GetAutoCompleteText(title, query, path, ResultType.Folder),
@@ -83,8 +83,7 @@ internal static Result CreateFolderResult(string title, string subtitle, string
8383
{
8484
try
8585
{
86-
IncrementRunCounterIfNeeded(path);
87-
Context.API.OpenDirectory(path);
86+
_openFolder(path);
8887
return true;
8988
}
9089
catch (Exception ex)
@@ -133,7 +132,7 @@ internal static Result CreateDriveSpaceDisplayResult(string path, string actionK
133132
ProgressBarColor = progressBarColor,
134133
Action = c =>
135134
{
136-
Context.API.OpenDirectory(path);
135+
_openFolder(path);
137136
return true;
138137
},
139138
TitleToolTip = path,
@@ -194,7 +193,7 @@ internal static Result CreateOpenCurrentFolderResult(string path, string actionK
194193
CopyText = folderPath,
195194
Action = _ =>
196195
{
197-
Context.API.OpenDirectory(folderPath);
196+
_openFolder(folderPath);
198197
return true;
199198
},
200199
ContextData = new SearchResult { Type = ResultType.Folder, FullPath = folderPath, WindowsIndexed = windowsIndexed }
@@ -207,7 +206,7 @@ internal static Result CreateFileResult(string filePath, Query query, int score
207206
? new Result.PreviewInfo { IsMedia = true, PreviewImagePath = filePath, }
208207
: Result.PreviewInfo.Default;
209208

210-
var title = Path.GetFileName(filePath) + addScoreInDebug(score);
209+
var title = Path.GetFileName(filePath) + _addScoreInDebug(score);
211210

212211
var result = new Result
213212
{
@@ -226,21 +225,15 @@ internal static Result CreateFileResult(string filePath, Query query, int score
226225
// TODO Why do we check if file exists here, but not in the other if conditions?
227226
if (File.Exists(filePath) && c.SpecialKeyState.CtrlPressed && c.SpecialKeyState.ShiftPressed)
228227
{
229-
// run the file as admin
230-
IncrementRunCounterIfNeeded(filePath);
231-
OpenFileAsAdmin(filePath);
228+
_openFileAsAdmin(filePath);
232229
}
233230
else if (c.SpecialKeyState.CtrlPressed)
234231
{
235-
// open folder and select this file
236-
IncrementRunCounterIfNeeded(filePath);
237-
Context.API.OpenDirectory(Path.GetDirectoryName(filePath), filePath);
232+
_openFolder(filePath, filePath);
238233
}
239234
else
240235
{
241-
// run the file
242-
IncrementRunCounterIfNeeded(filePath);
243-
FilesFolders.OpenPath(filePath);
236+
_openFile(filePath);
244237
}
245238
}
246239
catch (Exception ex)
@@ -257,12 +250,37 @@ internal static Result CreateFileResult(string filePath, Query query, int score
257250
return result;
258251
}
259252

260-
private static void OpenFileAsAdmin(string filePath)
253+
public static bool IsMedia(string extension)
254+
{
255+
if (string.IsNullOrEmpty(extension))
256+
{
257+
return false;
258+
}
259+
else
260+
{
261+
return MediaExtensions.Contains(extension.ToLowerInvariant());
262+
}
263+
}
264+
265+
private static void _openFile(string filePath)
266+
{
267+
_incrementEverythingRunCounterIfNeeded(filePath);
268+
FilesFolders.OpenPath(filePath);
269+
}
270+
271+
private static void _openFolder(string folderPath, string fileNameOrFilePath=null)
272+
{
273+
_incrementEverythingRunCounterIfNeeded(folderPath);
274+
Context.API.OpenDirectory(Path.GetDirectoryName(folderPath), fileNameOrFilePath);
275+
}
276+
277+
private static void _openFileAsAdmin(string filePath)
261278
{
262279
_ = Task.Run(() =>
263280
{
264281
try
265282
{
283+
_incrementEverythingRunCounterIfNeeded(filePath);
266284
Process.Start(new ProcessStartInfo
267285
{
268286
FileName = filePath,
@@ -278,25 +296,13 @@ private static void OpenFileAsAdmin(string filePath)
278296
});
279297
}
280298

281-
private static void IncrementRunCounterIfNeeded(string fileOrFolder)
299+
private static void _incrementEverythingRunCounterIfNeeded(string fileOrFolder)
282300
{
283301
if (Settings.EverythingEnabled)
284302
_ = Task.Run(() => EverythingApi.IncrementRunCounterAsync(fileOrFolder));
285303
}
286304

287-
public static bool IsMedia(string extension)
288-
{
289-
if (string.IsNullOrEmpty(extension))
290-
{
291-
return false;
292-
}
293-
else
294-
{
295-
return MediaExtensions.Contains(extension.ToLowerInvariant());
296-
}
297-
}
298-
299-
private static string addScoreInDebug(int score)
305+
private static string _addScoreInDebug(int score)
300306
{
301307
#if DEBUG
302308
return $" ➡️ {score}";

0 commit comments

Comments
 (0)