EmojiInputDialog.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/ScopeGuard.h>
  9. #include <AK/StringBuilder.h>
  10. #include <AK/Utf32View.h>
  11. #include <LibCore/DirIterator.h>
  12. #include <LibGUI/BoxLayout.h>
  13. #include <LibGUI/Button.h>
  14. #include <LibGUI/EmojiInputDialog.h>
  15. #include <LibGUI/EmojiInputDialogGML.h>
  16. #include <LibGUI/Event.h>
  17. #include <LibGUI/Frame.h>
  18. #include <LibGUI/ScrollableContainerWidget.h>
  19. #include <LibGUI/TextBox.h>
  20. #include <LibUnicode/CharacterTypes.h>
  21. #include <stdlib.h>
  22. namespace GUI {
  23. EmojiInputDialog::EmojiInputDialog(Window* parent_window)
  24. : Dialog(parent_window)
  25. {
  26. auto& main_widget = set_main_widget<Frame>();
  27. if (!main_widget.load_from_gml(emoji_input_dialog_gml))
  28. VERIFY_NOT_REACHED();
  29. set_frameless(true);
  30. resize(400, 300);
  31. auto& scrollable_container = *main_widget.find_descendant_of_type_named<GUI::ScrollableContainerWidget>("scrollable_container"sv);
  32. m_search_box = main_widget.find_descendant_of_type_named<GUI::TextBox>("search_box"sv);
  33. m_emojis_widget = main_widget.find_descendant_of_type_named<GUI::Widget>("emojis"sv);
  34. m_emojis = supported_emoji();
  35. scrollable_container.horizontal_scrollbar().set_visible(false);
  36. update_displayed_emoji();
  37. on_active_window_change = [this](bool is_active_window) {
  38. if (!is_active_window)
  39. close();
  40. };
  41. m_search_box->on_change = [this]() {
  42. update_displayed_emoji();
  43. };
  44. }
  45. auto EmojiInputDialog::supported_emoji() -> Vector<Emoji>
  46. {
  47. constexpr int button_size = 20;
  48. Vector<Emoji> code_points;
  49. Core::DirIterator dt("/res/emoji", Core::DirIterator::SkipDots);
  50. while (dt.has_next()) {
  51. auto filename = dt.next_path();
  52. auto lexical_path = LexicalPath(filename);
  53. if (lexical_path.extension() != "png")
  54. continue;
  55. auto basename = lexical_path.basename();
  56. if (!basename.starts_with("U+"sv))
  57. continue;
  58. // FIXME: Handle multi code point emojis.
  59. if (basename.contains('_'))
  60. continue;
  61. u32 code_point = strtoul(basename.to_string().characters() + 2, nullptr, 16);
  62. auto name = Unicode::code_point_display_name(code_point);
  63. // FIXME: Also emit U+FE0F for single code point emojis, currently
  64. // they get shown as text glyphs if available.
  65. // This will require buttons to don't calculate their length as 2,
  66. // currently it just shows an ellipsis. It will also require some
  67. // tweaking of the mechanism that is currently being used to insert
  68. // which is a key event with a single code point.
  69. StringBuilder builder;
  70. builder.append(Utf32View(&code_point, 1));
  71. auto emoji_text = builder.to_string();
  72. auto button = Button::construct(move(emoji_text));
  73. button->set_fixed_size(button_size, button_size);
  74. button->set_button_style(Gfx::ButtonStyle::Coolbar);
  75. button->on_click = [this, button = button](auto) {
  76. m_selected_emoji_text = button->text();
  77. done(ExecResult::OK);
  78. };
  79. code_points.empend(code_point, move(name), move(button));
  80. }
  81. return code_points;
  82. }
  83. void EmojiInputDialog::update_displayed_emoji()
  84. {
  85. ScopeGuard guard { [&] { m_emojis_widget->set_updates_enabled(true); } };
  86. m_emojis_widget->set_updates_enabled(false);
  87. m_emojis_widget->remove_all_children();
  88. constexpr size_t columns = 18;
  89. size_t rows = ceil_div(m_emojis.size(), columns);
  90. size_t index = 0;
  91. for (size_t row = 0; row < rows && index < m_emojis.size(); ++row) {
  92. auto& horizontal_container = m_emojis_widget->add<Widget>();
  93. horizontal_container.set_preferred_height(SpecialDimension::Fit);
  94. auto& horizontal_layout = horizontal_container.set_layout<HorizontalBoxLayout>();
  95. horizontal_layout.set_spacing(0);
  96. for (size_t column = 0; column < columns; ++column) {
  97. bool found_match = false;
  98. while (!found_match && (index < m_emojis.size())) {
  99. auto& emoji = m_emojis[index++];
  100. if (emoji.name.has_value())
  101. found_match = emoji.name->contains(m_search_box->text(), CaseSensitivity::CaseInsensitive);
  102. else
  103. found_match = m_search_box->text().is_empty();
  104. if (found_match)
  105. horizontal_container.add_child(*emoji.button);
  106. }
  107. }
  108. }
  109. }
  110. void EmojiInputDialog::event(Core::Event& event)
  111. {
  112. if (event.type() == Event::KeyDown) {
  113. auto& key_event = static_cast<KeyEvent&>(event);
  114. if (key_event.key() == Key_Escape) {
  115. done(ExecResult::Cancel);
  116. return;
  117. }
  118. }
  119. Dialog::event(event);
  120. }
  121. }