Skip to content

Commit

Permalink
Patch the default profile and version into the settings template
Browse files Browse the repository at this point in the history
  • Loading branch information
DHowett committed Apr 3, 2020
1 parent 9421553 commit 65cd6c3
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 25 deletions.
26 changes: 26 additions & 0 deletions src/cascadia/TerminalApp/AppLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,32 @@ namespace winrt::TerminalApp::implementation
return { L"" };
}

winrt::hstring AppLogic::ApplicationDisplayName() const
{
try
{
const auto package{ winrt::Windows::ApplicationModel::Package::Current() };
return package.DisplayName();
}
CATCH_LOG();

return RS_(L"AboutDialog_DisplayNameUnpackaged");
}

winrt::hstring AppLogic::ApplicationVersion() const
{
try
{
const auto package{ winrt::Windows::ApplicationModel::Package::Current() };
const auto version{ package.Id().Version() };
winrt::hstring formatted{ wil::str_printf<std::wstring>(L"%u.%u.%u.%u", version.Major, version.Minor, version.Build, version.Revision) };
return formatted;
}
CATCH_LOG();

return RS_(L"AboutDialog_VersionUnknown");
}

// -------------------------------- WinRT Events ---------------------------------
// Winrt events need a method for adding a callback to the event and removing the callback.
// These macros will define them both for you.
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/AppLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ namespace winrt::TerminalApp::implementation
int32_t SetStartupCommandline(array_view<const winrt::hstring> actions);
winrt::hstring EarlyExitMessage();

winrt::hstring ApplicationDisplayName() const;
winrt::hstring ApplicationVersion() const;

Windows::Foundation::Point GetLaunchDimensions(uint32_t dpi);
winrt::Windows::Foundation::Point GetLaunchInitialPositions(int32_t defaultInitialX, int32_t defaultInitialY);
winrt::Windows::UI::Xaml::ElementTheme GetRequestedTheme();
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/AppLogic.idl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ namespace TerminalApp

String Title { get; };

String ApplicationDisplayName { get; };
String ApplicationVersion { get; };

Windows.Foundation.Point GetLaunchDimensions(UInt32 dpi);
Windows.Foundation.Point GetLaunchInitialPositions(Int32 defaultInitialX, Int32 defaultInitialY);
Windows.UI.Xaml.ElementTheme GetRequestedTheme();
Expand Down
48 changes: 43 additions & 5 deletions src/cascadia/TerminalApp/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,33 @@ static constexpr std::wstring_view PACKAGED_PROFILE_ICON_PATH{ L"ms-appx:///Prof
static constexpr std::wstring_view PACKAGED_PROFILE_ICON_EXTENSION{ L".png" };
static constexpr std::wstring_view DEFAULT_LINUX_ICON_GUID{ L"{9acb9455-ca41-5af7-950f-6bca1bc9722f}" };

// make sure this matches defaults.json.
static constexpr std::wstring_view DEFAULT_WINDOWS_POWERSHELL_GUID{ L"{61c54bbd-c2c6-5271-96e7-009a87ff44bf}" };

// Method Description:
// - Returns the settings currently in use by the entire Terminal application.
// - Returns the active application logic singleton
// Throws:
// - HR E_INVALIDARG if the app isn't up and running.
const CascadiaSettings& CascadiaSettings::GetCurrentAppSettings()
static auto GetAppLogic()
{
auto currentXamlApp{ winrt::Windows::UI::Xaml::Application::Current().as<winrt::TerminalApp::App>() };
THROW_HR_IF_NULL(E_INVALIDARG, currentXamlApp);

auto appLogic = winrt::get_self<winrt::TerminalApp::implementation::AppLogic>(currentXamlApp.Logic());
THROW_HR_IF_NULL(E_INVALIDARG, appLogic);
auto appLogicPointer = winrt::get_self<winrt::TerminalApp::implementation::AppLogic>(currentXamlApp.Logic());
THROW_HR_IF_NULL(E_INVALIDARG, appLogicPointer);

winrt::com_ptr<winrt::TerminalApp::implementation::AppLogic> appLogic;
appLogic.copy_from(appLogicPointer);
return appLogic;
}

// Method Description:
// - Returns the settings currently in use by the entire Terminal application.
// Throws:
// - HR E_INVALIDARG if the app isn't up and running.
const CascadiaSettings& CascadiaSettings::GetCurrentAppSettings()
{
auto appLogic{ GetAppLogic() };
return *(appLogic->GetSettings());
}

Expand Down Expand Up @@ -70,7 +85,7 @@ CascadiaSettings::CascadiaSettings(const bool addDynamicProfiles)
// - profileName: the name of the profile's GUID to return.
// Return Value:
// - the GUID associated with the profile name.
std::optional<GUID> CascadiaSettings::FindGuid(const std::wstring& profileName) const noexcept
std::optional<GUID> CascadiaSettings::FindGuid(const std::wstring_view profileName) const noexcept
{
std::optional<GUID> profileGuid{};

Expand Down Expand Up @@ -673,3 +688,26 @@ void CascadiaSettings::_ValidateKeybindings()
_warnings.insert(_warnings.end(), keybindingWarnings.begin(), keybindingWarnings.end());
}
}

std::string CascadiaSettings::_ApplyFirstRunChangesToSettingsTemplate(std::string_view settingsTemplate) const
{
std::string finalSettings{ settingsTemplate };
auto replace{ [](std::string& haystack, std::string_view needle, std::string_view replacement) {
auto pos{ std::string::npos };
while ((pos = haystack.rfind(needle, pos)) != std::string::npos)
{
haystack.replace(pos, needle.size(), replacement);
}
} };

std::wstring defaultProfileGuid{ DEFAULT_WINDOWS_POWERSHELL_GUID };
if (const auto psCoreProfileGuid{ FindGuid(PowershellCoreProfileGenerator::GetPreferredPowershellProfileName()) })
{
defaultProfileGuid = Utils::GuidToString(*psCoreProfileGuid);
}

replace(finalSettings, "%DEFAULT_PROFILE%", til::u16u8(defaultProfileGuid));
replace(finalSettings, "%VERSION%", til::u16u8(GetAppLogic()->ApplicationVersion()));
replace(finalSettings, "%PRODUCT%", til::u16u8(GetAppLogic()->ApplicationDisplayName()));
return finalSettings;
}
3 changes: 2 additions & 1 deletion src/cascadia/TerminalApp/CascadiaSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class TerminalApp::CascadiaSettings final
static std::filesystem::path GetSettingsPath();
static std::filesystem::path GetDefaultSettingsPath();

std::optional<GUID> FindGuid(const std::wstring& profileName) const noexcept;
std::optional<GUID> FindGuid(const std::wstring_view profileName) const noexcept;
const Profile* FindProfile(GUID profileGuid) const noexcept;

std::vector<TerminalApp::SettingsLoadWarnings>& GetWarnings();
Expand All @@ -95,6 +95,7 @@ class TerminalApp::CascadiaSettings final
static const Json::Value& _GetDisabledProfileSourcesJsonObject(const Json::Value& json);
bool _PrependSchemaDirective();
bool _AppendDynamicProfilesToUserSettings();
std::string _ApplyFirstRunChangesToSettingsTemplate(std::string_view settingsTemplate) const;

void _ApplyDefaultsFromUserSettings();

Expand Down
20 changes: 13 additions & 7 deletions src/cascadia/TerminalApp/CascadiaSettingsSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,22 @@ std::unique_ptr<CascadiaSettings> CascadiaSettings::LoadAll()
{
resultPtr->_ParseJsonString(fileData.value(), false);
}
else

// Load profiles from dynamic profile generators. _userSettings should be
// created by now, because we're going to check in there for any generators
// that should be disabled (if the user had any settings.)
resultPtr->_LoadDynamicProfiles();

if (!fileHasData)
{
// We didn't find the user settings. We'll need to create a file
// to use as the user defaults.
// For now, just parse our user settings template as their user settings.
resultPtr->_ParseJsonString(UserSettingsJson, false);
auto userSettings{ resultPtr->_ApplyFirstRunChangesToSettingsTemplate(UserSettingsJson) };
resultPtr->_ParseJsonString(userSettings, false);
needToWriteFile = true;
}

// Load profiles from dynamic profile generators. _userSettings should be
// created by now, because we're going to check in there for any generators
// that should be disabled.
resultPtr->_LoadDynamicProfiles();

// See microsoft/terminal#2325: find the defaultSettings from the user's
// settings. Layer those settings upon all the existing profiles we have
// (defaults and dynamic profiles). We'll also set
Expand Down Expand Up @@ -902,5 +904,9 @@ const Json::Value& CascadiaSettings::_GetProfilesJsonObject(const Json::Value& j
// given object
const Json::Value& CascadiaSettings::_GetDisabledProfileSourcesJsonObject(const Json::Value& json)
{
if (!json)
{
return Json::Value::nullSingleton();
}
return json[JsonKey(DisabledProfileSourcesKey)];
}
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/DefaultProfileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ TerminalApp::Profile CreateDefaultProfile(const std::wstring_view name)
gsl::as_bytes(gsl::make_span(name))) };
TerminalApp::Profile newProfile{ profileGuid };

newProfile.SetName(static_cast<std::wstring>(name));
newProfile.SetName(name);

std::wstring iconPath{ PACKAGED_PROFILE_ICON_PATH };
iconPath.append(Microsoft::Console::Utils::GuidToString(profileGuid));
Expand Down
12 changes: 11 additions & 1 deletion src/cascadia/TerminalApp/PowershellCoreProfileGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static constexpr std::wstring_view POWERSHELL_PREVIEW_PFN{ L"Microsoft.PowerShel
static constexpr std::wstring_view PWSH_EXE{ L"pwsh.exe" };
static constexpr std::wstring_view POWERSHELL_ICON{ L"ms-appx:///ProfileIcons/pwsh.png" };
static constexpr std::wstring_view POWERSHELL_PREVIEW_ICON{ L"ms-appx:///ProfileIcons/pwsh-preview.png" };
static constexpr std::wstring_view POWERSHELL_PREFERRED_PROFILE_NAME{ L"PowerShell" };

namespace
{
Expand Down Expand Up @@ -322,8 +323,17 @@ std::vector<TerminalApp::Profile> PowershellCoreProfileGenerator::GenerateProfil
// (or the closest approximation thereof). It may choose a preview instance as the "best" if it is a higher version.
auto firstProfile = profiles.begin();
firstProfile->SetGuid(PowershellCoreGuid);
firstProfile->SetName(L"PowerShell");
firstProfile->SetName(POWERSHELL_PREFERRED_PROFILE_NAME);
}

return profiles;
}

// Function Description:
// - Returns the thing it's named for.
// Return value:
// - the thing it says in the name
const std::wstring_view PowershellCoreProfileGenerator::GetPreferredPowershellProfileName()
{
return POWERSHELL_PREFERRED_PROFILE_NAME;
}
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/PowershellCoreProfileGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace TerminalApp
class PowershellCoreProfileGenerator : public TerminalApp::IDynamicProfileGenerator
{
public:
static const std::wstring_view GetPreferredPowershellProfileName();

PowershellCoreProfileGenerator() = default;
~PowershellCoreProfileGenerator() = default;
std::wstring_view GetNamespace() override;
Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/TerminalApp/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,9 @@ void Profile::SetStartingDirectory(std::wstring startingDirectory) noexcept
_startingDirectory = std::move(startingDirectory);
}

void Profile::SetName(std::wstring name) noexcept
void Profile::SetName(const std::wstring_view name) noexcept
{
_name = std::move(name);
_name = static_cast<std::wstring>(name);
}

void Profile::SetUseAcrylic(bool useAcrylic) noexcept
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/Profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class TerminalApp::Profile final
void SetAcrylicOpacity(double opacity) noexcept;
void SetCommandline(std::wstring cmdline) noexcept;
void SetStartingDirectory(std::wstring startingDirectory) noexcept;
void SetName(std::wstring name) noexcept;
void SetName(const std::wstring_view name) noexcept;
void SetUseAcrylic(bool useAcrylic) noexcept;
void SetDefaultForeground(COLORREF defaultForeground) noexcept;
void SetDefaultBackground(COLORREF defaultBackground) noexcept;
Expand Down
8 changes: 2 additions & 6 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ namespace winrt::TerminalApp::implementation
{
try
{
const auto package{ winrt::Windows::ApplicationModel::Package::Current() };
return package.DisplayName();
return ::winrt::Windows::UI::Xaml::Application::Current().as<::winrt::TerminalApp::App>().Logic().ApplicationDisplayName();
}
CATCH_LOG();

Expand All @@ -248,10 +247,7 @@ namespace winrt::TerminalApp::implementation
{
try
{
const auto package{ winrt::Windows::ApplicationModel::Package::Current() };
const auto version{ package.Id().Version() };
winrt::hstring formatted{ wil::str_printf<std::wstring>(L"%u.%u.%u.%u", version.Major, version.Minor, version.Build, version.Revision) };
return formatted;
return ::winrt::Windows::UI::Xaml::Application::Current().as<::winrt::TerminalApp::App>().Logic().ApplicationVersion();
}
CATCH_LOG();

Expand Down
4 changes: 3 additions & 1 deletion src/cascadia/TerminalApp/userDefaults.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// This file was generated by %PRODUCT% %VERSION%

// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation

{
"$schema": "https://aka.ms/terminal-profiles-schema",

"defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"defaultProfile": "%DEFAULT_PROFILE%",

"profiles":
{
Expand Down

1 comment on commit 65cd6c3

@github-actions
Copy link

Choose a reason for hiding this comment

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

New misspellings found, please review:

  • rfind
To accept these changes, run the following commands
remove_obsolete_words=$(mktemp)
echo '#!/usr/bin/perl -ni
my $re=join "|", qw('"
Impl
trackpads
ve
XY
"');
next if /^($re)(?:$| .*)/;
print;' > $remove_obsolete_words
chmod +x $remove_obsolete_words
for file in .github/actions/spell-check/whitelist/alphabet.txt .github/actions/spell-check/whitelist/web.txt .github/actions/spell-check/whitelist/whitelist.txt; do $remove_obsolete_words $file; done
rm $remove_obsolete_words
(
echo "
impl
rfind
xy
"
) | sort -u -f | perl -ne 'next unless /./; print' > new_whitelist.txt && mv new_whitelist.txt '.github/actions/spell-check/whitelist/65cd6c3a7fb47ba1219be0e888bce2ddac8843ea.txt'
✏️ Contributor please read this
  • If the items listed above are names, please add them to .github/actions/spell-check/dictionary/names.txt.
  • If they're APIs, you can add them to a file in .github/actions/spell-check/dictionary/.
  • If they're just things you're using, please add them to an appropriate file in .github/actions/spell-check/whitelist/.
  • If you need to use a specific token in one place and it shouldn't generally be used, you can
    add an item in an appropriate file in .github/actions/spell-check/patterns/.

See the README.md in each directory for more information.

⚠️ Reviewers

At present, the action that triggered this message will not show its ❌ in this PR unless the branch is within this repository.
Thus, you should make sure that this comment has been addressed before encouraging the merge bot to merge this PR.

Please sign in to comment.