From 529c6c8314f58ad4901ed2a70f93d5c53eeb3e61 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Fri, 6 Jan 2017 22:24:08 +0100 Subject: [PATCH] Disable profile preview on main window focus loss --- Artemis/Artemis/DAL/ProfileProvider.cs | 2 +- Artemis/Artemis/Managers/ProfileManager.cs | 4 +- .../Artemis/Utilities/ActiveWindowHelper.cs | 37 +++++++++++++------ .../ViewModels/Profiles/ProfileViewModel.cs | 4 +- Artemis/Artemis/ViewModels/ShellViewModel.cs | 2 +- 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/Artemis/Artemis/DAL/ProfileProvider.cs b/Artemis/Artemis/DAL/ProfileProvider.cs index 2721ae7d6..547582dc0 100644 --- a/Artemis/Artemis/DAL/ProfileProvider.cs +++ b/Artemis/Artemis/DAL/ProfileProvider.cs @@ -89,7 +89,7 @@ namespace Artemis.DAL } File.WriteAllText(path + $@"\{prof.Slug}.json", json); - Logger.Debug("Saved profile {0}/{1}/{2}", prof.KeyboardSlug, prof.GameName, prof.Name); + Logger.Trace("Saved profile {0}/{1}/{2}", prof.KeyboardSlug, prof.GameName, prof.Name); } } diff --git a/Artemis/Artemis/Managers/ProfileManager.cs b/Artemis/Artemis/Managers/ProfileManager.cs index 65660c4aa..a28397959 100644 --- a/Artemis/Artemis/Managers/ProfileManager.cs +++ b/Artemis/Artemis/Managers/ProfileManager.cs @@ -4,6 +4,7 @@ using System.Timers; using Artemis.DAL; using Artemis.Modules.Abstract; using Artemis.Settings; +using Artemis.Utilities; using Ninject.Extensions.Logging; namespace Artemis.Managers @@ -46,9 +47,10 @@ namespace Artemis.Managers if (string.IsNullOrEmpty(_generalSettings.LastKeyboard) || _deviceManager.ChangingKeyboard) return; + // If Artemis doesn't have focus, don't preview var activePreview = PreviewViewModules.FirstOrDefault( vm => vm.IsActive && vm.UsesProfileEditor && vm.ModuleModel.Settings.IsEnabled); - if (activePreview != null) + if (activePreview != null && ActiveWindowHelper.MainWindowActive) EnsurePreviewActive(activePreview); else EnsurePreviewInactive(); diff --git a/Artemis/Artemis/Utilities/ActiveWindowHelper.cs b/Artemis/Artemis/Utilities/ActiveWindowHelper.cs index d072440b9..2c9430ee9 100644 --- a/Artemis/Artemis/Utilities/ActiveWindowHelper.cs +++ b/Artemis/Artemis/Utilities/ActiveWindowHelper.cs @@ -8,10 +8,13 @@ namespace Artemis.Utilities { #region DLL-Imports - private delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); + private delegate void WinEventDelegate( + IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, + uint dwmsEventTime); [DllImport("user32.dll")] - private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags); + private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, + WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags); [DllImport("user32.dll")] private static extern bool UnhookWinEvent(IntPtr hWinEventHook); @@ -21,7 +24,7 @@ namespace Artemis.Utilities [DllImport("user32.dll")] private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint processId); - + #endregion #region Constants @@ -49,8 +52,9 @@ namespace Artemis.Utilities private static IntPtr _activeWindow; - public static string ActiveWindowProcessName { get; private set; } - public static string ActiveWindowWindowTitle { get; private set; } + public static string ActiveWindowProcessName { get; private set; } = string.Empty; + public static string ActiveWindowWindowTitle { get; private set; } = string.Empty; + public static bool MainWindowActive => ActiveWindowProcessName.Contains("Artemis"); #endregion @@ -91,7 +95,7 @@ namespace Artemis.Utilities { uint pid; GetWindowThreadProcessId(hwnd, out pid); - return System.Diagnostics.Process.GetProcessById((int)pid).ProcessName; + return System.Diagnostics.Process.GetProcessById((int) pid).ProcessName; } catch { @@ -119,22 +123,28 @@ namespace Artemis.Utilities if (_activeWindowEventHook == IntPtr.Zero) { _activeWindowChangedDelegate = ActiveWindowChanged; - _activeWindowEventHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _activeWindowChangedDelegate, 0, 0, WINEVENT_OUTOFCONTEXT); + _activeWindowEventHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, + IntPtr.Zero, _activeWindowChangedDelegate, 0, 0, WINEVENT_OUTOFCONTEXT); } if (_windowTitleEventHook == IntPtr.Zero) { _windowTitleChangedDelegate = WindowTitleChanged; - _windowTitleEventHook = SetWinEventHook(EVENT_OBJECT_NAMECHANGE, EVENT_OBJECT_NAMECHANGE, IntPtr.Zero, _windowTitleChangedDelegate, 0, 0, WINEVENT_OUTOFCONTEXT); + _windowTitleEventHook = SetWinEventHook(EVENT_OBJECT_NAMECHANGE, EVENT_OBJECT_NAMECHANGE, + IntPtr.Zero, _windowTitleChangedDelegate, 0, 0, WINEVENT_OUTOFCONTEXT); } if (_windowMinimizedEventHook == IntPtr.Zero) { _windowMinimizedChangedDelegate = WindowMinimizedChanged; - _windowMinimizedEventHook = SetWinEventHook(EVENT_SYSTEM_MINIMIZEEND, EVENT_SYSTEM_MINIMIZEEND, IntPtr.Zero, _windowMinimizedChangedDelegate, 0, 0, WINEVENT_OUTOFCONTEXT); + _windowMinimizedEventHook = SetWinEventHook(EVENT_SYSTEM_MINIMIZEEND, EVENT_SYSTEM_MINIMIZEEND, + IntPtr.Zero, _windowMinimizedChangedDelegate, 0, 0, WINEVENT_OUTOFCONTEXT); } } - catch { /* catch'em all - I don't want it to crash here */ } + catch + { + /* catch'em all - I don't want it to crash here */ + } } public static void Dispose() @@ -162,9 +172,12 @@ namespace Artemis.Utilities _windowMinimizedEventHook = IntPtr.Zero; } } - catch { /* catch'em all - I don't want it to crash here */ } + catch + { + /* catch'em all - I don't want it to crash here */ + } } #endregion } -} +} \ No newline at end of file diff --git a/Artemis/Artemis/ViewModels/Profiles/ProfileViewModel.cs b/Artemis/Artemis/ViewModels/Profiles/ProfileViewModel.cs index 25dc25538..0c5009373 100644 --- a/Artemis/Artemis/ViewModels/Profiles/ProfileViewModel.cs +++ b/Artemis/Artemis/ViewModels/Profiles/ProfileViewModel.cs @@ -110,7 +110,7 @@ namespace Artemis.ViewModels.Profiles KeyboardPreview = null; // Setup layers for the next frame - if (ModuleModel.IsInitialized) + if (ModuleModel.IsInitialized && ActiveWindowHelper.MainWindowActive) ModuleModel.PreviewLayers = new List(); return; @@ -177,7 +177,7 @@ namespace Artemis.ViewModels.Profiles KeyboardPreview = drawnPreview; // Setup layers for the next frame - if (ModuleModel.IsInitialized) + if (ModuleModel.IsInitialized && ActiveWindowHelper.MainWindowActive) ModuleModel.PreviewLayers = renderLayers; } diff --git a/Artemis/Artemis/ViewModels/ShellViewModel.cs b/Artemis/Artemis/ViewModels/ShellViewModel.cs index ffdf07735..c8b83a39f 100644 --- a/Artemis/Artemis/ViewModels/ShellViewModel.cs +++ b/Artemis/Artemis/ViewModels/ShellViewModel.cs @@ -47,7 +47,7 @@ namespace Artemis.ViewModels // This gets updated automatically but during startup lets quickly preset it Enabled = GeneralSettings.Suspended; } - + protected override void OnViewReady(object view) { base.OnViewReady(view);