Skip to content

Commit

Permalink
Refactor speedClicks interpretation of 3x & 4x to share some code for…
Browse files Browse the repository at this point in the history
… word-wise selection

Signed-off-by: Christian Parpart <christian@parpart.family>
  • Loading branch information
christianparpart committed May 12, 2024
1 parent c902715 commit 2503648
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 40 deletions.
52 changes: 23 additions & 29 deletions src/vtbackend/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,25 +367,25 @@ bool Terminal::ensureFreshRenderBuffer(bool locked)
return true;
}

PageSize Terminal::SelectionHelper::pageSize() const noexcept
PageSize Terminal::TheSelectionHelper::pageSize() const noexcept
{
return terminal->pageSize();
}

bool Terminal::SelectionHelper::wrappedLine(LineOffset line) const noexcept
bool Terminal::TheSelectionHelper::wrappedLine(LineOffset line) const noexcept
{
return terminal->isLineWrapped(line);
}

bool Terminal::SelectionHelper::cellEmpty(CellLocation pos) const noexcept
bool Terminal::TheSelectionHelper::cellEmpty(CellLocation pos) const noexcept
{
// Word selection may be off by one
pos.column = std::min(pos.column, boxed_cast<ColumnOffset>(terminal->pageSize().columns - 1));

return terminal->currentScreen().isCellEmpty(pos);
}

int Terminal::SelectionHelper::cellWidth(CellLocation pos) const noexcept
int Terminal::TheSelectionHelper::cellWidth(CellLocation pos) const noexcept
{
// Word selection may be off by one
pos.column = std::min(pos.column, boxed_cast<ColumnOffset>(terminal->pageSize().columns - 1));
Expand Down Expand Up @@ -662,6 +662,21 @@ Handled Terminal::sendMousePressEvent(Modifiers modifiers,
return Handled { eventHandledByApp && !isModeEnabled(DECMode::MousePassiveTracking) };
}

void Terminal::triggerWordWiseSelection(CellLocation startPos, TheSelectionHelper const& selectionHelper)
{
setSelector(std::make_unique<WordWiseSelection>(selectionHelper, startPos, selectionUpdatedHelper()));

if (_selection->extend(startPos))
onSelectionUpdated();

if (_settings.visualizeSelectedWord)
{
auto const text = extractSelectionText();
auto const text32 = unicode::convert_to<char32_t>(string_view(text.data(), text.size()));
setNewSearchTerm(text32, true);
}
}

bool Terminal::handleMouseSelection(Modifiers modifiers)
{
verifyState();
Expand Down Expand Up @@ -695,30 +710,8 @@ bool Terminal::handleMouseSelection(Modifiers modifiers)
std::make_unique<LinearSelection>(_selectionHelper, startPos, selectionUpdatedHelper()));
}
break;
case 2:
setSelector(
std::make_unique<WordWiseSelection>(_selectionHelper, startPos, selectionUpdatedHelper()));
if (_selection->extend(startPos))
onSelectionUpdated();
if (_settings.visualizeSelectedWord)
{
auto const text = extractSelectionText();
auto const text32 = unicode::convert_to<char32_t>(string_view(text.data(), text.size()));
setNewSearchTerm(text32, true);
}
break;
case 3:
setSelector(std::make_unique<WordWiseSelection>(
_extendedSelectionHelper, startPos, selectionUpdatedHelper()));
if (_selection->extend(startPos))
onSelectionUpdated();
if (_settings.visualizeSelectedWord)
{
auto const text = extractSelectionText();
auto const text32 = unicode::convert_to<char32_t>(string_view(text.data(), text.size()));
setNewSearchTerm(text32, true);
}
break;
case 2: triggerWordWiseSelection(startPos, _selectionHelper); break;
case 3: triggerWordWiseSelection(startPos, _extendedSelectionHelper); break;
case 4:
setSelector(
std::make_unique<FullLineSelection>(_selectionHelper, startPos, selectionUpdatedHelper()));
Expand Down Expand Up @@ -1459,7 +1452,8 @@ void Terminal::setWordDelimiters(string const& wordDelimiters)
{
_settings.wordDelimiters = unicode::from_utf8(wordDelimiters);

_selectionHelper.wordDelimited = [wordDelimiters= _settings.wordDelimiters, this](CellLocation const& pos) {
_selectionHelper.wordDelimited = [wordDelimiters = _settings.wordDelimiters,
this](CellLocation const& pos) {
return this->wordDelimited(pos, wordDelimiters);
};
}
Expand Down
24 changes: 13 additions & 11 deletions src/vtbackend/Terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,17 @@ class Terminal
void updateIndicatorStatusLine();
void updateCursorVisibilityState() const noexcept;
void updateHoveringHyperlinkState();

struct TheSelectionHelper: public vtbackend::SelectionHelper
{
Terminal* terminal;
explicit TheSelectionHelper(Terminal* self): terminal { self } {}
[[nodiscard]] PageSize pageSize() const noexcept override;
[[nodiscard]] bool wrappedLine(LineOffset line) const noexcept override;
[[nodiscard]] bool cellEmpty(CellLocation pos) const noexcept override;
[[nodiscard]] int cellWidth(CellLocation pos) const noexcept override;
};
void triggerWordWiseSelection(CellLocation startPos, TheSelectionHelper const& selectionHelper);
bool handleMouseSelection(Modifiers modifiers);

/// Tests if the text selection should be extended by the given mouse position or not.
Expand Down Expand Up @@ -1054,17 +1065,8 @@ class Terminal

// {{{ selection states
std::unique_ptr<Selection> _selection;
struct SelectionHelper: public vtbackend::SelectionHelper
{
Terminal* terminal;
explicit SelectionHelper(Terminal* self): terminal { self } {}
[[nodiscard]] PageSize pageSize() const noexcept override;
[[nodiscard]] bool wrappedLine(LineOffset line) const noexcept override;
[[nodiscard]] bool cellEmpty(CellLocation pos) const noexcept override;
[[nodiscard]] int cellWidth(CellLocation pos) const noexcept override;
};
SelectionHelper _selectionHelper;
SelectionHelper _extendedSelectionHelper;
TheSelectionHelper _selectionHelper;
TheSelectionHelper _extendedSelectionHelper;
// }}}

// {{{ Render buffer state
Expand Down

0 comments on commit 2503648

Please sign in to comment.