diff --git a/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/ArchiveInfoPanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/ArchiveInfoPanel.xaml.cs index 84f5933..02643d8 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/ArchiveInfoPanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/ArchiveInfoPanel.xaml.cs @@ -20,6 +20,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Windows.Controls; +using QuickLook.ExtensionMethods; using SharpCompress.Archives; using SharpCompress.Common; using SharpCompress.Readers; @@ -88,8 +89,9 @@ namespace QuickLook.Plugin.ArchiveViewer archiveCount.Content = $"{_type} archive{t}"; - archiveSizeC.Content = $"Compressed size {_totalZippedSize.ToPrettySize(2)}"; - archiveSizeU.Content = $"Uncompressed size {sizeU.ToPrettySize(2)}"; + archiveSizeC.Content = + $"Compressed size {((long) _totalZippedSize).ToPrettySize(2)}"; + archiveSizeU.Content = $"Uncompressed size {((long) sizeU).ToPrettySize(2)}"; } private void LoadItemsFromArchive(string path) @@ -99,7 +101,7 @@ namespace QuickLook.Plugin.ArchiveViewer // ReaderFactory is slow... so limit its usage string[] useReader = {".tar.gz", ".tgz", ".tar.bz2", ".tar.lz", ".tar.xz"}; - if (useReader.Any(i => path.EndsWith(i))) + if (useReader.Any(path.ToLower().EndsWith)) { var reader = ReaderFactory.Open(stream, new ChardetReaderOptions()); diff --git a/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Converters.cs b/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Converters.cs index af5a8ff..ba6b58e 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Converters.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Converters.cs @@ -19,6 +19,7 @@ using System; using System.Globalization; using System.Windows; using System.Windows.Data; +using QuickLook.ExtensionMethods; namespace QuickLook.Plugin.ArchiveViewer { @@ -77,7 +78,7 @@ namespace QuickLook.Plugin.ArchiveViewer var size = (ulong) values[0]; var isFolder = (bool) values[1]; - return isFolder ? "" : size.ToPrettySize(2); + return isFolder ? "" : ((long) size).ToPrettySize(2); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) diff --git a/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Extensions.cs b/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Extensions.cs index f15d25f..ff05fce 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Extensions.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Extensions.cs @@ -49,29 +49,5 @@ namespace QuickLook.Plugin.ArchiveViewer } return foundElement; } - - public static string ToPrettySize(this ulong value, int decimalPlaces = 0) - { - const long OneKb = 1024; - const long OneMb = OneKb * 1024; - const long OneGb = OneMb * 1024; - const long OneTb = OneGb * 1024; - - var asTb = Math.Round((double) value / OneTb, decimalPlaces); - var asGb = Math.Round((double) value / OneGb, decimalPlaces); - var asMb = Math.Round((double) value / OneMb, decimalPlaces); - var asKb = Math.Round((double) value / OneKb, decimalPlaces); - var chosenValue = asTb > 1 - ? $"{asTb} TB" - : asGb > 1 - ? $"{asGb} GB" - : asMb > 1 - ? $"{asMb} MB" - : asKb > 1 - ? $"{asKb} KB" - : $"{Math.Round((double) value, decimalPlaces)} bytes"; - - return chosenValue; - } } } \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs index a1089ef..20d34d2 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/Plugin.cs @@ -17,12 +17,16 @@ using System; using System.IO; +using System.Linq; using System.Windows; namespace QuickLook.Plugin.ArchiveViewer { public class Plugin : IViewer { + private static readonly string[] Extensions = + {".rar", ".zip", ".tar", ".tgz", ".gz", ".bz2", ".lz", ".xz", ".7z"}; + private ArchiveInfoPanel _panel; public int Priority => 0; @@ -34,25 +38,7 @@ namespace QuickLook.Plugin.ArchiveViewer public bool CanHandle(string path) { - if (Directory.Exists(path)) - return false; - - switch (Path.GetExtension(path).ToLower()) - { - case ".rar": - case ".zip": - case ".tar": - case ".tgz": - case ".gz": - case ".bz2": - case ".lz": - case ".xz": - case ".7z": - return true; - - default: - return false; - } + return !Directory.Exists(path) && Extensions.Any(path.ToLower().EndsWith); } public void Prepare(string path, ContextObject context) diff --git a/QuickLook.Plugin/QuickLook.Plugin.CsvViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.CsvViewer/Plugin.cs index f7ff9c2..7d74156 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.CsvViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.CsvViewer/Plugin.cs @@ -34,10 +34,7 @@ namespace QuickLook.Plugin.CsvViewer public bool CanHandle(string path) { - if (Directory.Exists(path)) - return false; - - return Path.GetExtension(path).ToLower() == ".csv"; + return !Directory.Exists(path) && path.ToLower().EndsWith(".csv"); } public void Prepare(string path, ContextObject context) diff --git a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/DpiHelpers.cs b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/DpiHelpers.cs deleted file mode 100644 index 3c2caa4..0000000 --- a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/DpiHelpers.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright © 2017 Paddy Xu -// -// This file is part of QuickLook program. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -using System; -using System.Drawing; -using System.Runtime.InteropServices; - -namespace QuickLook.Plugin.HtmlViewer -{ - internal static class DpiHelper - { - public enum DeviceCap - { - /// - /// Logical pixels inch in X - /// - LOGPIXELSX = 88, - /// - /// Logical pixels inch in Y - /// - LOGPIXELSY = 90 - } - - public const float DEFAULT_DPI = 96; - - public static Dpi GetCurrentDpi() - { - var g = Graphics.FromHwnd(IntPtr.Zero); - var desktop = g.GetHdc(); - - var dpi = new Dpi - { - HorizontalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSX), - VerticalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSY) - }; - - return dpi; - } - - [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] - public static extern int GetDeviceCaps(IntPtr hDC, int nIndex); - } - - internal class Dpi - { - public float HorizontalDpi { get; set; } - public float VerticalDpi { get; set; } - } -} \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs index 9039cfe..6a2eee9 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/Plugin.cs @@ -17,6 +17,7 @@ using System; using System.IO; +using System.Linq; using System.Windows; using System.Windows.Threading; @@ -24,6 +25,8 @@ namespace QuickLook.Plugin.HtmlViewer { public class Plugin : IViewer { + private static readonly string[] Extensions = {".mht", ".mhtml", ".htm", ".html"}; + private WebpagePanel _panel; public int Priority => int.MaxValue; @@ -36,20 +39,7 @@ namespace QuickLook.Plugin.HtmlViewer public bool CanHandle(string path) { - if (Directory.Exists(path)) - return false; - - switch (Path.GetExtension(path).ToLower()) - { - case ".mht": - case ".mhtml": - case ".html": - case ".htm": - return true; - - default: - return false; - } + return !Directory.Exists(path) && Extensions.Any(path.ToLower().EndsWith); } public void Prepare(string path, ContextObject context) diff --git a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/QuickLook.Plugin.HtmlViewer.csproj b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/QuickLook.Plugin.HtmlViewer.csproj index 225efdc..93d6f91 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/QuickLook.Plugin.HtmlViewer.csproj +++ b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/QuickLook.Plugin.HtmlViewer.csproj @@ -72,7 +72,6 @@ - MSBuild:Compile diff --git a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/WebpagePanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/WebpagePanel.xaml.cs index 4c04bed..cbcaf43 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/WebpagePanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/WebpagePanel.xaml.cs @@ -20,6 +20,7 @@ using System.IO; using System.Text; using System.Windows.Controls; using System.Windows.Threading; +using QuickLook.Helpers; namespace QuickLook.Plugin.HtmlViewer { @@ -32,7 +33,7 @@ namespace QuickLook.Plugin.HtmlViewer { InitializeComponent(); - browser.Zoom = (int) (100 * DpiHelper.GetCurrentDpi().HorizontalDpi / DpiHelper.DEFAULT_DPI); + browser.Zoom = (int) (100 * DpiHelper.GetCurrentScaleFactor().Vertical); } public void Dispose() diff --git a/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Plugin.cs index e4272d9..e23f1c2 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/Plugin.cs @@ -17,12 +17,22 @@ using System; using System.IO; +using System.Linq; using System.Windows; namespace QuickLook.Plugin.IPreviewHandlers { public class Plugin : IViewer { + private static readonly string[] Extensions = + { + ".doc", ".docx", ".docm", + ".xls", ".xlsx", ".xlsm", ".xlsb", + /*".vsd", ".vsdx",*/ + ".ppt", ".pptx", + ".odt", ".ods", ".odp" + }; + private PreviewPanel _panel; public int Priority => int.MaxValue; @@ -37,29 +47,8 @@ namespace QuickLook.Plugin.IPreviewHandlers if (Directory.Exists(path)) return false; - switch (Path.GetExtension(path).ToLower()) - { - // Word - case ".doc": - case ".docx": - case ".docm": - // Excel - case ".xls": - case ".xlsx": - case ".xlsm": - case ".xlsb": - // Visio Viewer will not quit after preview, which cause serious memory issue - //case ".vsd": - //case ".vsdx": - // PowerPoint - case ".ppt": - case ".pptx": - // OpenDocument - case ".odt": - case ".ods": - case ".odp": - return PreviewHandlerHost.GetPreviewHandlerGUID(path) != Guid.Empty; - } + if (Extensions.Any(path.ToLower().EndsWith)) + return PreviewHandlerHost.GetPreviewHandlerGUID(path) != Guid.Empty; return false; } diff --git a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs index 4832844..8e2b891 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs @@ -25,7 +25,7 @@ namespace QuickLook.Plugin.ImageViewer { public class Plugin : IViewer { - private static readonly string[] _formats = + private static readonly string[] Formats = { // camera raw ".3fr", ".ari", ".arw", ".bay", ".crw", ".cr2", ".cap", ".data", ".dcs", ".dcr", ".dng", ".drf", ".eip", @@ -51,10 +51,7 @@ namespace QuickLook.Plugin.ImageViewer public bool CanHandle(string path) { - if (Directory.Exists(path)) - return false; - - return _formats.Contains(Path.GetExtension(path).ToLower()); + return !Directory.Exists(path) && Formats.Any(path.ToLower().EndsWith); } public void Prepare(string path, ContextObject context) diff --git a/QuickLook.Plugin/QuickLook.Plugin.MailViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.MailViewer/Plugin.cs index 49f692a..1f15cb0 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.MailViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.MailViewer/Plugin.cs @@ -17,6 +17,7 @@ using System; using System.IO; +using System.Linq; using System.Windows; using System.Windows.Threading; using MsgReader; @@ -27,7 +28,7 @@ namespace QuickLook.Plugin.MailViewer public class Plugin : IViewer { private WebpagePanel _panel; - private string tmpDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + private string _tmpDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); public int Priority => int.MaxValue; public bool AllowsTransparency => false; @@ -38,18 +39,7 @@ namespace QuickLook.Plugin.MailViewer public bool CanHandle(string path) { - if (Directory.Exists(path)) - return false; - - switch (Path.GetExtension(path).ToLower()) - { - case ".eml": - case ".msg": - return true; - - default: - return false; - } + return !Directory.Exists(path) && new[] {".eml", ".msg"}.Any(path.ToLower().EndsWith); } public void Prepare(string path, ContextObject context) @@ -74,18 +64,18 @@ namespace QuickLook.Plugin.MailViewer _panel?.Dispose(); _panel = null; - if (Directory.Exists(tmpDir)) - Directory.Delete(tmpDir, true); + if (Directory.Exists(_tmpDir)) + Directory.Delete(_tmpDir, true); } private string ExtractMailBody(string path) { - tmpDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - Directory.CreateDirectory(tmpDir); + _tmpDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + Directory.CreateDirectory(_tmpDir); var msg = new Reader(); - var files = msg.ExtractToFolder(path, tmpDir, true); + var files = msg.ExtractToFolder(path, _tmpDir, true); if (files.Length > 0 && !string.IsNullOrEmpty(files[0])) return files[0]; diff --git a/QuickLook.Plugin/QuickLook.Plugin.MarkdownViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.MarkdownViewer/Plugin.cs index 3de34cd..78efed9 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.MarkdownViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.MarkdownViewer/Plugin.cs @@ -17,6 +17,7 @@ using System; using System.IO; +using System.Linq; using System.Net; using System.Windows; using System.Windows.Threading; @@ -37,19 +38,7 @@ namespace QuickLook.Plugin.MarkdownViewer public bool CanHandle(string path) { - if (Directory.Exists(path)) - return false; - - switch (Path.GetExtension(path).ToLower()) - { - case ".markdown": - case ".md": - case ".rmd": - return true; - - default: - return false; - } + return !Directory.Exists(path) && new[] {".md", ".rmd", ".maekdown"}.Any(path.ToLower().EndsWith); } public void Prepare(string path, ContextObject context) diff --git a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/DpiHelpers.cs b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/DpiHelpers.cs deleted file mode 100644 index 445d0e0..0000000 --- a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/DpiHelpers.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright © 2017 Paddy Xu -// -// This file is part of QuickLook program. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -using System; -using System.Drawing; -using System.Runtime.InteropServices; - -namespace QuickLook.Plugin.PDFViewer -{ - internal static class DpiHelper - { - public enum DeviceCap - { - /// - /// Logical pixels inch in X - /// - LOGPIXELSX = 88, - /// - /// Logical pixels inch in Y - /// - LOGPIXELSY = 90 - } - - public const float DEFAULT_DPI = 96; - - public static Dpi GetCurrentDpi() - { - var g = Graphics.FromHwnd(IntPtr.Zero); - var desktop = g.GetHdc(); - - var dpi = new Dpi - { - HorizontalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSX), - VerticalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSY) - }; - - return dpi; - } - - [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] - public static extern int GetDeviceCaps(IntPtr hDC, int nIndex); - } - - internal class Dpi - { - public float HorizontalDpi { get; set; } - public float VerticalDpi { get; set; } - } -} \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Extensions.cs b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Extensions.cs deleted file mode 100644 index 406d972..0000000 --- a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Extensions.cs +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright © 2017 Paddy Xu -// -// This file is part of QuickLook program. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Drawing.Imaging; -using System.Windows; -using System.Windows.Media; -using System.Windows.Media.Imaging; - -namespace QuickLook.Plugin.PDFViewer -{ - public static class Extensions - { - public static void ForEach(this IEnumerable enumeration, Action action) - { - foreach (var item in enumeration) - action(item); - } - - public static T GetDescendantByType(this Visual element) where T : class - { - if (element == null) - return default(T); - if (element.GetType() == typeof(T)) - return element as T; - - T foundElement = null; - (element as FrameworkElement)?.ApplyTemplate(); - - for (var i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) - { - var visual = VisualTreeHelper.GetChild(element, i) as Visual; - foundElement = visual.GetDescendantByType(); - if (foundElement != null) - break; - } - return foundElement; - } - - public static BitmapSource ToBitmapSource(this Bitmap source) - { - BitmapSource bs = null; - try - { - var data = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), - ImageLockMode.ReadOnly, source.PixelFormat); - - bs = BitmapSource.Create(source.Width, source.Height, source.HorizontalResolution, - source.VerticalResolution, PixelFormats.Bgr24, null, - data.Scan0, data.Stride * source.Height, data.Stride); - - source.UnlockBits(data); - - bs.Freeze(); - } - catch - { - // ignored - } - - return bs; - } - } -} \ No newline at end of file diff --git a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/LibMuPdf.cs b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/LibMuPdf.cs index 6868a57..c7ce8fe 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/LibMuPdf.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/LibMuPdf.cs @@ -19,6 +19,7 @@ using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; +using QuickLook.Helpers; namespace QuickLook.Plugin.PDFViewer { @@ -37,9 +38,9 @@ namespace QuickLook.Plugin.PDFViewer else NativeMethods.BoundPage_32(document, page, ref pageBound); - var currentDpi = DpiHelper.GetCurrentDpi(); - var zoomX = zoomFactor * (currentDpi.HorizontalDpi / DpiHelper.DEFAULT_DPI); - var zoomY = zoomFactor * (currentDpi.VerticalDpi / DpiHelper.DEFAULT_DPI); + var scale = DpiHelper.GetCurrentScaleFactor(); + var zoomX = zoomFactor * scale.Horizontal; + var zoomY = zoomFactor * scale.Vertical; // gets the size of the page and multiplies it with zoom factors var width = (int) (pageBound.Width * zoomX); @@ -119,7 +120,7 @@ namespace QuickLook.Plugin.PDFViewer else NativeMethods.DropPixmap_32(context, pix); - bmp.SetResolution(currentDpi.HorizontalDpi, currentDpi.VerticalDpi); + bmp.SetResolution(scale.Horizontal * DpiHelper.DefaultDpi, scale.Vertical * DpiHelper.DefaultDpi); return bmp; } diff --git a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PageIdToImageConverter.cs b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PageIdToImageConverter.cs index c10db66..41409ae 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PageIdToImageConverter.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PageIdToImageConverter.cs @@ -18,6 +18,7 @@ using System; using System.Globalization; using System.Windows.Data; +using QuickLook.ExtensionMethods; namespace QuickLook.Plugin.PDFViewer { diff --git a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PdfViewerControl.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PdfViewerControl.xaml.cs index f992fb8..6216385 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PdfViewerControl.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/PdfViewerControl.xaml.cs @@ -212,7 +212,7 @@ namespace QuickLook.Plugin.PDFViewer // fill thumbnails list Enumerable.Range(0, PdfHandle.TotalPages).ForEach(PageIds.Add); - OnPropertyChanged("PageIds"); + OnPropertyChanged(nameof(PageIds)); CurrentPage = 0; pagePanel.DoZoomToFit(); diff --git a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs index 4f93602..a37e5ba 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/Plugin.cs @@ -24,6 +24,8 @@ namespace QuickLook.Plugin.PDFViewer { public class Plugin : IViewer { + private ContextObject _context; + private string _path; private PdfViewerControl _pdfControl; public int Priority => int.MaxValue; @@ -50,6 +52,9 @@ namespace QuickLook.Plugin.PDFViewer public void Prepare(string path, ContextObject context) { + _context = context; + _path = path; + var desiredSize = PdfViewerControl.GetDesiredControlSizeByFirstPage(path); context.SetPreferredSizeFit(desiredSize, 0.8); @@ -69,8 +74,8 @@ namespace QuickLook.Plugin.PDFViewer _pdfControl.LoadPdf(path); context.Title = $"{Path.GetFileName(path)} (1 / {_pdfControl.TotalPages})"; - _pdfControl.CurrentPageChanged += (sender2, e2) => context.Title = - $"{Path.GetFileName(path)} ({_pdfControl.CurrentPage + 1} / {_pdfControl.TotalPages})"; + + _pdfControl.CurrentPageChanged += UpdateVindowCaption; context.IsBusy = false; } catch (Exception e) @@ -87,8 +92,16 @@ namespace QuickLook.Plugin.PDFViewer { GC.SuppressFinalize(this); + _pdfControl.CurrentPageChanged -= UpdateVindowCaption; _pdfControl?.Dispose(); _pdfControl = null; + + _context = null; + } + + private void UpdateVindowCaption(object sender, EventArgs e2) + { + _context.Title = $"{Path.GetFileName(_path)} ({_pdfControl.CurrentPage + 1} / {_pdfControl.TotalPages})"; } ~Plugin() diff --git a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/QuickLook.Plugin.PdfViewer.csproj b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/QuickLook.Plugin.PdfViewer.csproj index a36f211..c1901ed 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/QuickLook.Plugin.PdfViewer.csproj +++ b/QuickLook.Plugin/QuickLook.Plugin.PDFViewer/QuickLook.Plugin.PdfViewer.csproj @@ -76,8 +76,6 @@ PdfViewerControl.xaml - - diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs index e01db86..7386afa 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/Plugin.cs @@ -37,14 +37,14 @@ namespace QuickLook.Plugin.TextViewer if (Directory.Exists(path)) return false; - const long MAX_SIZE = 20 * 1024 * 1024; + const long maxSize = 20 * 1024 * 1024; - if (Path.GetExtension(path).ToLower() == ".txt") - return new FileInfo(path).Length <= MAX_SIZE; + if (path.ToLower().EndsWith(".txt")) + return new FileInfo(path).Length <= maxSize; // if there is a matched highlighting scheme (by file extension), treat it as a plain text file if (HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(path)) != null) - return new FileInfo(path).Length <= MAX_SIZE; + return new FileInfo(path).Length <= maxSize; // otherwise, read the first 512KB, check if we can get something. using (var s = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) @@ -53,7 +53,7 @@ namespace QuickLook.Plugin.TextViewer var buffer = new byte[bufferLength]; var size = s.Read(buffer, 0, bufferLength); - return IsText(buffer, size) && new FileInfo(path).Length <= MAX_SIZE; + return IsText(buffer, size) && new FileInfo(path).Length <= maxSize; } } @@ -64,7 +64,7 @@ namespace QuickLook.Plugin.TextViewer public void View(string path, ContextObject context) { - _tvp = new TextViewerPanel(path, context); + _tvp = new TextViewerPanel(path); context.ViewerContent = _tvp; context.Title = $"{Path.GetFileName(path)}"; diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.xaml.cs index ccc7a6d..1617743 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.xaml.cs @@ -21,6 +21,7 @@ using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using ICSharpCode.AvalonEdit.Highlighting; +using QuickLook.Helpers; using UtfUnknown; namespace QuickLook.Plugin.TextViewer @@ -30,7 +31,7 @@ namespace QuickLook.Plugin.TextViewer /// public partial class TextViewerPanel : UserControl { - public TextViewerPanel(string path, ContextObject context) + public TextViewerPanel(string path) { InitializeComponent(); @@ -41,7 +42,7 @@ namespace QuickLook.Plugin.TextViewer viewer.PreviewMouseWheel += Viewer_MouseWheel; viewer.FontFamily = - new FontFamily(context.GetString("Editor_FontFamily", failsafe: "Consolas")); + new FontFamily(TranslationHelper.GetString("Editor_FontFamily")); LoadFile(path); } diff --git a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/Plugin.cs b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/Plugin.cs index 32c1bea..4bb7e7f 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/Plugin.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/Plugin.cs @@ -28,6 +28,18 @@ namespace QuickLook.Plugin.VideoViewer { public class Plugin : IViewer { + private static readonly string[] Formats = + { + // video + ".3g2", ".3gp", ".3gp2", ".3gpp", ".amv", ".asf", ".asf", ".avi", ".flv", ".m2ts", ".m4v", ".mkv", + ".mov", ".mp4", ".mp4v", ".mpeg", ".mpg", ".ogv", ".qt", ".vob", ".webm", ".wmv", + // audio + ".3gp", ".aa", ".aac", ".aax", ".act", ".aiff", ".amr", ".ape", ".au", ".awb", ".dct", ".dss", ".dvf", + ".flac", ".gsm", ".iklax", ".ivs", ".m4a", ".m4b", ".m4p", ".mmf", ".mp3", ".mpc", ".msv", ".ogg", + ".oga", ".mogg", ".opus", ".ra", ".rm", ".raw", ".tta", ".vox", ".wav", ".wma", ".wv", ".webm" + }; + private ContextObject _context; + private Size _mediaSize = Size.Empty; private ViewerPanel _vp; @@ -41,25 +53,13 @@ namespace QuickLook.Plugin.VideoViewer public bool CanHandle(string path) { - if (Directory.Exists(path)) - return false; - - var formats = new[] - { - // video - ".3g2", ".3gp", ".3gp2", ".3gpp", ".amv", ".asf", ".asf", ".avi", ".flv", ".m2ts", ".m4v", ".mkv", - ".mov", ".mp4", ".mp4v", ".mpeg", ".mpg", ".ogv", ".qt", ".vob", ".webm", ".wmv", - // audio - ".3gp", ".aa", ".aac", ".aax", ".act", ".aiff", ".amr", ".ape", ".au", ".awb", ".dct", ".dss", ".dvf", - ".flac", ".gsm", ".iklax", ".ivs", ".m4a", ".m4b", ".m4p", ".mmf", ".mp3", ".mpc", ".msv", ".ogg", - ".oga", ".mogg", ".opus", ".ra", ".rm", ".raw", ".tta", ".vox", ".wav", ".wma", ".wv", ".webm" - }; - - return formats.Contains(Path.GetExtension(path).ToLower()); + return !Directory.Exists(path) && Formats.Contains(Path.GetExtension(path).ToLower()); } public void Prepare(string path, ContextObject context) { + _context = context; + var def = new Size(450, 450); _mediaSize = GetMediaSizeWithVlc(path); @@ -77,7 +77,8 @@ namespace QuickLook.Plugin.VideoViewer context.ViewerContent = _vp; - _vp.mediaElement.VlcMediaPlayer.Opening += (sender, e) => context.IsBusy = false; + + _vp.mediaElement.VlcMediaPlayer.Opening += MarkReady; _vp.LoadAndPlay(path); @@ -89,8 +90,16 @@ namespace QuickLook.Plugin.VideoViewer public void Cleanup() { + _vp.mediaElement.VlcMediaPlayer.Opening -= MarkReady; _vp?.Dispose(); _vp = null; + + _context = null; + } + + private void MarkReady(object sender, ObjectEventArgs e) + { + _context.IsBusy = false; } private Size GetMediaSizeWithVlc(string path) diff --git a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs index 251ac66..0cf461c 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs @@ -266,7 +266,7 @@ namespace QuickLook.Plugin.VideoViewer [DebuggerNonUserCode] private void ShowErrorNotification(object sender, EventArgs e) { - _context.ShowNotification("", "An error occurred while loading the video."); + TrayIconManager.GetInstance().ShowNotification("", "An error occurred while loading the video."); mediaElement?.Stop(); Dispose(); diff --git a/QuickLook/Plugin/InfoPanel/Extensions.cs b/QuickLook/ExtensionMethods/BitmapExtensions.cs similarity index 85% rename from QuickLook/Plugin/InfoPanel/Extensions.cs rename to QuickLook/ExtensionMethods/BitmapExtensions.cs index f0d71bc..b35d6ac 100644 --- a/QuickLook/Plugin/InfoPanel/Extensions.cs +++ b/QuickLook/ExtensionMethods/BitmapExtensions.cs @@ -20,15 +20,15 @@ using System.Drawing.Imaging; using System.Windows.Media; using System.Windows.Media.Imaging; -namespace QuickLook.Plugin.InfoPanel +namespace QuickLook.ExtensionMethods { - public static class Extensions + public static class BitmapExtensions { - public static BitmapSource ToBitmapSource(this Bitmap source) + public static BitmapSource ToBitmapSource(this Bitmap old_source) { // BitmapSource.Create throws an exception when the image is scanned backward. // The Clone() will make it back scanning forward. - source = (Bitmap) source.Clone(); + var source = (Bitmap) old_source.Clone(); BitmapSource bs = null; try @@ -48,6 +48,10 @@ namespace QuickLook.Plugin.InfoPanel { // ignored } + finally + { + source.Dispose(); + } return bs; } diff --git a/QuickLook/ExtensionMethods/EnumerableExtensions.cs b/QuickLook/ExtensionMethods/EnumerableExtensions.cs new file mode 100644 index 0000000..fbb2200 --- /dev/null +++ b/QuickLook/ExtensionMethods/EnumerableExtensions.cs @@ -0,0 +1,31 @@ +// Copyright © 2017 Paddy Xu +// +// This file is part of QuickLook program. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +using System; +using System.Collections.Generic; + +namespace QuickLook.ExtensionMethods +{ + public static class EnumerableExtensions + { + public static void ForEach(this IEnumerable enumeration, Action action) + { + foreach (var item in enumeration) + action(item); + } + } +} \ No newline at end of file diff --git a/QuickLook/ExtensionMethods/FileExtensions.cs b/QuickLook/ExtensionMethods/FileExtensions.cs new file mode 100644 index 0000000..18b6c24 --- /dev/null +++ b/QuickLook/ExtensionMethods/FileExtensions.cs @@ -0,0 +1,48 @@ +// Copyright © 2017 Paddy Xu +// +// This file is part of QuickLook program. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +using System; + +namespace QuickLook.ExtensionMethods +{ + public static class FileExtensions + { + public static string ToPrettySize(this long value, int decimalPlaces = 0) + { + const long OneKb = 1024; + const long OneMb = OneKb * 1024; + const long OneGb = OneMb * 1024; + const long OneTb = OneGb * 1024; + + var asTb = Math.Round((double) value / OneTb, decimalPlaces); + var asGb = Math.Round((double) value / OneGb, decimalPlaces); + var asMb = Math.Round((double) value / OneMb, decimalPlaces); + var asKb = Math.Round((double) value / OneKb, decimalPlaces); + var chosenValue = asTb > 1 + ? $"{asTb} TB" + : asGb > 1 + ? $"{asGb} GB" + : asMb > 1 + ? $"{asMb} MB" + : asKb > 1 + ? $"{asKb} KB" + : $"{Math.Round((double) value, decimalPlaces)} bytes"; + + return chosenValue; + } + } +} \ No newline at end of file diff --git a/QuickLook/Helpers/DpiHelper.cs b/QuickLook/Helpers/DpiHelper.cs index 7813a37..6797232 100644 --- a/QuickLook/Helpers/DpiHelper.cs +++ b/QuickLook/Helpers/DpiHelper.cs @@ -21,26 +21,23 @@ using System.Runtime.InteropServices; namespace QuickLook.Helpers { - internal static class DpiHelper + public static class DpiHelper { - public const float DEFAULT_DPI = 96; + public const float DefaultDpi = 96; - public static Dpi GetCurrentDpi() + public static ScaleFactor GetCurrentScaleFactor() { var g = Graphics.FromHwnd(IntPtr.Zero); var desktop = g.GetHdc(); - var dpi = new Dpi - { - HorizontalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSX), - VerticalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSY) - }; + var horizontalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSX); + var verticalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSY); - return dpi; + return new ScaleFactor {Horizontal = horizontalDpi / DefaultDpi, Vertical = verticalDpi / DefaultDpi}; } [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] - public static extern int GetDeviceCaps(IntPtr hDC, int nIndex); + private static extern int GetDeviceCaps(IntPtr hDC, int nIndex); private enum DeviceCap { @@ -54,10 +51,10 @@ namespace QuickLook.Helpers LOGPIXELSY = 90 } - public struct Dpi + public struct ScaleFactor { - public float HorizontalDpi; - public float VerticalDpi; + public float Horizontal; + public float Vertical; } } } \ No newline at end of file diff --git a/QuickLook/Helpers/FileHelper.cs b/QuickLook/Helpers/FileHelper.cs index b8a080c..b602ebf 100644 --- a/QuickLook/Helpers/FileHelper.cs +++ b/QuickLook/Helpers/FileHelper.cs @@ -27,36 +27,37 @@ namespace QuickLook.Helpers { internal class FileHelper { - public static bool? GetAssocApplication(string path, out string appFriendlyName) + public static bool IsExecutable(string path, out string appFriendlyName) { appFriendlyName = string.Empty; - - if (string.IsNullOrEmpty(path)) - return null; - - if (Directory.Exists(path)) - return null; - - if (!File.Exists(path)) - return null; - var ext = Path.GetExtension(path).ToLower(); var isExe = new[] {".cmd", ".bat", ".pif", ".scf", ".exe", ".com", ".scr"}.Contains(ext.ToLower()); - // no assoc. app. found if (!isExe) - if (string.IsNullOrEmpty(GetAssocApplicationNative(ext, AssocStr.Command))) - if (string.IsNullOrEmpty(GetAssocApplicationNative(ext, AssocStr.AppId))) // UWP - return null; - - appFriendlyName = isExe - ? FileVersionInfo.GetVersionInfo(path).FileDescription - : GetAssocApplicationNative(ext, AssocStr.FriendlyAppName); + return false; + appFriendlyName = FileVersionInfo.GetVersionInfo(path).FileDescription; if (string.IsNullOrEmpty(appFriendlyName)) appFriendlyName = Path.GetFileName(path); - return isExe; + return true; + } + + public static bool GetAssocApplication(string path, out string appFriendlyName) + { + appFriendlyName = string.Empty; + var ext = Path.GetExtension(path).ToLower(); + + // no assoc. app. found + if (string.IsNullOrEmpty(GetAssocApplicationNative(ext, AssocStr.Command))) + if (string.IsNullOrEmpty(GetAssocApplicationNative(ext, AssocStr.AppId))) // UWP + return false; + + appFriendlyName = GetAssocApplicationNative(ext, AssocStr.FriendlyAppName); + if (string.IsNullOrEmpty(appFriendlyName)) + appFriendlyName = Path.GetFileName(path); + + return true; } [DllImport("shlwapi.dll", CharSet = CharSet.Auto, SetLastError = true)] diff --git a/QuickLook/Helpers/TranslationHelper.cs b/QuickLook/Helpers/TranslationHelper.cs index edfc31a..404dbbd 100644 --- a/QuickLook/Helpers/TranslationHelper.cs +++ b/QuickLook/Helpers/TranslationHelper.cs @@ -24,7 +24,7 @@ using System.Xml.XPath; namespace QuickLook.Helpers { - internal class TranslationHelper + public class TranslationHelper { private static readonly CultureInfo CurrentCultureInfo = CultureInfo.CurrentUICulture; //private static readonly CultureInfo CurrentCultureInfo = CultureInfo.GetCultureInfo("zh-CN"); diff --git a/QuickLook/Helpers/WindowHelper.cs b/QuickLook/Helpers/WindowHelper.cs index 6c13e1b..f14fb02 100644 --- a/QuickLook/Helpers/WindowHelper.cs +++ b/QuickLook/Helpers/WindowHelper.cs @@ -25,17 +25,15 @@ using QuickLook.NativeMethods; namespace QuickLook.Helpers { - internal static class WindowHelper + public static class WindowHelper { public static Rect GetCurrentWindowRect() { var screen = Screen.FromPoint(Cursor.Position).WorkingArea; - var dpi = DpiHelper.GetCurrentDpi(); - var scaleX = dpi.HorizontalDpi / DpiHelper.DEFAULT_DPI; - var scaleY = dpi.VerticalDpi / DpiHelper.DEFAULT_DPI; + var scale = DpiHelper.GetCurrentScaleFactor(); return new Rect( - new Point(screen.X / scaleX, screen.Y / scaleY), - new Size(screen.Width / scaleX, screen.Height / scaleY)); + new Point(screen.X / scale.Horizontal, screen.Y / scale.Vertical), + new Size(screen.Width / scale.Horizontal, screen.Height / scale.Vertical)); } public static void BringToFront(this Window window) diff --git a/QuickLook/MainWindowTransparent.xaml b/QuickLook/MainWindowTransparent.xaml index ba5f53a..98c916d 100644 --- a/QuickLook/MainWindowTransparent.xaml +++ b/QuickLook/MainWindowTransparent.xaml @@ -30,9 +30,14 @@ + + + + Duration="0:0:0.05" /> + Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.05" /> @@ -82,13 +86,14 @@ - + + - + + + - { - //if (AllowsTransparency) - // BlurWindow.EnableWindowBlur(this); - }; + windowCaptionContainer.MouseLeftButtonDown += WindowDragMoveStart; + windowCaptionContainer.MouseMove += WindowDragMoving; + windowCaptionContainer.MouseLeftButtonUp += WindowDragMoveEnd; - buttonPin.MouseLeftButtonUp += (sender, e) => + buttonPin.Click += (sender, e) => { if (Pinned) { @@ -68,7 +69,7 @@ namespace QuickLook ViewWindowManager.GetInstance().ForgetCurrentWindow(); }; - buttonCloseWindow.MouseLeftButtonUp += (sender, e) => + buttonCloseWindow.Click += (sender, e) => { if (Pinned) BeginClose(); @@ -84,10 +85,10 @@ namespace QuickLook ViewWindowManager.GetInstance().RunAndClosePreview(); }; - buttonWindowStatus.MouseLeftButtonUp += (sender, e) => + buttonWindowStatus.Click += (sender, e) => WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized; - buttonShare.MouseLeftButtonUp += + buttonShare.Click += (sender, e) => RunWith("rundll32.exe", $"shell32.dll,OpenAs_RunDLL {Path}"); } @@ -108,6 +109,53 @@ namespace QuickLook public event PropertyChangedEventHandler PropertyChanged; + private void WindowDragMoveEnd(object sender, MouseButtonEventArgs e) + { + _restoreForDragMove = false; + } + + private void WindowDragMoving(object sender, MouseEventArgs e) + { + if (!_restoreForDragMove) + return; + _restoreForDragMove = false; + + var scale = DpiHelper.GetCurrentScaleFactor(); + var point = PointToScreen(e.MouseDevice.GetPosition(this)); + point.X /= scale.Horizontal; + point.Y /= scale.Vertical; + + var monitor = WindowHelper.GetCurrentWindowRect(); + var precentLeft = (point.X - monitor.Left) / monitor.Width; + var precentTop = (point.Y - monitor.Top) / monitor.Height; + + Left = point.X - RestoreBounds.Width * precentLeft; + Top = point.Y - RestoreBounds.Height * precentTop; + + WindowState = WindowState.Normal; + + DragMove(); + } + + private void WindowDragMoveStart(object sender, MouseButtonEventArgs e) + { + if (e.ClickCount == 2) + { + if (ResizeMode != ResizeMode.CanResize && + ResizeMode != ResizeMode.CanResizeWithGrip) + return; + + WindowState = WindowState == WindowState.Maximized + ? WindowState.Normal + : WindowState.Maximized; + } + else + { + _restoreForDragMove = WindowState == WindowState.Maximized; + DragMove(); + } + } + internal void RunWith(string with, string arg) { if (string.IsNullOrEmpty(Path)) @@ -239,8 +287,6 @@ namespace QuickLook ResizeAndCenter(new Size(newWidth, newHeight)); - chrome.CaptionHeight = ContextObject.FullWindowDragging ? Height : windowCaptionContainer.Height - 5; - if (Visibility != Visibility.Visible) Show(); @@ -263,15 +309,43 @@ namespace QuickLook private void SetOpenWithButtonAndPath() { - var isExe = FileHelper.GetAssocApplication(Path, out string appFriendlyName); + buttonOpenWithText.Inlines.Clear(); - buttonOpenWith.Content = isExe == null - ? Directory.Exists(Path) - ? string.Format(TranslationHelper.GetString("MW_BrowseFolder"), System.IO.Path.GetFileName(Path)) - : string.Format(TranslationHelper.GetString("MW_Open"), System.IO.Path.GetFileName(Path)) - : isExe == true - ? string.Format(TranslationHelper.GetString("MW_Run"), appFriendlyName) - : string.Format(TranslationHelper.GetString("MW_OpenWith"), appFriendlyName); + if (Directory.Exists(Path)) + { + AddToInlines("MW_BrowseFolder", System.IO.Path.GetFileName(Path)); + return; + } + var isExe = FileHelper.IsExecutable(Path, out string appFriendlyName); + if (isExe) + { + AddToInlines("MW_Run", appFriendlyName); + return; + } + // not an exe + var found = FileHelper.GetAssocApplication(Path, out appFriendlyName); + if (found) + { + AddToInlines("MW_OpenWith", appFriendlyName); + return; + } + // assoc not found + AddToInlines("MW_Open", System.IO.Path.GetFileName(Path)); + + void AddToInlines(string str, string replaceWith) + { + str = TranslationHelper.GetString(str); + var elements = str.Split(new[] {"{0}"}, StringSplitOptions.None).ToList(); + while (elements.Count < 2) + elements.Add(string.Empty); + + buttonOpenWithText.Inlines.Add( + new Run(elements[0]) {FontWeight = FontWeights.Normal}); // text beforehand + buttonOpenWithText.Inlines.Add( + new Run(replaceWith) {FontWeight = FontWeights.SemiBold}); // appFriendlyName + buttonOpenWithText.Inlines.Add( + new Run(elements[1]) {FontWeight = FontWeights.Normal}); // text afterward + } } internal void BeginHide() @@ -301,15 +375,5 @@ namespace QuickLook { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } - - private void RemoveWindowChromeCaption(object sender, EventArgs e) - { - chrome.CaptionHeight = 0; - } - - private void RestoreWindowChromeCaption(object sender, EventArgs e) - { - chrome.CaptionHeight = ContextObject.FullWindowDragging ? Height : windowCaptionContainer.Height - 5; - } } } \ No newline at end of file diff --git a/QuickLook/Plugin/ContextObject.cs b/QuickLook/Plugin/ContextObject.cs index b1e5f83..711d59c 100644 --- a/QuickLook/Plugin/ContextObject.cs +++ b/QuickLook/Plugin/ContextObject.cs @@ -17,8 +17,6 @@ using System; using System.ComponentModel; -using System.Globalization; -using System.Reflection; using System.Runtime.CompilerServices; using System.Windows; using QuickLook.Annotations; @@ -142,26 +140,6 @@ namespace QuickLook.Plugin public event PropertyChangedEventHandler PropertyChanged; - /// - /// Get a string from translation Xml document. - /// - [MethodImpl(MethodImplOptions.NoInlining)] - public string GetString(string id, string file = null, CultureInfo locale = null, string failsafe = null) - { - return TranslationHelper.GetString(id, file, locale, failsafe, Assembly.GetCallingAssembly()); - } - - /// - /// Show a notification balloon. - /// - /// Title of the notification. - /// The content. - /// Is this indicates a error? - public void ShowNotification(string title, string content, bool isError = false) - { - TrayIconManager.GetInstance().ShowNotification(title, content, isError); - } - /// /// Set the size of viewer window and shrink to fit (to screen resolution). /// The window can take maximum (maxRatio*resolution) space. @@ -173,7 +151,7 @@ namespace QuickLook.Plugin if (maxRatio > 1) maxRatio = 1; - var max = GetMaximumDisplayBound(); + var max = WindowHelper.GetCurrentWindowRect(); var widthRatio = max.Width * maxRatio / size.Width; var heightRatio = max.Height * maxRatio / size.Height; @@ -186,14 +164,6 @@ namespace QuickLook.Plugin return ratio; } - /// - /// Get the device-independent resolution. - /// - public Rect GetMaximumDisplayBound() - { - return WindowHelper.GetCurrentWindowRect(); - } - internal void Reset() { ViewerWindow = null; diff --git a/QuickLook/Plugin/InfoPanel/DpiHelpers.cs b/QuickLook/Plugin/InfoPanel/DpiHelpers.cs deleted file mode 100644 index 16ebf41..0000000 --- a/QuickLook/Plugin/InfoPanel/DpiHelpers.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright © 2017 Paddy Xu -// -// This file is part of QuickLook program. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -using System; -using System.Drawing; -using System.Runtime.InteropServices; - -namespace QuickLook.Plugin.InfoPanel -{ - internal static class DpiHelper - { - public enum DeviceCap - { - /// - /// Logical pixels inch in X - /// - LOGPIXELSX = 88, - /// - /// Logical pixels inch in Y - /// - LOGPIXELSY = 90 - } - - public const float DEFAULT_DPI = 96; - - public static Dpi GetCurrentDpi() - { - var g = Graphics.FromHwnd(IntPtr.Zero); - var desktop = g.GetHdc(); - - var dpi = new Dpi - { - HorizontalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSX), - VerticalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSY) - }; - - return dpi; - } - - [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] - public static extern int GetDeviceCaps(IntPtr hDC, int nIndex); - } - - internal class Dpi - { - public float HorizontalDpi { get; set; } - public float VerticalDpi { get; set; } - } -} \ No newline at end of file diff --git a/QuickLook/Plugin/InfoPanel/FileHelper.cs b/QuickLook/Plugin/InfoPanel/FileHelper.cs index c4e1720..62eaf94 100644 --- a/QuickLook/Plugin/InfoPanel/FileHelper.cs +++ b/QuickLook/Plugin/InfoPanel/FileHelper.cs @@ -73,29 +73,5 @@ namespace QuickLook.Plugin.InfoPanel } } while (stack.Count != 0); } - - public static string ToPrettySize(this long value, int decimalPlaces = 0) - { - const long OneKb = 1024; - const long OneMb = OneKb * 1024; - const long OneGb = OneMb * 1024; - const long OneTb = OneGb * 1024; - - var asTb = Math.Round((double) value / OneTb, decimalPlaces); - var asGb = Math.Round((double) value / OneGb, decimalPlaces); - var asMb = Math.Round((double) value / OneMb, decimalPlaces); - var asKb = Math.Round((double) value / OneKb, decimalPlaces); - var chosenValue = asTb > 1 - ? $"{asTb} TB" - : asGb > 1 - ? $"{asGb} GB" - : asMb > 1 - ? $"{asMb} MB" - : asKb > 1 - ? $"{asKb} KB" - : $"{Math.Round((double) value, decimalPlaces)} bytes"; - - return chosenValue; - } } } \ No newline at end of file diff --git a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs index 6d00e04..5c72883 100644 --- a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs +++ b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs @@ -20,6 +20,7 @@ using System.Globalization; using System.IO; using System.Threading.Tasks; using System.Windows.Controls; +using QuickLook.ExtensionMethods; using QuickLook.Helpers; namespace QuickLook.Plugin.InfoPanel @@ -46,10 +47,12 @@ namespace QuickLook.Plugin.InfoPanel { Task.Run(() => { + var scale = DpiHelper.GetCurrentScaleFactor(); + var icon = WindowsThumbnailProvider.GetThumbnail(path, - (int) (128 * DpiHelper.GetCurrentDpi().HorizontalDpi / DpiHelper.DEFAULT_DPI), - (int) (128 * DpiHelper.GetCurrentDpi().VerticalDpi / DpiHelper.DEFAULT_DPI), + (int) (128 * scale.Horizontal), + (int) (128 * scale.Vertical), ThumbnailOptions.ScaleUp); var source = icon.ToBitmapSource(); diff --git a/QuickLook/QuickLook.csproj b/QuickLook/QuickLook.csproj index 643d291..5fc9626 100644 --- a/QuickLook/QuickLook.csproj +++ b/QuickLook/QuickLook.csproj @@ -120,6 +120,9 @@ + + + @@ -150,8 +153,6 @@ - - InfoPanel.xaml diff --git a/QuickLook/Styles/MainWindowStyles.xaml b/QuickLook/Styles/MainWindowStyles.xaml index 9c44b73..2a22a11 100644 --- a/QuickLook/Styles/MainWindowStyles.xaml +++ b/QuickLook/Styles/MainWindowStyles.xaml @@ -8,18 +8,73 @@ - + + + + + + diff --git a/QuickLook/Translations.config b/QuickLook/Translations.config index 44469b4..275ac35 100644 --- a/QuickLook/Translations.config +++ b/QuickLook/Translations.config @@ -5,10 +5,10 @@ Segoe UI QuickLook is running in the background. QuickLook is already running in the background. - Browse “{0}” - Open “{0}” - Open with “{0}” - Run “{0}” + Browse {0} + Open {0} + Open with {0} + Run {0} Run at &Startup QuickLook v{0} Check for &Updates... @@ -28,10 +28,10 @@ Segoe UI,Microsoft Yahei UI,Microsoft Yahei,SimSun QuickLook 正在后台运行。 另一个 QuickLook 进程正在运行。 - 浏览 “{0}” - 打开 “{0}” - 用 “{0}” 打开 - 运行 “{0}” + 浏览 {0} + 打开 {0} + 用 {0} 打开 + 运行 {0} 启动时自动运行 (&S) QuickLook v{0} 检查更新... (&U) @@ -51,10 +51,10 @@ Segoe UI,Microsoft JhengHei UI,Microsoft JhengHei,SimSun QuickLook 正在背景執行。 另一個 QuickLook 處理程序正在執行。 - 瀏覽 “{0}” - 開啟 “{0}” - 用 “{0}” 開啟 - 執行 “{0}” + 瀏覽 {0} + 開啟 {0} + 用 {0} 開啟 + 執行 {0} 系統啟動時自動執行 (&S) QuickLook v{0} 檢查更新... (&U) @@ -74,10 +74,10 @@ Segoe UI QuickLook está funcionando en segundo plano. QuickLook ya está funcionando en segundo plano. - Explorar “{0}” - Abrir “{0}” - Abrir con “{0}” - Iniciar “{0}” + Explorar {0} + Abrir {0} + Abrir con {0} + Iniciar {0} Iniciar con el &sistema QuickLook v{0} Buscar &actualizaciones... diff --git a/QuickLook/TrayIconManager.cs b/QuickLook/TrayIconManager.cs index efe0daa..74f7eba 100644 --- a/QuickLook/TrayIconManager.cs +++ b/QuickLook/TrayIconManager.cs @@ -88,7 +88,7 @@ namespace QuickLook } } - internal static TrayIconManager GetInstance() + public static TrayIconManager GetInstance() { return _instance ?? (_instance = new TrayIconManager()); }