1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-23 20:27:35 +00:00

LibGUI: Add a search box to filter EmojiInputDialog by code point names

This commit is contained in:
Timothy Flynn 2022-09-06 10:59:44 -04:00 committed by Linus Groh
parent a885406511
commit eeb7b153a2
4 changed files with 33 additions and 6 deletions

View file

@ -136,4 +136,4 @@ set(GENERATED_SOURCES
) )
serenity_lib(LibGUI gui) serenity_lib(LibGUI gui)
target_link_libraries(LibGUI LibCore LibGfx LibIPC LibThreading LibRegex LibSyntax LibConfig) target_link_libraries(LibGUI LibCore LibGfx LibIPC LibThreading LibRegex LibSyntax LibConfig LibUnicode)

View file

@ -17,6 +17,8 @@
#include <LibGUI/Event.h> #include <LibGUI/Event.h>
#include <LibGUI/Frame.h> #include <LibGUI/Frame.h>
#include <LibGUI/ScrollableContainerWidget.h> #include <LibGUI/ScrollableContainerWidget.h>
#include <LibGUI/TextBox.h>
#include <LibUnicode/CharacterTypes.h>
#include <stdlib.h> #include <stdlib.h>
namespace GUI { namespace GUI {
@ -32,6 +34,7 @@ EmojiInputDialog::EmojiInputDialog(Window* parent_window)
resize(400, 300); resize(400, 300);
auto& scrollable_container = *main_widget.find_descendant_of_type_named<GUI::ScrollableContainerWidget>("scrollable_container"sv); auto& scrollable_container = *main_widget.find_descendant_of_type_named<GUI::ScrollableContainerWidget>("scrollable_container"sv);
m_search_box = main_widget.find_descendant_of_type_named<GUI::TextBox>("search_box"sv);
m_emojis_widget = main_widget.find_descendant_of_type_named<GUI::Widget>("emojis"sv); m_emojis_widget = main_widget.find_descendant_of_type_named<GUI::Widget>("emojis"sv);
m_emojis = supported_emoji(); m_emojis = supported_emoji();
@ -42,6 +45,10 @@ EmojiInputDialog::EmojiInputDialog(Window* parent_window)
if (!is_active_window) if (!is_active_window)
close(); close();
}; };
m_search_box->on_change = [this]() {
update_displayed_emoji();
};
} }
auto EmojiInputDialog::supported_emoji() -> Vector<Emoji> auto EmojiInputDialog::supported_emoji() -> Vector<Emoji>
@ -63,6 +70,7 @@ auto EmojiInputDialog::supported_emoji() -> Vector<Emoji>
continue; continue;
u32 code_point = strtoul(basename.to_string().characters() + 2, nullptr, 16); u32 code_point = strtoul(basename.to_string().characters() + 2, nullptr, 16);
auto name = Unicode::code_point_display_name(code_point);
// FIXME: Also emit U+FE0F for single code point emojis, currently // FIXME: Also emit U+FE0F for single code point emojis, currently
// they get shown as text glyphs if available. // they get shown as text glyphs if available.
@ -82,7 +90,7 @@ auto EmojiInputDialog::supported_emoji() -> Vector<Emoji>
done(ExecResult::OK); done(ExecResult::OK);
}; };
code_points.empend(code_point, move(button)); code_points.empend(code_point, move(name), move(button));
} }
return code_points; return code_points;
@ -93,20 +101,32 @@ void EmojiInputDialog::update_displayed_emoji()
ScopeGuard guard { [&] { m_emojis_widget->set_updates_enabled(true); } }; ScopeGuard guard { [&] { m_emojis_widget->set_updates_enabled(true); } };
m_emojis_widget->set_updates_enabled(false); m_emojis_widget->set_updates_enabled(false);
m_emojis_widget->remove_all_children();
constexpr size_t columns = 18; constexpr size_t columns = 18;
size_t rows = ceil_div(m_emojis.size(), columns); size_t rows = ceil_div(m_emojis.size(), columns);
size_t index = 0; size_t index = 0;
for (size_t row = 0; row < rows && index < m_emojis.size(); ++row) { for (size_t row = 0; row < rows && index < m_emojis.size(); ++row) {
auto& horizontal_container = m_emojis_widget->add<Widget>(); auto& horizontal_container = m_emojis_widget->add<Widget>();
horizontal_container.set_preferred_height(SpecialDimension::Fit);
auto& horizontal_layout = horizontal_container.set_layout<HorizontalBoxLayout>(); auto& horizontal_layout = horizontal_container.set_layout<HorizontalBoxLayout>();
horizontal_layout.set_spacing(0); horizontal_layout.set_spacing(0);
for (size_t column = 0; column < columns; ++column) { for (size_t column = 0; column < columns; ++column) {
if (index < m_emojis.size()) { bool found_match = false;
while (!found_match && (index < m_emojis.size())) {
auto& emoji = m_emojis[index++]; auto& emoji = m_emojis[index++];
if (emoji.name.has_value())
found_match = emoji.name->contains(m_search_box->text(), CaseSensitivity::CaseInsensitive);
else
found_match = m_search_box->text().is_empty();
if (found_match)
horizontal_container.add_child(*emoji.button); horizontal_container.add_child(*emoji.button);
} else {
horizontal_container.add<Widget>();
} }
} }
} }

View file

@ -6,6 +6,11 @@
margins: [4] margins: [4]
} }
@GUI::TextBox {
name: "search_box"
fixed_height: 22
}
@GUI::ScrollableContainerWidget { @GUI::ScrollableContainerWidget {
name: "scrollable_container" name: "scrollable_container"
content_widget: @GUI::Widget { content_widget: @GUI::Widget {

View file

@ -15,6 +15,7 @@ class EmojiInputDialog final : public Dialog {
struct Emoji { struct Emoji {
u32 code_point { 0 }; u32 code_point { 0 };
Optional<String> name;
RefPtr<Button> button; RefPtr<Button> button;
}; };
@ -28,6 +29,7 @@ private:
Vector<Emoji> supported_emoji(); Vector<Emoji> supported_emoji();
void update_displayed_emoji(); void update_displayed_emoji();
RefPtr<TextBox> m_search_box;
RefPtr<Widget> m_emojis_widget; RefPtr<Widget> m_emojis_widget;
Vector<Emoji> m_emojis; Vector<Emoji> m_emojis;
String m_selected_emoji_text; String m_selected_emoji_text;