Skip to content

Commit

Permalink
[PTRun][Shell]Shell selection and combobox improvements (#28972)
Browse files Browse the repository at this point in the history
* Make combobox items sortable

* Update plugin settings

* settings description

* fix settings hang on outdated plugin code

* spell fixes, shell implementation, translation improvements

* rename property

* backward compatibility

* comment changes

* comment changes

* review feedback 1

* review feedback 2

* Code clean up
  • Loading branch information
htcfreek authored Oct 11, 2023
1 parent 5d669df commit 19827d0
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 47 deletions.
78 changes: 62 additions & 16 deletions src/modules/launcher/Plugins/Microsoft.Plugin.Shell/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,31 @@ public class Main : IPlugin, IPluginI18n, ISettingProvider, IContextMenu, ISavab

public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
{
new PluginAdditionalOption()
{
Key = "LeaveShellOpen",
DisplayLabel = Resources.wox_leave_shell_open,
Value = _settings.LeaveShellOpen,
},

new PluginAdditionalOption()
{
Key = "ShellCommandExecution",
DisplayLabel = Resources.wox_shell_command_execution,
DisplayDescription = Resources.wox_shell_command_execution_description,
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Combobox,
ComboBoxOptions = new List<string>
ComboBoxItems = new List<KeyValuePair<string, string>>
{
Resources.run_command_in_command_prompt,
Resources.run_command_in_powershell,
Resources.find_executable_file_and_run_it,
Resources.run_command_in_windows_terminal,
new KeyValuePair<string, string>(Resources.find_executable_file_and_run_it, "2"),
new KeyValuePair<string, string>(Resources.run_command_in_command_prompt, "0"),
new KeyValuePair<string, string>(Resources.run_command_in_powershell, "1"),
new KeyValuePair<string, string>(Resources.run_command_in_powershell_seven, "6"),
new KeyValuePair<string, string>(Resources.run_command_in_windows_terminal_cmd, "5"),
new KeyValuePair<string, string>(Resources.run_command_in_windows_terminal_powershell, "3"),
new KeyValuePair<string, string>(Resources.run_command_in_windows_terminal_powershell_seven, "4"),
},
ComboBoxValue = (int)_settings.Shell,
},

new PluginAdditionalOption()
{
Key = "LeaveShellOpen",
DisplayLabel = Resources.wox_leave_shell_open,
Value = _settings.LeaveShellOpen,
},
};

private PluginInitContext _context;
Expand Down Expand Up @@ -212,21 +216,63 @@ private ProcessStartInfo PrepareProcessStartInfo(string command, RunAsType runAs
}
else
{
arguments = $"\"{command} ; Read-Host -Prompt \\\"Press Enter to continue\\\"\"";
arguments = $"\"{command} ; Read-Host -Prompt \\\"{Resources.run_plugin_cmd_wait_message}\\\"\"";
}

info = ShellCommand.SetProcessStartInfo("powershell.exe", workingDirectory, arguments, runAsVerbArg);
}
else if (_settings.Shell == ExecutionShell.WindowsTerminal)
else if (_settings.Shell == ExecutionShell.PowerShellSeven)
{
string arguments;
if (_settings.LeaveShellOpen)
{
arguments = $"-NoExit -C \"{command}\"";
}
else
{
arguments = $"-C \"{command} ; Read-Host -Prompt \\\"{Resources.run_plugin_cmd_wait_message}\\\"\"";
}

info = ShellCommand.SetProcessStartInfo("pwsh.exe", workingDirectory, arguments, runAsVerbArg);
}
else if (_settings.Shell == ExecutionShell.WindowsTerminalCmd)
{
string arguments;
if (_settings.LeaveShellOpen)
{
arguments = $"cmd.exe /k \"{command}\"";
}
else
{
arguments = $"cmd.exe /c \"{command}\" & pause";
}

info = ShellCommand.SetProcessStartInfo("wt.exe", workingDirectory, arguments, runAsVerbArg);
}
else if (_settings.Shell == ExecutionShell.WindowsTerminalPowerShell)
{
string arguments;
if (_settings.LeaveShellOpen)
{
arguments = $"powershell -NoExit -C \"{command}\"";
}
else
{
arguments = $"powershell -C \"{command}\"";
}

info = ShellCommand.SetProcessStartInfo("wt.exe", workingDirectory, arguments, runAsVerbArg);
}
else if (_settings.Shell == ExecutionShell.WindowsTerminalPowerShellSeven)
{
string arguments;
if (_settings.LeaveShellOpen)
{
arguments = $"powershell -NoExit \"{command}\"";
arguments = $"pwsh.exe -NoExit -C \"{command}\"";
}
else
{
arguments = $"powershell \"{command}\"";
arguments = $"pwsh.exe -C \"{command}\"";
}

info = ShellCommand.SetProcessStartInfo("wt.exe", workingDirectory, arguments, runAsVerbArg);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,34 @@
<value>Keep shell open</value>
</data>
<data name="wox_shell_command_execution" xml:space="preserve">
<value>Shell command execution</value>
<value>Command execution</value>
</data>
<data name="run_command_in_command_prompt" xml:space="preserve">
<value>Run command in Command Prompt (cmd.exe)</value>
<value>Run in Command Prompt (cmd.exe)</value>
</data>
<data name="run_command_in_powershell" xml:space="preserve">
<value>Run command in PowerShell (PowerShell.exe)</value>
<value>Run in PowerShell (PowerShell.exe)</value>
</data>
<data name="find_executable_file_and_run_it" xml:space="preserve">
<value>Find executable file and run it</value>
<value>Find and run the executable file</value>
</data>
<data name="run_command_in_windows_terminal" xml:space="preserve">
<value>Run command in Windows Terminal (wt.exe)</value>
<data name="run_command_in_powershell_seven" xml:space="preserve">
<value>Run in PowerShell 7 (pwsh.exe)</value>
</data>
<data name="run_command_in_windows_terminal_cmd" xml:space="preserve">
<value>Run in Command Prompt (cmd.exe) using Windows Terminal</value>
</data>
<data name="run_command_in_windows_terminal_powershell" xml:space="preserve">
<value>Run in PowerShell (PowerShell.exe) using Windows Terminal</value>
</data>
<data name="run_command_in_windows_terminal_powershell_seven" xml:space="preserve">
<value>Run in PowerShell 7 (pwsh.exe) using Windows Terminal</value>
</data>
<data name="run_plugin_cmd_wait_message" xml:space="preserve">
<value>Press Enter to continue</value>
<comment>"Enter" means the Enter key on the keyboard.</comment>
</data>
<data name="wox_shell_command_execution_description" xml:space="preserve">
<value>All entries using the Windows Terminal force the Windows Terminal as the console host regardless of the system settings</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public enum ExecutionShell
Cmd = 0,
Powershell = 1,
RunCommand = 2,
WindowsTerminal = 3,
WindowsTerminalPowerShell = 3,
WindowsTerminalPowerShellSeven = 4,
WindowsTerminalCmd = 5,
PowerShellSeven = 6,
}
}
17 changes: 16 additions & 1 deletion src/settings-ui/Settings.UI.Library/PluginAdditionalOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ public enum AdditionalOptionType

public int ComboBoxValue { get; set; }

/// <summary>
/// Gets or sets the list of dropdown items for the ComboBox. Please use the item name as Key and an integer as Value.
/// The value gets converted in settings UI to an integer and will be saved in <see cref="ComboBoxValue"/>.
/// You can define the visibility order in settings ui by arranging the list items.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public List<string> ComboBoxOptions { get; set; }
public List<KeyValuePair<string, string>> ComboBoxItems { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string TextValue { get; set; }
Expand Down Expand Up @@ -91,5 +96,15 @@ public enum AdditionalOptionType
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public double? NumberBoxLargeChange { get; set; }

// Outdated properties kept for backward compatibility with third-party plugins. (They are only required to not have old third-party plugins crashing when propagating their plugin options.)
#pragma warning disable SA1623 // Property summary documentation should match accessors

/// <summary>
/// PLEASE DON'T USE ANYMORE!! (The property was used for the list of combobox items in the past and is not functional anymore.)
/// </summary>
[JsonIgnore]
public List<string> ComboBoxOptions { get; set; }
#pragma warning restore SA1623 // Property summary documentation should match accessors
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,8 @@
<ToggleSwitch x:Uid="ToggleSwitch" IsOn="{x:Bind ViewModel.TabSelectsContextButtons, Mode=TwoWay}" />
</controls:SettingsCard>

<controls:SettingsCard
x:Uid="PowerLauncher_UsePinyin"
HeaderIcon="{ui:FontIcon FontFamily={StaticResource SymbolThemeFontFamily}, Glyph=&#xE98A;}">
<ToggleSwitch
x:Uid="ToggleSwitch"
IsOn="{x:Bind ViewModel.UsePinyin, Mode=TwoWay}" />
<controls:SettingsCard x:Uid="PowerLauncher_UsePinyin" HeaderIcon="{ui:FontIcon FontFamily={StaticResource SymbolThemeFontFamily}, Glyph=&#xE98A;}">
<ToggleSwitch x:Uid="ToggleSwitch" IsOn="{x:Bind ViewModel.UsePinyin, Mode=TwoWay}" />
</controls:SettingsCard>

<controls:SettingsCard x:Uid="PowerLauncher_GenerateThumbnailsFromFiles">
Expand Down Expand Up @@ -388,8 +384,10 @@
Visibility="{x:Bind Path=ShowComboBox, Converter={StaticResource BoolToVisibilityConverter}}">
<ComboBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
ItemsSource="{x:Bind Path=ComboBoxOptions}"
SelectedIndex="{x:Bind Path=ComboBoxValue, Mode=TwoWay}" />
DisplayMemberPath="Key"
ItemsSource="{x:Bind Path=ComboBoxItems}"
SelectedValue="{x:Bind Path=ComboBoxValue, Mode=TwoWay}"
SelectedValuePath="Value" />
</controls:SettingsCard>
<!-- TextBox setting -->
<controls:SettingsCard
Expand Down Expand Up @@ -456,8 +454,10 @@
IsEnabled="{x:Bind Path=SecondSettingIsEnabled, Mode=OneWay}">
<ComboBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
ItemsSource="{x:Bind Path=ComboBoxOptions}"
SelectedIndex="{x:Bind Path=ComboBoxValue, Mode=TwoWay}" />
DisplayMemberPath="Key"
ItemsSource="{x:Bind Path=ComboBoxItems}"
SelectedValue="{x:Bind Path=ComboBoxValue, Mode=TwoWay}"
SelectedValuePath="Value" />
</controls:SettingsCard>
</StackPanel>
<!-- Checkbox And TextBox setting -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using Microsoft.PowerToys.Settings.UI.Library;

Expand Down Expand Up @@ -47,18 +48,18 @@ public bool Value

// ComboBox setting
public bool ShowComboBox => _additionalOption.PluginOptionType == PluginAdditionalOption.AdditionalOptionType.Combobox &&
_additionalOption.ComboBoxOptions != null && _additionalOption.ComboBoxOptions.Count > 0;
_additionalOption.ComboBoxItems != null && _additionalOption.ComboBoxItems.Count > 0;

public List<string> ComboBoxOptions => _additionalOption.ComboBoxOptions;
public List<KeyValuePair<string, string>> ComboBoxItems => _additionalOption.ComboBoxItems;

public int ComboBoxValue
public string ComboBoxValue
{
get => _additionalOption.ComboBoxValue;
get => _additionalOption.ComboBoxValue.ToString(CultureInfo.InvariantCulture);
set
{
if (value != _additionalOption.ComboBoxValue)
if (int.Parse(value, CultureInfo.InvariantCulture) != _additionalOption.ComboBoxValue)
{
_additionalOption.ComboBoxValue = value;
_additionalOption.ComboBoxValue = int.Parse(value, CultureInfo.InvariantCulture);
NotifyPropertyChanged();
}
}
Expand Down

0 comments on commit 19827d0

Please sign in to comment.