From 7676d5867b5fe618de57891364f93a74a504a45c Mon Sep 17 00:00:00 2001 From: Paddy Xu Date: Sat, 12 Aug 2017 18:17:04 +0300 Subject: [PATCH] all operations pass though Pipe --- .../PreviewPanel.xaml.cs | 5 +- QuickLook/App.xaml | 2 +- QuickLook/App.xaml.cs | 12 +- QuickLook/BackgroundListener.cs | 40 +++++- .../Controls/BusyDecorator/BusyDecorator.xaml | 5 +- QuickLook/FocusMonitor.cs | 30 ++-- QuickLook/Helpers/AutoStartupHelper.cs | 2 +- QuickLook/Helpers/Updater.cs | 11 +- QuickLook/MainWindowTransparent.xaml | 6 +- QuickLook/MainWindowTransparent.xaml.cs | 7 +- QuickLook/PipeServerManager.cs | 71 ++++++++-- QuickLook/Plugin/ContextObject.cs | 2 +- QuickLook/TrayIconManager.cs | 13 +- QuickLook/ViewWindowManager.cs | 128 ++++-------------- 14 files changed, 160 insertions(+), 174 deletions(-) diff --git a/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewPanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewPanel.xaml.cs index d3c5601..6eab082 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewPanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.IPreviewHandlers/PreviewPanel.xaml.cs @@ -17,6 +17,7 @@ using System; using System.Runtime.InteropServices; +using System.Windows; using System.Windows.Controls; using System.Windows.Threading; @@ -36,7 +37,7 @@ namespace QuickLook.Plugin.IPreviewHandlers public void Dispose() { - Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => + Application.Current.Dispatcher.BeginInvoke(new Action(() => { presenter.Child = null; presenter?.Dispose(); @@ -48,7 +49,7 @@ namespace QuickLook.Plugin.IPreviewHandlers public void PreviewFile(string file, ContextObject context) { - Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => + Application.Current.Dispatcher.BeginInvoke(new Action(() => { _control = new PreviewHandlerHost(); presenter.Child = _control; diff --git a/QuickLook/App.xaml b/QuickLook/App.xaml index ced5e3a..e0aa916 100644 --- a/QuickLook/App.xaml +++ b/QuickLook/App.xaml @@ -10,7 +10,7 @@ - ./Fonts/#Segoe MDL2 Assets + ./Fonts/#Segoe MDL2 Assets \ No newline at end of file diff --git a/QuickLook/App.xaml.cs b/QuickLook/App.xaml.cs index 08f8ee9..6677732 100644 --- a/QuickLook/App.xaml.cs +++ b/QuickLook/App.xaml.cs @@ -22,7 +22,6 @@ using System.Reflection; using System.Threading; using System.Threading.Tasks; using System.Windows; -using System.Windows.Threading; using QuickLook.Helpers; using QuickLook.Properties; @@ -103,26 +102,23 @@ namespace QuickLook private void RemoteCallShowPreview(StartupEventArgs e) { - PipeServerManager.SendMessage(e.Args.First()); + PipeServerManager.SendMessage(PipeMessages.Toggle, e.Args.First()); } private void RunListener(StartupEventArgs e) { TrayIconManager.GetInstance(); if (!e.Args.Contains("/autorun") && !IsUWP) - TrayIconManager.GetInstance() - .ShowNotification("", TranslationHelper.GetString("APP_START")); + TrayIconManager.ShowNotification("", TranslationHelper.GetString("APP_START")); if (e.Args.Contains("/first")) AutoStartupHelper.CreateAutorunShortcut(); NativeMethods.QuickLook.Init(); PluginManager.GetInstance(); + ViewWindowManager.GetInstance(); BackgroundListener.GetInstance(); - PipeServerManager.GetInstance().MessageReceived += - (msg, ea) => Dispatcher.BeginInvoke( - new Action(() => ViewWindowManager.GetInstance().InvokeViewer(msg as string, true)), - DispatcherPriority.ApplicationIdle); + PipeServerManager.GetInstance(); } private void App_OnExit(object sender, ExitEventArgs e) diff --git a/QuickLook/BackgroundListener.cs b/QuickLook/BackgroundListener.cs index 18f2808..3cbecfc 100644 --- a/QuickLook/BackgroundListener.cs +++ b/QuickLook/BackgroundListener.cs @@ -16,8 +16,8 @@ // along with this program. If not, see . using System; +using System.Diagnostics; using System.Windows.Forms; -using System.Windows.Threading; using QuickLook.Helpers; namespace QuickLook @@ -60,22 +60,50 @@ namespace QuickLook { _isKeyDownInDesktopOrShell = NativeMethods.QuickLook.GetFocusedWindowType() != NativeMethods.QuickLook.FocusedWindowType.Invalid; + _isKeyDownInDesktopOrShell |= WindowHelper.IsForegroundWindowBelongToSelf(); } // call InvokeRoutine only when the KeyDown is valid if (_isKeyDownInDesktopOrShell) - Dispatcher.CurrentDispatcher.BeginInvoke( - new Action(down => - ViewWindowManager.GetInstance().InvokeRoutine(e, down)), - DispatcherPriority.ApplicationIdle, - isKeyDown); + InvokeRoutine(e.KeyCode, isKeyDown); // reset variable only when KeyUp if (!isKeyDown) _isKeyDownInDesktopOrShell = false; } + private void InvokeRoutine(Keys key, bool isKeyDown) + { + var path = NativeMethods.QuickLook.GetCurrentSelection(); + + Debug.WriteLine($"InvokeRoutine: key={key},down={isKeyDown}"); + + if (isKeyDown) + switch (key) + { + case Keys.Enter: + PipeServerManager.SendMessage(PipeMessages.RunAndClose); + break; + } + else + switch (key) + { + case Keys.Up: + case Keys.Down: + case Keys.Left: + case Keys.Right: + PipeServerManager.SendMessage(PipeMessages.Invoke, path); + break; + case Keys.Space: + PipeServerManager.SendMessage(PipeMessages.Toggle, path); + break; + case Keys.Escape: + PipeServerManager.SendMessage(PipeMessages.Close); + break; + } + } + private void InstallKeyHook(KeyEventHandler downHandler, KeyEventHandler upHandler) { _hook = GlobalKeyboardHook.GetInstance(); diff --git a/QuickLook/Controls/BusyDecorator/BusyDecorator.xaml b/QuickLook/Controls/BusyDecorator/BusyDecorator.xaml index 88a103d..1a44713 100644 --- a/QuickLook/Controls/BusyDecorator/BusyDecorator.xaml +++ b/QuickLook/Controls/BusyDecorator/BusyDecorator.xaml @@ -10,7 +10,10 @@ - + +  + diff --git a/QuickLook/FocusMonitor.cs b/QuickLook/FocusMonitor.cs index b90b2e1..df8a99b 100644 --- a/QuickLook/FocusMonitor.cs +++ b/QuickLook/FocusMonitor.cs @@ -15,7 +15,6 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -using System; using System.Threading; using System.Threading.Tasks; @@ -23,20 +22,21 @@ namespace QuickLook { internal class FocusMonitor { - public delegate void HeartbeatEventHandler(HeartbeatEventArgs e); - private static FocusMonitor _instance; public bool IsRunning { get; private set; } - public event HeartbeatEventHandler Heartbeat; - public void Start() { + if (IsRunning) + return; + IsRunning = true; new Task(() => { + var last = string.Empty; + while (IsRunning) { Thread.Sleep(500); @@ -45,8 +45,12 @@ namespace QuickLook NativeMethods.QuickLook.FocusedWindowType.Invalid) continue; - var file = NativeMethods.QuickLook.GetCurrentSelection(); - Heartbeat?.Invoke(new HeartbeatEventArgs(DateTime.Now.Ticks, file)); + var path = NativeMethods.QuickLook.GetCurrentSelection(); + if (IsRunning && last != path) + { + last = path; + PipeServerManager.SendMessage(PipeMessages.Invoke, path); + } } }).Start(); } @@ -61,16 +65,4 @@ namespace QuickLook return _instance ?? (_instance = new FocusMonitor()); } } - - internal class HeartbeatEventArgs : EventArgs - { - public HeartbeatEventArgs(long tick, string files) - { - InvokeTick = tick; - FocusedFile = files; - } - - public long InvokeTick { get; } - public string FocusedFile { get; } - } } \ No newline at end of file diff --git a/QuickLook/Helpers/AutoStartupHelper.cs b/QuickLook/Helpers/AutoStartupHelper.cs index 4fa2a43..6e80a7e 100644 --- a/QuickLook/Helpers/AutoStartupHelper.cs +++ b/QuickLook/Helpers/AutoStartupHelper.cs @@ -50,7 +50,7 @@ namespace QuickLook.Helpers } catch (Exception) { - TrayIconManager.GetInstance().ShowNotification("", "Failed to add QuickLook to Startup folder."); + TrayIconManager.ShowNotification("", "Failed to add QuickLook to Startup folder."); } } diff --git a/QuickLook/Helpers/Updater.cs b/QuickLook/Helpers/Updater.cs index 9aacabe..f9e287d 100644 --- a/QuickLook/Helpers/Updater.cs +++ b/QuickLook/Helpers/Updater.cs @@ -47,7 +47,7 @@ namespace QuickLook.Helpers { if (!silent) Application.Current.Dispatcher.Invoke( - () => TrayIconManager.GetInstance().ShowNotification("", + () => TrayIconManager.ShowNotification("", TranslationHelper.GetString("Update_NoUpdate"))); return; } @@ -57,7 +57,7 @@ namespace QuickLook.Helpers Application.Current.Dispatcher.Invoke( () => { - TrayIconManager.GetInstance().ShowNotification("", + TrayIconManager.ShowNotification("", string.Format(TranslationHelper.GetString("Update_Found"), nVersion), timeout: 20000, clickEvent: @@ -69,7 +69,7 @@ namespace QuickLook.Helpers { Debug.WriteLine(e.Message); Application.Current.Dispatcher.Invoke( - () => TrayIconManager.GetInstance().ShowNotification("", + () => TrayIconManager.ShowNotification("", string.Format(TranslationHelper.GetString("Update_Error"), e.Message))); } }); @@ -94,14 +94,13 @@ namespace QuickLook.Helpers var changeLogPath = Path.GetTempFileName() + ".md"; File.WriteAllText(changeLogPath, notes); - Application.Current.Dispatcher.Invoke(() => ViewWindowManager.GetInstance() - .InvokeViewer(changeLogPath)); + PipeServerManager.SendMessage(PipeMessages.Invoke, changeLogPath); } catch (Exception e) { Debug.WriteLine(e.Message); Application.Current.Dispatcher.Invoke( - () => TrayIconManager.GetInstance().ShowNotification("", + () => TrayIconManager.ShowNotification("", string.Format(TranslationHelper.GetString("Update_Error"), e.Message))); } }); diff --git a/QuickLook/MainWindowTransparent.xaml b/QuickLook/MainWindowTransparent.xaml index a7b24c7..c331c6c 100644 --- a/QuickLook/MainWindowTransparent.xaml +++ b/QuickLook/MainWindowTransparent.xaml @@ -86,7 +86,8 @@ GlassVisibility="{Binding ContextObject.TitlebarBlurVisibility, ElementName=mainWindow, Converter={StaticResource BooleanToVisibilityConverter}}" NoiseVisibility="{Binding ContextObject.TitlebarBlurVisibility, ElementName=mainWindow, Converter={StaticResource BooleanToVisibilityConverter}}" /> - -