Skip to content

Commit 9a8ed35

Browse files
lampenlampenyaira2
authored andcommitted
QuickLook Integration (#353)
1 parent 584e72d commit 9a8ed35

File tree

7 files changed

+140
-5
lines changed

7 files changed

+140
-5
lines changed

Files.Launcher/Files.Launcher.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<ItemGroup>
5959
<Compile Include="Program.cs" />
6060
<Compile Include="Properties\AssemblyInfo.cs" />
61+
<Compile Include="QuickLook.cs" />
6162
</ItemGroup>
6263
<ItemGroup>
6364
<None Include="app.config" />

Files.Launcher/Program.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System.Diagnostics;
2+
using System.IO;
3+
using System.IO.Pipes;
4+
using System.Security.Principal;
25
using Windows.Storage;
36

47
namespace ProcessLauncher
@@ -7,7 +10,8 @@ class Program
710
{
811
static void Main(string[] args)
912
{
10-
var arguments = (string)ApplicationData.Current.LocalSettings.Values["Arguments"];
13+
var localSettings = ApplicationData.Current.LocalSettings;
14+
var arguments = (string)localSettings.Values["Arguments"];
1115
if (!string.IsNullOrWhiteSpace(arguments))
1216
{
1317
if (arguments.Equals("DetectUserPaths"))
@@ -20,6 +24,15 @@ static void Main(string[] args)
2024
ApplicationData.Current.LocalSettings.Values["DetectedVideosLocation"] = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders", "My Video", null);
2125
ApplicationData.Current.LocalSettings.Values["DetectedOneDriveLocation"] = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\OneDrive", "UserFolder", null);
2226
}
27+
else if (arguments.Equals("CheckQuickLookAvailability"))
28+
{
29+
QuickLook.CheckQuickLookAvailability(localSettings);
30+
}
31+
else if (arguments.Equals("ToggleQuickLook"))
32+
{
33+
var path = (string) localSettings.Values["path"];
34+
QuickLook.ToggleQuickLook(path);
35+
}
2336
else
2437
{
2538
var executable = (string)ApplicationData.Current.LocalSettings.Values["Application"];
@@ -55,7 +68,6 @@ static void Main(string[] args)
5568

5669

5770
}
58-
5971
}
6072
}
6173
}

Files.Launcher/QuickLook.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.IO.Pipes;
6+
using System.Linq;
7+
using System.Security.Principal;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Windows.Storage;
11+
12+
namespace ProcessLauncher
13+
{
14+
static class QuickLook
15+
{
16+
public static void ToggleQuickLook(string path)
17+
{
18+
string PipeName = "QuickLook.App.Pipe." + WindowsIdentity.GetCurrent().User?.Value;
19+
string Toggle = "QuickLook.App.PipeMessages.Toggle";
20+
21+
using (var client = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
22+
{
23+
client.Connect();
24+
25+
using (var writer = new StreamWriter(client))
26+
{
27+
writer.WriteLine($"{Toggle}|{path}");
28+
writer.Flush();
29+
}
30+
}
31+
}
32+
33+
public static void CheckQuickLookAvailability(ApplicationDataContainer localSettings)
34+
{
35+
static int QuickLookServerAvailable()
36+
{
37+
string PipeName = "QuickLook.App.Pipe." + WindowsIdentity.GetCurrent().User?.Value;
38+
string Switch = "QuickLook.App.PipeMessages.Switch";
39+
40+
using var client = new NamedPipeClientStream(".", PipeName, PipeDirection.Out);
41+
try
42+
{
43+
client.Connect(500);
44+
var serverInstances = client.NumberOfServerInstances;
45+
46+
using (var writer = new StreamWriter(client))
47+
{
48+
writer.WriteLine($"{Switch}|");
49+
writer.Flush();
50+
}
51+
52+
return serverInstances;
53+
54+
}
55+
catch (TimeoutException e)
56+
{
57+
client.Close();
58+
return 0;
59+
}
60+
}
61+
62+
localSettings.Values["quicklook_enabled"] = QuickLookServerAvailable() != 0;
63+
}
64+
}
65+
}

Files/App.xaml.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public App()
8282
SetPropertiesFromLocalSettings();
8383
PopulatePinnedSidebarItems();
8484
DetectWSLDistros();
85+
QuickLookIntegration();
8586
}
8687

8788
private async void DetectWSLDistros()
@@ -131,7 +132,12 @@ private async void DetectWSLDistros()
131132
// WSL Not Supported/Enabled
132133
areLinuxFilesSupported = false;
133134
}
134-
135+
}
136+
137+
private async void QuickLookIntegration()
138+
{
139+
ApplicationData.Current.LocalSettings.Values["Arguments"] = "CheckQuickLookAvailability";
140+
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
135141
}
136142

137143
private void SetPropertiesFromLocalSettings()

Files/GenericFileBrowser.xaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@
147147
<FontIcon Glyph="&#xE946;"/>
148148
</MenuFlyoutItem.Icon>
149149
</MenuFlyoutItem>
150+
<MenuFlyoutItem Click="{x:Bind AssociatedInteractions.ToggleQuickLook_Click}" Text="QuickLook" x:Name="QuickLook" Visibility="{x:Bind _isQuickLookEnabled}">
151+
<MenuFlyoutItem.KeyboardAccelerators>
152+
<KeyboardAccelerator Key="Space"/>
153+
</MenuFlyoutItem.KeyboardAccelerators>
154+
</MenuFlyoutItem>
150155
</MenuFlyout>
151156
</local:BaseLayout.Resources>
152157
<Grid ContextFlyout="{StaticResource BaseLayoutContextFlyout}" Background="Transparent" x:Name="RootGrid">

Files/GenericFileBrowser.xaml.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Files
1616
{
1717
public sealed partial class GenericFileBrowser : BaseLayout
1818
{
19-
19+
private bool _isQuickLookEnabled { get; set; } = false;
2020
public string previousFileName;
2121
private DataGridColumn _sortedColumn;
2222
public DataGridColumn SortedColumn
@@ -70,6 +70,15 @@ public GenericFileBrowser()
7070
}
7171

7272
App.OccupiedInstance.instanceViewModel.PropertyChanged += ViewModel_PropertyChanged;
73+
74+
// QuickLook Integration
75+
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
76+
var isQuickLookIntegrationEnabled = localSettings.Values["quicklook_enabled"];
77+
78+
if (isQuickLookIntegrationEnabled != null && isQuickLookIntegrationEnabled.Equals(true))
79+
{
80+
_isQuickLookEnabled = true;
81+
}
7382
}
7483

7584
private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)

Files/Interacts/Interaction.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,9 +996,46 @@ public void ClearAllItems()
996996
}
997997
}
998998

999+
public void ToggleQuickLook_Click(object sender, RoutedEventArgs e)
1000+
{
1001+
ToggleQuickLook();
1002+
}
1003+
1004+
public async void ToggleQuickLook()
1005+
{
1006+
try
1007+
{
1008+
string selectedItemPath = null;
1009+
int selectedItemCount;
1010+
Type sourcePageType = App.OccupiedInstance.ItemDisplayFrame.SourcePageType;
1011+
selectedItemCount = (currentInstance.ItemDisplayFrame.Content as BaseLayout).SelectedItems.Count;
1012+
if (selectedItemCount == 1)
1013+
{
1014+
selectedItemPath = (currentInstance.ItemDisplayFrame.Content as BaseLayout).SelectedItems[0].FilePath;
1015+
}
1016+
1017+
if (selectedItemCount == 1)
1018+
{
1019+
var clickedOnItem = (currentInstance.ItemDisplayFrame.Content as BaseLayout).SelectedItems[0];
1020+
1021+
Debug.WriteLine("Toggle QuickLook");
1022+
ApplicationData.Current.LocalSettings.Values["path"] = clickedOnItem.FilePath;
1023+
ApplicationData.Current.LocalSettings.Values["Arguments"] = "ToggleQuickLook";
1024+
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
1025+
}
1026+
}
1027+
catch (FileNotFoundException)
1028+
{
1029+
MessageDialog dialog = new MessageDialog("The file you are attempting to preview may have been moved or deleted.", "File Not Found");
1030+
var task = dialog.ShowAsync();
1031+
task.AsTask().Wait();
1032+
NavigationActions.Refresh_Click(null, null);
1033+
}
1034+
}
1035+
9991036
public void PushJumpChar(char letter)
10001037
{
10011038
App.OccupiedInstance.instanceViewModel.JumpString += letter.ToString().ToLower();
10021039
}
10031040
}
1004-
}
1041+
}

0 commit comments

Comments
 (0)