EmojiInputDialog.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/LexicalPath.h>
  27. #include <AK/StringBuilder.h>
  28. #include <AK/Utf32View.h>
  29. #include <LibCore/DirIterator.h>
  30. #include <LibGUI/BoxLayout.h>
  31. #include <LibGUI/Button.h>
  32. #include <LibGUI/EmojiInputDialog.h>
  33. #include <LibGUI/Event.h>
  34. #include <LibGUI/Frame.h>
  35. #include <stdlib.h>
  36. namespace GUI {
  37. static Vector<u32> supported_emoji_code_points()
  38. {
  39. Vector<u32> code_points;
  40. Core::DirIterator dt("/res/emoji", Core::DirIterator::SkipDots);
  41. while (dt.has_next()) {
  42. auto filename = dt.next_path();
  43. auto lexical_path = LexicalPath(filename);
  44. if (lexical_path.extension() != "png")
  45. continue;
  46. auto basename = lexical_path.basename();
  47. if (!basename.starts_with("U+"))
  48. continue;
  49. u32 code_point = strtoul(basename.characters() + 2, nullptr, 16);
  50. code_points.append(code_point);
  51. }
  52. return code_points;
  53. }
  54. EmojiInputDialog::EmojiInputDialog(Window* parent_window)
  55. : Dialog(parent_window)
  56. {
  57. set_frameless(true);
  58. auto& main_widget = set_main_widget<Frame>();
  59. main_widget.set_frame_shape(Gfx::FrameShape::Container);
  60. main_widget.set_frame_shadow(Gfx::FrameShadow::Raised);
  61. main_widget.set_fill_with_background_color(true);
  62. auto& main_layout = main_widget.set_layout<VerticalBoxLayout>();
  63. main_layout.set_spacing(0);
  64. auto code_points = supported_emoji_code_points();
  65. size_t index = 0;
  66. size_t columns = 6;
  67. size_t rows = ceil_div(code_points.size(), columns);
  68. for (size_t row = 0; row < rows && index < code_points.size(); ++row) {
  69. auto& horizontal_container = main_widget.add<Widget>();
  70. auto& horizontal_layout = horizontal_container.set_layout<HorizontalBoxLayout>();
  71. horizontal_layout.set_spacing(0);
  72. for (size_t column = 0; column < columns; ++column) {
  73. if (index < code_points.size()) {
  74. StringBuilder builder;
  75. builder.append(Utf32View(&code_points[index++], 1));
  76. auto emoji_text = builder.to_string();
  77. auto& button = horizontal_container.add<Button>(emoji_text);
  78. button.set_button_style(Gfx::ButtonStyle::CoolBar);
  79. button.on_click = [this, button = &button](auto) {
  80. m_selected_emoji_text = button->text();
  81. done(ExecOK);
  82. };
  83. } else {
  84. horizontal_container.add<Widget>();
  85. }
  86. }
  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(ExecCancel);
  95. return;
  96. }
  97. }
  98. Dialog::event(event);
  99. }
  100. }