microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
95.27k stars 8.27k forks source link

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

Closed marbaa closed 1 month ago

marbaa commented 1 year ago

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.

carlos-zamora commented 1 year ago

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

marbaa commented 1 year ago

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 commented 1 year ago

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

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

zadjii-msft commented 1 year ago

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: https://github.com/microsoft/terminal/blob/059f7701f241a314a049eb5d2c62cf7f4c3b803f/src/cascadia/WindowsTerminal/AppHost.cpp#L1396-L1412

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

w3lld0ne commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

MolotovCherry commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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

M322458 commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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).

"Open in Terminal" in Explorer does it for me sometimes. I'll try to figure out how to reproduce it reliably.

Infuriating!

Hicsy commented 9 months ago

2 days ago i noticed that when using the windows 11 context-menu Open in Terminal: Terminal launches BEHIND explorer 100% of the time. I use that link ~300 times per day and didn't notice it before, but I have an ultrawide, so perhaps it was always like this and I just fluked explorer never being in the same spot when it launches. More realistic estimate might be ~2-3 months ago when Power Rename also started opening behind W11 Explorer 100% of time when using that same context menu. I now end up with literally dozens of terminal windows in the background when I go to shutdown my PC each day, reminding me of all the times I didn't notice it launching behind focus.

AshleyScirra commented 9 months ago

In case it helps, I can reproduce this 100% on Windows 11 with these steps:

  1. Open File Explorer to a folder
  2. Right-click in a space in the folder and choose 'Open in Terminal' - it appears on top
  3. Minimize that Terminal window
  4. Right-click in a space in the folder and choose 'Open in Terminal' again - it appears underneath

I can't recall exactly but I don't think 'Open in Terminal' is in the explorer context menu by default, I think there's some developer setting to turn that on. Also step 4 requires that the terminal window attempts to open in a position overlapping File Explorer, so you might need to drag windows around to make sure that at step 4 they overlap.

This is really annoying for me as for my job I start the day by opening Terminal, starting a HTTP server, minimizing it, then doing web development all day which occasionally involves opening more terminals, which always appear under File Explorer every time.

pTinosq commented 7 months ago

I am having the exact same issues. Interestingly enough here's what I have found:

  1. Running wt in the run window = window goes to back
  2. Searching in windows search for "terminal" then pressing enter = window goes to back
  3. Right clicking taskbar icon and selecting terminal = window goes to back
  4. Opening terminal by doing win + x then selecting terminal = windows goes to front????

So a temporary shortcut for fellow shortcut lovers is to do win + x then i. That's the fastest way I have found to open a new wt without it going to the back.

This is so frustrating πŸ₯²

MolotovCherry commented 7 months ago

To follow up on pTinosq, I've been having this problem of it launching in the background for a long time.

I can confirm point 4 indeed did cause it to go to front for me. Huh.

jakenybo commented 7 months ago

in my configuration, right-clicking inside a folder in windows explorer and selecting "Open in Terminal" launches the Windows Terminal window behind the Windows Explorer window. frustrating because it require me to click down on the taskbar to bring it to the front for use. any suggestions on a fix?

lucasreddinger commented 7 months ago

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

--

@jakenybo do the steps provided in https://github.com/microsoft/terminal/issues/15479#issuecomment-1895644698 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 commented 7 months ago

@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 commented 7 months ago

Also having the problem here!

I came to the same conclusion as @jakenybo.

Versions

alex-campulungeanu commented 7 months ago

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 commented 6 months ago

I experience this behavior consistently when:

This behavior does not occur when:

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 commented 6 months ago

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 commented 6 months ago

@marbaa That sounds like the WSL issue here: https://github.com/microsoft/wslg/issues/1212.

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

snip3rnick commented 6 months ago

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.

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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

zadjii-msft commented 1 month ago

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

AshleyScirra commented 1 month ago

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 commented 1 month ago

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

microsoft-github-policy-service[bot] commented 1 month ago

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.