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

Feature: Open in Terminal RichCommand #11445

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1ce41a0
Open in terminal Rich Command
ferrariofilippo Feb 23, 2023
cd0a279
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Feb 27, 2023
299a6d2
Selected Path support & Removed duplicate code
ferrariofilippo Feb 27, 2023
178ca4f
Unnecessary using
ferrariofilippo Feb 27, 2023
c3a6296
Update src/Files.App/Strings/en-US/Resources.resw
yaira2 Mar 1, 2023
94c0f94
Merge branch 'main' into Open_In_Terminal_Rich_Command
yaira2 Mar 1, 2023
cb85d08
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 4, 2023
ee12b9c
Build Error
ferrariofilippo Mar 4, 2023
a4ec37c
Path spaces fix
ferrariofilippo Mar 5, 2023
c6c9f1e
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 5, 2023
0302f89
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 6, 2023
4fd5439
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 8, 2023
0a549e2
Merge branch 'main' into Open_In_Terminal_Rich_Command
yaira2 Mar 8, 2023
bab0573
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 10, 2023
934e9e8
Removed BaseShellPage Code
ferrariofilippo Mar 10, 2023
509d32c
Requested Changes
ferrariofilippo Mar 11, 2023
a01e58c
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 11, 2023
e6f7209
Open folders in tabs instead of windows
ferrariofilippo Mar 11, 2023
8403589
Added Open actions folder
ferrariofilippo Mar 11, 2023
5a369a4
Build Error
ferrariofilippo Mar 11, 2023
106336a
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 12, 2023
625aa3f
Avoid Character Escape
ferrariofilippo Mar 12, 2023
ab18254
Add Action to file name
ferrariofilippo Mar 12, 2023
fb50945
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 13, 2023
d133e40
Build Error
ferrariofilippo Mar 14, 2023
f66ba20
Requested Changes
ferrariofilippo Mar 15, 2023
58ad20d
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 15, 2023
5684ae6
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 16, 2023
43e1316
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 16, 2023
cd48e16
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 19, 2023
58d364c
Open Parent Folder
ferrariofilippo Mar 20, 2023
ecdbd04
Merge branch 'main' into Open_In_Terminal_Rich_Command
ferrariofilippo Mar 20, 2023
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
61 changes: 61 additions & 0 deletions src/Files.App/Actions/Global/OpenTerminalAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.System;

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

public string Label { get; } = "OpenTerminal".GetLocalizedResource();

public virtual HotKey HotKey { get; } = new((VirtualKey)192, VirtualKeyModifiers.Control);

public RichGlyph Glyph { get; } = new RichGlyph("\uE756");

public Task ExecuteAsync()
{
var terminalStartInfo = GetProcessStartInfo();
if (terminalStartInfo is not null)
{
try
{
App.Window.DispatcherQueue.TryEnqueue(() => Process.Start(terminalStartInfo));
}
catch (OperationCanceledException)
{
}
}

return Task.CompletedTask;
}

protected virtual ProcessStartInfo? GetProcessStartInfo()
{
var path = GetPath();
if (path == string.Empty)
return null;

return new()
{
FileName = "wt.exe",
Arguments = $"-d {path}"
};
}

protected string GetPath()
{
// Return folder path if there is a folder selected, otherwise the current directory.
return context.ShellPage?.SlimContentPage?.SelectedItem?.PrimaryItemAttribute is StorageItemTypes.Folder
? context.ShellPage.SlimContentPage.SelectedItem.ItemPath
: context.ShellPage?.FilesystemViewModel.WorkingDirectory ?? string.Empty;
}
}
}
26 changes: 26 additions & 0 deletions src/Files.App/Actions/Global/OpenTerminalAsAdmin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Files.App.Commands;
using Files.App.Extensions;
using System.Diagnostics;
using Windows.System;

namespace Files.App.Actions
{
internal class OpenTerminalAsAdminAction : OpenTerminalAction
{
public new string Label { get; } = "OpenTerminalAsAdmin".GetLocalizedResource();

public override HotKey HotKey { get; } = new((VirtualKey)192, VirtualKeyModifiers.Control | VirtualKeyModifiers.Shift);

protected override ProcessStartInfo? GetProcessStartInfo()
{
var startInfo = base.GetProcessStartInfo();
if (startInfo is not null)
{
startInfo.Verb = "runas";
startInfo.UseShellExecute = true;
}

return startInfo;
}
}
}
2 changes: 2 additions & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public enum CommandCodes

// global
OpenHelp,
OpenTerminal,
OpenTerminalAsAdmin,
ToggleFullScreen,

// show
Expand Down
4 changes: 4 additions & 0 deletions src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ internal class CommandManager : ICommandManager

public IRichCommand None => commands[CommandCodes.None];
public IRichCommand OpenHelp => commands[CommandCodes.OpenHelp];
public IRichCommand OpenTerminal => commands[CommandCodes.OpenTerminal];
public IRichCommand OpenTerminalAsAdmin => commands[CommandCodes.OpenTerminalAsAdmin];
public IRichCommand ToggleFullScreen => commands[CommandCodes.ToggleFullScreen];
public IRichCommand ToggleShowHiddenItems => commands[CommandCodes.ToggleShowHiddenItems];
public IRichCommand ToggleShowFileExtensions => commands[CommandCodes.ToggleShowFileExtensions];
Expand All @@ -54,6 +56,8 @@ public CommandManager()
private static IDictionary<CommandCodes, IAction> CreateActions() => new Dictionary<CommandCodes, IAction>
{
[CommandCodes.OpenHelp] = new OpenHelpAction(),
[CommandCodes.OpenTerminal] = new OpenTerminalAction(),
[CommandCodes.OpenTerminalAsAdmin] = new OpenTerminalAsAdminAction(),
[CommandCodes.ToggleFullScreen] = new ToggleFullScreenAction(),
[CommandCodes.ToggleShowHiddenItems] = new ToggleShowHiddenItemsAction(),
[CommandCodes.ToggleShowFileExtensions] = new ToggleShowFileExtensionsAction(),
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface ICommandManager : IEnumerable<IRichCommand>
IRichCommand None { get; }

IRichCommand OpenHelp { get; }
IRichCommand OpenTerminal { get; }
IRichCommand OpenTerminalAsAdmin { get; }
IRichCommand ToggleFullScreen { get; }

IRichCommand ToggleShowHiddenItems { get; }
Expand Down
6 changes: 6 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -2601,6 +2601,12 @@
<data name="ApplyToAllConflictingItems" xml:space="preserve">
<value>Apply this action to all conflicting items</value>
</data>
<data name="OpenTerminal" xml:space="preserve">
<value>Open in terminal</value>
</data>
<data name="OpenTerminalAsAdmin" xml:space="preserve">
<value>Open in terminal as admin</value>
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
</data>
<data name="Save" xml:space="preserve">
<value>Save</value>
</data>
Expand Down
22 changes: 7 additions & 15 deletions src/Files.App/Views/BaseShellPage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.WinUI;
using Files.App.Commands;
using Files.App.DataModels;
using Files.App.EventArguments;
using Files.App.Extensions;
Expand All @@ -27,12 +28,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.System;
using Windows.UI.Core;
using SortDirection = Files.Shared.Enums.SortDirection;
Expand All @@ -56,6 +55,8 @@ public abstract class BaseShellPage : Page, IShellPage, INotifyPropertyChanged

protected readonly IUpdateService updateSettingsService = Ioc.Default.GetRequiredService<IUpdateService>();

protected readonly ICommandManager commands = Ioc.Default.GetRequiredService<ICommandManager>();

public ToolbarViewModel ToolbarViewModel { get; } = new ToolbarViewModel();

public IBaseLayout SlimContentPage => ContentPage;
Expand Down Expand Up @@ -284,19 +285,10 @@ protected void ShellPage_PreviewKeyDown(object sender, KeyRoutedEventArgs args)
// Ctrl + ` (accent key), open terminal
case (true, _, false, true, (VirtualKey)192):
ferrariofilippo marked this conversation as resolved.
Show resolved Hide resolved

// Check if there is a folder selected, if not use the current directory.
string path = FilesystemViewModel.WorkingDirectory;
if (SlimContentPage?.SelectedItem?.PrimaryItemAttribute == StorageItemTypes.Folder)
path = SlimContentPage.SelectedItem.ItemPath;

var terminalStartInfo = new ProcessStartInfo()
{
FileName = "wt.exe",
Arguments = $"-d {path}",
Verb = shift ? "runas" : "",
UseShellExecute = true
};
DispatcherQueue.TryEnqueue(() => Process.Start(terminalStartInfo));
if (shift)
commands.OpenTerminalAsAdmin.ExecuteAsync();
else
commands.OpenTerminal.ExecuteAsync();

args.Handled = true;

Expand Down