|
| 1 | +// Copyright (c) Files Community |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.IO; |
| 5 | + |
| 6 | +namespace Files.App.Services.PreviewPopupProviders |
| 7 | +{ |
| 8 | + public sealed class PowerToysPeekProvider : IPreviewPopupProvider |
| 9 | + { |
| 10 | + public static PowerToysPeekProvider Instance { get; } = new(); |
| 11 | + |
| 12 | + private static string? _peekExecutablePath; |
| 13 | + |
| 14 | + public async Task TogglePreviewPopupAsync(string path) |
| 15 | + { |
| 16 | + await DoPreviewAsync(path); |
| 17 | + } |
| 18 | + |
| 19 | + public async Task SwitchPreviewAsync(string path) |
| 20 | + { |
| 21 | + // Not used |
| 22 | + } |
| 23 | + |
| 24 | + private async Task DoPreviewAsync(string path) |
| 25 | + { |
| 26 | + if (_peekExecutablePath != null) |
| 27 | + { |
| 28 | + try |
| 29 | + { |
| 30 | + var psi = new ProcessStartInfo |
| 31 | + { |
| 32 | + FileName = _peekExecutablePath, |
| 33 | + Arguments = $"\"{path}\"", |
| 34 | + UseShellExecute = true, |
| 35 | + CreateNoWindow = true, |
| 36 | + WindowStyle = ProcessWindowStyle.Hidden |
| 37 | + }; |
| 38 | + |
| 39 | + Process.Start(psi); |
| 40 | + } |
| 41 | + catch |
| 42 | + { |
| 43 | + // Ignore |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public async Task<bool> DetectAvailability() |
| 49 | + { |
| 50 | + var exeName = "PowerToys.Peek.UI.exe"; |
| 51 | + var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); |
| 52 | + var perUserPath = Path.Combine(localAppData, "PowerToys", "WinUI3Apps", exeName); |
| 53 | + |
| 54 | + // User path |
| 55 | + if (File.Exists(perUserPath)) |
| 56 | + { |
| 57 | + _peekExecutablePath = perUserPath; |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + // Machine-wide path |
| 62 | + string programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); |
| 63 | + string machinePath = Path.Combine(programFiles, "PowerToys", "WinUI3Apps", exeName); |
| 64 | + |
| 65 | + if (File.Exists(machinePath)) |
| 66 | + { |
| 67 | + _peekExecutablePath = machinePath; |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + // Not found |
| 72 | + return false; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments