Skip to content

Commit

Permalink
RichCommand: QuickLook (#11771)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaira2 authored Mar 21, 2023
1 parent c6fbec9 commit a51134b
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 39 deletions.
53 changes: 53 additions & 0 deletions src/Files.App/Actions/Content/QuickLook/LaunchQuickLookAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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
{
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

public HotKey HotKey { get; } = new(VirtualKey.Space);

public bool IsExecutable => 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):
OnPropertyChanged(nameof(IsExecutable));
var _ = SwitchQuickLookPreview();
break;
}
}

private async Task SwitchQuickLookPreview()
{
if (IsExecutable)
await QuickLookHelpers.ToggleQuickLook(context.SelectedItem!.ItemPath, true);
}
}
}
3 changes: 3 additions & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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 @@ -56,6 +56,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 @@ -162,6 +163,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 @@ -44,6 +44,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 @@ -72,12 +72,5 @@ public bool IsPasteEnabled
get => isPasteEnabled;
set => SetProperty(ref isPasteEnabled, value);
}

private bool isQuickLookAvailable;
public bool IsQuickLookAvailable
{
get => isQuickLookAvailable;
set => SetProperty(ref isQuickLookAvailable, value);
}
}
}
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 @@ -2634,4 +2634,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>
3 changes: 0 additions & 3 deletions src/Files.App/Views/LayoutModes/ColumnViewBase.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,7 @@ protected override async void FileList_PreviewKeyDown(object sender, KeyRoutedEv
else if (e.Key == VirtualKey.Space)
{
if (!IsRenamingItem && !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))
{
Expand Down
11 changes: 2 additions & 9 deletions src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,6 @@ 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)
{
await QuickLookHelpers.ToggleQuickLook(ParentShellPageInstance, true);
}

if (e != null)
{
Expand Down Expand Up @@ -367,11 +363,8 @@ protected override async void FileList_PreviewKeyDown(object sender, KeyRoutedEv
}
else if (e.Key == VirtualKey.Space)
{
if (!IsRenamingItem && !isHeaderFocused && !isFooterFocused && !ParentShellPageInstance.ToolbarViewModel.IsEditModeEnabled)
{
if (!IsRenamingItem && !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))
{
Expand Down Expand Up @@ -828,7 +821,7 @@ private void UpdateCheckboxVisibility(object sender, bool? isPointerOver = null)
if (sender is ListViewItem control && control.FindDescendant<UserControl>() is UserControl userControl)
{
// Save pointer over state accordingly
if(isPointerOver.HasValue)
if (isPointerOver.HasValue)
control.SetValue(IsPointerOverProperty, isPointerOver);
// Handle visual states
if (control.IsSelected || control.GetValue(IsPointerOverProperty) is not false)
Expand Down
5 changes: 1 addition & 4 deletions src/Files.App/Views/LayoutModes/GridViewBrowser.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,8 @@ protected override async void FileList_PreviewKeyDown(object sender, KeyRoutedEv
}
else if (e.Key == VirtualKey.Space)
{
if (!IsRenamingItem && !isFooterFocused && !ParentShellPageInstance.ToolbarViewModel.IsEditModeEnabled)
{
if (!IsRenamingItem && !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))
{
Expand Down
3 changes: 0 additions & 3 deletions src/Files.App/Views/LayoutModes/StandardLayoutMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ protected virtual void ZoomIn(object? sender, GroupOption option)
protected virtual async void FileList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedItems = ListViewBase.SelectedItems.Cast<ListedItem>().Where(x => x is not null).ToList();

if (SelectedItems.Count == 1 && App.AppModel.IsQuickLookAvailable)
await QuickLookHelpers.ToggleQuickLook(ParentShellPageInstance, true);
}

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

0 comments on commit a51134b

Please sign in to comment.