1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +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.Threading;
using System.Threading.Tasks;
using System.Windows;
using Artemis.Properties;
using Artemis.Utilities;
@ -8,7 +6,6 @@ using CUE.NET;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Keyboard;
using MahApps.Metro.Controls.Dialogs;
using Ninject.Extensions.Logging;
using Point = System.Drawing.Point;
@ -31,48 +28,9 @@ namespace Artemis.DeviceProviders.Corsair
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()
{
// 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++)
{
if (CueSDK.IsSDKAvailable(CorsairDeviceType.Keyboard))
return true;
Thread.Sleep(2000);
}
return false;
return CueSDK.IsSDKAvailable(CorsairDeviceType.Keyboard);
}
/// <summary>

View File

@ -1,5 +1,6 @@
using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
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 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());
}

View File

@ -99,7 +99,6 @@ namespace Artemis.Managers
ReleaseActiveKeyboard();
}
// TODO: LoopManager shouldn't be running at this point
_logger.Debug("Enabling keyboard: {0}", keyboardProvider.Name);
// 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");
_processTimer.Stop();
_processTimer.Dispose();
LoopManager.Stop();
EffectManager.ActiveEffect.Dispose();
GameStateWebServer.Stop();
PipeServer.Stop();
_processTimer?.Stop();
_processTimer?.Dispose();
LoopManager?.Stop();
EffectManager?.ActiveEffect?.Dispose();
GameStateWebServer?.Stop();
PipeServer?.Stop();
}
/// <summary>

View File

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

View File

@ -1,4 +1,7 @@
using System.Linq;
using System.Threading.Tasks;
using Artemis.Managers;
using Artemis.Services;
using Artemis.ViewModels.Abstract;
using Artemis.ViewModels.Flyouts;
using Caliburn.Micro;
@ -8,11 +11,16 @@ namespace Artemis.ViewModels
{
public sealed class ShellViewModel : Conductor<IScreen>.Collection.OneActive
{
private readonly DeviceManager _deviceManager;
private readonly MetroDialogService _dialogService;
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;
_deviceManager = deviceManager;
_dialogService = dialogService;
events.Subscribe(this);
@ -34,7 +42,7 @@ namespace Artemis.ViewModels
ActiveItem = _viewModels.FirstOrDefault();
}
public void Settings()
{
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.Managers;
using Artemis.Services;
@ -41,7 +43,8 @@ namespace Artemis.ViewModels
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
{
@ -114,9 +117,36 @@ namespace Artemis.ViewModels
return;
_checkedForUpdate = true;
ShowKeyboardDialog();
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()
{