Skip to content

Commit

Permalink
follow updates
Browse files Browse the repository at this point in the history
  • Loading branch information
SplitGemini committed Jan 21, 2021
1 parent 36f2604 commit eb8325e
Show file tree
Hide file tree
Showing 39 changed files with 507 additions and 1,195 deletions.
65 changes: 13 additions & 52 deletions QuickLook.Common/ExtensionMethods/EncodingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using NChardet;
using System;
using System.Diagnostics;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using UtfUnknown;

Expand All @@ -15,78 +14,40 @@ public static class EncodingExtensions
/// </summary>
public static Encoding GetEncoding(string filename, int taster = 1000)
{
//unix 可能是识别失败,使用奇葩方法
if(filename.ToLower().EndsWith("yml") || filename.ToLower().EndsWith("log"))
/* 改为默认uf8奇葩方法保留
//unix 可能是识别失败,使用奇葩方法,常见代码默认utf-8
//黑名单自动识别
string[] black_list = { ".txt" };
if(!black_list.Any(filename.ToLower().EndsWith))
{
return Encoding.UTF8;
}
*/
var encoding = Encoding.Default;
Detector detect = new Detector();
CharsetDetectionObserver cdo = new CharsetDetectionObserver();
detect.Init(cdo);
var buffer = new MemoryStream();
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var lb = new byte[8192];
bool done = false;
bool isAscii = true;
while (fs.Position < fs.Length && buffer.Length < taster)
{
var lb = new byte[8192];
int len = fs.Read(lb, 0, lb.Length);
buffer.Write(lb, 0, len);

// 探测是否为 Ascii 编码
if (isAscii)
isAscii = detect.isAscii(lb, len);

// 如果不是 Ascii 编码,并且编码未确定,则继续探测
if (!isAscii && !done)
done = detect.DoIt(lb, len, false);
//if (done)
//break;
}
}
detect.DataEnd();
var bufferCopy = buffer.ToArray();
buffer.Dispose();
var Detected = CharsetDetector.DetectFromBytes(bufferCopy).Detected;
Debug.WriteLine("Confidence = " + Detected?.Confidence);
if (Detected?.Confidence > 0.5)
{
encoding = Detected.Encoding ?? Encoding.Default;
encoding = Detected.Encoding ?? Encoding.UTF8;
Debug.WriteLine("UTF-UNKNOWN Charset = " + encoding.EncodingName);
return Detected.Encoding;
}
string[] prob = detect.getProbableCharsets();
if (prob.Length > 0)
{
try {
encoding = Encoding.GetEncoding(prob[0]);
foreach(var str in prob)
{
Debug.WriteLine("NChardet Probable Charset = " + str);
}
}
catch
{
encoding = Encoding.Default;
}

}
return encoding;
} else return Encoding.UTF8;
}
public static Encoding GetEncoding(byte[] buffer)
{
return CharsetDetector.DetectFromBytes(buffer).Detected?.Encoding ?? Encoding.Default;
}
}
public class CharsetDetectionObserver : ICharsetDetectionObserver
{
public string Charset = null;

public void Notify(string charset)
{
Charset = charset;
return CharsetDetector.DetectFromBytes(buffer).Detected?.Encoding ?? Encoding.UTF8;
}
}

Expand Down
66 changes: 59 additions & 7 deletions QuickLook.Common/Helpers/DpiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,78 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using QuickLook.Common.NativeMethods;

namespace QuickLook.Common.Helpers
{
public static class DpiHelper
{
public const float DefaultDpi = 96;
public const int DefaultDpi = 96;

public static ScaleFactor GetScaleFactorFromWindow(Window window)
{
return GetScaleFactorFromWindow(new WindowInteropHelper(window).EnsureHandle());
}

public static ScaleFactor GetCurrentScaleFactor()
{
var g = Graphics.FromHwnd(IntPtr.Zero);
var desktop = g.GetHdc();
return GetScaleFactorFromWindow(User32.GetForegroundWindow());
}

public static ScaleFactor GetScaleFactorFromWindow(IntPtr hwnd)
{
var dpiX = DefaultDpi;
var dpiY = DefaultDpi;

var horizontalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSX);
var verticalDpi = GetDeviceCaps(desktop, (int) DeviceCap.LOGPIXELSY);
try
{
if (Environment.OSVersion.Version > new Version(6, 2)) // Windows 8.1 = 6.3.9200
{
var hMonitor = MonitorFromWindow(hwnd, MonitorDefaults.TOPRIMARY);
GetDpiForMonitor(hMonitor, MonitorDpiType.EFFECTIVE_DPI, out dpiX, out dpiY);
}
else
{
var g = Graphics.FromHwnd(IntPtr.Zero);
var desktop = g.GetHdc();

return new ScaleFactor {Horizontal = horizontalDpi / DefaultDpi, Vertical = verticalDpi / DefaultDpi};
dpiX = GetDeviceCaps(desktop, DeviceCap.LOGPIXELSX);
dpiY = GetDeviceCaps(desktop, DeviceCap.LOGPIXELSY);
}
}
catch (Exception e)
{
ProcessHelper.WriteLog(e.ToString());
}

return new ScaleFactor {Horizontal = (float) dpiX / DefaultDpi, Vertical = (float) dpiY / DefaultDpi};
}

[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
private static extern int GetDeviceCaps(IntPtr hDC, int nIndex);
private static extern int GetDeviceCaps(IntPtr hDC, DeviceCap nIndex);

[DllImport("user32.dll")]
private static extern IntPtr MonitorFromWindow(IntPtr hWnd, MonitorDefaults dwFlags);

[DllImport("shcore.dll")]
private static extern uint
GetDpiForMonitor(IntPtr hMonitor, MonitorDpiType dpiType, out int dpiX, out int dpiY);

private enum MonitorDpiType
{
EFFECTIVE_DPI = 0,
ANGULAR_DPI = 1,
RAW_DPI = 2
}

private enum MonitorDefaults
{
TONULL = 0,
TOPRIMARY = 1,
TONEAREST = 2
}

private enum DeviceCap
{
Expand Down
11 changes: 11 additions & 0 deletions QuickLook.Common/Helpers/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using QuickLook.Common.NativeMethods;

namespace QuickLook.Common.Helpers
Expand Down Expand Up @@ -62,6 +64,15 @@ public static bool IsOnWindows10S()
return osType == PRODUCT_CLOUD || osType == PRODUCT_CLOUDN;
}

public static bool IsShuttingDown()
{
var isShuttingDownProperty =
typeof(Application).GetProperty("IsShuttingDown", BindingFlags.NonPublic | BindingFlags.Static);
if (isShuttingDownProperty == null)
throw new Exception("Unable to detect Application.IsShuttingDown.");
return (bool)isShuttingDownProperty.GetValue(Application.Current);
}

public static void WriteLog(string msg)
{
Debug.WriteLine(msg);
Expand Down
48 changes: 32 additions & 16 deletions QuickLook.Common/Helpers/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,32 @@ public enum WindowCompositionAttribute
WcaAccentPolicy = 19
}

public static Rect GetCurrentDesktopRect()
public static Size GetCurrentDesktopSize()
{
return GetDesktopRectFromWindow(User32.GetForegroundWindow());
var scale = DpiHelper.GetCurrentScaleFactor();
var rect = GetCurrentDesktopRectInPixel();

return new Size(rect.Width / scale.Horizontal, rect.Height / scale.Vertical);
}

public static Rect GetCurrentDesktopRectInPixel()
{
return GetDesktopRectFromWindowInPixel(User32.GetForegroundWindow());
}

public static Rect GetDesktopRectFromWindow(Window window)
public static Rect GetDesktopRectFromWindowInPixel(Window window)
{
return GetDesktopRectFromWindow(new WindowInteropHelper(window).Handle);
return GetDesktopRectFromWindowInPixel(new WindowInteropHelper(window).Handle);
}

public static Rect GetDesktopRectFromWindow(IntPtr hwnd)
public static Rect GetDesktopRectFromWindowInPixel(IntPtr hwnd)
{
var screen = Screen.FromHandle(hwnd).WorkingArea;

var scale = DpiHelper.GetCurrentScaleFactor();
return new Rect(
new Point(screen.X / scale.Horizontal, screen.Y / scale.Vertical),
new Size(screen.Width / scale.Horizontal, screen.Height / scale.Vertical));
var area = new Rect(new Point(screen.X, screen.Y),
new Size(screen.Width, screen.Height));

return area;
}

public static void BringToFront(this Window window, bool keep)
Expand All @@ -67,20 +75,28 @@ public static void BringToFront(this Window window, bool keep)
}

public static void MoveWindow(this Window window,
double left,
double top,
double pxLeft,
double pxTop,
double width,
double height)
{
int pxLeft = 0, pxTop = 0;
if (left != 0 || top != 0)
TransformToPixels(window, left, top,
out pxLeft, out pxTop);
var handle = new WindowInteropHelper(window).EnsureHandle();

// scale the size to the primary display
TransformToPixels(window, width, height,
out var pxWidth, out var pxHeight);

User32.MoveWindow(new WindowInteropHelper(window).Handle, pxLeft, pxTop, pxWidth, pxHeight, true);
// Use absolute location and relative size. WPF will scale the size to the target display
User32.MoveWindow(handle, (int) Math.Round(pxLeft), (int) Math.Round(pxTop), pxWidth, pxHeight, true);
}

public static Rect GetWindowRectInPixel(this Window window)
{
var handle = new WindowInteropHelper(window).EnsureHandle();

User32.GetWindowRect(handle, out User32.RECT nRect);

return new Rect(new Point(nRect.Left, nRect.Top), new Point(nRect.Right, nRect.Bottom));
}

private static void TransformToPixels(this Visual visual,
Expand Down
Loading

0 comments on commit eb8325e

Please sign in to comment.