From fb1491a4af66ce1c6532a0abe105e402cd9e99e0 Mon Sep 17 00:00:00 2001 From: leejy12 <60320918+leejy12@users.noreply.github.com> Date: Wed, 1 Jun 2022 02:47:52 +0900 Subject: [PATCH] Hide "Open in Terminal" context menu option appropriately (#13206) This commit hides the "Open in Terminal" context menu option when the context menu is opened in a non-filesystem path like "Quick Actions". Closes #12578 --- .github/actions/spelling/expect/expect.txt | 2 ++ .../ShellExtension/OpenTerminalHere.cpp | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.github/actions/spelling/expect/expect.txt b/.github/actions/spelling/expect/expect.txt index 2cc27be650e..f4f432cb1a3 100644 --- a/.github/actions/spelling/expect/expect.txt +++ b/.github/actions/spelling/expect/expect.txt @@ -2179,6 +2179,8 @@ SETTITLE setw Setwindow SETWINDOWINFO +SFGAO +SFGAOF sfi SFINAE SFUI diff --git a/src/cascadia/ShellExtension/OpenTerminalHere.cpp b/src/cascadia/ShellExtension/OpenTerminalHere.cpp index 5f472d72f4b..d9b872136da 100644 --- a/src/cascadia/ShellExtension/OpenTerminalHere.cpp +++ b/src/cascadia/ShellExtension/OpenTerminalHere.cpp @@ -97,7 +97,7 @@ HRESULT OpenTerminalHere::GetTitle(IShellItemArray* /*psiItemArray*/, return SHStrDup(resource.data(), ppszName); } -HRESULT OpenTerminalHere::GetState(IShellItemArray* /*psiItemArray*/, +HRESULT OpenTerminalHere::GetState(IShellItemArray* psiItemArray, BOOL /*fOkToBeSlow*/, EXPCMDSTATE* pCmdState) { @@ -106,10 +106,25 @@ HRESULT OpenTerminalHere::GetState(IShellItemArray* /*psiItemArray*/, // E_PENDING and this object will be called back on a background thread with // fOkToBeSlow == TRUE - // We however don't need to bother with any of that, so we'll just return - // ECS_ENABLED. + // We however don't need to bother with any of that. + + // If no item was selected when the context menu was opened and Explorer + // is not at a valid path (e.g. This PC or Quick Access), we should hide + // the verb from the context menu. + if (psiItemArray == nullptr) + { + const auto path = this->_GetPathFromExplorer(); + *pCmdState = path.empty() ? ECS_HIDDEN : ECS_ENABLED; + } + else + { + winrt::com_ptr psi; + psiItemArray->GetItemAt(0, psi.put()); + SFGAOF attributes; + const bool isFileSystemItem = (psi->GetAttributes(SFGAO_FILESYSTEM, &attributes) == S_OK); + *pCmdState = isFileSystemItem ? ECS_ENABLED : ECS_HIDDEN; + } - *pCmdState = ECS_ENABLED; return S_OK; }