Skip to content

Commit

Permalink
TextEditor: The delete key should work even when there's no selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Mar 20, 2019
1 parent be45337 commit ed2303e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Applications/TextEditor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int main(int argc, char** argv)
});

auto delete_action = GAction::create("Delete", { 0, Key_Delete }, GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/16x16/delete.rgb", { 16, 16 }), [&] (const GAction&) {
text_editor->delete_selection();
text_editor->do_delete();
});

auto menubar = make<GMenuBar>();
Expand Down
49 changes: 27 additions & 22 deletions LibGUI/GTextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,28 +382,7 @@ void GTextEditor::keydown_event(GKeyEvent& event)
}

if (event.key() == KeyCode::Key_Delete) {
if (has_selection()) {
delete_selection();
return;
}
if (m_cursor.column() < current_line().length()) {
// Delete within line
current_line().remove(m_cursor.column());
update_content_size();
update_cursor();
return;
}
if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
// Delete at end of line; merge with next line
auto& next_line = *m_lines[m_cursor.line() + 1];
int previous_length = current_line().length();
current_line().append(next_line.characters(), next_line.length());
m_lines.remove(m_cursor.line() + 1);
update_content_size();
update();
set_cursor(m_cursor.line(), previous_length);
return;
}
do_delete();
return;
}

Expand All @@ -413,6 +392,32 @@ void GTextEditor::keydown_event(GKeyEvent& event)
return GWidget::keydown_event(event);
}

void GTextEditor::do_delete()
{
if (has_selection()) {
delete_selection();
return;
}
if (m_cursor.column() < current_line().length()) {
// Delete within line
current_line().remove(m_cursor.column());
update_content_size();
update_cursor();
return;
}
if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
// Delete at end of line; merge with next line
auto& next_line = *m_lines[m_cursor.line() + 1];
int previous_length = current_line().length();
current_line().append(next_line.characters(), next_line.length());
m_lines.remove(m_cursor.line() + 1);
update_content_size();
update();
set_cursor(m_cursor.line(), previous_length);
return;
}
}

void GTextEditor::insert_at_cursor(const String& text)
{
// FIXME: This should obviously not be implemented this way.
Expand Down
3 changes: 2 additions & 1 deletion LibGUI/GTextEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class GTextEditor : public GScrollableWidget {
void cut();
void copy();
void paste();
void delete_selection();
void do_delete();

Function<void(GTextEditor&)> on_return_pressed;
Function<void(GTextEditor&)> on_escape_pressed;
Expand Down Expand Up @@ -150,6 +150,7 @@ class GTextEditor : public GScrollableWidget {
Rect ruler_content_rect(int line) const;
void toggle_selection_if_needed_for_event(const GKeyEvent&);
void insert_at_cursor_or_replace_selection(const String&);
void delete_selection();

Type m_type { MultiLine };

Expand Down

0 comments on commit ed2303e

Please sign in to comment.