Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copyFormatting keybinding arg and array support #6004

Merged
merged 27 commits into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0395d46
allow copyFormatting to be an array
carlos-zamora May 15, 2020
8a52b23
add copyFormatting as a copy arg
carlos-zamora May 15, 2020
0bb55eb
add keybinding arg to copy
carlos-zamora May 15, 2020
d86c1ea
update docs and fix a few comments
carlos-zamora May 19, 2020
322da3b
code format
carlos-zamora May 19, 2020
f2bfeda
change sentinel value to 0
carlos-zamora Jun 24, 2020
bb27289
attempt to use JsonUtilsNew (and fail :( )
carlos-zamora Jun 26, 2020
0532d0d
Merge branch 'master' into dev/cazamor/copy-formatting-arg
carlos-zamora Jul 2, 2020
0dc5b2a
PR comments (2 errors from JsonUtilsNew and WI_...)
carlos-zamora Jul 6, 2020
f1c93eb
Merge branch 'master' into dev/cazamor/copy-formatting-arg
carlos-zamora Jul 10, 2020
f95bdac
IT WORKS!!!!!!
carlos-zamora Jul 10, 2020
f72a443
I like default params too much...
carlos-zamora Jul 10, 2020
82e25a5
Merge branch 'master' into dev/cazamor/copy-formatting-arg
carlos-zamora Jul 13, 2020
708f586
merge master
carlos-zamora Jul 17, 2020
e7d0f2a
JsonUtilsNew is no more
carlos-zamora Jul 17, 2020
bf85092
a leap of faith (should work, but my machine is dumb)
carlos-zamora Jul 17, 2020
21856aa
better documentation surrounding format override
carlos-zamora Jul 20, 2020
bbe7d82
remove debugging vars
carlos-zamora Jul 20, 2020
b785989
dustin's PR comments
carlos-zamora Jul 21, 2020
260a98f
Merge branch 'master' into dev/cazamor/copy-formatting-arg
carlos-zamora Aug 7, 2020
1230b77
fix plain value, add generated name
carlos-zamora Aug 7, 2020
15e019f
remove plain and unnecessary resw changes
carlos-zamora Aug 10, 2020
2806285
befriend localization
carlos-zamora Aug 11, 2020
3661dc9
Merge branch 'master' into dev/cazamor/copy-formatting-arg
carlos-zamora Aug 11, 2020
bcb750f
clean up GenerateName
carlos-zamora Aug 11, 2020
06d2e6c
FlagMapper should use BaseEnumMapper's TypeDescription()
carlos-zamora Aug 11, 2020
339f65c
actually test it this time...
carlos-zamora Aug 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/cascadia/SettingsSchema.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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 <kbd>Ctrl</kbd> + <kbd>T</kbd>. |
| `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. |
| `copyFormatting` | Optional | Boolean, Array | `true` | 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. An array of specific formats can also be used. Supported array values include `html` and `rtf`. Plain text is always copied. |
| `largePasteWarning` | Optional | Boolean | `true` | When set to `true`, trying to paste text with more than 5 KiB of characters will display a warning asking you whether to continue or not with the paste. |
| `multiLinePasteWarning` | Optional | Boolean | `true` | When set to `true`, trying to paste text with a _new line_ character will display a warning asking you whether to continue or not with the paste. |
| `defaultProfile` | _Required_ | String | PowerShell guid | Sets the default profile. Opens by typing <kbd>Ctrl</kbd> + <kbd>T</kbd> or by clicking the '+' icon. The guid of the desired default profile is used as the value. |
Expand Down Expand Up @@ -126,7 +126,7 @@ For commands with arguments:
| `closePane` | Close the active pane. | | | |
| `closeTab` | Close the current tab. | | | |
| `closeWindow` | Close the current window and all tabs within it. | | | |
| `copy` | Copy the selected terminal content to your Windows Clipboard. | `singleLine` | boolean | When `true`, the copied content will be copied as a single line. When `false`, newlines persist from the selected text. |
| `copy` | Copy the selected terminal content to your Windows Clipboard. | 1. `singleLine`<br>2. `copyFormatting` | 1. boolean<br>2. boolean, array | 1. When `true`, the copied content will be copied as a single line. When `false`, newlines persist from the selected text.<br>2. 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. An array of specific formats can also be used. Supported array values include `html` and `rtf`. Plain text is always copied. Not setting this value inherits the behavior of the `copyFormatting` global setting. |
| `duplicateTab` | Make a copy and open the current tab. | | | |
| `find` | Open the search dialog box. | | | |
| `moveFocus` | Focus on a different pane depending on direction. | `direction`* | `left`, `right`, `up`, `down` | Direction in which the focus will move. |
Expand Down
42 changes: 40 additions & 2 deletions doc/cascadia/profiles.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,32 @@
],
"type": "string"
},
"CopyFormat": {
"oneOf": [
{
"type": "boolean"
},
{
"type": "array",
"items": {
"type": "string",
"enum": [
"html",
"rtf"
]
}
},
{
"type": "string",
"enum": [
"html",
"rtf",
"all",
"none"
]
}
]
},
"NewTerminalArgs": {
"properties": {
"commandline": {
Expand Down Expand Up @@ -152,6 +178,18 @@
"type": "boolean",
"default": false,
"description": "If true, the copied content will be copied as a single line (even if there are hard line breaks present in the text). If false, newlines persist from the selected text."
},
"copyFormatting": {
"default": null,
"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. An array of specific formats can also be used. Supported array values include `html` and `rtf`. Plain text is always copied. Not setting this value inherits the behavior of the `copyFormatting` global setting.",
"oneOf": [
{
"$ref": "#/definitions/CopyFormat"
},
{
"type": "null"
}
]
}
}
}
Expand Down Expand Up @@ -399,8 +437,8 @@
},
"copyFormatting": {
"default": true,
"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"
"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. An array of specific formats can also be used. Supported array values include `html` and `rtf`. Plain text is always copied.",
"$ref": "#/definitions/CopyFormat"
},
"largePasteWarning": {
"default": true,
Expand Down
36 changes: 34 additions & 2 deletions src/cascadia/TerminalApp/ActionArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <LibraryResources.h>

using namespace winrt::Microsoft::Terminal::TerminalControl;

namespace winrt::TerminalApp::implementation
{
winrt::hstring NewTerminalArgs::GenerateName() const
Expand Down Expand Up @@ -62,11 +64,41 @@ namespace winrt::TerminalApp::implementation

winrt::hstring CopyTextArgs::GenerateName() const
{
std::wstringstream ss;

if (_SingleLine)
{
return RS_(L"CopyTextAsSingleLineCommandKey");
ss << fmt::format(L"singleLine: {}, ", _SingleLine);
}

if (_CopyFormatting != nullptr)
{
ss << L"copyFormatting: ";
if (_CopyFormatting.Value() == CopyFormat::All)
{
ss << fmt::format(L"all, ");
carlos-zamora marked this conversation as resolved.
Show resolved Hide resolved
}
else if (_CopyFormatting.Value() == static_cast<CopyFormat>(0))
{
ss << fmt::format(L"none, ");
}
else
{
if (WI_IsFlagSet(_CopyFormatting.Value(), CopyFormat::HTML))
{
ss << fmt::format(L"html, ");
}

if (WI_IsFlagSet(_CopyFormatting.Value(), CopyFormat::RTF))
{
ss << fmt::format(L"rtf, ");
}
}
}
return RS_(L"CopyTextCommandKey");

// Chop off the last ","
auto result = fmt::format(L"{}, {}", RS_(L"CopyTextCommandKey"), ss.str());
return winrt::hstring{ result.substr(0, result.size() - 2) };
carlos-zamora marked this conversation as resolved.
Show resolved Hide resolved
carlos-zamora marked this conversation as resolved.
Show resolved Hide resolved
}

winrt::hstring NewTabArgs::GenerateName() const
Expand Down
6 changes: 5 additions & 1 deletion src/cascadia/TerminalApp/ActionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ namespace winrt::TerminalApp::implementation
{
CopyTextArgs() = default;
GETSET_PROPERTY(bool, SingleLine, false);
GETSET_PROPERTY(Windows::Foundation::IReference<Microsoft::Terminal::TerminalControl::CopyFormat>, CopyFormatting, nullptr);

static constexpr std::string_view SingleLineKey{ "singleLine" };
static constexpr std::string_view CopyFormattingKey{ "copyFormatting" };

public:
hstring GenerateName() const;
Expand All @@ -103,7 +105,8 @@ namespace winrt::TerminalApp::implementation
auto otherAsUs = other.try_as<CopyTextArgs>();
if (otherAsUs)
{
return otherAsUs->_SingleLine == _SingleLine;
return otherAsUs->_SingleLine == _SingleLine &&
otherAsUs->_CopyFormatting == _CopyFormatting;
carlos-zamora marked this conversation as resolved.
Show resolved Hide resolved
}
return false;
};
Expand All @@ -112,6 +115,7 @@ namespace winrt::TerminalApp::implementation
// LOAD BEARING: Not using make_self here _will_ break you in the future!
auto args = winrt::make_self<CopyTextArgs>();
JsonUtils::GetValueForKey(json, SingleLineKey, args->_SingleLine);
JsonUtils::GetValueForKey(json, CopyFormattingKey, args->_CopyFormatting);
return { *args, {} };
}
};
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace TerminalApp
[default_interface] runtimeclass CopyTextArgs : IActionArgs
{
Boolean SingleLine { get; };
Windows.Foundation.IReference<Microsoft.Terminal.TerminalControl.CopyFormat> CopyFormatting { get; };
};

[default_interface] runtimeclass NewTabArgs : IActionArgs
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ namespace winrt::TerminalApp::implementation
{
if (const auto& realArgs = args.ActionArgs().try_as<TerminalApp::CopyTextArgs>())
{
const auto handled = _CopyText(realArgs.SingleLine());
const auto handled = _CopyText(realArgs.SingleLine(), realArgs.CopyFormatting());
args.Handled(handled);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/GlobalAppSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class TerminalApp::GlobalAppSettings final
GETSET_PROPERTY(bool, ShowTabsInTitlebar, true);
GETSET_PROPERTY(std::wstring, WordDelimiters); // default value set in constructor
GETSET_PROPERTY(bool, CopyOnSelect, false);
GETSET_PROPERTY(bool, CopyFormatting, false);
GETSET_PROPERTY(winrt::Microsoft::Terminal::TerminalControl::CopyFormat, CopyFormatting, 0);
GETSET_PROPERTY(bool, WarnAboutLargePaste, true);
GETSET_PROPERTY(bool, WarnAboutMultiLinePaste, true);
GETSET_PROPERTY(LaunchPosition, InitialPosition);
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@
<value>Copy text</value>
</data>
<data name="CopyTextAsSingleLineCommandKey" xml:space="preserve">
<value>Copy text as a single line</value>
<value>as a single line</value>
</data>
<data name="PasteTextCommandKey" xml:space="preserve">
<value>Paste</value>
Expand Down
17 changes: 14 additions & 3 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1597,18 +1597,28 @@ namespace winrt::TerminalApp::implementation
DataPackage dataPack = DataPackage();
dataPack.RequestedOperation(DataPackageOperation::Copy);

// The EventArgs.Formats() is an override for the global setting "copyFormatting"
// iff it is set
bool useGlobal = copiedData.Formats() == nullptr;
auto copyFormats = useGlobal ?
_settings->GlobalSettings().CopyFormatting() :
copiedData.Formats().Value();

// copy text to dataPack
dataPack.SetText(copiedData.Text());

if (_settings->GlobalSettings().CopyFormatting())
if (WI_IsFlagSet(copyFormats, CopyFormat::HTML))
{
// copy html to dataPack
const auto htmlData = copiedData.Html();
if (!htmlData.empty())
{
dataPack.SetHtmlFormat(htmlData);
}
}

if (WI_IsFlagSet(copyFormats, CopyFormat::RTF))
{
// copy rtf data to dataPack
const auto rtfData = copiedData.Rtf();
if (!rtfData.empty())
Expand Down Expand Up @@ -1701,12 +1711,13 @@ namespace winrt::TerminalApp::implementation
// - Copy text from the focused terminal to the Windows Clipboard
// Arguments:
// - singleLine: if enabled, copy contents as a single line of text
// - formats: dictate which formats need to be copied
// Return Value:
// - true iff we we able to copy text (if a selection was active)
bool TerminalPage::_CopyText(const bool singleLine)
bool TerminalPage::_CopyText(const bool singleLine, const Windows::Foundation::IReference<CopyFormat>& formats)
{
const auto control = _GetActiveControl();
return control.CopySelectionToClipboard(singleLine);
return control.CopySelectionToClipboard(singleLine, formats);
}

// Method Description:
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ namespace winrt::TerminalApp::implementation
winrt::fire_and_forget _CopyToClipboardHandler(const IInspectable sender, const winrt::Microsoft::Terminal::TerminalControl::CopyToClipboardEventArgs copiedData);
winrt::fire_and_forget _PasteFromClipboardHandler(const IInspectable sender,
const Microsoft::Terminal::TerminalControl::PasteFromClipboardEventArgs eventArgs);
bool _CopyText(const bool trimTrailingWhitespace);
bool _CopyText(const bool singleLine, const Windows::Foundation::IReference<Microsoft::Terminal::TerminalControl::CopyFormat>& formats);
void _PasteText();

fire_and_forget _LaunchSettings(const winrt::TerminalApp::SettingsTarget target);
Expand Down
24 changes: 24 additions & 0 deletions src/cascadia/TerminalApp/TerminalSettingsSerializationHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,30 @@ JSON_ENUM_MAPPER(::winrt::Microsoft::UI::Xaml::Controls::TabViewWidthMode)
};
};

JSON_FLAG_MAPPER(::winrt::Microsoft::Terminal::TerminalControl::CopyFormat)
{
JSON_MAPPINGS(5) = {
pair_type{ "none", AllClear },
pair_type{ "html", ValueType::HTML },
pair_type{ "rtf", ValueType::RTF },
pair_type{ "all", AllSet },
};

auto FromJson(const Json::Value& json)
{
if (json.isBool())
{
return json.asBool() ? AllSet : AllClear;
}
return BaseFlagMapper::FromJson(json);
}

bool CanConvert(const Json::Value& json)
{
return BaseFlagMapper::CanConvert(json) || json.isBool();
}
};

// Type Description:
// - Helper for converting the initial position string into
// 2 coordinate values. We allow users to only provide one coordinate,
Expand Down
37 changes: 22 additions & 15 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ constexpr const auto ScrollBarUpdateInterval = std::chrono::milliseconds(8);
// The minimum delay between updating the TSF input control.
constexpr const auto TsfRedrawInterval = std::chrono::milliseconds(100);

DEFINE_ENUM_FLAG_OPERATORS(winrt::Microsoft::Terminal::TerminalControl::CopyFormat);

namespace winrt::Microsoft::Terminal::TerminalControl::implementation
{
// Helper static function to ensure that all ambiguous-width glyphs are reported as narrow.
Expand Down Expand Up @@ -1136,7 +1138,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
}
else
{
CopySelectionToClipboard(shiftEnabled);
CopySelectionToClipboard(shiftEnabled, nullptr);
}
}
}
Expand Down Expand Up @@ -1296,7 +1298,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// Right clicks and middle clicks should not need to do anything when released.
if (_settings.CopyOnSelect() && point.Properties().PointerUpdateKind() == Windows::UI::Input::PointerUpdateKind::LeftButtonReleased && _selectionNeedsToBeCopied)
{
CopySelectionToClipboard();
CopySelectionToClipboard(false, nullptr);
}
}
else if (ptr.PointerDeviceType() == Windows::Devices::Input::PointerDeviceType::Touch)
Expand Down Expand Up @@ -2054,9 +2056,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation

void TermControl::_CopyToClipboard(const std::wstring_view& wstr)
{
auto copyArgs = winrt::make_self<CopyToClipboardEventArgs>(winrt::hstring(wstr),
winrt::hstring(L""),
winrt::hstring(L""));
auto copyArgs = winrt::make_self<CopyToClipboardEventArgs>(winrt::hstring(wstr));
_clipboardCopyHandlers(*this, *copyArgs);
}

Expand Down Expand Up @@ -2120,7 +2120,9 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// - CopyOnSelect does NOT clear the selection
// Arguments:
// - singleLine: collapse all of the text to one line
bool TermControl::CopySelectionToClipboard(bool singleLine)
// - formats: which formats to copy (defined by action's CopyFormatting arg). nullptr
// if we should defer which formats are copied to the global setting
bool TermControl::CopySelectionToClipboard(bool singleLine, const Windows::Foundation::IReference<CopyFormat>& formats)
{
if (_closing)
{
Expand Down Expand Up @@ -2150,16 +2152,20 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// GH#5347 - Don't provide a title for the generated HTML, as many
// web applications will paste the title first, followed by the HTML
// content, which is unexpected.
const auto htmlData = TextBuffer::GenHTML(bufferData,
_actualFont.GetUnscaledSize().Y,
_actualFont.GetFaceName(),
_settings.DefaultBackground());
const auto htmlData = formats == nullptr || WI_IsFlagSet(formats.Value(), CopyFormat::HTML) ?
TextBuffer::GenHTML(bufferData,
_actualFont.GetUnscaledSize().Y,
_actualFont.GetFaceName(),
_settings.DefaultBackground()) :
"";

// convert to RTF format
const auto rtfData = TextBuffer::GenRTF(bufferData,
_actualFont.GetUnscaledSize().Y,
_actualFont.GetFaceName(),
_settings.DefaultBackground());
const auto rtfData = formats == nullptr || WI_IsFlagSet(formats.Value(), CopyFormat::RTF) ?
TextBuffer::GenRTF(bufferData,
_actualFont.GetUnscaledSize().Y,
_actualFont.GetFaceName(),
_settings.DefaultBackground()) :
"";

if (!_settings.CopyOnSelect())
{
Expand All @@ -2170,7 +2176,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// send data up for clipboard
auto copyArgs = winrt::make_self<CopyToClipboardEventArgs>(winrt::hstring(textData),
winrt::to_hstring(htmlData),
winrt::to_hstring(rtfData));
winrt::to_hstring(rtfData),
formats);
_clipboardCopyHandlers(*this, *copyArgs);
return true;
}
Expand Down
Loading