Make QWindow update its screen when moved to a different one

Also implements the Cocoa backend for that.

Change-Id: I32977e12a04e1cf48b12333442482746c69ce133
Reviewed-by: Samuel Rødal <samuel.rodal@digia.com>
Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
This commit is contained in:
Gabriel de Dietrich 2013-05-21 15:09:28 +02:00 committed by The Qt Project
parent f610814b05
commit 602bd98737
10 changed files with 82 additions and 16 deletions

View File

@ -1285,6 +1285,9 @@ void QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePriv
case QWindowSystemInterfacePrivate::WindowStateChanged:
QGuiApplicationPrivate::processWindowStateChangedEvent(static_cast<QWindowSystemInterfacePrivate::WindowStateChangedEvent *>(e));
break;
case QWindowSystemInterfacePrivate::WindowScreenChanged:
QGuiApplicationPrivate::processWindowScreenChangedEvent(static_cast<QWindowSystemInterfacePrivate::WindowScreenChangedEvent *>(e));
break;
case QWindowSystemInterfacePrivate::ApplicationStateChanged:
QGuiApplicationPrivate::processApplicationStateChangedEvent(static_cast<QWindowSystemInterfacePrivate::ApplicationStateChangedEvent *>(e));
break;
@ -1655,6 +1658,16 @@ void QGuiApplicationPrivate::processWindowStateChangedEvent(QWindowSystemInterfa
}
}
void QGuiApplicationPrivate::processWindowScreenChangedEvent(QWindowSystemInterfacePrivate::WindowScreenChangedEvent *wse)
{
if (QWindow *window = wse->window.data()) {
if (QScreen *screen = wse->screen.data())
window->d_func()->setScreen(screen, false /* recreate */);
else // Fall back to default behavior, and try to find some appropriate screen
window->setScreen(0);
}
}
void QGuiApplicationPrivate::processApplicationStateChangedEvent(QWindowSystemInterfacePrivate::ApplicationStateChangedEvent *e)
{
if (e->newState == applicationState)

View File

@ -126,6 +126,7 @@ public:
static void processActivatedEvent(QWindowSystemInterfacePrivate::ActivatedWindowEvent *e);
static void processWindowStateChangedEvent(QWindowSystemInterfacePrivate::WindowStateChangedEvent *e);
static void processWindowScreenChangedEvent(QWindowSystemInterfacePrivate::WindowScreenChangedEvent *e);
static void processApplicationStateChangedEvent(QWindowSystemInterfacePrivate::ApplicationStateChangedEvent *e);

View File

@ -345,6 +345,25 @@ void QWindowPrivate::updateVisibility()
emit q->visibilityChanged(visibility);
}
void QWindowPrivate::setScreen(QScreen *newScreen, bool recreate)
{
Q_Q(QWindow);
if (newScreen != q->screen()) {
const bool shouldRecreate = recreate && platformWindow != 0;
if (shouldRecreate)
q->destroy();
if (screen)
q->disconnect(screen, SIGNAL(destroyed(QObject*)), q, SLOT(screenDestroyed(QObject*)));
screen = newScreen;
if (newScreen) {
q->connect(screen, SIGNAL(destroyed(QObject*)), q, SLOT(screenDestroyed(QObject*)));
if (shouldRecreate)
q->create();
}
emit q->screenChanged(newScreen);
}
}
/*!
Sets the \a surfaceType of the window.
@ -1568,20 +1587,7 @@ void QWindow::setScreen(QScreen *newScreen)
Q_D(QWindow);
if (!newScreen)
newScreen = QGuiApplication::primaryScreen();
if (newScreen != screen()) {
const bool wasCreated = d->platformWindow != 0;
if (wasCreated)
destroy();
if (d->screen)
disconnect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));
d->screen = newScreen;
if (newScreen) {
connect(d->screen, SIGNAL(destroyed(QObject*)), this, SLOT(screenDestroyed(QObject*)));
if (wasCreated)
create();
}
emit screenChanged(newScreen);
}
d->setScreen(newScreen, true /* recreate */);
}
void QWindow::screenDestroyed(QObject *object)

View File

@ -127,6 +127,8 @@ public:
void updateVisibility();
void _q_clearAlert();
void setScreen(QScreen *newScreen, bool recreate);
QWindow::SurfaceType surfaceType;
Qt::WindowFlags windowFlags;
QWindow *parentWindow;

View File

@ -126,6 +126,13 @@ void QWindowSystemInterface::handleWindowStateChanged(QWindow *tlw, Qt::WindowSt
QWindowSystemInterfacePrivate::handleWindowSystemEvent(e);
}
void QWindowSystemInterface::handleWindowScreenChanged(QWindow *tlw, QScreen *screen)
{
QWindowSystemInterfacePrivate::WindowScreenChangedEvent *e =
new QWindowSystemInterfacePrivate::WindowScreenChangedEvent(tlw, screen);
QWindowSystemInterfacePrivate::handleWindowSystemEvent(e);
}
void QWindowSystemInterface::handleApplicationStateChanged(Qt::ApplicationState newState)
{
Q_ASSERT(QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ApplicationState));

View File

@ -138,6 +138,7 @@ public:
static void handleWindowActivated(QWindow *w, Qt::FocusReason r = Qt::OtherFocusReason);
static void handleWindowStateChanged(QWindow *w, Qt::WindowState newState);
static void handleWindowScreenChanged(QWindow *w, QScreen *newScreen);
static void handleApplicationStateChanged(Qt::ApplicationState newState);

View File

@ -91,7 +91,8 @@ public:
PlatformPanel = UserInputEvent | 0x17,
ContextMenu = UserInputEvent | 0x18,
ApplicationStateChanged = 0x19,
FlushEvents = 0x20
FlushEvents = 0x20,
WindowScreenChanged = 0x21
};
class WindowSystemEvent {
@ -158,6 +159,16 @@ public:
Qt::WindowState newState;
};
class WindowScreenChangedEvent : public WindowSystemEvent {
public:
WindowScreenChangedEvent(QWindow *w, QScreen *s)
: WindowSystemEvent(WindowScreenChanged), window(w), screen(s)
{ }
QPointer<QWindow> window;
QPointer<QScreen> screen;
};
class ApplicationStateChangedEvent : public WindowSystemEvent {
public:
ApplicationStateChangedEvent(Qt::ApplicationState newState)

View File

@ -100,11 +100,23 @@ static void cleanupCocoaApplicationDelegate()
- (id)init
{
self = [super init];
if (self)
if (self) {
inLaunch = true;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(updateScreens:)
name:NSApplicationDidChangeScreenParametersNotification
object:NSApp];
}
return self;
}
- (void)updateScreens:(NSNotification *)notification
{
if (QCocoaIntegration *ci = dynamic_cast<QCocoaIntegration *>(QGuiApplicationPrivate::platformIntegration()))
ci->updateScreens();
}
- (void)dealloc
{
sharedCocoaApplicationDelegate = nil;
@ -114,6 +126,8 @@ static void cleanupCocoaApplicationDelegate()
[NSApp setDelegate:reflectionDelegate];
[reflectionDelegate release];
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}

View File

@ -125,6 +125,7 @@ public:
QList<int> possibleKeys(const QKeyEvent *event) const;
void updateScreens();
QCocoaScreen *screenAtIndex(int index) const { return mScreens.at(index); }
private:

View File

@ -58,6 +58,7 @@
#include <private/qguiapplication_p.h>
#include "qcocoabackingstore.h"
#include "qcocoaglcontext.h"
#include "qcocoaintegration.h"
#ifdef QT_COCOA_ENABLE_ACCESSIBILITY_INSPECTOR
#include <accessibilityinspector.h>
@ -276,6 +277,15 @@ static QTouchDevice *touchDevice = 0;
m_platformWindow->obscureWindow();
} else if ([notificationName isEqualToString: @"NSWindowDidOrderOnScreenAndFinishAnimatingNotification"]) {
m_platformWindow->exposeWindow();
} else if (notificationName == NSWindowDidChangeScreenNotification) {
if (m_window) {
QCocoaIntegration *ci = static_cast<QCocoaIntegration *>(QGuiApplicationPrivate::platformIntegration());
NSUInteger screenIndex = [[NSScreen screens] indexOfObject:self.window.screen];
if (screenIndex != NSNotFound) {
QCocoaScreen *cocoaScreen = ci->screenAtIndex(screenIndex);
QWindowSystemInterface::handleWindowScreenChanged(m_window, cocoaScreen->screen());
}
}
} else {
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7