Closed akbyrd closed 4 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
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.
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.
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.
I think I'm looking for something like
or maybe an optional parameter to
open-session
to focus the windowFor 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.