// Copyright © 2017-2025 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 .
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
{
///
/// Load and insert the GlassLayer control to the videoControlContainer.
///
private partial void LoadAndInsertGlassLayer()
{
// Replace XAML with C# dynamic construction
// Implementation without dependencies of QuickLook.exe assembly
//
//
//
//
//
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"]);
//