diff --git a/Plugins/Flow.Launcher.Plugin.Sys/Command.cs b/Plugins/Flow.Launcher.Plugin.Sys/Command.cs new file mode 100644 index 000000000..84f5f046f --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.Sys/Command.cs @@ -0,0 +1,17 @@ +using System.Text.Json.Serialization; + +namespace Flow.Launcher.Plugin.Sys +{ + public class Command : BaseModel + { + public string Key { get; set; } + + [JsonIgnore] + public string Name { get; set; } + + [JsonIgnore] + public string Description { get; set; } + + public string Keyword { get; set; } + } +} diff --git a/Plugins/Flow.Launcher.Plugin.Sys/Languages/en.xaml b/Plugins/Flow.Launcher.Plugin.Sys/Languages/en.xaml index 2a266f8f6..e1d83fa38 100644 --- a/Plugins/Flow.Launcher.Plugin.Sys/Languages/en.xaml +++ b/Plugins/Flow.Launcher.Plugin.Sys/Languages/en.xaml @@ -4,8 +4,9 @@ xmlns:system="clr-namespace:System;assembly=mscorlib"> - Command + Name Description + Command Shutdown Restart @@ -29,6 +30,8 @@ Toggle Game Mode Set the Flow Launcher Theme + Edit + Shutdown Computer Restart Computer diff --git a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs index 05687701d..ef9da2719 100644 --- a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs @@ -19,8 +19,36 @@ namespace Flow.Launcher.Plugin.Sys public class Main : IPlugin, ISettingProvider, IPluginI18n { private PluginInitContext context; + private Settings settings; private ThemeSelector themeSelector; - private Dictionary KeywordTitleMappings = new Dictionary(); + + private readonly Dictionary KeywordTitleMappings = new() + { + {"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"}, + {"Set Flow Launcher Theme", "flowlauncher_plugin_sys_theme_selector_cmd"} + }; + private readonly Dictionary KeywordDescriptionMappings = new(); + + private SettingsViewModel _viewModel; // SHTDN_REASON_MAJOR_OTHER indicates a generic shutdown reason that isn't categorized under hardware failure, software updates, or other predefined reasons. // SHTDN_REASON_FLAG_PLANNED marks the shutdown as planned rather than an unexpected shutdown or failure @@ -28,12 +56,8 @@ namespace Flow.Launcher.Plugin.Sys public Control CreateSettingPanel() { - var commands = Commands(); - foreach (var c in commands) - { - c.Title = GetDynamicTitle(null, c); - } - return new SysSettings(commands); + UpdateLocalizedNameDescription(false); + return new SysSettings(_viewModel); } public List Query(Query query) @@ -67,6 +91,29 @@ namespace Flow.Launcher.Plugin.Sys return results; } + private string GetTitle(string key) + { + if (!KeywordTitleMappings.TryGetValue(key, out var translationKey)) + { + Log.Error("Flow.Launcher.Plugin.Sys.Main", $"Title not found for: {key}"); + return "Title Not Found"; + } + + return context.API.GetTranslation(translationKey); + } + + private string GetDescription(string key) + { + if (!KeywordDescriptionMappings.TryGetValue(key, out var translationKey)) + { + Log.Error("Flow.Launcher.Plugin.Sys.Main", $"Description not found for: {key}"); + return "Description Not Found"; + } + + return context.API.GetTranslation(translationKey); + } + + [Obsolete] private string GetDynamicTitle(Query query, Result result) { if (!KeywordTitleMappings.TryGetValue(result.Title, out var translationKey)) @@ -96,30 +143,26 @@ namespace Flow.Launcher.Plugin.Sys public void Init(PluginInitContext context) { this.context = context; + settings = context.API.LoadSettingJsonStorage(); + _viewModel = new SettingsViewModel(settings); themeSelector = new ThemeSelector(context); - KeywordTitleMappings = new Dictionary{ - {"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"}, - {"Set Flow Launcher Theme", "flowlauncher_plugin_sys_theme_selector_cmd"} - }; + foreach (string key in KeywordTitleMappings.Keys) + { + // Remove _cmd in the last of the strings + KeywordDescriptionMappings[key] = KeywordTitleMappings[key][..^4]; + } + } + + private void UpdateLocalizedNameDescription(bool force) + { + if (string.IsNullOrEmpty(settings.Commands[0].Name) || force) + { + foreach (var c in settings.Commands) + { + c.Name = GetTitle(c.Key); + c.Description = GetDescription(c.Key); + } + } } private static unsafe bool EnableShutdownPrivilege() diff --git a/Plugins/Flow.Launcher.Plugin.Sys/Settings.cs b/Plugins/Flow.Launcher.Plugin.Sys/Settings.cs new file mode 100644 index 000000000..f39e6d65f --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.Sys/Settings.cs @@ -0,0 +1,127 @@ +using System.Collections.ObjectModel; +using System.Text.Json.Serialization; + +namespace Flow.Launcher.Plugin.Sys; + +public class Settings : BaseModel +{ + public Settings() + { + if (Commands.Count > 0) + { + SelectedCommand = Commands[0]; + } + } + + public ObservableCollection Commands { get; set; } = new ObservableCollection + { + new() + { + Key = "Shutdown", + Keyword = "Shutdown" + }, + new() + { + Key = "Restart", + Keyword = "Restart" + }, + new() + { + Key = "Restart With Advanced Boot Options", + Keyword = "Restart With Advanced Boot Options" + }, + new() + { + Key = "Log Off/Sign Out", + Keyword = "Log Off/Sign Out" + }, + new() + { + Key = "Lock", + Keyword = "Lock" + }, + new() + { + Key = "Sleep", + Keyword = "Sleep" + }, + new() + { + Key = "Hibernate", + Keyword = "Hibernate" + }, + new() + { + Key = "Index Option", + Keyword = "Index Option" + }, + new() + { + Key = "Empty Recycle Bin", + Keyword = "Empty Recycle Bin" + }, + new() + { + Key = "Open Recycle Bin", + Keyword = "Open Recycle Bin" + }, + new() + { + Key = "Exit", + Keyword = "Exit" + }, + new() + { + Key = "Save Settings", + Keyword = "Save Settings" + }, + new() + { + Key = "Restart Flow Launcher", + Keyword = "Restart Flow Launcher" + }, + new() + { + Key = "Settings", + Keyword = "Settings" + }, + new() + { + Key = "Reload Plugin Data", + Keyword = "Reload Plugin Data" + }, + new() + { + Key = "Check For Update", + Keyword = "Check For Update" + }, + new() + { + Key = "Open Log Location", + Keyword = "Open Log Location" + }, + new() + { + Key = "Flow Launcher Tips", + Keyword = "Flow Launcher Tips" + }, + new() + { + Key = "Flow Launcher UserData Folder", + Keyword = "Flow Launcher UserData Folder" + }, + new() + { + Key = "Toggle Game Mode", + Keyword = "Toggle Game Mode" + }, + new() + { + Key = "Set Flow Launcher Theme", + Keyword = "Set Flow Launcher Theme" + } + }; + + [JsonIgnore] + public Command SelectedCommand { get; set; } +} diff --git a/Plugins/Flow.Launcher.Plugin.Sys/SettingsViewModel.cs b/Plugins/Flow.Launcher.Plugin.Sys/SettingsViewModel.cs new file mode 100644 index 000000000..0755dffa9 --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.Sys/SettingsViewModel.cs @@ -0,0 +1,12 @@ +namespace Flow.Launcher.Plugin.Sys +{ + public class SettingsViewModel + { + public SettingsViewModel(Settings settings) + { + Settings = settings; + } + + public Settings Settings { get; } + } +} diff --git a/Plugins/Flow.Launcher.Plugin.Sys/SysSettings.xaml b/Plugins/Flow.Launcher.Plugin.Sys/SysSettings.xaml index f806900de..67e840e53 100644 --- a/Plugins/Flow.Launcher.Plugin.Sys/SysSettings.xaml +++ b/Plugins/Flow.Launcher.Plugin.Sys/SysSettings.xaml @@ -4,36 +4,66 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:vm="clr-namespace:Flow.Launcher.Plugin.Sys" + d:DataContext="{d:DesignInstance vm:SettingsViewModel}" d:DesignHeight="300" d:DesignWidth="300" mc:Ignorable="d"> - + + + + + + - + - + + - + + + + + + + + + + + +