address Leonard's comments

This commit is contained in:
Carlos Zamora 2021-09-23 10:54:18 -07:00
parent 30009eb0eb
commit b7842624a0
4 changed files with 64 additions and 83 deletions

View file

@ -33,74 +33,6 @@ constexpr const auto TsfRedrawInterval = std::chrono::milliseconds(100);
// The minimum delay between updating the locations of regex patterns
constexpr const auto UpdatePatternLocationsInterval = std::chrono::milliseconds(500);
static constexpr std::optional<Terminal::SelectionDirection> ConvertVKeyToSelectionDirection(WORD vkey)
{
switch (vkey)
{
case VK_LEFT:
case VK_HOME:
return Terminal::SelectionDirection::Left;
case VK_RIGHT:
case VK_END:
return Terminal::SelectionDirection::Right;
case VK_UP:
case VK_PRIOR:
return Terminal::SelectionDirection::Up;
case VK_DOWN:
case VK_NEXT:
return Terminal::SelectionDirection::Down;
default:
return std::nullopt;
}
}
static constexpr std::optional<std::tuple<Terminal::SelectionDirection, Terminal::SelectionExpansion>> ConvertKeyEventToUpdateSelectionParams(const ControlKeyStates mods, const WORD vkey)
{
if (mods.IsShiftPressed() && !mods.IsAltPressed())
{
if (const auto dir{ ConvertVKeyToSelectionDirection(vkey) })
{
if (mods.IsCtrlPressed())
{
switch (vkey)
{
case VK_LEFT:
case VK_RIGHT:
// Move by word
return std::make_tuple(*dir, Terminal::SelectionExpansion::Word);
case VK_HOME:
case VK_END:
// Move by buffer
return std::make_tuple(*dir, Terminal::SelectionExpansion::Buffer);
default:
__fallthrough;
}
}
else
{
switch (vkey)
{
case VK_HOME:
case VK_END:
case VK_PRIOR:
case VK_NEXT:
// Move by viewport
return std::make_tuple(*dir, Terminal::SelectionExpansion::Viewport);
case VK_LEFT:
case VK_RIGHT:
case VK_UP:
case VK_DOWN:
// Move by character
return std::make_tuple(*dir, Terminal::SelectionExpansion::Char);
default:
__fallthrough;
}
}
}
}
return std::nullopt;
}
namespace winrt::Microsoft::Terminal::Control::implementation
{
// Helper static function to ensure that all ambiguous-width glyphs are reported as narrow.
@ -441,10 +373,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
keyDown)
{
// try to update the selection
if (const auto updateSlnParams{ ConvertKeyEventToUpdateSelectionParams(modifiers, vkey) })
if (const auto updateSlnParams{ ::Terminal::ConvertKeyEventToUpdateSelectionParams(modifiers, vkey) })
{
auto lock = _terminal->LockForWriting();
_terminal->UpdateSelection(std::get<0>(*updateSlnParams), std::get<1>(*updateSlnParams));
_terminal->UpdateSelection(updateSlnParams->first, updateSlnParams->second);
_renderer->TriggerSelection();
return true;
}

View file

@ -249,6 +249,9 @@ public:
void SetBlockSelection(const bool isEnabled) noexcept;
void UpdateSelection(SelectionDirection direction, SelectionExpansion mode);
using UpdateSelectionParams = std::optional<std::pair<SelectionDirection, SelectionExpansion>>;
static UpdateSelectionParams ConvertKeyEventToUpdateSelectionParams(const ControlKeyStates mods, const WORD vkey);
const TextBuffer::TextAndColor RetrieveSelectedTextFromBuffer(bool trimTrailingWhitespace);
#pragma endregion
@ -383,7 +386,7 @@ private:
#pragma region TextSelection
// These methods are defined in TerminalSelection.cpp
std::vector<SMALL_RECT> _GetSelectionRects() const noexcept;
std::tuple<COORD, COORD, bool> _PivotSelection(const COORD targetPos) const;
std::pair<COORD, COORD> _PivotSelection(const COORD targetPos, bool& targetStart) const;
std::pair<COORD, COORD> _ExpandSelectionAnchors(std::pair<COORD, COORD> anchors) const;
COORD _ConvertToBufferCell(const COORD viewportPos) const;
void _MoveByChar(SelectionDirection direction, COORD& pos);

View file

@ -151,9 +151,8 @@ void Terminal::SetSelectionEnd(const COORD viewportPos, std::optional<SelectionE
// Otherwise, we may accidentally expand during other selection-based actions
_multiClickSelectionMode = newExpansionMode.has_value() ? *newExpansionMode : _multiClickSelectionMode;
std::pair<COORD, COORD> anchors;
bool targetStart;
std::tie(anchors.first, anchors.second, targetStart) = _PivotSelection(textBufferPos);
bool targetStart = false;
const auto anchors = _PivotSelection(textBufferPos, targetStart);
const auto expandedAnchors = _ExpandSelectionAnchors(anchors);
if (newExpansionMode.has_value())
@ -178,24 +177,22 @@ void Terminal::SetSelectionEnd(const COORD viewportPos, std::optional<SelectionE
// - This ensures start < end when compared
// Arguments:
// - targetPos: the (x,y) coordinate we are moving to on the text buffer
// - targetStart: if true, target will be the new start. Otherwise, target will be the new end.
// Return Value:
// - COORD: the new start for a selection
// - COORD: the new end for a selection
// - bool: if true, target will be the new start. Otherwise, target will be the new end.
std::tuple<COORD, COORD, bool> Terminal::_PivotSelection(const COORD targetPos) const
// - the new start/end for a selection
std::pair<COORD, COORD> Terminal::_PivotSelection(const COORD targetPos, bool& targetStart) const
{
const bool targetStart{ _buffer->GetSize().CompareInBounds(targetPos, _selection->pivot) <= 0 };
if (targetStart)
if (targetStart = _buffer->GetSize().CompareInBounds(targetPos, _selection->pivot) <= 0)
{
// target is before pivot
// treat target as start
return { targetPos, _selection->pivot, targetStart };
return std::make_pair(targetPos, _selection->pivot);
}
else
{
// target is after pivot
// treat pivot as start
return { _selection->pivot, targetPos, targetStart };
return std::make_pair(_selection->pivot, targetPos);
}
}
@ -238,6 +235,52 @@ void Terminal::SetBlockSelection(const bool isEnabled) noexcept
_blockSelection = isEnabled;
}
Terminal::UpdateSelectionParams Terminal::ConvertKeyEventToUpdateSelectionParams(const ControlKeyStates mods, const WORD vkey)
{
if (mods.IsShiftPressed() && !mods.IsAltPressed())
{
if (mods.IsCtrlPressed())
{
// Ctrl + Shift + _
switch (vkey)
{
case VK_LEFT:
return UpdateSelectionParams{ std::in_place, SelectionDirection::Left, SelectionExpansion::Word };
case VK_RIGHT:
return UpdateSelectionParams{ std::in_place, SelectionDirection::Right, SelectionExpansion::Word };
case VK_HOME:
return UpdateSelectionParams{ std::in_place, SelectionDirection::Left, SelectionExpansion::Buffer };
case VK_END:
return UpdateSelectionParams{ std::in_place, SelectionDirection::Right, SelectionExpansion::Buffer };
}
}
else
{
// Shift + _
switch (vkey)
{
case VK_HOME:
return UpdateSelectionParams{ std::in_place, SelectionDirection::Left, SelectionExpansion::Viewport };
case VK_END:
return UpdateSelectionParams{ std::in_place, SelectionDirection::Right, SelectionExpansion::Viewport };
case VK_PRIOR:
return UpdateSelectionParams{ std::in_place, SelectionDirection::Up, SelectionExpansion::Viewport };
case VK_NEXT:
return UpdateSelectionParams{ std::in_place, SelectionDirection::Down, SelectionExpansion::Viewport };
case VK_LEFT:
return UpdateSelectionParams{ std::in_place, SelectionDirection::Left, SelectionExpansion::Char };
case VK_RIGHT:
return UpdateSelectionParams{ std::in_place, SelectionDirection::Right, SelectionExpansion::Char };
case VK_UP:
return UpdateSelectionParams{ std::in_place, SelectionDirection::Up, SelectionExpansion::Char };
case VK_DOWN:
return UpdateSelectionParams{ std::in_place, SelectionDirection::Down, SelectionExpansion::Char };
}
}
}
return std::nullopt;
}
void Terminal::UpdateSelection(SelectionDirection direction, SelectionExpansion mode)
{
// 1. Figure out which endpoint to update
@ -263,7 +306,9 @@ void Terminal::UpdateSelection(SelectionDirection direction, SelectionExpansion
}
// 3. Actually modify the selection
std::tie(_selection->start, _selection->end, std::ignore) = _PivotSelection(targetPos);
// NOTE: targetStart doesn't matter here
bool targetStart = false;
std::tie(_selection->start, _selection->end) = _PivotSelection(targetPos, targetStart);
// 4. Scroll (if necessary)
if (const auto viewport = _GetVisibleViewport(); !viewport.IsInBounds(targetPos))

View file

@ -493,6 +493,7 @@ void DxEngine::_ComputePixelShaderSettings() noexcept
// actual failure from the API itself.
[[nodiscard]] HRESULT DxEngine::_CreateSurfaceHandle() noexcept
{
#pragma warning(suppress : 26447)
wil::unique_hmodule hDComp{ LoadLibraryEx(L"Dcomp.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32) };
RETURN_LAST_ERROR_IF(hDComp.get() == nullptr);