diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ca8f522..a4a3863 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,6 +19,8 @@ add_executable(tomato texture_uploader.hpp ./sdlrenderer_texture_uploader.hpp ./sdlrenderer_texture_uploader.cpp + ./sdl_clipboard_utils.hpp + ./sdl_clipboard_utils.cpp ./file_selector.hpp ./file_selector.cpp diff --git a/src/chat_gui4.cpp b/src/chat_gui4.cpp index b9f2f31..de16df1 100644 --- a/src/chat_gui4.cpp +++ b/src/chat_gui4.cpp @@ -11,6 +11,11 @@ #include #include +#include + +#include "./sdl_clipboard_utils.hpp" +#include "SDL_clipboard.h" + #include #include #include @@ -256,6 +261,14 @@ void ChatGui4::render(void) { } } ImGui::EndChild(); + + if (ImGui::IsKeyPressed(ImGuiKey_V) && ImGui::IsKeyPressed(ImGuiMod_Shortcut)) { + if (const auto* mime_type = clipboardHasImage(); mime_type != nullptr) { + size_t data_size = 0; + const auto* data = SDL_GetClipboardData(mime_type, &data_size); + // open file send preview.rawpixels + } + } } ImGui::EndChild(); } diff --git a/src/main.cpp b/src/main.cpp index 42a1a1d..ec0f3ba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,6 @@ #include #include "./theme.hpp" -#include "./sdlrenderer_texture_uploader.hpp" #include "./start_screen.hpp" @@ -63,6 +62,11 @@ int main(int argc, char** argv) { quit = true; break; } + + if (screen->handleEvent(event)) { + continue; + } + ImGui_ImplSDL3_ProcessEvent(&event); } if (quit) { diff --git a/src/main_screen.cpp b/src/main_screen.cpp index 5d4cfa4..6a2f5a3 100644 --- a/src/main_screen.cpp +++ b/src/main_screen.cpp @@ -55,6 +55,10 @@ MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path) : MainScreen::~MainScreen(void) { } +bool MainScreen::handleEvent(SDL_Event& e) { + return false; +} + Screen* MainScreen::poll(bool& quit) { auto new_time = std::chrono::high_resolution_clock::now(); const float time_delta {std::chrono::duration(new_time - last_time).count()}; diff --git a/src/main_screen.hpp b/src/main_screen.hpp index c8c71f5..6f9de6a 100644 --- a/src/main_screen.hpp +++ b/src/main_screen.hpp @@ -55,6 +55,8 @@ struct MainScreen final : public Screen { MainScreen(SDL_Renderer* renderer_, std::string save_path); ~MainScreen(void); + bool handleEvent(SDL_Event& e) override; + // return nullptr if not next // sets bool quit to true if exit Screen* poll(bool&) override; diff --git a/src/screen.hpp b/src/screen.hpp index c48d9d1..238f014 100644 --- a/src/screen.hpp +++ b/src/screen.hpp @@ -1,8 +1,13 @@ #pragma once +#include + struct Screen { virtual ~Screen(void) = default; + // return true if handled + virtual bool handleEvent(SDL_Event& e) { return false; } + // return nullptr if not next // sets bool quit to true if exit virtual Screen* poll(bool& quit) = 0; diff --git a/src/sdl_clipboard_utils.cpp b/src/sdl_clipboard_utils.cpp new file mode 100644 index 0000000..3968462 --- /dev/null +++ b/src/sdl_clipboard_utils.cpp @@ -0,0 +1,23 @@ +#include "./sdl_clipboard_utils.hpp" + +#include + +#include + +const char* clipboardHasImage(void) { + const static std::vector image_mime_types { + "image/webp", + "image/png", + "image/gif", + "image/jpeg", + "image/bmp", + }; + + for (const char* mime_type : image_mime_types) { + if (SDL_HasClipboardData(mime_type) == SDL_TRUE) { + return mime_type; + } + } + + return nullptr; +} diff --git a/src/sdl_clipboard_utils.hpp b/src/sdl_clipboard_utils.hpp new file mode 100644 index 0000000..3e1fd52 --- /dev/null +++ b/src/sdl_clipboard_utils.hpp @@ -0,0 +1,6 @@ +#pragma once + +// returns the mimetype (c-string) of the image in the clipboard +// or nullptr, if there is none +const char* clipboardHasImage(void); +