Skip to content
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

RichCommand: Run With Powershell #11914

Merged
merged 4 commits into from
Apr 3, 2023
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
47 changes: 47 additions & 0 deletions src/Files.App/Actions/Content/Run/RunWithPowershellAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Helpers;
using Files.App.Shell;
using Files.Backend.Helpers;
using System.Threading.Tasks;

namespace Files.App.Actions
{
internal class RunWithPowershellAction : ObservableObject, IAction
{
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

public bool IsExecutable => context.SelectedItem is not null &&
FileExtensionHelpers.IsPowerShellFile(context.SelectedItem.FileExtension);

public string Label => "RunWithPowerShell".GetLocalizedResource();

public string Description => "TODO: Need to be described.";

yaira2 marked this conversation as resolved.
Show resolved Hide resolved
public RichGlyph Glyph => new("\uE756");

public RunWithPowershellAction()
{
context.PropertyChanged += Context_PropertyChanged;
}

public async Task ExecuteAsync()
{
Win32API.RunPowershellCommand($"{context.ShellPage?.SlimContentPage?.SelectedItem.ItemPath}", false);
}

private void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IContentPageContext.SelectedItems):
case nameof(IContentPageContext.Folder):
OnPropertyChanged(nameof(IsExecutable));
break;
}
}
}
}
1 change: 1 addition & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public enum CommandCodes
// Run
RunAsAdmin,
RunAsAnotherUser,
RunWithPowershell,

// QuickLook
LaunchQuickLook,
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ internal class CommandManager : ICommandManager
public IRichCommand InstallInfDriver => commands[CommandCodes.InstallInfDriver];
public IRichCommand RunAsAdmin => commands[CommandCodes.RunAsAdmin];
public IRichCommand RunAsAnotherUser => commands[CommandCodes.RunAsAnotherUser];
public IRichCommand RunWithPowershell => commands[CommandCodes.RunWithPowershell];
public IRichCommand LaunchQuickLook => commands[CommandCodes.LaunchQuickLook];
public IRichCommand CompressIntoArchive => commands[CommandCodes.CompressIntoArchive];
public IRichCommand CompressIntoSevenZip => commands[CommandCodes.CompressIntoSevenZip];
Expand Down Expand Up @@ -196,6 +197,7 @@ public CommandManager()
[CommandCodes.InstallInfDriver] = new InstallInfDriverAction(),
[CommandCodes.RunAsAdmin] = new RunAsAdminAction(),
[CommandCodes.RunAsAnotherUser] = new RunAsAnotherUserAction(),
[CommandCodes.RunWithPowershell] = new RunWithPowershellAction(),
[CommandCodes.LaunchQuickLook] = new LaunchQuickLookAction(),
[CommandCodes.CompressIntoArchive] = new CompressIntoArchiveAction(),
[CommandCodes.CompressIntoSevenZip] = new CompressIntoSevenZipAction(),
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>

IRichCommand RunAsAdmin { get; }
IRichCommand RunAsAnotherUser { get; }
IRichCommand RunWithPowershell { get; }

IRichCommand LaunchQuickLook { get; }

Expand Down
10 changes: 5 additions & 5 deletions src/Files.App/UserControls/InnerNavigationToolbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@
x:Name="RunWithPowerShellButton"
Width="Auto"
MinWidth="40"
x:Load="{x:Bind ViewModel.IsPowerShellScript, Mode=OneWay, FallbackValue=False}"
x:Load="{x:Bind Commands.RunWithPowershell.IsExecutable, Mode=OneWay}"
AutomationProperties.Name="RunWithPowerShell"
0x5bfa marked this conversation as resolved.
Show resolved Hide resolved
Command="{x:Bind ViewModel.RunWithPowerShellCommand, Mode=OneWay}"
Label="{helpers:ResourceString Name=RunScript}"
Command="{x:Bind Commands.RunWithPowershell}"
Label="{x:Bind Commands.RunWithPowershell.Label}"
LabelPosition="Default"
ToolTipService.ToolTip="{helpers:ResourceString Name=RunWithPowerShell}">
ToolTipService.ToolTip="{x:Bind Commands.RunWithPowershell.LabelWithHotKey}">
<AppBarButton.Icon>
<FontIcon Glyph="&#xE756;" />
<FontIcon Glyph="{x:Bind Commands.RunWithPowershell.Glyph.BaseGlyph}" />
</AppBarButton.Icon>
</AppBarButton>
<AppBarButton
Expand Down
2 changes: 0 additions & 2 deletions src/Files.App/ViewModels/ToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,6 @@ private void SearchRegion_Escaped(object? sender, ISearchBox searchBox)

public ICommand PropertiesCommand { get; set; }

public ICommand? RunWithPowerShellCommand { get; set; }

public ICommand? UpdateCommand { get; set; }

public ICommand? PlayAllCommand { get; set; }
Expand Down
1 change: 0 additions & 1 deletion src/Files.App/Views/BaseShellPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,6 @@ protected void InitToolbarCommands()
ToolbarViewModel.OpenNewPaneCommand = new RelayCommand(() => PaneHolder?.OpenPathInNewPane("Home".GetLocalizedResource()));
ToolbarViewModel.ClosePaneCommand = new RelayCommand(() => PaneHolder?.CloseActivePane());
ToolbarViewModel.CreateNewFileCommand = new RelayCommand<ShellNewEntry>(x => UIFilesystemHelpers.CreateFileFromDialogResultType(AddItemDialogItemType.File, x, this));
ToolbarViewModel.RunWithPowerShellCommand = new RelayCommand(async () => await Win32Helpers.InvokeWin32ComponentAsync("powershell", this, PathNormalization.NormalizePath(SlimContentPage?.SelectedItem.ItemPath)));
ToolbarViewModel.PropertiesCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.ShowPropertiesCommand.Execute(null));
ToolbarViewModel.UpdateCommand = new AsyncRelayCommand(async () => await updateSettingsService.DownloadUpdates());
ToolbarViewModel.PlayAllCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.PlayAllCommand.Execute(null));
Expand Down