Skip to content

Window focus during debug in VSCode #1175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ryanfog opened this issue Jan 26, 2018 · 4 comments
Open

Window focus during debug in VSCode #1175

ryanfog opened this issue Jan 26, 2018 · 4 comments

Comments

@ryanfog
Copy link

ryanfog commented Jan 26, 2018

•VSCode Version: Code 1.19.3 (7c4205b, 2018-01-25T10:36:34.867Z)
•OS Version: Windows_NT x64 10.0.16299
•Extensions:
Author (truncated)
Version
PowerShell ms- 1.5.1
team ms- 1.122.0

Issue Description

I use the code snippet below a lot to display a windows explorer window for the selection of a input file by a end user. When debugging in VSCode the dialog box that is called is not brought to the foreground. In order to see the dialog, you need to minimize VSCode answer the dialog which returns focus back to VSCode.

Function Get-FileName($StartDirectory){
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.initialDirectory = $StartDirectory
$openFileDialog.filter = "All files (.)| ."
$openFileDialog.ShowDialog() | Out-Null
$openFileDialog.filename
}

Attached Logs

PSR of issue.zip

@TylerLeonhardt
Copy link
Member

Thanks for the report @ryanfog!

@rkeithhill this has happened with other windows, right? Like the credential window I think... or the Azure login window.

@rkeithhill
Copy link
Contributor

rkeithhill commented Jan 29, 2018

Yup. In this case, the issue is specific to Windows since Windows Forms doesn't run on Linux/macOS. At first I thought you could solve this by getting VSCODE's main window handle:

$handle = (Get-Process -ID $env:VSCODE_ID).MainWindowHandle

and wrapping that in a type that implement IWin32Window e.g.:

$owner = [System.Windows.Forms.NativeWindow]::FromHandle($handle)

and then pass that owner into the ShowDialog() call. But it doesn't work. The Window handle we get seems valid but when I pass it to NativeWindow::FromHandle(), I always get gack $null. :-(

@TylerLeonhardt
Copy link
Member

@rkeithhill it looks like this also happens in the default PowerShell console that VSCode provides. Perhaps we should open a bug on them (if one's not already opened) and see if they have any ideas?

@rkeithhill
Copy link
Contributor

rkeithhill commented Jan 29, 2018

Perhaps we open an enhancement request on VSCode i.e. provide a way that an extension OR a debugged end-user program can bring UI to the top, over VSCode. Often UI generated by other programs (debug extension, user programs) show up behind VSCode and it confuses users (causes bug submissions).

BTW there may be other ways to do this for certain types of UI where you can get the Window handle and use the SetForegroundWindow() Win32 API to bring that window to the top. This unfortunately doesn't work so well with modal UI like ShowDialog() (unless you want to spin up a job to do this).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
@TylerLeonhardt @rkeithhill @ryanfog and others