From 90493a3864717b61e7f47bb99864c4d855c9e8fc Mon Sep 17 00:00:00 2001 From: Jon ESA Date: Wed, 1 Apr 2026 21:03:21 +0100 Subject: [PATCH] improved clipboard handler --- main.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index cd2fcab..8f3eda2 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -25,6 +27,48 @@ #include "VersionsPanel.h" #include "WebSocketController.h" +#ifdef __EMSCRIPTEN__ +#include +#endif + +// --------------------------------------------------------------------------- +// Copy text to the OS clipboard. +// Uses navigator.clipboard (modern, async) with execCommand fallback (HTTP). +// Falls back to Qt clipboard on non-WASM builds. +// --------------------------------------------------------------------------- +static void copyToClipboard(const QString &text) +{ +#ifdef __EMSCRIPTEN__ + QByteArray utf8 = text.toUtf8(); + EM_ASM({ + var txt = UTF8ToString($0, $1); + if (navigator.clipboard && navigator.clipboard.writeText) { + navigator.clipboard.writeText(txt).catch(function() { + var ta = document.createElement('textarea'); + ta.value = txt; + ta.style.position = 'fixed'; + ta.style.opacity = '0'; + document.body.appendChild(ta); + ta.focus(); ta.select(); + document.execCommand('copy'); + document.body.removeChild(ta); + }); + } else { + var ta = document.createElement('textarea'); + ta.value = txt; + ta.style.position = 'fixed'; + ta.style.opacity = '0'; + document.body.appendChild(ta); + ta.focus(); ta.select(); + document.execCommand('copy'); + document.body.removeChild(ta); + } + }, utf8.constData(), utf8.size()); +#else + QGuiApplication::clipboard()->setText(text); +#endif +} + static QWidget *makeGamesTab(WebSocketController *ctrl, QWidget *parent) { auto *panel = new GamesPanel(parent); @@ -270,17 +314,27 @@ static QWidget *makeManualTab(WebSocketController *ctrl, QWidget *parent) } layout->addLayout(inputGrid); - // Log view + clear button - auto *clearBtn = new QPushButton("Clear Log", page); - clearBtn->setMaximumWidth(100); + // Log view + action buttons auto *log = new QTextEdit(page); log->setReadOnly(true); log->setFont(QFont("Courier", 9)); + + auto *logBtnRow = new QHBoxLayout(); + auto *copyLogBtn = new QPushButton("Copy Log", page); + auto *clearBtn = new QPushButton("Clear Log", page); + copyLogBtn->setMaximumWidth(110); + clearBtn->setMaximumWidth(100); + logBtnRow->addWidget(copyLogBtn); + logBtnRow->addWidget(clearBtn); + logBtnRow->addStretch(1); + layout->addWidget(log, 1); - layout->addWidget(clearBtn); + layout->addLayout(logBtnRow); ctrl->addLogView(log); - QObject::connect(clearBtn, &QPushButton::clicked, log, &QTextEdit::clear); + QObject::connect(clearBtn, &QPushButton::clicked, log, &QTextEdit::clear); + QObject::connect(copyLogBtn, &QPushButton::clicked, page, + [log]() { copyToClipboard(log->toPlainText()); }); return page; }