Skip to content

Terminal receives focus on drag n drop#20003

Merged
carlos-zamora merged 2 commits intomicrosoft:mainfrom
etbala:main
Mar 27, 2026
Merged

Terminal receives focus on drag n drop#20003
carlos-zamora merged 2 commits intomicrosoft:mainfrom
etbala:main

Conversation

@etbala
Copy link
Copy Markdown
Contributor

@etbala etbala commented Mar 23, 2026

Summary

Closes #19934

Updates terminal to gain foreground focus when a user drags and drops a file into the terminal.

Changes

Updates the DragDropHandler to call SetForegroundWindow.

Validation

  • Built and deployed locally
  • Manually verified terminal gains focus when dragging and dropping


if (const auto hwnd = reinterpret_cast<HWND>(OwningHwnd()))
{
SetForegroundWindow(hwnd);
Copy link
Copy Markdown
Member

@lhecker lhecker Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind also adding this here?

void Window::_HandleDrop(const WPARAM wParam) const
{
const auto drop = reinterpret_cast<HDROP>(wParam);
Clipboard::Instance().PasteDrop(drop);
DragFinish(drop);
}

Something like this:

if (const auto hwnd = GetWindowHandle())
{
    SetForegroundWindow(hwnd);
}

This way, even the old conhost (the classic console window) would get this improvement. You can test conhost by setting Conhost\Host.EXE as your startup project.

...I'm not sure if it'll work though, given that it relies on WM_DROPFILES and not OLE.

Thank you for working on this!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added! Looks like it was just as easy as calling SetForegroundWindow again.

@etbala
Copy link
Copy Markdown
Contributor Author

etbala commented Mar 23, 2026

@microsoft-github-policy-service agree

Copy link
Copy Markdown
Member

@carlos-zamora carlos-zamora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Tested on Windows Terminal and Conhost

Thank you so much for doing this! 🙏

@carlos-zamora
Copy link
Copy Markdown
Member

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@carlos-zamora carlos-zamora enabled auto-merge (squash) March 27, 2026 21:15
@DHowett
Copy link
Copy Markdown
Member

DHowett commented Mar 27, 2026

I'm a little hesitant to put TermControl--which is supposed to know only XAML and nothing of the window outside of it--in charge of this... but I'll probably let it slide for now and fix it when I move drag/drop up into TerminalApp.

@DHowett
Copy link
Copy Markdown
Member

DHowett commented Mar 27, 2026

(thanks for the fix!)

@carlos-zamora carlos-zamora merged commit a1a43a4 into microsoft:main Mar 27, 2026
14 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Terminal should automatically receive focus after dragging and dropping files

4 participants