From 554ed6098a0c1e24c0ed251e164ca44ea9f3482f Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 27 Jun 2025 02:20:26 +0200 Subject: [PATCH] Make ctrl-F16 track one window (instead of executable) --- .../Services/Implementation/ProcessMonitor.cs | 47 +++++++++++++++++-- .../Implementation/ThumbnailManager.cs | 38 ++++++++++++++- .../Services/Interface/IProcessMonitor.cs | 2 + 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/src/Eve-O-Preview/Services/Implementation/ProcessMonitor.cs b/src/Eve-O-Preview/Services/Implementation/ProcessMonitor.cs index d369145..697c86d 100644 --- a/src/Eve-O-Preview/Services/Implementation/ProcessMonitor.cs +++ b/src/Eve-O-Preview/Services/Implementation/ProcessMonitor.cs @@ -17,21 +17,29 @@ namespace EveOPreview.Services.Implementation private readonly IDictionary _processCache; private IProcessInfo _currentProcessInfo; private readonly IThumbnailConfiguration _configuration; + private readonly HashSet _trackedPids; // Change to HashSet for multiple PIDs #endregion public ProcessMonitor(IThumbnailConfiguration configuration) { this._processCache = new Dictionary(512); this._configuration = configuration; + this._trackedPids = new HashSet(); // Initialize empty set // This field cannot be initialized properly in constructor // At the moment this code is executed the main application window is not yet initialized this._currentProcessInfo = new ProcessInfo(IntPtr.Zero, ""); } - private bool IsMonitoredProcess(string processName) + private bool IsMonitoredProcess(string processName, int processId) { - // This is a possible extension point + // If we're tracking this specific PID, include it + if (_trackedPids.Contains(processId)) + { + return true; + } + + // Also include any processes that match executable tracking return _configuration.IsExecutableToPreview(processName); } @@ -80,8 +88,9 @@ namespace EveOPreview.Services.Implementation foreach (Process process in Process.GetProcesses()) { string processName = process.ProcessName; + int processId = process.Id; - if (!this.IsMonitoredProcess(processName)) + if (!this.IsMonitoredProcess(processName, processId)) { continue; } @@ -130,5 +139,37 @@ namespace EveOPreview.Services.Implementation this._processCache.Remove(index); } } + + // Update to handle multiple PIDs + public void SetTrackedPid(int pid) + { + if (pid == 0) // Special case: 0 means stop tracking all + { + this._trackedPids.Clear(); + return; + } + + if (this._trackedPids.Contains(pid)) + { + this._trackedPids.Remove(pid); // Toggle off if already tracking + } + else + { + this._trackedPids.Add(pid); // Toggle on if not tracking + } + } + + // Update to return currently tracked PID (for compatibility) + public int GetTrackedPid() + { + // Return the first tracked PID, or 0 if none + return this._trackedPids.FirstOrDefault(); + } + + // Add method to get all tracked PIDs if needed + public IReadOnlyCollection GetTrackedPids() + { + return this._trackedPids; + } } } diff --git a/src/Eve-O-Preview/Services/Implementation/ThumbnailManager.cs b/src/Eve-O-Preview/Services/Implementation/ThumbnailManager.cs index e2a9508..fa5836f 100644 --- a/src/Eve-O-Preview/Services/Implementation/ThumbnailManager.cs +++ b/src/Eve-O-Preview/Services/Implementation/ThumbnailManager.cs @@ -132,6 +132,43 @@ namespace EveOPreview.Services }; var registered = this._toggleTrackingHotkey.Register(); System.Diagnostics.Debug.WriteLine($"Hotkey registration result: {registered}"); + + // Setup single process tracking hotkey (Ctrl+F16) + var toggleSingleProcessHotkey = new HotkeyHandler(mainHandle, Keys.Control | Keys.F16); + toggleSingleProcessHotkey.Pressed += (object s, HandledEventArgs e) => + { + var foregroundHandle = this._windowManager.GetForegroundWindowHandle(); + if (foregroundHandle != IntPtr.Zero) + { + uint processId; + GetWindowThreadProcessId(foregroundHandle, out processId); + var process = Process.GetProcessById((int)processId); + + if (process != null) + { + System.Diagnostics.Debug.WriteLine($"Found process: {process.ProcessName} (PID: {process.Id})"); + + // Get the current tracked PID from ProcessMonitor + var currentTrackedPid = this._processMonitor.GetTrackedPid(); + + if (currentTrackedPid == process.Id) + { + // Stop tracking this process + this._processMonitor.SetTrackedPid(0); + System.Diagnostics.Debug.WriteLine($"Stopped tracking process {process.Id}"); + } + else + { + // Start tracking this process + this._processMonitor.SetTrackedPid(process.Id); + System.Diagnostics.Debug.WriteLine($"Started tracking process {process.Id}"); + } + e.Handled = true; + } + } + }; + registered = toggleSingleProcessHotkey.Register(); + System.Diagnostics.Debug.WriteLine($"Single process hotkey registration result: {registered}"); } public IThumbnailView GetClientByTitle(string title) @@ -996,7 +1033,6 @@ namespace EveOPreview.Services && (width > ThumbnailManager.WINDOW_SIZE_THRESHOLD) && (height > ThumbnailManager.WINDOW_SIZE_THRESHOLD); } - [DllImport("user32.dll")] private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam); diff --git a/src/Eve-O-Preview/Services/Interface/IProcessMonitor.cs b/src/Eve-O-Preview/Services/Interface/IProcessMonitor.cs index 22029a4..865bd0f 100644 --- a/src/Eve-O-Preview/Services/Interface/IProcessMonitor.cs +++ b/src/Eve-O-Preview/Services/Interface/IProcessMonitor.cs @@ -7,5 +7,7 @@ namespace EveOPreview.Services IProcessInfo GetMainProcess(); ICollection GetAllProcesses(); void GetUpdatedProcesses(out ICollection addedProcesses, out ICollection updatedProcesses, out ICollection removedProcesses); + int GetTrackedPid(); + void SetTrackedPid(int pid); } } \ No newline at end of file