|
@@ -1,47 +1,79 @@
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
+using System.Reflection;
|
|
|
using PixiEditor.AvaloniaUI.Views.Dialogs;
|
|
|
using PixiEditor.Extensions;
|
|
|
+using PixiEditor.Extensions.Helpers;
|
|
|
using PixiEditor.Extensions.Windowing;
|
|
|
|
|
|
namespace PixiEditor.AvaloniaUI.Models.AppExtensions.Services;
|
|
|
|
|
|
public class WindowProvider : IWindowProvider
|
|
|
{
|
|
|
- private Dictionary<string, Func<IPopupWindow>> _openHandlers = new();
|
|
|
+ private readonly Dictionary<string, Type> registeredWindows = new();
|
|
|
+ private ExtensionLoader extensionLoader;
|
|
|
+ private IServiceProvider services;
|
|
|
|
|
|
- public WindowProvider RegisterHandler(string id, Func<IPopupWindow> handler)
|
|
|
+ internal WindowProvider(ExtensionLoader loader, IServiceProvider services)
|
|
|
{
|
|
|
- if (_openHandlers.ContainsKey(id))
|
|
|
+ this.extensionLoader = loader;
|
|
|
+ this.services = services;
|
|
|
+ }
|
|
|
+
|
|
|
+ public WindowProvider RegisterWindow<T>() where T : IPopupWindow
|
|
|
+ {
|
|
|
+ Type type = typeof(T);
|
|
|
+ string? id = extensionLoader.GetTypeId(type);
|
|
|
+ if (id is null)
|
|
|
{
|
|
|
- _openHandlers[id] = handler;
|
|
|
- throw new ArgumentException($"Window with id {id} already has a handler");
|
|
|
+ throw new ArgumentException($"Window {type} doesn't seem to be part of an extension.");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!registeredWindows.TryAdd(id, type))
|
|
|
+ {
|
|
|
+ throw new ArgumentException($"Window with id {id} is already registered.");
|
|
|
}
|
|
|
|
|
|
- _openHandlers.Add(id, handler);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
public PopupWindow CreatePopupWindow(string title, object body)
|
|
|
{
|
|
|
- return new PopupWindow(new PixiEditorPopup() { Title = title, Content = body });
|
|
|
+ return new PopupWindow(new PixiEditorPopup { Title = title, Content = body });
|
|
|
+ }
|
|
|
+
|
|
|
+ public PopupWindow GetWindow(WindowType type)
|
|
|
+ {
|
|
|
+ string id = type.GetDescription();
|
|
|
+ return GetWindow($"PixiEditor.{id}");
|
|
|
}
|
|
|
|
|
|
- public PopupWindow OpenWindow(WindowType type)
|
|
|
+ public PopupWindow GetWindow(string windowId)
|
|
|
{
|
|
|
- return OpenWindow($"PixiEditor.{type}");
|
|
|
+ if (registeredWindows.TryGetValue(windowId, out Type? handler))
|
|
|
+ {
|
|
|
+ object[] args = TryGetConstructorArgs(handler);
|
|
|
+ return new PopupWindow((IPopupWindow)Activator.CreateInstance(handler, args));
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new ArgumentException($"Window with id {windowId} does not exist");
|
|
|
}
|
|
|
|
|
|
- public PopupWindow OpenWindow(string windowId)
|
|
|
+ private object?[] TryGetConstructorArgs(Type handler)
|
|
|
{
|
|
|
- var handler = _openHandlers.FirstOrDefault(x => x.Key == windowId);
|
|
|
- if (handler.Key != null)
|
|
|
+ ConstructorInfo[] constructors = handler.GetConstructors();
|
|
|
+ if (constructors.Length == 0)
|
|
|
{
|
|
|
- return new PopupWindow(handler.Value());
|
|
|
+ return Array.Empty<object>();
|
|
|
}
|
|
|
- else
|
|
|
+
|
|
|
+ ConstructorInfo constructor = constructors[0];
|
|
|
+ ParameterInfo[] parameters = constructor.GetParameters();
|
|
|
+ if (parameters.Length == 0)
|
|
|
{
|
|
|
- throw new ArgumentException($"Window with id {windowId} does not exist");
|
|
|
+ return Array.Empty<object>();
|
|
|
}
|
|
|
+
|
|
|
+ return parameters.Select(x => services.GetService(x.ParameterType)).ToArray();
|
|
|
}
|
|
|
}
|