فهرست منبع

LibGUI: Change delete key handling from action to keydown_event

Having the delete key handling be done via an action limits our ability
to support key modifiers (e.g. ctrl+delete deleting the word in front of
the cursor).

The fact that it was an action _did_ allow us to have a delete button in
the TextEditor UI. However, this is an odd choice in the first place
that isn't common in other text editors, so I just removed it.
Andrew January 3 سال پیش
والد
کامیت
22e80bae29

+ 0 - 2
Userland/Applications/TextEditor/MainWidget.cpp

@@ -327,7 +327,6 @@ MainWidget::MainWidget()
     m_toolbar->add_action(m_editor->cut_action());
     m_toolbar->add_action(m_editor->copy_action());
     m_toolbar->add_action(m_editor->paste_action());
-    m_toolbar->add_action(m_editor->delete_action());
 
     m_toolbar->add_separator();
 
@@ -384,7 +383,6 @@ void MainWidget::initialize_menubar(GUI::Window& window)
     edit_menu.add_action(m_editor->cut_action());
     edit_menu.add_action(m_editor->copy_action());
     edit_menu.add_action(m_editor->paste_action());
-    edit_menu.add_action(m_editor->delete_action());
     edit_menu.add_separator();
     edit_menu.add_action(*m_vim_emulation_setting_action);
     edit_menu.add_separator();

+ 20 - 4
Userland/Libraries/LibGUI/TextEditor.cpp

@@ -84,7 +84,6 @@ void TextEditor::create_actions()
     m_copy_action->set_enabled(false);
     m_paste_action = CommonActions::make_paste_action([&](auto&) { paste(); }, this);
     m_paste_action->set_enabled(is_editable() && Clipboard::the().mime_type().starts_with("text/") && !Clipboard::the().data().is_empty());
-    m_delete_action = CommonActions::make_delete_action([&](auto&) { do_delete(); }, this);
     if (is_multi_line()) {
         m_go_to_line_action = Action::create(
             "Go to line...", { Mod_Ctrl, Key_L }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
@@ -834,8 +833,28 @@ void TextEditor::keydown_event(KeyEvent& event)
     }
 
     if (event.key() == KeyCode::Key_Delete) {
+        if (!is_editable())
+            return;
         if (m_autocomplete_box)
             hide_autocomplete();
+        if (has_selection()) {
+            delete_selection();
+            did_update_selection();
+            return;
+        }
+
+        if (m_cursor.column() < current_line().length()) {
+            // Delete within line
+            TextRange erased_range(m_cursor, { m_cursor.line(), m_cursor.column() + 1 });
+            execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
+            return;
+        }
+        if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
+            // Delete at end of line; merge with next line
+            TextRange erased_range(m_cursor, { m_cursor.line() + 1, 0 });
+            execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
+            return;
+        }
         return;
     }
 
@@ -1531,14 +1550,12 @@ void TextEditor::set_mode(const Mode mode)
     switch (mode) {
     case Editable:
         m_cut_action->set_enabled(has_selection() && !text_is_secret());
-        m_delete_action->set_enabled(true);
         m_paste_action->set_enabled(true);
         set_accepts_emoji_input(true);
         break;
     case DisplayOnly:
     case ReadOnly:
         m_cut_action->set_enabled(false);
-        m_delete_action->set_enabled(false);
         m_paste_action->set_enabled(false);
         set_accepts_emoji_input(false);
         break;
@@ -1577,7 +1594,6 @@ void TextEditor::context_menu_event(ContextMenuEvent& event)
         m_context_menu->add_action(cut_action());
         m_context_menu->add_action(copy_action());
         m_context_menu->add_action(paste_action());
-        m_context_menu->add_action(delete_action());
         m_context_menu->add_separator();
         m_context_menu->add_action(select_all_action());
         if (is_multi_line()) {

+ 0 - 2
Userland/Libraries/LibGUI/TextEditor.h

@@ -166,7 +166,6 @@ public:
     Action& cut_action() { return *m_cut_action; }
     Action& copy_action() { return *m_copy_action; }
     Action& paste_action() { return *m_paste_action; }
-    Action& delete_action() { return *m_delete_action; }
     Action& go_to_line_action() { return *m_go_to_line_action; }
     Action& select_all_action() { return *m_select_all_action; }
 
@@ -365,7 +364,6 @@ private:
     RefPtr<Action> m_cut_action;
     RefPtr<Action> m_copy_action;
     RefPtr<Action> m_paste_action;
-    RefPtr<Action> m_delete_action;
     RefPtr<Action> m_go_to_line_action;
     RefPtr<Action> m_select_all_action;
     Core::ElapsedTimer m_triple_click_timer;