Skip to content

Run with PowerShell button on toolbar #7767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Files/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -2699,4 +2699,10 @@ We use App Center to track which settings are being used, find bugs, and fix cra
<data name="Bytes" xml:space="preserve">
<value>Bytes</value>
</data>
</root>
<data name="RunScript" xml:space="preserve">
<value>Run script</value>
</data>
<data name="RunWithPowerShell" xml:space="preserve">
<value>Run with PowerShell</value>
</data>
</root>
16 changes: 14 additions & 2 deletions src/Files/UserControls/InnerNavigationToolbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@
x:Name="PropertiesButton"
Width="Auto"
MinWidth="40"
AccessKey="O"
AutomationProperties.Name="{helpers:ResourceString Name=PropertiesTitle}"
Command="{x:Bind ViewModel.PropertiesCommand, Mode=OneWay}"
IsEnabled="{x:Bind converters:MultiBooleanConverter.AndConvert(ViewModel.CanViewProperties, ViewModel.InstanceViewModel.IsPageTypeNotHome), Mode=OneWay, FallbackValue=False}"
Label="{helpers:ResourceString Name=PropertiesTitle}"
AutomationProperties.Name="{helpers:ResourceString Name=PropertiesTitle}"
AccessKey="O"
LabelPosition="Collapsed"
ToolTipService.ToolTip="{helpers:ResourceString Name=PropertiesTitle}">
<AppBarButton.Content>
Expand All @@ -213,6 +213,18 @@
<FontIcon FontFamily="{StaticResource RecycleBinIcons}" Glyph="&#xEF88;" />
</AppBarButton.Icon>
</AppBarButton>
<AppBarButton
x:Name="RunWithPowerShellButton"
x:Load="{x:Bind ViewModel.IsPowerShellScript, Mode=OneWay, FallbackValue=False}"
AutomationProperties.Name="RunWithPowerShell"
Command="{x:Bind ViewModel.RunWithPowerShellCommand, Mode=OneWay}"
Label="{helpers:ResourceString Name=RunScript}"
LabelPosition="Default"
ToolTipService.ToolTip="{helpers:ResourceString Name=RunWithPowerShell}">
<AppBarButton.Icon>
<FontIcon Glyph="&#xE756;" />
</AppBarButton.Icon>
</AppBarButton>
</CommandBar.PrimaryCommands>
</CommandBar>
<CommandBar
Expand Down
20 changes: 19 additions & 1 deletion src/Files/ViewModels/NavToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,8 @@ public void SearchRegion_LostFocus(object sender, RoutedEventArgs e)

public ICommand PropertiesCommand { get; set; }

public ICommand RunWithPowerShellCommand { get; set; }

public async Task SetPathBoxDropDownFlyoutAsync(MenuFlyout flyout, PathBoxItem pathItem, IShellPage shellPage)
{
var nextPathItemTitle = PathComponents[PathComponents.IndexOf(pathItem) + 1].Title;
Expand Down Expand Up @@ -1079,19 +1081,35 @@ public List<ListedItem> SelectedItems
OnPropertyChanged(nameof(CanCopy));
OnPropertyChanged(nameof(CanShare));
OnPropertyChanged(nameof(CanRename));
OnPropertyChanged(nameof(IsPowerShellScript));
OnPropertyChanged(nameof(HasAdditionnalAction));
OnPropertyChanged(nameof(CanViewProperties));
}
}
}

public bool HasAdditionnalAction => InstanceViewModel.IsPageTypeRecycleBin;
public bool HasAdditionnalAction
{
get
{
if (InstanceViewModel.IsPageTypeRecycleBin)
return true;

if (IsPowerShellScript)
return true;

return false;
}
}

public bool CanCopy => SelectedItems is not null && SelectedItems.Any();
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));
public bool CanRename => SelectedItems is not null && SelectedItems.Count == 1;
public bool CanViewProperties => SelectedItems is not null && SelectedItems.Any();
public bool CanEmptyRecycleBin => InstanceViewModel.IsPageTypeRecycleBin && HasItem;

public bool IsPowerShellScript => SelectedItems is not null && SelectedItems.Any() && SelectedItems.First().PrimaryItemAttribute == StorageItemTypes.File && new[] { ".ps1", }.Contains(SelectedItems.First().FileExtension, StringComparer.OrdinalIgnoreCase);

public void Dispose()
{
SearchBox.Escaped -= SearchRegion_Escaped;
Expand Down
4 changes: 2 additions & 2 deletions src/Files/Views/ModernShellPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

using SortDirection = Files.Enums.SortDirection;

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

Expand Down Expand Up @@ -1140,4 +1140,4 @@ public class NavigationArguments
public bool IsLayoutSwitch { get; set; } = false;
public IEnumerable<string> SelectItems { get; set; }
}
}
}