Skip to content

Commit

Permalink
[qtmozembed] Add an explicit orientation property to WebView. Contrib…
Browse files Browse the repository at this point in the history
…utes to JB#48722

The window orientation remains the default source, but can be
overwritten to better match the state of the owning UI.
  • Loading branch information
adenexter committed Mar 16, 2020
1 parent 8e336e4 commit 8c0e5d9
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 109 deletions.
1 change: 0 additions & 1 deletion src/qmozview_p.cpp
Expand Up @@ -111,7 +111,6 @@ QMozViewPrivate::QMozViewPrivate(IMozQViewIface *aViewIface, QObject *publicPtr)
, mIsInputFieldFocused(false)
, mPreedit(false)
, mViewIsFocused(false)
, mHasContext(false)
, mPressed(false)
, mDragging(false)
, mFlicking(false)
Expand Down
1 change: 0 additions & 1 deletion src/qmozview_p.h
Expand Up @@ -181,7 +181,6 @@ public Q_SLOTS:
bool mIsInputFieldFocused;
bool mPreedit;
bool mViewIsFocused;
bool mHasContext;
bool mPressed;
bool mDragging;
bool mFlicking;
Expand Down
1 change: 0 additions & 1 deletion src/qopenglwebpage.cpp
Expand Up @@ -67,7 +67,6 @@ QOpenGLWebPage::QOpenGLWebPage(QObject *parent)
, mThrottlePainting(false)
{
d->mContext = QMozContext::instance();
d->mHasContext = true;

connect(this, SIGNAL(viewInitialized()), this, SLOT(processViewInitialization()));
connect(this, SIGNAL(loadProgressChanged()), this, SLOT(updateLoaded()));
Expand Down
207 changes: 112 additions & 95 deletions src/quickmozview.cpp
Expand Up @@ -59,12 +59,10 @@ class ObjectCleanup : public QRunnable

}

QSizeF webContentWindowSize(const QQuickWindow *window, const QSizeF &size) {
Q_ASSERT(window);

QSizeF webContentWindowSize(Qt::ScreenOrientation orientation, const QSizeF &size)
{
// Set size for EmbedLiteWindow in "portrait"
QSizeF s = size;
Qt::ScreenOrientation orientation = window->contentOrientation();
if (orientation == Qt::LandscapeOrientation || orientation == Qt::InvertedLandscapeOrientation) {
s.transpose();
}
Expand All @@ -76,11 +74,14 @@ QuickMozView::QuickMozView(QQuickItem *parent)
, d(new QMozViewPrivate(new IMozQView<QuickMozView>(*this), this))
, mTexture(nullptr)
, mParentID(0)
, mOrientation(Qt::PrimaryOrientation)
, mExplicitViewportWidth(false)
, mExplicitViewportHeight(false)
, mExplicitOrientation(false)
, mPrivateMode(false)
, mUseQmlMouse(false)
, mComposited(false)
, mActive(false)
, mBackground(false)
, mLoaded(false)
, mFollowItemGeometry(true)
{
Expand Down Expand Up @@ -154,59 +155,25 @@ void QuickMozView::updateEnabled()
d->mEnabled = QQuickItem::isEnabled();
}

void QuickMozView::updateGLContextInfo(QOpenGLContext *ctx)
{
d->mHasContext = ctx != nullptr && ctx->surface() != nullptr;
if (!d->mHasContext) {
printf("ERROR: QuickMozView not supposed to work without GL context\n");
return;
}
}

void QuickMozView::itemChange(ItemChange change, const ItemChangeData &data)
{
if (change == ItemSceneChange) {
QQuickWindow *win = window();
if (!win)
return;
// All of these signals are emitted from scene graph rendering thread.
connect(win, SIGNAL(beforeSynchronizing()), this, SLOT(createThreadRenderObject()), Qt::DirectConnection);
connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(clearThreadRenderObject()), Qt::DirectConnection);
connect(win, SIGNAL(visibleChanged(bool)), this, SLOT(windowVisibleChanged(bool)));
connect(win, &QQuickWindow::contentOrientationChanged, this, [=](Qt::ScreenOrientation orientation) {
if (d->mMozWindow) {
d->mMozWindow->setContentOrientation(orientation);
}
});
if (d->mSize.isEmpty()) {
d->mSize = win->size();
if (data.window) {
connect(data.window, &QQuickWindow::contentOrientationChanged, this, &QuickMozView::updateOrientation);

updateOrientation(data.window->contentOrientation());
}
}
QQuickItem::itemChange(change, data);
}

void QuickMozView::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
if (mFollowItemGeometry) {
updateContentSize(newGeometry.size());
}
QQuickItem::geometryChanged(newGeometry, oldGeometry);
}

void QuickMozView::createThreadRenderObject()
{
updateGLContextInfo(QOpenGLContext::currentContext());
disconnect(window(), SIGNAL(beforeSynchronizing()), this, 0);
}

void QuickMozView::clearThreadRenderObject()
{
QOpenGLContext *ctx = QOpenGLContext::currentContext();
Q_ASSERT(ctx != NULL && ctx->makeCurrent(ctx->surface()));
updateContentSize(QSizeF(
mExplicitViewportWidth ? d->mSize.width() : newGeometry.width(),
mExplicitViewportHeight ? d->mSize.height() : newGeometry.height()));

QQuickWindow *win = window();
if (!win) return;
connect(win, SIGNAL(beforeSynchronizing()), this, SLOT(createThreadRenderObject()), Qt::DirectConnection);
QQuickItem::geometryChanged(newGeometry, oldGeometry);
}

void QuickMozView::createView()
Expand All @@ -217,14 +184,14 @@ void QuickMozView::createView()

QMozWindow *mozWindow = d->mContext->registeredWindow();
if (!mozWindow) {
mozWindow = new QMozWindow(webContentWindowSize(window(), d->mSize).toSize());
mozWindow = new QMozWindow(webContentWindowSize(mOrientation, d->mSize).toSize());
d->mContext->registerWindow(mozWindow);
} else if (d->mDirtyState & QMozViewPrivate::DirtySize) {
updateContentSize(d->mSize);
} else if (d->mDirtyState & QMozViewPrivate::DirtySize && mActive) {
mozWindow->setSize(webContentWindowSize(mOrientation, d->mSize).toSize());
}

if (window()) {
mozWindow->setContentOrientation(window()->contentOrientation());
if (mActive) {
mozWindow->setContentOrientation(mOrientation);
}

d->setMozWindow(mozWindow);
Expand Down Expand Up @@ -300,8 +267,8 @@ QuickMozView::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
node->setTexture(mTexture);
}

node->setRect(QRectF(d->renderingOffset(), d->mSize));
node->setOrientation(window()->contentOrientation());
node->setRect(boundingRect);
node->setOrientation(mOrientation);
node->markDirty(QSGNode::DirtyMaterial);

return node;
Expand All @@ -321,14 +288,6 @@ void QuickMozView::releaseResources()
}
}

void QuickMozView::windowVisibleChanged(bool visible)
{
if (visible == mBackground) {
mBackground = !visible;
Q_EMIT backgroundChanged();
}
}

int QuickMozView::parentId() const
{
return mParentID;
Expand All @@ -353,6 +312,7 @@ void QuickMozView::setActive(bool active)
SetIsActive(active);
if (active) {
resumeRendering();
polish();
} else {
mComposited = false;
update();
Expand All @@ -365,37 +325,11 @@ void QuickMozView::setActive(bool active)
}
}

bool QuickMozView::background() const
{
return mBackground;
}

bool QuickMozView::loaded() const
{
return mLoaded;
}

bool QuickMozView::followItemGeometry() const
{
return mFollowItemGeometry;
}

/*!
* \fn QuickMozView::setFollowItemGeometry(bool follow)
* Set this to false when you need to control content size manually.
* For instance virtual keyboard opening should not resize the content
* rather you should set margins for the content when opening virtual keyboard.
* Remember to set this back to true after you have finished manual content size
* manipulation and/or virtual is lowered.
*/
void QuickMozView::setFollowItemGeometry(bool follow)
{
if (mFollowItemGeometry != follow) {
mFollowItemGeometry = follow;
Q_EMIT followItemGeometryChanged();
}
}

/*!
* \fn QuickMozView::updateContentSize(const QSize &size)
* Updates web content size to given \a size. Web content size can be
Expand All @@ -407,15 +341,18 @@ void QuickMozView::setFollowItemGeometry(bool follow)
*/
void QuickMozView::updateContentSize(const QSizeF &size)
{
// Skip noise coming from rotation change as width and height is updated
// separately. Downside is that this breaks intentional resizing to square but
// I think that this is less evil.
const QSizeF originalSize = d->mSize;

polish();

if (d->mMozWindow && (size.width() != size.height())) {
QSizeF s = webContentWindowSize(window(), size);
d->mMozWindow->setSize(s.toSize());
}
d->setSize(size);

if (d->mSize.width() != originalSize.width()) {
Q_EMIT viewportWidthChanged();
}
if (d->mSize.height() != originalSize.height()) {
Q_EMIT viewportHeightChanged();
}
}

void QuickMozView::compositingFinished()
Expand Down Expand Up @@ -700,6 +637,78 @@ QMargins QuickMozView::margins() const
return d->mMargins;
}

Qt::ScreenOrientation QuickMozView::orientation() const
{
return mOrientation;
}

void QuickMozView::setOrientation(Qt::ScreenOrientation orientation)
{
mExplicitOrientation = true;
if (mOrientation != orientation && orientation != Qt::PrimaryOrientation) {
polish();

mOrientation = orientation;

Q_EMIT orientationChanged();
}
}

void QuickMozView::resetOrientation()
{
mExplicitOrientation = false;
if (QQuickWindow *window = this->window()) {
updateOrientation(window->contentOrientation());
}
}

void QuickMozView::updateOrientation(Qt::ScreenOrientation orientation)
{
if (!mExplicitOrientation) {
polish();

if (mOrientation != orientation) {
mOrientation = orientation;

Q_EMIT orientationChanged();
}
}
}

qreal QuickMozView::viewportWidth() const
{
return d->mSize.width();
}

void QuickMozView::setViewportWidth(qreal width)
{
mExplicitViewportWidth = true;
updateContentSize(QSize(width, d->mSize.height()));
}

void QuickMozView::resetViewportWidth()
{
mExplicitViewportWidth = false;
updateContentSize(QSize(width(), d->mSize.height()));
}

qreal QuickMozView::viewportHeight() const
{
return d->mSize.height();
}

void QuickMozView::setViewportHeight(qreal height)
{
mExplicitViewportHeight = true;
updateContentSize(QSize(d->mSize.width(), height));
}

void QuickMozView::resetViewportHeight()
{
mExplicitViewportHeight = false;
updateContentSize(QSize(d->mSize.width(), height()));
}

void QuickMozView::setMargins(QMargins margins)
{
d->SetMargins(margins, true);
Expand Down Expand Up @@ -908,3 +917,11 @@ void QuickMozView::resumeRendering()
{
d->mMozWindow->resumeRendering();
}

void QuickMozView::updatePolish()
{
if (d->mMozWindow && mActive) {
d->mMozWindow->setContentOrientation(mOrientation);
d->mMozWindow->setSize(webContentWindowSize(mOrientation, d->mSize).toSize());
}
}

0 comments on commit 8c0e5d9

Please sign in to comment.