terminal/src/cascadia/TerminalSettingsModel/DefaultTerminal.cpp

90 lines
3.2 KiB
C++
Raw Normal View History

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
#include "DefaultTerminal.h"
#include "DefaultTerminal.g.cpp"
#include <LibraryResources.h>
using namespace winrt::Microsoft::Terminal::Settings;
using namespace winrt::Microsoft::Terminal::Settings::Model::implementation;
DefaultTerminal::DefaultTerminal(DelegationConfig::DelegationPackage&& pkg) :
_pkg{ pkg }
{
}
winrt::hstring DefaultTerminal::Name() const
{
return _pkg.terminal.name.empty() ? winrt::hstring{ RS_(L"InboxWindowsConsoleName") } : winrt::hstring{ _pkg.terminal.name };
}
winrt::hstring DefaultTerminal::Version() const
{
// If there's no version information... return empty string instead.
const auto& version = _pkg.terminal.version;
if (DelegationConfig::PkgVersion{} == version)
{
return winrt::hstring{};
}
fmt::wmemory_buffer buffer;
fmt::format_to(buffer, L"{}.{}.{}.{}", version.major, version.minor, version.build, version.revision);
return winrt::hstring{ buffer.data(), gsl::narrow_cast<winrt::hstring::size_type>(buffer.size()) };
}
winrt::hstring DefaultTerminal::Author() const
{
return _pkg.terminal.author.empty() ? winrt::hstring{ RS_(L"InboxWindowsConsoleAuthor") } : winrt::hstring{ _pkg.terminal.author };
}
winrt::hstring DefaultTerminal::Icon() const
{
return _pkg.terminal.logo.empty() ? winrt::hstring{ L"\uE756" } : winrt::hstring{ _pkg.terminal.logo };
}
std::pair<std::vector<Model::DefaultTerminal>, Model::DefaultTerminal> DefaultTerminal::Available()
{
// The potential of returning nullptr for defaultTerminal feels weird, but XAML can
// handle that appropriately and will select nothing as current in the dropdown.
std::vector<Model::DefaultTerminal> defaultTerminals;
Model::DefaultTerminal defaultTerminal{ nullptr };
std::vector<DelegationConfig::DelegationPackage> allPackages;
DelegationConfig::DelegationPackage currentPackage;
LOG_IF_FAILED(DelegationConfig::s_GetAvailablePackages(allPackages, currentPackage));
for (auto& pkg : allPackages)
{
// Be a bit careful here: We're moving pkg into the constructor.
// Afterwards it'll be invalid, so we need to cache isCurrent.
const auto isCurrent = pkg == currentPackage;
auto p = winrt::make<DefaultTerminal>(std::move(pkg));
if (isCurrent)
{
defaultTerminal = p;
}
defaultTerminals.emplace_back(std::move(p));
}
Prevent crashes in Settings UI launch on OS versions before package management extensions (#10238) Prevent crashes in Settings UI launch on OS versions before package management extensions ## PR Checklist * [x] Closes #10106 * [x] I work here * [x] Manual tests passed. ## Detailed Description of the Pull Request / Additional comments - On older OS versions like 18363, some of the COM interfaces we use to look up information from the OS application package management catalog (to find default terminals) are unavailable. This returns `E_NOINTERFACE`. This then ends up returning an empty list of items and null as a selected item. - I had intended for that to not return that particular error all the way up and just log it because the console and terminal lookup functions always return at least one element: the one representing the `conhost.exe` that is already on the machine. - I have changed the "default packages" lookup to log instead of return failures like E_NOINTERFACE such that it can continue processing and make the "package" of the hardcoded `conhost.exe` default no matter what. (It will still return an error if there are somehow 0 packages because that code changed or some other catastrophic event happened...) - I have also changed the Model to have a nulled DefaultTerminal model object (as all winrt objects are nullable) instead of using an optional. I did this because XAML is perfectly happy receiving a `nullptr` for a selected item and will just not select anything. By contrast, if it has an exception occur... it will just bubble that out and crash. ## Validation Steps Performed - Simulated no items returned from list and nullptr returned to XAML on Current() method of Model. Validated XAML will happily select no item from list (and is fine with an empty list of items... that is it doesn't crash). - Simulated downlevel OS returning package management errors in lookup catalog functions after the hardcoded default is added to the list. Ensured that this error is only logged, the remainder of the package identification functions make the hardcoded default package, and it is presented as your one and only option in the XAML.
2021-05-27 19:53:00 +02:00
return { std::move(defaultTerminals), std::move(defaultTerminal) };
}
bool DefaultTerminal::HasCurrent()
{
std::vector<DelegationConfig::DelegationPackage> allPackages;
DelegationConfig::DelegationPackage currentPackage;
LOG_IF_FAILED(DelegationConfig::s_GetAvailablePackages(allPackages, currentPackage));
// Good old conhost has a hardcoded GUID of {00000000-0000-0000-0000-000000000000}.
return currentPackage.terminal.clsid != CLSID{};
}
void DefaultTerminal::Current(const Model::DefaultTerminal& term)
{
THROW_IF_FAILED(DelegationConfig::s_SetDefaultByPackage(winrt::get_self<DefaultTerminal>(term)->_pkg, true));
}