From f9dd951fc32dc4e8b0470e7eec89f55468f12c58 Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Fri, 5 Aug 2022 18:52:14 +0200 Subject: [PATCH] JumpList code cleanup --- src/cascadia/TerminalApp/Jumplist.cpp | 75 +++++++++++---------------- src/cascadia/TerminalApp/Jumplist.h | 4 +- 2 files changed, 31 insertions(+), 48 deletions(-) diff --git a/src/cascadia/TerminalApp/Jumplist.cpp b/src/cascadia/TerminalApp/Jumplist.cpp index dcbcef030ac..4665a35b80d 100644 --- a/src/cascadia/TerminalApp/Jumplist.cpp +++ b/src/cascadia/TerminalApp/Jumplist.cpp @@ -89,12 +89,8 @@ winrt::fire_and_forget Jumplist::UpdateJumplist(const CascadiaSettings& settings winrt::com_ptr jumplistItems; jumplistItems.capture(jumplistInstance, &ICustomDestinationList::BeginList, &slots); - // It's easier to clear the list and re-add everything. The settings aren't - // updated often, and there likely isn't a huge amount of items to add. - THROW_IF_FAILED(jumplistItems->Clear()); - // Update the list of profiles. - THROW_IF_FAILED(_updateProfiles(jumplistItems.get(), strongSettings.ActiveProfiles().GetView())); + _updateProfiles(jumplistItems.get(), strongSettings.ActiveProfiles().GetView()); // TODO GH#1571: Add items from the future customizable new tab dropdown as well. // This could either replace the default profiles, or be added alongside them. @@ -116,26 +112,22 @@ winrt::fire_and_forget Jumplist::UpdateJumplist(const CascadiaSettings& settings // - profiles - The profiles to add to the jumplist // Return Value: // - S_OK or HRESULT failure code. -[[nodiscard]] HRESULT Jumplist::_updateProfiles(IObjectCollection* jumplistItems, winrt::Windows::Foundation::Collections::IVectorView profiles) noexcept +void Jumplist::_updateProfiles(IObjectCollection* jumplistItems, winrt::Windows::Foundation::Collections::IVectorView profiles) { - try - { - for (const auto& profile : profiles) - { - // Craft the arguments following "wt.exe" - auto args = fmt::format(L"-p {}", to_hstring(profile.Guid())); - - // Create the shell link object for the profile - winrt::com_ptr shLink; - const auto normalizedIconPath{ _normalizeIconPath(profile.Icon()) }; - RETURN_IF_FAILED(_createShellLink(profile.Name(), normalizedIconPath, args, shLink.put())); + // It's easier to clear the list and re-add everything. The settings aren't + // updated often, and there likely isn't a huge amount of items to add. + THROW_IF_FAILED(jumplistItems->Clear()); - RETURN_IF_FAILED(jumplistItems->AddObject(shLink.get())); - } + for (const auto& profile : profiles) + { + // Craft the arguments following "wt.exe" + auto args = fmt::format(L"-p {}", to_hstring(profile.Guid())); - return S_OK; + // Create the shell link object for the profile + const auto normalizedIconPath{ _normalizeIconPath(profile.Icon()) }; + const auto shLink = _createShellLink(profile.Name(), normalizedIconPath, args); + THROW_IF_FAILED(jumplistItems->AddObject(shLink.get())); } - CATCH_RETURN(); } // Method Description: @@ -150,36 +142,27 @@ winrt::fire_and_forget Jumplist::UpdateJumplist(const CascadiaSettings& settings // - shLink: The shell link object to return. // Return Value: // - S_OK or HRESULT failure code. -[[nodiscard]] HRESULT Jumplist::_createShellLink(const std::wstring_view name, - const std::wstring_view path, - const std::wstring_view args, - IShellLinkW** shLink) noexcept +winrt::com_ptr Jumplist::_createShellLink(const std::wstring_view name, const std::wstring_view path, const std::wstring_view args) { - try - { - auto sh = winrt::create_instance(CLSID_ShellLink, CLSCTX_ALL); + auto sh = winrt::create_instance(CLSID_ShellLink, CLSCTX_ALL); - const auto module{ GetWtExePath() }; - RETURN_IF_FAILED(sh->SetPath(module.data())); - RETURN_IF_FAILED(sh->SetArguments(args.data())); + const auto module{ GetWtExePath() }; + THROW_IF_FAILED(sh->SetPath(module.data())); + THROW_IF_FAILED(sh->SetArguments(args.data())); - PROPVARIANT titleProp; - titleProp.vt = VT_LPWSTR; - titleProp.pwszVal = const_cast(name.data()); + PROPVARIANT titleProp; + titleProp.vt = VT_LPWSTR; + titleProp.pwszVal = const_cast(name.data()); - PROPVARIANT iconProp; - iconProp.vt = VT_LPWSTR; - iconProp.pwszVal = const_cast(path.data()); + PROPVARIANT iconProp; + iconProp.vt = VT_LPWSTR; + iconProp.pwszVal = const_cast(path.data()); - auto propStore{ sh.as() }; - RETURN_IF_FAILED(propStore->SetValue(PKEY_Title, titleProp)); - RETURN_IF_FAILED(propStore->SetValue(PKEY_AppUserModel_DestListLogoUri, iconProp)); + auto propStore{ sh.as() }; + THROW_IF_FAILED(propStore->SetValue(PKEY_Title, titleProp)); + THROW_IF_FAILED(propStore->SetValue(PKEY_AppUserModel_DestListLogoUri, iconProp)); - RETURN_IF_FAILED(propStore->Commit()); + THROW_IF_FAILED(propStore->Commit()); - *shLink = sh.detach(); - - return S_OK; - } - CATCH_RETURN(); + return sh; } diff --git a/src/cascadia/TerminalApp/Jumplist.h b/src/cascadia/TerminalApp/Jumplist.h index 997c9778f82..ea7797431ea 100644 --- a/src/cascadia/TerminalApp/Jumplist.h +++ b/src/cascadia/TerminalApp/Jumplist.h @@ -21,6 +21,6 @@ class Jumplist static winrt::fire_and_forget UpdateJumplist(const winrt::Microsoft::Terminal::Settings::Model::CascadiaSettings& settings) noexcept; private: - [[nodiscard]] static HRESULT _updateProfiles(IObjectCollection* jumplistItems, winrt::Windows::Foundation::Collections::IVectorView profiles) noexcept; - [[nodiscard]] static HRESULT _createShellLink(const std::wstring_view name, const std::wstring_view path, const std::wstring_view args, IShellLinkW** shLink) noexcept; + static void _updateProfiles(IObjectCollection* jumplistItems, winrt::Windows::Foundation::Collections::IVectorView profiles); + static winrt::com_ptr _createShellLink(const std::wstring_view name, const std::wstring_view path, const std::wstring_view args); };