Skip to content

Commit

Permalink
Decouple some plugins from QuickLook
Browse files Browse the repository at this point in the history
  • Loading branch information
emako committed Dec 27, 2024
1 parent bdbac0b commit f50eb64
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 7 deletions.
53 changes: 53 additions & 0 deletions QuickLook.Plugin/QuickLook.Plugin.PluginInstaller/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright © 2024 QL-Win Contributors
//
// 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 <http://www.gnu.org/licenses/>.

using QuickLook.Common.Helpers;
using System;
using System.IO;
using System.Linq;
using System.Reflection;

namespace QuickLook.Plugin.PluginInstaller;

internal static class App
{
/// <summary>
/// <see cref="QuickLook.App.UserPluginPath"/>
/// </summary>
public static string UserPluginPath
{
get
{
// Just in case
static string Fallback() => Path.Combine(SettingHelper.LocalDataPath, "QuickLook.Plugin\\");

try
{
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies()
.First(a => a.GetName(false).Name == "QuickLook");
var appType = loadedAssemblies?.GetType("QuickLook.App");
var fieldInfo = appType?.GetField(nameof(UserPluginPath), BindingFlags.Public | BindingFlags.Static);

return (fieldInfo?.GetValue(null) as string) ?? Fallback();
}
catch
{
return Fallback();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@
<Name>QuickLook.Common</Name>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\..\QuickLook\QuickLook.csproj">
<!-- Keep QuickLook reference here for App testing -->
<!--<ProjectReference Include="..\..\QuickLook\QuickLook.csproj">
<Project>{8B4A9CE5-67B5-4A94-81CB-3771F688FDEB}</Project>
<Name>QuickLook</Name>
<Private>False</Private>
</ProjectReference>
</ProjectReference>-->
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@
<Name>QuickLook.Common</Name>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\..\QuickLook\QuickLook.csproj">
<!-- Keep QuickLook reference here for glassLayer:GlassLayer testing -->
<!--<ProjectReference Include="..\..\QuickLook\QuickLook.csproj">
<Project>{8B4A9CE5-67B5-4A94-81CB-3771F688FDEB}</Project>
<Name>QuickLook</Name>
<Private>False</Private>
</ProjectReference>
</ProjectReference>-->
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright © 2024 QL-Win Contributors
//
// 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 <http://www.gnu.org/licenses/>.

using QuickLook.Common.Helpers;
using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace QuickLook.Plugin.VideoViewer;

public partial class ViewerPanel : UserControl, IDisposable, INotifyPropertyChanged
{
/// <summary>
/// Load and insert the GlassLayer control to the videoControlContainer.
/// </summary>
private partial void LoadAndInsertGlassLayer()
{
// Replace XAML with C# dynamic construction
// Implementation without dependencies of QuickLook.exe assembly

//<glassLayer:GlassLayer xmlns:glassLayer='clr-namespace:QuickLook.Controls.GlassLayer;assembly=QuickLook'
// ColorOverlayVisibility='{Binding ElementName=viewerPanel, Path=HasVideo, Converter={StaticResource BooleanToVisibilityConverter}}'
// GlassVisibility='{Binding ElementName=viewerPanel, Path=HasVideo, Converter={StaticResource BooleanToVisibilityConverter}}'
// OverlayColor='{DynamicResource CaptionBackground}'>
// <glassLayer:GlassLayer.Style>
// <Style TargetType='glassLayer:GlassLayer'>
// <Setter Property='BlurredElement' Value='{Binding ElementName=mediaElement}' />
// <Style.Triggers>
// <DataTrigger Binding='{Binding ElementName=viewerPanel, Path=HasVideo}' Value='True'>
// <Setter Property='BlurredElement' Value='{Binding ElementName=mediaElement}' />
// </DataTrigger>
// </Style.Triggers>
// </Style>
// </glassLayer:GlassLayer.Style>
//</glassLayer:GlassLayer>

try
{
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies()
.First(a => a.GetName(false).Name == "QuickLook");

var glassLayerType = loadedAssemblies?.GetType("QuickLook.Controls.GlassLayer.GlassLayer")
?? throw new TypeLoadException
(
"""
The type 'QuickLook.Controls.GlassLayer.GlassLayer' could not be found in the loaded assembly 'QuickLook.exe'.
Make sure the assembly is correctly loaded and the type exists.
"""
);

// glassLayer:GlassLayer
var glassLayerInstance = Activator.CreateInstance(glassLayerType);

// Prepare the `SetBinding` method
var setBindingMethod = glassLayerType.GetMethod("SetBinding", BindingFlags.Public | BindingFlags.Instance, null, [typeof(DependencyProperty), typeof(BindingBase)], null);

// Prepare the `SetResourceReference` method
var setResourceReferenceMethod = glassLayerType.GetMethod("SetResourceReference", BindingFlags.Public | BindingFlags.Instance);

// ColorOverlayVisibility="{Binding ElementName=viewerPanel, Path=HasVideo, Converter={StaticResource BooleanToVisibilityConverter}}"
var colorOverlayVisibilityProperty = glassLayerType.GetField("ColorOverlayVisibilityProperty", BindingFlags.Public | BindingFlags.Static)?.GetValue(null)
?? throw new InvalidOperationException("ColorOverlayVisibilityProperty not found.");

Binding colorOverlayVisibilityBinding = new(nameof(HasVideo))
{
ElementName = nameof(viewerPanel),
Converter = (BooleanToVisibilityConverter)Resources[nameof(BooleanToVisibilityConverter)]
};

setBindingMethod.Invoke(glassLayerInstance, [colorOverlayVisibilityProperty, colorOverlayVisibilityBinding]);

// GlassVisibility="{Binding ElementName=viewerPanel, Path=HasVideo, Converter={StaticResource BooleanToVisibilityConverter}}"
var glassVisibilityProperty = glassLayerType.GetField("GlassVisibilityProperty", BindingFlags.Public | BindingFlags.Static)?.GetValue(null)
?? throw new InvalidOperationException("GlassVisibilityProperty not found.");

Binding glassVisibilityBinding = new(nameof(HasVideo))
{
ElementName = nameof(viewerPanel),
Converter = (BooleanToVisibilityConverter)Resources[nameof(BooleanToVisibilityConverter)]
};

setBindingMethod.Invoke(glassLayerInstance, [glassVisibilityProperty, glassVisibilityBinding]);

// OverlayColor="{DynamicResource CaptionBackground}"
var overlayColorProperty = glassLayerType.GetField("OverlayColorProperty", BindingFlags.Public | BindingFlags.Static)?.GetValue(null)
?? throw new InvalidOperationException("OverlayColorProperty not found.");

setResourceReferenceMethod.Invoke(glassLayerInstance, [overlayColorProperty, "CaptionBackground"]);

// <Style TargetType="glassLayer:GlassLayer">
var styleConstructor = typeof(Style).GetConstructor([typeof(Type)]);
var style = (Style)styleConstructor.Invoke([glassLayerType]);

// <Setter Property="BlurredElement" Value="{Binding ElementName=mediaElement}" />
var blurredElementProperty = glassLayerType.GetField("BlurredElementProperty", BindingFlags.Public | BindingFlags.Static)?.GetValue(null)
?? throw new InvalidOperationException("BlurredElementProperty not found.");

var blurredElementSetter = new Setter((DependencyProperty)blurredElementProperty, new Binding()
{
ElementName = "mediaElement"
});
style.Setters.Add(blurredElementSetter);

// <DataTrigger Binding="{Binding ElementName=viewerPanel, Path=HasVideo}" Value="True">
var dataTrigger = new DataTrigger()
{
Binding = new Binding("HasVideo")
{
ElementName = "viewerPanel"
},
Value = true,
};

var dataTriggerSetter = new Setter((DependencyProperty)blurredElementProperty, new Binding()
{
ElementName = "mediaElement"
});
dataTrigger.Setters.Add(dataTriggerSetter);

style.Triggers.Add(dataTrigger);

// <glassLayer:GlassLayer.Style>
glassLayerType.GetProperty(nameof(Style)).SetValue(glassLayerInstance, style);

// Insert `glassLayer:GlassLayer` to `videoControlContainer` in XAML
videoControlContainer.Children.Insert(0, (UIElement)glassLayerInstance);
}
catch (Exception e)
{
ProcessHelper.WriteLog(e.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:glassLayer="clr-namespace:QuickLook.Controls.GlassLayer;assembly=QuickLook"
xmlns:local="clr-namespace:QuickLook.Plugin.VideoViewer"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mediakit="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=QuickLook.WPFMediaKit"
Expand Down Expand Up @@ -158,7 +157,9 @@
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Grid.Resources>
<glassLayer:GlassLayer ColorOverlayVisibility="{Binding ElementName=viewerPanel, Path=HasVideo, Converter={StaticResource BooleanToVisibilityConverter}}"
<!-- Keep glassLayer:GlassLayer here for the dependency testing -->
<!-- xmlns:glassLayer="clr-namespace:QuickLook.Controls.GlassLayer;assembly=QuickLook" -->
<!--<glassLayer:GlassLayer ColorOverlayVisibility="{Binding ElementName=viewerPanel, Path=HasVideo, Converter={StaticResource BooleanToVisibilityConverter}}"
GlassVisibility="{Binding ElementName=viewerPanel, Path=HasVideo, Converter={StaticResource BooleanToVisibilityConverter}}"
OverlayColor="{DynamicResource CaptionBackground}">
<glassLayer:GlassLayer.Style>
Expand All @@ -171,7 +172,7 @@
</Style.Triggers>
</Style>
</glassLayer:GlassLayer.Style>
</glassLayer:GlassLayer>
</glassLayer:GlassLayer>-->
<DockPanel Margin="8,0,8,0">
<Button x:Name="buttonPlayPause"
DockPanel.Dock="Left"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public partial class ViewerPanel : UserControl, IDisposable, INotifyPropertyChan
public ViewerPanel(ContextObject context)
{
InitializeComponent();
LoadAndInsertGlassLayer();

// apply global theme
Resources.MergedDictionaries[0].MergedDictionaries.Clear();
Expand Down Expand Up @@ -100,6 +101,8 @@ public ViewerPanel(ContextObject context)
PreviewMouseWheel += (sender, e) => ChangeVolume((double)e.Delta / 120 * 0.04);
}

private partial void LoadAndInsertGlassLayer();

public bool HasVideo
{
get => _hasVideo;
Expand Down

0 comments on commit f50eb64

Please sign in to comment.