EmojiInputDialog.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. // FIXME: Also emit U+FE0F for single code point emojis, currently
  111. // they get shown as text glyphs if available.
  112. // This will require buttons to don't calculate their length as 2,
  113. // currently it just shows an ellipsis. It will also require some
  114. // tweaking of the mechanism that is currently being used to insert
  115. // which is a key event with a single code point.
  116. StringBuilder builder;
  117. builder.append(Utf32View(&code_point, 1));
  118. auto emoji_text = builder.to_string();
  119. auto button = Button::construct(move(emoji_text));
  120. button->set_fixed_size(button_size, button_size);
  121. button->set_button_style(Gfx::ButtonStyle::Coolbar);
  122. button->on_click = [this, button = button](auto) {
  123. m_selected_emoji_text = button->text();
  124. done(ExecResult::OK);
  125. };
  126. if (emoji.has_value())
  127. button->set_tooltip(emoji->name);
  128. code_points.empend(code_point, move(button), move(emoji));
  129. }
  130. quick_sort(code_points, [](auto const& lhs, auto const& rhs) {
  131. if (lhs.emoji.has_value() && rhs.emoji.has_value())
  132. return lhs.emoji->display_order < rhs.emoji->display_order;
  133. if (lhs.emoji.has_value())
  134. return true;
  135. if (rhs.emoji.has_value())
  136. return false;
  137. return lhs.code_point < rhs.code_point;
  138. });
  139. return code_points;
  140. }
  141. void EmojiInputDialog::update_displayed_emoji()
  142. {
  143. ScopeGuard guard { [&] { m_emojis_widget->set_updates_enabled(true); } };
  144. m_emojis_widget->set_updates_enabled(false);
  145. m_emojis_widget->remove_all_children();
  146. constexpr size_t columns = 18;
  147. size_t rows = ceil_div(m_emojis.size(), columns);
  148. size_t index = 0;
  149. for (size_t row = 0; row < rows && index < m_emojis.size(); ++row) {
  150. auto& horizontal_container = m_emojis_widget->add<Widget>();
  151. horizontal_container.set_preferred_height(SpecialDimension::Fit);
  152. auto& horizontal_layout = horizontal_container.set_layout<HorizontalBoxLayout>();
  153. horizontal_layout.set_spacing(0);
  154. for (size_t column = 0; column < columns; ++column) {
  155. bool found_match = false;
  156. while (!found_match && (index < m_emojis.size())) {
  157. auto& emoji = m_emojis[index++];
  158. if (m_selected_category.has_value()) {
  159. if (!emoji.emoji.has_value() || emoji.emoji->group != m_selected_category)
  160. continue;
  161. }
  162. if (emoji.emoji.has_value())
  163. found_match = emoji.emoji->name.contains(m_search_box->text(), CaseSensitivity::CaseInsensitive);
  164. else
  165. found_match = m_search_box->text().is_empty();
  166. if (found_match)
  167. horizontal_container.add_child(*emoji.button);
  168. }
  169. }
  170. }
  171. }
  172. void EmojiInputDialog::event(Core::Event& event)
  173. {
  174. if (event.type() == Event::KeyDown) {
  175. auto& key_event = static_cast<KeyEvent&>(event);
  176. if (key_event.key() == Key_Escape) {
  177. done(ExecResult::Cancel);
  178. return;
  179. }
  180. }
  181. Dialog::event(event);
  182. }
  183. }