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 12 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
1 change: 1 addition & 0 deletions doc/cascadia/profiles.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@
"switchToTab",
"tabSearch",
"toggleAlwaysOnTop",
"toggleBlockSelection",
"toggleFocusMode",
"selectAll",
"setFocusMode",
Expand Down
58 changes: 32 additions & 26 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,12 @@ namespace winrt::Microsoft::Terminal::Control::implementation
vkey != VK_SNAPSHOT &&
keyDown)
{
if (_terminal->IsInMarkMode() && modifiers.IsCtrlPressed() && vkey == 'A')
const auto isInMarkMode = _terminal->SelectionMode() == ::Microsoft::Terminal::Core::Terminal::SelectionInteractionMode::Mark;
if (isInMarkMode && modifiers.IsCtrlPressed() && vkey == 'A')
Comment on lines +420 to +421
Copy link
Member

Choose a reason for hiding this comment

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

The namespace qualifier seems overly specific. FYI this should work as expected from other languages:

using ::Microsoft::Terminal::Core::Terminal::SelectionInteractionMode;

{
auto lock = _terminal->LockForWriting();
_terminal->SelectAll();
_updateSelection();
_updateSelectionUI(true);
return true;
}

Expand All @@ -430,15 +431,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
auto lock = _terminal->LockForWriting();
_terminal->UpdateSelection(updateSlnParams->first, updateSlnParams->second, modifiers);
_updateSelection();
_updateSelectionUI(true);
return true;
}

// GH#8791 - don't dismiss selection if Windows key was also pressed as a key-combination.
if (!modifiers.IsWinPressed())
{
_terminal->ClearSelection();
_updateSelection();
_updateSelectionUI(false);
}

// When there is a selection active, escape should clear it and NOT flow through
Expand Down Expand Up @@ -950,8 +951,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
const auto end{ _terminal->SelectionEndForRendering() };
info.EndPos = { end.X, end.Y };

info.MovingEnd = _terminal->MovingEnd();
info.MovingCursor = _terminal->MovingCursor();
info.Endpoint = static_cast<SelectionEndpointTarget>(_terminal->SelectionEndpointTarget());

const auto bufferSize{ _terminal->GetTextBuffer().GetSize() };
info.StartAtLeftBoundary = _terminal->GetSelectionAnchor().x == bufferSize.Left();
Expand Down Expand Up @@ -981,11 +981,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation

// save location (for rendering) + render
_terminal->SetSelectionEnd(terminalPosition);
_renderer->TriggerSelection();

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

// Called when the Terminal wants to set something to the clipboard, i.e.
Expand All @@ -998,7 +997,6 @@ 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
Expand Down Expand Up @@ -1044,12 +1042,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
bgColor) :
"";

if (!_settings->CopyOnSelect())
{
_terminal->ClearSelection();
_updateSelection();
}

// send data up for clipboard
_CopyToClipboardHandlers(*this,
winrt::make<CopyToClipboardEventArgs>(winrt::hstring{ textData },
Expand All @@ -1063,7 +1055,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
auto lock = _terminal->LockForWriting();
_terminal->SelectAll();
_updateSelection();
_updateSelectionUI(true);
}

void ControlCore::ClearSelection()
{
auto lock = _terminal->LockForWriting();
_terminal->ClearSelection();
_updateSelectionUI(false);
}

bool ControlCore::ToggleBlockSelection()
Expand All @@ -1085,12 +1084,12 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
auto lock = _terminal->LockForWriting();
_terminal->ToggleMarkMode();
_updateSelection();
_updateSelectionUI(true);
}

bool ControlCore::IsInMarkMode() const
Control::SelectionInteractionMode ControlCore::SelectionMode() const
{
return _terminal->IsInMarkMode();
return static_cast<Control::SelectionInteractionMode>(_terminal->SelectionMode());
}

// Method Description:
Expand All @@ -1100,7 +1099,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
_terminal->WritePastedText(hstr);
_terminal->ClearSelection();
_updateSelection();
_updateSelectionUI(false);
_terminal->TrySnapOnInput();
}

Expand Down Expand Up @@ -1420,11 +1419,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
_terminal->SetBlockSelection(false);
search.Select();
_renderer->TriggerSelection();

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

// Raise a FoundMatch event, which the control will use to notify
Expand Down Expand Up @@ -1625,14 +1623,22 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_terminal->MultiClickSelection(terminalPosition, mode);
selectionNeedsToBeCopied = true;
}
_updateSelection();
// this is used for mouse selection,
// so hide the markers
_updateSelectionUI(false);
}

void ControlCore::_updateSelection()
// Method Description:
// - Updates the renderer's representation of the selection as well as the selection marker overlay in TermControl
// Arguments:
// - tryShowMarkers: if true, show the selection marker overlay. Otherwise hide it.
void ControlCore::_updateSelectionUI(bool tryShowMarkers)
carlos-zamora marked this conversation as resolved.
Show resolved Hide resolved
{
_renderer->TriggerSelection();
const bool clearMarkers{ !_terminal->IsSelectionActive() };
_UpdateSelectionMarkersHandlers(*this, winrt::make<implementation::UpdateSelectionMarkersEventArgs>(clearMarkers));
// if show markers and there's a selection, show the markers.
// otherwise, hide the markers (i.e. mouse selection, search, etc...)
const bool showMarkers{ _terminal->IsSelectionActive() && tryShowMarkers };
_UpdateSelectionMarkersHandlers(*this, winrt::make<implementation::UpdateSelectionMarkersEventArgs>(!showMarkers));
}

void ControlCore::AttachUiaEngine(::Microsoft::Console::Render::IRenderEngine* const pEngine)
Expand Down
5 changes: 3 additions & 2 deletions src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
void PasteText(const winrt::hstring& hstr);
bool CopySelectionToClipboard(bool singleLine, const Windows::Foundation::IReference<CopyFormat>& formats);
void SelectAll();
void ClearSelection();
bool ToggleBlockSelection();
void ToggleMarkMode();
bool IsInMarkMode() const;
Control::SelectionInteractionMode SelectionMode() const;

void GotFocus();
void LostFocus();
Expand Down Expand Up @@ -271,7 +272,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
bool _setFontSizeUnderLock(int fontSize);
void _updateFont(const bool initialUpdate = false);
void _refreshSizeUnderLock();
void _updateSelection();
void _updateSelectionUI(bool tryShowMarkers);

void _sendInputToConnection(std::wstring_view wstr);

Expand Down
21 changes: 18 additions & 3 deletions src/cascadia/TerminalControl/ControlCore.idl
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,26 @@ namespace Microsoft.Terminal.Control
All
};

enum SelectionInteractionMode
{
None,
Mouse,
Keyboard,
Mark
};

[flags]
enum SelectionEndpointTarget
{
Start = 0x1,
End = 0x2
};

struct SelectionData
{
Microsoft.Terminal.Core.Point StartPos;
Microsoft.Terminal.Core.Point EndPos;
Boolean MovingEnd;
Boolean MovingCursor;
SelectionEndpointTarget Endpoint;
Boolean StartAtLeftBoundary;
Boolean EndAtRightBoundary;
};
Expand Down Expand Up @@ -75,10 +89,10 @@ namespace Microsoft.Terminal.Control
void SendInput(String text);
void PasteText(String text);
void SelectAll();
void ClearSelection();
Boolean ToggleBlockSelection();
void ToggleMarkMode();
void ClearBuffer(ClearBufferType clearType);
Boolean IsInMarkMode();

void SetHoveredCell(Microsoft.Terminal.Core.Point terminalPosition);
void ClearHoveredCell();
Expand All @@ -101,6 +115,7 @@ namespace Microsoft.Terminal.Control
Boolean HasSelection { get; };
IVector<String> SelectedText(Boolean trimTrailingWhitespace);
SelectionData SelectionInfo { get; };
SelectionInteractionMode SelectionMode();

String HoveredUriText { get; };
Windows.Foundation.IReference<Microsoft.Terminal.Core.Point> HoveredCell { get; };
Expand Down
16 changes: 9 additions & 7 deletions src/cascadia/TerminalControl/ControlInteractivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ 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
Expand Down Expand Up @@ -257,15 +256,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation
}
else if (WI_IsFlagSet(buttonState, MouseButtonState::IsRightButtonDown))
{
// CopyOnSelect right click always pastes
if (_core->CopyOnSelect() || !_core->HasSelection())
// Try to copy the text and clear 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.

It could be helpful to mention here that CopySelectionToClipboard returns false if the selection is empty, which results in our "unique" right-click behavior (copy if something's selected, paste if it isn't).

const auto successfulCopy = CopySelectionToClipboard(shiftEnabled, nullptr);
_core->ClearSelection();
if (_core->CopyOnSelect() || !successfulCopy)
{
// CopyOnSelect: right click always pastes!
// Otherwise: no selection --> paste
RequestPasteTextFromClipboard();
}
else
{
CopySelectionToClipboard(shiftEnabled, nullptr);
}
}
}

Expand Down Expand Up @@ -383,6 +382,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
isLeftMouseRelease &&
_selectionNeedsToBeCopied)
{
// IMPORTANT!
// DO NOT clear the selection here!
// Otherwise, the selection will be cleared immediately after you make it.
CopySelectionToClipboard(false, nullptr);
}

Expand Down
19 changes: 11 additions & 8 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
// Manually show the cursor when a key is pressed. Restarting
// the timer prevents flickering.
_core.CursorOn(!_core.IsInMarkMode());
_core.CursorOn(_core.SelectionMode() != SelectionInteractionMode::Mark);
_cursorTimer->Start();
}

Expand Down Expand Up @@ -1659,7 +1659,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
if (_cursorTimer)
{
// When the terminal focuses, show the cursor immediately
_core.CursorOn(!_core.IsInMarkMode());
_core.CursorOn(_core.SelectionMode() != SelectionInteractionMode::Mark);
_cursorTimer->Start();
}

Expand Down Expand Up @@ -1902,7 +1902,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
return false;
}

return _interactivity.CopySelectionToClipboard(singleLine, formats);
const auto successfulCopy = _interactivity.CopySelectionToClipboard(singleLine, formats);
_core.ClearSelection();
return successfulCopy;
}

// Method Description:
Expand Down Expand Up @@ -2830,9 +2832,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation

// show/update selection markers
// figure out which endpoint to move, get it and the relevant icon (hide the other icon)
const auto selectionAnchor{ markerData.MovingEnd ? markerData.EndPos : markerData.StartPos };
const auto& marker{ markerData.MovingEnd ? SelectionEndMarker() : SelectionStartMarker() };
const auto& otherMarker{ markerData.MovingEnd ? SelectionStartMarker() : SelectionEndMarker() };
const auto movingEnd{ WI_IsFlagSet(markerData.Endpoint, SelectionEndpointTarget::End) };
const auto selectionAnchor{ movingEnd ? markerData.EndPos : markerData.StartPos };
const auto& marker{ movingEnd ? SelectionEndMarker() : SelectionStartMarker() };
const auto& otherMarker{ movingEnd ? SelectionStartMarker() : SelectionEndMarker() };
if (selectionAnchor.Y < 0 || selectionAnchor.Y >= _core.ViewHeight())
{
// if the endpoint is outside of the viewport,
Expand All @@ -2841,7 +2844,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
otherMarker.Visibility(Visibility::Collapsed);
co_return;
}
else if (markerData.MovingCursor)
else if (WI_AreAllFlagsSet(markerData.Endpoint, SelectionEndpointTarget::Start | SelectionEndpointTarget::End))
{
// display both markers
displayMarker(true);
Expand All @@ -2851,7 +2854,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
// display one marker,
// but hide the other
displayMarker(markerData.MovingEnd);
displayMarker(movingEnd);
otherMarker.Visibility(Visibility::Collapsed);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/cascadia/TerminalCore/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ Terminal::Terminal() :
_snapOnInput{ true },
_altGrAliasing{ true },
_blockSelection{ false },
_markMode{ false },
_selectionMode{ SelectionInteractionMode::None },
_selection{ std::nullopt },
_selectionEndpoint{ static_cast<SelectionEndpoint>(0) },
_taskbarState{ 0 },
_taskbarProgress{ 0 },
_trimBlockSelection{ false },
Expand Down Expand Up @@ -1369,7 +1370,7 @@ void Terminal::SetCursorOn(const bool isOn)
bool Terminal::IsCursorBlinkingAllowed() const noexcept
{
const auto& cursor = _activeBuffer().GetCursor();
return !_markMode && cursor.IsBlinkingAllowed();
return _selectionMode != SelectionInteractionMode::Mark && cursor.IsBlinkingAllowed();
}

// Method Description:
Expand Down
Loading