EmojiInputDialog.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. EmojiCateogry { Unicode::EmojiGroup::SerenityOS, "/res/emoji/U+10CD0B.png"sv },
  42. };
  43. static void resize_bitmap_if_needed(NonnullRefPtr<Gfx::Bitmap>& bitmap)
  44. {
  45. constexpr int max_icon_size = 12;
  46. if ((bitmap->width() > max_icon_size) || (bitmap->height() > max_icon_size)) {
  47. auto x_ratio = static_cast<float>(max_icon_size) / static_cast<float>(bitmap->width());
  48. auto y_ratio = static_cast<float>(max_icon_size) / static_cast<float>(bitmap->height());
  49. auto ratio = min(x_ratio, y_ratio);
  50. bitmap = bitmap->scaled(ratio, ratio).release_value_but_fixme_should_propagate_errors();
  51. }
  52. }
  53. EmojiInputDialog::EmojiInputDialog(Window* parent_window)
  54. : Dialog(parent_window)
  55. , m_category_action_group(make<ActionGroup>())
  56. {
  57. auto& main_widget = set_main_widget<Frame>();
  58. if (!main_widget.load_from_gml(emoji_input_dialog_gml))
  59. VERIFY_NOT_REACHED();
  60. set_frameless(true);
  61. set_blocks_command_palette(true);
  62. set_blocks_emoji_input(true);
  63. set_window_mode(GUI::WindowMode::CaptureInput);
  64. resize(400, 300);
  65. auto& scrollable_container = *main_widget.find_descendant_of_type_named<GUI::ScrollableContainerWidget>("scrollable_container"sv);
  66. m_search_box = main_widget.find_descendant_of_type_named<GUI::TextBox>("search_box"sv);
  67. m_toolbar = main_widget.find_descendant_of_type_named<GUI::Toolbar>("toolbar"sv);
  68. m_emojis_widget = main_widget.find_descendant_of_type_named<GUI::Widget>("emojis"sv);
  69. m_emojis = supported_emoji();
  70. m_category_action_group->set_exclusive(true);
  71. m_category_action_group->set_unchecking_allowed(true);
  72. for (auto const& category : s_emoji_groups) {
  73. auto name = Unicode::emoji_group_to_string(category.group);
  74. auto tooltip = name.replace("&"sv, "&&"sv, ReplaceMode::FirstOnly);
  75. auto bitmap = Gfx::Bitmap::try_load_from_file(category.emoji).release_value_but_fixme_should_propagate_errors();
  76. resize_bitmap_if_needed(bitmap);
  77. auto set_filter_action = Action::create_checkable(
  78. tooltip, bitmap, [this, group = category.group](auto& action) {
  79. if (action.is_checked())
  80. m_selected_category = group;
  81. else
  82. m_selected_category = {};
  83. m_search_box->set_text({}, AllowCallback::No);
  84. update_displayed_emoji();
  85. },
  86. this);
  87. m_category_action_group->add_action(*set_filter_action);
  88. m_toolbar->add_action(*set_filter_action);
  89. }
  90. scrollable_container.horizontal_scrollbar().set_visible(false);
  91. update_displayed_emoji();
  92. on_active_input_change = [this](bool is_active_input) {
  93. if (!is_active_input)
  94. close();
  95. };
  96. on_input_preemption = [this](InputPreemptor preemptor) {
  97. if (preemptor != InputPreemptor::ContextMenu)
  98. close();
  99. };
  100. m_search_box->on_change = [this]() {
  101. update_displayed_emoji();
  102. };
  103. }
  104. auto EmojiInputDialog::supported_emoji() -> Vector<Emoji>
  105. {
  106. constexpr int button_size = 20;
  107. Vector<Emoji> code_points;
  108. Core::DirIterator dt("/res/emoji", Core::DirIterator::SkipDots);
  109. while (dt.has_next()) {
  110. auto filename = dt.next_path();
  111. auto lexical_path = LexicalPath(filename);
  112. if (lexical_path.extension() != "png")
  113. continue;
  114. auto basename = lexical_path.basename();
  115. if (!basename.starts_with("U+"sv))
  116. continue;
  117. // FIXME: Handle multi code point emojis.
  118. if (basename.contains('_'))
  119. continue;
  120. u32 code_point = strtoul(basename.to_string().characters() + 2, nullptr, 16);
  121. auto emoji = Unicode::find_emoji_for_code_points({ code_point });
  122. if (!emoji.has_value()) {
  123. emoji = Unicode::Emoji {};
  124. emoji->group = Unicode::EmojiGroup::Unknown;
  125. emoji->display_order = NumericLimits<u32>::max();
  126. }
  127. // FIXME: Also emit U+FE0F for single code point emojis, currently
  128. // they get shown as text glyphs if available.
  129. // This will require buttons to don't calculate their length as 2,
  130. // currently it just shows an ellipsis. It will also require some
  131. // tweaking of the mechanism that is currently being used to insert
  132. // which is a key event with a single code point.
  133. StringBuilder builder;
  134. builder.append(Utf32View(&code_point, 1));
  135. auto emoji_text = builder.to_string();
  136. auto button = Button::construct(move(emoji_text));
  137. button->set_fixed_size(button_size, button_size);
  138. button->set_button_style(Gfx::ButtonStyle::Coolbar);
  139. button->on_click = [this, button = button](auto) {
  140. m_selected_emoji_text = button->text();
  141. done(ExecResult::OK);
  142. };
  143. if (!emoji->name.is_empty())
  144. button->set_tooltip(emoji->name);
  145. code_points.empend(move(button), emoji.release_value());
  146. }
  147. quick_sort(code_points, [](auto const& lhs, auto const& rhs) {
  148. return lhs.emoji.display_order < rhs.emoji.display_order;
  149. });
  150. return code_points;
  151. }
  152. void EmojiInputDialog::update_displayed_emoji()
  153. {
  154. ScopeGuard guard { [&] { m_emojis_widget->set_updates_enabled(true); } };
  155. m_emojis_widget->set_updates_enabled(false);
  156. m_emojis_widget->remove_all_children();
  157. constexpr size_t columns = 18;
  158. size_t rows = ceil_div(m_emojis.size(), columns);
  159. size_t index = 0;
  160. for (size_t row = 0; row < rows && index < m_emojis.size(); ++row) {
  161. auto& horizontal_container = m_emojis_widget->add<Widget>();
  162. horizontal_container.set_preferred_height(SpecialDimension::Fit);
  163. auto& horizontal_layout = horizontal_container.set_layout<HorizontalBoxLayout>();
  164. horizontal_layout.set_spacing(0);
  165. for (size_t column = 0; column < columns; ++column) {
  166. bool found_match = false;
  167. while (!found_match && (index < m_emojis.size())) {
  168. auto& emoji = m_emojis[index++];
  169. if (m_selected_category.has_value() && emoji.emoji.group != m_selected_category)
  170. continue;
  171. if (!emoji.emoji.name.is_empty())
  172. found_match = emoji.emoji.name.contains(m_search_box->text(), CaseSensitivity::CaseInsensitive);
  173. else
  174. found_match = m_search_box->text().is_empty();
  175. if (found_match)
  176. horizontal_container.add_child(*emoji.button);
  177. }
  178. }
  179. }
  180. }
  181. void EmojiInputDialog::event(Core::Event& event)
  182. {
  183. if (event.type() == Event::KeyDown) {
  184. auto& key_event = static_cast<KeyEvent&>(event);
  185. if (key_event.key() == Key_Escape) {
  186. done(ExecResult::Cancel);
  187. return;
  188. }
  189. }
  190. Dialog::event(event);
  191. }
  192. }