2020-05-17 21:49:54 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2022-02-22 23:30:54 +00:00
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
2020-05-17 21:49:54 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-17 21:49:54 +02:00
|
|
|
*/
|
|
|
|
|
|
2020-05-26 14:52:44 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
2020-05-17 21:49:54 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
|
#include <AK/Utf32View.h>
|
|
|
|
|
#include <LibCore/DirIterator.h>
|
|
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
|
#include <LibGUI/Button.h>
|
|
|
|
|
#include <LibGUI/EmojiInputDialog.h>
|
2022-09-01 08:48:07 -04:00
|
|
|
#include <LibGUI/EmojiInputDialogGML.h>
|
2020-05-17 21:49:54 +02:00
|
|
|
#include <LibGUI/Event.h>
|
|
|
|
|
#include <LibGUI/Frame.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
namespace GUI {
|
|
|
|
|
|
2020-08-05 16:31:20 -04:00
|
|
|
static Vector<u32> supported_emoji_code_points()
|
2020-05-17 21:49:54 +02:00
|
|
|
{
|
2020-08-05 16:31:20 -04:00
|
|
|
Vector<u32> code_points;
|
2020-05-17 21:49:54 +02:00
|
|
|
Core::DirIterator dt("/res/emoji", Core::DirIterator::SkipDots);
|
|
|
|
|
while (dt.has_next()) {
|
|
|
|
|
auto filename = dt.next_path();
|
2020-05-26 14:52:44 +03:00
|
|
|
auto lexical_path = LexicalPath(filename);
|
|
|
|
|
if (lexical_path.extension() != "png")
|
2020-05-17 21:49:54 +02:00
|
|
|
continue;
|
2021-11-11 00:55:02 +01:00
|
|
|
auto basename = lexical_path.basename();
|
2022-07-11 17:32:29 +00:00
|
|
|
if (!basename.starts_with("U+"sv))
|
2020-05-17 21:49:54 +02:00
|
|
|
continue;
|
2022-02-22 23:34:17 +00:00
|
|
|
// FIXME: Handle multi code point emojis.
|
|
|
|
|
if (basename.contains('_'))
|
|
|
|
|
continue;
|
AK+Everywhere: Use mostly StringView in LexicalPath
This changes the m_parts, m_dirname, m_basename, m_title and m_extension
member variables to StringViews onto the m_string String. It also
removes the m_is_absolute member in favour of computing if a path is
absolute in the is_absolute() getter. Due to this, the canonicalize()
method has been completely rewritten.
The parts() getter still returns a Vector<String>, although it is no
longer a const reference as m_parts is no longer a Vector<String>.
Rather, it is constructed from the StringViews in m_parts upon request.
The parts_view() getter has been added, which returns Vector<StringView>
const&. Most previous users of parts() have been changed to use
parts_view(), except where Strings are required.
Due to this change, it's is now no longer allow to create temporary
LexicalPath objects to call the dirname, basename, title, or extension
getters on them because the returned StringViews will point to possible
freed memory.
2021-06-29 17:06:21 +02:00
|
|
|
u32 code_point = strtoul(basename.to_string().characters() + 2, nullptr, 16);
|
2020-08-05 16:31:20 -04:00
|
|
|
code_points.append(code_point);
|
2020-05-17 21:49:54 +02:00
|
|
|
}
|
2020-08-05 16:31:20 -04:00
|
|
|
return code_points;
|
2020-05-17 21:49:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EmojiInputDialog::EmojiInputDialog(Window* parent_window)
|
|
|
|
|
: Dialog(parent_window)
|
|
|
|
|
{
|
|
|
|
|
auto& main_widget = set_main_widget<Frame>();
|
2022-09-01 08:48:07 -04:00
|
|
|
if (!main_widget.load_from_gml(emoji_input_dialog_gml))
|
|
|
|
|
VERIFY_NOT_REACHED();
|
2020-05-17 21:49:54 +02:00
|
|
|
|
2022-09-01 08:48:07 -04:00
|
|
|
auto& emojis_widget = *main_widget.find_descendant_of_type_named<GUI::Widget>("emojis"sv);
|
2020-08-05 16:31:20 -04:00
|
|
|
auto code_points = supported_emoji_code_points();
|
2020-05-17 21:49:54 +02:00
|
|
|
|
|
|
|
|
size_t index = 0;
|
2022-09-01 08:48:07 -04:00
|
|
|
size_t columns = 18;
|
2020-08-05 16:31:20 -04:00
|
|
|
size_t rows = ceil_div(code_points.size(), columns);
|
2020-05-17 21:49:54 +02:00
|
|
|
|
2022-09-01 08:48:07 -04:00
|
|
|
constexpr int button_size = 20;
|
2022-02-22 23:30:54 +00:00
|
|
|
// 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.
|
|
|
|
|
constexpr int magic_offset = 7;
|
|
|
|
|
int dialog_width = button_size * columns + magic_offset;
|
|
|
|
|
int dialog_height = button_size * rows;
|
|
|
|
|
|
2022-08-17 19:38:46 -04:00
|
|
|
resize(dialog_width, dialog_height);
|
2022-02-22 23:30:54 +00:00
|
|
|
set_frameless(true);
|
|
|
|
|
|
2020-08-05 16:31:20 -04:00
|
|
|
for (size_t row = 0; row < rows && index < code_points.size(); ++row) {
|
2022-09-01 08:48:07 -04:00
|
|
|
auto& horizontal_container = emojis_widget.add<Widget>();
|
2020-05-17 21:49:54 +02:00
|
|
|
auto& horizontal_layout = horizontal_container.set_layout<HorizontalBoxLayout>();
|
|
|
|
|
horizontal_layout.set_spacing(0);
|
|
|
|
|
for (size_t column = 0; column < columns; ++column) {
|
2020-08-05 16:31:20 -04:00
|
|
|
if (index < code_points.size()) {
|
2022-02-22 23:34:17 +00:00
|
|
|
// FIXME: Also emit U+FE0F for single code point emojis, currently
|
|
|
|
|
// they get shown as text glyphs if available.
|
|
|
|
|
// This will require buttons to don't calculate their length as 2,
|
|
|
|
|
// currently it just shows an ellipsis. It will also require some
|
|
|
|
|
// tweaking of the mechanism that is currently being used to insert
|
|
|
|
|
// which is a key event with a single code point.
|
2020-05-18 09:55:19 +02:00
|
|
|
StringBuilder builder;
|
2020-08-05 16:31:20 -04:00
|
|
|
builder.append(Utf32View(&code_points[index++], 1));
|
2020-05-18 09:55:19 +02:00
|
|
|
auto emoji_text = builder.to_string();
|
|
|
|
|
auto& button = horizontal_container.add<Button>(emoji_text);
|
2022-02-22 23:30:54 +00:00
|
|
|
button.set_fixed_size(button_size, button_size);
|
2021-04-13 16:18:20 +02:00
|
|
|
button.set_button_style(Gfx::ButtonStyle::Coolbar);
|
2020-05-18 09:55:19 +02:00
|
|
|
button.on_click = [this, button = &button](auto) {
|
|
|
|
|
m_selected_emoji_text = button->text();
|
2022-05-13 13:10:27 +01:00
|
|
|
done(ExecResult::OK);
|
2020-05-18 09:55:19 +02:00
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
horizontal_container.add<Widget>();
|
2020-05-17 21:49:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-07 18:16:21 +02:00
|
|
|
|
|
|
|
|
on_active_window_change = [this](bool is_active_window) {
|
|
|
|
|
if (!is_active_window)
|
|
|
|
|
close();
|
|
|
|
|
};
|
2020-05-17 21:49:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EmojiInputDialog::event(Core::Event& event)
|
|
|
|
|
{
|
|
|
|
|
if (event.type() == Event::KeyDown) {
|
|
|
|
|
auto& key_event = static_cast<KeyEvent&>(event);
|
|
|
|
|
if (key_event.key() == Key_Escape) {
|
2022-05-13 13:10:27 +01:00
|
|
|
done(ExecResult::Cancel);
|
2020-05-17 21:49:54 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Dialog::event(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|