EmojiInputDialog.cpp 3.9 KB

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