EmojiInputDialog.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/LexicalPath.h>
  7. #include <AK/StringBuilder.h>
  8. #include <AK/Utf32View.h>
  9. #include <LibCore/DirIterator.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Button.h>
  12. #include <LibGUI/EmojiInputDialog.h>
  13. #include <LibGUI/Event.h>
  14. #include <LibGUI/Frame.h>
  15. #include <stdlib.h>
  16. namespace GUI {
  17. static Vector<u32> supported_emoji_code_points()
  18. {
  19. Vector<u32> code_points;
  20. Core::DirIterator dt("/res/emoji", Core::DirIterator::SkipDots);
  21. while (dt.has_next()) {
  22. auto filename = dt.next_path();
  23. auto lexical_path = LexicalPath(filename);
  24. if (lexical_path.extension() != "png")
  25. continue;
  26. auto basename = lexical_path.basename();
  27. if (!basename.starts_with("U+"))
  28. continue;
  29. u32 code_point = strtoul(basename.to_string().characters() + 2, nullptr, 16);
  30. code_points.append(code_point);
  31. }
  32. return code_points;
  33. }
  34. EmojiInputDialog::EmojiInputDialog(Window* parent_window)
  35. : Dialog(parent_window)
  36. {
  37. // FIXME: Dialog should automatically adjust to content
  38. set_minimum_size(190, 190);
  39. set_frameless(true);
  40. auto& main_widget = set_main_widget<Frame>();
  41. main_widget.set_frame_shape(Gfx::FrameShape::Container);
  42. main_widget.set_frame_shadow(Gfx::FrameShadow::Raised);
  43. main_widget.set_fill_with_background_color(true);
  44. auto& main_layout = main_widget.set_layout<VerticalBoxLayout>();
  45. main_layout.set_margins(1);
  46. main_layout.set_spacing(0);
  47. auto code_points = supported_emoji_code_points();
  48. size_t index = 0;
  49. size_t columns = 10;
  50. size_t rows = ceil_div(code_points.size(), columns);
  51. for (size_t row = 0; row < rows && index < code_points.size(); ++row) {
  52. auto& horizontal_container = main_widget.add<Widget>();
  53. auto& horizontal_layout = horizontal_container.set_layout<HorizontalBoxLayout>();
  54. horizontal_layout.set_spacing(0);
  55. for (size_t column = 0; column < columns; ++column) {
  56. if (index < code_points.size()) {
  57. StringBuilder builder;
  58. builder.append(Utf32View(&code_points[index++], 1));
  59. auto emoji_text = builder.to_string();
  60. auto& button = horizontal_container.add<Button>(emoji_text);
  61. button.set_fixed_size(18, 18);
  62. button.set_button_style(Gfx::ButtonStyle::Coolbar);
  63. button.on_click = [this, button = &button](auto) {
  64. m_selected_emoji_text = button->text();
  65. done(ExecOK);
  66. };
  67. } else {
  68. horizontal_container.add<Widget>();
  69. }
  70. }
  71. }
  72. }
  73. void EmojiInputDialog::event(Core::Event& event)
  74. {
  75. if (event.type() == Event::KeyDown) {
  76. auto& key_event = static_cast<KeyEvent&>(event);
  77. if (key_event.key() == Key_Escape) {
  78. done(ExecCancel);
  79. return;
  80. }
  81. }
  82. Dialog::event(event);
  83. }
  84. }