1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-31 17:53:32 +00:00

Modules - Added enable override mechanism

Profile modules - Added animated module enable/disable
This commit is contained in:
Robert 2020-08-24 19:23:29 +02:00
parent 73e992bbb7
commit ed479abebc
21 changed files with 442 additions and 122 deletions

View File

@ -1,4 +1,5 @@
using System.Diagnostics; using System;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Artemis.Core.Extensions; using Artemis.Core.Extensions;
@ -39,7 +40,7 @@ namespace Artemis.Core.Plugins.Modules.ActivationRequirements
var processes = ProcessName != null ? Process.GetProcessesByName(ProcessName).Where(p => !p.HasExited) : Process.GetProcesses().Where(p => !p.HasExited); var processes = ProcessName != null ? Process.GetProcessesByName(ProcessName).Where(p => !p.HasExited) : Process.GetProcesses().Where(p => !p.HasExited);
return Location != null return Location != null
? processes.Any(p => Path.GetDirectoryName(p.GetProcessFilename()) == Location) ? processes.Any(p => string.Equals(Path.GetDirectoryName(p.GetProcessFilename()), Location, StringComparison.CurrentCultureIgnoreCase))
: processes.Any(); : processes.Any();
} }
} }

View File

@ -79,6 +79,12 @@ namespace Artemis.Core.Plugins.Modules
/// </summary> /// </summary>
public string DisplayIcon { get; set; } public string DisplayIcon { get; set; }
/// <summary>
/// A path to an image to use as the modules display icon that's shown in the menu.
/// <para>If set, takes precedence over <see cref="DisplayIcon" /></para>
/// </summary>
public string DisplayIconPath { get; set; }
/// <summary> /// <summary>
/// Gets whether this module is activated. A module can only be active while its <see cref="ActivationRequirements" /> /// Gets whether this module is activated. A module can only be active while its <see cref="ActivationRequirements" />
/// are met /// are met
@ -138,14 +144,22 @@ namespace Artemis.Core.Plugins.Modules
public abstract void Render(double deltaTime, ArtemisSurface surface, SKCanvas canvas, SKImageInfo canvasInfo); public abstract void Render(double deltaTime, ArtemisSurface surface, SKCanvas canvas, SKImageInfo canvasInfo);
/// <summary> /// <summary>
/// Called when the <see cref="ActivationRequirements" /> are met /// Called when the <see cref="ActivationRequirements" /> are met or during an override
/// </summary> /// </summary>
public abstract void ModuleActivated(); /// <param name="isOverride">
/// If true, the activation was due to an override. This usually means the module was activated
/// by the profile editor
/// </param>
public abstract void ModuleActivated(bool isOverride);
/// <summary> /// <summary>
/// Called when the <see cref="ActivationRequirements" /> are no longer met /// Called when the <see cref="ActivationRequirements" /> are no longer met or during an override
/// </summary> /// </summary>
public abstract void ModuleDeactivated(); /// <param name="isOverride">
/// If true, the deactivation was due to an override. This usually means the module was deactivated
/// by the profile editor
/// </param>
public abstract void ModuleDeactivated(bool isOverride);
/// <summary> /// <summary>
/// Evaluates the activation requirements following the <see cref="ActivationRequirementMode" /> and returns the result /// Evaluates the activation requirements following the <see cref="ActivationRequirementMode" /> and returns the result
@ -163,22 +177,22 @@ namespace Artemis.Core.Plugins.Modules
return false; return false;
} }
internal virtual void Activate() internal virtual void Activate(bool isOverride)
{ {
if (IsActivated) if (IsActivated)
return; return;
ModuleActivated(); ModuleActivated(isOverride);
IsActivated = true; IsActivated = true;
} }
internal virtual void Deactivate() internal virtual void Deactivate(bool isOverride)
{ {
if (!IsActivated) if (!IsActivated)
return; return;
IsActivated = false; IsActivated = false;
ModuleDeactivated(); ModuleDeactivated(isOverride);
} }
internal void ApplyToEntity() internal void ApplyToEntity()

View File

@ -193,9 +193,9 @@ namespace Artemis.Core.Plugins.Modules
/// </summary> /// </summary>
public bool AnimatingProfileChange { get; private set; } public bool AnimatingProfileChange { get; private set; }
internal override void Deactivate() internal override void Deactivate(bool isOverride)
{ {
base.Deactivate(); base.Deactivate(isOverride);
var profile = ActiveProfile; var profile = ActiveProfile;
ActiveProfile = null; ActiveProfile = null;

View File

@ -175,8 +175,8 @@ namespace Artemis.Core.Services
lock (_modules) lock (_modules)
{ {
modules = _modules.Where(m => m.IsActivated || m.InternalExpandsMainDataModel) modules = _modules.Where(m => m.IsActivated || m.InternalExpandsMainDataModel)
.OrderByDescending(m => m.PriorityCategory) .OrderBy(m => m.PriorityCategory)
.ThenByDescending(m => m.Priority) .ThenBy(m => m.Priority)
.ToList(); .ToList();
} }

View File

@ -0,0 +1,41 @@
using System.Threading.Tasks;
using Artemis.Core.Plugins.Modules;
namespace Artemis.Core.Services.Interfaces
{
/// <summary>
/// A service providing module activation functionality
/// </summary>
public interface IModuleService : IArtemisService
{
/// <summary>
/// Gets whether an override is currently being applied
/// </summary>
bool ApplyingOverride { get; }
/// <summary>
/// Gets the current active module override. If set, all other modules are deactivated and only the
/// <see cref="ActiveModuleOverride" /> is active.
/// </summary>
Module ActiveModuleOverride { get; }
/// <summary>
/// Changes the current <see cref="ActiveModuleOverride" /> and deactivates all other modules
/// </summary>
/// <param name="overrideModule"></param>
Task SetActiveModuleOverride(Module overrideModule);
/// <summary>
/// Evaluates every enabled module's activation requirements and activates/deactivates modules accordingly
/// </summary>
Task UpdateModuleActivation();
/// <summary>
/// Updates the priority and priority category of the given module
/// </summary>
/// <param name="module">The module to update</param>
/// <param name="category">The new priority category of the module</param>
/// <param name="priority">The new priority of the module</param>
void UpdateModulePriority(Module module, ModulePriorityCategory category, int priority);
}
}

View File

@ -7,6 +7,9 @@ using RGB.NET.Core;
namespace Artemis.Core.Services.Interfaces namespace Artemis.Core.Services.Interfaces
{ {
/// <summary>
/// A service providing plugin management
/// </summary>
public interface IPluginService : IArtemisService, IDisposable public interface IPluginService : IArtemisService, IDisposable
{ {
/// <summary> /// <summary>

View File

@ -1,5 +1,9 @@
using System.Diagnostics; using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Timers; using System.Timers;
using Artemis.Core.Events; using Artemis.Core.Events;
using Artemis.Core.Plugins.Modules; using Artemis.Core.Plugins.Modules;
@ -7,6 +11,7 @@ using Artemis.Core.Services.Interfaces;
using Artemis.Core.Services.Storage.Interfaces; using Artemis.Core.Services.Storage.Interfaces;
using Artemis.Storage.Repositories.Interfaces; using Artemis.Storage.Repositories.Interfaces;
using Serilog; using Serilog;
using Timer = System.Timers.Timer;
namespace Artemis.Core.Services namespace Artemis.Core.Services
{ {
@ -26,58 +31,85 @@ namespace Artemis.Core.Services
_pluginService.PluginEnabled += PluginServiceOnPluginEnabled; _pluginService.PluginEnabled += PluginServiceOnPluginEnabled;
var activationUpdateTimer = new Timer(2000); var activationUpdateTimer = new Timer(2000);
activationUpdateTimer.Elapsed += ActivationUpdateTimerOnElapsed;
activationUpdateTimer.Start(); activationUpdateTimer.Start();
activationUpdateTimer.Elapsed += ActivationUpdateTimerOnElapsed;
PopulatePriorities(); PopulatePriorities();
} }
private void ActivationUpdateTimerOnElapsed(object sender, ElapsedEventArgs e) public bool ApplyingOverride { get; private set; }
public Module ActiveModuleOverride { get; private set; }
public async Task SetActiveModuleOverride(Module overrideModule)
{ {
UpdateModuleActivation(); if (ActiveModuleOverride == overrideModule)
return;
try
{
// Not the cleanest way but locks don't work async and I cba with a mutex
while (ApplyingOverride)
await Task.Delay(50);
ApplyingOverride = true;
ActiveModuleOverride = overrideModule;
// If set to null, resume regular activation
if (ActiveModuleOverride == null)
{
await UpdateModuleActivation();
_logger.Information("Cleared active module override");
return;
}
// If a module was provided, activate it and deactivate everything else
var modules = _pluginService.GetPluginsOfType<Module>().ToList();
var deactivationTasks = new List<Task>();
foreach (var module in modules)
{
if (module != ActiveModuleOverride)
deactivationTasks.Add(DeactivateModule(module, true));
}
await Task.WhenAll(deactivationTasks);
if (!ActiveModuleOverride.IsActivated)
await ActivateModule(ActiveModuleOverride, true);
_logger.Information($"Set active module override to {ActiveModuleOverride.DisplayName}");
}
finally
{
ApplyingOverride = false;
}
} }
public void UpdateModuleActivation() public async Task UpdateModuleActivation()
{ {
if (ActiveModuleOverride != null)
return;
var stopwatch = new Stopwatch(); var stopwatch = new Stopwatch();
stopwatch.Start(); stopwatch.Start();
var modules = _pluginService.GetPluginsOfType<Module>().ToList(); var modules = _pluginService.GetPluginsOfType<Module>().ToList();
var tasks = new List<Task>();
foreach (var module in modules) foreach (var module in modules)
{ {
var shouldBeActivated = module.EvaluateActivationRequirements(); var shouldBeActivated = module.EvaluateActivationRequirements();
if (shouldBeActivated && !module.IsActivated) if (shouldBeActivated && !module.IsActivated)
{ tasks.Add(ActivateModule(module, false));
module.Activate();
// If this is a profile module, activate the last active profile after module activation
if (module is ProfileModule profileModule)
_profileService.ActivateLastProfile(profileModule);
}
else if (!shouldBeActivated && module.IsActivated) else if (!shouldBeActivated && module.IsActivated)
module.Deactivate(); tasks.Add(DeactivateModule(module, false));
} }
await Task.WhenAll(tasks);
stopwatch.Stop(); stopwatch.Stop();
if (stopwatch.ElapsedMilliseconds > 100) if (stopwatch.ElapsedMilliseconds > 100)
_logger.Warning("Activation requirements evaluation took too long: {moduleCount} module(s) in {elapsed}", modules.Count, stopwatch.Elapsed); _logger.Warning("Activation requirements evaluation took too long: {moduleCount} module(s) in {elapsed}", modules.Count, stopwatch.Elapsed);
} }
public void PopulatePriorities()
{
var modules = _pluginService.GetPluginsOfType<Module>().ToList();
var moduleEntities = _moduleRepository.GetAll();
foreach (var module in modules)
{
var entity = moduleEntities.FirstOrDefault(e => e.PluginGuid == module.PluginInfo.Guid);
if (entity != null)
{
module.Entity = entity;
module.PriorityCategory = (ModulePriorityCategory) entity.PriorityCategory;
module.Priority = entity.Priority;
}
}
}
public void UpdateModulePriority(Module module, ModulePriorityCategory category, int priority) public void UpdateModulePriority(Module module, ModulePriorityCategory category, int priority)
{ {
var modules = _pluginService.GetPluginsOfType<Module>().Where(m => m.PriorityCategory == category).OrderBy(m => m.Priority).ToList(); var modules = _pluginService.GetPluginsOfType<Module>().Where(m => m.PriorityCategory == category).OrderBy(m => m.Priority).ToList();
@ -105,6 +137,46 @@ namespace Artemis.Core.Services
} }
} }
private async void ActivationUpdateTimerOnElapsed(object sender, ElapsedEventArgs e)
{
await UpdateModuleActivation();
}
private async Task ActivateModule(Module module, bool isOverride)
{
module.Activate(isOverride);
// If this is a profile module, activate the last active profile after module activation
if (module is ProfileModule profileModule)
await _profileService.ActivateLastProfileAnimated(profileModule);
}
private async Task DeactivateModule(Module module, bool isOverride)
{
// If this is a profile module, activate the last active profile after module activation
if (module.IsActivated && module is ProfileModule profileModule)
await profileModule.ChangeActiveProfileAnimated(null, null);
module.Deactivate(isOverride);
}
private void PopulatePriorities()
{
var modules = _pluginService.GetPluginsOfType<Module>().ToList();
var moduleEntities = _moduleRepository.GetAll();
foreach (var module in modules)
{
var entity = moduleEntities.FirstOrDefault(e => e.PluginGuid == module.PluginInfo.Guid);
if (entity != null)
{
module.Entity = entity;
module.PriorityCategory = (ModulePriorityCategory) entity.PriorityCategory;
module.Priority = entity.Priority;
}
}
}
private void PluginServiceOnPluginEnabled(object sender, PluginEventArgs e) private void PluginServiceOnPluginEnabled(object sender, PluginEventArgs e)
{ {
if (e.PluginInfo.Instance is Module module) if (e.PluginInfo.Instance is Module module)
@ -124,8 +196,4 @@ namespace Artemis.Core.Services
UpdateModulePriority(module, module.DefaultPriorityCategory, 1); UpdateModulePriority(module, module.DefaultPriorityCategory, 1);
} }
} }
public interface IModuleService : IArtemisService
{
}
} }

View File

@ -51,6 +51,14 @@ namespace Artemis.Core.Services.Storage.Interfaces
/// <param name="profileModule"></param> /// <param name="profileModule"></param>
void ActivateLastProfile(ProfileModule profileModule); void ActivateLastProfile(ProfileModule profileModule);
/// <summary>
/// Asynchronously activates the last profile of the given profile module using a fade animation
/// </summary>
/// <param name="profileModule"></param>
/// <returns></returns>
Task ActivateLastProfileAnimated(ProfileModule profileModule);
/// <summary> /// <summary>
/// Activates the profile described in the given <see cref="ProfileDescriptor" /> with the currently active surface /// Activates the profile described in the given <see cref="ProfileDescriptor" /> with the currently active surface
/// </summary> /// </summary>

View File

@ -64,6 +64,13 @@ namespace Artemis.Core.Services.Storage
ActivateProfile(activeProfile); ActivateProfile(activeProfile);
} }
public async Task ActivateLastProfileAnimated(ProfileModule profileModule)
{
var activeProfile = GetLastActiveProfile(profileModule);
if (activeProfile != null)
await ActivateProfileAnimated(activeProfile);
}
public Profile ActivateProfile(ProfileDescriptor profileDescriptor) public Profile ActivateProfile(ProfileDescriptor profileDescriptor)
{ {
if (profileDescriptor.ProfileModule.ActiveProfile?.EntityId == profileDescriptor.Id) if (profileDescriptor.ProfileModule.ActiveProfile?.EntityId == profileDescriptor.Id)
@ -94,12 +101,25 @@ namespace Artemis.Core.Services.Storage
var profile = new Profile(profileDescriptor.ProfileModule, profileEntity); var profile = new Profile(profileDescriptor.ProfileModule, profileEntity);
InstantiateProfile(profile); InstantiateProfile(profile);
void ActivatingProfileSurfaceUpdate(object sender, SurfaceConfigurationEventArgs e) => profile.PopulateLeds(e.Surface);
void ActivatingProfilePluginToggle(object sender, PluginEventArgs e) => InstantiateProfile(profile);
// This could happen during activation so subscribe to it
_pluginService.PluginEnabled += ActivatingProfilePluginToggle;
_pluginService.PluginDisabled += ActivatingProfilePluginToggle;
_surfaceService.SurfaceConfigurationUpdated += ActivatingProfileSurfaceUpdate;
await profileDescriptor.ProfileModule.ChangeActiveProfileAnimated(profile, _surfaceService.ActiveSurface); await profileDescriptor.ProfileModule.ChangeActiveProfileAnimated(profile, _surfaceService.ActiveSurface);
SaveActiveProfile(profileDescriptor.ProfileModule); SaveActiveProfile(profileDescriptor.ProfileModule);
_pluginService.PluginEnabled -= ActivatingProfilePluginToggle;
_pluginService.PluginDisabled -= ActivatingProfilePluginToggle;
_surfaceService.SurfaceConfigurationUpdated -= ActivatingProfileSurfaceUpdate;
return profile; return profile;
} }
public void ClearActiveProfile(ProfileModule module) public void ClearActiveProfile(ProfileModule module)
{ {
module.ChangeActiveProfile(null, _surfaceService.ActiveSurface); module.ChangeActiveProfile(null, _surfaceService.ActiveSurface);
@ -234,6 +254,9 @@ namespace Artemis.Core.Services.Storage
private void SaveActiveProfile(ProfileModule module) private void SaveActiveProfile(ProfileModule module)
{ {
if (module.ActiveProfile == null)
return;
var profileEntities = _profileRepository.GetByPluginGuid(module.PluginInfo.Guid); var profileEntities = _profileRepository.GetByPluginGuid(module.PluginInfo.Guid);
foreach (var profileEntity in profileEntities) foreach (var profileEntity in profileEntities)
{ {

View File

@ -79,12 +79,12 @@ namespace Artemis.Core.Utilities
throw new NotImplementedException(); throw new NotImplementedException();
} }
public override void ModuleActivated() public override void ModuleActivated(bool isOverride)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public override void ModuleDeactivated() public override void ModuleDeactivated(bool isOverride)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -0,0 +1,46 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>Artemis.Plugins.Modules.Overlay</AssemblyName>
<RootNamespace>Artemis.Plugins.Modules.Overlay</RootNamespace>
<Platforms>x64</Platforms>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SkiaSharp" Version="1.68.3" />
<PackageReference Include="Stylet" Version="1.3.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Artemis.Core\Artemis.Core.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Update="plugin.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(BuildingInsideVisualStudio)' == 'true'">
<Exec Command="echo Copying resources to plugin output directory&#xD;&#xA;XCOPY &quot;$(ProjectDir)Images&quot; &quot;$(TargetDir)Images&quot; /s /q /i /y&#xD;&#xA;XCOPY &quot;$(ProjectDir)Layouts&quot; &quot;$(TargetDir)Layouts&quot; /s /q /i /y&#xD;&#xA;echo Copying plugin to Artemis plugin directory&#xD;&#xA;XCOPY &quot;$(TargetDir.TrimEnd('\'))&quot; &quot;%25ProgramData%25\Artemis\Plugins\$(ProjectName)&quot; /s /q /i /y&#xD;&#xA;" />
</Target>
</Project>

View File

@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>Artemis.Plugins.Modules.Overlay</AssemblyName>
<RootNamespace>Artemis.Plugins.Modules.Overlay</RootNamespace>
<Platforms>x64</Platforms>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SkiaSharp" Version="1.68.3" />
<PackageReference Include="Stylet" Version="1.3.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Artemis.Core\Artemis.Core.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="plugin.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(BuildingInsideVisualStudio)' == 'true'">
<Exec Command="echo Copying resources to plugin output directory&#xD;&#xA;XCOPY &quot;$(ProjectDir)Images&quot; &quot;$(TargetDir)Images&quot; /s /q /i /y&#xD;&#xA;XCOPY &quot;$(ProjectDir)Layouts&quot; &quot;$(TargetDir)Layouts&quot; /s /q /i /y&#xD;&#xA;echo Copying plugin to Artemis plugin directory&#xD;&#xA;XCOPY &quot;$(TargetDir.TrimEnd('\'))&quot; &quot;%25ProgramData%25\Artemis\Plugins\$(ProjectName)&quot; /s /q /i /y&#xD;&#xA;" />
</Target>
</Project>

View File

@ -0,0 +1,35 @@
using Artemis.Core.Plugins.Modules;
using Artemis.Core.Plugins.Modules.ActivationRequirements;
namespace Artemis.Plugins.Modules.Overlay
{
// The core of your module. Hover over the method names to see a description.
public class OverlayModule : ProfileModule
{
// This is the beginning of your plugin life cycle. Use this instead of a constructor.
public override void EnablePlugin()
{
DisplayName = "Overlay";
DisplayIcon = "ArrangeBringToFront";
DefaultPriorityCategory = ModulePriorityCategory.Overlay;
ActivationRequirements.Add(new ProcessActivationRequirement("taskmgr"));
}
// This is the end of your plugin life cycle.
public override void DisablePlugin()
{
// Make sure to clean up resources where needed (dispose IDisposables etc.)
}
public override void ModuleActivated(bool isOverride)
{
// When this gets called your activation requirements have been met and the module will start displaying
}
public override void ModuleDeactivated(bool isOverride)
{
// When this gets called your activation requirements are no longer met and your module will stop displaying
}
}
}

View File

@ -0,0 +1,7 @@
{
"Guid": "29e3ff97-83a5-44fc-a2dc-04f446b54146",
"Name": "Overlay module",
"Description": "A general profile-enabled overlay module for every-day use",
"Version": "1.0.0.0",
"Main": "Artemis.Plugins.Modules.Overlay.dll"
}

View File

@ -2,6 +2,8 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Artemis.Core.Plugins.Modules; using Artemis.Core.Plugins.Modules;
using Artemis.Core.Services;
using Artemis.Core.Services.Interfaces;
using Artemis.UI.Ninject.Factories; using Artemis.UI.Ninject.Factories;
using Ninject; using Ninject;
using Ninject.Parameters; using Ninject.Parameters;
@ -11,22 +13,41 @@ namespace Artemis.UI.Screens.Module
{ {
public class ModuleRootViewModel : Conductor<Screen>.Collection.OneActive public class ModuleRootViewModel : Conductor<Screen>.Collection.OneActive
{ {
private readonly IModuleService _moduleService;
private readonly IProfileEditorVmFactory _profileEditorVmFactory; private readonly IProfileEditorVmFactory _profileEditorVmFactory;
private readonly IKernel _kernel; private readonly IKernel _kernel;
public ModuleRootViewModel(Core.Plugins.Modules.Module module, IProfileEditorVmFactory profileEditorVmFactory, IKernel kernel) public ModuleRootViewModel(Core.Plugins.Modules.Module module, IModuleService moduleService, IProfileEditorVmFactory profileEditorVmFactory, IKernel kernel)
{ {
DisplayName = module?.DisplayName; DisplayName = module?.DisplayName;
Module = module; Module = module;
_moduleService = moduleService;
_profileEditorVmFactory = profileEditorVmFactory; _profileEditorVmFactory = profileEditorVmFactory;
_kernel = kernel; _kernel = kernel;
Task.Run(AddTabsAsync);
} }
public Core.Plugins.Modules.Module Module { get; } public Core.Plugins.Modules.Module Module { get; }
protected override void OnActivate()
{
Task.Run(async () =>
{
await _moduleService.SetActiveModuleOverride(Module);
await AddTabsAsync();
});
base.OnActivate();
}
protected override void OnDeactivate()
{
Task.Run(async () =>
{
await _moduleService.SetActiveModuleOverride(null);
});
base.OnDeactivate();
}
private async Task AddTabsAsync() private async Task AddTabsAsync()
{ {
// Give the screen a moment to active without freezing the UI thread // Give the screen a moment to active without freezing the UI thread

View File

@ -228,14 +228,14 @@ namespace Artemis.UI.Screens.ProfileEditor
_snackbarMessageQueue.Enqueue("Redid profile update", "UNDO", Undo); _snackbarMessageQueue.Enqueue("Redid profile update", "UNDO", Undo);
} }
protected override void OnInitialActivate() protected override void OnActivate()
{ {
LoadWorkspaceSettings(); LoadWorkspaceSettings();
Module.IsProfileUpdatingDisabled = true; Module.IsProfileUpdatingDisabled = true;
Module.ActiveProfileChanged += ModuleOnActiveProfileChanged; Module.ActiveProfileChanged += ModuleOnActiveProfileChanged;
Execute.PostToUIThread(LoadProfiles); Execute.PostToUIThread(LoadProfiles);
base.OnInitialActivate(); base.OnActivate();
} }
protected override void OnClose() protected override void OnClose()
@ -243,6 +243,8 @@ namespace Artemis.UI.Screens.ProfileEditor
SaveWorkspaceSettings(); SaveWorkspaceSettings();
Module.IsProfileUpdatingDisabled = false; Module.IsProfileUpdatingDisabled = false;
Module.ActiveProfileChanged -= ModuleOnActiveProfileChanged; Module.ActiveProfileChanged -= ModuleOnActiveProfileChanged;
_profileEditorService.ChangeSelectedProfile(null);
base.OnClose(); base.OnClose();
} }

View File

@ -137,7 +137,7 @@ namespace Artemis.UI.Screens
private void SidebarViewModelOnPropertyChanged(object sender, PropertyChangedEventArgs e) private void SidebarViewModelOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{ {
if (e.PropertyName == nameof(SidebarViewModel.SelectedItem)) if (e.PropertyName == nameof(SidebarViewModel.SelectedItem) && ActiveItem != SidebarViewModel.SelectedItem)
{ {
SidebarViewModel.IsSidebarOpen = false; SidebarViewModel.IsSidebarOpen = false;
ActiveItemReady = false; ActiveItemReady = false;

View File

@ -1,8 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Timers; using System.Timers;
using System.Windows.Media.Imaging;
using Artemis.Core.Events; using Artemis.Core.Events;
using Artemis.Core.Services.Interfaces; using Artemis.Core.Services.Interfaces;
using Artemis.UI.Events; using Artemis.UI.Events;
@ -22,15 +24,15 @@ namespace Artemis.UI.Screens.Sidebar
{ {
public class SidebarViewModel : PropertyChangedBase, IHandle<RequestSelectSidebarItemEvent>, IDisposable public class SidebarViewModel : PropertyChangedBase, IHandle<RequestSelectSidebarItemEvent>, IDisposable
{ {
private readonly Timer _activeModulesUpdateTimer;
private readonly IKernel _kernel; private readonly IKernel _kernel;
private readonly IModuleVmFactory _moduleVmFactory; private readonly IModuleVmFactory _moduleVmFactory;
private readonly IPluginService _pluginService; private readonly IPluginService _pluginService;
private string _activeModules;
private bool _isSidebarOpen;
private IScreen _selectedItem;
private BindableCollection<INavigationItem> _sidebarItems; private BindableCollection<INavigationItem> _sidebarItems;
private Dictionary<INavigationItem, Core.Plugins.Modules.Module> _sidebarModules; private Dictionary<INavigationItem, Core.Plugins.Modules.Module> _sidebarModules;
private IScreen _selectedItem;
private bool _isSidebarOpen;
private readonly Timer _activeModulesUpdateTimer;
private string _activeModules;
public SidebarViewModel(IKernel kernel, IEventAggregator eventAggregator, IModuleVmFactory moduleVmFactory, IPluginService pluginService) public SidebarViewModel(IKernel kernel, IEventAggregator eventAggregator, IModuleVmFactory moduleVmFactory, IPluginService pluginService)
{ {
@ -52,15 +54,6 @@ namespace Artemis.UI.Screens.Sidebar
eventAggregator.Subscribe(this); eventAggregator.Subscribe(this);
} }
private void ActiveModulesUpdateTimerOnElapsed(object sender, EventArgs e)
{
if (!IsSidebarOpen)
return;
var activeModules = SidebarModules.Count(m => m.Value.IsActivated);
ActiveModules = activeModules == 1 ? "1 active module" : $"{activeModules} active modules";
}
public BindableCollection<INavigationItem> SidebarItems public BindableCollection<INavigationItem> SidebarItems
{ {
get => _sidebarItems; get => _sidebarItems;
@ -96,6 +89,18 @@ namespace Artemis.UI.Screens.Sidebar
} }
} }
public void Dispose()
{
SelectedItem?.Deactivate();
SelectedItem = null;
_pluginService.PluginEnabled -= PluginServiceOnPluginEnabled;
_pluginService.PluginDisabled -= PluginServiceOnPluginDisabled;
_activeModulesUpdateTimer.Stop();
_activeModulesUpdateTimer.Elapsed -= ActiveModulesUpdateTimerOnElapsed;
}
public void SetupSidebar() public void SetupSidebar()
{ {
SidebarItems.Clear(); SidebarItems.Clear();
@ -120,7 +125,7 @@ namespace Artemis.UI.Screens.Sidebar
} }
// ReSharper disable once UnusedMember.Global - Called by view // ReSharper disable once UnusedMember.Global - Called by view
public async Task SelectItem(WillSelectNavigationItemEventArgs args) public void SelectItem(WillSelectNavigationItemEventArgs args)
{ {
if (args.NavigationItemToSelect == null) if (args.NavigationItemToSelect == null)
{ {
@ -128,7 +133,7 @@ namespace Artemis.UI.Screens.Sidebar
return; return;
} }
await SelectSidebarItem(args.NavigationItemToSelect); SelectSidebarItem(args.NavigationItemToSelect);
} }
public void AddModule(Core.Plugins.Modules.Module module) public void AddModule(Core.Plugins.Modules.Module module)
@ -137,11 +142,19 @@ namespace Artemis.UI.Screens.Sidebar
if (SidebarModules.Any(io => io.Value == module)) if (SidebarModules.Any(io => io.Value == module))
return; return;
// Icon is provided as string to avoid having to reference MaterialDesignThemes object icon;
var parsedIcon = Enum.TryParse<PackIconKind>(module.DisplayIcon, true, out var iconEnum); if (module.DisplayIconPath != null && File.Exists(Path.Combine(module.PluginInfo.Directory.FullName, module.DisplayIconPath)))
if (parsedIcon == false) icon = new BitmapImage(new Uri(Path.Combine(module.PluginInfo.Directory.FullName, module.DisplayIconPath)));
iconEnum = PackIconKind.QuestionMarkCircle; else
var sidebarItem = new FirstLevelNavigationItem {Icon = iconEnum, Label = module.DisplayName}; {
// Icon is provided as string to avoid having to reference MaterialDesignThemes
var parsedIcon = Enum.TryParse<PackIconKind>(module.DisplayIcon, true, out var iconEnum);
if (parsedIcon == false)
iconEnum = PackIconKind.QuestionMarkCircle;
icon = iconEnum;
}
var sidebarItem = new FirstLevelNavigationItem {Icon = icon, Label = module.DisplayName};
SidebarItems.Add(sidebarItem); SidebarItems.Add(sidebarItem);
SidebarModules.Add(sidebarItem, module); SidebarModules.Add(sidebarItem, module);
} }
@ -157,54 +170,48 @@ namespace Artemis.UI.Screens.Sidebar
SidebarModules.Remove(existing.Key); SidebarModules.Remove(existing.Key);
} }
private async Task SelectSidebarItem(INavigationItem sidebarItem) private void ActiveModulesUpdateTimerOnElapsed(object sender, EventArgs e)
{
if (!IsSidebarOpen)
return;
var activeModules = SidebarModules.Count(m => m.Value.IsActivated);
ActiveModules = activeModules == 1 ? "1 active module" : $"{activeModules} active modules";
}
private void SelectSidebarItem(INavigationItem sidebarItem)
{ {
// A module was selected if the dictionary contains the selected item // A module was selected if the dictionary contains the selected item
if (SidebarModules.ContainsKey(sidebarItem)) if (SidebarModules.ContainsKey(sidebarItem))
await ActivateModule(sidebarItem); ActivateModule(sidebarItem);
else if (sidebarItem is FirstLevelNavigationItem navigationItem) else if (sidebarItem is FirstLevelNavigationItem navigationItem)
await ActivateViewModel(navigationItem.Label); ActivateViewModel(navigationItem.Label);
else if (await CloseCurrentItem()) else
SelectedItem = null; SelectedItem = null;
} }
private async Task<bool> CloseCurrentItem() private void ActivateViewModel(string label)
{
if (SelectedItem == null)
return true;
var canClose = await SelectedItem.CanCloseAsync();
if (!canClose)
return false;
SelectedItem.Close();
return true;
}
private async Task ActivateViewModel(string label)
{ {
if (label == "Home") if (label == "Home")
await ActivateViewModel<HomeViewModel>(); ActivateViewModel<HomeViewModel>();
else if (label == "News") else if (label == "News")
await ActivateViewModel<NewsViewModel>(); ActivateViewModel<NewsViewModel>();
else if (label == "Workshop") else if (label == "Workshop")
await ActivateViewModel<WorkshopViewModel>(); ActivateViewModel<WorkshopViewModel>();
else if (label == "Surface Editor") else if (label == "Surface Editor")
await ActivateViewModel<SurfaceEditorViewModel>(); ActivateViewModel<SurfaceEditorViewModel>();
else if (label == "Settings") else if (label == "Settings")
await ActivateViewModel<SettingsViewModel>(); ActivateViewModel<SettingsViewModel>();
} }
private async Task ActivateViewModel<T>() private void ActivateViewModel<T>()
{ {
if (await CloseCurrentItem()) SelectedItem = (IScreen) _kernel.Get<T>();
SelectedItem = (IScreen) _kernel.Get<T>();
} }
private async Task ActivateModule(INavigationItem sidebarItem) private void ActivateModule(INavigationItem sidebarItem)
{ {
if (await CloseCurrentItem()) SelectedItem = SidebarModules.ContainsKey(sidebarItem) ? _moduleVmFactory.Create(SidebarModules[sidebarItem]) : null;
SelectedItem = SidebarModules.ContainsKey(sidebarItem) ? _moduleVmFactory.Create(SidebarModules[sidebarItem]) : null;
} }
#region Event handlers #region Event handlers
@ -223,21 +230,9 @@ namespace Artemis.UI.Screens.Sidebar
public void Handle(RequestSelectSidebarItemEvent message) public void Handle(RequestSelectSidebarItemEvent message)
{ {
Execute.OnUIThread(async () => await ActivateViewModel(message.Label)); ActivateViewModel(message.Label);
} }
#endregion #endregion
public void Dispose()
{
var closeTask = CloseCurrentItem();
closeTask.Wait();
_pluginService.PluginEnabled -= PluginServiceOnPluginEnabled;
_pluginService.PluginDisabled -= PluginServiceOnPluginDisabled;
_activeModulesUpdateTimer.Stop();
_activeModulesUpdateTimer.Elapsed -= ActiveModulesUpdateTimerOnElapsed;
}
} }
} }

View File

@ -10,6 +10,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Artemis.UI", "Artemis.UI\Ar
{3D83760B-0A36-4C8F-978D-7949C3FC862B} = {3D83760B-0A36-4C8F-978D-7949C3FC862B} {3D83760B-0A36-4C8F-978D-7949C3FC862B} = {3D83760B-0A36-4C8F-978D-7949C3FC862B}
{8DC7960F-6DDF-4007-A155-17E124F39374} = {8DC7960F-6DDF-4007-A155-17E124F39374} {8DC7960F-6DDF-4007-A155-17E124F39374} = {8DC7960F-6DDF-4007-A155-17E124F39374}
{DCF7C321-95DC-4507-BB61-A7C5356E58EC} = {DCF7C321-95DC-4507-BB61-A7C5356E58EC} {DCF7C321-95DC-4507-BB61-A7C5356E58EC} = {DCF7C321-95DC-4507-BB61-A7C5356E58EC}
{00318027-7FDB-4C86-AB86-9005A481E330} = {00318027-7FDB-4C86-AB86-9005A481E330}
{E592F239-FAA0-4840-9C85-46E5867D06D5} = {E592F239-FAA0-4840-9C85-46E5867D06D5} {E592F239-FAA0-4840-9C85-46E5867D06D5} = {E592F239-FAA0-4840-9C85-46E5867D06D5}
{36C10640-A31F-4DEE-9F0E-9B9E3F12753D} = {36C10640-A31F-4DEE-9F0E-9B9E3F12753D} {36C10640-A31F-4DEE-9F0E-9B9E3F12753D} = {36C10640-A31F-4DEE-9F0E-9B9E3F12753D}
{62214042-667E-4B29-B64E-1A68CE6FE209} = {62214042-667E-4B29-B64E-1A68CE6FE209} {62214042-667E-4B29-B64E-1A68CE6FE209} = {62214042-667E-4B29-B64E-1A68CE6FE209}
@ -76,6 +77,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Artemis.Plugins.LayerEffect
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Artemis.Plugins.Devices.Debug", "Plugins\Artemis.Plugins.Devices.Debug\Artemis.Plugins.Devices.Debug.csproj", "{3D83760B-0A36-4C8F-978D-7949C3FC862B}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Artemis.Plugins.Devices.Debug", "Plugins\Artemis.Plugins.Devices.Debug\Artemis.Plugins.Devices.Debug.csproj", "{3D83760B-0A36-4C8F-978D-7949C3FC862B}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Artemis.Plugins.Modules.Overlay", "Artemis.Plugins.Modules.Overlay\Artemis.Plugins.Modules.Overlay.csproj", "{00318027-7FDB-4C86-AB86-9005A481E330}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -260,6 +263,12 @@ Global
{3D83760B-0A36-4C8F-978D-7949C3FC862B}.Release|Any CPU.Build.0 = Release|Any CPU {3D83760B-0A36-4C8F-978D-7949C3FC862B}.Release|Any CPU.Build.0 = Release|Any CPU
{3D83760B-0A36-4C8F-978D-7949C3FC862B}.Release|x64.ActiveCfg = Release|Any CPU {3D83760B-0A36-4C8F-978D-7949C3FC862B}.Release|x64.ActiveCfg = Release|Any CPU
{3D83760B-0A36-4C8F-978D-7949C3FC862B}.Release|x64.Build.0 = Release|Any CPU {3D83760B-0A36-4C8F-978D-7949C3FC862B}.Release|x64.Build.0 = Release|Any CPU
{00318027-7FDB-4C86-AB86-9005A481E330}.Debug|Any CPU.ActiveCfg = Debug|x64
{00318027-7FDB-4C86-AB86-9005A481E330}.Debug|x64.ActiveCfg = Debug|x64
{00318027-7FDB-4C86-AB86-9005A481E330}.Debug|x64.Build.0 = Debug|x64
{00318027-7FDB-4C86-AB86-9005A481E330}.Release|Any CPU.ActiveCfg = Release|x64
{00318027-7FDB-4C86-AB86-9005A481E330}.Release|x64.ActiveCfg = Release|x64
{00318027-7FDB-4C86-AB86-9005A481E330}.Release|x64.Build.0 = Release|x64
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -287,6 +296,7 @@ Global
{2C1477DC-7A5C-4B65-85DB-1F16A18FB2EC} = {E830A02B-A7E5-4A6B-943F-76B0A542630C} {2C1477DC-7A5C-4B65-85DB-1F16A18FB2EC} = {E830A02B-A7E5-4A6B-943F-76B0A542630C}
{62214042-667E-4B29-B64E-1A68CE6FE209} = {2C1477DC-7A5C-4B65-85DB-1F16A18FB2EC} {62214042-667E-4B29-B64E-1A68CE6FE209} = {2C1477DC-7A5C-4B65-85DB-1F16A18FB2EC}
{3D83760B-0A36-4C8F-978D-7949C3FC862B} = {88792A7E-F037-4280-81D3-B131508EF1D8} {3D83760B-0A36-4C8F-978D-7949C3FC862B} = {88792A7E-F037-4280-81D3-B131508EF1D8}
{00318027-7FDB-4C86-AB86-9005A481E330} = {B258A061-FA19-4835-8DC4-E9C3AE3664A0}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C203080A-4473-4CC2-844B-F552EA43D66A} SolutionGuid = {C203080A-4473-4CC2-844B-F552EA43D66A}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using Artemis.Core.Plugins; using Artemis.Core.Plugins;
using Artemis.Core.Plugins.DeviceProviders; using Artemis.Core.Plugins.DeviceProviders;
@ -8,6 +9,7 @@ using Artemis.Plugins.Devices.Debug.Settings;
using Artemis.Plugins.Devices.Debug.ViewModels; using Artemis.Plugins.Devices.Debug.ViewModels;
using RGB.NET.Core; using RGB.NET.Core;
using RGB.NET.Devices.Debug; using RGB.NET.Devices.Debug;
using Serilog;
namespace Artemis.Plugins.Devices.Debug namespace Artemis.Plugins.Devices.Debug
{ {
@ -16,10 +18,12 @@ namespace Artemis.Plugins.Devices.Debug
{ {
private readonly IRgbService _rgbService; private readonly IRgbService _rgbService;
private readonly PluginSettings _settings; private readonly PluginSettings _settings;
private readonly ILogger _logger;
public DebugDeviceProvider(IRgbService rgbService, PluginSettings settings) : base(RGB.NET.Devices.Debug.DebugDeviceProvider.Instance) public DebugDeviceProvider(IRgbService rgbService, PluginSettings settings, ILogger logger) : base(RGB.NET.Devices.Debug.DebugDeviceProvider.Instance)
{ {
_settings = settings; _settings = settings;
_logger = logger;
_rgbService = rgbService; _rgbService = rgbService;
} }
@ -35,7 +39,14 @@ namespace Artemis.Plugins.Devices.Debug
foreach (var deviceDefinition in definitions.Value) foreach (var deviceDefinition in definitions.Value)
RGB.NET.Devices.Debug.DebugDeviceProvider.Instance.AddFakeDeviceDefinition(deviceDefinition.Layout, deviceDefinition.ImageLayout); RGB.NET.Devices.Debug.DebugDeviceProvider.Instance.AddFakeDeviceDefinition(deviceDefinition.Layout, deviceDefinition.ImageLayout);
_rgbService.AddDeviceProvider(RgbDeviceProvider); try
{
_rgbService.AddDeviceProvider(RgbDeviceProvider);
}
catch (Exception e)
{
_logger.Warning(e, "Debug device provided failed to initialize, check paths");
}
} }
public override void DisablePlugin() public override void DisablePlugin()

View File

@ -28,11 +28,11 @@ namespace Artemis.Plugins.Modules.General
{ {
} }
public override void ModuleActivated() public override void ModuleActivated(bool isOverride)
{ {
} }
public override void ModuleDeactivated() public override void ModuleDeactivated(bool isOverride)
{ {
} }