x13pixels / remedybg-issues

Public repository for tracking issues (bugs and features) for the RemedyBG debugger
85 stars 0 forks source link

Command line option to focus current window #307

Closed akbyrd closed 4 months ago

akbyrd commented 5 months ago

I've been excited to try remedybg for a long time and I'm finally getting around to it. One thing that I'd like to be able to do that I don't currently see is to focus an existing window. Specifically, I'd like to open a session (if it doesn't exist), focus the window (if it already exists), and start debugging. I can do the first and last part, but not the middle.

remedybg open-session session.rdbg && `
remedybg start-debugging

I think I'm looking for something like

remedybg open-session session.rdbg && `
remedybg start-debugging && `
remedybg focus-window

or maybe an optional parameter to open-session to focus the window

remedybg open-session session.rdbg 1 && `
remedybg start-debugging

For context, I'm using a command in vscode to get an experience similar to hitting F5 in vs or vscode. I'd like the first time I run the command to open remedy and subsequent runs to focus the existing window. Apologies if I've overlooked an existing solution in the readme.

akbyrd commented 5 months ago

I just noticed that remedybg start-debugging will focus the window if it's minimized, but will not focus the window if it is not minimized.

Windows has some rules for when focusing an application programmatically will work. SetForegroundWindow for example: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow#remarks

akbyrd commented 5 months ago

A hacky workaround for anyone else that stumbles across this:

PowerShell script:

$processName = $args[0]
$processes = [System.Diagnostics.Process]::GetProcessesByName($processName)
if ($processes.Count -gt 0)
{
    $import = '[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);'
    $obj = Add-Type -MemberDefinition $import -Name "Func" -PassThru
    $obj::SetForegroundWindow($processes[0].MainWindowHandle)
}

Usage:

remedybg open-session session.rdbg && `
remedybg start-debugging && `
focus-process-main-window.ps1 remedybg

I assume this works because the focus request comes from a foreground application instead of a background application. I have no idea if there's a reliable Win32 way for a background process to reliably cause itself to come into focus.

x13pixels commented 4 months ago

0.4.0.4: In case it helps, I went ahead and added a remedybg.exe bring-debugger-to-foreground with the caveat that you mentioned above: Windows does not always honor the request.