diff options
author | rhin123 <ryanrhin@gmail.com> | 2020-03-05 22:34:31 -0600 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-06 20:20:02 +0100 |
commit | 72ef3e529acc8a634cbaab3dad22c6befcc58731 (patch) | |
tree | 1ed7f24bdcd7b7067948b7da2e4015216c397134 /Libraries/LibVT | |
parent | 4ba206b7e583285939a388ca930983e33d32b28b (diff) | |
download | serenity-72ef3e529acc8a634cbaab3dad22c6befcc58731.zip |
TerminalWidget: Implement ALT selection
When holding ALT & selecting text, the terminal now forms a rectangle
selection similar to other terminal behaviors.
Diffstat (limited to 'Libraries/LibVT')
-rw-r--r-- | Libraries/LibVT/TerminalWidget.cpp | 34 | ||||
-rw-r--r-- | Libraries/LibVT/TerminalWidget.h | 3 |
2 files changed, 34 insertions, 3 deletions
diff --git a/Libraries/LibVT/TerminalWidget.cpp b/Libraries/LibVT/TerminalWidget.cpp index 9ecb9f8fcc..5108097138 100644 --- a/Libraries/LibVT/TerminalWidget.cpp +++ b/Libraries/LibVT/TerminalWidget.cpp @@ -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() diff --git a/Libraries/LibVT/TerminalWidget.h b/Libraries/LibVT/TerminalWidget.h index f518a21eb1..c62c54cba2 100644 --- a/Libraries/LibVT/TerminalWidget.h +++ b/Libraries/LibVT/TerminalWidget.h @@ -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 }; |