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

Miscellaneous bug fixes for Mark Mode #13358

Merged
merged 13 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
18 changes: 14 additions & 4 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,13 +998,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// Method Description:
// - Given a copy-able selection, get the selected text from the buffer and send it to the
// Windows Clipboard (CascadiaWin32:main.cpp).
// - CopyOnSelect does NOT clear the selection
// Arguments:
// - singleLine: collapse all of the text to one line
// - formats: which formats to copy (defined by action's CopyFormatting arg). nullptr
// if we should defer which formats are copied to the global setting
// - clearSelection: if true, clear the selection. Used for CopyOnSelect.
bool ControlCore::CopySelectionToClipboard(bool singleLine,
const Windows::Foundation::IReference<CopyFormat>& formats)
const Windows::Foundation::IReference<CopyFormat>& formats,
bool clearSelection)
carlos-zamora marked this conversation as resolved.
Show resolved Hide resolved
{
// no selection --> nothing to copy
if (!_terminal->IsSelectionActive())
Expand Down Expand Up @@ -1044,7 +1045,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
bgColor) :
"";

if (!_settings->CopyOnSelect())
if (clearSelection)
{
_terminal->ClearSelection();
_updateSelection();
Expand Down Expand Up @@ -1093,6 +1094,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
return _terminal->IsInMarkMode();
}

bool ControlCore::IsInQuickEditMode() const
{
return _terminal->IsInQuickEditMode();
}

// Method Description:
// - Pre-process text pasted (presumably from the clipboard)
// before sending it over the terminal's connection.
Expand Down Expand Up @@ -1625,7 +1631,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_terminal->MultiClickSelection(terminalPosition, mode);
selectionNeedsToBeCopied = true;
}
_updateSelection();
_renderer->TriggerSelection();
carlos-zamora marked this conversation as resolved.
Show resolved Hide resolved

// this is used for mouse selection,
// so hide the markers
_UpdateSelectionMarkersHandlers(*this, winrt::make<implementation::UpdateSelectionMarkersEventArgs>(true));
}

void ControlCore::_updateSelection()
Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ namespace winrt::Microsoft::Terminal::Control::implementation

void SendInput(const winrt::hstring& wstr);
void PasteText(const winrt::hstring& hstr);
bool CopySelectionToClipboard(bool singleLine, const Windows::Foundation::IReference<CopyFormat>& formats);
bool CopySelectionToClipboard(bool singleLine, const Windows::Foundation::IReference<CopyFormat>& formats, bool clearSelection = true);
void SelectAll();
bool ToggleBlockSelection();
void ToggleMarkMode();
bool IsInMarkMode() const;
bool IsInQuickEditMode() const;

void GotFocus();
void LostFocus();
Expand Down
30 changes: 23 additions & 7 deletions src/cascadia/TerminalControl/ControlInteractivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// Method Description:
// - Given a copy-able selection, get the selected text from the buffer and send it to the
// Windows Clipboard (CascadiaWin32:main.cpp).
// - CopyOnSelect does NOT clear the selection
// Arguments:
// - singleLine: collapse all of the text to one line
// - formats: which formats to copy (defined by action's CopyFormatting arg). nullptr
// if we should defer which formats are copied to the global setting
// - clearSelection: if true, clear the selection after copying it. Used for CopyOnSelect.
bool ControlInteractivity::CopySelectionToClipboard(bool singleLine,
const Windows::Foundation::IReference<CopyFormat>& formats)
const Windows::Foundation::IReference<CopyFormat>& formats,
bool clearSelection)
{
if (_core)
{
Expand All @@ -164,7 +165,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// Mark the current selection as copied
_selectionNeedsToBeCopied = false;

return _core->CopySelectionToClipboard(singleLine, formats);
return _core->CopySelectionToClipboard(singleLine, formats, clearSelection);
}

return false;
Expand Down Expand Up @@ -257,15 +258,27 @@ namespace winrt::Microsoft::Terminal::Control::implementation
}
else if (WI_IsFlagSet(buttonState, MouseButtonState::IsRightButtonDown))
{
// CopyOnSelect right click always pastes
if (_core->CopyOnSelect() || !_core->HasSelection())
if (_core->CopyOnSelect())
{
// CopyOnSelect:
// 1. keyboard selection? --> copy the new content first
// 2. right click always pastes!
if (_core->IsInQuickEditMode() || _core->IsInMarkMode())
{
CopySelectionToClipboard(shiftEnabled, nullptr);
}
RequestPasteTextFromClipboard();
}
else
else if (_core->HasSelection())
{
// copy selected text
CopySelectionToClipboard(shiftEnabled, nullptr);
}
else
{
// no selection --> paste
RequestPasteTextFromClipboard();
}
}
}

Expand Down Expand Up @@ -383,7 +396,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
isLeftMouseRelease &&
_selectionNeedsToBeCopied)
{
CopySelectionToClipboard(false, nullptr);
// IMPORTANT!
// Set clearSelection to false here!
// Otherwise, the selection will be cleared immediately after you make it.
CopySelectionToClipboard(false, nullptr, /*clearSelection*/ false);
}

_singleClickTouchdownPos = std::nullopt;
Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalControl/ControlInteractivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
#pragma endregion

bool CopySelectionToClipboard(bool singleLine,
const Windows::Foundation::IReference<CopyFormat>& formats);
const Windows::Foundation::IReference<CopyFormat>& formats,
bool clearSelection = true);
void RequestPasteTextFromClipboard();
void SetEndSelectionPoint(const Core::Point pixelPosition);
bool ManglePathsForWsl();
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalCore/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Terminal::Terminal() :
_altGrAliasing{ true },
_blockSelection{ false },
_markMode{ false },
_quickEditMode{ false },
_selection{ std::nullopt },
_taskbarState{ 0 },
_taskbarProgress{ 0 },
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalCore/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ class Microsoft::Terminal::Core::Terminal final :
void SetBlockSelection(const bool isEnabled) noexcept;
void UpdateSelection(SelectionDirection direction, SelectionExpansion mode, ControlKeyStates mods);
void SelectAll();
const bool IsInQuickEditMode() const noexcept;
bool IsInMarkMode() const;
void ToggleMarkMode();

Expand Down Expand Up @@ -334,6 +335,7 @@ class Microsoft::Terminal::Core::Terminal final :
std::wstring _wordDelimiters;
SelectionExpansion _multiClickSelectionMode;
bool _markMode;
bool _quickEditMode;
#pragma endregion

std::unique_ptr<TextBuffer> _mainBuffer;
Expand Down
11 changes: 10 additions & 1 deletion src/cascadia/TerminalCore/TerminalSelection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ void Terminal::SetBlockSelection(const bool isEnabled) noexcept
_blockSelection = isEnabled;
}

const bool Terminal::IsInQuickEditMode() const noexcept
{
return _quickEditMode;
}

bool Terminal::IsInMarkMode() const
{
return _markMode;
Expand All @@ -296,6 +301,7 @@ void Terminal::ToggleMarkMode()
_selection->end = cursorPos;
_selection->pivot = cursorPos;
_markMode = true;
_quickEditMode = false;
_blockSelection = false;
}
}
Expand Down Expand Up @@ -393,7 +399,8 @@ void Terminal::UpdateSelection(SelectionDirection direction, SelectionExpansion
break;
}

// 3. Actually modify the selection
// 3. Actually modify the selection state
_quickEditMode = !_markMode;
if (_markMode && !mods.IsShiftPressed())
{
// [Mark Mode] + shift unpressed --> move all three (i.e. just use arrow keys)
Expand All @@ -404,6 +411,7 @@ void Terminal::UpdateSelection(SelectionDirection direction, SelectionExpansion
else
{
// [Mark Mode] + shift --> updating a standard selection
// This is also standard quick-edit modification
// NOTE: targetStart doesn't matter here
auto targetStart = false;
std::tie(_selection->start, _selection->end) = _PivotSelection(targetPos, targetStart);
Expand Down Expand Up @@ -577,6 +585,7 @@ void Terminal::ClearSelection()
{
_selection = std::nullopt;
_markMode = false;
_quickEditMode = false;
}

// Method Description:
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@
// Clipboard Integration
{ "command": { "action": "copy", "singleLine": false }, "keys": "ctrl+shift+c" },
{ "command": { "action": "copy", "singleLine": false }, "keys": "ctrl+insert" },
{ "command": { "action": "copy", "singleLine": false }, "keys": "enter" },
Copy link
Member

Choose a reason for hiding this comment

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

This is potentially a spicy change 🌶️

I know conhost did this in the past. I feel like we've discissed this in the past on the repl, but I can't find the discussion if we specifically said we'd add this to the defaults or not.

We may also want to consider something like #13115 - what does that even mean? Is that something separate from a text selection?

Copy link
Member Author

Choose a reason for hiding this comment

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

We may also want to consider something like #13115 - what does that even mean? Is that something separate from a text selection?

My vision for #13115 (which isn't even specced tbh), is that it's a way to navigate within mark mode. It might even be something as simple as [shift]+tab to move to the next/previous hyperlink. That 100% needs to be specc'd though.

This is potentially a spicy change 🌶️

Yeah, I don't remember the discussion either :/. The paths forward I see are...

  1. bind copy() to Enter in defaults.json (configurable)
  2. make Enter copy the text when in mark mode (non-configurable)
  3. don't even consider Enter entirely. If the user wants it, the user can bind it

Dustin seemed to be a little confused when using mark mode saying that there's no clear way to copy the selection. I think this stems from him using copyOnSelect, which seems fair because you never really had to think about copying your selection before. But we can't copy the selection every time the selection changes in mark mode. That would totally mess up clipboard history for people who use it. :/

We could pull this part out and discuss it at sync on Monday? It's a one-liner with no risk. Really just "political" reasons to decide to add it in or not.

Copy link
Member Author

Choose a reason for hiding this comment

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

Funny enough, now I'm thinking that we don't add this as a keybinding in defaults.json. Maybe we should make it non-configurable to allow for this.

That way, Enter copies when in mark mode, but opens the URL when it's selected/found by tabbing through he buffer in mark mode. It would then go back to copying if the user modified the selection.

Copy link
Member

Choose a reason for hiding this comment

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

Putting it in defaults.json allows the user to unbind it from both behaviors, but still gives us the freedom to change it (as opposed to inserting it in usersettings.json). We don't need to make the decision now, because defaults.json is entirely under our control across app updates.

Copy link
Contributor

Choose a reason for hiding this comment

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

Have we considered adding a mode option to key bindings that would only apply when enabled? They could correspond to this enum #13360 (comment)

Alacritty has the same concept (vi mode is mark mode) https://github.com/alacritty/alacritty/blob/master/alacritty/src/config/bindings.rs

Copy link
Member

Choose a reason for hiding this comment

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

Adding modal key bindings has been hotly debated, we just don't have the cycles to do it right now :)

{ "command": "paste", "keys": "ctrl+shift+v" },
{ "command": "paste", "keys": "shift+insert" },
{ "command": "selectAll", "keys": "ctrl+shift+a" },
Expand Down