Skip to content

Commit b893d86

Browse files
Jack251970TBM13
authored andcommitted
Merge pull request Flow-Launcher#4011 from Flow-Launcher/flow_launcher_localization1
Use Flow.Launcher.Localization to improve code quality
1 parent b198bdd commit b893d86

31 files changed

+168
-195
lines changed

Flow.Launcher.Core/Flow.Launcher.Core.csproj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<ErrorReport>prompt</ErrorReport>
3333
<WarningLevel>4</WarningLevel>
3434
<Prefer32Bit>false</Prefer32Bit>
35+
<NoWarn>$(NoWarn);FLSG0007</NoWarn>
3536
</PropertyGroup>
3637

3738
<ItemGroup>
@@ -52,12 +53,24 @@
5253
</ItemGroup>
5354

5455
<ItemGroup>
56+
<PackageReference Include="Flow.Launcher.Localization" Version="0.0.6" />
5557
<PackageReference Include="Meziantou.Framework.Win32.Jobs" Version="3.4.5" />
5658
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.1" />
5759
<PackageReference Include="StreamJsonRpc" Version="2.22.11" />
5860
<PackageReference Include="YamlDotNet" Version="16.3.0" />
5961
</ItemGroup>
60-
62+
63+
<PropertyGroup>
64+
<FLLUseDependencyInjection>true</FLLUseDependencyInjection>
65+
</PropertyGroup>
66+
67+
<ItemGroup>
68+
<AdditionalFiles Remove="Languages\en.xaml" />
69+
<AdditionalFiles Include="..\Flow.Launcher\Languages\en.xaml">
70+
<Link>Languages\en.xaml</Link>
71+
</AdditionalFiles>
72+
</ItemGroup>
73+
6174
<ItemGroup>
6275
<ProjectReference Include="..\Flow.Launcher.Infrastructure\Flow.Launcher.Infrastructure.csproj" />
6376
<ProjectReference Include="..\Flow.Launcher.Plugin\Flow.Launcher.Plugin.csproj" />

Flow.Launcher.Core/Plugin/JsonRPCPluginSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public Control CreateSettingPanel()
286286
HorizontalAlignment = HorizontalAlignment.Left,
287287
VerticalAlignment = VerticalAlignment.Center,
288288
Margin = SettingPanelItemLeftMargin,
289-
Content = API.GetTranslation("select")
289+
Content = Localize.select()
290290
};
291291

292292
Btn.Click += (_, _) =>

Flow.Launcher.Core/Plugin/PluginConfig.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@
55
using Flow.Launcher.Infrastructure;
66
using Flow.Launcher.Plugin;
77
using System.Text.Json;
8-
using CommunityToolkit.Mvvm.DependencyInjection;
98

109
namespace Flow.Launcher.Core.Plugin
1110
{
1211
internal abstract class PluginConfig
1312
{
1413
private static readonly string ClassName = nameof(PluginConfig);
1514

16-
// We should not initialize API in static constructor because it will create another API instance
17-
private static IPublicAPI api = null;
18-
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
19-
2015
/// <summary>
2116
/// Parse plugin metadata in the given directories
2217
/// </summary>
@@ -38,7 +33,7 @@ public static List<PluginMetadata> Parse(string[] pluginDirectories)
3833
}
3934
catch (Exception e)
4035
{
41-
API.LogException(ClassName, $"Can't delete <{directory}>", e);
36+
PublicApi.Instance.LogException(ClassName, $"Can't delete <{directory}>", e);
4237
}
4338
}
4439
else
@@ -55,7 +50,7 @@ public static List<PluginMetadata> Parse(string[] pluginDirectories)
5550

5651
duplicateList
5752
.ForEach(
58-
x => API.LogWarn(ClassName,
53+
x => PublicApi.Instance.LogWarn(ClassName,
5954
string.Format("Duplicate plugin name: {0}, id: {1}, version: {2} " +
6055
"not loaded due to version not the highest of the duplicates",
6156
x.Name, x.ID, x.Version),
@@ -107,7 +102,7 @@ private static PluginMetadata GetPluginMetadata(string pluginDirectory)
107102
string configPath = Path.Combine(pluginDirectory, Constant.PluginMetadataFileName);
108103
if (!File.Exists(configPath))
109104
{
110-
API.LogError(ClassName, $"Didn't find config file <{configPath}>");
105+
PublicApi.Instance.LogError(ClassName, $"Didn't find config file <{configPath}>");
111106
return null;
112107
}
113108

@@ -123,19 +118,19 @@ private static PluginMetadata GetPluginMetadata(string pluginDirectory)
123118
}
124119
catch (Exception e)
125120
{
126-
API.LogException(ClassName, $"Invalid json for config <{configPath}>", e);
121+
PublicApi.Instance.LogException(ClassName, $"Invalid json for config <{configPath}>", e);
127122
return null;
128123
}
129124

130125
if (!AllowedLanguage.IsAllowed(metadata.Language))
131126
{
132-
API.LogError(ClassName, $"Invalid language <{metadata.Language}> for config <{configPath}>");
127+
PublicApi.Instance.LogError(ClassName, $"Invalid language <{metadata.Language}> for config <{configPath}>");
133128
return null;
134129
}
135130

136131
if (!File.Exists(metadata.ExecuteFilePath))
137132
{
138-
API.LogError(ClassName, $"Execute file path didn't exist <{metadata.ExecuteFilePath}> for conifg <{configPath}");
133+
PublicApi.Instance.LogError(ClassName, $"Execute file path didn't exist <{metadata.ExecuteFilePath}> for conifg <{configPath}");
139134
return null;
140135
}
141136

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Linq;
66
using System.Threading;
77
using System.Threading.Tasks;
8-
using CommunityToolkit.Mvvm.DependencyInjection;
98
using Flow.Launcher.Core.ExternalPlugins;
109
using Flow.Launcher.Infrastructure;
1110
using Flow.Launcher.Infrastructure.UserSettings;
@@ -25,10 +24,6 @@ public static class PluginManager
2524
public static readonly HashSet<PluginPair> GlobalPlugins = new();
2625
public static readonly Dictionary<string, PluginPair> NonGlobalPlugins = new();
2726

28-
// We should not initialize API in static constructor because it will create another API instance
29-
private static IPublicAPI api = null;
30-
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
31-
3227
private static PluginsSettings Settings;
3328

3429
private static IEnumerable<PluginPair> _contextMenuPlugins;
@@ -58,12 +53,12 @@ public static void Save()
5853
}
5954
catch (Exception e)
6055
{
61-
API.LogException(ClassName, $"Failed to save plugin {pluginPair.Metadata.Name}", e);
56+
PublicApi.Instance.LogException(ClassName, $"Failed to save plugin {pluginPair.Metadata.Name}", e);
6257
}
6358
}
6459

65-
API.SavePluginSettings();
66-
API.SavePluginCaches();
60+
PublicApi.Instance.SavePluginSettings();
61+
PublicApi.Instance.SavePluginCaches();
6762
}
6863

6964
public static async ValueTask DisposePluginsAsync()
@@ -90,7 +85,7 @@ private static async Task DisposePluginAsync(PluginPair pluginPair)
9085
}
9186
catch (Exception e)
9287
{
93-
API.LogException(ClassName, $"Failed to dispose plugin {pluginPair.Metadata.Name}", e);
88+
PublicApi.Instance.LogException(ClassName, $"Failed to dispose plugin {pluginPair.Metadata.Name}", e);
9489
}
9590
}
9691

@@ -181,7 +176,7 @@ private static void UpdatePluginDirectory(List<PluginMetadata> metadatas)
181176
{
182177
if (string.IsNullOrEmpty(metadata.AssemblyName))
183178
{
184-
API.LogWarn(ClassName, $"AssemblyName is empty for plugin with metadata: {metadata.Name}");
179+
PublicApi.Instance.LogWarn(ClassName, $"AssemblyName is empty for plugin with metadata: {metadata.Name}");
185180
continue; // Skip if AssemblyName is not set, which can happen for erroneous plugins
186181
}
187182
metadata.PluginSettingsDirectoryPath = Path.Combine(DataLocation.PluginSettingsDirectory, metadata.AssemblyName);
@@ -191,7 +186,7 @@ private static void UpdatePluginDirectory(List<PluginMetadata> metadatas)
191186
{
192187
if (string.IsNullOrEmpty(metadata.Name))
193188
{
194-
API.LogWarn(ClassName, $"Name is empty for plugin with metadata: {metadata.Name}");
189+
PublicApi.Instance.LogWarn(ClassName, $"Name is empty for plugin with metadata: {metadata.Name}");
195190
continue; // Skip if Name is not set, which can happen for erroneous plugins
196191
}
197192
metadata.PluginSettingsDirectoryPath = Path.Combine(DataLocation.PluginSettingsDirectory, metadata.Name);
@@ -212,28 +207,28 @@ public static async Task InitializePluginsAsync()
212207
{
213208
try
214209
{
215-
var milliseconds = await API.StopwatchLogDebugAsync(ClassName, $"Init method time cost for <{pair.Metadata.Name}>",
216-
() => pair.Plugin.InitAsync(new PluginInitContext(pair.Metadata, API)));
210+
var milliseconds = await PublicApi.Instance.StopwatchLogDebugAsync(ClassName, $"Init method time cost for <{pair.Metadata.Name}>",
211+
() => pair.Plugin.InitAsync(new PluginInitContext(pair.Metadata, PublicApi.Instance)));
217212

218213
pair.Metadata.InitTime += milliseconds;
219-
API.LogInfo(ClassName,
214+
PublicApi.Instance.LogInfo(ClassName,
220215
$"Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>");
221216
}
222217
catch (Exception e)
223218
{
224-
API.LogException(ClassName, $"Fail to Init plugin: {pair.Metadata.Name}", e);
219+
PublicApi.Instance.LogException(ClassName, $"Fail to Init plugin: {pair.Metadata.Name}", e);
225220
if (pair.Metadata.Disabled && pair.Metadata.HomeDisabled)
226221
{
227222
// If this plugin is already disabled, do not show error message again
228223
// Or else it will be shown every time
229-
API.LogDebug(ClassName, $"Skipped init for <{pair.Metadata.Name}> due to error");
224+
PublicApi.Instance.LogDebug(ClassName, $"Skipped init for <{pair.Metadata.Name}> due to error");
230225
}
231226
else
232227
{
233228
pair.Metadata.Disabled = true;
234229
pair.Metadata.HomeDisabled = true;
235230
failedPlugins.Enqueue(pair);
236-
API.LogDebug(ClassName, $"Disable plugin <{pair.Metadata.Name}> because init failed");
231+
PublicApi.Instance.LogDebug(ClassName, $"Disable plugin <{pair.Metadata.Name}> because init failed");
237232
}
238233
}
239234
}));
@@ -258,15 +253,12 @@ public static async Task InitializePluginsAsync()
258253
}
259254
}
260255

261-
if (failedPlugins.Any())
256+
if (!failedPlugins.IsEmpty)
262257
{
263258
var failed = string.Join(",", failedPlugins.Select(x => x.Metadata.Name));
264-
API.ShowMsg(
265-
API.GetTranslation("failedToInitializePluginsTitle"),
266-
string.Format(
267-
API.GetTranslation("failedToInitializePluginsMessage"),
268-
failed
269-
),
259+
PublicApi.Instance.ShowMsg(
260+
Localize.failedToInitializePluginsTitle(),
261+
Localize.failedToInitializePluginsMessage(failed),
270262
"",
271263
false
272264
);
@@ -301,7 +293,7 @@ public static async Task<List<Result>> QueryForPluginAsync(PluginPair pair, Quer
301293

302294
try
303295
{
304-
var milliseconds = await API.StopwatchLogDebugAsync(ClassName, $"Cost for {metadata.Name}",
296+
var milliseconds = await PublicApi.Instance.StopwatchLogDebugAsync(ClassName, $"Cost for {metadata.Name}",
305297
async () => results = await pair.Plugin.QueryAsync(query, token).ConfigureAwait(false));
306298

307299
token.ThrowIfCancellationRequested();
@@ -345,7 +337,7 @@ public static async Task<List<Result>> QueryHomeForPluginAsync(PluginPair pair,
345337

346338
try
347339
{
348-
var milliseconds = await API.StopwatchLogDebugAsync(ClassName, $"Cost for {metadata.Name}",
340+
var milliseconds = await PublicApi.Instance.StopwatchLogDebugAsync(ClassName, $"Cost for {metadata.Name}",
349341
async () => results = await ((IAsyncHomeQuery)pair.Plugin).HomeQueryAsync(token).ConfigureAwait(false));
350342

351343
token.ThrowIfCancellationRequested();
@@ -362,7 +354,7 @@ public static async Task<List<Result>> QueryHomeForPluginAsync(PluginPair pair,
362354
}
363355
catch (Exception e)
364356
{
365-
API.LogException(ClassName, $"Failed to query home for plugin: {metadata.Name}", e);
357+
PublicApi.Instance.LogException(ClassName, $"Failed to query home for plugin: {metadata.Name}", e);
366358
return null;
367359
}
368360
return results;
@@ -429,7 +421,7 @@ public static List<Result> GetContextMenusForPlugin(Result result)
429421
}
430422
catch (Exception e)
431423
{
432-
API.LogException(ClassName,
424+
PublicApi.Instance.LogException(ClassName,
433425
$"Can't load context menus for plugin <{pluginPair.Metadata.Name}>",
434426
e);
435427
}

Flow.Launcher.Core/Plugin/PluginsLoader.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5-
using System.Threading.Tasks;
6-
using System.Windows;
7-
using CommunityToolkit.Mvvm.DependencyInjection;
85
#pragma warning disable IDE0005
96
using Flow.Launcher.Infrastructure.Logger;
107
#pragma warning restore IDE0005
@@ -17,10 +14,6 @@ public static class PluginsLoader
1714
{
1815
private static readonly string ClassName = nameof(PluginsLoader);
1916

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-
2417
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginsSettings settings)
2518
{
2619
var dotnetPlugins = DotNetPlugins(metadatas);
@@ -44,7 +37,7 @@ private static IEnumerable<PluginPair> DotNetPlugins(List<PluginMetadata> source
4437

4538
foreach (var metadata in metadatas)
4639
{
47-
var milliseconds = API.StopwatchLogDebug(ClassName, $"Constructor init cost for {metadata.Name}", () =>
40+
var milliseconds = PublicApi.Instance.StopwatchLogDebug(ClassName, $"Constructor init cost for {metadata.Name}", () =>
4841
{
4942
Assembly assembly = null;
5043
IAsyncPlugin plugin = null;
@@ -69,19 +62,19 @@ private static IEnumerable<PluginPair> DotNetPlugins(List<PluginMetadata> source
6962
#else
7063
catch (Exception e) when (assembly == null)
7164
{
72-
Log.Exception(ClassName, $"Couldn't load assembly for the plugin: {metadata.Name}", e);
65+
PublicApi.Instance.LogException(ClassName, $"Couldn't load assembly for the plugin: {metadata.Name}", e);
7366
}
7467
catch (InvalidOperationException e)
7568
{
76-
Log.Exception(ClassName, $"Can't find the required IPlugin interface for the plugin: <{metadata.Name}>", e);
69+
PublicApi.Instance.LogException(ClassName, $"Can't find the required IPlugin interface for the plugin: <{metadata.Name}>", e);
7770
}
7871
catch (ReflectionTypeLoadException e)
7972
{
80-
Log.Exception(ClassName, $"The GetTypes method was unable to load assembly types for the plugin: <{metadata.Name}>", e);
73+
PublicApi.Instance.LogException(ClassName, $"The GetTypes method was unable to load assembly types for the plugin: <{metadata.Name}>", e);
8174
}
8275
catch (Exception e)
8376
{
84-
Log.Exception(ClassName, $"The following plugin has errored and can not be loaded: <{metadata.Name}>", e);
77+
PublicApi.Instance.LogException(ClassName, $"The following plugin has errored and can not be loaded: <{metadata.Name}>", e);
8578
}
8679
#endif
8780

@@ -101,12 +94,12 @@ private static IEnumerable<PluginPair> DotNetPlugins(List<PluginMetadata> source
10194
var errorPluginString = string.Join(Environment.NewLine, erroredPlugins);
10295

10396
var errorMessage = erroredPlugins.Count > 1 ?
104-
API.GetTranslation("pluginsHaveErrored") :
105-
API.GetTranslation("pluginHasErrored");
97+
Localize.pluginsHaveErrored() :
98+
Localize.pluginHasErrored();
10699

107-
API.ShowMsgError($"{errorMessage}{Environment.NewLine}{Environment.NewLine}" +
100+
PublicApi.Instance.ShowMsgError($"{errorMessage}{Environment.NewLine}{Environment.NewLine}" +
108101
$"{errorPluginString}{Environment.NewLine}{Environment.NewLine}" +
109-
API.GetTranslation("referToLogs"));
102+
Localize.referToLogs());
110103
}
111104

112105
return plugins;

0 commit comments

Comments
 (0)