Skip to content

Commit

Permalink
Merge pull request #2468 from flooxo/sys_cmd
Browse files Browse the repository at this point in the history
Add translation keys for System Command Plugin
  • Loading branch information
VictoriousRaptor authored Jan 28, 2024
2 parents 097bc69 + 82a8d56 commit 1e45894
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
22 changes: 22 additions & 0 deletions Plugins/Flow.Launcher.Plugin.Sys/Languages/en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@
<system:String x:Key="flowlauncher_plugin_sys_command">Command</system:String>
<system:String x:Key="flowlauncher_plugin_sys_desc">Description</system:String>

<system:String x:Key="flowlauncher_plugin_sys_shutdown_computer_cmd">Shutdown</system:String>
<system:String x:Key="flowlauncher_plugin_sys_restart_computer_cmd">Restart</system:String>
<system:String x:Key="flowlauncher_plugin_sys_restart_advanced_cmd">Restart With Advanced Boot Options</system:String>
<system:String x:Key="flowlauncher_plugin_sys_log_off_cmd">Log Off/Sign Out</system:String>
<system:String x:Key="flowlauncher_plugin_sys_lock_cmd">Lock</system:String>
<system:String x:Key="flowlauncher_plugin_sys_sleep_cmd">Sleep</system:String>
<system:String x:Key="flowlauncher_plugin_sys_hibernate_cmd">Hibernate</system:String>
<system:String x:Key="flowlauncher_plugin_sys_indexoption_cmd">Index Option</system:String>
<system:String x:Key="flowlauncher_plugin_sys_emptyrecyclebin_cmd">Empty Recycle Bin</system:String>
<system:String x:Key="flowlauncher_plugin_sys_openrecyclebin_cmd">Open Recycle Bin</system:String>
<system:String x:Key="flowlauncher_plugin_sys_exit_cmd">Exit</system:String>
<system:String x:Key="flowlauncher_plugin_sys_save_all_settings_cmd">Save Settings</system:String>
<system:String x:Key="flowlauncher_plugin_sys_restart_cmd">Restart Flow Launcher"</system:String>
<system:String x:Key="flowlauncher_plugin_sys_setting_cmd">Settings</system:String>
<system:String x:Key="flowlauncher_plugin_sys_reload_plugin_data_cmd">Reload Plugin Data</system:String>
<system:String x:Key="flowlauncher_plugin_sys_check_for_update_cmd">Check For Update</system:String>
<system:String x:Key="flowlauncher_plugin_sys_open_log_location_cmd">Open Log Location</system:String>
<system:String x:Key="flowlauncher_plugin_sys_open_docs_tips_cmd">Flow Launcher Tips</system:String>
<system:String x:Key="flowlauncher_plugin_sys_open_userdata_location_cmd">Flow Launcher UserData Folder</system:String>
<system:String x:Key="flowlauncher_plugin_sys_toggle_game_mode_cmd">Toggle Game Mode</system:String>

<!-- Command Descriptions -->
<system:String x:Key="flowlauncher_plugin_sys_shutdown_computer">Shutdown Computer</system:String>
<system:String x:Key="flowlauncher_plugin_sys_restart_computer">Restart Computer</system:String>
<system:String x:Key="flowlauncher_plugin_sys_restart_advanced">Restart the computer with Advanced Boot Options for Safe and Debugging modes, as well as other options</system:String>
Expand Down
56 changes: 52 additions & 4 deletions Plugins/Flow.Launcher.Plugin.Sys/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin.SharedCommands;
using Application = System.Windows.Application;
Expand All @@ -19,6 +21,7 @@ namespace Flow.Launcher.Plugin.Sys
public class Main : IPlugin, ISettingProvider, IPluginI18n
{
private PluginInitContext context;
private Dictionary<string, string> KeywordTitleMappings = new Dictionary<string, string>();

#region DllImport

Expand Down Expand Up @@ -59,6 +62,8 @@ public List<Result> Query(Query query)
var results = new List<Result>();
foreach (var c in commands)
{
c.Title = GetDynamicTitle(query, c);

var titleMatch = StringMatcher.FuzzySearch(query.Search, c.Title);
var subTitleMatch = StringMatcher.FuzzySearch(query.Search, c.SubTitle);

Expand All @@ -77,9 +82,52 @@ public List<Result> Query(Query query)
return results;
}

private string GetDynamicTitle(Query query, Result result)
{
if (!KeywordTitleMappings.TryGetValue(result.Title, out var translationKey))
{
Log.Error("Flow.Launcher.Plugin.Sys.Main", $"Dynamic Title not found for: {result.Title}");
return "Title Not Found";
}

var translatedTitle = context.API.GetTranslation(translationKey);

if (result.Title == translatedTitle)
{
return result.Title;
}

var englishTitleMatch = StringMatcher.FuzzySearch(query.Search, result.Title);
var translatedTitleMatch = StringMatcher.FuzzySearch(query.Search, translatedTitle);

return englishTitleMatch.Score >= translatedTitleMatch.Score ? result.Title : translatedTitle;
}

public void Init(PluginInitContext context)
{
this.context = context;
KeywordTitleMappings = new Dictionary<string, string>{
{"Shutdown", "flowlauncher_plugin_sys_shutdown_computer_cmd"},
{"Restart", "flowlauncher_plugin_sys_restart_computer_cmd"},
{"Restart With Advanced Boot Options", "flowlauncher_plugin_sys_restart_advanced_cmd"},
{"Log Off/Sign Out", "flowlauncher_plugin_sys_log_off_cmd"},
{"Lock", "flowlauncher_plugin_sys_lock_cmd"},
{"Sleep", "flowlauncher_plugin_sys_sleep_cmd"},
{"Hibernate", "flowlauncher_plugin_sys_hibernate_cmd"},
{"Index Option", "flowlauncher_plugin_sys_indexoption_cmd"},
{"Empty Recycle Bin", "flowlauncher_plugin_sys_emptyrecyclebin_cmd"},
{"Open Recycle Bin", "flowlauncher_plugin_sys_openrecyclebin_cmd"},
{"Exit", "flowlauncher_plugin_sys_exit_cmd"},
{"Save Settings", "flowlauncher_plugin_sys_save_all_settings_cmd"},
{"Restart Flow Launcher", "flowlauncher_plugin_sys_restart_cmd"},
{"Settings", "flowlauncher_plugin_sys_setting_cmd"},
{"Reload Plugin Data", "flowlauncher_plugin_sys_reload_plugin_data_cmd"},
{"Check For Update", "flowlauncher_plugin_sys_check_for_update_cmd"},
{"Open Log Location", "flowlauncher_plugin_sys_open_log_location_cmd"},
{"Flow Launcher Tips", "flowlauncher_plugin_sys_open_docs_tips_cmd"},
{"Flow Launcher UserData Folder", "flowlauncher_plugin_sys_open_userdata_location_cmd"},
{"Toggle Game Mode", "flowlauncher_plugin_sys_toggle_game_mode_cmd"}
};
}

private List<Result> Commands()
Expand Down Expand Up @@ -139,7 +187,7 @@ private List<Result> Commands()
context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_restart_computer_advanced"),
context.API.GetTranslation("flowlauncher_plugin_sys_restart_computer"),
MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
Process.Start("shutdown", "/r /o /t 0");
Expand All @@ -158,7 +206,7 @@ private List<Result> Commands()
context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_logoff_computer"),
context.API.GetTranslation("flowlauncher_plugin_sys_log_off"),
MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
ExitWindowsEx(EWX_LOGOFF, 0);
Expand Down Expand Up @@ -198,7 +246,7 @@ private List<Result> Commands()
info.UseShellExecute = true;
ShellCommand.Execute(info);
return true;
}
},
Expand Down Expand Up @@ -317,7 +365,7 @@ private List<Result> Commands()
context.API.GetTranslation(
"flowlauncher_plugin_sys_dlgtext_all_applicableplugins_reloaded")),
System.Threading.Tasks.TaskScheduler.Current);
return true;
}
},
Expand Down

0 comments on commit 1e45894

Please sign in to comment.