EmojiInputDialog.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. set_frameless(true);
  38. auto& main_widget = set_main_widget<Frame>();
  39. main_widget.set_frame_shape(Gfx::FrameShape::Container);
  40. main_widget.set_frame_shadow(Gfx::FrameShadow::Raised);
  41. main_widget.set_fill_with_background_color(true);
  42. auto& main_layout = main_widget.set_layout<VerticalBoxLayout>();
  43. main_layout.set_margins(1);
  44. main_layout.set_spacing(0);
  45. auto code_points = supported_emoji_code_points();
  46. size_t index = 0;
  47. size_t columns = 6;
  48. size_t rows = ceil_div(code_points.size(), columns);
  49. for (size_t row = 0; row < rows && index < code_points.size(); ++row) {
  50. auto& horizontal_container = main_widget.add<Widget>();
  51. auto& horizontal_layout = horizontal_container.set_layout<HorizontalBoxLayout>();
  52. horizontal_layout.set_spacing(0);
  53. for (size_t column = 0; column < columns; ++column) {
  54. if (index < code_points.size()) {
  55. StringBuilder builder;
  56. builder.append(Utf32View(&code_points[index++], 1));
  57. auto emoji_text = builder.to_string();
  58. auto& button = horizontal_container.add<Button>(emoji_text);
  59. button.set_min_size(16, 16);
  60. button.set_button_style(Gfx::ButtonStyle::Coolbar);
  61. button.on_click = [this, button = &button](auto) {
  62. m_selected_emoji_text = button->text();
  63. done(ExecOK);
  64. };
  65. } else {
  66. horizontal_container.add<Widget>();
  67. }
  68. }
  69. }
  70. }
  71. void EmojiInputDialog::event(Core::Event& event)
  72. {
  73. if (event.type() == Event::KeyDown) {
  74. auto& key_event = static_cast<KeyEvent&>(event);
  75. if (key_event.key() == Key_Escape) {
  76. done(ExecCancel);
  77. return;
  78. }
  79. }
  80. Dialog::event(event);
  81. }
  82. }