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: QuickLook #11771

Merged
merged 19 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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/LaunchQuickLookAction.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;
using Windows.System;

namespace Files.App.Actions
{
internal class LaunchQuickLookAction : ObservableObject, IAction
{
public HotKey HotKey { get; } = new(VirtualKey.Space);

public IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
public bool IsExecutable => context.SelectedItem is not null &&
context.SelectedItems.Count == 1 &&
(!context.ShellPage?.ToolbarViewModel?.IsEditModeEnabled ?? false) &&
(!context.ShellPage?.SlimContentPage?.IsRenamingItem ?? false);

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

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

public async Task ExecuteAsync()
{
await QuickLookHelpers.ToggleQuickLook(context.SelectedItem!.ItemPath);
}

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

// QuickLook
LaunchQuickLook,

// Archives
CompressIntoArchive,
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 @@ -57,6 +57,7 @@ internal class CommandManager : ICommandManager
public IRichCommand DeleteItem => commands[CommandCodes.DeleteItem];
public IRichCommand RunAsAdmin => commands[CommandCodes.RunAsAdmin];
public IRichCommand RunAsAnotherUser => commands[CommandCodes.RunAsAnotherUser];
public IRichCommand LaunchQuickLook => commands[CommandCodes.LaunchQuickLook];
public IRichCommand CompressIntoArchive => commands[CommandCodes.CompressIntoArchive];
public IRichCommand CompressIntoSevenZip => commands[CommandCodes.CompressIntoSevenZip];
public IRichCommand CompressIntoZip => commands[CommandCodes.CompressIntoZip];
Expand Down Expand Up @@ -160,6 +161,7 @@ public CommandManager()
[CommandCodes.DeleteItem] = new DeleteItemAction(),
[CommandCodes.RunAsAdmin] = new RunAsAdminAction(),
[CommandCodes.RunAsAnotherUser] = new RunAsAnotherUserAction(),
[CommandCodes.LaunchQuickLook] = new LaunchQuickLookAction(),
[CommandCodes.CompressIntoArchive] = new CompressIntoArchiveAction(),
[CommandCodes.CompressIntoSevenZip] = new CompressIntoSevenZipAction(),
[CommandCodes.CompressIntoZip] = new CompressIntoZipAction(),
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 @@ -43,6 +43,8 @@ public interface ICommandManager : IEnumerable<IRichCommand>

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

IRichCommand LaunchQuickLook { get; }

IRichCommand CompressIntoArchive { get; }
IRichCommand CompressIntoSevenZip { get; }
Expand Down
7 changes: 0 additions & 7 deletions src/Files.App/DataModels/AppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ public bool IsPasteEnabled
set => SetProperty(ref isPasteEnabled, value);
}

private bool isQuickLookAvailable;
public bool IsQuickLookAvailable
{
get => isQuickLookAvailable;
set => SetProperty(ref isQuickLookAvailable, value);
}

private FontFamily symbolFontFamily;
public FontFamily SymbolFontFamily
{
Expand Down
23 changes: 10 additions & 13 deletions src/Files.App/Helpers/QuickLookHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ namespace Files.App.Helpers;
public static class QuickLookHelpers
{
private const int TIMEOUT = 500;
private static string pipeName = $"QuickLook.App.Pipe.{WindowsIdentity.GetCurrent().User?.Value}";
private static string pipeMessageSwitch = "QuickLook.App.PipeMessages.Switch";
private static string pipeMessageToggle = "QuickLook.App.PipeMessages.Toggle";

public static async Task ToggleQuickLook(IShellPage associatedInstance, bool switchPreview = false)
public static async Task ToggleQuickLook(string path, bool switchPreview = false)
{
if (!associatedInstance.SlimContentPage.IsItemSelected || associatedInstance.SlimContentPage.IsRenamingItem)
return;

App.AppModel.IsQuickLookAvailable = await DetectQuickLookAvailability();
bool isQuickLookAvailable = await DetectQuickLookAvailability();

if (App.AppModel.IsQuickLookAvailable == false)
if (isQuickLookAvailable == false)
return;

string pipeName = $"QuickLook.App.Pipe.{WindowsIdentity.GetCurrent().User?.Value}";
string message = switchPreview ? "QuickLook.App.PipeMessages.Switch" : "QuickLook.App.PipeMessages.Toggle";
string message = switchPreview ? pipeMessageSwitch : pipeMessageToggle;

await using var client = new NamedPipeClientStream(".", pipeName, PipeDirection.Out);
try
{
await client.ConnectAsync(TIMEOUT);

await using var writer = new StreamWriter(client);
await writer.WriteLineAsync($"{message}|{associatedInstance.SlimContentPage.SelectedItem.ItemPath}");
await writer.WriteLineAsync($"{message}|{path}");
await writer.FlushAsync();
}
catch (TimeoutException)
Expand All @@ -41,18 +41,15 @@ public static async Task ToggleQuickLook(IShellPage associatedInstance, bool swi
private static async Task<bool> DetectQuickLookAvailability()
{
static async Task<int> QuickLookServerAvailable()
{
string pipeName = $"QuickLook.App.Pipe.{WindowsIdentity.GetCurrent().User?.Value}";
string pipeSwitch = "QuickLook.App.PipeMessages.Switch";

{
await using var client = new NamedPipeClientStream(".", pipeName, PipeDirection.Out);
try
{
await client.ConnectAsync(TIMEOUT);
var serverInstances = client.NumberOfServerInstances;

await using var writer = new StreamWriter(client);
await writer.WriteLineAsync($"{pipeSwitch}|");
await writer.WriteLineAsync($"{pipeMessageSwitch}|");
await writer.FlushAsync();

return serverInstances;
Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -2628,4 +2628,7 @@
<data name="ToggleSortDirection" xml:space="preserve">
<value>Toggle sort direction</value>
</data>
<data name="LaunchQuickLook" xml:space="preserve">
<value>Launch QuickLook</value>
</data>
</root>
8 changes: 0 additions & 8 deletions src/Files.App/Views/LayoutModes/ColumnViewBase.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,6 @@ protected override async void FileList_PreviewKeyDown(object sender, KeyRoutedEv
FilePropertiesHelpers.ShowProperties(ParentShellPageInstance);
e.Handled = true;
}
else if (e.Key == VirtualKey.Space)
{
if (!IsRenamingItem && !ParentShellPageInstance.ToolbarViewModel.IsEditModeEnabled)
{
e.Handled = true;
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
await QuickLookHelpers.ToggleQuickLook(ParentShellPageInstance);
}
}
else if (e.KeyStatus.IsMenuKeyDown && (e.Key == VirtualKey.Left || e.Key == VirtualKey.Right || e.Key == VirtualKey.Up))
{
// Unfocus the GridView so keyboard shortcut can be handled
Expand Down
12 changes: 2 additions & 10 deletions src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ private void FolderSettings_LayoutModeChangeRequested(object? sender, LayoutMode
private async void FileList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedItems = FileList.SelectedItems.Cast<ListedItem>().Where(x => x is not null).ToList();
if (SelectedItems.Count == 1 && App.AppModel.IsQuickLookAvailable)
if (SelectedItems.Count == 1)
{
await QuickLookHelpers.ToggleQuickLook(ParentShellPageInstance, true);
await QuickLookHelpers.ToggleQuickLook(SelectedItem.ItemPath, true);
}

if (e != null)
Expand Down Expand Up @@ -365,14 +365,6 @@ protected override async void FileList_PreviewKeyDown(object sender, KeyRoutedEv
FilePropertiesHelpers.ShowProperties(ParentShellPageInstance);
e.Handled = true;
}
else if (e.Key == VirtualKey.Space)
{
if (!IsRenamingItem && !isHeaderFocused && !isFooterFocused && !ParentShellPageInstance.ToolbarViewModel.IsEditModeEnabled)
{
e.Handled = true;
await QuickLookHelpers.ToggleQuickLook(ParentShellPageInstance);
}
}
else if (e.KeyStatus.IsMenuKeyDown && (e.Key == VirtualKey.Left || e.Key == VirtualKey.Right || e.Key == VirtualKey.Up))
{
// Unfocus the GridView so keyboard shortcut can be handled
Expand Down
8 changes: 0 additions & 8 deletions src/Files.App/Views/LayoutModes/GridViewBrowser.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,6 @@ protected override async void FileList_PreviewKeyDown(object sender, KeyRoutedEv
FilePropertiesHelpers.ShowProperties(ParentShellPageInstance);
e.Handled = true;
}
else if (e.Key == VirtualKey.Space)
{
if (!IsRenamingItem && !isFooterFocused && !ParentShellPageInstance.ToolbarViewModel.IsEditModeEnabled)
{
e.Handled = true;
await QuickLookHelpers.ToggleQuickLook(ParentShellPageInstance);
}
}
else if (e.KeyStatus.IsMenuKeyDown && (e.Key == VirtualKey.Left || e.Key == VirtualKey.Right || e.Key == VirtualKey.Up))
{
// Unfocus the GridView so keyboard shortcut can be handled
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/Views/LayoutModes/StandardLayoutMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ protected virtual async void FileList_SelectionChanged(object sender, SelectionC
{
SelectedItems = ListViewBase.SelectedItems.Cast<ListedItem>().Where(x => x is not null).ToList();

if (SelectedItems.Count == 1 && App.AppModel.IsQuickLookAvailable)
await QuickLookHelpers.ToggleQuickLook(ParentShellPageInstance, true);
if (SelectedItems.Count == 1)
await QuickLookHelpers.ToggleQuickLook(SelectedItem.ItemPath, true);
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
}

protected abstract void FileList_PreviewKeyDown(object sender, KeyRoutedEventArgs e);
Expand Down