diff --git a/Artemis/Artemis/DeviceProviders/Corsair/CorsairRGB.cs b/Artemis/Artemis/DeviceProviders/Corsair/CorsairRGB.cs index ba375a6d7..6cef35822 100644 --- a/Artemis/Artemis/DeviceProviders/Corsair/CorsairRGB.cs +++ b/Artemis/Artemis/DeviceProviders/Corsair/CorsairRGB.cs @@ -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 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); } /// diff --git a/Artemis/Artemis/DeviceProviders/KeyboardProvider.cs b/Artemis/Artemis/DeviceProviders/KeyboardProvider.cs index 7498c6279..ab0984904 100644 --- a/Artemis/Artemis/DeviceProviders/KeyboardProvider.cs +++ b/Artemis/Artemis/DeviceProviders/KeyboardProvider.cs @@ -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 CanEnableAsync(ProgressDialogController dialog) + /// + /// Runs CanEnable asynchronously multiple times until successful, cancelled or max tries reached + /// + /// + /// + public Task 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) + /// + /// Runs CanEnable asynchronously + /// + /// + /// + public Task EnableAsync(ProgressDialogController dialog) { return Task.Run(() => Enable()); } diff --git a/Artemis/Artemis/Managers/DeviceManager.cs b/Artemis/Artemis/Managers/DeviceManager.cs index a805481a9..b3eba9ba9 100644 --- a/Artemis/Artemis/Managers/DeviceManager.cs +++ b/Artemis/Artemis/Managers/DeviceManager.cs @@ -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 diff --git a/Artemis/Artemis/Managers/MainManager.cs b/Artemis/Artemis/Managers/MainManager.cs index 4a25ea4af..3d612aa7b 100644 --- a/Artemis/Artemis/Managers/MainManager.cs +++ b/Artemis/Artemis/Managers/MainManager.cs @@ -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(); } /// diff --git a/Artemis/Artemis/Services/MetroDialogService.cs b/Artemis/Artemis/Services/MetroDialogService.cs index 7bdb4ab62..04465596e 100644 --- a/Artemis/Artemis/Services/MetroDialogService.cs +++ b/Artemis/Artemis/Services/MetroDialogService.cs @@ -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 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)); } } } \ No newline at end of file diff --git a/Artemis/Artemis/ViewModels/ShellViewModel.cs b/Artemis/Artemis/ViewModels/ShellViewModel.cs index b04887ff6..e7a6f4fba 100644 --- a/Artemis/Artemis/ViewModels/ShellViewModel.cs +++ b/Artemis/Artemis/ViewModels/ShellViewModel.cs @@ -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.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; diff --git a/Artemis/Artemis/ViewModels/SystemTrayViewModel.cs b/Artemis/Artemis/ViewModels/SystemTrayViewModel.cs index dd5cc716d..1d0c43bdb 100644 --- a/Artemis/Artemis/ViewModels/SystemTrayViewModel.cs +++ b/Artemis/Artemis/ViewModels/SystemTrayViewModel.cs @@ -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() {