conda / menuinst

Cross platform menu item installation
https://conda.github.io/menuinst/
BSD 3-Clause "New" or "Revised" License
33 stars 41 forks source link

Allow suppressing cmd.exe windows on launching applications #128

Closed mrclary closed 11 months ago

mrclary commented 1 year ago

Checklist

What is the idea?

When launching applications from shortcuts created by menuinst on Windows, a cmd.exe window opens. A key should be provided in the menu.json file to specify whether the shortcut should suppress this behavior and menuinst should take the necessary steps to enforce this when creating the shortcut.

Why is this needed?

Unwanted cmd.exe windows are not suppressed.

What should happen?

cmd.exe windows should be suppressed or not according to specification in the menu.json file.

Additional Context

No response

mrclary commented 1 year ago

ping @jaimergp

jaimergp commented 1 year ago

We have some logic for this, but it might be not working. Take a look at the windows module in the cep-devel branch. I am typing from the phone, sorry for the brevity!

mrclary commented 1 year ago

Hmm... I don't see anything in cep-devel. But upon closer review, the cmd.exe window is only visible for a brief flash, then disappears; also the shorcut "Target" is:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "start 'C:\Users\rclary\AppData\Local\Spyder-5.4.4.dev1521\envs\spyder-5.4.4.dev1521\Menu\Spyder.bat' -WindowStyle hidden"

which has -WindowStyle hidden. Perhaps this is related to #127?

jaimergp commented 1 year ago

Yea, exactly, that's the part I was mentioning:

https://github.com/conda/menuinst/blob/b0199ae47be39903b533b39406dfcd837ccbe5d4/menuinst/platforms/win.py#L266-L269

Maybe the activation scripts are launching CMD on their own too, perhaps an oversight. Let's leave this open because I need to investigate further.

jaimergp commented 1 year ago

We might need a different approach (VBScript?) since the flashing behaviour is a known issue:

https://superuser.com/questions/62525/run-a-batch-file-in-a-completely-hidden-way

ccordoba12 commented 1 year ago

This could work (see last comment on that answer).

mrclary commented 1 year ago

This could work (see last comment on that answer).

I added /MIN to the shortcut target, but it did not seem to have any affect

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "start /MIN 'C:\Users\rclary\AppData\Local\Spyder-5.4.4.dev1521\envs\spyder-5.4.4.dev1521\Menu\Spyder.bat' -WindowStyle hidden"

Commenting out the entire contents of Spyder.bat did not have any affect either (except that Spyder does not start, as expected), the cmd window still flashes with titlebar "Spyder". So I don't think it has anything to do with the @CALL %ACTIVATOR% in Spyder.bat.

Something about -WindowStyle hidden in the shortcut target is not being respected. I'm wondering if this may be related to #127: if the icons are consolidated, perhaps the hidden window directive will be respected? 🤷🏼‍♂️

mrclary commented 1 year ago

I also get the same behavior bypassing PowerShell

C:\WINDOWS\system32\cmd.exe /c start /min 'C:\Users\rclary\AppData\Local\Spyder-5.4.4.dev1521\envs\spyder-5.4.4.dev1521\Menu\Spyder.bat'
ccordoba12 commented 1 year ago

I also get the same behavior bypassing PowerShell

This is strange because cmd start should hide any terminal windows when running.

jaimergp commented 1 year ago

I am looking into this as part of #119 and one thing I noticed is that Powershell does indeed hide the CMD window. What we see is the powershell dialog starting CMD 😬

I checked with the bare cmd /C START... and it doesn't work as expected. It also leaves the dialog open 🤷

It's getting more and more complicated with so many shell layers calling each other!

jaimergp commented 1 year ago

Uh, turns out this way of launching the script produces an almost invisible flash 😂

command = [
    f'"{system32 / "cmd.exe"}"', "/C", "START", "/MIN", '""',
    f'"{system32 / "WindowsPowerShell" / "v1.0" / "powershell.exe"}"',
    "-WindowStyle", "hidden",
    f"\"start '{script}' {arg1}-WindowStyle hidden\"",
]
mrclary commented 1 year ago

Perhaps an infinite series of nested shells would converge to zero visibility 🤣

jaimergp commented 1 year ago

cep-devel has the snippet above now, which should make things snappier!

mrclary commented 1 year ago

Two pieces of information to add to the mix:

  1. If I set the "Run:" shortcut property to "Minimized", then I don't see the cmd window flash. Screenshot 2023-06-22 at 11 21 51 AM

    But one side effect is that the application launched inside the bat file does not have focus if another application is already open.

  2. If I don't request environment activation and don't have a precommand, then the Spyder.bat script is not created and the command is used as the shortcut target instead of Spyder.bat. In this case, with command as {{ PREFIX }}\pythonw.exe {{ PREFIX }}\Scripts\spyder-script.py, there is no cmd window flash. spyder-script.py is created at conda install and is determined by Spyder's feedstock. The drawback is that environment activation and precommand aren't used. In the case of Spyder, we may not need these, but as a general solution this could be a problem.

A note: spyder.exe is created by conda install as well but will always generate a cmd window if used, under any circumstance.

jaimergp commented 1 year ago

The CMD flash is a byproduct of us having to run some shell code before running the shortcut target, sadly. If the shortcut doesn't need activation or pre-launch logic, then we can call it directly. If it works for Spyder, then perfect! Less moving parts too.

Activation is default in case some package in the dependency tree requires activation-time logic to work properly (e.g. let's say compilers) and it can be difficult for users to diagnose.

jaimergp commented 11 months ago

Current approach doesn't "flash" but still creates a (short lived) console window that is minimized to task bar. Still hacky and not ideal but It Works (tm).