improved clipboard handler

This commit is contained in:
Jon ESA
2026-04-01 21:03:21 +01:00
parent 693e1d8e37
commit 90493a3864

View File

@@ -1,4 +1,6 @@
#include <QApplication>
#include <QClipboard>
#include <QGuiApplication>
#include <QButtonGroup>
#include <QFont>
#include <QGridLayout>
@@ -25,6 +27,48 @@
#include "VersionsPanel.h"
#include "WebSocketController.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#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(copyLogBtn, &QPushButton::clicked, page,
[log]() { copyToClipboard(log->toPlainText()); });
return page;
}