impr: Added clip library to improve clipboard situation

This commit is contained in:
WerWolv
2025-05-29 19:56:46 +02:00
parent 224fa83c65
commit 96ef758bbd
11 changed files with 146 additions and 36 deletions

View File

@@ -42,6 +42,7 @@ set(LIBIMHEX_SOURCES
source/helpers/keys.cpp
source/helpers/freetype.cpp
source/helpers/udp_server.cpp
source/helpers/clipboard.cpp
source/test/tests.cpp
@@ -160,8 +161,7 @@ if (NOT IMHEX_EXTERNAL_PLUGIN_BUILD)
endif()
if (NOT EMSCRIPTEN)
# curl is only used in non-emscripten builds
target_link_libraries(libimhex ${LIBIMHEX_LIBRARY_TYPE} CURL::libcurl)
target_link_libraries(libimhex ${LIBIMHEX_LIBRARY_TYPE} CURL::libcurl clip)
endif()
target_include_directories(libimhex ${LIBIMHEX_LIBRARY_TYPE} ${MBEDTLS_INCLUDE_DIR} ${LIBBACKTRACE_INCLUDE_DIRS} ${MAGIC_INCLUDE_DIRS})

View File

@@ -0,0 +1,16 @@
#pragma once
#include <hex.hpp>
#include <span>
#include <string>
#include <vector>
namespace hex::clipboard {
void init();
void setBinaryData(std::span<const u8> data);
[[nodiscard]] std::vector<u8> getBinaryData();
void setTextData(const std::string &string);
[[nodiscard]] std::string getTextData();
}

View File

@@ -0,0 +1,91 @@
#include <hex/helpers/clipboard.hpp>
#if __has_include(<clip.h>)
#define CLIP_LIBRARY
#endif
#if defined(CLIP_LIBRARY)
#include <clip.h>
#else
#include <imgui.h>
#include <fmt/color.h>
#include <hex/helpers/utils.hpp>
#endif
namespace hex::clipboard {
#if defined(CLIP_LIBRARY)
static clip::format s_binaryFormat;
static clip::format s_textFormat;
void init() {
s_binaryFormat = clip::register_format("net.werwolv.imhex.binary");
s_textFormat = clip::text_format();
}
void setBinaryData(std::span<const u8> data) {
clip::lock l;
l.set_data(s_binaryFormat, reinterpret_cast<const char*>(data.data()), data.size());
}
std::vector<u8> getBinaryData() {
clip::lock l;
const auto size = l.get_data_length(s_binaryFormat);
std::vector<u8> data(size);
l.get_data(s_binaryFormat, reinterpret_cast<char*>(data.data()), size);
return data;
}
void setTextData(const std::string &string) {
clip::lock l;
l.set_data(s_textFormat, string.data(), string.size());
}
std::string getTextData() {
clip::lock l;
const auto size = l.get_data_length(s_binaryFormat);
std::string data(size, 0x00);
l.get_data(s_textFormat, data.data(), size);
return data;
}
#else
void init() {}
void setBinaryData(std::span<const u8> data) {
constexpr static auto Format = "{0:02X} ";
std::string result;
result.reserve(fmt::format(Format, 0x00).size() * data.size_bytes());
for (const auto &byte : data)
result += fmt::format(Format, byte);
result.pop_back();
ImGui::SetClipboardText(result.c_str());
}
std::vector<u8> getBinaryData() {
auto clipboard = ImGui::GetClipboardText();
if (clipboard == nullptr)
return {};
return parseHexString(clipboard);
}
void setTextData(const std::string &string) {
ImGui::SetClipboardText(string.c_str());
}
std::string getTextData() {
return ImGui::GetClipboardText();
}
#endif
}

1
lib/third_party/clip vendored Submodule

Submodule lib/third_party/clip added at 7a60eabaad