EmojiInputDialog.cpp 4.1 KB

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