From e5a9f030f24d9036d15864ad0df7893e889a4f6f Mon Sep 17 00:00:00 2001 From: Etienne Rodriguez Date: Fri, 14 Oct 2022 00:28:54 +0200 Subject: [PATCH] LibGUI: Insert first displayed emoji on return This enables users to insert emojis without using the mouse by searching for it in the EmojiInputDialog and then hitting return. --- .../Libraries/LibGUI/EmojiInputDialog.cpp | 25 ++++++++++++++++--- Userland/Libraries/LibGUI/EmojiInputDialog.h | 3 +++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibGUI/EmojiInputDialog.cpp b/Userland/Libraries/LibGUI/EmojiInputDialog.cpp index 85b4db6a60..682de4192d 100644 --- a/Userland/Libraries/LibGUI/EmojiInputDialog.cpp +++ b/Userland/Libraries/LibGUI/EmojiInputDialog.cpp @@ -153,6 +153,10 @@ EmojiInputDialog::EmojiInputDialog(Window* parent_window) m_search_box->on_change = [this]() { update_displayed_emoji(); }; + + m_search_box->on_return_pressed = [this]() { + select_first_displayed_emoji(); + }; } auto EmojiInputDialog::supported_emoji() -> Vector @@ -182,6 +186,7 @@ auto EmojiInputDialog::supported_emoji() -> Vector builder.append_code_point(*code_point); code_points.append(*code_point); }); + auto text = builder.to_string(); auto emoji = Unicode::find_emoji_for_code_points(code_points); if (!emoji.has_value()) { @@ -193,7 +198,7 @@ auto EmojiInputDialog::supported_emoji() -> Vector auto button = EmojiButton::construct(move(code_points)); button->set_fixed_size(button_size, button_size); button->set_button_style(Gfx::ButtonStyle::Coolbar); - button->on_click = [this, text = builder.to_string()](auto) { + button->on_click = [this, text](auto) { m_selected_emoji_text = move(text); done(ExecResult::OK); }; @@ -201,7 +206,7 @@ auto EmojiInputDialog::supported_emoji() -> Vector if (!emoji->name.is_empty()) button->set_tooltip(emoji->name); - emojis.empend(move(button), emoji.release_value()); + emojis.empend(move(button), emoji.release_value(), move(text)); } quick_sort(emojis, [](auto const& lhs, auto const& rhs) { @@ -217,6 +222,7 @@ void EmojiInputDialog::update_displayed_emoji() m_emojis_widget->set_updates_enabled(false); m_emojis_widget->remove_all_children(); + m_first_displayed_emoji = nullptr; constexpr size_t columns = 18; size_t rows = ceil_div(m_emojis.size(), columns); @@ -247,13 +253,26 @@ void EmojiInputDialog::update_displayed_emoji() found_match = result.score > 0; } - if (found_match) + if (found_match) { horizontal_container.add_child(*emoji.button); + + if (m_first_displayed_emoji == nullptr) + m_first_displayed_emoji = &emoji; + } } } } } +void EmojiInputDialog::select_first_displayed_emoji() +{ + if (m_first_displayed_emoji == nullptr) + return; + + m_selected_emoji_text = m_first_displayed_emoji->text; + done(ExecResult::OK); +} + void EmojiInputDialog::event(Core::Event& event) { if (event.type() == Event::KeyDown) { diff --git a/Userland/Libraries/LibGUI/EmojiInputDialog.h b/Userland/Libraries/LibGUI/EmojiInputDialog.h index 70d6bc7c46..beb139520c 100644 --- a/Userland/Libraries/LibGUI/EmojiInputDialog.h +++ b/Userland/Libraries/LibGUI/EmojiInputDialog.h @@ -19,6 +19,7 @@ class EmojiInputDialog final : public Dialog { struct Emoji { RefPtr