Skip to content

Commit 2276df7

Browse files
authored
Run with PowerShell button on toolbar (#7767)
1 parent 432a541 commit 2276df7

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

src/Files/Strings/en-US/Resources.resw

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2699,4 +2699,10 @@ We use App Center to track which settings are being used, find bugs, and fix cra
26992699
<data name="Bytes" xml:space="preserve">
27002700
<value>Bytes</value>
27012701
</data>
2702-
</root>
2702+
<data name="RunScript" xml:space="preserve">
2703+
<value>Run script</value>
2704+
</data>
2705+
<data name="RunWithPowerShell" xml:space="preserve">
2706+
<value>Run with PowerShell</value>
2707+
</data>
2708+
</root>

src/Files/UserControls/InnerNavigationToolbar.xaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@
189189
x:Name="PropertiesButton"
190190
Width="Auto"
191191
MinWidth="40"
192+
AccessKey="O"
193+
AutomationProperties.Name="{helpers:ResourceString Name=PropertiesTitle}"
192194
Command="{x:Bind ViewModel.PropertiesCommand, Mode=OneWay}"
193195
IsEnabled="{x:Bind converters:MultiBooleanConverter.AndConvert(ViewModel.CanViewProperties, ViewModel.InstanceViewModel.IsPageTypeNotHome), Mode=OneWay, FallbackValue=False}"
194196
Label="{helpers:ResourceString Name=PropertiesTitle}"
195-
AutomationProperties.Name="{helpers:ResourceString Name=PropertiesTitle}"
196-
AccessKey="O"
197197
LabelPosition="Collapsed"
198198
ToolTipService.ToolTip="{helpers:ResourceString Name=PropertiesTitle}">
199199
<AppBarButton.Content>
@@ -213,6 +213,18 @@
213213
<FontIcon FontFamily="{StaticResource RecycleBinIcons}" Glyph="&#xEF88;" />
214214
</AppBarButton.Icon>
215215
</AppBarButton>
216+
<AppBarButton
217+
x:Name="RunWithPowerShellButton"
218+
x:Load="{x:Bind ViewModel.IsPowerShellScript, Mode=OneWay, FallbackValue=False}"
219+
AutomationProperties.Name="RunWithPowerShell"
220+
Command="{x:Bind ViewModel.RunWithPowerShellCommand, Mode=OneWay}"
221+
Label="{helpers:ResourceString Name=RunScript}"
222+
LabelPosition="Default"
223+
ToolTipService.ToolTip="{helpers:ResourceString Name=RunWithPowerShell}">
224+
<AppBarButton.Icon>
225+
<FontIcon Glyph="&#xE756;" />
226+
</AppBarButton.Icon>
227+
</AppBarButton>
216228
</CommandBar.PrimaryCommands>
217229
</CommandBar>
218230
<CommandBar

src/Files/ViewModels/NavToolbarViewModel.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,8 @@ public void SearchRegion_LostFocus(object sender, RoutedEventArgs e)
797797

798798
public ICommand PropertiesCommand { get; set; }
799799

800+
public ICommand RunWithPowerShellCommand { get; set; }
801+
800802
public async Task SetPathBoxDropDownFlyoutAsync(MenuFlyout flyout, PathBoxItem pathItem, IShellPage shellPage)
801803
{
802804
var nextPathItemTitle = PathComponents[PathComponents.IndexOf(pathItem) + 1].Title;
@@ -1079,19 +1081,35 @@ public List<ListedItem> SelectedItems
10791081
OnPropertyChanged(nameof(CanCopy));
10801082
OnPropertyChanged(nameof(CanShare));
10811083
OnPropertyChanged(nameof(CanRename));
1084+
OnPropertyChanged(nameof(IsPowerShellScript));
1085+
OnPropertyChanged(nameof(HasAdditionnalAction));
10821086
OnPropertyChanged(nameof(CanViewProperties));
10831087
}
10841088
}
10851089
}
10861090

1087-
public bool HasAdditionnalAction => InstanceViewModel.IsPageTypeRecycleBin;
1091+
public bool HasAdditionnalAction
1092+
{
1093+
get
1094+
{
1095+
if (InstanceViewModel.IsPageTypeRecycleBin)
1096+
return true;
1097+
1098+
if (IsPowerShellScript)
1099+
return true;
1100+
1101+
return false;
1102+
}
1103+
}
10881104

10891105
public bool CanCopy => SelectedItems is not null && SelectedItems.Any();
10901106
public bool CanShare => SelectedItems is not null && SelectedItems.Any() && DataTransferManager.IsSupported() && !SelectedItems.Any(x => (x.IsShortcutItem && !x.IsLinkItem) || x.IsHiddenItem || (x.PrimaryItemAttribute == StorageItemTypes.Folder && !x.IsZipItem));
10911107
public bool CanRename => SelectedItems is not null && SelectedItems.Count == 1;
10921108
public bool CanViewProperties => SelectedItems is not null && SelectedItems.Any();
10931109
public bool CanEmptyRecycleBin => InstanceViewModel.IsPageTypeRecycleBin && HasItem;
10941110

1111+
public bool IsPowerShellScript => SelectedItems is not null && SelectedItems.Any() && SelectedItems.First().PrimaryItemAttribute == StorageItemTypes.File && new[] { ".ps1", }.Contains(SelectedItems.First().FileExtension, StringComparer.OrdinalIgnoreCase);
1112+
10951113
public void Dispose()
10961114
{
10971115
SearchBox.Escaped -= SearchRegion_Escaped;

src/Files/Views/ModernShellPage.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
using Windows.UI.Xaml.Media;
3434
using Windows.UI.Xaml.Media.Animation;
3535
using Windows.UI.Xaml.Navigation;
36-
3736
using SortDirection = Files.Enums.SortDirection;
3837

3938
namespace Files.Views
@@ -204,6 +203,7 @@ private void InitToolbarCommands()
204203
NavToolbarViewModel.DeleteCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.DeleteItemCommand.Execute(null));
205204
NavToolbarViewModel.CutCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.CutItemCommand.Execute(null));
206205
NavToolbarViewModel.EmptyRecycleBinCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.EmptyRecycleBinCommand.Execute(null));
206+
NavToolbarViewModel.RunWithPowerShellCommand = new RelayCommand(async () => await Win32Helpers.InvokeWin32ComponentAsync("powershell", this, PathNormalization.NormalizePath(SlimContentPage?.SelectedItems.First().ItemPath)));
207207
NavToolbarViewModel.PropertiesCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.ShowPropertiesCommand.Execute(null));
208208
}
209209

@@ -1140,4 +1140,4 @@ public class NavigationArguments
11401140
public bool IsLayoutSwitch { get; set; } = false;
11411141
public IEnumerable<string> SelectItems { get; set; }
11421142
}
1143-
}
1143+
}

0 commit comments

Comments
 (0)