From 6a09d893295d1b3f681f88d08c9eb3e93c171c5e Mon Sep 17 00:00:00 2001 From: electrikmilk Date: Thu, 8 Sep 2022 18:06:08 -0400 Subject: [PATCH] Base+LibGUI: Add insert emoji common action This adds a common action to invoke the emoji picker. --- Base/res/icons/16x16/emoji.png | Bin 0 -> 149 bytes Userland/Libraries/LibGUI/Action.h | 1 + Userland/Libraries/LibGUI/CommonActions.cpp | 7 +++++++ Userland/Libraries/LibGUI/TextEditor.cpp | 16 ++++++++++++++++ Userland/Libraries/LibGUI/TextEditor.h | 3 +++ 5 files changed, 27 insertions(+) create mode 100644 Base/res/icons/16x16/emoji.png diff --git a/Base/res/icons/16x16/emoji.png b/Base/res/icons/16x16/emoji.png new file mode 100644 index 0000000000000000000000000000000000000000..741fc047bd0813846238af493f56a8fddc0dbea5 GIT binary patch literal 149 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4mJh`2Kmqb6B!s7Vmw_OLn>}1CoC}gQE&X# z-p(iD^JTL?6- make_paste_action(Function, Core::Object* p NonnullRefPtr make_delete_action(Function, Core::Object* parent = nullptr); NonnullRefPtr make_move_to_front_action(Function, Core::Object* parent = nullptr); NonnullRefPtr make_move_to_back_action(Function, Core::Object* parent = nullptr); +NonnullRefPtr make_insert_emoji_action(Function, Core::Object* parent = nullptr); NonnullRefPtr make_fullscreen_action(Function, Core::Object* parent = nullptr); NonnullRefPtr make_quit_action(Function); NonnullRefPtr make_help_action(Function, Core::Object* parent = nullptr); diff --git a/Userland/Libraries/LibGUI/CommonActions.cpp b/Userland/Libraries/LibGUI/CommonActions.cpp index 883a74d5d7..ab91e093d0 100644 --- a/Userland/Libraries/LibGUI/CommonActions.cpp +++ b/Userland/Libraries/LibGUI/CommonActions.cpp @@ -96,6 +96,13 @@ NonnullRefPtr make_paste_action(Function callback, Core:: return action; } +NonnullRefPtr make_insert_emoji_action(Function callback, Core::Object* parent) +{ + auto action = Action::create("&Insert Emoji", { Mod_Ctrl | Mod_Alt, Key_Space }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/emoji.png"sv).release_value_but_fixme_should_propagate_errors(), move(callback), parent); + action->set_status_tip("Open the Emoji Picker"); + return action; +} + NonnullRefPtr make_fullscreen_action(Function callback, Core::Object* parent) { auto action = Action::create("&Fullscreen", { Mod_None, Key_F11 }, move(callback), parent); diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index cf2d80c8e9..dc9a006017 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -105,6 +106,7 @@ void TextEditor::create_actions() this); } m_select_all_action = CommonActions::make_select_all_action([this](auto&) { select_all(); }, this); + m_insert_emoji_action = CommonActions::make_insert_emoji_action([&](auto&) { insert_emoji(); }, this); } void TextEditor::set_text(StringView text, AllowCallback allow_callback) @@ -771,6 +773,17 @@ void TextEditor::select_all() update(); } +void TextEditor::insert_emoji() +{ + auto emoji_input_dialog = EmojiInputDialog::construct(window()); + emoji_input_dialog->set_window_mode(GUI::WindowMode::Passive); + if (emoji_input_dialog->exec() != EmojiInputDialog::ExecResult::OK) + return; + + auto emoji_code_point = emoji_input_dialog->selected_emoji_text(); + insert_at_cursor_or_replace_selection(emoji_code_point); +} + void TextEditor::keydown_event(KeyEvent& event) { if (!is_editable() && event.key() == KeyCode::Key_Tab) @@ -1667,12 +1680,14 @@ void TextEditor::set_mode(const Mode mode) case Editable: m_cut_action->set_enabled(has_selection() && !text_is_secret()); m_paste_action->set_enabled(true); + m_insert_emoji_action->set_enabled(true); set_accepts_emoji_input(true); break; case DisplayOnly: case ReadOnly: m_cut_action->set_enabled(false); m_paste_action->set_enabled(false); + m_insert_emoji_action->set_enabled(false); set_accepts_emoji_input(false); break; default: @@ -1717,6 +1732,7 @@ void TextEditor::context_menu_event(ContextMenuEvent& event) m_context_menu->add_action(paste_action()); m_context_menu->add_separator(); m_context_menu->add_action(select_all_action()); + m_context_menu->add_action(insert_emoji_action()); if (is_multi_line()) { m_context_menu->add_separator(); m_context_menu->add_action(go_to_line_action()); diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h index fb1721f05c..9b892e0167 100644 --- a/Userland/Libraries/LibGUI/TextEditor.h +++ b/Userland/Libraries/LibGUI/TextEditor.h @@ -150,6 +150,7 @@ public: void delete_previous_char(); void delete_from_line_start_to_cursor(); void select_all(); + void insert_emoji(); void select_current_line(); virtual void undo(); virtual void redo(); @@ -176,6 +177,7 @@ public: Action& paste_action() { return *m_paste_action; } Action& go_to_line_action() { return *m_go_to_line_action; } Action& select_all_action() { return *m_select_all_action; } + Action& insert_emoji_action() { return *m_insert_emoji_action; } void add_custom_context_menu_action(Action&); @@ -394,6 +396,7 @@ private: RefPtr m_paste_action; RefPtr m_go_to_line_action; RefPtr m_select_all_action; + RefPtr m_insert_emoji_action; Core::ElapsedTimer m_triple_click_timer; NonnullRefPtrVector m_custom_context_menu_actions;