EmojiInputDialog.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/LexicalPath.h>
  8. #include <AK/QuickSort.h>
  9. #include <AK/ScopeGuard.h>
  10. #include <AK/StringBuilder.h>
  11. #include <AK/Utf32View.h>
  12. #include <LibCore/DirIterator.h>
  13. #include <LibGUI/Action.h>
  14. #include <LibGUI/ActionGroup.h>
  15. #include <LibGUI/BoxLayout.h>
  16. #include <LibGUI/Button.h>
  17. #include <LibGUI/EmojiInputDialog.h>
  18. #include <LibGUI/EmojiInputDialogGML.h>
  19. #include <LibGUI/Event.h>
  20. #include <LibGUI/Frame.h>
  21. #include <LibGUI/ScrollableContainerWidget.h>
  22. #include <LibGUI/TextBox.h>
  23. #include <LibGUI/Toolbar.h>
  24. #include <LibGfx/Bitmap.h>
  25. #include <stdlib.h>
  26. namespace GUI {
  27. struct EmojiCateogry {
  28. Unicode::EmojiGroup group;
  29. StringView emoji;
  30. };
  31. static constexpr auto s_emoji_groups = Array {
  32. EmojiCateogry { Unicode::EmojiGroup::SmileysAndEmotion, "/res/emoji/U+1F600.png"sv },
  33. EmojiCateogry { Unicode::EmojiGroup::PeopleAndBody, "/res/emoji/U+1FAF3.png"sv },
  34. EmojiCateogry { Unicode::EmojiGroup::AnimalsAndNature, "/res/emoji/U+1F33B.png"sv },
  35. EmojiCateogry { Unicode::EmojiGroup::FoodAndDrink, "/res/emoji/U+1F355.png"sv },
  36. EmojiCateogry { Unicode::EmojiGroup::TravelAndPlaces, "/res/emoji/U+1F3D6.png"sv },
  37. EmojiCateogry { Unicode::EmojiGroup::Activities, "/res/emoji/U+1F3B3.png"sv },
  38. EmojiCateogry { Unicode::EmojiGroup::Objects, "/res/emoji/U+1F4E6.png"sv },
  39. EmojiCateogry { Unicode::EmojiGroup::Symbols, "/res/emoji/U+2764.png"sv },
  40. EmojiCateogry { Unicode::EmojiGroup::Flags, "/res/emoji/U+1F6A9.png"sv },
  41. };
  42. EmojiInputDialog::EmojiInputDialog(Window* parent_window)
  43. : Dialog(parent_window)
  44. , m_category_action_group(make<ActionGroup>())
  45. {
  46. auto& main_widget = set_main_widget<Frame>();
  47. if (!main_widget.load_from_gml(emoji_input_dialog_gml))
  48. VERIFY_NOT_REACHED();
  49. set_frameless(true);
  50. set_blocks_command_palette(true);
  51. set_blocks_emoji_input(true);
  52. set_window_mode(GUI::WindowMode::CaptureInput);
  53. resize(400, 300);
  54. auto& scrollable_container = *main_widget.find_descendant_of_type_named<GUI::ScrollableContainerWidget>("scrollable_container"sv);
  55. m_search_box = main_widget.find_descendant_of_type_named<GUI::TextBox>("search_box"sv);
  56. m_toolbar = main_widget.find_descendant_of_type_named<GUI::Toolbar>("toolbar"sv);
  57. m_emojis_widget = main_widget.find_descendant_of_type_named<GUI::Widget>("emojis"sv);
  58. m_emojis = supported_emoji();
  59. m_category_action_group->set_exclusive(true);
  60. m_category_action_group->set_unchecking_allowed(true);
  61. for (auto const& category : s_emoji_groups) {
  62. auto name = Unicode::emoji_group_to_string(category.group);
  63. auto tooltip = name.replace("&"sv, "&&"sv, ReplaceMode::FirstOnly);
  64. auto bitmap = Gfx::Bitmap::try_load_from_file(category.emoji).release_value_but_fixme_should_propagate_errors();
  65. auto set_filter_action = Action::create_checkable(
  66. tooltip, bitmap, [this, group = category.group](auto& action) {
  67. if (action.is_checked())
  68. m_selected_category = group;
  69. else
  70. m_selected_category = {};
  71. m_search_box->set_text({}, AllowCallback::No);
  72. update_displayed_emoji();
  73. },
  74. this);
  75. m_category_action_group->add_action(*set_filter_action);
  76. m_toolbar->add_action(*set_filter_action);
  77. }
  78. scrollable_container.horizontal_scrollbar().set_visible(false);
  79. update_displayed_emoji();
  80. on_active_input_change = [this](bool is_active_input) {
  81. if (!is_active_input)
  82. close();
  83. };
  84. on_input_preemption = [this](InputPreemptor preemptor) {
  85. if (preemptor != InputPreemptor::ContextMenu)
  86. close();
  87. };
  88. m_search_box->on_change = [this]() {
  89. update_displayed_emoji();
  90. };
  91. }
  92. auto EmojiInputDialog::supported_emoji() -> Vector<Emoji>
  93. {
  94. constexpr int button_size = 20;
  95. Vector<Emoji> code_points;
  96. Core::DirIterator dt("/res/emoji", Core::DirIterator::SkipDots);
  97. while (dt.has_next()) {
  98. auto filename = dt.next_path();
  99. auto lexical_path = LexicalPath(filename);
  100. if (lexical_path.extension() != "png")
  101. continue;
  102. auto basename = lexical_path.basename();
  103. if (!basename.starts_with("U+"sv))
  104. continue;
  105. // FIXME: Handle multi code point emojis.
  106. if (basename.contains('_'))
  107. continue;
  108. u32 code_point = strtoul(basename.to_string().characters() + 2, nullptr, 16);
  109. auto emoji = Unicode::find_emoji_for_code_points({ code_point });
  110. if (!emoji.has_value()) {
  111. emoji = Unicode::Emoji {};
  112. emoji->group = Unicode::EmojiGroup::Unknown;
  113. emoji->display_order = NumericLimits<u32>::max();
  114. }
  115. // FIXME: Also emit U+FE0F for single code point emojis, currently
  116. // they get shown as text glyphs if available.
  117. // This will require buttons to don't calculate their length as 2,
  118. // currently it just shows an ellipsis. It will also require some
  119. // tweaking of the mechanism that is currently being used to insert
  120. // which is a key event with a single code point.
  121. StringBuilder builder;
  122. builder.append(Utf32View(&code_point, 1));
  123. auto emoji_text = builder.to_string();
  124. auto button = Button::construct(move(emoji_text));
  125. button->set_fixed_size(button_size, button_size);
  126. button->set_button_style(Gfx::ButtonStyle::Coolbar);
  127. button->on_click = [this, button = button](auto) {
  128. m_selected_emoji_text = button->text();
  129. done(ExecResult::OK);
  130. };
  131. if (!emoji->name.is_empty())
  132. button->set_tooltip(emoji->name);
  133. code_points.empend(move(button), emoji.release_value());
  134. }
  135. quick_sort(code_points, [](auto const& lhs, auto const& rhs) {
  136. return lhs.emoji.display_order < rhs.emoji.display_order;
  137. });
  138. return code_points;
  139. }
  140. void EmojiInputDialog::update_displayed_emoji()
  141. {
  142. ScopeGuard guard { [&] { m_emojis_widget->set_updates_enabled(true); } };
  143. m_emojis_widget->set_updates_enabled(false);
  144. m_emojis_widget->remove_all_children();
  145. constexpr size_t columns = 18;
  146. size_t rows = ceil_div(m_emojis.size(), columns);
  147. size_t index = 0;
  148. for (size_t row = 0; row < rows && index < m_emojis.size(); ++row) {
  149. auto& horizontal_container = m_emojis_widget->add<Widget>();
  150. horizontal_container.set_preferred_height(SpecialDimension::Fit);
  151. auto& horizontal_layout = horizontal_container.set_layout<HorizontalBoxLayout>();
  152. horizontal_layout.set_spacing(0);
  153. for (size_t column = 0; column < columns; ++column) {
  154. bool found_match = false;
  155. while (!found_match && (index < m_emojis.size())) {
  156. auto& emoji = m_emojis[index++];
  157. if (m_selected_category.has_value() && emoji.emoji.group != m_selected_category)
  158. continue;
  159. if (!emoji.emoji.name.is_empty())
  160. found_match = emoji.emoji.name.contains(m_search_box->text(), CaseSensitivity::CaseInsensitive);
  161. else
  162. found_match = m_search_box->text().is_empty();
  163. if (found_match)
  164. horizontal_container.add_child(*emoji.button);
  165. }
  166. }
  167. }
  168. }
  169. void EmojiInputDialog::event(Core::Event& event)
  170. {
  171. if (event.type() == Event::KeyDown) {
  172. auto& key_event = static_cast<KeyEvent&>(event);
  173. if (key_event.key() == Key_Escape) {
  174. done(ExecResult::Cancel);
  175. return;
  176. }
  177. }
  178. Dialog::event(event);
  179. }
  180. }