EmojiInputDialog.cpp 4.0 KB

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