terminal icon indicating copy to clipboard operation
terminal copied to clipboard

Terminal flashes when running a command that opens up scripts in new tabs

Open SimonCockx opened this issue 2 years ago • 2 comments

Windows Terminal version

1.15.3466.0

Windows build number

10.0.22621.0

Steps to reproduce

  1. Set "New instance behaviour" setting to "Attach to most recently window on this desktop".
  2. Open two tabs (say Tab 1 and Tab 2).
  3. In Tab 1, run any command that runs a script in a new terminal. Minimal example: run start powershell {echo hello}.
  4. Observe that you are now in Tab 2.

Expected Behavior

I would expect the tab initiating a command to keep focus. (i.e., I would expect that Tab 1 would remain focussed)

Actual Behavior

Minimal example:

https://user-images.githubusercontent.com/47859223/209470441-6c5bb94e-8c98-4907-a4cc-9c77c59e5fd2.mp4

Observe that I initiate the command in Tab 1, but I end up in Tab 2.


To get a real feeling of when this behaviour is incredibly annoying, run a command that launches many terminals. Then you get the "flashing" behaviour mentioned in the title of this issue.

I'm using nx as a build tool, which constantly opens up new terminals to run subscripts. Observe:

https://user-images.githubusercontent.com/47859223/209470449-41c284a5-0d49-4bfd-bf98-ab76cfe66ae9.mp4

The observed behaviour is that this command opens a new tab which immediately gets the focus, then closes within a matter of milliseconds, as a consequence focussing on the last tab of the terminal instead of the initial one. This is repeated several times.

SimonCockx avatar Dec 25 '22 13:12 SimonCockx

I see two potential solutions.

Solution 1: do not automatically focus on a tab that is created from a script

Running start powershell {echo hello} creates a new tab, but this tab should not get the focus.

This could potentially be a setting, e.g., "Focus behaviour", with options:

  • automatically focus on newly created tabs
  • do not focus newly created tabs

I think we should differentiate between tabs created by a command (e.g., start powershell) and tabs created by clicking the "+". The latter should of course always get focus.

Solution 2: when the tab with focus closes, automatically focus the last tab that had focus

Running start powershell {echo hello} creates a new tab with focus, but when the tab closes, the focus automatically goes back to the initial tab. In the example, focus would go back to "Tab 1". There would still be flashing behaviour, but it would be a bit better as I would be able to follow along the console output of the actual command while it's running, and I end up in the initial tab.

SimonCockx avatar Dec 25 '22 20:12 SimonCockx

Huh. To try and break this down another level, this is basically what's happening.

  • You've got WT(A) open with two tabs [1, 2]
  • in tab 1, you launch start pwsh ....
  • This launches pwsh.exe.
  • this creates a new Terminal instance (B), which determines it needs to attach this process to the existing window (A)
  • Window A opens a new tab [3] to handle the new pwsh instance.
  • [3] closes immediately, and the Terminal immediately focuses that last in order tab, not the MRU tab.

I coulda swore we had a discussion somewhere on the repo about doing what you describe in solution 2, maybe always. Just seemed like something sensible. But I can't find any trace of that discussion now... I'll sync up with my team (monday?) and double check to see if we're all cool with that.

Note: For discussion Are we okay changing the behavior on closing tabs to focus the MRU tab, not the previous in-order?

zadjii-msft avatar Jan 04 '23 00:01 zadjii-msft

Yup! We should do this. Committing to 1.18.

carlos-zamora avatar Jan 09 '23 21:01 carlos-zamora

Another note from discussion: We're pretty sure we already do this, and rudimentary testing confirms. What might be going on here is something like:

  1. defterm windows might to get added to the MRU order (especially if they're short-lived)
  2. maybe if there's too many short-lived tabs, then on closing one of them, we try to focus another one that's already gone, and ultimately focus the last (out of default)

there's some debugging that we need to do here

zadjii-msft avatar Jan 09 '23 22:01 zadjii-msft