From 59f0ccebc7b360735b72651a733f9fffc7b85135 Mon Sep 17 00:00:00 2001 From: Heiko <61519853+htcfreek@users.noreply.github.com> Date: Wed, 20 Sep 2023 19:31:40 +0200 Subject: [PATCH] [PT Run > PluginAdditionalOptions] Refactoring and more settings types (#28601) * refactor existing code * fixes * fix combo box layout * improve layout * add more settings types * combined setting * enabled state * fix spelling * improve settings.json handling on null values * textbox improvements * rework xaml code * fix xaml style * spell fixes * spell fixes * update comment --- .../Plugins/Microsoft.Plugin.Shell/Main.cs | 6 +- .../PluginAdditionalOption.cs | 67 ++++++- .../PowerLauncherSettings.cs | 1 + .../SettingsXAML/Views/PowerLauncherPage.xaml | 168 +++++++++++++++++- .../PluginAdditionalOptionViewModel.cs | 75 +++++++- 5 files changed, 296 insertions(+), 21 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs index d458b7996a..dfbdbc8450 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs @@ -52,7 +52,7 @@ namespace Microsoft.Plugin.Shell { Key = "ShellCommandExecution", DisplayLabel = Resources.wox_shell_command_execution, - SelectionTypeValue = (int)PluginAdditionalOption.SelectionType.Combobox, + PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Combobox, ComboBoxOptions = new List { Resources.run_command_in_command_prompt, @@ -60,7 +60,7 @@ namespace Microsoft.Plugin.Shell Resources.find_executable_file_and_run_it, Resources.run_command_in_windows_terminal, }, - Option = (int)_settings.Shell, + ComboBoxValue = (int)_settings.Shell, }, }; @@ -442,7 +442,7 @@ namespace Microsoft.Plugin.Shell _settings.LeaveShellOpen = leaveShellOpen; var optionShell = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "ShellCommandExecution"); - shellOption = optionShell?.Option ?? shellOption; + shellOption = optionShell?.ComboBoxValue ?? shellOption; _settings.Shell = (ExecutionShell)shellOption; } diff --git a/src/settings-ui/Settings.UI.Library/PluginAdditionalOption.cs b/src/settings-ui/Settings.UI.Library/PluginAdditionalOption.cs index 362384c534..54e0c7868e 100644 --- a/src/settings-ui/Settings.UI.Library/PluginAdditionalOption.cs +++ b/src/settings-ui/Settings.UI.Library/PluginAdditionalOption.cs @@ -3,17 +3,28 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Text.Json.Serialization; namespace Microsoft.PowerToys.Settings.UI.Library { public class PluginAdditionalOption { - public enum SelectionType + public enum AdditionalOptionType { Checkbox = 0, Combobox = 1, + Textbox = 2, + Numberbox = 3, + CheckboxAndCombobox = 11, + CheckboxAndTextbox = 12, + CheckboxAndNumberbox = 13, } + /// + /// Gets or sets the layout type of the option in settings ui (Optional; Default is checkbox) + /// + public AdditionalOptionType PluginOptionType { get; set; } + public string Key { get; set; } public string DisplayLabel { get; set; } @@ -21,14 +32,64 @@ namespace Microsoft.PowerToys.Settings.UI.Library /// /// Gets or sets a value to show a description of this setting in the settings ui. (Optional) /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string DisplayDescription { get; set; } + /// + /// Gets or sets a value to show a label for the second setting if two combined settings are shown + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string SecondDisplayLabel { get; set; } + + /// + /// Gets or sets a value to show a description for the second setting in the settings ui if two combined settings are shown. (Optional) + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string SecondDisplayDescription { get; set; } + + /// + /// Gets or sets a value indicating whether the checkbox is set or not set + /// public bool Value { get; set; } + public int ComboBoxValue { get; set; } + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public List ComboBoxOptions { get; set; } - public int Option { get; set; } + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string TextValue { get; set; } - public int SelectionTypeValue { get; set; } + /// + /// Gets or sets the value that specifies the maximum number of characters allowed for user input in the text box. (Optional; Default is 0 which means no limit.) + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public int? TextBoxMaxLength { get; set; } + + public double NumberValue { get; set; } + + /// + /// Gets or sets a minimal value for the number box. (Optional; Default is Double.MinValue) + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public double? NumberBoxMin { get; set; } + + /// + /// Gets or sets a maximal value for the number box. (Optional; Default is Double.MaxValue) + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public double? NumberBoxMax { get; set; } + + /// + /// Gets or sets the value for small changes of the number box. (Optional; Default is 1) + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public double? NumberBoxSmallChange { get; set; } + + /// + /// Gets or sets the value for large changes of the number box. (Optional; Default is 10) + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public double? NumberBoxLargeChange { get; set; } } } diff --git a/src/settings-ui/Settings.UI.Library/PowerLauncherSettings.cs b/src/settings-ui/Settings.UI.Library/PowerLauncherSettings.cs index b1e4cec04d..b85906a001 100644 --- a/src/settings-ui/Settings.UI.Library/PowerLauncherSettings.cs +++ b/src/settings-ui/Settings.UI.Library/PowerLauncherSettings.cs @@ -33,6 +33,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library var options = new JsonSerializerOptions { WriteIndented = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, }; if (settingsUtils == null) diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerLauncherPage.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerLauncherPage.xaml index 94fd68d5bc..9e62c42206 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerLauncherPage.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerLauncherPage.xaml @@ -360,6 +360,7 @@ + - + CornerRadius="0" + Visibility="{x:Bind Path=ShowCheckBox, Converter={StaticResource BoolToVisibilityConverter}}"> + + + + + + + + + + + + + + + + + + IsChecked="{x:Bind Path=Value, Mode=TwoWay}" /> + + + + + + + + - - + IsChecked="{x:Bind Path=Value, Mode=TwoWay}" /> + + + + + + + + + + + + + + + _additionalOption.DisplayLabel; public string DisplayDescription => _additionalOption.DisplayDescription; + // Labels of second setting of combined types + public string SecondDisplayLabel => _additionalOption.SecondDisplayLabel; + + public string SecondDisplayDescription => _additionalOption.SecondDisplayDescription; + + // Bool checkbox setting + public bool ShowCheckBox => _additionalOption.PluginOptionType == PluginAdditionalOption.AdditionalOptionType.Checkbox; + public bool Value { get => _additionalOption.Value; @@ -31,29 +40,83 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { _additionalOption.Value = value; NotifyPropertyChanged(); + NotifyPropertyChanged(nameof(SecondSettingIsEnabled)); } } } + // ComboBox setting + public bool ShowComboBox => _additionalOption.PluginOptionType == PluginAdditionalOption.AdditionalOptionType.Combobox && + _additionalOption.ComboBoxOptions != null && _additionalOption.ComboBoxOptions.Count > 0; + public List ComboBoxOptions => _additionalOption.ComboBoxOptions; - public int Option + public int ComboBoxValue { - get => _additionalOption.Option; + get => _additionalOption.ComboBoxValue; set { - if (value != _additionalOption.Option) + if (value != _additionalOption.ComboBoxValue) { - _additionalOption.Option = value; + _additionalOption.ComboBoxValue = value; NotifyPropertyChanged(); } } } - public bool ShowComboBox => _additionalOption.SelectionTypeValue == (int)PluginAdditionalOption.SelectionType.Combobox && _additionalOption.ComboBoxOptions != null && _additionalOption.ComboBoxOptions.Count > 0; + // TextBox setting + public bool ShowTextBox => _additionalOption.PluginOptionType == PluginAdditionalOption.AdditionalOptionType.Textbox; - public bool ShowCheckBox => _additionalOption.SelectionTypeValue == (int)PluginAdditionalOption.SelectionType.Checkbox; + public int TextBoxMaxLength => (_additionalOption.TextBoxMaxLength == null) ? 0 : _additionalOption.TextBoxMaxLength.Value; // 0 ist the default and means no limit. + public string TextValue + { + get => _additionalOption.TextValue; + set + { + if (value != _additionalOption.TextValue) + { + _additionalOption.TextValue = value; + NotifyPropertyChanged(); + } + } + } + + // NumberBox setting + public bool ShowNumberBox => _additionalOption.PluginOptionType == PluginAdditionalOption.AdditionalOptionType.Numberbox; + + public double NumberBoxMin => (_additionalOption.NumberBoxMin == null) ? double.MinValue : _additionalOption.NumberBoxMin.Value; + + public double NumberBoxMax => (_additionalOption.NumberBoxMax == null) ? double.MaxValue : _additionalOption.NumberBoxMax.Value; + + public double NumberBoxSmallChange => (_additionalOption.NumberBoxSmallChange == null) ? 1 : _additionalOption.NumberBoxSmallChange.Value; + + public double NumberBoxLargeChange => (_additionalOption.NumberBoxLargeChange == null) ? 10 : _additionalOption.NumberBoxLargeChange.Value; + + public double NumberValue + { + get => _additionalOption.NumberValue; + set + { + if (value != _additionalOption.NumberValue) + { + _additionalOption.NumberValue = value; + NotifyPropertyChanged(); + } + } + } + + // Show combined settings cards + public bool ShowCheckboxAndCombobox => _additionalOption.PluginOptionType == PluginAdditionalOption.AdditionalOptionType.CheckboxAndCombobox; + + public bool ShowCheckboxAndTextbox => _additionalOption.PluginOptionType == PluginAdditionalOption.AdditionalOptionType.CheckboxAndTextbox; + + public bool ShowCheckboxAndNumberbox => _additionalOption.PluginOptionType == PluginAdditionalOption.AdditionalOptionType.CheckboxAndNumberbox; + + // Enabled state of ComboBox, TextBox, NumberBox (If combined with checkbox then checkbox value decides it.) + public bool SecondSettingIsEnabled => (int)_additionalOption.PluginOptionType > 10 ? _additionalOption.Value : true; + + // Handle property changes public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")