macOS: Determine opaqueness and backgroundColor of NSWindow declaratively

Instead of imperatively trying to keep the logic consistent in many
different call sites.

Task-number: QTBUG-61909
Change-Id: I8d647690c47656f34673555a8a8aa3ec6ffc73d1
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Tor Arne Vestbø 2017-08-24 14:01:13 +02:00
parent 2a131b94db
commit fefbed5eae
3 changed files with 24 additions and 9 deletions

View File

@ -749,17 +749,13 @@ void QCocoaWindow::setOpacity(qreal level)
return; return;
m_view.window.alphaValue = level; m_view.window.alphaValue = level;
m_view.window.opaque = isOpaque();
} }
void QCocoaWindow::setMask(const QRegion &region) void QCocoaWindow::setMask(const QRegion &region)
{ {
qCDebug(lcQpaCocoaWindow) << "QCocoaWindow::setMask" << window() << region; qCDebug(lcQpaCocoaWindow) << "QCocoaWindow::setMask" << window() << region;
if (isContentView())
m_view.window.backgroundColor = !region.isEmpty() ? [NSColor clearColor] : nil;
[qnsview_cast(m_view) setMaskRegion:&region]; [qnsview_cast(m_view) setMaskRegion:&region];
m_view.window.opaque = isOpaque();
} }
bool QCocoaWindow::setKeyboardGrabEnabled(bool grab) bool QCocoaWindow::setKeyboardGrabEnabled(bool grab)
@ -1318,11 +1314,6 @@ QCocoaNSWindow *QCocoaWindow::createNSWindow(bool shouldBePanel)
nsWindow.restorable = NO; nsWindow.restorable = NO;
nsWindow.level = windowLevel(flags); nsWindow.level = windowLevel(flags);
if (!isOpaque()) {
nsWindow.backgroundColor = [NSColor clearColor];
nsWindow.opaque = NO;
}
if (shouldBePanel) { if (shouldBePanel) {
// Qt::Tool windows hide on app deactivation, unless Qt::WA_MacAlwaysShowToolWindow is set // Qt::Tool windows hide on app deactivation, unless Qt::WA_MacAlwaysShowToolWindow is set
nsWindow.hidesOnDeactivate = ((type & Qt::Tool) == Qt::Tool) && !alwaysShowToolWindow(); nsWindow.hidesOnDeactivate = ((type & Qt::Tool) == Qt::Tool) && !alwaysShowToolWindow();

View File

@ -58,6 +58,8 @@ QT_FORWARD_DECLARE_CLASS(QCocoaWindow)
- (void)sendEvent:(NSEvent*)theEvent; - (void)sendEvent:(NSEvent*)theEvent;
- (void)closeAndRelease; - (void)closeAndRelease;
- (void)dealloc; - (void)dealloc;
- (BOOL)isOpaque;
- (NSColor *)backgroundColor;
@property (nonatomic, readonly) QCocoaWindow *platformWindow; @property (nonatomic, readonly) QCocoaWindow *platformWindow;
@end @end

View File

@ -153,6 +153,28 @@ static bool isMouseEvent(NSEvent *ev)
return canBecomeMain; return canBecomeMain;
} }
- (BOOL)isOpaque
{
return self.platformWindow ?
self.platformWindow->isOpaque() : qt_objcDynamicSuper();
}
/*!
Borderless windows need a transparent background
Technically windows with NSTexturedBackgroundWindowMask (such
as windows with unified toolbars) need to draw the textured
background of the NSWindow, and can't have a transparent
background, but as NSBorderlessWindowMask is 0, you can't
have a window with NSTexturedBackgroundWindowMask that is
also borderless.
*/
- (NSColor *)backgroundColor
{
return self.styleMask == NSBorderlessWindowMask
? [NSColor clearColor] : qt_objcDynamicSuper();
}
- (void)sendEvent:(NSEvent*)theEvent - (void)sendEvent:(NSEvent*)theEvent
{ {
// We might get events for a NSWindow after the corresponding platform // We might get events for a NSWindow after the corresponding platform