Skip to content
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

Sometimes the Terminal launches behind all other windows / without foreground / isn't activated #15479

Closed
marbaa opened this issue May 30, 2023 · 42 comments
Labels
Area-Windowing Window frame, quake mode, tearout Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. Priority-1 A description (P1) Product-Terminal The new Windows Terminal. Resolution-Fix-Available It's available in an Insiders build or a release

Comments

@marbaa
Copy link

marbaa commented May 30, 2023

Windows Terminal version

1.18.1421.0

Windows build number

10.0.22621.1702

Other Software

No response

Steps to reproduce

Just leave the default value in Settings -> Launch parameters -> Launch Mode -> Default.

Terminal window is launched but has no focus. Doesn't matter which profile is set to default, if Powershell, or WSL, or whatever else. Often happens that if I'm in other application and I run Terminal, from pinned taskbar icon for example, it is not visible at all. Doesn't matter if I run it with keyboard shortcut Win + 1-9 or with middle mouse button. It starts behind the current application which I'm in.

Expected Behavior

Terminal window in foreground with focus.

Actual Behavior

Terminal window in foreground without focus, and sometimes even hidden behind other application.

@marbaa marbaa added Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels May 30, 2023
@carlos-zamora carlos-zamora added Product-Terminal The new Windows Terminal. Area-Windowing Window frame, quake mode, tearout and removed Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels May 31, 2023
@carlos-zamora carlos-zamora added this to the Terminal v1.19 milestone May 31, 2023
@carlos-zamora carlos-zamora added the Priority-1 A description (P1) label May 31, 2023
@carlos-zamora
Copy link
Member

@DHowett had a pretty simple repro with:
wt -w -1
Run that from the Run dialog

@marbaa
Copy link
Author

marbaa commented Jun 19, 2023

Found situation when this nofocus issue occurs. It is because of Quake mode.

Steps to repro:
1. Lanuch WT by whatever way it is possible (from Start menu, from taskbar, .exe, ...)
2. Open Quake-mode with WIN + ~~ ~~3. Launch some ssh session there (I have always running ```az network tunnel``` session in Quake-mode window)~~ ~~4. Hide Quake-mode with WIN +
5. Launch new WT instance from step 1.

This way, new lanuched WT doesn't have focus. If you disconnect the sessions in Quake-mode, and hide Quake-mode, then the newly WT instances will have focus correctly.

edit 20.6.
Above is not true. Next day, it happened without using Quake-mode.

@zadjii-msft
Copy link
Member

Uhg guys I can't get this to repro. Certainly not consistently. I had it happen like, once by

  • shift clicking on the taskbar (with a window already open),
  • waiting what seemed like a minute
  • activating other windows. Minimizing one.
  • then eventually the new window appeared, beneath everything.

I reckon that this regressed in the #5000 changes. Especially somewhere around the way that a new window gets summoned in the existing process.

I just can't for the life of me get a consistent repro. It probably depends on

  • If there's a terminal window already running
  • How you spawn the new terminal window
  • what you do before the new terminal window launches

@zadjii-msft
Copy link
Member

Something blind we could try:

diff --git a/src/cascadia/WindowsTerminal/AppHost.cpp b/src/cascadia/WindowsTerminal/AppHost.cpp
index 81218dbf4..af5d8d7ff 100644
--- a/src/cascadia/WindowsTerminal/AppHost.cpp
+++ b/src/cascadia/WindowsTerminal/AppHost.cpp
@@ -1406,6 +1406,8 @@ winrt::fire_and_forget AppHost::_WindowInitializedHandler(const winrt::Windows::
                               nCmdShow == SW_FORCEMINIMIZE;
     if (!noForeground)
     {
+        LOG_IF_WIN32_BOOL_FALSE(BringWindowToTop(_window->GetHandle()));
+
         SetForegroundWindow(_window->GetHandle());
         _peasantNotifyActivateWindow();
     }

around:

ShowWindow(_window->GetHandle(), nCmdShow);
// If we didn't start the window hidden (in one way or another), then try to
// pull ourselves to the foreground. Don't necessarily do a whole "summon",
// we don't really want to STEAL foreground if someone rightfully took it
const bool noForeground = nCmdShow == SW_SHOWMINIMIZED ||
nCmdShow == SW_SHOWNOACTIVATE ||
nCmdShow == SW_SHOWMINNOACTIVE ||
nCmdShow == SW_SHOWNA ||
nCmdShow == SW_FORCEMINIMIZE;
if (!noForeground)
{
SetForegroundWindow(_window->GetHandle());
_peasantNotifyActivateWindow();
}

That's what I'd bet the culprit is, if there is one.

@w3lld0ne
Copy link

w3lld0ne commented Sep 20, 2023

Uhg guys I can't get this to repro. Certainly not consistently. I had it happen like, once by

  • shift clicking on the taskbar (with a window already open),
  • waiting what seemed like a minute
  • activating other windows. Minimizing one.
  • then eventually the new window appeared, beneath everything.

I reckon that this regressed in the #5000 changes. Especially somewhere around the way that a new window gets summoned in the existing process.

I just can't for the life of me get a consistent repro. It probably depends on

  • If there's a terminal window already running
  • How you spawn the new terminal window
  • what you do before the new terminal window launches

Not sure it's actually related to this issue but anyways, regarding a non-focused window on startup, you can also take a look at this thread from Directory Opus user.

I can confirm the same behaviour: invoking default "Open in Terminal" context menu entry in DOpus is misbehaving (it's not happening with Explorer or when launching the Terminal from Start Menu, though).

@zadjii-msft
Copy link
Member

Welp. I can't repro that with Directory Opus myself. Just launches straight to the foreground, from both a cold launch and a warm launch.

Are you doing anything weird with elevate: true in your profiles? From the GIF in the linked thread, that looks like they're running with UAC disabled entirely, but that might be another variable here.

@w3lld0ne
Copy link

w3lld0ne commented Sep 21, 2023

Welp. I can't repro that with Directory Opus myself. Just launches straight to the foreground, from both a cold launch and a warm launch.

Are you doing anything weird with elevate: true in your profiles? From the GIF in the linked thread, that looks like they're running with UAC disabled entirely, but that might be another variable here.

Indeed, I had UAC disabled as well, but after re-enabling it, Terminal no longer launches as admin, but still launches behind DOpus window. Also note that the problem is only with 'Open in Terminal' entry, if for example I launch PowerShell or Cmd directly - everything works fine, they're opened in Terminal which is correctly brought to foreground.

Update1: It happens for me regardless of LaunchMode mentioned in this issue (I've tried 'Default', 'Maximized', 'Fullscreen', 'Focus', 'Maximized Focus' and they all appear behind DOpus).

Update2: If I now set the default profile to launch elevated, the system correctly shows me UAC prompt and then brings the window to foreground.

Update3: If I launch DOpus itself elevated, then there is no 'Open in Terminal' entry anymore and when I try to launch PowerShell/Cmd directly they no longer spawn inside WT, but instead in their own consoles.

@marbaa
Copy link
Author

marbaa commented Sep 25, 2023

I can repro my previous steps like 99% all the time. Doesn't matter if it is fresh boot with nothing running, or launching over 30 apps.

Next thing is if I click drop arrow in terminal, and Shift + Click on any profile, new window is shown behind Terminal window from which I executed new profile, not in foreground. But now, not always, steps to reproduce:

  1. Don't have any Terminal running.
  2. Start Terminal with whatever way.
  3. Shift + Click on any new profile in down arrow menu - new Terminal window is shown in background.
  4. Shift + Click again on any new profile to open another session - new Terminal winndow is now shown in foreground.

But this is not 100% always. If I do it continuously, it spawns new windows randomly in foreground/background.

@lhecker
Copy link
Member

lhecker commented Sep 26, 2023

Relatedly to this issue, WT sometimes opens in the background, behind all other applications. I can fairly readily reproduce that by holding shift while clicking it in the start menu. Holding shift should simply make the launched application unfocused and this works correctly for e.g. conhost, but WT doesn't even launch in the foreground.

@marbaa
Copy link
Author

marbaa commented Sep 27, 2023

Now, with v1.19, WT is launched in background 100% times, behind all apps. Looks like new instance is put on the last place of Z-order.
If no apps are running, WT is launched with no focus.

@zadjii-msft
Copy link
Member

zadjii-msft commented Sep 27, 2023

I can fairly readily reproduce that by holding shift while clicking it in the start menu.

You wanna know something crazy? I can 100% repro that, UNLESS I have a debugger attached. With either VS or WinDBG attached, the window always opens in the front, with foreground. That's just amazingly annoying for the sake of attempting to debug.

Interestingly enough, I get the same behavior with Notepad that I'm seeing here. Curious.

@w3lld0ne
Copy link

w3lld0ne commented Oct 3, 2023

Interestingly enough, I get the same behavior with Notepad that I'm seeing here. Curious.

For me, it sometimes also happens with ShareX, when double-clicking their tray icon. May be not a Terminal, but a Windows issue?

Also, found out that the second & further Terminal windows always open behind any other apps & even behind previous Terminal windows. This happens regardless of how the first window was launched.
NVIDIA_Share_VnhH32fR9W

This is the latest v1.18, and the following param: image

@zadjii-msft zadjii-msft changed the title Window has no focus when Launch Mode is set to Default Sometimes the Terminal launches behind all other windows / without foreground / isn't activated Oct 4, 2023
@MolotovCherry
Copy link

MolotovCherry commented Oct 28, 2023

This seems to happen to me when I enable open a quake window / run in headless

It stops happening once I kill all wt processes and there's no quake mode enabled

For me, 100% reproducibility

@wreiske
Copy link

wreiske commented Nov 9, 2023

This is incredibly annoying on windows 11 since there is no way to ungroup taskbar items (or at least there wasn't, I haven't researched lately)... I feel like recently something has changed with how this works.

I'll hit windows key, type term and hit enter to open a new terminal fast. It's in my muscle memory. Now I have to go searching for the new window every time!

@M322458
Copy link

M322458 commented Nov 14, 2023

I have the same issue. Just started after a fresh install of windows and all apps.

@M322458
Copy link

M322458 commented Nov 14, 2023

I went to settings, changed the launch parameters and tested a few options and saved. Changed them back to defaults and now it's not happening. We'll see if it re-occurs.

@M322458
Copy link

M322458 commented Nov 15, 2023

I went to settings, changed the launch parameters and tested a few options and saved. Changed them back to defaults and now it's not happening. We'll see if it re-occurs.

It didn't fix it for long.

@lucasreddinger
Copy link

@AshleyScirra thank you for your replication steps. they also replicate for me.

--

@jakenybo do the steps provided in #15479 (comment) replicate your issue reliably (and compared to beginning with no terminal windows open and minimized)?

many people here report being frustrated with this problem, so i would assume these people do not have a fix. i suspect we simply have to use workarounds

@jakenybo
Copy link

@lucasreddinger, yes those steps replicate the same behavior on my machine. i'm learning that i think the issue has more to do with whether or not you already have an instance of Windows Terminal running. if no prior Windows Terminal instance is running, the "Open in Terminal" option in a folder will open the Windows Terminal window on top of Windows Explorer (as it should). if you already have another instance of Windows Terminal running (even minimized to system tray, as is the case with me), then all subsequent "Open in Terminal" options will open the Windows Terminal window behind the WIndows Explorer window (which is erroneous behavior given its default behavior). i don't know why anyone would ever want it to open in the background if you just selected "Open in Terminal". that should always result in Window Terminal opening on top of all other windows

a fix for this would be great!

@barthofu
Copy link

barthofu commented Mar 5, 2024

Also having the problem here!

I came to the same conclusion as @jakenybo.

Versions

  • Windows Terminal: 1.19.10573.0
  • Windows version: Windows 11 Family - 23H2 - 22631.3155

@alex-campulungeanu
Copy link

alex-campulungeanu commented Mar 10, 2024

in my case, the win+r + wt combination openes a terminal in foreground if i have another terminal opened which was moved on the screen using winkey+alt

@eonward
Copy link

eonward commented Mar 21, 2024

I experience this behavior consistently when:

  • Launching from from system-generated start menu shortcut, search result, or 'all apps' list
    • Except when launching in admin mode
  • Launching from pinned taskbar element (or by left clicking taskbar element > l-click Terminal > Open)
    • Except when launching in admin mode

This behavior does not occur when:

  • Launching from Win + X menu
  • Launching by double clicking executable
  • Launching from user-created shortcut

In my case, the behavior described above is consistent across profiles, launching directories, and seems independent of running instances and in-app settings.

Build 22631.3296
WT 1.19.10573.0

PD: Here's the icon in case anyone is trying to make new shortcuts:
https://raw.githubusercontent.com/microsoft/terminal/master/res/terminal.ico

@marbaa
Copy link
Author

marbaa commented Mar 22, 2024

New observation. I'm getting 100% no-focus when launching Terminal with WSL2 default profile for the first time. Doesn't matter how/from where.

As for fist time it took like 2-3 seconds to start WSL2. Probably it is launhch Hyper-V services or whatever else, I don't know. During startup it has focus, WSL2 distro shows shell and Terminal loses focus. Funny thing is that while it is starting, it affect VMware Player window, because it flashes.

Often ctrl + click from dropdown menu opens new Terminal without focus, or even behind current Terminal window.
Mess..

Terminal Version: 1.20.10572.0
Windows 11 22H2 (22621.3155)
Dell 7560 + NVIDIA T1200 (driver 551.61)

I think it never happened with WSL1 as it starts instantly.

@tusharsnx
Copy link
Contributor

tusharsnx commented Mar 22, 2024

@marbaa That sounds like the WSL issue here: microsoft/wslg#1212.

This also means that specific issue is not related to WT, and could be repro on other terminals (Eg. VS, VSCode)

@snip3rnick
Copy link

I have been facing the same issue consistently when opening the WT through the Windows 11 start menu or PowerToys Run.
By my observation this occurs after one Window of WT is already open somewhere, which I do have at all times.
The new terminal window seemingly is placed in the Window stack on top of the topmost existing WT Window.

  • WT Version: 1.19.10573.0
  • WinVer: Win11 23H2 (22631.3296)

I could see this being resolved by forcibly ordering the new window on top of the Window stack with the HWND_TOPMOST flag in a SetWindowPos call right after the window creation.
I acknowledge that I have not looked at the implementation which may already include this or similar.

@Youngerkind
Copy link

Youngerkind commented May 4, 2024

For the version (1.19.11213.0, released on May 4th, 2024), the problem seemed solved. New window can be created on the top of the old window. But that is not the case if new windows are created on a different desktop:

If I open one window at one virtual desktop, switch to another virtual desktop, open a new window and close it. From now on, the new created window will lose the focus, thought the cursor blinking.

I find this from about one year ago and use the preview one downloaded here, instead of that installed in the Microsoft store.
I need this feature for that, I always open several terminal windows on several desktops, some for remote ssh, some for local wsl and so on.

@MolotovCherry
Copy link

MolotovCherry commented May 4, 2024

I tried the new prelease. Problem still happens to me. Any subsequent launches after the first have their windows go behind everything else, regardless whether the launch was from context menu or app shortcut in the taskbar search

Edit: just realized my comment sounds a bit snarky. That was not intended. I read the above comment wrong thinking it was solved in the new version, and wanted to make it clear it still happens for me

@snip3rnick
Copy link

I can confirm that the behavior does persist with ne new version on my system as well.
When startet using PowerToys Run or the explorer context menu the new terminal will open as the second topmost window.
Interestingly though when opened with the Windows start menu it opened behind every other window (even other terminals).

I hope hope this can be resolved, I am happy with the terminal application regardless. It would just add convenience if the opened terminal was visible and had keyboard focus.

If I can find the time I will try to help in finding a workaround.

@tusharsnx
Copy link
Contributor

tusharsnx commented May 4, 2024

If I can find the time I will try to help in finding a workaround.

You know what, if you just ensure that any window of the terminal is focused when the new window is starting up, the new window always starts in the foreground.

And same goes for when no window of the terminal has focus, the new window starts behind all other windows on the screen😕

It doesn't work as a workaround though, because only the foreground window (or its process) can request to change the foreground window to a different one.

@whitestrake
Copy link

I have some different behaviours depending on launch method, but for me, it seems to only be failing with the Quake console active.

Launch method Quake console exited Quake console running
Win+X, I ✅ Foreground, active ⛔ Foreground, inactive
Win+R, "wt", Enter ✅ Foreground, active ⛔ Background
Win, "wt", Enter ✅ Foreground, active ✅ Foreground, active
Win, "terminal", Enter ✅ Foreground, active ⛔ Background

@AldieNightStar
Copy link

Started to open on background always. Only win + "wt" is working on active. But i want to open in current directory. So i have to hide all windows to find terminal. Strange update

@n00mkrad
Copy link

n00mkrad commented Jun 4, 2024

Same issue. Also happens when run through "Send To" shortcut or when started through Flow Launcher.

github-merge-queue bot pushed a commit that referenced this issue Jun 10, 2024
Sometimes subsequent WT windows open in the background behind other
applications. This PR tries to fix it.

Refs #15895
Refs #15479

Mysterious bug (and annoying). There are even some discussions about
happening to the first startup, not just subsequent ones. Sometimes the
window may show up without animation too. So I don't think this is the
final solution, but it did get solved on my computer, for now.

## Validation Steps Performed
0. Quit all WT windows if some.
1. Open File Explorer, click "Open in Terminal" in context menu.
2. Move the newly opened window and minimize it.
3. Back to step 1 and repeat several times.
4. All the windows should open in the foreground correctly (yet possibly
without animation).

---------

Co-authored-by: Mike Griese <migrie@microsoft.com>
DHowett pushed a commit that referenced this issue Jun 24, 2024
Sometimes subsequent WT windows open in the background behind other
applications. This PR tries to fix it.

Refs #15895
Refs #15479

Mysterious bug (and annoying). There are even some discussions about
happening to the first startup, not just subsequent ones. Sometimes the
window may show up without animation too. So I don't think this is the
final solution, but it did get solved on my computer, for now.

## Validation Steps Performed
0. Quit all WT windows if some.
1. Open File Explorer, click "Open in Terminal" in context menu.
2. Move the newly opened window and minimize it.
3. Back to step 1 and repeat several times.
4. All the windows should open in the foreground correctly (yet possibly
without animation).

---------

Co-authored-by: Mike Griese <migrie@microsoft.com>
(cherry picked from commit c52ba7d)
Service-Card-Id: 92715122
Service-Version: 1.21
DHowett pushed a commit that referenced this issue Jun 26, 2024
Sometimes subsequent WT windows open in the background behind other
applications. This PR tries to fix it.

Refs #15895
Refs #15479

Mysterious bug (and annoying). There are even some discussions about
happening to the first startup, not just subsequent ones. Sometimes the
window may show up without animation too. So I don't think this is the
final solution, but it did get solved on my computer, for now.

## Validation Steps Performed
0. Quit all WT windows if some.
1. Open File Explorer, click "Open in Terminal" in context menu.
2. Move the newly opened window and minimize it.
3. Back to step 1 and repeat several times.
4. All the windows should open in the foreground correctly (yet possibly
without animation).

---------

Co-authored-by: Mike Griese <migrie@microsoft.com>
(cherry picked from commit c52ba7d)
Service-Card-Id: 92715121
Service-Version: 1.20
@zadjii-msft zadjii-msft modified the milestones: Terminal v1.21, Backlog Aug 29, 2024
@zadjii-msft
Copy link
Member

This may have been fixed by #17368 in 1.22 Preview - can anyone confirm/?

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Aug 29, 2024
@AshleyScirra
Copy link

The steps I described in this comment no longer reproduce for me with Windows Terminal v1.20.11781.0 on Windows 11 23H2 (build 22631.4037). Glad it's working as this had been bugging me for ages!

@pTinosq
Copy link

pTinosq commented Sep 2, 2024

This may have been fixed by #17368 in 1.22 Preview - can anyone confirm/?

@zadjii-msft Can confirm that this is working for me! Thanks so much

Windows information:
Edition Windows 11 Business
Version 23H2
Installed on ‎22/‎09/‎2023
OS build 22631.4037
Experience Windows Feature Experience Pack 1000.22700.1027.0

@zadjii-msft zadjii-msft added the Resolution-Fix-Available It's available in an Insiders build or a release label Sep 3, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. label Sep 7, 2024
Copy link
Contributor

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Windowing Window frame, quake mode, tearout Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. Priority-1 A description (P1) Product-Terminal The new Windows Terminal. Resolution-Fix-Available It's available in an Insiders build or a release
Projects
None yet
Development

No branches or pull requests