fold WebEntityAPIHelper members into OffscreenQmlSurface

This commit is contained in:
Seth Alves 2016-09-09 13:41:09 -07:00
parent f6398315b1
commit dd39d46883
3 changed files with 29 additions and 60 deletions

View file

@ -100,7 +100,7 @@ bool RenderableWebEntityItem::buildWebSurface(EntityTreeRenderer* renderer) {
// forward web events to EntityScriptingInterface
auto entities = DependencyManager::get<EntityScriptingInterface>();
const EntityItemID entityItemID = getID();
QObject::connect(_webSurface->_webEntityAPIHelper, &WebEntityAPIHelper::webEventReceived, [=](const QVariant& message) {
QObject::connect(_webSurface, &OffscreenQmlSurface::webEventReceived, [=](const QVariant& message) {
emit entities->webEventReceived(entityItemID, message);
});
@ -308,7 +308,7 @@ void RenderableWebEntityItem::destroyWebSurface() {
// The lifetime of the QML surface MUST be managed by the main thread
// Additionally, we MUST use local variables copied by value, rather than
// member variables, since they would implicitly refer to a this that
// member variables, since they would implicitly refer to a this that
// is no longer valid
auto webSurface = _webSurface;
AbstractViewStateInterface::instance()->postLambdaEvent([webSurface] {
@ -318,7 +318,6 @@ void RenderableWebEntityItem::destroyWebSurface() {
}
}
void RenderableWebEntityItem::update(const quint64& now) {
auto interval = now - _lastRenderTime;
if (interval > MAX_NO_RENDER_INTERVAL) {
@ -326,12 +325,11 @@ void RenderableWebEntityItem::update(const quint64& now) {
}
}
bool RenderableWebEntityItem::isTransparent() {
float fadeRatio = _isFading ? Interpolate::calculateFadeRatio(_fadeStartTime) : 1.0f;
return fadeRatio < OPAQUE_ALPHA_THRESHOLD;
}
void RenderableWebEntityItem::emitScriptEvent(const QVariant& message) {
_webSurface->_webEntityAPIHelper->emitScriptEvent(message);
_webSurface->emitScriptEvent(message);
}

View file

@ -419,9 +419,7 @@ bool OffscreenQmlRenderThread::allowNewFrame(uint8_t fps) {
}
OffscreenQmlSurface::OffscreenQmlSurface() {
_webEntityAPIHelper = new WebEntityAPIHelper;
_webEntityAPIHelper->setOffscreenQmlSurface(this);
_webEntityAPIHelper->moveToThread(qApp->thread());
// moveToThread(qApp->thread());
}
static const uint64_t MAX_SHUTDOWN_WAIT_SECS = 2;
@ -435,9 +433,6 @@ OffscreenQmlSurface::~OffscreenQmlSurface() {
qWarning() << "Failed to shut down the QML Renderer Thread";
}
_webEntityAPIHelper->setOffscreenQmlSurface(nullptr);
_webEntityAPIHelper->deleteLater();
delete _rootItem;
delete _renderer;
delete _qmlComponent;
@ -540,13 +535,13 @@ QObject* OffscreenQmlSurface::load(const QUrl& qmlSource, std::function<void(QQm
_qmlComponent->loadUrl(qmlSource, QQmlComponent::PreferSynchronous);
if (_qmlComponent->isLoading()) {
connect(_qmlComponent, &QQmlComponent::statusChanged, this,
connect(_qmlComponent, &QQmlComponent::statusChanged, this,
[this, f](QQmlComponent::Status){
finishQmlLoad(f);
});
return nullptr;
}
return finishQmlLoad(f);
}
@ -588,7 +583,7 @@ QObject* OffscreenQmlSurface::finishQmlLoad(std::function<void(QQmlContext*, QOb
// All quick items should be focusable
QQuickItem* newItem = qobject_cast<QQuickItem*>(newObject);
if (newItem) {
// Make sure we make items focusable (critical for
// Make sure we make items focusable (critical for
// supporting keyboard shortcuts)
newItem->setFlag(QQuickItem::ItemIsFocusScope, true);
}
@ -616,11 +611,11 @@ QObject* OffscreenQmlSurface::finishQmlLoad(std::function<void(QQmlContext*, QOb
}
void OffscreenQmlSurface::updateQuick() {
// If we're
// If we're
// a) not set up
// b) already rendering a frame
// c) rendering too fast
// then skip this
// then skip this
if (!_renderer || _renderer->_rendering || !_renderer->allowNewFrame(_maxFps)) {
return;
}
@ -691,7 +686,6 @@ bool OffscreenQmlSurface::eventFilter(QObject* originalDestination, QEvent* even
}
#endif
switch (event->type()) {
case QEvent::Resize: {
QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
@ -761,8 +755,8 @@ void OffscreenQmlSurface::resume() {
_paused = false;
requestRender();
getRootItem()->setProperty("eventBridge", QVariant::fromValue(_webEntityAPIHelper));
getRootContext()->setContextProperty("webEntity", _webEntityAPIHelper);
getRootItem()->setProperty("eventBridge", QVariant::fromValue(this));
getRootContext()->setContextProperty("webEntity", this);
}
bool OffscreenQmlSurface::isPaused() const {
@ -897,13 +891,7 @@ void OffscreenQmlSurface::setKeyboardRaised(bool raised) {
}
void WebEntityAPIHelper::synthesizeKeyPress(QString key) {
if (_offscreenQmlSurface) {
_offscreenQmlSurface->synthesizeKeyPress(key);
}
}
void WebEntityAPIHelper::emitScriptEvent(const QVariant& message) {
void OffscreenQmlSurface::emitScriptEvent(const QVariant& message) {
if (QThread::currentThread() != thread()) {
QMetaObject::invokeMethod(this, "emitScriptEvent", Qt::QueuedConnection, Q_ARG(QVariant, message));
} else {
@ -911,15 +899,15 @@ void WebEntityAPIHelper::emitScriptEvent(const QVariant& message) {
}
}
void WebEntityAPIHelper::emitWebEvent(const QVariant& message) {
void OffscreenQmlSurface::emitWebEvent(const QVariant& message) {
if (QThread::currentThread() != thread()) {
QMetaObject::invokeMethod(this, "emitWebEvent", Qt::QueuedConnection, Q_ARG(QVariant, message));
} else {
// special case to handle raising and lowering the virtual keyboard
if (message.type() == QVariant::String && message.toString() == "_RAISE_KEYBOARD" && _offscreenQmlSurface) {
_offscreenQmlSurface->setKeyboardRaised(true);
} else if (message.type() == QVariant::String && message.toString() == "_LOWER_KEYBOARD" && _offscreenQmlSurface) {
_offscreenQmlSurface->setKeyboardRaised(false);
if (message.type() == QVariant::String && message.toString() == "_RAISE_KEYBOARD") {
setKeyboardRaised(true);
} else if (message.type() == QVariant::String && message.toString() == "_LOWER_KEYBOARD") {
setKeyboardRaised(false);
} else {
emit webEventReceived(message);
}

View file

@ -25,31 +25,7 @@ class QQmlContext;
class QQmlComponent;
class QQuickWindow;
class QQuickItem;
class OffscreenQmlRenderThread;
class OffscreenQmlSurface;
class WebEntityAPIHelper : public QObject {
Q_OBJECT
public:
void setOffscreenQmlSurface(OffscreenQmlSurface* renderableWebEntityItem) {
_offscreenQmlSurface = renderableWebEntityItem;
}
Q_INVOKABLE void synthesizeKeyPress(QString key);
// event bridge
public slots:
void emitScriptEvent(const QVariant& scriptMessage);
void emitWebEvent(const QVariant& webMessage);
signals:
void scriptEventReceived(const QVariant& message);
void webEventReceived(const QVariant& message);
protected:
OffscreenQmlSurface* _offscreenQmlSurface{ nullptr };
};
class OffscreenQmlSurface : public QObject {
Q_OBJECT
@ -92,13 +68,10 @@ public:
QPointF mapToVirtualScreen(const QPointF& originalPoint, QObject* originalWidget);
bool eventFilter(QObject* originalDestination, QEvent* event) override;
void setKeyboardRaised(bool raised);
void synthesizeKeyPress(QString key);
Q_INVOKABLE void setKeyboardRaised(bool raised);
Q_INVOKABLE void synthesizeKeyPress(QString key);
// XXX make private
WebEntityAPIHelper* _webEntityAPIHelper;
signals:
void textureUpdated(unsigned int texture);
void focusObjectChanged(QObject* newFocus);
@ -109,6 +82,16 @@ public slots:
void requestRender();
void onAboutToQuit();
// event bridge
public slots:
void emitScriptEvent(const QVariant& scriptMessage);
void emitWebEvent(const QVariant& webMessage);
signals:
void scriptEventReceived(const QVariant& message);
void webEventReceived(const QVariant& message);
protected:
bool filterEnabled(QObject* originalDestination, QEvent* event) const;
void setFocusText(bool newFocusText);