[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
This commit is contained in:
Heiko 2023-09-20 19:31:40 +02:00 committed by GitHub
parent 5ddd8d72dd
commit 59f0ccebc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 296 additions and 21 deletions

View File

@ -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<string>
{
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;
}

View File

@ -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,
}
/// <summary>
/// Gets or sets the layout type of the option in settings ui (Optional; Default is checkbox)
/// </summary>
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
/// <summary>
/// Gets or sets a value to show a description of this setting in the settings ui. (Optional)
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string DisplayDescription { get; set; }
/// <summary>
/// Gets or sets a value to show a label for the second setting if two combined settings are shown
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string SecondDisplayLabel { get; set; }
/// <summary>
/// Gets or sets a value to show a description for the second setting in the settings ui if two combined settings are shown. (Optional)
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string SecondDisplayDescription { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the checkbox is set or not set
/// </summary>
public bool Value { get; set; }
public int ComboBoxValue { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public List<string> ComboBoxOptions { get; set; }
public int Option { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string TextValue { get; set; }
public int SelectionTypeValue { get; set; }
/// <summary>
/// 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.)
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? TextBoxMaxLength { get; set; }
public double NumberValue { get; set; }
/// <summary>
/// Gets or sets a minimal value for the number box. (Optional; Default is Double.MinValue)
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public double? NumberBoxMin { get; set; }
/// <summary>
/// Gets or sets a maximal value for the number box. (Optional; Default is Double.MaxValue)
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public double? NumberBoxMax { get; set; }
/// <summary>
/// Gets or sets the value for small changes of the number box. (Optional; Default is 1)
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public double? NumberBoxSmallChange { get; set; }
/// <summary>
/// Gets or sets the value for large changes of the number box. (Optional; Default is 10)
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public double? NumberBoxLargeChange { get; set; }
}
}

View File

@ -33,6 +33,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
var options = new JsonSerializerOptions
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
if (settingsUtils == null)

View File

@ -360,6 +360,7 @@
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="ViewModels:PluginAdditionalOptionViewModel">
<StackPanel HorizontalAlignment="Stretch" Orientation="Vertical">
<!-- Checkbox setting -->
<controls:SettingsCard
MinHeight="0"
Margin="1"
@ -367,23 +368,172 @@
Background="Transparent"
BorderThickness="0,0,0,0"
ContentAlignment="Left"
CornerRadius="0">
<StackPanel Orientation="Vertical">
CornerRadius="0"
Visibility="{x:Bind Path=ShowCheckBox, Converter={StaticResource BoolToVisibilityConverter}}">
<custom:CheckBoxWithDescriptionControl
Margin="56,0,0,0"
Description="{x:Bind Path=DisplayDescription}"
Header="{x:Bind Path=DisplayLabel}"
IsChecked="{x:Bind Path=Value, Mode=TwoWay}"
Visibility="{x:Bind Path=ShowCheckBox, Converter={StaticResource BoolToVisibilityConverter}}" />
IsChecked="{x:Bind Path=Value, Mode=TwoWay}" />
</controls:SettingsCard>
<!-- ComboBox setting -->
<controls:SettingsCard
MinHeight="0"
Margin="56,8,45,8"
Background="Transparent"
BorderThickness="0,0,0,0"
CornerRadius="0"
Description="{x:Bind Path=DisplayDescription}"
Header="{x:Bind Path=DisplayLabel}"
Visibility="{x:Bind Path=ShowComboBox, Converter={StaticResource BoolToVisibilityConverter}}">
<ComboBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
ItemsSource="{x:Bind Path=ComboBoxOptions}"
SelectedIndex="{x:Bind Path=ComboBoxValue, Mode=TwoWay}" />
</controls:SettingsCard>
<!-- TextBox setting -->
<controls:SettingsCard
MinHeight="0"
Margin="56,8,45,8"
Background="Transparent"
BorderThickness="0,0,0,0"
CornerRadius="0"
Description="{x:Bind Path=DisplayDescription}"
Header="{x:Bind Path=DisplayLabel}"
Visibility="{x:Bind Path=ShowTextBox, Converter={StaticResource BoolToVisibilityConverter}}">
<TextBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
MaxWidth="450"
MaxLength="{x:Bind Path=TextBoxMaxLength, Mode=OneWay}"
Text="{x:Bind Path=TextValue, Mode=TwoWay}" />
</controls:SettingsCard>
<!-- NumberBox setting -->
<controls:SettingsCard
MinHeight="0"
Margin="56,8,45,8"
Background="Transparent"
BorderThickness="0,0,0,0"
CornerRadius="0"
Description="{x:Bind Path=DisplayDescription}"
Header="{x:Bind Path=DisplayLabel}"
Visibility="{x:Bind Path=ShowNumberBox, Converter={StaticResource BoolToVisibilityConverter}}">
<NumberBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
LargeChange="{x:Bind Path=NumberBoxLargeChange, Mode=OneWay}"
Maximum="{x:Bind Path=NumberBoxMax, Mode=OneWay}"
Minimum="{x:Bind Path=NumberBoxMin, Mode=OneWay}"
SmallChange="{x:Bind Path=NumberBoxSmallChange, Mode=OneWay}"
SpinButtonPlacementMode="Compact"
Value="{x:Bind Path=NumberValue, Mode=TwoWay}" />
</controls:SettingsCard>
<!-- Checkbox And ComboBox setting -->
<StackPanel
HorizontalAlignment="Stretch"
Orientation="Vertical"
Visibility="{x:Bind Path=ShowCheckboxAndCombobox, Converter={StaticResource BoolToVisibilityConverter}}">
<controls:SettingsCard
MinHeight="0"
Margin="1"
Padding="0,6,0,4"
Background="Transparent"
BorderThickness="0,0,0,0"
ContentAlignment="Left"
CornerRadius="0">
<custom:CheckBoxWithDescriptionControl
Margin="56,0,0,0"
Description="{x:Bind Path=DisplayDescription}"
Header="{x:Bind Path=DisplayLabel}"
ItemsSource="{x:Bind Path=ComboBoxOptions}"
SelectedIndex="{x:Bind Path=Option, Mode=TwoWay}"
Visibility="{x:Bind Path=ShowComboBox, Converter={StaticResource BoolToVisibilityConverter}}" />
</StackPanel>
IsChecked="{x:Bind Path=Value, Mode=TwoWay}" />
</controls:SettingsCard>
<controls:SettingsCard
MinHeight="0"
Margin="84,0,45,8"
Background="Transparent"
BorderThickness="0,0,0,0"
CornerRadius="0"
Description="{x:Bind Path=SecondDisplayDescription}"
Header="{x:Bind Path=SecondDisplayLabel}"
IsEnabled="{x:Bind Path=SecondSettingIsEnabled, Mode=OneWay}">
<ComboBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
ItemsSource="{x:Bind Path=ComboBoxOptions}"
SelectedIndex="{x:Bind Path=ComboBoxValue, Mode=TwoWay}" />
</controls:SettingsCard>
</StackPanel>
<!-- Checkbox And TextBox setting -->
<StackPanel
HorizontalAlignment="Stretch"
Orientation="Vertical"
Visibility="{x:Bind Path=ShowCheckboxAndTextbox, Converter={StaticResource BoolToVisibilityConverter}}">
<controls:SettingsCard
MinHeight="0"
Margin="1"
Padding="0,6,0,4"
Background="Transparent"
BorderThickness="0,0,0,0"
ContentAlignment="Left"
CornerRadius="0">
<custom:CheckBoxWithDescriptionControl
Margin="56,0,0,0"
Description="{x:Bind Path=DisplayDescription}"
Header="{x:Bind Path=DisplayLabel}"
IsChecked="{x:Bind Path=Value, Mode=TwoWay}" />
</controls:SettingsCard>
<controls:SettingsCard
MinHeight="0"
Margin="84,0,45,8"
Background="Transparent"
BorderThickness="0,0,0,0"
CornerRadius="0"
Description="{x:Bind Path=SecondDisplayDescription}"
Header="{x:Bind Path=SecondDisplayLabel}"
IsEnabled="{x:Bind Path=SecondSettingIsEnabled, Mode=OneWay}">
<TextBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
MaxWidth="450"
MaxLength="{x:Bind Path=TextBoxMaxLength, Mode=OneWay}"
Text="{x:Bind Path=TextValue, Mode=TwoWay}" />
</controls:SettingsCard>
</StackPanel>
<!-- Checkbox And NumberBox setting -->
<StackPanel
HorizontalAlignment="Stretch"
Orientation="Vertical"
Visibility="{x:Bind Path=ShowCheckboxAndNumberbox, Converter={StaticResource BoolToVisibilityConverter}}">
<controls:SettingsCard
MinHeight="0"
Margin="1"
Padding="0,6,0,4"
Background="Transparent"
BorderThickness="0,0,0,0"
ContentAlignment="Left"
CornerRadius="0">
<custom:CheckBoxWithDescriptionControl
Margin="56,0,0,0"
Description="{x:Bind Path=DisplayDescription}"
Header="{x:Bind Path=DisplayLabel}"
IsChecked="{x:Bind Path=Value, Mode=TwoWay}" />
</controls:SettingsCard>
<controls:SettingsCard
MinHeight="0"
Margin="84,0,45,8"
Background="Transparent"
BorderThickness="0,0,0,0"
CornerRadius="0"
Description="{x:Bind Path=SecondDisplayDescription}"
Header="{x:Bind Path=SecondDisplayLabel}"
IsEnabled="{x:Bind Path=SecondSettingIsEnabled, Mode=OneWay}">
<NumberBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
LargeChange="{x:Bind Path=NumberBoxLargeChange, Mode=OneWay}"
Maximum="{x:Bind Path=NumberBoxMax, Mode=OneWay}"
Minimum="{x:Bind Path=NumberBoxMin, Mode=OneWay}"
SmallChange="{x:Bind Path=NumberBoxSmallChange, Mode=OneWay}"
SpinButtonPlacementMode="Compact"
Value="{x:Bind Path=NumberValue, Mode=TwoWay}" />
</controls:SettingsCard>
</StackPanel>
<!-- Separator line -->
<Rectangle
Height="1"
HorizontalAlignment="Stretch"

View File

@ -18,10 +18,19 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
_additionalOption = additionalOption;
}
// Labels of single and first setting of combined types
public string DisplayLabel => _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<string> 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 = "")