Make ctrl-F16 track one window (instead of executable)

This commit is contained in:
2025-06-27 02:20:26 +02:00
parent 5373ba8cfe
commit 554ed6098a
3 changed files with 83 additions and 4 deletions

View File

@@ -17,21 +17,29 @@ namespace EveOPreview.Services.Implementation
private readonly IDictionary<IntPtr, string> _processCache;
private IProcessInfo _currentProcessInfo;
private readonly IThumbnailConfiguration _configuration;
private readonly HashSet<int> _trackedPids; // Change to HashSet for multiple PIDs
#endregion
public ProcessMonitor(IThumbnailConfiguration configuration)
{
this._processCache = new Dictionary<IntPtr, string>(512);
this._configuration = configuration;
this._trackedPids = new HashSet<int>(); // 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<int> GetTrackedPids()
{
return this._trackedPids;
}
}
}

View File

@@ -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);

View File

@@ -7,5 +7,7 @@ namespace EveOPreview.Services
IProcessInfo GetMainProcess();
ICollection<IProcessInfo> GetAllProcesses();
void GetUpdatedProcesses(out ICollection<IProcessInfo> addedProcesses, out ICollection<IProcessInfo> updatedProcesses, out ICollection<IProcessInfo> removedProcesses);
int GetTrackedPid();
void SetTrackedPid(int pid);
}
}