EmojiInputDialog.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/StringUtils.h>
  12. #include <AK/Utf32View.h>
  13. #include <LibCore/DirIterator.h>
  14. #include <LibGUI/Action.h>
  15. #include <LibGUI/ActionGroup.h>
  16. #include <LibGUI/BoxLayout.h>
  17. #include <LibGUI/Button.h>
  18. #include <LibGUI/EmojiInputDialog.h>
  19. #include <LibGUI/EmojiInputDialogGML.h>
  20. #include <LibGUI/Event.h>
  21. #include <LibGUI/Frame.h>
  22. #include <LibGUI/ScrollableContainerWidget.h>
  23. #include <LibGUI/TextBox.h>
  24. #include <LibGUI/Toolbar.h>
  25. #include <LibGfx/Bitmap.h>
  26. #include <stdlib.h>
  27. namespace GUI {
  28. struct EmojiCateogry {
  29. Unicode::EmojiGroup group;
  30. StringView emoji;
  31. };
  32. static constexpr auto s_emoji_groups = Array {
  33. EmojiCateogry { Unicode::EmojiGroup::SmileysAndEmotion, "/res/emoji/U+1F600.png"sv },
  34. EmojiCateogry { Unicode::EmojiGroup::PeopleAndBody, "/res/emoji/U+1FAF3.png"sv },
  35. EmojiCateogry { Unicode::EmojiGroup::AnimalsAndNature, "/res/emoji/U+1F33B.png"sv },
  36. EmojiCateogry { Unicode::EmojiGroup::FoodAndDrink, "/res/emoji/U+1F355.png"sv },
  37. EmojiCateogry { Unicode::EmojiGroup::TravelAndPlaces, "/res/emoji/U+1F3D6.png"sv },
  38. EmojiCateogry { Unicode::EmojiGroup::Activities, "/res/emoji/U+1F3B3.png"sv },
  39. EmojiCateogry { Unicode::EmojiGroup::Objects, "/res/emoji/U+1F4E6.png"sv },
  40. EmojiCateogry { Unicode::EmojiGroup::Symbols, "/res/emoji/U+2764.png"sv },
  41. EmojiCateogry { Unicode::EmojiGroup::Flags, "/res/emoji/U+1F6A9.png"sv },
  42. EmojiCateogry { Unicode::EmojiGroup::SerenityOS, "/res/emoji/U+10CD0B.png"sv },
  43. };
  44. static void resize_bitmap_if_needed(NonnullRefPtr<Gfx::Bitmap>& bitmap)
  45. {
  46. constexpr int max_icon_size = 12;
  47. if ((bitmap->width() > max_icon_size) || (bitmap->height() > max_icon_size)) {
  48. auto x_ratio = static_cast<float>(max_icon_size) / static_cast<float>(bitmap->width());
  49. auto y_ratio = static_cast<float>(max_icon_size) / static_cast<float>(bitmap->height());
  50. auto ratio = min(x_ratio, y_ratio);
  51. bitmap = bitmap->scaled(ratio, ratio).release_value_but_fixme_should_propagate_errors();
  52. }
  53. }
  54. class EmojiButton final : public Button {
  55. C_OBJECT(EmojiButton);
  56. private:
  57. explicit EmojiButton(String emoji_icon_path)
  58. : Button()
  59. , m_emoji_icon_path(move(emoji_icon_path))
  60. {
  61. }
  62. virtual void paint_event(PaintEvent& event) override
  63. {
  64. if (m_first_paint_event) {
  65. m_first_paint_event = false;
  66. auto bitmap = Gfx::Bitmap::try_load_from_file(m_emoji_icon_path).release_value_but_fixme_should_propagate_errors();
  67. resize_bitmap_if_needed(bitmap);
  68. set_icon(move(bitmap));
  69. }
  70. Button::paint_event(event);
  71. }
  72. bool m_first_paint_event { true };
  73. String m_emoji_icon_path;
  74. };
  75. EmojiInputDialog::EmojiInputDialog(Window* parent_window)
  76. : Dialog(parent_window)
  77. , m_category_action_group(make<ActionGroup>())
  78. {
  79. auto& main_widget = set_main_widget<Frame>();
  80. if (!main_widget.load_from_gml(emoji_input_dialog_gml))
  81. VERIFY_NOT_REACHED();
  82. set_frameless(true);
  83. set_blocks_command_palette(true);
  84. set_blocks_emoji_input(true);
  85. set_window_mode(GUI::WindowMode::CaptureInput);
  86. resize(400, 300);
  87. auto& scrollable_container = *main_widget.find_descendant_of_type_named<GUI::ScrollableContainerWidget>("scrollable_container"sv);
  88. m_search_box = main_widget.find_descendant_of_type_named<GUI::TextBox>("search_box"sv);
  89. m_toolbar = main_widget.find_descendant_of_type_named<GUI::Toolbar>("toolbar"sv);
  90. m_emojis_widget = main_widget.find_descendant_of_type_named<GUI::Widget>("emojis"sv);
  91. m_emojis = supported_emoji();
  92. m_category_action_group->set_exclusive(true);
  93. m_category_action_group->set_unchecking_allowed(true);
  94. for (auto const& category : s_emoji_groups) {
  95. auto name = Unicode::emoji_group_to_string(category.group);
  96. auto tooltip = name.replace("&"sv, "&&"sv, ReplaceMode::FirstOnly);
  97. auto bitmap = Gfx::Bitmap::try_load_from_file(category.emoji).release_value_but_fixme_should_propagate_errors();
  98. resize_bitmap_if_needed(bitmap);
  99. auto set_filter_action = Action::create_checkable(
  100. tooltip, bitmap, [this, group = category.group](auto& action) {
  101. if (action.is_checked())
  102. m_selected_category = group;
  103. else
  104. m_selected_category = {};
  105. m_search_box->set_text({}, AllowCallback::No);
  106. update_displayed_emoji();
  107. },
  108. this);
  109. m_category_action_group->add_action(*set_filter_action);
  110. m_toolbar->add_action(*set_filter_action);
  111. }
  112. scrollable_container.horizontal_scrollbar().set_visible(false);
  113. update_displayed_emoji();
  114. on_active_input_change = [this](bool is_active_input) {
  115. if (!is_active_input)
  116. close();
  117. };
  118. on_input_preemption = [this](InputPreemptor preemptor) {
  119. if (preemptor != InputPreemptor::ContextMenu)
  120. close();
  121. };
  122. m_search_box->on_change = [this]() {
  123. update_displayed_emoji();
  124. };
  125. }
  126. auto EmojiInputDialog::supported_emoji() -> Vector<Emoji>
  127. {
  128. constexpr int button_size = 20;
  129. Vector<Emoji> emojis;
  130. Core::DirIterator dt("/res/emoji", Core::DirIterator::SkipDots);
  131. while (dt.has_next()) {
  132. auto filename = dt.next_full_path();
  133. auto lexical_path = LexicalPath(filename);
  134. if (lexical_path.extension() != "png")
  135. continue;
  136. auto basename = lexical_path.basename();
  137. if (!basename.starts_with("U+"sv))
  138. continue;
  139. basename = basename.substring_view(0, basename.length() - lexical_path.extension().length() - 1);
  140. StringBuilder builder;
  141. Vector<u32> code_points;
  142. basename.for_each_split_view('_', false, [&](auto segment) {
  143. auto code_point = AK::StringUtils::convert_to_uint_from_hex<u32>(segment.substring_view(2));
  144. VERIFY(code_point.has_value());
  145. builder.append_code_point(*code_point);
  146. code_points.append(*code_point);
  147. });
  148. auto emoji = Unicode::find_emoji_for_code_points(code_points);
  149. if (!emoji.has_value()) {
  150. emoji = Unicode::Emoji {};
  151. emoji->group = Unicode::EmojiGroup::Unknown;
  152. emoji->display_order = NumericLimits<u32>::max();
  153. }
  154. auto button = EmojiButton::construct(move(filename));
  155. button->set_fixed_size(button_size, button_size);
  156. button->set_button_style(Gfx::ButtonStyle::Coolbar);
  157. button->on_click = [this, text = builder.to_string()](auto) {
  158. m_selected_emoji_text = move(text);
  159. done(ExecResult::OK);
  160. };
  161. if (!emoji->name.is_empty())
  162. button->set_tooltip(emoji->name);
  163. emojis.empend(move(button), emoji.release_value());
  164. }
  165. quick_sort(emojis, [](auto const& lhs, auto const& rhs) {
  166. return lhs.emoji.display_order < rhs.emoji.display_order;
  167. });
  168. return emojis;
  169. }
  170. void EmojiInputDialog::update_displayed_emoji()
  171. {
  172. ScopeGuard guard { [&] { m_emojis_widget->set_updates_enabled(true); } };
  173. m_emojis_widget->set_updates_enabled(false);
  174. m_emojis_widget->remove_all_children();
  175. constexpr size_t columns = 18;
  176. size_t rows = ceil_div(m_emojis.size(), columns);
  177. size_t index = 0;
  178. for (size_t row = 0; row < rows && index < m_emojis.size(); ++row) {
  179. auto& horizontal_container = m_emojis_widget->add<Widget>();
  180. horizontal_container.set_preferred_height(SpecialDimension::Fit);
  181. auto& horizontal_layout = horizontal_container.set_layout<HorizontalBoxLayout>();
  182. horizontal_layout.set_spacing(0);
  183. for (size_t column = 0; column < columns; ++column) {
  184. bool found_match = false;
  185. while (!found_match && (index < m_emojis.size())) {
  186. auto& emoji = m_emojis[index++];
  187. if (m_selected_category.has_value() && emoji.emoji.group != m_selected_category)
  188. continue;
  189. if (!emoji.emoji.name.is_empty())
  190. found_match = emoji.emoji.name.contains(m_search_box->text(), CaseSensitivity::CaseInsensitive);
  191. else
  192. found_match = m_search_box->text().is_empty();
  193. if (found_match)
  194. horizontal_container.add_child(*emoji.button);
  195. }
  196. }
  197. }
  198. }
  199. void EmojiInputDialog::event(Core::Event& event)
  200. {
  201. if (event.type() == Event::KeyDown) {
  202. auto& key_event = static_cast<KeyEvent&>(event);
  203. if (key_event.key() == Key_Escape) {
  204. done(ExecResult::Cancel);
  205. return;
  206. }
  207. }
  208. Dialog::event(event);
  209. }
  210. }