Skip to content

Commit ab34e83

Browse files
authored
Merge branch 'dev' into 250320BookmarkFavicon
2 parents cce4e89 + c730361 commit ab34e83

File tree

9 files changed

+74
-54
lines changed

9 files changed

+74
-54
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ namespace Flow.Launcher.Core.Plugin
2323
/// </summary>
2424
public static class PluginManager
2525
{
26+
private static readonly string ClassName = nameof(PluginManager);
27+
2628
private static IEnumerable<PluginPair> _contextMenuPlugins;
2729

2830
public static List<PluginPair> AllPlugins { get; private set; }
@@ -194,7 +196,7 @@ public static async Task InitializePluginsAsync()
194196
{
195197
try
196198
{
197-
var milliseconds = await Stopwatch.DebugAsync($"|PluginManager.InitializePlugins|Init method time cost for <{pair.Metadata.Name}>",
199+
var milliseconds = await API.StopwatchLogDebugAsync(ClassName, $"Init method time cost for <{pair.Metadata.Name}>",
198200
() => pair.Plugin.InitAsync(new PluginInitContext(pair.Metadata, API)));
199201

200202
pair.Metadata.InitTime += milliseconds;
@@ -266,7 +268,7 @@ public static async Task<List<Result>> QueryForPluginAsync(PluginPair pair, Quer
266268

267269
try
268270
{
269-
var milliseconds = await Stopwatch.DebugAsync($"|PluginManager.QueryForPlugin|Cost for {metadata.Name}",
271+
var milliseconds = await API.StopwatchLogDebugAsync(ClassName, $"Cost for {metadata.Name}",
270272
async () => results = await pair.Plugin.QueryAsync(query, token).ConfigureAwait(false));
271273

272274
token.ThrowIfCancellationRequested();

Flow.Launcher.Core/Plugin/PluginsLoader.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111
#pragma warning restore IDE0005
1212
using Flow.Launcher.Infrastructure.UserSettings;
1313
using Flow.Launcher.Plugin;
14-
using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
1514

1615
namespace Flow.Launcher.Core.Plugin
1716
{
1817
public static class PluginsLoader
1918
{
19+
private static readonly string ClassName = nameof(PluginsLoader);
20+
21+
// We should not initialize API in static constructor because it will create another API instance
22+
private static IPublicAPI api = null;
23+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
24+
2025
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginsSettings settings)
2126
{
2227
var dotnetPlugins = DotNetPlugins(metadatas);
@@ -59,8 +64,7 @@ private static IEnumerable<PluginPair> DotNetPlugins(List<PluginMetadata> source
5964

6065
foreach (var metadata in metadatas)
6166
{
62-
var milliseconds = Stopwatch.Debug(
63-
$"|PluginsLoader.DotNetPlugins|Constructor init cost for {metadata.Name}", () =>
67+
var milliseconds = API.StopwatchLogDebug(ClassName, $"Constructor init cost for {metadata.Name}", () =>
6468
{
6569
Assembly assembly = null;
6670
IAsyncPlugin plugin = null;

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@
77
using System.Threading.Tasks;
88
using System.Windows.Media;
99
using System.Windows.Media.Imaging;
10-
using Flow.Launcher.Infrastructure.Logger;
10+
using CommunityToolkit.Mvvm.DependencyInjection;
1111
using Flow.Launcher.Infrastructure.Storage;
12+
using Flow.Launcher.Plugin;
1213
using SharpVectors.Converters;
1314
using SharpVectors.Renderers.Wpf;
1415

1516
namespace Flow.Launcher.Infrastructure.Image
1617
{
1718
public static class ImageLoader
1819
{
20+
// We should not initialize API in static constructor because it will create another API instance
21+
private static IPublicAPI api = null;
22+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
23+
24+
private static readonly string ClassName = nameof(ImageLoader);
25+
1926
private static readonly ImageCache ImageCache = new();
2027
private static SemaphoreSlim storageLock { get; } = new SemaphoreSlim(1, 1);
2128
private static BinaryStorage<List<(string, bool)>> _storage;
@@ -51,15 +58,14 @@ public static async Task InitializeAsync()
5158

5259
_ = Task.Run(async () =>
5360
{
54-
await Stopwatch.NormalAsync("|ImageLoader.Initialize|Preload images cost", async () =>
61+
await API.StopwatchLogInfoAsync(ClassName, "Preload images cost", async () =>
5562
{
5663
foreach (var (path, isFullImage) in usage)
5764
{
5865
await LoadAsync(path, isFullImage);
5966
}
6067
});
61-
Log.Info(
62-
$"|ImageLoader.Initialize|Number of preload images is <{ImageCache.CacheSize()}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}");
68+
API.LogInfo(ClassName, $"Number of preload images is <{ImageCache.CacheSize()}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}");
6369
});
6470
}
6571

@@ -75,7 +81,7 @@ await _storage.SaveAsync(ImageCache.EnumerateEntries()
7581
}
7682
catch (System.Exception e)
7783
{
78-
Log.Exception($"|ImageLoader.SaveAsync|Failed to save image cache to file", e);
84+
API.LogException(ClassName, "Failed to save image cache to file", e);
7985
}
8086
finally
8187
{
@@ -170,8 +176,8 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
170176
}
171177
catch (System.Exception e2)
172178
{
173-
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path} on first try", e);
174-
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path} on second try", e2);
179+
API.LogException(ClassName, $"|ImageLoader.Load|Failed to get thumbnail for {path} on first try", e);
180+
API.LogException(ClassName, $"|ImageLoader.Load|Failed to get thumbnail for {path} on second try", e2);
175181

176182
ImageSource image = ImageCache[Constant.MissingImgIcon, false];
177183
ImageCache[path, false] = image;

Flow.Launcher.Infrastructure/Stopwatch.cs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Threading.Tasks;
43
using Flow.Launcher.Infrastructure.Logger;
54

65
namespace Flow.Launcher.Infrastructure
76
{
87
public static class Stopwatch
98
{
10-
private static readonly Dictionary<string, long> Count = new Dictionary<string, long>();
11-
private static readonly object Locker = new object();
129
/// <summary>
1310
/// This stopwatch will appear only in Debug mode
1411
/// </summary>
@@ -62,36 +59,5 @@ public static async Task<long> NormalAsync(string message, Func<Task> action)
6259
Log.Info(info);
6360
return milliseconds;
6461
}
65-
66-
67-
68-
public static void StartCount(string name, Action action)
69-
{
70-
var stopWatch = new System.Diagnostics.Stopwatch();
71-
stopWatch.Start();
72-
action();
73-
stopWatch.Stop();
74-
var milliseconds = stopWatch.ElapsedMilliseconds;
75-
lock (Locker)
76-
{
77-
if (Count.ContainsKey(name))
78-
{
79-
Count[name] += milliseconds;
80-
}
81-
else
82-
{
83-
Count[name] = 0;
84-
}
85-
}
86-
}
87-
88-
public static void EndCount()
89-
{
90-
foreach (var key in Count.Keys)
91-
{
92-
string info = $"{key} already cost {Count[key]}ms";
93-
Log.Debug(info);
94-
}
95-
}
9662
}
9763
}

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,5 +510,31 @@ public interface IPublicAPI
510510
/// </param>
511511
/// <returns></returns>
512512
public Task UninstallPluginAsync(PluginMetadata pluginMetadata, bool removePluginSettings = false);
513+
514+
/// <summary>
515+
/// Log debug message of the time taken to execute a method
516+
/// Message will only be logged in Debug mode
517+
/// </summary>
518+
/// <returns>The time taken to execute the method in milliseconds</returns>
519+
public long StopwatchLogDebug(string className, string message, Action action, [CallerMemberName] string methodName = "");
520+
521+
/// <summary>
522+
/// Log debug message of the time taken to execute a method asynchronously
523+
/// Message will only be logged in Debug mode
524+
/// </summary>
525+
/// <returns>The time taken to execute the method in milliseconds</returns>
526+
public Task<long> StopwatchLogDebugAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "");
527+
528+
/// <summary>
529+
/// Log info message of the time taken to execute a method
530+
/// </summary>
531+
/// <returns>The time taken to execute the method in milliseconds</returns>
532+
public long StopwatchLogInfo(string className, string message, Action action, [CallerMemberName] string methodName = "");
533+
534+
/// <summary>
535+
/// Log info message of the time taken to execute a method asynchronously
536+
/// </summary>
537+
/// <returns>The time taken to execute the method in milliseconds</returns>
538+
public Task<long> StopwatchLogInfoAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "");
513539
}
514540
}

Flow.Launcher/App.xaml.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
using Flow.Launcher.ViewModel;
2222
using Microsoft.Extensions.DependencyInjection;
2323
using Microsoft.Extensions.Hosting;
24-
using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
2524

2625
namespace Flow.Launcher
2726
{
@@ -35,6 +34,8 @@ public partial class App : IDisposable, ISingleInstanceApp
3534

3635
#region Private Fields
3736

37+
private static readonly string ClassName = nameof(App);
38+
3839
private static bool _disposed;
3940
private MainWindow _mainWindow;
4041
private readonly MainViewModel _mainVM;
@@ -136,7 +137,7 @@ public static void Main()
136137

137138
private async void OnStartup(object sender, StartupEventArgs e)
138139
{
139-
await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
140+
await API.StopwatchLogInfoAsync(ClassName, "Startup cost", async () =>
140141
{
141142
// Because new message box api uses MessageBoxEx window,
142143
// if it is created and closed before main window is created, it will cause the application to exit.
@@ -313,7 +314,7 @@ protected virtual void Dispose(bool disposing)
313314
_disposed = true;
314315
}
315316

316-
Stopwatch.Normal("|App.Dispose|Dispose cost", () =>
317+
API.StopwatchLogInfo(ClassName, "Dispose cost", () =>
317318
{
318319
Log.Info("|App.Dispose|Begin Flow Launcher dispose ----------------------------------------------------");
319320

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using Flow.Launcher.ViewModel;
3232
using JetBrains.Annotations;
3333
using Squirrel;
34+
using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
3435

3536
namespace Flow.Launcher
3637
{
@@ -431,6 +432,18 @@ public void InstallPlugin(UserPlugin plugin, string zipFilePath) =>
431432
public Task UninstallPluginAsync(PluginMetadata pluginMetadata, bool removePluginSettings = false) =>
432433
PluginManager.UninstallPluginAsync(pluginMetadata, removePluginSettings);
433434

435+
public long StopwatchLogDebug(string className, string message, Action action, [CallerMemberName] string methodName = "") =>
436+
Stopwatch.Debug($"|{className}.{methodName}|{message}", action);
437+
438+
public Task<long> StopwatchLogDebugAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "") =>
439+
Stopwatch.DebugAsync($"|{className}.{methodName}|{message}", action);
440+
441+
public long StopwatchLogInfo(string className, string message, Action action, [CallerMemberName] string methodName = "") =>
442+
Stopwatch.Normal($"|{className}.{methodName}|{message}", action);
443+
444+
public Task<long> StopwatchLogInfoAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "") =>
445+
Stopwatch.NormalAsync($"|{className}.{methodName}|{message}", action);
446+
434447
#endregion
435448

436449
#region Private Methods

Plugins/Flow.Launcher.Plugin.Explorer/ViewModels/SettingsViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ public string ExcludedFileTypes
524524
}
525525
}
526526

527-
public int MaxResultLowerLimit => 100;
527+
public int MaxResultLowerLimit => 1;
528528
public int MaxResultUpperLimit => 100000;
529529

530530
public int MaxResult

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Flow.Launcher.Plugin.SharedCommands;
1414
using Microsoft.Extensions.Caching.Memory;
1515
using Path = System.IO.Path;
16-
using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch;
1716

1817
namespace Flow.Launcher.Plugin.Program
1918
{
@@ -33,6 +32,8 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I
3332

3433
internal static PluginInitContext Context { get; private set; }
3534

35+
private static readonly string ClassName = nameof(Main);
36+
3637
private static readonly List<Result> emptyResults = new();
3738

3839
private static readonly MemoryCacheOptions cacheOptions = new() { SizeLimit = 1560 };
@@ -189,7 +190,7 @@ public async Task InitAsync(PluginInitContext context)
189190

190191
var _win32sCount = 0;
191192
var _uwpsCount = 0;
192-
await Stopwatch.NormalAsync("|Flow.Launcher.Plugin.Program.Main|Preload programs cost", async () =>
193+
await Context.API.StopwatchLogInfoAsync(ClassName, "Preload programs cost", async () =>
193194
{
194195
var pluginCacheDirectory = Context.CurrentPluginMetadata.PluginCacheDirectoryPath;
195196
FilesFolders.ValidateDirectory(pluginCacheDirectory);
@@ -256,6 +257,7 @@ static void MoveFile(string sourcePath, string destinationPath)
256257
});
257258
Context.API.LogInfo(ClassName, $"Number of preload win32 programs <{_win32sCount}>");
258259
Context.API.LogInfo(ClassName, $"Number of preload uwps <{_uwpsCount}>");
260+
259261
var cacheEmpty = _win32sCount == 0 || _uwpsCount == 0;
260262

261263
if (cacheEmpty || _settings.LastIndexTime.AddHours(30) < DateTime.Now)
@@ -332,12 +334,12 @@ public static async Task IndexProgramsAsync()
332334
{
333335
var win32Task = Task.Run(async () =>
334336
{
335-
await Stopwatch.NormalAsync("|Flow.Launcher.Plugin.Program.Main|Win32Program index cost", IndexWin32ProgramsAsync);
337+
await Context.API.StopwatchLogInfoAsync(ClassName, "Win32Program index cost", IndexWin32ProgramsAsync);
336338
});
337339

338340
var uwpTask = Task.Run(async () =>
339341
{
340-
await Stopwatch.NormalAsync("|Flow.Launcher.Plugin.Program.Main|UWPProgram index cost", IndexUwpProgramsAsync);
342+
await Context.API.StopwatchLogInfoAsync(ClassName, "UWPProgram index cost", IndexUwpProgramsAsync);
341343
});
342344

343345
await Task.WhenAll(win32Task, uwpTask).ConfigureAwait(false);

0 commit comments

Comments
 (0)