EmojiInputDialog.cpp 3.9 KB

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