Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make opening the settings file more robust #1841

Merged
merged 2 commits into from
Jul 9, 2019
Merged

Make opening the settings file more robust #1841

merged 2 commits into from
Jul 9, 2019

Conversation

maru-sama
Copy link
Contributor

@maru-sama maru-sama commented Jul 5, 2019

This fixes two issues.

  • Opens the assigned default application regardless of its configuration.
    Gvim for example only reacts to the "edit" verb so when selected as default application won't open.
    Using nullptr results in using the first specified application.
    This fixes Settings do not open if json files are assigned to gvim #1789
  • If no application is assigned for json files fall back to notepad

See https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea for more details
especially why the result code checking is so horrific.

Summary of the Pull Request

References

PR Checklist

  • Closes Settings do not open if json files are assigned to gvim #1789
  • CLA signed. If not, go over here and sign the CLA
  • Tests added/passed
  • Requires documentation to be updated
  • I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #xxx

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

  • Set a default application and make sure it opens
  • Remove the default application and make sure that notepad gets opened

@@ -445,7 +445,12 @@ namespace winrt::TerminalApp::implementation
co_await winrt::resume_background();

const auto settingsPath = CascadiaSettings::GetSettingsPath();
ShellExecute(nullptr, L"open", settingsPath.c_str(), nullptr, nullptr, SW_SHOW);

HINSTANCE res = ShellExecute(nullptr, nullptr, settingsPath.c_str(), nullptr, nullptr, SW_SHOW);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the "try something else when the first one fails," but why did you drop the "open" verb?

Also can you leave a comment back to this page https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea that explains why the result code checking is so horrific for this API?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think dropping the open verb is also documented there:

NULL
The default verb is used, if available. If not, the "open" verb is used. If neither verb is available, the system uses the first verb listed in the registry.

That seems strictly better. GVim registers the edit verb (which we learned in #1789!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to explain this in the commit message. Gvim only sets up the registry for the "edit" verb so it does not work with the "open" verb. Setting this to NULL solves the issue as well.

NULL
The default verb is used, if available. If not, the "open" verb is used. If neither verb is available, > the system uses the first verb listed in the registry.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miniksa if you're concerned about the oddities of the ShellExecute return value, it might make sense to use ShellExecuteEx instead because that returns true or false.

As to why it's so odd, it made more sense in 16 bits.

This fixes two issues.

 * Opens the assigned default application regardless of its configuration.
   Gvim for example only reacts to the "edit" verb so when selected as default application won't open.
   Using nullptr results in using the first specified application.
   This fixes #1789
 * If no application is assigned for json files fall back to notepad

 See https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea for more details
 especially why the result code checking is so horrific.
Copy link
Contributor

@DHowett-MSFT DHowett-MSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing the work and investigation here!

ShellExecute(nullptr, L"open", settingsPath.c_str(), nullptr, nullptr, SW_SHOW);

HINSTANCE res = ShellExecute(nullptr, nullptr, settingsPath.c_str(), nullptr, nullptr, SW_SHOW);
if ((INT_PTR)res <= 32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In additional to Michael's comment, I'd prefer a static_cast<intptr_t>() versus a C cast, but I'm otherwise totally willing to approve this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me change this and push again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well apparently only

(reinterpret_cast<intptr_t>(res)

works, which I think does the same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this some more I think that reinterpret_cast just works since it does not check anything and the static cast fails, hence I will keep it as it is. Feel free to change it.

Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with fixing the c-style cast, but otherwise this is a great fix, thanks!

@maru-sama
Copy link
Contributor Author

maru-sama commented Jul 8, 2019

I agree with fixing the c-style cast, but otherwise this is a great fix, thanks!

I tried that before and could not get static_cast to work at all. reinterpret_cast was working but I do not feel comfortable using it here.

@maru-sama
Copy link
Contributor Author

maru-sama commented Jul 8, 2019

I did find a "fix" for that. but it looks akward....

static_cast<int>(reinterpret_cast<uintptr_t>.....

This should get rid if the compiler warnings I get otherwise. Should I add this or leave it like it is right now?

@maru-sama
Copy link
Contributor Author

Hi anything else that I can do to get this merged?

Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me. Thanks!

@zadjii-msft zadjii-msft merged commit bce8a79 into microsoft:master Jul 9, 2019
mcpiroman pushed a commit to mcpiroman/terminal that referenced this pull request Jul 23, 2019
* Make opening the settings file more robust

This fixes two issues.

 * Opens the assigned default application regardless of its configuration.
   Gvim for example only reacts to the "edit" verb so when selected as default application won't open.
   Using nullptr results in using the first specified application.
   This fixes microsoft#1789
 * If no application is assigned for json files fall back to notepad

 See https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea for more details
 especially why the result code checking is so horrific.

* Fix c-style cast
@ghost
Copy link

ghost commented Aug 3, 2019

🎉Windows Terminal Preview v0.3.2142.0 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Settings do not open if json files are assigned to gvim
5 participants