From 70d286774f84d84361af805c26eb9d13c5febc27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 25 May 2022 21:26:42 +0200 Subject: [PATCH] Platform: use StringView in *Application::Text*Event. Which allows to get rid of a now-unneeded ArrayView include in the header. Also, now that we're returning a StringView, it's useful to have the view always null-terminated. In SDL2 and Emscripten it was already like that, GLFW needed a minor change. --- src/Magnum/Platform/EmscriptenApplication.cpp | 8 +++--- src/Magnum/Platform/EmscriptenApplication.h | 14 ++++++---- src/Magnum/Platform/GlfwApplication.cpp | 7 ++--- src/Magnum/Platform/GlfwApplication.h | 14 ++++++---- src/Magnum/Platform/Sdl2Application.cpp | 5 ++-- src/Magnum/Platform/Sdl2Application.h | 27 ++++++++++++------- .../Test/EmscriptenApplicationTest.cpp | 3 +-- .../Platform/Test/GlfwApplicationTest.cpp | 3 +-- .../Platform/Test/Sdl2ApplicationTest.cpp | 3 +-- 9 files changed, 49 insertions(+), 35 deletions(-) diff --git a/src/Magnum/Platform/EmscriptenApplication.cpp b/src/Magnum/Platform/EmscriptenApplication.cpp index dce134886..48b0587c6 100644 --- a/src/Magnum/Platform/EmscriptenApplication.cpp +++ b/src/Magnum/Platform/EmscriptenApplication.cpp @@ -631,11 +631,11 @@ void EmscriptenApplication::setupCallbacks(bool resizable) { emscripten_set_keydown_callback(keyboardListeningElement, this, false, ([](int, const EmscriptenKeyboardEvent* event, void* userData) -> Int { EmscriptenApplication& app = *static_cast(userData); - const std::size_t keyLen = std::strlen(event->key); + const Containers::StringView key = event->key; /* If the key name is a single letter or a start of an UTF-8 - sequence, pass it to the text input even tas well */ - if(app.isTextInputActive() && (std::strlen(event->key) == 1 || (std::strlen(event->key) >= 1 && UnsignedByte(event->key[0]) > 127))) { - TextInputEvent e{*event, {event->key, keyLen}}; + sequence, pass it to the text input event as well */ + if(app.isTextInputActive() && key.size() == 1 || (key.size() >= 1 && UnsignedByte(key[0]) > 127)) { + TextInputEvent e{*event, key}; app.textInputEvent(e); return e.isAccepted(); } diff --git a/src/Magnum/Platform/EmscriptenApplication.h b/src/Magnum/Platform/EmscriptenApplication.h index 18d2488af..143a4b0b3 100644 --- a/src/Magnum/Platform/EmscriptenApplication.h +++ b/src/Magnum/Platform/EmscriptenApplication.h @@ -34,7 +34,6 @@ */ #endif -#include /* Needed by the MAGNUM_EMSCRIPTENAPPLICATION_MAIN() macro */ /** @todo use an Optional */ #include @@ -1897,8 +1896,13 @@ class EmscriptenApplication::TextInputEvent { /** @copydoc EmscriptenApplication::InputEvent::setAccepted() */ void setAccepted(bool accepted = true) { _accepted = accepted; } - /** @brief Input text in UTF-8 */ - Containers::ArrayView text() const { return _text; } + /** + * @brief Input text + * + * The returned view is in UTF-8 and is always + * @relativeref{Corrade,Containers::StringViewFlag::NullTerminated}. + */ + Containers::StringView text() const { return _text; } /** @brief Underlying Emscripten event */ const EmscriptenKeyboardEvent& event() const { return _event; } @@ -1906,10 +1910,10 @@ class EmscriptenApplication::TextInputEvent { private: friend EmscriptenApplication; - explicit TextInputEvent(const EmscriptenKeyboardEvent& event, Containers::ArrayView text): _event(event), _text{text}, _accepted{false} {} + explicit TextInputEvent(const EmscriptenKeyboardEvent& event, Containers::StringView text): _event(event), _text{text}, _accepted{false} {} const EmscriptenKeyboardEvent& _event; - const Containers::ArrayView _text; + const Containers::StringView _text; bool _accepted; }; diff --git a/src/Magnum/Platform/GlfwApplication.cpp b/src/Magnum/Platform/GlfwApplication.cpp index 43df16bb8..1852cf8fc 100644 --- a/src/Magnum/Platform/GlfwApplication.cpp +++ b/src/Magnum/Platform/GlfwApplication.cpp @@ -654,9 +654,10 @@ void GlfwApplication::setupCallbacks() { if(!(app._flags & Flag::TextInputActive)) return; - char utf8[4]{}; - const std::size_t size = Utility::Unicode::utf8(codepoint, utf8); - TextInputEvent e{{utf8, size}}; + /* One extra byte to ensure it gets always null-terminated */ + char utf8[4 + 1]{}; + const std::size_t size = Utility::Unicode::utf8(codepoint, Containers::staticArrayView(utf8).prefix<4>()); + TextInputEvent e{{utf8, size, Containers::StringViewFlag::NullTerminated}}; app.textInputEvent(e); }); } diff --git a/src/Magnum/Platform/GlfwApplication.h b/src/Magnum/Platform/GlfwApplication.h index 92d9e53a0..edc31b9a5 100644 --- a/src/Magnum/Platform/GlfwApplication.h +++ b/src/Magnum/Platform/GlfwApplication.h @@ -32,7 +32,6 @@ * @brief Class @ref Magnum::Platform::GlfwApplication, macro @ref MAGNUM_GLFWAPPLICATION_MAIN() */ -#include #include #include /** @todo PIMPL Configuration instead? */ @@ -2050,15 +2049,20 @@ class GlfwApplication::TextInputEvent { */ void setAccepted(bool accepted = true) { _accepted = accepted; } - /** @brief Input text in UTF-8 */ - Containers::ArrayView text() const { return _text; } + /** + * @brief Input text + * + * The returned view is in UTF-8 and is always + * @relativeref{Corrade,Containers::StringViewFlag::NullTerminated}. + */ + Containers::StringView text() const { return _text; } private: friend GlfwApplication; - explicit TextInputEvent(Containers::ArrayView text): _text{text}, _accepted{false} {} + explicit TextInputEvent(Containers::StringView text): _text{text}, _accepted{false} {} - const Containers::ArrayView _text; + const Containers::StringView _text; bool _accepted; }; diff --git a/src/Magnum/Platform/Sdl2Application.cpp b/src/Magnum/Platform/Sdl2Application.cpp index 744e338bc..73053b600 100644 --- a/src/Magnum/Platform/Sdl2Application.cpp +++ b/src/Magnum/Platform/Sdl2Application.cpp @@ -27,7 +27,6 @@ #include "Sdl2Application.h" -#include /** @todo remove, needed for std::strlen() in TextInputEvent */ #ifdef CORRADE_TARGET_CLANG_CL /* SDL does #pragma pack(push,8) and #pragma pack(pop,8) in different headers (begin_code.h and end_code.h) and clang-cl doesn't like that, even though it @@ -963,12 +962,12 @@ bool Sdl2Application::mainLoopIteration() { } case SDL_TEXTINPUT: { - TextInputEvent e{event, {event.text.text, std::strlen(event.text.text)}}; + TextInputEvent e{event, event.text.text}; textInputEvent(e); } break; case SDL_TEXTEDITING: { - TextEditingEvent e{event, {event.edit.text, std::strlen(event.text.text)}, event.edit.start, event.edit.length}; + TextEditingEvent e{event, event.edit.text, event.edit.start, event.edit.length}; textEditingEvent(e); } break; diff --git a/src/Magnum/Platform/Sdl2Application.h b/src/Magnum/Platform/Sdl2Application.h index 2f528644b..6ae6c1880 100644 --- a/src/Magnum/Platform/Sdl2Application.h +++ b/src/Magnum/Platform/Sdl2Application.h @@ -31,7 +31,6 @@ * @brief Class @ref Magnum::Platform::Sdl2Application, macro @ref MAGNUM_SDL2APPLICATION_MAIN() */ -#include #include #include #include /** @todo PIMPL Configuration instead? */ @@ -2798,8 +2797,13 @@ class Sdl2Application::TextInputEvent { */ void setAccepted(bool accepted = true) { _accepted = accepted; } - /** @brief Input text in UTF-8 */ - Containers::ArrayView text() const { return _text; } + /** + * @brief Input text + * + * The returned view is in UTF-8 and is always + * @relativeref{Corrade,Containers::StringViewFlag::NullTerminated}. + */ + Containers::StringView text() const { return _text; } /** * @brief Underlying SDL event @@ -2812,10 +2816,10 @@ class Sdl2Application::TextInputEvent { private: friend Sdl2Application; - explicit TextInputEvent(const SDL_Event& event, Containers::ArrayView text): _event(event), _text{text}, _accepted{false} {} + explicit TextInputEvent(const SDL_Event& event, Containers::StringView text): _event(event), _text{text}, _accepted{false} {} const SDL_Event& _event; - const Containers::ArrayView _text; + const Containers::StringView _text; bool _accepted; }; @@ -2851,8 +2855,13 @@ class Sdl2Application::TextEditingEvent { */ void setAccepted(bool accepted = true) { _accepted = accepted; } - /** @brief Input text in UTF-8 */ - Containers::ArrayView text() const { return _text; } + /** + * @brief Input text + * + * The returned view is in UTF-8 and is always + * @relativeref{Corrade,Containers::StringViewFlag::NullTerminated}. + */ + Containers::StringView text() const { return _text; } /** @brief Location to begin editing from */ Int start() const { return _start; } @@ -2871,10 +2880,10 @@ class Sdl2Application::TextEditingEvent { private: friend Sdl2Application; - explicit TextEditingEvent(const SDL_Event& event, Containers::ArrayView text, Int start, Int length): _event(event), _text{text}, _start{start}, _length{length}, _accepted{false} {} + explicit TextEditingEvent(const SDL_Event& event, Containers::StringView text, Int start, Int length): _event(event), _text{text}, _start{start}, _length{length}, _accepted{false} {} const SDL_Event& _event; - const Containers::ArrayView _text; + const Containers::StringView _text; const Int _start; const Int _length; bool _accepted; diff --git a/src/Magnum/Platform/Test/EmscriptenApplicationTest.cpp b/src/Magnum/Platform/Test/EmscriptenApplicationTest.cpp index dcf466ee9..9cb1cce83 100644 --- a/src/Magnum/Platform/Test/EmscriptenApplicationTest.cpp +++ b/src/Magnum/Platform/Test/EmscriptenApplicationTest.cpp @@ -26,7 +26,6 @@ */ #include -#include #include "Magnum/Platform/EmscriptenApplication.h" #include "Magnum/GL/Renderer.h" @@ -140,7 +139,7 @@ struct EmscriptenApplicationTest: Platform::Application { } void textInputEvent(TextInputEvent& event) override { - Debug{} << "text input event:" << std::string{event.text(), event.text().size()}; + Debug{} << "text input event:" << event.text(); event.setAccepted(); } diff --git a/src/Magnum/Platform/Test/GlfwApplicationTest.cpp b/src/Magnum/Platform/Test/GlfwApplicationTest.cpp index 6fe6dfb5d..d4c3981d4 100644 --- a/src/Magnum/Platform/Test/GlfwApplicationTest.cpp +++ b/src/Magnum/Platform/Test/GlfwApplicationTest.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include "Magnum/ImageView.h" @@ -102,7 +101,7 @@ struct GlfwApplicationTest: Platform::Application { } void textInputEvent(TextInputEvent& event) override { - Debug{} << "text input event:" << std::string{event.text(), event.text().size()}; + Debug{} << "text input event:" << event.text(); } }; diff --git a/src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp b/src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp index 20513c946..2adeaf08b 100644 --- a/src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp +++ b/src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include "Magnum/ImageView.h" @@ -142,7 +141,7 @@ struct Sdl2ApplicationTest: Platform::Application { } void textInputEvent(TextInputEvent& event) override { - Debug{} << "text input event:" << std::string{event.text(), event.text().size()}; + Debug{} << "text input event:" << event.text(); } /* Should fire on currently not handled events, such as minimize/maximize