1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:08:11 +00:00

LibGUI: Move code to display emoji buttons to a helper function

For a search box to be added, this code will need to be re-invoked as
the search query updates.
This commit is contained in:
Timothy Flynn 2022-09-06 08:25:02 -04:00 committed by Linus Groh
parent 3aaaacdb3a
commit a511dec5ca
2 changed files with 24 additions and 17 deletions

View file

@ -52,23 +52,31 @@ 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);
auto& 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);
auto code_points = supported_emoji_code_points(); m_code_points = supported_emoji_code_points();
size_t index = 0;
size_t columns = 18;
size_t rows = ceil_div(code_points.size(), columns);
constexpr int button_size = 20;
scrollable_container.horizontal_scrollbar().set_visible(false); scrollable_container.horizontal_scrollbar().set_visible(false);
update_displayed_emoji();
for (size_t row = 0; row < rows && index < code_points.size(); ++row) { on_active_window_change = [this](bool is_active_window) {
auto& horizontal_container = emojis_widget.add<Widget>(); if (!is_active_window)
close();
};
}
void EmojiInputDialog::update_displayed_emoji()
{
constexpr int button_size = 20;
constexpr size_t columns = 18;
size_t rows = ceil_div(m_code_points.size(), columns);
size_t index = 0;
for (size_t row = 0; row < rows && index < m_code_points.size(); ++row) {
auto& horizontal_container = m_emojis_widget->add<Widget>();
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 < code_points.size()) { if (index < m_code_points.size()) {
// 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.
// This will require buttons to don't calculate their length as 2, // This will require buttons to don't calculate their length as 2,
@ -76,7 +84,7 @@ EmojiInputDialog::EmojiInputDialog(Window* parent_window)
// tweaking of the mechanism that is currently being used to insert // tweaking of the mechanism that is currently being used to insert
// which is a key event with a single code point. // which is a key event with a single code point.
StringBuilder builder; StringBuilder builder;
builder.append(Utf32View(&code_points[index++], 1)); builder.append(Utf32View(&m_code_points[index++], 1));
auto emoji_text = builder.to_string(); auto emoji_text = builder.to_string();
auto& button = horizontal_container.add<Button>(emoji_text); auto& button = horizontal_container.add<Button>(emoji_text);
button.set_fixed_size(button_size, button_size); button.set_fixed_size(button_size, button_size);
@ -90,11 +98,6 @@ EmojiInputDialog::EmojiInputDialog(Window* parent_window)
} }
} }
} }
on_active_window_change = [this](bool is_active_window) {
if (!is_active_window)
close();
};
} }
void EmojiInputDialog::event(Core::Event& event) void EmojiInputDialog::event(Core::Event& event)

View file

@ -20,6 +20,10 @@ private:
virtual void event(Core::Event&) override; virtual void event(Core::Event&) override;
explicit EmojiInputDialog(Window* parent_window); explicit EmojiInputDialog(Window* parent_window);
void update_displayed_emoji();
RefPtr<Widget> m_emojis_widget;
Vector<u32> m_code_points;
String m_selected_emoji_text; String m_selected_emoji_text;
}; };