Skip to content

Commit

Permalink
Connect clipboard functionality to their keybindings (microsoft#1093)
Browse files Browse the repository at this point in the history
* Connects clipboard functionality to their keybindings.

* Cleaning up comments and whitespace.

* Added "copyTextWithoutNewlines" keybinding.

* Fixing tabs in idl file

* Fixing merge conflicts

* Adding default keybindings for copy and paste to ctrl-shift-c and ctrl-shift-v, respectively.

* Complying with refactoring

* Fixing formatting issues
  • Loading branch information
d-bingham authored and mcpiroman committed Jul 2, 2019
1 parent 4eefdcc commit a428484
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/cascadia/TerminalApp/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ namespace winrt::TerminalApp::implementation
bindings.ScrollDownPage([this]() { _ScrollPage(1); });
bindings.SwitchToTab([this](const auto index) { _SelectTab({ index }); });
bindings.OpenSettings([this]() { _OpenSettings(); });
bindings.CopyText([this](const auto trimWhitespace) { _CopyText(trimWhitespace); });
bindings.PasteText([this]() { _PasteText(); });
}

// Method Description:
Expand Down Expand Up @@ -1015,6 +1017,14 @@ namespace winrt::TerminalApp::implementation
control.CopySelectionToClipboard(trimTrailingWhitespace);
}

// Method Description:
// - Paste text from the Windows Clipboard to the focused terminal
void App::_PasteText()
{
const auto control = _GetFocusedControl();
control.PasteTextFromClipboard();
}

// Method Description:
// - Sets focus to the tab to the right or left the currently selected tab.
void App::_SelectNextTab(const bool bMoveRight)
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ namespace winrt::TerminalApp::implementation

void _Scroll(int delta);
void _CopyText(const bool trimTrailingWhitespace);
void _PasteText();
void _SplitVertical(const std::optional<GUID>& profileGuid);
void _SplitHorizontal(const std::optional<GUID>& profileGuid);
void _SplitPane(const Pane::SplitState splitType, const std::optional<GUID>& profileGuid);

// Todo: add more event implementations here
// MSFT:20641986: Add keybindings for New Window
void _ScrollPage(int delta);
Expand Down
5 changes: 4 additions & 1 deletion src/cascadia/TerminalApp/AppKeyBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ namespace winrt::TerminalApp::implementation
switch (action)
{
case ShortcutAction::CopyText:
_CopyTextHandlers();
_CopyTextHandlers(true);
return true;
case ShortcutAction::CopyTextWithoutNewlines:
_CopyTextHandlers(false);
return true;
case ShortcutAction::PasteText:
_PasteTextHandlers();
Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalApp/AppKeyBindings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace TerminalApp
enum ShortcutAction
{
CopyText = 0,
CopyTextWithoutNewlines,
PasteText,
NewTab,
NewTabProfile0,
Expand Down Expand Up @@ -42,7 +43,7 @@ namespace TerminalApp
OpenSettings
};

delegate void CopyTextEventArgs();
delegate void CopyTextEventArgs(Boolean trimWhitespace);
delegate void PasteTextEventArgs();
delegate void NewTabEventArgs();
delegate void NewTabWithProfileEventArgs(Int32 profileIndex);
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/AppKeyBindingsSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static constexpr std::string_view KeysKey{ "keys" };
static constexpr std::string_view CommandKey{ "command" };

static constexpr std::string_view CopyTextKey{ "copy" };
static constexpr std::string_view CopyTextWithoutNewlinesKey{ "copyTextWithoutNewlines" };
static constexpr std::string_view PasteTextKey{ "paste" };
static constexpr std::string_view NewTabKey{ "newTab" };
static constexpr std::string_view NewTabWithProfile0Key{ "newTabProfile0" };
Expand Down Expand Up @@ -60,6 +61,7 @@ static constexpr std::string_view SplitVerticalKey{ "splitVertical" };
// about here.
static const std::map<std::string_view, ShortcutAction, std::less<>> commandNames{
{ CopyTextKey, ShortcutAction::CopyText },
{ CopyTextWithoutNewlinesKey, ShortcutAction::CopyTextWithoutNewlines },
{ PasteTextKey, ShortcutAction::PasteText },
{ NewTabKey, ShortcutAction::NewTab },
{ NewTabWithProfile0Key, ShortcutAction::NewTabProfile0 },
Expand Down
9 changes: 9 additions & 0 deletions src/cascadia/TerminalApp/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ void CascadiaSettings::_CreateDefaultKeybindings()
keyBindings.SetKeyBinding(ShortcutAction::CloseTab,
KeyChord{ KeyModifiers::Ctrl,
static_cast<int>('W') });

keyBindings.SetKeyBinding(ShortcutAction::CopyText,
KeyChord{ KeyModifiers::Ctrl | KeyModifiers::Shift,
static_cast<int>('C') });

keyBindings.SetKeyBinding(ShortcutAction::PasteText,
KeyChord{ KeyModifiers::Ctrl | KeyModifiers::Shift,
static_cast<int>('V') });

keyBindings.SetKeyBinding(ShortcutAction::OpenSettings,
KeyChord{ KeyModifiers::Ctrl,
VK_OEM_COMMA });
Expand Down
21 changes: 14 additions & 7 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,13 +728,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// paste selection, otherwise
else
{
// attach TermControl::_SendInputToConnection() as the clipboardDataHandler.
// This is called when the clipboard data is loaded.
auto clipboardDataHandler = std::bind(&TermControl::_SendInputToConnection, this, std::placeholders::_1);
auto pasteArgs = winrt::make_self<PasteFromClipboardEventArgs>(clipboardDataHandler);

// send paste event up to TermApp
_clipboardPasteHandlers(*this, *pasteArgs);
PasteTextFromClipboard();
}
}
}
Expand Down Expand Up @@ -1341,6 +1335,19 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
_clipboardCopyHandlers(copiedData);
}

// Method Description:
// - Initiate a paste operation.
void TermControl::PasteTextFromClipboard()
{
// attach TermControl::_SendInputToConnection() as the clipboardDataHandler.
// This is called when the clipboard data is loaded.
auto clipboardDataHandler = std::bind(&TermControl::_SendInputToConnection, this, std::placeholders::_1);
auto pasteArgs = winrt::make_self<PasteFromClipboardEventArgs>(clipboardDataHandler);

// send paste event up to TermApp
_clipboardPasteHandlers(*this, *pasteArgs);
}

void TermControl::Close()
{
if (!_closing.exchange(true))
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation

hstring Title();
void CopySelectionToClipboard(bool trimTrailingWhitespace);
void PasteTextFromClipboard();
void Close();
bool ShouldCloseOnExit() const noexcept;

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace Microsoft.Terminal.TerminalControl

String Title { get; };
void CopySelectionToClipboard(Boolean trimTrailingWhitespace);
void PasteTextFromClipboard();
void Close();
Boolean ShouldCloseOnExit { get; };

Expand Down

0 comments on commit a428484

Please sign in to comment.