// Copyright (c) Arlo Godfrey. All Rights Reserved.
// Licensed under the GNU Lesser General Public License, Version 3.0 with additional terms.
// See the LICENSE, LICENSE.LESSER and LICENSE.ADDITIONAL files in the project root for more information.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using StrixMusic.Sdk.AdapterModels;
using StrixMusic.Sdk.AppModels;
using StrixMusic.Sdk.CoreModels;
using StrixMusic.Sdk.Plugins.Model;
namespace StrixMusic.Sdk.PluginModels;
///
/// Wraps an instance of with the provided plugins.
///
public class ImagePluginWrapper : IImage, IPluginWrapper
{
private readonly IImage _image;
///
/// Initializes a new instance of the class.
///
/// The instance to wrap around and apply plugins to.
/// The plugins that are applied to items returned from or emitted by this collection.
internal ImagePluginWrapper(IImage image, params SdkModelPlugin[] plugins)
{
foreach (var item in plugins)
ActivePlugins.Import(item);
ActivePlugins = GlobalModelPluginConnector.Create(ActivePlugins);
_image = ActivePlugins.Image.Execute(image);
AttachEvents(_image);
}
///
public SdkModelPlugin ActivePlugins { get; } = new(PluginModelWrapperInfo.Metadata);
private void AttachEvents(IImage image)
{
image.SourcesChanged += OnSourcesChanged;
}
private void DetachEvents(IImage image)
{
image.SourcesChanged -= OnSourcesChanged;
}
private void OnSourcesChanged(object sender, EventArgs e) => SourcesChanged?.Invoke(sender, e);
///
public event EventHandler? SourcesChanged;
///
public Uri Uri => _image.Uri;
///
public double Height => _image.Height;
///
public double Width => _image.Width;
///
public bool Equals(ICoreImage other) => _image.Equals(other);
///
public IReadOnlyList Sources => _image.Sources;
///
public ValueTask DisposeAsync()
{
DetachEvents(_image);
return _image.DisposeAsync();
}
}