reuse plugin instances

This commit is contained in:
Paddy Xu
2017-05-16 19:52:17 +03:00
parent b9d83bfd91
commit fd080f80e4
17 changed files with 73 additions and 51 deletions

View File

@@ -45,7 +45,7 @@ namespace QuickLook.Plugin.ArchiveViewer
context.IsBusy = false; context.IsBusy = false;
} }
public void Dispose() public void Cleanup()
{ {
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
@@ -54,7 +54,7 @@ namespace QuickLook.Plugin.ArchiveViewer
~Plugin() ~Plugin()
{ {
Dispose(); Cleanup();
} }
} }
} }

View File

@@ -43,7 +43,7 @@ namespace QuickLook.Plugin.HtmlViewer
context.IsBusy = false; context.IsBusy = false;
} }
public void Dispose() public void Cleanup()
{ {
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
@@ -52,7 +52,7 @@ namespace QuickLook.Plugin.HtmlViewer
~Plugin() ~Plugin()
{ {
Dispose(); Cleanup();
} }
} }
} }

View File

@@ -50,7 +50,7 @@ namespace QuickLook.Plugin.ImageViewer
context.IsBusy = false; context.IsBusy = false;
} }
public void Dispose() public void Cleanup()
{ {
} }
} }

View File

@@ -44,7 +44,7 @@ namespace QuickLook.Plugin.MarkdownViewer
context.IsBusy = false; context.IsBusy = false;
} }
public void Dispose() public void Cleanup()
{ {
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
@@ -53,7 +53,7 @@ namespace QuickLook.Plugin.MarkdownViewer
~Plugin() ~Plugin()
{ {
Dispose(); Cleanup();
} }
private string GenerateMarkdownHtml(string path) private string GenerateMarkdownHtml(string path)

View File

@@ -69,7 +69,7 @@ namespace QuickLook.Plugin.OfficeViewer
context.IsBusy = false; context.IsBusy = false;
} }
public void Dispose() public void Cleanup()
{ {
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
@@ -89,7 +89,7 @@ namespace QuickLook.Plugin.OfficeViewer
~PluginInterface() ~PluginInterface()
{ {
Dispose(); Cleanup();
} }
} }
} }

View File

@@ -49,7 +49,7 @@ namespace QuickLook.Plugin.PDFViewer
}; };
} }
public void Dispose() public void Cleanup()
{ {
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
@@ -59,7 +59,7 @@ namespace QuickLook.Plugin.PDFViewer
~Plugin() ~Plugin()
{ {
Dispose(); Cleanup();
} }
} }
} }

View File

@@ -56,7 +56,7 @@ namespace QuickLook.Plugin.TextViewer
context.IsBusy = false; context.IsBusy = false;
} }
public void Dispose() public void Cleanup()
{ {
} }
} }

View File

@@ -52,7 +52,7 @@ namespace QuickLook.Plugin.VideoViewer
context.IsBusy = false; context.IsBusy = false;
} }
public void Dispose() public void Cleanup()
{ {
_vp?.Dispose(); _vp?.Dispose();
} }

View File

@@ -77,13 +77,16 @@ namespace QuickLook
private new void Hide() private new void Hide()
{ {
if (App.RunningAsViewer)
Application.Current.Shutdown();
container.Content = null; container.Content = null;
// clean up plugin and refresh ContextObject for next use // clean up plugin and refresh ContextObject for next use
ContextObject.ViewerPlugin?.Dispose(); ContextObject.ViewerPlugin?.Cleanup();
ContextObject.Reset(); ContextObject.Reset();
//GC.Collect(); GC.Collect();
// revert UI changes // revert UI changes
ContextObject.IsBusy = true; ContextObject.IsBusy = true;

View File

@@ -5,6 +5,8 @@ namespace QuickLook
{ {
internal static class PidHelper internal static class PidHelper
{ {
private static FileStream _pidLocker;
private static readonly string PidListener = private static readonly string PidListener =
Path.Combine(Path.GetTempPath(), "QuickLook.App.Listener.D6EC3F8DDF6B.pid"); Path.Combine(Path.GetTempPath(), "QuickLook.App.Listener.D6EC3F8DDF6B.pid");
@@ -35,11 +37,18 @@ namespace QuickLook
internal static void WritePid() internal static void WritePid()
{ {
File.WriteAllText(App.RunningAsViewer ? PidViewer : PidListener, Process.GetCurrentProcess().Id.ToString()); var pidFile = App.RunningAsViewer ? PidViewer : PidListener;
File.WriteAllText(pidFile, Process.GetCurrentProcess().Id.ToString());
_pidLocker = File.Open(pidFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
} }
internal static void DeletePid() internal static void DeletePid()
{ {
_pidLocker?.Close();
_pidLocker = null;
File.Delete(App.RunningAsViewer ? PidViewer : PidListener); File.Delete(App.RunningAsViewer ? PidViewer : PidListener);
} }
} }

View File

@@ -70,7 +70,7 @@ namespace QuickLook.Plugin
public void DisposePlugin() public void DisposePlugin()
{ {
ViewerPlugin?.Dispose(); ViewerPlugin?.Cleanup();
ViewerPlugin = null; ViewerPlugin = null;
} }

View File

@@ -35,6 +35,6 @@
/// <summary> /// <summary>
/// Release any unmanaged resource here. /// Release any unmanaged resource here.
/// </summary> /// </summary>
void Dispose(); void Cleanup();
} }
} }

View File

@@ -11,7 +11,24 @@
<ColumnDefinition Width="150" /> <ColumnDefinition Width="150" />
<ColumnDefinition Width="65*" /> <ColumnDefinition Width="65*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Image x:Name="image" Grid.Column="0" Height="128" Width="128" Stretch="Fill" Margin="0,-20,0,0" /> <Image x:Name="image" Grid.Column="0" Height="128" Width="128" Stretch="Fill" Opacity="0" Margin="0,-20,0,0">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding Source, ElementName=image}" Value="{x:Null}">
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1"
BeginTime="0:0:0" Duration="0:0:0.05" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<Grid Grid.Column="1"> <Grid Grid.Column="1">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="20" /> <ColumnDefinition Width="20" />

View File

@@ -1,4 +1,5 @@
using System.Globalization; using System;
using System.Globalization;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Controls; using System.Windows.Controls;
@@ -24,6 +25,8 @@ namespace QuickLook.Plugin.InfoPanel
} }
public void DisplayInfo(string path) public void DisplayInfo(string path)
{
Task.Run(() =>
{ {
var icon = var icon =
WindowsThumbnailProvider.GetThumbnail(path, WindowsThumbnailProvider.GetThumbnail(path,
@@ -31,10 +34,12 @@ namespace QuickLook.Plugin.InfoPanel
(int) (128 * DpiHelper.GetCurrentDpi().VerticalDpi / DpiHelper.DEFAULT_DPI), (int) (128 * DpiHelper.GetCurrentDpi().VerticalDpi / DpiHelper.DEFAULT_DPI),
ThumbnailOptions.ScaleUp); ThumbnailOptions.ScaleUp);
image.Source = icon.ToBitmapSource(); var source = icon.ToBitmapSource();
icon.Dispose(); icon.Dispose();
Dispatcher.BeginInvoke(new Action(() => image.Source = source));
});
var name = Path.GetFileName(path); var name = Path.GetFileName(path);
filename.Text = string.IsNullOrEmpty(name) ? path : name; filename.Text = string.IsNullOrEmpty(name) ? path : name;

View File

@@ -3,7 +3,7 @@ using System.Windows;
namespace QuickLook.Plugin.InfoPanel namespace QuickLook.Plugin.InfoPanel
{ {
public class PluginInterface : IViewer, IDisposable public class PluginInterface : IViewer
{ {
private InfoPanel _ip; private InfoPanel _ip;
@@ -31,14 +31,9 @@ namespace QuickLook.Plugin.InfoPanel
context.IsBusy = false; context.IsBusy = false;
} }
public void Dispose() public void Cleanup()
{ {
_ip.Stop = true; _ip.Stop = true;
} }
~PluginInterface()
{
Dispose();
}
} }
} }

View File

@@ -5,7 +5,6 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using QuickLook.ExtensionMethods; using QuickLook.ExtensionMethods;
using QuickLook.Plugin; using QuickLook.Plugin;
using QuickLook.Plugin.InfoPanel;
namespace QuickLook namespace QuickLook
{ {
@@ -18,9 +17,9 @@ namespace QuickLook
LoadPlugins(); LoadPlugins();
} }
internal Type DefaultPlugin { get; } = typeof(PluginInterface); internal IViewer DefaultPlugin { get; } = new Plugin.InfoPanel.PluginInterface();
internal List<Type> LoadedPlugins { get; private set; } = new List<Type>(); internal List<IViewer> LoadedPlugins { get; private set; } = new List<IViewer>();
internal static PluginManager GetInstance() internal static PluginManager GetInstance()
{ {
@@ -38,17 +37,16 @@ namespace QuickLook
var can = false; var can = false;
try try
{ {
can = plugin.CreateInstance<IViewer>().CanHandle(path); can = plugin.CanHandle(path);
} }
catch (Exception) catch (Exception)
{ {
// ignored // ignored
} }
return can; return can;
}) });
?.CreateInstance<IViewer>();
return matched ?? DefaultPlugin.CreateInstance<IViewer>(); return matched ?? DefaultPlugin;
} }
private void LoadPlugins() private void LoadPlugins()
@@ -63,10 +61,10 @@ namespace QuickLook
where !t.IsInterface && !t.IsAbstract where !t.IsInterface && !t.IsAbstract
where typeof(IViewer).IsAssignableFrom(t) where typeof(IViewer).IsAssignableFrom(t)
select t).ToList() select t).ToList()
.ForEach(type => LoadedPlugins.Add(type)); .ForEach(type => LoadedPlugins.Add(type.CreateInstance<IViewer>()));
}); });
LoadedPlugins = LoadedPlugins.OrderByDescending(i => i.CreateInstance<IViewer>().Priority).ToList(); LoadedPlugins = LoadedPlugins.OrderByDescending(i => i.Priority).ToList();
} }
} }
} }

View File

@@ -20,11 +20,6 @@ namespace QuickLook
internal ViewWindowManager() internal ViewWindowManager()
{ {
_viewWindow = new MainWindow(); _viewWindow = new MainWindow();
_viewWindow.Closed += (sender, e) =>
{
if (App.RunningAsViewer)
Application.Current.Shutdown();
};
} }
internal void InvokeRoutine() internal void InvokeRoutine()
@@ -66,10 +61,10 @@ namespace QuickLook
Debug.WriteLine(e.ToString()); Debug.WriteLine(e.ToString());
Debug.WriteLine(e.StackTrace); Debug.WriteLine(e.StackTrace);
if (matchedPlugin.GetType() != PluginManager.GetInstance().DefaultPlugin) if (matchedPlugin != PluginManager.GetInstance().DefaultPlugin)
{ {
matchedPlugin.Dispose(); matchedPlugin.Cleanup();
matchedPlugin = PluginManager.GetInstance().DefaultPlugin.CreateInstance<IViewer>(); matchedPlugin = PluginManager.GetInstance().DefaultPlugin;
BeginShowNewWindow(matchedPlugin, path); BeginShowNewWindow(matchedPlugin, path);
} }
else else