diff --git a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs index 4077980..b8cd8ce 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs @@ -56,6 +56,8 @@ namespace QuickLook.Plugin.VideoViewer mediaElement.Stop(); _context.ShowNotification("", "An error occurred while loading the video."); + + throw new Exception(); } public void LoadAndPlay(string path) diff --git a/QuickLook/MainWindow.xaml b/QuickLook/MainWindow.xaml index 89c346d..e17d06d 100644 --- a/QuickLook/MainWindow.xaml +++ b/QuickLook/MainWindow.xaml @@ -20,22 +20,27 @@ - + @@ -52,37 +57,52 @@ - + - + - + diff --git a/QuickLook/MainWindow.xaml.cs b/QuickLook/MainWindow.xaml.cs index d70e978..cb05bd2 100644 --- a/QuickLook/MainWindow.xaml.cs +++ b/QuickLook/MainWindow.xaml.cs @@ -2,7 +2,8 @@ using System.Diagnostics; using System.Windows; using System.Windows.Input; -using System.Windows.Interop; +using System.Windows.Threading; +using QuickLook.ExtensionMethods; using QuickLook.Helpers; using QuickLook.Plugin; @@ -11,7 +12,7 @@ namespace QuickLook /// /// Interaction logic for MainWindow.xaml /// - internal partial class MainWindow : Window, IDisposable + internal partial class MainWindow : Window { internal MainWindow() { @@ -20,31 +21,23 @@ namespace QuickLook InitializeComponent(); + // revert designer changes + windowPanel.Opacity = 0d; + busyIndicatorLayer.Visibility = Visibility.Visible; + busyIndicatorLayer.Opacity = 1d; + // do not set TopMost property if we are now debugging. it makes debugging painful... if (!Debugger.IsAttached) Topmost = true; - // restore changes by Designer - windowPanel.Opacity = 0d; - busyIndicatorLayer.Visibility = Visibility.Visible; - Loaded += (sender, e) => AeroGlassHelper.EnableBlur(this); - buttonCloseWindow.MouseLeftButtonUp += (sender, e) => Close(); + buttonCloseWindow.MouseLeftButtonUp += (sender, e) => { Hide(); }; + titleBarArea.PreviewMouseLeftButtonDown += DragMoveCurrentWindow; } - public ContextObject ContextObject { get; } - - public void Dispose() - { - GC.SuppressFinalize(this); - - ContextObject?.Dispose(); - - // stop the background thread - busyDecorator?.Dispose(); - } + public ContextObject ContextObject { get; private set; } private void DragMoveCurrentWindow(object sender, MouseButtonEventArgs e) { @@ -71,17 +64,37 @@ namespace QuickLook Width = ContextObject.PreferredSize.Width + windowBorder.BorderThickness.Left + windowBorder.BorderThickness.Right; + Left = (SystemParameters.VirtualScreenWidth - Width) / 2; + Top = (SystemParameters.VirtualScreenHeight - Height) / 2; + ResizeMode = ContextObject.CanResize ? ResizeMode.CanResizeWithGrip : ResizeMode.NoResize; base.Show(); - if (!ContextObject.Focusable) - WindowHelper.SetNoactivate(new WindowInteropHelper(this)); + //if (!ContextObject.Focusable) + // WindowHelper.SetNoactivate(new WindowInteropHelper(this)); + } + + private new void Hide() + { + container.Content = null; + + // clean up plugin and refresh ContextObject for next use + ContextObject.ViewerPlugin?.Dispose(); + ContextObject.Reset(); + + GC.Collect(); + + // revert UI changes + ContextObject.IsBusy = true; + + Left -= 10000; + Dispatcher.Delay(100, _ => base.Hide()); } internal void BeginShow(IViewer matchedPlugin, string path) { - ContextObject.CurrentContentContainer = viewContentContainer; + ContextObject.CurrentContentContainer = container; ContextObject.ViewerPlugin = matchedPlugin; // get window size before showing it @@ -89,12 +102,19 @@ namespace QuickLook Show(); - matchedPlugin.View(path, ContextObject); + // load plugin, do not block UI + Dispatcher.BeginInvoke(new Action(() => matchedPlugin.View(path, ContextObject)), + DispatcherPriority.Render); } - ~MainWindow() + internal bool BeginHide() { - Dispose(); + if (Visibility != Visibility.Visible) + return false; + + Hide(); + + return true; } } } \ No newline at end of file diff --git a/QuickLook/Plugin/ContextObject.cs b/QuickLook/Plugin/ContextObject.cs index b161293..1635d91 100644 --- a/QuickLook/Plugin/ContextObject.cs +++ b/QuickLook/Plugin/ContextObject.cs @@ -2,6 +2,7 @@ using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; +using System.Windows.Controls; using QuickLook.Annotations; namespace QuickLook.Plugin @@ -9,12 +10,12 @@ namespace QuickLook.Plugin /// /// A runtime object which allows interaction between this plugin and QuickLook. /// - public class ContextObject : INotifyPropertyChanged, IDisposable + public class ContextObject : INotifyPropertyChanged { private bool _isBusy = true; private string _title = ""; - internal ViewContentContainer CurrentContentContainer; + internal ContentControl CurrentContentContainer; internal IViewer ViewerPlugin; /// @@ -35,8 +36,8 @@ namespace QuickLook.Plugin /// public object ViewerContent { - get => CurrentContentContainer.container.Content; - set => CurrentContentContainer.container.Content = value; + get => CurrentContentContainer.Content; + set => CurrentContentContainer.Content = value; } /// @@ -67,10 +68,8 @@ namespace QuickLook.Plugin /// public bool Focusable { get; set; } = false; - public void Dispose() + public void DisposePlugin() { - GC.SuppressFinalize(this); - ViewerPlugin?.Dispose(); ViewerPlugin = null; } @@ -105,7 +104,6 @@ namespace QuickLook.Plugin var heightRatio = max.Height * maxRatio / size.Height; var ratio = Math.Min(widthRatio, heightRatio); - if (ratio > 1) ratio = 1; PreferredSize = new Size {Width = size.Width * ratio, Height = size.Height * ratio}; @@ -121,15 +119,20 @@ namespace QuickLook.Plugin return new Size(SystemParameters.VirtualScreenWidth, SystemParameters.VirtualScreenHeight); } + internal void Reset() + { + Title = ""; + ViewerContent = null; + IsBusy = true; + PreferredSize = new Size(); + CanResize = true; + Focusable = false; + } + [NotifyPropertyChangedInvocator] protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } - - ~ContextObject() - { - Dispose(); - } } } \ No newline at end of file diff --git a/QuickLook/QuickLook.csproj b/QuickLook/QuickLook.csproj index 8fded32..e8e199f 100644 --- a/QuickLook/QuickLook.csproj +++ b/QuickLook/QuickLook.csproj @@ -101,9 +101,6 @@ - - ViewContentContainer.xaml - @@ -134,10 +131,6 @@ MainWindow.xaml Code - - Designer - MSBuild:Compile - MSBuild:Compile Designer diff --git a/QuickLook/ViewContentContainer.xaml b/QuickLook/ViewContentContainer.xaml deleted file mode 100644 index 83e09e7..0000000 --- a/QuickLook/ViewContentContainer.xaml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/QuickLook/ViewContentContainer.xaml.cs b/QuickLook/ViewContentContainer.xaml.cs deleted file mode 100644 index 72b5ce2..0000000 --- a/QuickLook/ViewContentContainer.xaml.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Windows.Controls; - -namespace QuickLook -{ - /// - /// Interaction logic for ViewContentContainer.xaml - /// - public partial class ViewContentContainer : UserControl - { - public ViewContentContainer() - { - InitializeComponent(); - } - } -} \ No newline at end of file diff --git a/QuickLook/ViewWindowManager.cs b/QuickLook/ViewWindowManager.cs index e5dc39b..8832183 100644 --- a/QuickLook/ViewWindowManager.cs +++ b/QuickLook/ViewWindowManager.cs @@ -15,7 +15,31 @@ namespace QuickLook { private static ViewWindowManager _instance; - private MainWindow _viewWindow; + private readonly MainWindow _viewWindow; + + internal ViewWindowManager() + { + _viewWindow = new MainWindow(); + _viewWindow.Closed += (sender, e) => + { + if (App.RunningAsViewer) + Application.Current.Shutdown(); + }; + } + + internal void InvokeRoutine() + { + if (!WindowHelper.IsFocusedControlExplorerItem()) + if (!WindowHelper.IsFocusedWindowSelf()) + return; + + if (_viewWindow.BeginHide()) + return; + + var path = GetCurrentSelection(); + + InvokeViewer(path); + } internal void InvokeViewer(string path) { @@ -29,43 +53,15 @@ namespace QuickLook BeginShowNewWindow(matchedPlugin, path); } - internal void InvokeRoutine() - { - if (!WindowHelper.IsFocusedControlExplorerItem()) - if (!WindowHelper.IsFocusedWindowSelf()) - return; - - if (CloseCurrentWindow()) - return; - - var path = GetCurrentSelection(); - - InvokeViewer(path); - } - private void BeginShowNewWindow(IViewer matchedPlugin, string path) { - _viewWindow = new MainWindow(); - _viewWindow.Closed += (sender2, e2) => - { - if (App.RunningAsViewer) - { - Application.Current.Shutdown(); - return; - } - - _viewWindow.Dispose(); - _viewWindow = null; - GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); - }; - try { _viewWindow.BeginShow(matchedPlugin, path); } catch (Exception e) // if current plugin failed, switch to default one. { - _viewWindow.Close(); + _viewWindow.BeginHide(); Debug.WriteLine(e.ToString()); Debug.WriteLine(e.StackTrace); @@ -81,34 +77,6 @@ namespace QuickLook throw; } } -#pragma warning disable 1058 - catch // Catch SEH exceptions here. -#pragma warning restore 1058 - { - _viewWindow.Close(); - - if (matchedPlugin.GetType() != PluginManager.GetInstance().DefaultPlugin) - { - matchedPlugin.Dispose(); - matchedPlugin = PluginManager.GetInstance().DefaultPlugin.CreateInstance(); - BeginShowNewWindow(matchedPlugin, path); - } - else - { - throw; - } - } - } - - private bool CloseCurrentWindow() - { - if (_viewWindow != null) - { - _viewWindow.Close(); - - return true; - } - return false; } private string GetCurrentSelection()