From 9dd06ea8ed663e25fa91a267bbd76a812d860fe5 Mon Sep 17 00:00:00 2001 From: Carlos Zamora Date: Mon, 6 Apr 2020 16:23:35 -0700 Subject: [PATCH 1/2] Add 'copyFormatting' global setting --- doc/cascadia/SettingsSchema.md | 1 + doc/cascadia/profiles.schema.json | 5 ++++ .../TerminalApp/GlobalAppSettings.cpp | 16 ++++++++++ src/cascadia/TerminalApp/GlobalAppSettings.h | 4 +++ src/cascadia/TerminalControl/TermControl.cpp | 29 +++++++++++-------- .../TerminalSettings/IControlSettings.idl | 1 + .../TerminalSettings/TerminalSettings.cpp | 11 +++++++ .../TerminalSettings/terminalsettings.h | 3 ++ .../UnitTests_TerminalCore/MockTermSettings.h | 3 ++ 9 files changed, 61 insertions(+), 12 deletions(-) diff --git a/doc/cascadia/SettingsSchema.md b/doc/cascadia/SettingsSchema.md index 14c83aeb5ca..6522b036504 100644 --- a/doc/cascadia/SettingsSchema.md +++ b/doc/cascadia/SettingsSchema.md @@ -7,6 +7,7 @@ Properties listed below affect the entire window, regardless of the profile sett | -------- | --------- | ---- | ------- | ----------- | | `alwaysShowTabs` | _Required_ | Boolean | `true` | When set to `true`, tabs are always displayed. When set to `false` and `showTabsInTitlebar` is set to `false`, tabs only appear after typing Ctrl + T. | | `copyOnSelect` | Optional | Boolean | `false` | When set to `true`, a selection is immediately copied to your clipboard upon creation. When set to `false`, the selection persists and awaits further action. | +| `copyFormatting` | Optional | Boolean | `false` | When set to `true`, the color and font formatting of selected text is also copied to your clipboard. When set to `false`, only plain text is copied to your clipboard. | | `defaultProfile` | _Required_ | String | PowerShell guid | Sets the default profile. Opens by typing Ctrl + T or by clicking the '+' icon. The guid of the desired default profile is used as the value. | | `initialCols` | _Required_ | Integer | `120` | The number of columns displayed in the window upon first load. | | `initialPosition` | Optional | String | `","` | The position of the top left corner of the window upon first load. On a system with multiple displays, these coordinates are relative to the top left of the primary display. If `launchMode` is set to `"maximized"`, the window will be maximized on the monitor specified by those coordinates. | diff --git a/doc/cascadia/profiles.schema.json b/doc/cascadia/profiles.schema.json index 356a474ab40..db6a7e894e3 100644 --- a/doc/cascadia/profiles.schema.json +++ b/doc/cascadia/profiles.schema.json @@ -277,6 +277,11 @@ "description": "When set to true, a selection is immediately copied to your clipboard upon creation. When set to false, the selection persists and awaits further action.", "type": "boolean" }, + "copyFormatting": { + "default": false, + "description": "When set to `true`, the color and font formatting of selected text is also copied to your clipboard. When set to `false`, only plain text is copied to your clipboard.", + "type": "boolean" + }, "defaultProfile": { "$ref": "#/definitions/ProfileGuid", "description": "Sets the default profile. Opens by clicking the '+' icon or typing the key binding assigned to 'newTab'. The guid of the desired default profile is used as the value." diff --git a/src/cascadia/TerminalApp/GlobalAppSettings.cpp b/src/cascadia/TerminalApp/GlobalAppSettings.cpp index 7d6238cb076..4dd8722cd55 100644 --- a/src/cascadia/TerminalApp/GlobalAppSettings.cpp +++ b/src/cascadia/TerminalApp/GlobalAppSettings.cpp @@ -32,6 +32,7 @@ static constexpr std::wstring_view TitleLengthTabWidthModeValue{ L"titleLength" static constexpr std::string_view ShowTabsInTitlebarKey{ "showTabsInTitlebar" }; static constexpr std::string_view WordDelimitersKey{ "wordDelimiters" }; static constexpr std::string_view CopyOnSelectKey{ "copyOnSelect" }; +static constexpr std::string_view CopyFormattingKey{ "copyFormatting" }; static constexpr std::string_view LaunchModeKey{ "launchMode" }; static constexpr std::string_view ConfirmCloseAllKey{ "confirmCloseAllTabs" }; static constexpr std::string_view SnapToGridOnResizeKey{ "snapToGridOnResize" }; @@ -67,6 +68,7 @@ GlobalAppSettings::GlobalAppSettings() : _tabWidthMode{ TabViewWidthMode::Equal }, _wordDelimiters{ DEFAULT_WORD_DELIMITERS }, _copyOnSelect{ false }, + _copyFormatting{ false }, _launchMode{ LaunchMode::DefaultMode }, _debugFeatures{ debugFeaturesDefault } { @@ -161,6 +163,16 @@ void GlobalAppSettings::SetCopyOnSelect(const bool copyOnSelect) noexcept _copyOnSelect = copyOnSelect; } +bool GlobalAppSettings::GetCopyFormatting() const noexcept +{ + return _copyFormatting; +} + +void GlobalAppSettings::SetCopyFormatting(const bool copyFormatting) noexcept +{ + _copyFormatting = copyFormatting; +} + LaunchMode GlobalAppSettings::GetLaunchMode() const noexcept { return _launchMode; @@ -223,6 +235,7 @@ void GlobalAppSettings::ApplyToSettings(TerminalSettings& settings) const noexce settings.WordDelimiters(_wordDelimiters); settings.CopyOnSelect(_copyOnSelect); + settings.CopyFormatting(_copyFormatting); } // Method Description: @@ -245,6 +258,7 @@ Json::Value GlobalAppSettings::ToJson() const jsonObject[JsonKey(ShowTabsInTitlebarKey)] = _showTabsInTitlebar; jsonObject[JsonKey(WordDelimitersKey)] = winrt::to_string(_wordDelimiters); jsonObject[JsonKey(CopyOnSelectKey)] = _copyOnSelect; + jsonObject[JsonKey(CopyFormattingKey)] = _copyFormatting; jsonObject[JsonKey(LaunchModeKey)] = winrt::to_string(_SerializeLaunchMode(_launchMode)); jsonObject[JsonKey(ThemeKey)] = winrt::to_string(_SerializeTheme(_theme)); jsonObject[JsonKey(TabWidthModeKey)] = winrt::to_string(_SerializeTabWidthMode(_tabWidthMode)); @@ -311,6 +325,8 @@ void GlobalAppSettings::LayerJson(const Json::Value& json) JsonUtils::GetBool(json, CopyOnSelectKey, _copyOnSelect); + JsonUtils::GetBool(json, CopyFormattingKey, _copyFormatting); + if (auto launchMode{ json[JsonKey(LaunchModeKey)] }) { _launchMode = _ParseLaunchMode(GetWstringFromJson(launchMode)); diff --git a/src/cascadia/TerminalApp/GlobalAppSettings.h b/src/cascadia/TerminalApp/GlobalAppSettings.h index 076b9129471..d8acdce6d4e 100644 --- a/src/cascadia/TerminalApp/GlobalAppSettings.h +++ b/src/cascadia/TerminalApp/GlobalAppSettings.h @@ -68,6 +68,9 @@ class TerminalApp::GlobalAppSettings final bool GetCopyOnSelect() const noexcept; void SetCopyOnSelect(const bool copyOnSelect) noexcept; + bool GetCopyFormatting() const noexcept; + void SetCopyFormatting(const bool copyFormatting) noexcept; + std::optional GetInitialX() const noexcept; std::optional GetInitialY() const noexcept; @@ -110,6 +113,7 @@ class TerminalApp::GlobalAppSettings final bool _showTabsInTitlebar; std::wstring _wordDelimiters; bool _copyOnSelect; + bool _copyFormatting; winrt::Windows::UI::Xaml::ElementTheme _theme; winrt::Microsoft::UI::Xaml::Controls::TabViewWidthMode _tabWidthMode; diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index 2ce794eecc1..13610a8b7ad 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -1908,18 +1908,23 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation textData += text; } - // convert text to HTML format - const auto htmlData = TextBuffer::GenHTML(bufferData, - _actualFont.GetUnscaledSize().Y, - _actualFont.GetFaceName(), - _settings.DefaultBackground(), - "Windows Terminal"); - - // convert to RTF format - const auto rtfData = TextBuffer::GenRTF(bufferData, - _actualFont.GetUnscaledSize().Y, - _actualFont.GetFaceName(), - _settings.DefaultBackground()); + std::string htmlData{}; + std::string rtfData{}; + if (_settings.CopyFormatting()) + { + // convert text to HTML format + htmlData = TextBuffer::GenHTML(bufferData, + _actualFont.GetUnscaledSize().Y, + _actualFont.GetFaceName(), + _settings.DefaultBackground(), + "Windows Terminal"); + + // convert to RTF format + rtfData = TextBuffer::GenRTF(bufferData, + _actualFont.GetUnscaledSize().Y, + _actualFont.GetFaceName(), + _settings.DefaultBackground()); + } if (!_settings.CopyOnSelect()) { diff --git a/src/cascadia/TerminalSettings/IControlSettings.idl b/src/cascadia/TerminalSettings/IControlSettings.idl index 4f40c32691b..3a974c3c15b 100644 --- a/src/cascadia/TerminalSettings/IControlSettings.idl +++ b/src/cascadia/TerminalSettings/IControlSettings.idl @@ -39,6 +39,7 @@ namespace Microsoft.Terminal.Settings IKeyBindings KeyBindings; Boolean CopyOnSelect; + Boolean CopyFormatting; String Commandline; String StartingDirectory; diff --git a/src/cascadia/TerminalSettings/TerminalSettings.cpp b/src/cascadia/TerminalSettings/TerminalSettings.cpp index 46fe1b45d36..9a66038db1b 100644 --- a/src/cascadia/TerminalSettings/TerminalSettings.cpp +++ b/src/cascadia/TerminalSettings/TerminalSettings.cpp @@ -28,6 +28,7 @@ namespace winrt::Microsoft::Terminal::Settings::implementation _cursorHeight{ DEFAULT_CURSOR_HEIGHT }, _wordDelimiters{ DEFAULT_WORD_DELIMITERS }, _copyOnSelect{ false }, + _copyFormatting{ false }, _profileName{}, _useAcrylic{ false }, _tintOpacity{ 0.5 }, @@ -197,6 +198,16 @@ namespace winrt::Microsoft::Terminal::Settings::implementation _copyOnSelect = value; } + bool TerminalSettings::CopyFormatting() noexcept + { + return _copyFormatting; + } + + void TerminalSettings::CopyFormatting(bool value) noexcept + { + _copyFormatting = value; + } + void TerminalSettings::ProfileName(hstring const& value) { _profileName = value; diff --git a/src/cascadia/TerminalSettings/terminalsettings.h b/src/cascadia/TerminalSettings/terminalsettings.h index e78fa00901e..e2f1ed4acfb 100644 --- a/src/cascadia/TerminalSettings/terminalsettings.h +++ b/src/cascadia/TerminalSettings/terminalsettings.h @@ -53,6 +53,8 @@ namespace winrt::Microsoft::Terminal::Settings::implementation void WordDelimiters(hstring const& value); bool CopyOnSelect() noexcept; void CopyOnSelect(bool value) noexcept; + bool CopyFormatting() noexcept; + void CopyFormatting(bool value) noexcept; // ------------------------ End of Core Settings ----------------------- hstring ProfileName(); @@ -134,6 +136,7 @@ namespace winrt::Microsoft::Terminal::Settings::implementation winrt::Windows::UI::Xaml::HorizontalAlignment _backgroundImageHorizontalAlignment; winrt::Windows::UI::Xaml::VerticalAlignment _backgroundImageVerticalAlignment; bool _copyOnSelect; + bool _copyFormatting; hstring _commandline; hstring _startingDir; hstring _startingTitle; diff --git a/src/cascadia/UnitTests_TerminalCore/MockTermSettings.h b/src/cascadia/UnitTests_TerminalCore/MockTermSettings.h index 6866caea39a..6fbb6aed252 100644 --- a/src/cascadia/UnitTests_TerminalCore/MockTermSettings.h +++ b/src/cascadia/UnitTests_TerminalCore/MockTermSettings.h @@ -34,6 +34,7 @@ namespace TerminalCoreUnitTests uint32_t CursorHeight() { return 42UL; } winrt::hstring WordDelimiters() { return winrt::hstring(DEFAULT_WORD_DELIMITERS); } bool CopyOnSelect() { return _copyOnSelect; } + bool CopyFormatting() { return _copyFormatting; } winrt::hstring StartingTitle() { return _startingTitle; } bool SuppressApplicationTitle() { return _suppressApplicationTitle; } uint32_t SelectionBackground() { return COLOR_WHITE; } @@ -54,6 +55,7 @@ namespace TerminalCoreUnitTests void CursorHeight(uint32_t) {} void WordDelimiters(winrt::hstring) {} void CopyOnSelect(bool copyOnSelect) { _copyOnSelect = copyOnSelect; } + void CopyFormatting(bool copyFormatting) { _copyFormatting = copyFormatting; } void StartingTitle(winrt::hstring const& value) { _startingTitle = value; } void SuppressApplicationTitle(bool suppressApplicationTitle) { _suppressApplicationTitle = suppressApplicationTitle; } void SelectionBackground(uint32_t) {} @@ -66,6 +68,7 @@ namespace TerminalCoreUnitTests int32_t _initialRows; int32_t _initialCols; bool _copyOnSelect{ false }; + bool _copyFormatting{ false }; bool _suppressApplicationTitle{ false }; winrt::hstring _startingTitle; }; From 147c8df8ae30bcd6f0e1c4b5035884dc42104754 Mon Sep 17 00:00:00 2001 From: Carlos Zamora Date: Thu, 9 Apr 2020 10:09:05 -0700 Subject: [PATCH 2/2] move copyFormatting check to TermApp --- .../TerminalApp/GlobalAppSettings.cpp | 6 ---- src/cascadia/TerminalApp/GlobalAppSettings.h | 1 - src/cascadia/TerminalApp/TerminalPage.cpp | 23 ++++++++------- src/cascadia/TerminalControl/TermControl.cpp | 29 ++++++++----------- .../TerminalSettings/IControlSettings.idl | 1 - .../TerminalSettings/TerminalSettings.cpp | 11 ------- .../TerminalSettings/terminalsettings.h | 3 -- .../UnitTests_TerminalCore/MockTermSettings.h | 3 -- 8 files changed, 25 insertions(+), 52 deletions(-) diff --git a/src/cascadia/TerminalApp/GlobalAppSettings.cpp b/src/cascadia/TerminalApp/GlobalAppSettings.cpp index 4dd8722cd55..62c814779f1 100644 --- a/src/cascadia/TerminalApp/GlobalAppSettings.cpp +++ b/src/cascadia/TerminalApp/GlobalAppSettings.cpp @@ -168,11 +168,6 @@ bool GlobalAppSettings::GetCopyFormatting() const noexcept return _copyFormatting; } -void GlobalAppSettings::SetCopyFormatting(const bool copyFormatting) noexcept -{ - _copyFormatting = copyFormatting; -} - LaunchMode GlobalAppSettings::GetLaunchMode() const noexcept { return _launchMode; @@ -235,7 +230,6 @@ void GlobalAppSettings::ApplyToSettings(TerminalSettings& settings) const noexce settings.WordDelimiters(_wordDelimiters); settings.CopyOnSelect(_copyOnSelect); - settings.CopyFormatting(_copyFormatting); } // Method Description: diff --git a/src/cascadia/TerminalApp/GlobalAppSettings.h b/src/cascadia/TerminalApp/GlobalAppSettings.h index d8acdce6d4e..44435c3d577 100644 --- a/src/cascadia/TerminalApp/GlobalAppSettings.h +++ b/src/cascadia/TerminalApp/GlobalAppSettings.h @@ -69,7 +69,6 @@ class TerminalApp::GlobalAppSettings final void SetCopyOnSelect(const bool copyOnSelect) noexcept; bool GetCopyFormatting() const noexcept; - void SetCopyFormatting(const bool copyFormatting) noexcept; std::optional GetInitialX() const noexcept; diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index 5df4710db4d..a09f0cc8461 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -1310,18 +1310,21 @@ namespace winrt::TerminalApp::implementation // copy text to dataPack dataPack.SetText(copiedData.Text()); - // copy html to dataPack - const auto htmlData = copiedData.Html(); - if (!htmlData.empty()) + if (_settings->GlobalSettings().GetCopyFormatting()) { - dataPack.SetHtmlFormat(htmlData); - } + // copy html to dataPack + const auto htmlData = copiedData.Html(); + if (!htmlData.empty()) + { + dataPack.SetHtmlFormat(htmlData); + } - // copy rtf data to dataPack - const auto rtfData = copiedData.Rtf(); - if (!rtfData.empty()) - { - dataPack.SetRtf(rtfData); + // copy rtf data to dataPack + const auto rtfData = copiedData.Rtf(); + if (!rtfData.empty()) + { + dataPack.SetRtf(rtfData); + } } try diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index 13610a8b7ad..2ce794eecc1 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -1908,23 +1908,18 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation textData += text; } - std::string htmlData{}; - std::string rtfData{}; - if (_settings.CopyFormatting()) - { - // convert text to HTML format - htmlData = TextBuffer::GenHTML(bufferData, - _actualFont.GetUnscaledSize().Y, - _actualFont.GetFaceName(), - _settings.DefaultBackground(), - "Windows Terminal"); - - // convert to RTF format - rtfData = TextBuffer::GenRTF(bufferData, - _actualFont.GetUnscaledSize().Y, - _actualFont.GetFaceName(), - _settings.DefaultBackground()); - } + // convert text to HTML format + const auto htmlData = TextBuffer::GenHTML(bufferData, + _actualFont.GetUnscaledSize().Y, + _actualFont.GetFaceName(), + _settings.DefaultBackground(), + "Windows Terminal"); + + // convert to RTF format + const auto rtfData = TextBuffer::GenRTF(bufferData, + _actualFont.GetUnscaledSize().Y, + _actualFont.GetFaceName(), + _settings.DefaultBackground()); if (!_settings.CopyOnSelect()) { diff --git a/src/cascadia/TerminalSettings/IControlSettings.idl b/src/cascadia/TerminalSettings/IControlSettings.idl index 3a974c3c15b..4f40c32691b 100644 --- a/src/cascadia/TerminalSettings/IControlSettings.idl +++ b/src/cascadia/TerminalSettings/IControlSettings.idl @@ -39,7 +39,6 @@ namespace Microsoft.Terminal.Settings IKeyBindings KeyBindings; Boolean CopyOnSelect; - Boolean CopyFormatting; String Commandline; String StartingDirectory; diff --git a/src/cascadia/TerminalSettings/TerminalSettings.cpp b/src/cascadia/TerminalSettings/TerminalSettings.cpp index 9a66038db1b..46fe1b45d36 100644 --- a/src/cascadia/TerminalSettings/TerminalSettings.cpp +++ b/src/cascadia/TerminalSettings/TerminalSettings.cpp @@ -28,7 +28,6 @@ namespace winrt::Microsoft::Terminal::Settings::implementation _cursorHeight{ DEFAULT_CURSOR_HEIGHT }, _wordDelimiters{ DEFAULT_WORD_DELIMITERS }, _copyOnSelect{ false }, - _copyFormatting{ false }, _profileName{}, _useAcrylic{ false }, _tintOpacity{ 0.5 }, @@ -198,16 +197,6 @@ namespace winrt::Microsoft::Terminal::Settings::implementation _copyOnSelect = value; } - bool TerminalSettings::CopyFormatting() noexcept - { - return _copyFormatting; - } - - void TerminalSettings::CopyFormatting(bool value) noexcept - { - _copyFormatting = value; - } - void TerminalSettings::ProfileName(hstring const& value) { _profileName = value; diff --git a/src/cascadia/TerminalSettings/terminalsettings.h b/src/cascadia/TerminalSettings/terminalsettings.h index e2f1ed4acfb..e78fa00901e 100644 --- a/src/cascadia/TerminalSettings/terminalsettings.h +++ b/src/cascadia/TerminalSettings/terminalsettings.h @@ -53,8 +53,6 @@ namespace winrt::Microsoft::Terminal::Settings::implementation void WordDelimiters(hstring const& value); bool CopyOnSelect() noexcept; void CopyOnSelect(bool value) noexcept; - bool CopyFormatting() noexcept; - void CopyFormatting(bool value) noexcept; // ------------------------ End of Core Settings ----------------------- hstring ProfileName(); @@ -136,7 +134,6 @@ namespace winrt::Microsoft::Terminal::Settings::implementation winrt::Windows::UI::Xaml::HorizontalAlignment _backgroundImageHorizontalAlignment; winrt::Windows::UI::Xaml::VerticalAlignment _backgroundImageVerticalAlignment; bool _copyOnSelect; - bool _copyFormatting; hstring _commandline; hstring _startingDir; hstring _startingTitle; diff --git a/src/cascadia/UnitTests_TerminalCore/MockTermSettings.h b/src/cascadia/UnitTests_TerminalCore/MockTermSettings.h index 6fbb6aed252..6866caea39a 100644 --- a/src/cascadia/UnitTests_TerminalCore/MockTermSettings.h +++ b/src/cascadia/UnitTests_TerminalCore/MockTermSettings.h @@ -34,7 +34,6 @@ namespace TerminalCoreUnitTests uint32_t CursorHeight() { return 42UL; } winrt::hstring WordDelimiters() { return winrt::hstring(DEFAULT_WORD_DELIMITERS); } bool CopyOnSelect() { return _copyOnSelect; } - bool CopyFormatting() { return _copyFormatting; } winrt::hstring StartingTitle() { return _startingTitle; } bool SuppressApplicationTitle() { return _suppressApplicationTitle; } uint32_t SelectionBackground() { return COLOR_WHITE; } @@ -55,7 +54,6 @@ namespace TerminalCoreUnitTests void CursorHeight(uint32_t) {} void WordDelimiters(winrt::hstring) {} void CopyOnSelect(bool copyOnSelect) { _copyOnSelect = copyOnSelect; } - void CopyFormatting(bool copyFormatting) { _copyFormatting = copyFormatting; } void StartingTitle(winrt::hstring const& value) { _startingTitle = value; } void SuppressApplicationTitle(bool suppressApplicationTitle) { _suppressApplicationTitle = suppressApplicationTitle; } void SelectionBackground(uint32_t) {} @@ -68,7 +66,6 @@ namespace TerminalCoreUnitTests int32_t _initialRows; int32_t _initialCols; bool _copyOnSelect{ false }; - bool _copyFormatting{ false }; bool _suppressApplicationTitle{ false }; winrt::hstring _startingTitle; };