EmojiInputDialog.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <stdlib.h>
  18. namespace GUI {
  19. static Vector<u32> supported_emoji_code_points()
  20. {
  21. Vector<u32> code_points;
  22. Core::DirIterator dt("/res/emoji", Core::DirIterator::SkipDots);
  23. while (dt.has_next()) {
  24. auto filename = dt.next_path();
  25. auto lexical_path = LexicalPath(filename);
  26. if (lexical_path.extension() != "png")
  27. continue;
  28. auto basename = lexical_path.basename();
  29. if (!basename.starts_with("U+"sv))
  30. continue;
  31. // FIXME: Handle multi code point emojis.
  32. if (basename.contains('_'))
  33. continue;
  34. u32 code_point = strtoul(basename.to_string().characters() + 2, nullptr, 16);
  35. code_points.append(code_point);
  36. }
  37. return code_points;
  38. }
  39. EmojiInputDialog::EmojiInputDialog(Window* parent_window)
  40. : Dialog(parent_window)
  41. {
  42. auto& main_widget = set_main_widget<Frame>();
  43. if (!main_widget.load_from_gml(emoji_input_dialog_gml))
  44. VERIFY_NOT_REACHED();
  45. auto& emojis_widget = *main_widget.find_descendant_of_type_named<GUI::Widget>("emojis"sv);
  46. auto code_points = supported_emoji_code_points();
  47. size_t index = 0;
  48. size_t columns = 18;
  49. size_t rows = ceil_div(code_points.size(), columns);
  50. constexpr int button_size = 20;
  51. // FIXME: I have no idea why this is needed, you'd think that button width * number of buttons would make them fit, but the last one gets cut off.
  52. constexpr int magic_offset = 7;
  53. int dialog_width = button_size * columns + magic_offset;
  54. int dialog_height = button_size * rows;
  55. resize(dialog_width, dialog_height);
  56. set_frameless(true);
  57. for (size_t row = 0; row < rows && index < code_points.size(); ++row) {
  58. auto& horizontal_container = emojis_widget.add<Widget>();
  59. auto& horizontal_layout = horizontal_container.set_layout<HorizontalBoxLayout>();
  60. horizontal_layout.set_spacing(0);
  61. for (size_t column = 0; column < columns; ++column) {
  62. if (index < code_points.size()) {
  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_points[index++], 1));
  71. auto emoji_text = builder.to_string();
  72. auto& button = horizontal_container.add<Button>(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. } else {
  80. horizontal_container.add<Widget>();
  81. }
  82. }
  83. }
  84. on_active_window_change = [this](bool is_active_window) {
  85. if (!is_active_window)
  86. close();
  87. };
  88. }
  89. void EmojiInputDialog::event(Core::Event& event)
  90. {
  91. if (event.type() == Event::KeyDown) {
  92. auto& key_event = static_cast<KeyEvent&>(event);
  93. if (key_event.key() == Key_Escape) {
  94. done(ExecResult::Cancel);
  95. return;
  96. }
  97. }
  98. Dialog::event(event);
  99. }
  100. }