1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:38:11 +00:00

GTextEditor: Add Home/End and Ctrl+Home/Ctrl+End navigation shortcuts.

For start/end of line and start/end of document respectively.
This commit is contained in:
Andreas Kling 2019-03-07 13:21:51 +01:00
parent 8dcec749ed
commit 6a6bcc5daf
2 changed files with 36 additions and 18 deletions

View file

@ -121,7 +121,7 @@ void GTextEditor::paint_event(GPaintEvent& event)
void GTextEditor::keydown_event(GKeyEvent& event) void GTextEditor::keydown_event(GKeyEvent& event)
{ {
if (event.key() == KeyCode::Key_Up) { if (!event.modifiers() && event.key() == KeyCode::Key_Up) {
if (m_cursor.line() > 0) { if (m_cursor.line() > 0) {
int new_line = m_cursor.line() - 1; int new_line = m_cursor.line() - 1;
int new_column = min(m_cursor.column(), m_lines[new_line].length()); int new_column = min(m_cursor.column(), m_lines[new_line].length());
@ -129,7 +129,7 @@ void GTextEditor::keydown_event(GKeyEvent& event)
} }
return; return;
} }
if (event.key() == KeyCode::Key_Down) { if (!event.modifiers() && event.key() == KeyCode::Key_Down) {
if (m_cursor.line() < (m_lines.size() - 1)) { if (m_cursor.line() < (m_lines.size() - 1)) {
int new_line = m_cursor.line() + 1; int new_line = m_cursor.line() + 1;
int new_column = min(m_cursor.column(), m_lines[new_line].length()); int new_column = min(m_cursor.column(), m_lines[new_line].length());
@ -137,20 +137,36 @@ void GTextEditor::keydown_event(GKeyEvent& event)
} }
return; return;
} }
if (event.key() == KeyCode::Key_Left) { if (!event.modifiers() && event.key() == KeyCode::Key_Left) {
if (m_cursor.column() > 0) { if (m_cursor.column() > 0) {
int new_column = m_cursor.column() - 1; int new_column = m_cursor.column() - 1;
set_cursor(m_cursor.line(), new_column); set_cursor(m_cursor.line(), new_column);
} }
return; return;
} }
if (event.key() == KeyCode::Key_Right) { if (!event.modifiers() && event.key() == KeyCode::Key_Right) {
if (m_cursor.column() < (m_lines[m_cursor.line()].length())) { if (m_cursor.column() < current_line().length()) {
int new_column = m_cursor.column() + 1; int new_column = m_cursor.column() + 1;
set_cursor(m_cursor.line(), new_column); set_cursor(m_cursor.line(), new_column);
} }
return; return;
} }
if (!event.modifiers() && event.key() == KeyCode::Key_Home) {
set_cursor(m_cursor.line(), 0);
return;
}
if (!event.modifiers() && event.key() == KeyCode::Key_End) {
set_cursor(m_cursor.line(), current_line().length());
return;
}
if (event.ctrl() && event.key() == KeyCode::Key_Home) {
set_cursor(0, 0);
return;
}
if (event.ctrl() && event.key() == KeyCode::Key_End) {
set_cursor(line_count() - 1, m_lines[line_count() - 1].length());
return;
}
return GWidget::keydown_event(event); return GWidget::keydown_event(event);
} }
@ -169,11 +185,10 @@ Rect GTextEditor::cursor_content_rect() const
if (!m_cursor.is_valid()) if (!m_cursor.is_valid())
return { }; return { };
ASSERT(!m_lines.is_empty()); ASSERT(!m_lines.is_empty());
auto& line = m_lines[m_cursor.line()]; ASSERT(m_cursor.column() <= (current_line().text().length() + 1));
ASSERT(m_cursor.column() <= (line.text().length() + 1));
int x = 0; int x = 0;
for (int i = 0; i < m_cursor.column(); ++i) for (int i = 0; i < m_cursor.column(); ++i)
x += font().glyph_width(line.text()[i]); x += font().glyph_width(current_line().text()[i]);
return { x, m_cursor.line() * line_height(), 1, line_height() }; return { x, m_cursor.line() * line_height(), 1, line_height() };
} }

View file

@ -55,16 +55,6 @@ private:
virtual void timer_event(GTimerEvent&) override; virtual void timer_event(GTimerEvent&) override;
virtual bool accepts_focus() const override { return true; } virtual bool accepts_focus() const override { return true; }
void update_scrollbar_ranges();
Rect line_content_rect(int item_index) const;
Rect line_widget_rect(int line_index) const;
Rect cursor_content_rect() const;
void update_cursor();
void set_cursor(int line, int column);
GScrollBar* m_vertical_scrollbar { nullptr };
GScrollBar* m_horizontal_scrollbar { nullptr };
class Line { class Line {
public: public:
Line() { } Line() { }
@ -78,6 +68,19 @@ private:
String m_text; String m_text;
mutable int m_cached_width { -1 }; mutable int m_cached_width { -1 };
}; };
void update_scrollbar_ranges();
Rect line_content_rect(int item_index) const;
Rect line_widget_rect(int line_index) const;
Rect cursor_content_rect() const;
void update_cursor();
void set_cursor(int line, int column);
Line& current_line() { return m_lines[m_cursor.line()]; }
const Line& current_line() const { return m_lines[m_cursor.line()]; }
GScrollBar* m_vertical_scrollbar { nullptr };
GScrollBar* m_horizontal_scrollbar { nullptr };
Vector<Line> m_lines; Vector<Line> m_lines;
GTextPosition m_cursor; GTextPosition m_cursor;
bool m_cursor_state { true }; bool m_cursor_state { true };