diff --git a/src/Artemis.Core/Services/Interfaces/IPluginService.cs b/src/Artemis.Core/Services/Interfaces/IPluginService.cs index 50f65773a..0c8deca44 100644 --- a/src/Artemis.Core/Services/Interfaces/IPluginService.cs +++ b/src/Artemis.Core/Services/Interfaces/IPluginService.cs @@ -70,10 +70,10 @@ namespace Artemis.Core.Services List GetAllPluginInfo(); /// - /// Finds all enabled instances of + /// Finds all enabled instances of /// /// Either or a plugin type implementing - /// Returns a list of plugin instances of + /// Returns a list of plugin instances of List GetPluginsOfType() where T : Plugin; /// @@ -90,6 +90,12 @@ namespace Artemis.Core.Services /// Plugin GetPluginByDevice(IRGBDevice device); + /// + /// Returns the plugin info of the current call stack + /// + /// If the current call stack contains a plugin, the plugin. Otherwise null + Plugin GetCallingPlugin(); + #region Events /// diff --git a/src/Artemis.Core/Services/PluginService.cs b/src/Artemis.Core/Services/PluginService.cs index 8f25194a9..fa328b720 100644 --- a/src/Artemis.Core/Services/PluginService.cs +++ b/src/Artemis.Core/Services/PluginService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Linq; @@ -360,6 +361,22 @@ namespace Artemis.Core.Services return GetPluginsOfType().First(d => d.RgbDeviceProvider.Devices != null && d.RgbDeviceProvider.Devices.Contains(rgbDevice)); } + public Plugin GetCallingPlugin() + { + StackTrace stackTrace = new StackTrace(); // get call stack + StackFrame[] stackFrames = stackTrace.GetFrames(); // get method calls (frames) + + foreach (StackFrame stackFrame in stackFrames) + { + Assembly assembly = stackFrame.GetMethod().DeclaringType.Assembly; + Plugin plugin = GetPluginByAssembly(assembly); + if (plugin != null) + return plugin; + } + + return null; + } + public void Dispose() { UnloadPlugins(); diff --git a/src/Artemis.UI.Shared/Services/Dialog/DialogService.cs b/src/Artemis.UI.Shared/Services/Dialog/DialogService.cs index e4d8d4aad..bf9403174 100644 --- a/src/Artemis.UI.Shared/Services/Dialog/DialogService.cs +++ b/src/Artemis.UI.Shared/Services/Dialog/DialogService.cs @@ -1,8 +1,12 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using System.Windows; +using Artemis.Core; +using Artemis.Core.Services; using Artemis.UI.Shared.Screens.Dialogs; using Artemis.UI.Shared.Screens.Exceptions; using MaterialDesignThemes.Wpf; @@ -18,17 +22,42 @@ namespace Artemis.UI.Shared.Services private readonly IKernel _kernel; private readonly IViewManager _viewManager; private readonly IWindowManager _windowManager; + private readonly IPluginService _pluginService; - public DialogService(IKernel kernel, IViewManager viewManager, IWindowManager windowManager) + public DialogService(IKernel kernel, IViewManager viewManager, IWindowManager windowManager, IPluginService pluginService) { _kernel = kernel; _viewManager = viewManager; _windowManager = windowManager; + _pluginService = pluginService; + } + + private async Task ShowDialog(IParameter[] parameters) where T : DialogViewModelBase + { + if (parameters == null) throw new ArgumentNullException(nameof(parameters)); + return await ShowDialog("RootDialog", _kernel.Get(parameters)); + } + + private async Task ShowDialog(string identifier, DialogViewModelBase viewModel) + { + Task result = null; + await Execute.OnUIThreadAsync(() => + { + UIElement view = _viewManager.CreateViewForModel(viewModel); + _viewManager.BindViewToModel(view, viewModel); + + if (identifier == null) + result = DialogHost.Show(view, viewModel.OnDialogOpened, viewModel.OnDialogClosed); + else + result = DialogHost.Show(view, identifier, viewModel.OnDialogOpened, viewModel.OnDialogClosed); + }); + + return await result; } public async Task ShowConfirmDialog(string header, string text, string confirmText = "Confirm", string cancelText = "Cancel") { - IParameter[] arguments = new IParameter[] + IParameter[] arguments = { new ConstructorArgument("header", header), new ConstructorArgument("text", text), @@ -41,7 +70,7 @@ namespace Artemis.UI.Shared.Services public async Task ShowConfirmDialogAt(string identifier, string header, string text, string confirmText = "Confirm", string cancelText = "Cancel") { - IParameter[] arguments = new IParameter[] + IParameter[] arguments = { new ConstructorArgument("header", header), new ConstructorArgument("text", text), @@ -66,12 +95,6 @@ namespace Artemis.UI.Shared.Services return ShowDialog(paramsArray); } - public async Task ShowDialog(IParameter[] parameters) where T : DialogViewModelBase - { - if (parameters == null) throw new ArgumentNullException(nameof(parameters)); - return await ShowDialog("RootDialog", _kernel.Get(parameters)); - } - public async Task ShowDialogAt(string identifier) where T : DialogViewModelBase { return await ShowDialog(identifier, _kernel.Get()); @@ -86,9 +109,13 @@ namespace Artemis.UI.Shared.Services return await ShowDialogAt(identifier, paramsArray); } - public async Task ShowDialogAt(string identifier, IParameter[] parameters) where T : DialogViewModelBase + private async Task ShowDialogAt(string identifier, IParameter[] parameters) where T : DialogViewModelBase { + Plugin callingPlugin = _pluginService.GetCallingPlugin(); if (parameters == null) throw new ArgumentNullException(nameof(parameters)); + + if (callingPlugin != null) + return await ShowDialog(identifier, callingPlugin.PluginInfo.Kernel.Get(parameters)); return await ShowDialog(identifier, _kernel.Get(parameters)); } @@ -96,22 +123,5 @@ namespace Artemis.UI.Shared.Services { _windowManager.ShowDialog(new ExceptionViewModel(message, exception)); } - - private async Task ShowDialog(string identifier, DialogViewModelBase viewModel) - { - Task result = null; - await Execute.OnUIThreadAsync(() => - { - UIElement view = _viewManager.CreateViewForModel(viewModel); - _viewManager.BindViewToModel(view, viewModel); - - if (identifier == null) - result = DialogHost.Show(view, viewModel.OnDialogOpened, viewModel.OnDialogClosed); - else - result = DialogHost.Show(view, identifier, viewModel.OnDialogOpened, viewModel.OnDialogClosed); - }); - - return await result; - } } } \ No newline at end of file diff --git a/src/Artemis.UI.Shared/Services/Interfaces/IDialogService.cs b/src/Artemis.UI.Shared/Services/Interfaces/IDialogService.cs index 2687833c7..9a5cd0241 100644 --- a/src/Artemis.UI.Shared/Services/Interfaces/IDialogService.cs +++ b/src/Artemis.UI.Shared/Services/Interfaces/IDialogService.cs @@ -51,15 +51,6 @@ namespace Artemis.UI.Shared.Services /// A task resolving to the result of the dialog's Task ShowDialog(Dictionary parameters) where T : DialogViewModelBase; - /// - /// Shows a dialog by initializing a view model implementing using an array of - /// Ninject , requires you to reference Ninject - /// - /// The type of the view model - /// An array of Ninject to pass to the view model during activation - /// A task resolving to the result of the dialog's - Task ShowDialog(IParameter[] parameters) where T : DialogViewModelBase; - /// /// Shows a dialog by initializing a view model implementing /// @@ -84,19 +75,6 @@ namespace Artemis.UI.Shared.Services /// A task resolving to the result of the dialog's Task ShowDialogAt(string identifier, Dictionary parameters) where T : DialogViewModelBase; - /// - /// Shows a dialog by initializing a view model implementing using an array of - /// Ninject , requires you to reference Ninject - /// - /// The type of the view model - /// - /// The identifier of the to use eg. - /// <materialDesign:DialogHost Identifier="MyDialogHost"> - /// - /// An array of Ninject to pass to the view model during activation - /// A task resolving to the result of the dialog's - Task ShowDialogAt(string identifier, IParameter[] parameters) where T : DialogViewModelBase; - /// /// Shows a dialog displaying the provided message and exception. Does not handle, log or throw the exception. ///