Files: Apps are launching behind the Files UWP app window

Created on 10 Jun 2020  路  24Comments  路  Source: files-community/Files

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
bug help wanted triage approved

Most helpful comment

for me any file opens behind the app

All 24 comments

for me any file opens behind the app

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

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.

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

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.

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

in the microsoft store version it does happen, but with the last changes to the master branch I can no longer reproduce this issue

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

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

I don鈥檛 really know. Since it鈥檚 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.

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

only with the terminal app for me
terminal

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

@gave92 This sounds right up your ally. 馃榾

@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

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

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

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

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

@gave92 @yaichenbaum 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);
     }
}

@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

@duke7553 I can still reproduce it.

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

@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

@duke7553 Here's an idea. Create a fake blank foreground and make it transparent

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Cyberdroid1 picture Cyberdroid1  路  3Comments

DeDaMrAzR picture DeDaMrAzR  路  3Comments

Jaiganeshkumaran picture Jaiganeshkumaran  路  3Comments

alialix2 picture alialix2  路  3Comments

xpirad picture xpirad  路  3Comments