2019-05-03 00:29:04 +02:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
|
|
|
// Licensed under the MIT license.
|
|
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
#include "GlobalAppSettings.h"
|
|
|
|
#include "../../types/inc/Utils.hpp"
|
|
|
|
#include "../../inc/DefaultSettings.h"
|
2019-06-04 23:55:27 +02:00
|
|
|
#include "AppKeyBindingsSerialization.h"
|
|
|
|
#include "Utils.h"
|
2019-05-03 00:29:04 +02:00
|
|
|
|
|
|
|
using namespace TerminalApp;
|
|
|
|
using namespace winrt::Microsoft::Terminal::Settings;
|
|
|
|
using namespace winrt::TerminalApp;
|
|
|
|
using namespace winrt::Windows::Data::Json;
|
|
|
|
using namespace winrt::Windows::UI::Xaml;
|
|
|
|
using namespace ::Microsoft::Console;
|
|
|
|
|
2019-06-04 23:55:27 +02:00
|
|
|
static constexpr std::string_view KeybindingsKey{ "keybindings" };
|
|
|
|
static constexpr std::string_view DefaultProfileKey{ "defaultProfile" };
|
|
|
|
static constexpr std::string_view AlwaysShowTabsKey{ "alwaysShowTabs" };
|
|
|
|
static constexpr std::string_view InitialRowsKey{ "initialRows" };
|
|
|
|
static constexpr std::string_view InitialColsKey{ "initialCols" };
|
|
|
|
static constexpr std::string_view ShowTitleInTitlebarKey{ "showTerminalTitleInTitlebar" };
|
|
|
|
static constexpr std::string_view RequestedThemeKey{ "requestedTheme" };
|
|
|
|
static constexpr std::string_view ShowTabsInTitlebarKey{ "showTabsInTitlebar" };
|
2019-07-12 01:06:18 +02:00
|
|
|
static constexpr std::string_view WordDelimitersKey{ "wordDelimiters" };
|
2019-05-03 00:29:04 +02:00
|
|
|
|
2019-06-04 23:55:27 +02:00
|
|
|
static constexpr std::wstring_view LightThemeValue{ L"light" };
|
|
|
|
static constexpr std::wstring_view DarkThemeValue{ L"dark" };
|
|
|
|
static constexpr std::wstring_view SystemThemeValue{ L"system" };
|
2019-05-03 00:29:04 +02:00
|
|
|
|
|
|
|
GlobalAppSettings::GlobalAppSettings() :
|
|
|
|
_keybindings{},
|
|
|
|
_colorSchemes{},
|
|
|
|
_defaultProfile{},
|
2019-05-24 00:02:32 +02:00
|
|
|
_alwaysShowTabs{ true },
|
2019-05-03 00:29:04 +02:00
|
|
|
_initialRows{ DEFAULT_ROWS },
|
|
|
|
_initialCols{ DEFAULT_COLS },
|
|
|
|
_showTitleInTitlebar{ true },
|
2019-05-24 00:02:32 +02:00
|
|
|
_showTabsInTitlebar{ true },
|
2019-07-12 01:06:18 +02:00
|
|
|
_requestedTheme{ ElementTheme::Default },
|
|
|
|
_wordDelimiters{ DEFAULT_WORD_DELIMITERS }
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalAppSettings::~GlobalAppSettings()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<ColorScheme>& GlobalAppSettings::GetColorSchemes() const noexcept
|
|
|
|
{
|
|
|
|
return _colorSchemes;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ColorScheme>& GlobalAppSettings::GetColorSchemes() noexcept
|
|
|
|
{
|
|
|
|
return _colorSchemes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalAppSettings::SetDefaultProfile(const GUID defaultProfile) noexcept
|
|
|
|
{
|
|
|
|
_defaultProfile = defaultProfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
GUID GlobalAppSettings::GetDefaultProfile() const noexcept
|
|
|
|
{
|
|
|
|
return _defaultProfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
AppKeyBindings GlobalAppSettings::GetKeybindings() const noexcept
|
|
|
|
{
|
|
|
|
return _keybindings;
|
|
|
|
}
|
|
|
|
|
2019-05-21 16:26:04 +02:00
|
|
|
void GlobalAppSettings::SetKeybindings(winrt::TerminalApp::AppKeyBindings newBindings) noexcept
|
|
|
|
{
|
|
|
|
_keybindings = newBindings;
|
|
|
|
}
|
|
|
|
|
2019-05-03 00:29:04 +02:00
|
|
|
bool GlobalAppSettings::GetAlwaysShowTabs() const noexcept
|
|
|
|
{
|
|
|
|
return _alwaysShowTabs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalAppSettings::SetAlwaysShowTabs(const bool showTabs) noexcept
|
|
|
|
{
|
|
|
|
_alwaysShowTabs = showTabs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalAppSettings::GetShowTitleInTitlebar() const noexcept
|
|
|
|
{
|
|
|
|
return _showTitleInTitlebar;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalAppSettings::SetShowTitleInTitlebar(const bool showTitleInTitlebar) noexcept
|
|
|
|
{
|
|
|
|
_showTitleInTitlebar = showTitleInTitlebar;
|
|
|
|
}
|
|
|
|
|
|
|
|
ElementTheme GlobalAppSettings::GetRequestedTheme() const noexcept
|
|
|
|
{
|
|
|
|
return _requestedTheme;
|
|
|
|
}
|
|
|
|
|
2019-05-20 22:54:19 +02:00
|
|
|
void GlobalAppSettings::SetRequestedTheme(const ElementTheme requestedTheme) noexcept
|
|
|
|
{
|
|
|
|
_requestedTheme = requestedTheme;
|
|
|
|
}
|
2019-05-03 00:29:04 +02:00
|
|
|
|
2019-07-12 01:06:18 +02:00
|
|
|
std::wstring GlobalAppSettings::GetWordDelimiters() const noexcept
|
|
|
|
{
|
|
|
|
return _wordDelimiters;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalAppSettings::SetWordDelimiters(const std::wstring wordDelimiters) noexcept
|
|
|
|
{
|
|
|
|
_wordDelimiters = wordDelimiters;
|
|
|
|
}
|
|
|
|
|
2019-05-03 00:29:04 +02:00
|
|
|
#pragma region ExperimentalSettings
|
|
|
|
bool GlobalAppSettings::GetShowTabsInTitlebar() const noexcept
|
|
|
|
{
|
|
|
|
return _showTabsInTitlebar;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalAppSettings::SetShowTabsInTitlebar(const bool showTabsInTitlebar) noexcept
|
|
|
|
{
|
|
|
|
_showTabsInTitlebar = showTabsInTitlebar;
|
|
|
|
}
|
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
// Method Description:
|
|
|
|
// - Applies appropriate settings from the globals into the given TerminalSettings.
|
|
|
|
// Arguments:
|
|
|
|
// - settings: a TerminalSettings object to add global property values to.
|
|
|
|
// Return Value:
|
|
|
|
// - <none>
|
|
|
|
void GlobalAppSettings::ApplyToSettings(TerminalSettings& settings) const noexcept
|
|
|
|
{
|
|
|
|
settings.KeyBindings(GetKeybindings());
|
|
|
|
settings.InitialRows(_initialRows);
|
|
|
|
settings.InitialCols(_initialCols);
|
2019-07-12 01:06:18 +02:00
|
|
|
settings.WordDelimiters(_wordDelimiters);
|
2019-05-03 00:29:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Method Description:
|
|
|
|
// - Serialize this object to a JsonObject.
|
|
|
|
// Arguments:
|
|
|
|
// - <none>
|
|
|
|
// Return Value:
|
|
|
|
// - a JsonObject which is an equivalent serialization of this object.
|
2019-06-04 23:55:27 +02:00
|
|
|
Json::Value GlobalAppSettings::ToJson() const
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
2019-06-04 23:55:27 +02:00
|
|
|
Json::Value jsonObject;
|
2019-05-03 00:29:04 +02:00
|
|
|
|
2019-06-04 23:55:27 +02:00
|
|
|
jsonObject[JsonKey(DefaultProfileKey)] = winrt::to_string(Utils::GuidToString(_defaultProfile));
|
|
|
|
jsonObject[JsonKey(InitialRowsKey)] = _initialRows;
|
|
|
|
jsonObject[JsonKey(InitialColsKey)] = _initialCols;
|
|
|
|
jsonObject[JsonKey(AlwaysShowTabsKey)] = _alwaysShowTabs;
|
|
|
|
jsonObject[JsonKey(ShowTitleInTitlebarKey)] = _showTitleInTitlebar;
|
|
|
|
jsonObject[JsonKey(ShowTabsInTitlebarKey)] = _showTabsInTitlebar;
|
2019-07-12 01:06:18 +02:00
|
|
|
jsonObject[JsonKey(WordDelimitersKey)] = winrt::to_string(_wordDelimiters);
|
2019-06-04 23:55:27 +02:00
|
|
|
jsonObject[JsonKey(RequestedThemeKey)] = winrt::to_string(_SerializeTheme(_requestedTheme));
|
|
|
|
jsonObject[JsonKey(KeybindingsKey)] = AppKeyBindingsSerialization::ToJson(_keybindings);
|
2019-05-21 16:26:04 +02:00
|
|
|
|
2019-05-03 00:29:04 +02:00
|
|
|
return jsonObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method Description:
|
|
|
|
// - Create a new instance of this class from a serialized JsonObject.
|
|
|
|
// Arguments:
|
|
|
|
// - json: an object which should be a serialization of a GlobalAppSettings object.
|
|
|
|
// Return Value:
|
|
|
|
// - a new GlobalAppSettings instance created from the values in `json`
|
2019-06-04 23:55:27 +02:00
|
|
|
GlobalAppSettings GlobalAppSettings::FromJson(const Json::Value& json)
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
|
|
|
GlobalAppSettings result{};
|
|
|
|
|
2019-06-04 23:55:27 +02:00
|
|
|
if (auto defaultProfile{ json[JsonKey(DefaultProfileKey)] })
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
2019-06-04 23:55:27 +02:00
|
|
|
auto guid = Utils::GuidFromString(GetWstringFromJson(defaultProfile));
|
2019-05-03 00:29:04 +02:00
|
|
|
result._defaultProfile = guid;
|
|
|
|
}
|
|
|
|
|
2019-06-04 23:55:27 +02:00
|
|
|
if (auto alwaysShowTabs{ json[JsonKey(AlwaysShowTabsKey)] })
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
2019-06-04 23:55:27 +02:00
|
|
|
result._alwaysShowTabs = alwaysShowTabs.asBool();
|
2019-05-03 00:29:04 +02:00
|
|
|
}
|
2019-06-04 23:55:27 +02:00
|
|
|
if (auto initialRows{ json[JsonKey(InitialRowsKey)] })
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
2019-06-04 23:55:27 +02:00
|
|
|
result._initialRows = initialRows.asInt();
|
2019-05-03 00:29:04 +02:00
|
|
|
}
|
2019-06-04 23:55:27 +02:00
|
|
|
if (auto initialCols{ json[JsonKey(InitialColsKey)] })
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
2019-06-04 23:55:27 +02:00
|
|
|
result._initialCols = initialCols.asInt();
|
2019-05-03 00:29:04 +02:00
|
|
|
}
|
|
|
|
|
2019-06-04 23:55:27 +02:00
|
|
|
if (auto showTitleInTitlebar{ json[JsonKey(ShowTitleInTitlebarKey)] })
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
2019-06-04 23:55:27 +02:00
|
|
|
result._showTitleInTitlebar = showTitleInTitlebar.asBool();
|
2019-05-03 00:29:04 +02:00
|
|
|
}
|
|
|
|
|
2019-06-04 23:55:27 +02:00
|
|
|
if (auto showTabsInTitlebar{ json[JsonKey(ShowTabsInTitlebarKey)] })
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
2019-06-04 23:55:27 +02:00
|
|
|
result._showTabsInTitlebar = showTabsInTitlebar.asBool();
|
2019-05-03 00:29:04 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 01:06:18 +02:00
|
|
|
if (auto wordDelimiters{ json[JsonKey(WordDelimitersKey)] })
|
|
|
|
{
|
|
|
|
result._wordDelimiters = GetWstringFromJson(wordDelimiters);
|
|
|
|
}
|
|
|
|
|
2019-06-04 23:55:27 +02:00
|
|
|
if (auto requestedTheme{ json[JsonKey(RequestedThemeKey)] })
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
2019-06-04 23:55:27 +02:00
|
|
|
result._requestedTheme = _ParseTheme(GetWstringFromJson(requestedTheme));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto keybindings{ json[JsonKey(KeybindingsKey)] })
|
|
|
|
{
|
|
|
|
result._keybindings = AppKeyBindingsSerialization::FromJson(keybindings);
|
2019-05-03 00:29:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method Description:
|
|
|
|
// - Helper function for converting a user-specified cursor style corresponding
|
|
|
|
// CursorStyle enum value
|
|
|
|
// Arguments:
|
|
|
|
// - themeString: The string value from the settings file to parse
|
|
|
|
// Return Value:
|
|
|
|
// - The corresponding enum value which maps to the string provided by the user
|
|
|
|
ElementTheme GlobalAppSettings::_ParseTheme(const std::wstring& themeString) noexcept
|
|
|
|
{
|
2019-06-04 23:55:27 +02:00
|
|
|
if (themeString == LightThemeValue)
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
|
|
|
return ElementTheme::Light;
|
|
|
|
}
|
2019-06-04 23:55:27 +02:00
|
|
|
else if (themeString == DarkThemeValue)
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
|
|
|
return ElementTheme::Dark;
|
|
|
|
}
|
2019-06-04 23:55:27 +02:00
|
|
|
// default behavior for invalid data or SystemThemeValue
|
2019-05-03 00:29:04 +02:00
|
|
|
return ElementTheme::Default;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method Description:
|
2019-05-21 08:15:44 +02:00
|
|
|
// - Helper function for converting a CursorStyle to its corresponding string
|
2019-05-03 00:29:04 +02:00
|
|
|
// value.
|
|
|
|
// Arguments:
|
|
|
|
// - theme: The enum value to convert to a string.
|
|
|
|
// Return Value:
|
|
|
|
// - The string value for the given CursorStyle
|
2019-05-22 00:39:26 +02:00
|
|
|
std::wstring_view GlobalAppSettings::_SerializeTheme(const ElementTheme theme) noexcept
|
2019-05-03 00:29:04 +02:00
|
|
|
{
|
|
|
|
switch (theme)
|
|
|
|
{
|
2019-06-11 22:27:09 +02:00
|
|
|
case ElementTheme::Light:
|
|
|
|
return LightThemeValue;
|
|
|
|
case ElementTheme::Dark:
|
|
|
|
return DarkThemeValue;
|
|
|
|
default:
|
|
|
|
return SystemThemeValue;
|
2019-05-03 00:29:04 +02:00
|
|
|
}
|
|
|
|
}
|