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

Cleaned up CorsairRGB keyboard provider, added dialog on window open if a keyboard is still being enabled.

This commit is contained in:
SpoinkyNL 2016-06-14 11:56:27 +02:00
parent b546d98db0
commit 6627a48d93
7 changed files with 96 additions and 75 deletions

View File

@ -1,6 +1,4 @@
using System.Drawing; using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using Artemis.Properties; using Artemis.Properties;
using Artemis.Utilities; using Artemis.Utilities;
@ -8,7 +6,6 @@ using CUE.NET;
using CUE.NET.Brushes; using CUE.NET.Brushes;
using CUE.NET.Devices.Generic.Enums; using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Keyboard; using CUE.NET.Devices.Keyboard;
using MahApps.Metro.Controls.Dialogs;
using Ninject.Extensions.Logging; using Ninject.Extensions.Logging;
using Point = System.Drawing.Point; using Point = System.Drawing.Point;
@ -31,48 +28,9 @@ namespace Artemis.DeviceProviders.Corsair
public ILogger Logger { get; set; } public ILogger Logger { get; set; }
public sealed override Task<bool> CanEnableAsync(ProgressDialogController dialog)
{
return Task.Run(() =>
{
// This will skip the check-loop if the SDK is initialized
if (CueSDK.IsInitialized)
return CueSDK.IsSDKAvailable(CorsairDeviceType.Keyboard);
for (var tries = 0; tries < 9; tries++)
{
// Stop trying if cancelled by user
if (dialog != null && dialog.IsCanceled)
{
dialog.SetIndeterminate();
return false;
}
dialog?.SetProgress(0.1*(tries + 1));
if (CueSDK.IsSDKAvailable(CorsairDeviceType.Keyboard))
{
dialog?.SetIndeterminate();
return true;
}
Thread.Sleep(2000);
}
dialog?.SetIndeterminate();
return false;
});
}
public override bool CanEnable() public override bool CanEnable()
{ {
// This will skip the check-loop if the SDK is initialized return CueSDK.IsSDKAvailable(CorsairDeviceType.Keyboard);
if (CueSDK.IsInitialized)
return CueSDK.IsSDKAvailable(CorsairDeviceType.Keyboard);
for (var tries = 0; tries < 9; tries++)
{
if (CueSDK.IsSDKAvailable(CorsairDeviceType.Keyboard))
return true;
Thread.Sleep(2000);
}
return false;
} }
/// <summary> /// <summary>

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using MahApps.Metro.Controls.Dialogs; using MahApps.Metro.Controls.Dialogs;
@ -40,12 +41,48 @@ namespace Artemis.DeviceProviders
public Rect KeyboardRectangle(int scale) => new Rect(new Size(Width*scale, Height*scale)); public Rect KeyboardRectangle(int scale) => new Rect(new Size(Width*scale, Height*scale));
public virtual Task<bool> CanEnableAsync(ProgressDialogController dialog) /// <summary>
/// Runs CanEnable asynchronously multiple times until successful, cancelled or max tries reached
/// </summary>
/// <param name="dialog"></param>
/// <returns></returns>
public Task<bool> CanEnableAsync(ProgressDialogController dialog)
{ {
return Task.Run(() => CanEnable()); return Task.Run(() =>
{
for (var tries = 1; tries <= 10; tries++)
{
// Dialog interaction
if (dialog != null)
{
// Stop if cancelled by user
if (dialog.IsCanceled)
{
dialog.SetIndeterminate();
return false;
}
// Updated progress to indicate how much tries are left
dialog.SetProgress(0.1*tries);
}
if (CanEnable())
{
dialog?.SetIndeterminate();
return true;
}
Thread.Sleep(2000);
}
dialog?.SetIndeterminate();
return false;
});
} }
public virtual Task EnableAsync(ProgressDialogController dialog) /// <summary>
/// Runs CanEnable asynchronously
/// </summary>
/// <param name="dialog"></param>
/// <returns></returns>
public Task EnableAsync(ProgressDialogController dialog)
{ {
return Task.Run(() => Enable()); return Task.Run(() => Enable());
} }

View File

@ -99,7 +99,6 @@ namespace Artemis.Managers
ReleaseActiveKeyboard(); ReleaseActiveKeyboard();
} }
// TODO: LoopManager shouldn't be running at this point
_logger.Debug("Enabling keyboard: {0}", keyboardProvider.Name); _logger.Debug("Enabling keyboard: {0}", keyboardProvider.Name);
// Create a dialog to let the user know Artemis hasn't frozen // Create a dialog to let the user know Artemis hasn't frozen

View File

@ -78,12 +78,12 @@ namespace Artemis.Managers
{ {
_logger.Debug("Shutting down MainManager"); _logger.Debug("Shutting down MainManager");
_processTimer.Stop(); _processTimer?.Stop();
_processTimer.Dispose(); _processTimer?.Dispose();
LoopManager.Stop(); LoopManager?.Stop();
EffectManager.ActiveEffect.Dispose(); EffectManager?.ActiveEffect?.Dispose();
GameStateWebServer.Stop(); GameStateWebServer?.Stop();
PipeServer.Stop(); PipeServer?.Stop();
} }
/// <summary> /// <summary>

View File

@ -102,26 +102,16 @@ namespace Artemis.Services
}; };
if (initialDir != null) if (initialDir != null)
{
ofd.InitialDirectory = initialDir; ofd.InitialDirectory = initialDir;
}
if (Application.Current.MainWindow != null) res = Application.Current.MainWindow != null
{ ? ofd.ShowDialog(Application.Current.MainWindow)
res = ofd.ShowDialog(Application.Current.MainWindow); : ofd.ShowDialog();
}
else
{
res = ofd.ShowDialog();
}
if (res == true) if (res == true)
{
lPath = ofd.FileName; lPath = ofd.FileName;
}
else else
{
res = false; res = false;
}
}); });
path = lPath; path = lPath;
@ -132,10 +122,9 @@ namespace Artemis.Services
public Task<ProgressDialogController> ShowProgressDialog(string title, string message, bool isCancelable = false, public Task<ProgressDialogController> ShowProgressDialog(string title, string message, bool isCancelable = false,
MetroDialogSettings settings = null) MetroDialogSettings settings = null)
{ {
return GetActiveWindow() == null var activeWindow = GetActiveWindow();
? null return activeWindow?.Dispatcher.Invoke(
: GetActiveWindow().Dispatcher.Invoke(() => () => activeWindow.ShowProgressAsync(title, message, isCancelable, settings));
GetActiveWindow()?.ShowProgressAsync(title, message, isCancelable, settings));
} }
} }
} }

View File

@ -1,4 +1,7 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Artemis.Managers;
using Artemis.Services;
using Artemis.ViewModels.Abstract; using Artemis.ViewModels.Abstract;
using Artemis.ViewModels.Flyouts; using Artemis.ViewModels.Flyouts;
using Caliburn.Micro; using Caliburn.Micro;
@ -8,11 +11,16 @@ namespace Artemis.ViewModels
{ {
public sealed class ShellViewModel : Conductor<IScreen>.Collection.OneActive public sealed class ShellViewModel : Conductor<IScreen>.Collection.OneActive
{ {
private readonly DeviceManager _deviceManager;
private readonly MetroDialogService _dialogService;
private readonly BaseViewModel[] _viewModels; private readonly BaseViewModel[] _viewModels;
public ShellViewModel(IKernel kernel, IEventAggregator events, BaseViewModel[] viewModels) public ShellViewModel(IKernel kernel, IEventAggregator events, BaseViewModel[] viewModels,
DeviceManager deviceManager, MetroDialogService dialogService)
{ {
_viewModels = viewModels; _viewModels = viewModels;
_deviceManager = deviceManager;
_dialogService = dialogService;
events.Subscribe(this); events.Subscribe(this);
@ -34,7 +42,7 @@ namespace Artemis.ViewModels
ActiveItem = _viewModels.FirstOrDefault(); ActiveItem = _viewModels.FirstOrDefault();
} }
public void Settings() public void Settings()
{ {
Flyouts.First().IsOpen = !Flyouts.First().IsOpen; Flyouts.First().IsOpen = !Flyouts.First().IsOpen;

View File

@ -1,4 +1,6 @@
using System.Windows; using System;
using System.Threading.Tasks;
using System.Windows;
using Artemis.Events; using Artemis.Events;
using Artemis.Managers; using Artemis.Managers;
using Artemis.Services; using Artemis.Services;
@ -41,7 +43,8 @@ namespace Artemis.ViewModels
public bool CanShowWindow => !_shellViewModel.IsActive; public bool CanShowWindow => !_shellViewModel.IsActive;
public bool CanHideWindow => _shellViewModel.IsActive; public bool CanHideWindow => _shellViewModel.IsActive && !MainManager.DeviceManager.ChangingKeyboard;
public bool CanToggleEnabled => !MainManager.DeviceManager.ChangingKeyboard;
public bool Enabled public bool Enabled
{ {
@ -114,9 +117,36 @@ namespace Artemis.ViewModels
return; return;
_checkedForUpdate = true; _checkedForUpdate = true;
ShowKeyboardDialog();
Updater.CheckForUpdate(DialogService); Updater.CheckForUpdate(DialogService);
} }
private async void ShowKeyboardDialog()
{
NotifyOfPropertyChange(() => CanHideWindow);
NotifyOfPropertyChange(() => CanToggleEnabled);
var dialog = await DialogService.ShowProgressDialog("Enabling keyboard",
"Artemis is still busy trying to enable your last used keyboard. " +
"Please what while the progress completes");
dialog.SetIndeterminate();
while (MainManager.DeviceManager.ChangingKeyboard)
await Task.Delay(200);
NotifyOfPropertyChange(() => CanHideWindow);
NotifyOfPropertyChange(() => CanToggleEnabled);
try
{
await dialog.CloseAsync();
}
catch (InvalidOperationException)
{
// Occurs when window is closed again, can't find a proper check for this
}
}
public void HideWindow() public void HideWindow()
{ {