Use Flow.Launcher.Localization to improve code quality

This commit is contained in:
Jack251970 2025-06-23 12:38:27 +08:00
parent e6a38ad6e2
commit 1b05643b64
8 changed files with 58 additions and 39 deletions

View file

@ -45,7 +45,7 @@ public abstract class ChromiumBookmarkLoader : IBookmarkLoader
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to register bookmark file monitoring: {bookmarkPath}", ex);
Main.Context.API.LogException(ClassName, $"Failed to register bookmark file monitoring: {bookmarkPath}", ex);
continue;
}
@ -58,7 +58,7 @@ public abstract class ChromiumBookmarkLoader : IBookmarkLoader
var faviconDbPath = Path.Combine(profile, "Favicons");
if (File.Exists(faviconDbPath))
{
Main._context.API.StopwatchLogInfo(ClassName, $"Load {profileBookmarks.Count} favicons cost", () =>
Main.Context.API.StopwatchLogInfo(ClassName, $"Load {profileBookmarks.Count} favicons cost", () =>
{
LoadFaviconsFromDb(faviconDbPath, profileBookmarks);
});
@ -125,7 +125,7 @@ public abstract class ChromiumBookmarkLoader : IBookmarkLoader
}
else
{
Main._context.API.LogError(ClassName, $"type property not found for {subElement.GetString()}");
Main.Context.API.LogError(ClassName, $"type property not found for {subElement.GetString()}");
}
}
}
@ -190,7 +190,7 @@ public abstract class ChromiumBookmarkLoader : IBookmarkLoader
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to extract bookmark favicon: {bookmark.Url}", ex);
Main.Context.API.LogException(ClassName, $"Failed to extract bookmark favicon: {bookmark.Url}", ex);
}
finally
{

View file

@ -9,11 +9,11 @@ internal static class BookmarkLoader
{
internal static MatchResult MatchProgram(Bookmark bookmark, string queryString)
{
var match = Main._context.API.FuzzySearch(queryString, bookmark.Name);
var match = Main.Context.API.FuzzySearch(queryString, bookmark.Name);
if (match.IsSearchPrecisionScoreMet())
return match;
return Main._context.API.FuzzySearch(queryString, bookmark.Url);
return Main.Context.API.FuzzySearch(queryString, bookmark.Url);
}
internal static List<Bookmark> LoadAllBookmarks(Settings setting)

View file

@ -49,7 +49,7 @@ public abstract class FirefoxBookmarkLoaderBase : IBookmarkLoader
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to register Firefox bookmark file monitoring: {placesPath}", ex);
Main.Context.API.LogException(ClassName, $"Failed to register Firefox bookmark file monitoring: {placesPath}", ex);
return bookmarks;
}
@ -84,7 +84,7 @@ public abstract class FirefoxBookmarkLoaderBase : IBookmarkLoader
var faviconDbPath = Path.Combine(Path.GetDirectoryName(placesPath), "favicons.sqlite");
if (File.Exists(faviconDbPath))
{
Main._context.API.StopwatchLogInfo(ClassName, $"Load {bookmarks.Count} favicons cost", () =>
Main.Context.API.StopwatchLogInfo(ClassName, $"Load {bookmarks.Count} favicons cost", () =>
{
LoadFaviconsFromDb(faviconDbPath, bookmarks);
});
@ -98,7 +98,7 @@ public abstract class FirefoxBookmarkLoaderBase : IBookmarkLoader
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to load Firefox bookmarks: {placesPath}", ex);
Main.Context.API.LogException(ClassName, $"Failed to load Firefox bookmarks: {placesPath}", ex);
}
// Delete temporary file
@ -111,7 +111,7 @@ public abstract class FirefoxBookmarkLoaderBase : IBookmarkLoader
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
Main.Context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
}
return bookmarks;
@ -186,7 +186,7 @@ public abstract class FirefoxBookmarkLoaderBase : IBookmarkLoader
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to extract Firefox favicon: {bookmark.Url}", ex);
Main.Context.API.LogException(ClassName, $"Failed to extract Firefox favicon: {bookmark.Url}", ex);
}
finally
{

View file

@ -81,7 +81,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Flow.Launcher.Infrastructure\Flow.Launcher.Infrastructure.csproj" />
<ProjectReference Include="..\..\Flow.Launcher.Plugin\Flow.Launcher.Plugin.csproj" />
</ItemGroup>
@ -96,6 +95,7 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="Flow.Launcher.Localization" Version="0.0.3" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.3" />
</ItemGroup>

View file

@ -27,9 +27,9 @@ public static class FaviconHelper
}
catch (Exception ex1)
{
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex1);
Main.Context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex1);
}
Main._context.API.LogException(ClassName, $"Failed to copy favicon DB: {dbPath}", ex);
Main.Context.API.LogException(ClassName, $"Failed to copy favicon DB: {dbPath}", ex);
return;
}
@ -39,7 +39,7 @@ public static class FaviconHelper
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to connect to SQLite: {tempDbPath}", ex);
Main.Context.API.LogException(ClassName, $"Failed to connect to SQLite: {tempDbPath}", ex);
}
// Delete temporary file
@ -49,7 +49,7 @@ public static class FaviconHelper
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
Main.Context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
}
}
@ -61,7 +61,7 @@ public static class FaviconHelper
}
catch (Exception ex)
{
Main._context.API.LogException(ClassName, $"Failed to save image: {outputPath}", ex);
Main.Context.API.LogException(ClassName, $"Failed to save image: {outputPath}", ex);
}
}

View file

@ -19,7 +19,7 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
internal static string _faviconCacheDir;
internal static PluginInitContext _context;
internal static PluginInitContext Context { get; set; }
internal static Settings _settings;
@ -29,7 +29,7 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
public void Init(PluginInitContext context)
{
_context = context;
Context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>();
@ -42,7 +42,7 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
private static void LoadBookmarksIfEnabled()
{
if (_context.CurrentPluginMetadata.Disabled)
if (Context.CurrentPluginMetadata.Disabled)
{
// Don't load or monitor files if disabled
return;
@ -84,7 +84,7 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
Score = BookmarkLoader.MatchProgram(c, param).Score,
Action = _ =>
{
_context.API.OpenUrl(c.Url);
Context.API.OpenUrl(c.Url);
return true;
},
@ -108,7 +108,7 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
Score = 5,
Action = _ =>
{
_context.API.OpenUrl(c.Url);
Context.API.OpenUrl(c.Url);
return true;
},
ContextData = new BookmarkAttributes { Url = c.Url }
@ -192,12 +192,12 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
public string GetTranslatedPluginTitle()
{
return _context.API.GetTranslation("flowlauncher_plugin_browserbookmark_plugin_name");
return Localize.flowlauncher_plugin_browserbookmark_plugin_name();
}
public string GetTranslatedPluginDescription()
{
return _context.API.GetTranslation("flowlauncher_plugin_browserbookmark_plugin_description");
return Localize.flowlauncher_plugin_browserbookmark_plugin_description();
}
public Control CreateSettingPanel()
@ -211,22 +211,22 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
{
new()
{
Title = _context.API.GetTranslation("flowlauncher_plugin_browserbookmark_copyurl_title"),
SubTitle = _context.API.GetTranslation("flowlauncher_plugin_browserbookmark_copyurl_subtitle"),
Title = Localize.flowlauncher_plugin_browserbookmark_copyurl_title(),
SubTitle = Localize.flowlauncher_plugin_browserbookmark_copyurl_subtitle(),
Action = _ =>
{
try
{
_context.API.CopyToClipboard(((BookmarkAttributes)selectedResult.ContextData).Url);
Context.API.CopyToClipboard(((BookmarkAttributes)selectedResult.ContextData).Url);
return true;
}
catch (Exception e)
{
var message = "Failed to set url in clipboard";
_context.API.LogException(ClassName, message, e);
Context.API.LogException(ClassName, message, e);
_context.API.ShowMsg(message);
Context.API.ShowMsg(message);
return false;
}

View file

@ -1,4 +1,7 @@
namespace Flow.Launcher.Plugin.BrowserBookmark.Models;
using System.Collections.Generic;
using Flow.Launcher.Localization.Attributes;
namespace Flow.Launcher.Plugin.BrowserBookmark.Models;
public class CustomBrowser : BaseModel
{
@ -11,8 +14,11 @@ public class CustomBrowser : BaseModel
get => _name;
set
{
_name = value;
OnPropertyChanged();
if (_name != value)
{
_name = value;
OnPropertyChanged();
}
}
}
@ -21,24 +27,36 @@ public class CustomBrowser : BaseModel
get => _dataDirectoryPath;
set
{
_dataDirectoryPath = value;
OnPropertyChanged();
if (_dataDirectoryPath != value)
{
_dataDirectoryPath = value;
OnPropertyChanged();
}
}
}
public List<BrowserTypeLocalized> AllBrowserTypes { get; } = BrowserTypeLocalized.GetValues();
public BrowserType BrowserType
{
get => _browserType;
set
{
_browserType = value;
OnPropertyChanged();
if (_browserType != value)
{
_browserType = value;
OnPropertyChanged();
}
}
}
}
[EnumLocalize]
public enum BrowserType
{
[EnumLocalizeValue("Chromium")]
Chromium,
[EnumLocalizeValue("Firefox")]
Firefox,
}

View file

@ -5,7 +5,6 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Flow.Launcher.Plugin.BrowserBookmark.Models"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="clr-namespace:Flow.Launcher.Infrastructure.UI;assembly=Flow.Launcher.Infrastructure"
Title="{DynamicResource flowlauncher_plugin_browserbookmark_bookmarkDataSetting}"
Width="550"
Background="{DynamicResource PopuBGColor}"
@ -142,8 +141,10 @@
Margin="5 10 10 0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
ItemsSource="{Binding Source={ui:EnumBindingSource {x:Type local:BrowserType}}}"
SelectedItem="{Binding BrowserType}" />
DisplayMemberPath="Display"
ItemsSource="{Binding AllBrowserTypes}"
SelectedValue="{Binding BrowserType}"
SelectedValuePath="Value" />
<TextBlock
Grid.Row="3"
Grid.Column="0"