Files icon indicating copy to clipboard operation
Files copied to clipboard

Apps are launching behind the Files UWP app window

Open Taras-Parfeniuk opened this issue 6 years ago • 25 comments

Describe the bug If Windows Terminal set as default terminal app, after a click on 'Open in Terminal' context menu option the terminal window opens behind the Files UWP window, and the Files UWP window remains in focus.

To Reproduce Steps to reproduce the behavior:

  1. Go to 'Settings / Preferences'
  2. Select 'Windows Terminal' in 'Terminal Applications' dropdown
  3. Go to any directory
  4. Open right click context menu
  5. Click on 'Open in Terminal' option
  6. The Windows Terminal window opens, but the Files UWP window remains in focus

Expected behavior A newly opened terminal window should be focused like it is for CMD terminal application.

Screenshot FilesUWP-WinTerminal

Desktop (please complete the following information):

  • OS Version: Windows 10 1909 (Build: 18363.900)
  • Files UWP Version: 0.9.2.0
  • Windows Terminal Version: 1.0.1401.0

Taras-Parfeniuk avatar Jun 10 '20 15:06 Taras-Parfeniuk

@XPoppyX Same for me. I think it's a bug.

jgkumaran avatar Jun 15 '20 14:06 jgkumaran

When opening the app we need to set it to be focused, I haven't looked into it but I would assume it's a simple parameter.

yair100 avatar Jun 15 '20 14:06 yair100

for me only for the first or second time, launching the terminal, the terminal is in the background. After the third time it always opens in the foreground

FelixZacher avatar Jun 16 '20 11:06 FelixZacher

I have not been able to reproduce this issue, @XPoppyX @Jaiganeshkumaran @lampenlampen what version of Windows are you running? Perhaps the behavior changed with 2004 and that is why I cannot reproduce this.

yair100 avatar Jun 17 '20 04:06 yair100

@yair100 I am using Windows 10 Insider Preview Fast Ring.

jgkumaran avatar Jun 17 '20 05:06 jgkumaran

If you're using Windows 10 Insider Preview, the issue of apps opening in the background has been fixed with Build 20150.

jgkumaran avatar Jun 20 '20 14:06 jgkumaran

If you're using Windows 10 Insider Preview, the issue of apps opening in the background has been fixed with Build 20150.

Can it be fixed in "regular" Windows 10 (I use 19.09)?

FibYar avatar Jun 20 '20 14:06 FibYar

I don’t really know. Since it’s a system wide bug that affects all applications occasionally and not a Files UWP, I recommend you to give report this issue to Microsoft via Feedback Hub.

jgkumaran avatar Jun 20 '20 14:06 jgkumaran

I noticed Command Prompt doesn't have this issue. Can anyone else verify if this bug is with the Windows Terminal app only?

lukeblevins avatar Jun 25 '20 17:06 lukeblevins

We could always bring the Window to the foreground with Win32 APIs shortly after launching it.

@gave92 This sounds right up your ally. 😀

lukeblevins avatar Jun 25 '20 18:06 lukeblevins

@duke7553 I'm not finding a good way of bringing the window to the front :/ Issue is finding what was launched:

  • Launcher.LaunchFileAsync won't return the process/window that was opened
  • Process.Start(file_path) often returns null if the launched app was UWP

gave92 avatar Jul 02 '20 18:07 gave92

I have this same issue and also when opening a PDF file. Acrobat Pro opens in the background. I would add more details to this response to the email I received however, I have no idea where to look. I just really love this file manager!

EDIT

JP Johnston ME CSWP Rogue Exploration Technologies

jonnypjohnston avatar Jul 02 '20 18:07 jonnypjohnston

@gave92 So, you're saying Process.MainWindowHandle would return null, so we cannot use it in BringWindowToTop() ?

lukeblevins avatar Jul 02 '20 21:07 lukeblevins

@duke7553 The issue of launched app appearing on the background does not seem limited to the Window Terminal (#1270). So I was hoping to find something that works in the general case (e.g. opening a file).

  • Where we have a process object, like here, we can use Process.MainWindowHandle and bring the window to the top (this should work for windows terminal)
  • In other places a process object is not available:
    • like here we don't get a process or window handle out of LaunchFileAsync
    • and here we'll get a null return value from Process.Start in case the file was launched in a UWP app

Edit: but perhaps we can start by fixing Window Terminal and think on the rest later :) Edit 2: opened a PR with some test code that may solve this: @XPoppyX could you test this and see if it solves the issue? thanks!

Edit 3 The above PR #985 succeeded in consistently bringing the window to the front but I was not able to activate it (user had to click on it)

gave92 avatar Jul 03 '20 06:07 gave92

@duke7553 can this ticket be refactored to be more general?

MarcAnt01 avatar Aug 26 '20 18:08 MarcAnt01

@gave92 @yair100 I'm not sure if this one is an issue anymore.

In my testing, multiple apps like Terminal, Notepad, and Edge all launch in front of the app window, as expected. Maybe if it is still an issue, we can do something like this when launching another process(?):

public static void DemoteTopmostWindow()
{
     var topHwnd = PInvoke.User32.GetTopWindow(IntPtr.Zero);
     if (topHwnd != IntPtr.Zero)
     {
          var nextHwnd = PInvoke.User32.GetWindow(
                topHwnd,
                PInvoke.User32.GetWindowCommands.GW_HWNDNEXT);
          PInvoke.User32.SetForegroundWindow(nextHwnd);
     }
}

lukeblevins avatar Sep 07 '20 05:09 lukeblevins

@duke7553 I'm not sure if this is still an issue, as I was never able to reproduce this consistently. A couple of possible issues with the code above (that I remember from my previous testing):

  • the opened window is not always the "next" one, sometimes is the one at the very bottom
  • SetForegroundWindow was not working for me, possibly because of calling it from a process which is not on the foreground

gave92 avatar Sep 07 '20 05:09 gave92

@duke7553 I can still reproduce it.

yair100 avatar Sep 07 '20 12:09 yair100

@duke7553 I'm not sure if this is still an issue, as I was never able to reproduce this consistently. A couple of possible issues with the code above (that I remember from my previous testing):

  • the opened window is not always the "next" one, sometimes is the one at the very bottom
  • SetForegroundWindow was not working for me, possibly because of calling it from a process which is not on the foreground

Just read the docs on this. You're right, it has to be called from the foreground process, so that function won't work.

lukeblevins avatar Sep 07 '20 16:09 lukeblevins

@duke7553 there should be a way around this since Autohotkey and the like manage it, I tried looking at their source and they're doing some magic with AttachThreadInput function but could not get it to work

gave92 avatar Sep 07 '20 17:09 gave92