Skip to content

Commit 1b05643

Browse files
committed
Use Flow.Launcher.Localization to improve code quality
1 parent e6a38ad commit 1b05643

File tree

8 files changed

+58
-39
lines changed

8 files changed

+58
-39
lines changed

Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected List<Bookmark> LoadBookmarks(string browserDataPath, string name)
4545
}
4646
catch (Exception ex)
4747
{
48-
Main._context.API.LogException(ClassName, $"Failed to register bookmark file monitoring: {bookmarkPath}", ex);
48+
Main.Context.API.LogException(ClassName, $"Failed to register bookmark file monitoring: {bookmarkPath}", ex);
4949
continue;
5050
}
5151

@@ -58,7 +58,7 @@ protected List<Bookmark> LoadBookmarks(string browserDataPath, string name)
5858
var faviconDbPath = Path.Combine(profile, "Favicons");
5959
if (File.Exists(faviconDbPath))
6060
{
61-
Main._context.API.StopwatchLogInfo(ClassName, $"Load {profileBookmarks.Count} favicons cost", () =>
61+
Main.Context.API.StopwatchLogInfo(ClassName, $"Load {profileBookmarks.Count} favicons cost", () =>
6262
{
6363
LoadFaviconsFromDb(faviconDbPath, profileBookmarks);
6464
});
@@ -125,7 +125,7 @@ private static void EnumerateFolderBookmark(JsonElement folderElement, ICollecti
125125
}
126126
else
127127
{
128-
Main._context.API.LogError(ClassName, $"type property not found for {subElement.GetString()}");
128+
Main.Context.API.LogError(ClassName, $"type property not found for {subElement.GetString()}");
129129
}
130130
}
131131
}
@@ -190,7 +190,7 @@ ORDER BY b.width DESC
190190
}
191191
catch (Exception ex)
192192
{
193-
Main._context.API.LogException(ClassName, $"Failed to extract bookmark favicon: {bookmark.Url}", ex);
193+
Main.Context.API.LogException(ClassName, $"Failed to extract bookmark favicon: {bookmark.Url}", ex);
194194
}
195195
finally
196196
{

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/BookmarkLoader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ internal static class BookmarkLoader
99
{
1010
internal static MatchResult MatchProgram(Bookmark bookmark, string queryString)
1111
{
12-
var match = Main._context.API.FuzzySearch(queryString, bookmark.Name);
12+
var match = Main.Context.API.FuzzySearch(queryString, bookmark.Name);
1313
if (match.IsSearchPrecisionScoreMet())
1414
return match;
1515

16-
return Main._context.API.FuzzySearch(queryString, bookmark.Url);
16+
return Main.Context.API.FuzzySearch(queryString, bookmark.Url);
1717
}
1818

1919
internal static List<Bookmark> LoadAllBookmarks(Settings setting)

Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected List<Bookmark> GetBookmarksFromPath(string placesPath)
4949
}
5050
catch (Exception ex)
5151
{
52-
Main._context.API.LogException(ClassName, $"Failed to register Firefox bookmark file monitoring: {placesPath}", ex);
52+
Main.Context.API.LogException(ClassName, $"Failed to register Firefox bookmark file monitoring: {placesPath}", ex);
5353
return bookmarks;
5454
}
5555

@@ -84,7 +84,7 @@ protected List<Bookmark> GetBookmarksFromPath(string placesPath)
8484
var faviconDbPath = Path.Combine(Path.GetDirectoryName(placesPath), "favicons.sqlite");
8585
if (File.Exists(faviconDbPath))
8686
{
87-
Main._context.API.StopwatchLogInfo(ClassName, $"Load {bookmarks.Count} favicons cost", () =>
87+
Main.Context.API.StopwatchLogInfo(ClassName, $"Load {bookmarks.Count} favicons cost", () =>
8888
{
8989
LoadFaviconsFromDb(faviconDbPath, bookmarks);
9090
});
@@ -98,7 +98,7 @@ protected List<Bookmark> GetBookmarksFromPath(string placesPath)
9898
}
9999
catch (Exception ex)
100100
{
101-
Main._context.API.LogException(ClassName, $"Failed to load Firefox bookmarks: {placesPath}", ex);
101+
Main.Context.API.LogException(ClassName, $"Failed to load Firefox bookmarks: {placesPath}", ex);
102102
}
103103

104104
// Delete temporary file
@@ -111,7 +111,7 @@ protected List<Bookmark> GetBookmarksFromPath(string placesPath)
111111
}
112112
catch (Exception ex)
113113
{
114-
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
114+
Main.Context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
115115
}
116116

117117
return bookmarks;
@@ -186,7 +186,7 @@ ORDER BY i.width DESC -- Select largest icon available
186186
}
187187
catch (Exception ex)
188188
{
189-
Main._context.API.LogException(ClassName, $"Failed to extract Firefox favicon: {bookmark.Url}", ex);
189+
Main.Context.API.LogException(ClassName, $"Failed to extract Firefox favicon: {bookmark.Url}", ex);
190190
}
191191
finally
192192
{

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Flow.Launcher.Plugin.BrowserBookmark.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
</ItemGroup>
8282

8383
<ItemGroup>
84-
<ProjectReference Include="..\..\Flow.Launcher.Infrastructure\Flow.Launcher.Infrastructure.csproj" />
8584
<ProjectReference Include="..\..\Flow.Launcher.Plugin\Flow.Launcher.Plugin.csproj" />
8685
</ItemGroup>
8786

@@ -96,6 +95,7 @@
9695

9796
<ItemGroup>
9897
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
98+
<PackageReference Include="Flow.Launcher.Localization" Version="0.0.3" />
9999
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.3" />
100100
</ItemGroup>
101101

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Helper/FaviconHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public static void LoadFaviconsFromDb(string faviconCacheDir, string dbPath, Act
2727
}
2828
catch (Exception ex1)
2929
{
30-
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex1);
30+
Main.Context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex1);
3131
}
32-
Main._context.API.LogException(ClassName, $"Failed to copy favicon DB: {dbPath}", ex);
32+
Main.Context.API.LogException(ClassName, $"Failed to copy favicon DB: {dbPath}", ex);
3333
return;
3434
}
3535

@@ -39,7 +39,7 @@ public static void LoadFaviconsFromDb(string faviconCacheDir, string dbPath, Act
3939
}
4040
catch (Exception ex)
4141
{
42-
Main._context.API.LogException(ClassName, $"Failed to connect to SQLite: {tempDbPath}", ex);
42+
Main.Context.API.LogException(ClassName, $"Failed to connect to SQLite: {tempDbPath}", ex);
4343
}
4444

4545
// Delete temporary file
@@ -49,7 +49,7 @@ public static void LoadFaviconsFromDb(string faviconCacheDir, string dbPath, Act
4949
}
5050
catch (Exception ex)
5151
{
52-
Main._context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
52+
Main.Context.API.LogException(ClassName, $"Failed to delete temporary favicon DB: {tempDbPath}", ex);
5353
}
5454
}
5555

@@ -61,7 +61,7 @@ public static void SaveBitmapData(byte[] imageData, string outputPath)
6161
}
6262
catch (Exception ex)
6363
{
64-
Main._context.API.LogException(ClassName, $"Failed to save image: {outputPath}", ex);
64+
Main.Context.API.LogException(ClassName, $"Failed to save image: {outputPath}", ex);
6565
}
6666
}
6767

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
1919

2020
internal static string _faviconCacheDir;
2121

22-
internal static PluginInitContext _context;
22+
internal static PluginInitContext Context { get; set; }
2323

2424
internal static Settings _settings;
2525

@@ -29,7 +29,7 @@ public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContex
2929

3030
public void Init(PluginInitContext context)
3131
{
32-
_context = context;
32+
Context = context;
3333

3434
_settings = context.API.LoadSettingJsonStorage<Settings>();
3535

@@ -42,7 +42,7 @@ public void Init(PluginInitContext context)
4242

4343
private static void LoadBookmarksIfEnabled()
4444
{
45-
if (_context.CurrentPluginMetadata.Disabled)
45+
if (Context.CurrentPluginMetadata.Disabled)
4646
{
4747
// Don't load or monitor files if disabled
4848
return;
@@ -84,7 +84,7 @@ public List<Result> Query(Query query)
8484
Score = BookmarkLoader.MatchProgram(c, param).Score,
8585
Action = _ =>
8686
{
87-
_context.API.OpenUrl(c.Url);
87+
Context.API.OpenUrl(c.Url);
8888

8989
return true;
9090
},
@@ -108,7 +108,7 @@ public List<Result> Query(Query query)
108108
Score = 5,
109109
Action = _ =>
110110
{
111-
_context.API.OpenUrl(c.Url);
111+
Context.API.OpenUrl(c.Url);
112112
return true;
113113
},
114114
ContextData = new BookmarkAttributes { Url = c.Url }
@@ -192,12 +192,12 @@ public static void ReloadAllBookmarks(bool disposeFileWatchers = true)
192192

193193
public string GetTranslatedPluginTitle()
194194
{
195-
return _context.API.GetTranslation("flowlauncher_plugin_browserbookmark_plugin_name");
195+
return Localize.flowlauncher_plugin_browserbookmark_plugin_name();
196196
}
197197

198198
public string GetTranslatedPluginDescription()
199199
{
200-
return _context.API.GetTranslation("flowlauncher_plugin_browserbookmark_plugin_description");
200+
return Localize.flowlauncher_plugin_browserbookmark_plugin_description();
201201
}
202202

203203
public Control CreateSettingPanel()
@@ -211,22 +211,22 @@ public List<Result> LoadContextMenus(Result selectedResult)
211211
{
212212
new()
213213
{
214-
Title = _context.API.GetTranslation("flowlauncher_plugin_browserbookmark_copyurl_title"),
215-
SubTitle = _context.API.GetTranslation("flowlauncher_plugin_browserbookmark_copyurl_subtitle"),
214+
Title = Localize.flowlauncher_plugin_browserbookmark_copyurl_title(),
215+
SubTitle = Localize.flowlauncher_plugin_browserbookmark_copyurl_subtitle(),
216216
Action = _ =>
217217
{
218218
try
219219
{
220-
_context.API.CopyToClipboard(((BookmarkAttributes)selectedResult.ContextData).Url);
220+
Context.API.CopyToClipboard(((BookmarkAttributes)selectedResult.ContextData).Url);
221221

222222
return true;
223223
}
224224
catch (Exception e)
225225
{
226226
var message = "Failed to set url in clipboard";
227-
_context.API.LogException(ClassName, message, e);
227+
Context.API.LogException(ClassName, message, e);
228228

229-
_context.API.ShowMsg(message);
229+
Context.API.ShowMsg(message);
230230

231231
return false;
232232
}
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace Flow.Launcher.Plugin.BrowserBookmark.Models;
1+
using System.Collections.Generic;
2+
using Flow.Launcher.Localization.Attributes;
3+
4+
namespace Flow.Launcher.Plugin.BrowserBookmark.Models;
25

36
public class CustomBrowser : BaseModel
47
{
@@ -11,8 +14,11 @@ public string Name
1114
get => _name;
1215
set
1316
{
14-
_name = value;
15-
OnPropertyChanged();
17+
if (_name != value)
18+
{
19+
_name = value;
20+
OnPropertyChanged();
21+
}
1622
}
1723
}
1824

@@ -21,24 +27,36 @@ public string DataDirectoryPath
2127
get => _dataDirectoryPath;
2228
set
2329
{
24-
_dataDirectoryPath = value;
25-
OnPropertyChanged();
30+
if (_dataDirectoryPath != value)
31+
{
32+
_dataDirectoryPath = value;
33+
OnPropertyChanged();
34+
}
2635
}
2736
}
2837

38+
public List<BrowserTypeLocalized> AllBrowserTypes { get; } = BrowserTypeLocalized.GetValues();
39+
2940
public BrowserType BrowserType
3041
{
3142
get => _browserType;
3243
set
3344
{
34-
_browserType = value;
35-
OnPropertyChanged();
45+
if (_browserType != value)
46+
{
47+
_browserType = value;
48+
OnPropertyChanged();
49+
}
3650
}
3751
}
3852
}
3953

54+
[EnumLocalize]
4055
public enum BrowserType
4156
{
57+
[EnumLocalizeValue("Chromium")]
4258
Chromium,
59+
60+
[EnumLocalizeValue("Firefox")]
4361
Firefox,
4462
}

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/CustomBrowserSetting.xaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:local="clr-namespace:Flow.Launcher.Plugin.BrowserBookmark.Models"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8-
xmlns:ui="clr-namespace:Flow.Launcher.Infrastructure.UI;assembly=Flow.Launcher.Infrastructure"
98
Title="{DynamicResource flowlauncher_plugin_browserbookmark_bookmarkDataSetting}"
109
Width="550"
1110
Background="{DynamicResource PopuBGColor}"
@@ -142,8 +141,10 @@
142141
Margin="5 10 10 0"
143142
HorizontalAlignment="Left"
144143
VerticalAlignment="Center"
145-
ItemsSource="{Binding Source={ui:EnumBindingSource {x:Type local:BrowserType}}}"
146-
SelectedItem="{Binding BrowserType}" />
144+
DisplayMemberPath="Display"
145+
ItemsSource="{Binding AllBrowserTypes}"
146+
SelectedValue="{Binding BrowserType}"
147+
SelectedValuePath="Value" />
147148
<TextBlock
148149
Grid.Row="3"
149150
Grid.Column="0"

0 commit comments

Comments
 (0)