TerminalWidget: Implement ALT selection

When holding ALT & selecting text, the terminal now forms a rectangle
selection similar to other terminal behaviors.
This commit is contained in:
rhin123 2020-03-05 22:34:31 -06:00 committed by Andreas Kling
parent 4ba206b7e5
commit 72ef3e529a
Notes: sideshowbarker 2024-07-19 08:51:52 +09:00
2 changed files with 34 additions and 3 deletions

View File

@ -235,6 +235,9 @@ void TerminalWidget::keydown_event(GUI::KeyEvent& event)
}
write(m_ptm_fd, "\033[6~", 4);
return;
case KeyCode::Key_Alt:
m_alt_key_held = true;
return;
default:
break;
}
@ -272,6 +275,17 @@ void TerminalWidget::keydown_event(GUI::KeyEvent& event)
m_scrollbar->set_value(m_scrollbar->max());
}
void TerminalWidget::keyup_event(GUI::KeyEvent& event)
{
switch (event.key()) {
case KeyCode::Key_Alt:
m_alt_key_held = false;
return;
default:
break;
}
}
void TerminalWidget::paint_event(GUI::PaintEvent& event)
{
GUI::Frame::paint_event(event);
@ -490,6 +504,15 @@ bool TerminalWidget::selection_contains(const VT::Position& position) const
if (!has_selection())
return false;
if (m_rectangle_selection) {
auto min_selection_column = min(m_selection_start.column(), m_selection_end.column());
auto max_selection_column = max(m_selection_start.column(), m_selection_end.column());
auto min_selection_row = min(m_selection_start.row(), m_selection_end.row());
auto max_selection_row = max(m_selection_start.row(), m_selection_end.row());
return position.column() >= min_selection_column && position.column() <= max_selection_column && position.row() >= min_selection_row && position.row() <= max_selection_row;
}
return position >= normalized_selection_start() && position <= normalized_selection_end();
}
@ -569,6 +592,11 @@ void TerminalWidget::mousedown_event(GUI::MouseEvent& event)
m_selection_start = buffer_position_at(event.position());
m_selection_end = {};
}
if (m_alt_key_held)
m_rectangle_selection = true;
else if (m_rectangle_selection)
m_rectangle_selection = false;
update();
}
}
@ -613,7 +641,7 @@ String TerminalWidget::selected_text() const
break;
}
builder.append(line.characters[column]);
if (column == line.m_length - 1) {
if (column == line.m_length - 1 || (m_rectangle_selection && column == last_column)) {
builder.append('\n');
}
}
@ -624,12 +652,12 @@ String TerminalWidget::selected_text() const
int TerminalWidget::first_selection_column_on_row(int row) const
{
return row == normalized_selection_start().row() ? normalized_selection_start().column() : 0;
return row == normalized_selection_start().row() || m_rectangle_selection ? normalized_selection_start().column() : 0;
}
int TerminalWidget::last_selection_column_on_row(int row) const
{
return row == normalized_selection_end().row() ? normalized_selection_end().column() : m_terminal.columns() - 1;
return row == normalized_selection_end().row() || m_rectangle_selection ? normalized_selection_end().column() : m_terminal.columns() - 1;
}
void TerminalWidget::terminal_history_changed()

View File

@ -92,6 +92,7 @@ private:
virtual void paint_event(GUI::PaintEvent&) override;
virtual void resize_event(GUI::ResizeEvent&) override;
virtual void keydown_event(GUI::KeyEvent&) override;
virtual void keyup_event(GUI::KeyEvent&) override;
virtual void mousedown_event(GUI::MouseEvent&) override;
virtual void mousemove_event(GUI::MouseEvent&) override;
virtual void mousewheel_event(GUI::MouseEvent&) override;
@ -130,6 +131,8 @@ private:
bool m_should_beep { false };
bool m_belling { false };
bool m_alt_key_held { false };
bool m_rectangle_selection { false };
int m_pixel_width { 0 };
int m_pixel_height { 0 };