Skip to content

Commit 224dab7

Browse files
committed
Add dynamic title
Signed-off-by: Florian Grabmeier <[email protected]>
1 parent 865c478 commit 224dab7

File tree

1 file changed

+67
-23
lines changed
  • Plugins/Flow.Launcher.Plugin.Sys

1 file changed

+67
-23
lines changed

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

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5+
using System.Linq;
56
using System.Runtime.InteropServices;
67
using System.Windows;
78
using System.Windows.Forms;
89
using System.Windows.Interop;
910
using Flow.Launcher.Infrastructure;
11+
using Flow.Launcher.Infrastructure.Logger;
1012
using Flow.Launcher.Infrastructure.UserSettings;
1113
using Flow.Launcher.Plugin.SharedCommands;
1214
using Application = System.Windows.Application;
@@ -19,6 +21,7 @@ namespace Flow.Launcher.Plugin.Sys
1921
public class Main : IPlugin, ISettingProvider, IPluginI18n
2022
{
2123
private PluginInitContext context;
24+
private Dictionary<string, string> KeywordTitleMappings = new Dictionary<string, string>();
2225

2326
#region DllImport
2427

@@ -59,6 +62,8 @@ public List<Result> Query(Query query)
5962
var results = new List<Result>();
6063
foreach (var c in commands)
6164
{
65+
c.Title = GetDynamicTitle(query, c);
66+
6267
var titleMatch = StringMatcher.FuzzySearch(query.Search, c.Title);
6368
var subTitleMatch = StringMatcher.FuzzySearch(query.Search, c.SubTitle);
6469

@@ -77,9 +82,48 @@ public List<Result> Query(Query query)
7782
return results;
7883
}
7984

85+
private string GetDynamicTitle(Query query, Result result)
86+
{
87+
var pair = KeywordTitleMappings
88+
.Where(kvp => kvp.Key == result.Title && kvp.Key != kvp.Value)
89+
.FirstOrDefault();
90+
91+
if (pair.Equals(default))
92+
{
93+
Log.Error($"Dynamic Title not found for: {result.Title}");
94+
return "Title Not Found";
95+
}
96+
97+
var englishTitleMatch = StringMatcher.FuzzySearch(query.Search, pair.Key);
98+
var translatedTitleMatch = StringMatcher.FuzzySearch(query.Search, pair.Value);
99+
100+
return englishTitleMatch.Score >= translatedTitleMatch.Score ? pair.Key : pair.Value;
101+
}
102+
80103
public void Init(PluginInitContext context)
81104
{
82105
this.context = context;
106+
KeywordTitleMappings = new Dictionary<string, string>{
107+
{"Shutdown", context.API.GetTranslation("flowlauncher_plugin_sys_shutdown_computer_cmd")},
108+
{"Restart", context.API.GetTranslation("flowlauncher_plugin_sys_restart_computer_cmd")},
109+
{"Restart With Advanced Boot Options", context.API.GetTranslation("flowlauncher_plugin_sys_restart_advanced_cmd")},
110+
{"Log Off/Sign Out", context.API.GetTranslation("flowlauncher_plugin_sys_log_off_cmd")},
111+
{"Lock", context.API.GetTranslation("flowlauncher_plugin_sys_lock_cmd")},
112+
{"Sleep", context.API.GetTranslation("flowlauncher_plugin_sys_sleep_cmd")},
113+
{"Hibernate", context.API.GetTranslation("flowlauncher_plugin_sys_hibernate_cmd")},
114+
{"Index Option", context.API.GetTranslation("flowlauncher_plugin_sys_indexoption_cmd")},
115+
{"Empty Recycle Bin", context.API.GetTranslation("flowlauncher_plugin_sys_emptyrecyclebin_cmd")},
116+
{"Open Recycle Bin", context.API.GetTranslation("flowlauncher_plugin_sys_openrecyclebin_cmd")},
117+
{"Exit", context.API.GetTranslation("flowlauncher_plugin_sys_exit_cmd")},
118+
{"Save Settings", context.API.GetTranslation("flowlauncher_plugin_sys_save_all_settings_cmd")},
119+
{"Restart Flow Launcher", context.API.GetTranslation("flowlauncher_plugin_sys_restart_cmd")},
120+
{"Settings", context.API.GetTranslation("flowlauncher_plugin_sys_setting_cmd")},
121+
{"Reload Plugin Data", context.API.GetTranslation("flowlauncher_plugin_sys_reload_plugin_data_cmd")},
122+
{"Check For Update", context.API.GetTranslation("flowlauncher_plugin_sys_check_for_update_cmd")},
123+
{"Open Log Location", context.API.GetTranslation("flowlauncher_plugin_sys_open_log_location_cmd")},
124+
{"Flow Launcher Tips", context.API.GetTranslation("flowlauncher_plugin_sys_open_docs_tips_cmd")},
125+
{"Flow Launcher UserData Folder", context.API.GetTranslation("flowlauncher_plugin_sys_open_userdata_location_cmd")}
126+
};
83127
}
84128

85129
private List<Result> Commands()
@@ -89,7 +133,7 @@ private List<Result> Commands()
89133
{
90134
new Result
91135
{
92-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_shutdown_computer_cmd"),
136+
Title = "Shutdown",
93137
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_shutdown_computer"),
94138
Glyph = new GlyphInfo (FontFamily:"/Resources/#Segoe Fluent Icons", Glyph:"\xe7e8"),
95139
IcoPath = "Images\\shutdown.png",
@@ -109,7 +153,7 @@ private List<Result> Commands()
109153
},
110154
new Result
111155
{
112-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_restart_computer_cmd"),
156+
Title = "Restart",
113157
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_restart_computer"),
114158
Glyph = new GlyphInfo (FontFamily:"/Resources/#Segoe Fluent Icons", Glyph:"\xe777"),
115159
IcoPath = "Images\\restart.png",
@@ -129,7 +173,7 @@ private List<Result> Commands()
129173
},
130174
new Result
131175
{
132-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_restart_advanced_cmd"),
176+
Title = "Restart With Advanced Boot Options",
133177
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_restart_advanced"),
134178
Glyph = new GlyphInfo (FontFamily:"/Resources/#Segoe Fluent Icons", Glyph:"\xecc5"),
135179
IcoPath = "Images\\restart_advanced.png",
@@ -139,7 +183,7 @@ private List<Result> Commands()
139183
context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_restart_computer_advanced"),
140184
context.API.GetTranslation("flowlauncher_plugin_sys_restart_computer"),
141185
MessageBoxButton.YesNo, MessageBoxImage.Warning);
142-
186+
143187
if (result == MessageBoxResult.Yes)
144188
Process.Start("shutdown", "/r /o /t 0");
145189

@@ -148,7 +192,7 @@ private List<Result> Commands()
148192
},
149193
new Result
150194
{
151-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_log_off_cmd"),
195+
Title = "Log Off/Sign Out",
152196
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_log_off"),
153197
Glyph = new GlyphInfo (FontFamily:"/Resources/#Segoe Fluent Icons", Glyph:"\xe77b"),
154198
IcoPath = "Images\\logoff.png",
@@ -158,7 +202,7 @@ private List<Result> Commands()
158202
context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_logoff_computer"),
159203
context.API.GetTranslation("flowlauncher_plugin_sys_log_off"),
160204
MessageBoxButton.YesNo, MessageBoxImage.Warning);
161-
205+
162206
if (result == MessageBoxResult.Yes)
163207
ExitWindowsEx(EWX_LOGOFF, 0);
164208

@@ -167,7 +211,7 @@ private List<Result> Commands()
167211
},
168212
new Result
169213
{
170-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_lock_cmd"),
214+
Title = "Lock",
171215
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_lock"),
172216
Glyph = new GlyphInfo (FontFamily:"/Resources/#Segoe Fluent Icons", Glyph:"\xe72e"),
173217
IcoPath = "Images\\lock.png",
@@ -179,15 +223,15 @@ private List<Result> Commands()
179223
},
180224
new Result
181225
{
182-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_sleep_cmd"),
226+
Title = "Sleep",
183227
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_sleep"),
184228
Glyph = new GlyphInfo (FontFamily:"/Resources/#Segoe Fluent Icons", Glyph:"\xec46"),
185229
IcoPath = "Images\\sleep.png",
186230
Action = c => FormsApplication.SetSuspendState(PowerState.Suspend, false, false)
187231
},
188232
new Result
189233
{
190-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_hibernate_cmd"),
234+
Title = "Hibernate",
191235
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_hibernate"),
192236
Glyph = new GlyphInfo (FontFamily:"/Resources/#Segoe Fluent Icons", Glyph:"\xe945"),
193237
IcoPath = "Images\\hibernate.png",
@@ -198,13 +242,13 @@ private List<Result> Commands()
198242
info.UseShellExecute = true;
199243

200244
ShellCommand.Execute(info);
201-
245+
202246
return true;
203247
}
204248
},
205249
new Result
206250
{
207-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_restart_explorer_cmd"),
251+
Title = "Index Option",
208252
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_indexoption"),
209253
IcoPath = "Images\\indexoption.png",
210254
Glyph = new GlyphInfo (FontFamily:"/Resources/#Segoe Fluent Icons", Glyph:"\xe773"),
@@ -219,7 +263,7 @@ private List<Result> Commands()
219263
},
220264
new Result
221265
{
222-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_emptyrecyclebin_cmd"),
266+
Title = "Empty Recycle Bin",
223267
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_emptyrecyclebin"),
224268
IcoPath = "Images\\recyclebin.png",
225269
Glyph = new GlyphInfo (FontFamily:"/Resources/#Segoe Fluent Icons", Glyph:"\xe74d"),
@@ -242,7 +286,7 @@ private List<Result> Commands()
242286
},
243287
new Result
244288
{
245-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_openrecyclebin_cmd"),
289+
Title = "Open Recycle Bin",
246290
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_openrecyclebin"),
247291
IcoPath = "Images\\openrecyclebin.png",
248292
Glyph = new GlyphInfo (FontFamily:"/Resources/#Segoe Fluent Icons", Glyph:"\xe74d"),
@@ -257,7 +301,7 @@ private List<Result> Commands()
257301
},
258302
new Result
259303
{
260-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_exit_cmd"),
304+
Title = "Exit",
261305
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_exit"),
262306
IcoPath = "Images\\app.png",
263307
Action = c =>
@@ -268,7 +312,7 @@ private List<Result> Commands()
268312
},
269313
new Result
270314
{
271-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_save_all_settings_cmd"),
315+
Title = "Save Settings",
272316
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_save_all_settings"),
273317
IcoPath = "Images\\app.png",
274318
Action = c =>
@@ -281,7 +325,7 @@ private List<Result> Commands()
281325
},
282326
new Result
283327
{
284-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_restart_cmd"),
328+
Title = "Restart Flow Launcher",
285329
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_restart"),
286330
IcoPath = "Images\\app.png",
287331
Action = c =>
@@ -292,7 +336,7 @@ private List<Result> Commands()
292336
},
293337
new Result
294338
{
295-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_setting_cmd"),
339+
Title = "Settings",
296340
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_setting"),
297341
IcoPath = "Images\\app.png",
298342
Action = c =>
@@ -303,7 +347,7 @@ private List<Result> Commands()
303347
},
304348
new Result
305349
{
306-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_reload_plugin_data_cmd"),
350+
Title = "Reload Plugin Data",
307351
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_reload_plugin_data"),
308352
IcoPath = "Images\\app.png",
309353
Action = c =>
@@ -317,13 +361,13 @@ private List<Result> Commands()
317361
context.API.GetTranslation(
318362
"flowlauncher_plugin_sys_dlgtext_all_applicableplugins_reloaded")),
319363
System.Threading.Tasks.TaskScheduler.Current);
320-
364+
321365
return true;
322366
}
323367
},
324368
new Result
325369
{
326-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_check_for_update_cmd"),
370+
Title = "Check For Update",
327371
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_check_for_update"),
328372
IcoPath = "Images\\checkupdate.png",
329373
Action = c =>
@@ -335,7 +379,7 @@ private List<Result> Commands()
335379
},
336380
new Result
337381
{
338-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_open_log_location_cmd"),
382+
Title = "Open Log Location",
339383
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_open_log_location"),
340384
IcoPath = "Images\\app.png",
341385
Action = c =>
@@ -347,7 +391,7 @@ private List<Result> Commands()
347391
},
348392
new Result
349393
{
350-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_open_docs_tips_cmd"),
394+
Title = "Flow Launcher Tips",
351395
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_open_docs_tips"),
352396
IcoPath = "Images\\app.png",
353397
Action = c =>
@@ -358,7 +402,7 @@ private List<Result> Commands()
358402
},
359403
new Result
360404
{
361-
Title = context.API.GetTranslation("flowlauncher_plugin_sys_open_userdata_location_cmd"),
405+
Title = "Flow Launcher UserData Folder",
362406
SubTitle = context.API.GetTranslation("flowlauncher_plugin_sys_open_userdata_location"),
363407
IcoPath = "Images\\app.png",
364408
Action = c =>

0 commit comments

Comments
 (0)