From 18e06c37e10943f4b4f6d57b7044b9bce3a23202 Mon Sep 17 00:00:00 2001 From: Alexandra Cherdantseva Date: Mon, 30 Dec 2019 14:40:03 +0300 Subject: wasm: do not get canvas as property of js global object You cannot be sure that property with specified key in a global object is really a canvas. Should use `document.getElementById`. Change-Id: Ife55adaad5517aed64122b0c9bff32489cf19a2f Reviewed-by: Lorn Potter --- src/plugins/platforms/wasm/qwasmclipboard.cpp | 4 +++- src/plugins/platforms/wasm/qwasmcursor.cpp | 8 ++++++-- src/plugins/platforms/wasm/qwasmeventtranslator.cpp | 12 +++++++----- src/plugins/platforms/wasm/qwasmopenglcontext.cpp | 2 +- src/plugins/platforms/wasm/qwasmscreen.cpp | 9 ++++++--- 5 files changed, 23 insertions(+), 12 deletions(-) (limited to 'src/plugins/platforms') diff --git a/src/plugins/platforms/wasm/qwasmclipboard.cpp b/src/plugins/platforms/wasm/qwasmclipboard.cpp index d4a1e4dd50..fb46f1534f 100644 --- a/src/plugins/platforms/wasm/qwasmclipboard.cpp +++ b/src/plugins/platforms/wasm/qwasmclipboard.cpp @@ -198,7 +198,9 @@ void QWasmClipboard::installEventHandlers(const QString &canvasId) return; // Fallback path for browsers which do not support direct clipboard access - val canvas = val::global(canvasId.toUtf8().constData()); + val document = val::global("document"); + val canvas = document.call("getElementById", val(canvasId.toUtf8().constData())); + canvas.call("addEventListener", std::string("cut"), val::module_property("qtClipboardCutTo")); canvas.call("addEventListener", std::string("copy"), diff --git a/src/plugins/platforms/wasm/qwasmcursor.cpp b/src/plugins/platforms/wasm/qwasmcursor.cpp index 28ec3b58dd..c04fa6441a 100644 --- a/src/plugins/platforms/wasm/qwasmcursor.cpp +++ b/src/plugins/platforms/wasm/qwasmcursor.cpp @@ -36,6 +36,8 @@ #include #include +using namespace emscripten; + void QWasmCursor::changeCursor(QCursor *windowCursor, QWindow *window) { if (!windowCursor || !window) @@ -54,8 +56,10 @@ void QWasmCursor::changeCursor(QCursor *windowCursor, QWindow *window) htmlCursorName = "auto"; // Set cursor on the canvas - QString canvasId = QWasmScreen::get(screen)->canvasId(); - emscripten::val canvasStyle = emscripten::val::global(canvasId.toUtf8().constData())["style"]; + QByteArray canvasId = QWasmScreen::get(screen)->canvasId().toUtf8(); + val document = val::global("document"); + val canvas = document.call("getElementById", val(canvasId.constData())); + val canvasStyle = canvas["style"]; canvasStyle.set("cursor", emscripten::val(htmlCursorName.constData())); } diff --git a/src/plugins/platforms/wasm/qwasmeventtranslator.cpp b/src/plugins/platforms/wasm/qwasmeventtranslator.cpp index ad94ba9c77..8b7d00082f 100644 --- a/src/plugins/platforms/wasm/qwasmeventtranslator.cpp +++ b/src/plugins/platforms/wasm/qwasmeventtranslator.cpp @@ -47,9 +47,10 @@ #include -QT_BEGIN_NAMESPACE using namespace emscripten; +QT_BEGIN_NAMESPACE + typedef struct emkb2qt { const char *em; unsigned int qt; @@ -353,10 +354,11 @@ void QWasmEventTranslator::initEventHandlers() g_useNaturalScrolling = false; // make this !default on macOS if (emscripten::val::global("window")["safari"].isUndefined()) { - - emscripten::val::global(canvasId).call("addEventListener", - std::string("wheel"), - val::module_property("qtMouseWheelEvent")); + val document = val::global("document"); + val canvas = document.call("getElementById", val(canvasId)); + canvas.call("addEventListener", + std::string("wheel"), + val::module_property("qtMouseWheelEvent")); } } diff --git a/src/plugins/platforms/wasm/qwasmopenglcontext.cpp b/src/plugins/platforms/wasm/qwasmopenglcontext.cpp index 0532b7e726..501ab99116 100644 --- a/src/plugins/platforms/wasm/qwasmopenglcontext.cpp +++ b/src/plugins/platforms/wasm/qwasmopenglcontext.cpp @@ -106,7 +106,7 @@ EMSCRIPTEN_WEBGL_CONTEXT_HANDLE QWasmOpenGLContext::createEmscriptenContext(cons attributes.depth = useDepthStencil; attributes.stencil = useDepthStencil; - EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context(canvasId.toLocal8Bit().constData(), &attributes); + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context(canvasId.toUtf8().constData(), &attributes); return context; } diff --git a/src/plugins/platforms/wasm/qwasmscreen.cpp b/src/plugins/platforms/wasm/qwasmscreen.cpp index 37f1ea832a..fe44fdb096 100644 --- a/src/plugins/platforms/wasm/qwasmscreen.cpp +++ b/src/plugins/platforms/wasm/qwasmscreen.cpp @@ -44,6 +44,7 @@ #include #include +using namespace emscripten; QT_BEGIN_NAMESPACE @@ -182,13 +183,15 @@ void QWasmScreen::updateQScreenAndCanvasRenderSize() QSizeF cssSize(css_width, css_height); QSizeF canvasSize = cssSize * devicePixelRatio(); - emscripten::val canvas = emscripten::val::global(canvasId.constData()); + val document = val::global("document"); + val canvas = document.call("getElementById", val(canvasId.constData())); + canvas.set("width", canvasSize.width()); canvas.set("height", canvasSize.height()); QPoint offset; - offset.setX(emscripten::val::global(canvasId.constData())["offsetTop"].as()); - offset.setY(emscripten::val::global(canvasId.constData())["offsetLeft"].as()); + offset.setX(canvas["offsetTop"].as()); + offset.setY(canvas["offsetLeft"].as()); emscripten::val rect = canvas.call("getBoundingClientRect"); QPoint position(rect["left"].as() - offset.x(), rect["top"].as() - offset.y()); -- cgit v1.2.3 From 2f366a63b20a943ae3099605c2cdb34009ca5602 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 13 Jan 2020 15:58:14 +0100 Subject: Windows QPA: Fix message box and other system sounds Re-add the code hooking into QWindowsUiaAccessibility::notifyAccessibilityUpdate() which was removed by 0cf6297c15be45d852be98c862bd0211e6de1aa2. Fixes: QTBUG-81342 Change-Id: Ie97d7cca5b774196d53b675c92d84f4ce208f987 Reviewed-by: Oliver Wolff --- .../uiautomation/qwindowsuiaaccessibility.cpp | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'src/plugins/platforms') diff --git a/src/plugins/platforms/windows/uiautomation/qwindowsuiaaccessibility.cpp b/src/plugins/platforms/windows/uiautomation/qwindowsuiaaccessibility.cpp index c7c0deab3f..32a57473ad 100644 --- a/src/plugins/platforms/windows/uiautomation/qwindowsuiaaccessibility.cpp +++ b/src/plugins/platforms/windows/uiautomation/qwindowsuiaaccessibility.cpp @@ -52,6 +52,8 @@ #include #include +#include + QT_BEGIN_NAMESPACE using namespace QWindowsUiAutomation; @@ -85,12 +87,63 @@ bool QWindowsUiaAccessibility::handleWmGetObject(HWND hwnd, WPARAM wParam, LPARA return false; } +// Retrieve sound name by checking the icon property of a message box +// should it be the event object. +static QString alertSound(const QObject *object) +{ + if (object->inherits("QMessageBox")) { + enum MessageBoxIcon { // Keep in sync with QMessageBox::Icon + Information = 1, + Warning = 2, + Critical = 3 + }; + switch (object->property("icon").toInt()) { + case Information: + return QStringLiteral("SystemAsterisk"); + case Warning: + return QStringLiteral("SystemExclamation"); + case Critical: + return QStringLiteral("SystemHand"); + } + } + return QStringLiteral("SystemAsterisk"); +} + +static QString soundFileName(const QString &soundName) +{ + const QString key = QStringLiteral("AppEvents\\Schemes\\Apps\\.Default\\") + + soundName + QStringLiteral("\\.Current"); + return QWinRegistryKey(HKEY_CURRENT_USER, key).stringValue(L""); +} + +static void playSystemSound(const QString &soundName) +{ + if (!soundName.isEmpty() && !soundFileName(soundName).isEmpty()) { + PlaySound(reinterpret_cast(soundName.utf16()), nullptr, + SND_ALIAS | SND_ASYNC | SND_NODEFAULT | SND_NOWAIT); + } +} + // Handles accessibility update notifications. void QWindowsUiaAccessibility::notifyAccessibilityUpdate(QAccessibleEvent *event) { if (!event) return; + switch (event->type()) { + case QAccessible::PopupMenuStart: + playSystemSound(QStringLiteral("MenuPopup")); + break; + case QAccessible::MenuCommand: + playSystemSound(QStringLiteral("MenuCommand")); + break; + case QAccessible::Alert: + playSystemSound(alertSound(event->object())); + break; + default: + break; + } + QAccessibleInterface *accessible = event->accessibleInterface(); if (!isActive() || !accessible || !accessible->isValid()) return; -- cgit v1.2.3 From 01d24eea09e1312e9fa7eee98e98ce22ed504aba Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 14 Jan 2020 09:26:46 +0100 Subject: Windows QPA: Fix co-existence of several Qt versions in an application Change qtbase/ef54abae43db79792b40dfdca30ac0fa1b582354 added a new dummy message window for power notification. This causes the static class name conflict check to assume there is no conflict since it does not exist in previous Qt versions. Change it to perform the for each class name. Fixes: QTBUG-81347 Change-Id: I290806d021ac7de130a41e996d03b8fb4eb2c437 Reviewed-by: Oliver Wolff --- src/plugins/platforms/windows/qwindowscontext.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'src/plugins/platforms') diff --git a/src/plugins/platforms/windows/qwindowscontext.cpp b/src/plugins/platforms/windows/qwindowscontext.cpp index a2dd25f8cc..d31352b854 100644 --- a/src/plugins/platforms/windows/qwindowscontext.cpp +++ b/src/plugins/platforms/windows/qwindowscontext.cpp @@ -604,15 +604,12 @@ QString QWindowsContext::registerWindowClass(QString cname, // each one has to have window class names with a unique name // The first instance gets the unmodified name; if the class // has already been registered by another instance of Qt then - // add a UUID. - static int classExists = -1; - + // add a UUID. The check needs to be performed for each name + // in case new message windows are added (QTBUG-81347). const auto appInstance = static_cast(GetModuleHandle(nullptr)); - if (classExists == -1) { - WNDCLASS wcinfo; - classExists = GetClassInfo(appInstance, reinterpret_cast(cname.utf16()), &wcinfo); - classExists = classExists && wcinfo.lpfnWndProc != proc; - } + WNDCLASS wcinfo; + const bool classExists = GetClassInfo(appInstance, reinterpret_cast(cname.utf16()), &wcinfo) == TRUE + && wcinfo.lpfnWndProc != proc; if (classExists) cname += QUuid::createUuid().toString(); -- cgit v1.2.3 From 8298118c527ae427ea2e8718122e1a65f4bb8bbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 7 Jan 2020 16:18:10 +0100 Subject: macOS: Move palette setup into platform theme MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The theme was the only client, so there's no point in keeping it separate from its only call site. Change-Id: I4783c5db6975ad2daaede704ab5855c57f190344 Reviewed-by: Morten Johan Sørvig --- src/plugins/platforms/cocoa/cocoa.pro | 2 - src/plugins/platforms/cocoa/qcocoasystemsettings.h | 54 ----- .../platforms/cocoa/qcocoasystemsettings.mm | 245 --------------------- src/plugins/platforms/cocoa/qcocoatheme.mm | 196 ++++++++++++++++- 4 files changed, 195 insertions(+), 302 deletions(-) delete mode 100644 src/plugins/platforms/cocoa/qcocoasystemsettings.h delete mode 100644 src/plugins/platforms/cocoa/qcocoasystemsettings.mm (limited to 'src/plugins/platforms') diff --git a/src/plugins/platforms/cocoa/cocoa.pro b/src/plugins/platforms/cocoa/cocoa.pro index 4cf9e64447..6645b6c90a 100644 --- a/src/plugins/platforms/cocoa/cocoa.pro +++ b/src/plugins/platforms/cocoa/cocoa.pro @@ -25,7 +25,6 @@ SOURCES += main.mm \ qcocoaclipboard.mm \ qcocoadrag.mm \ qmacclipboard.mm \ - qcocoasystemsettings.mm \ qcocoainputcontext.mm \ qcocoaservices.mm \ qcocoasystemtrayicon.mm \ @@ -59,7 +58,6 @@ HEADERS += qcocoaintegration.h \ qcocoaclipboard.h \ qcocoadrag.h \ qmacclipboard.h \ - qcocoasystemsettings.h \ qcocoainputcontext.h \ qcocoaservices.h \ qcocoasystemtrayicon.h \ diff --git a/src/plugins/platforms/cocoa/qcocoasystemsettings.h b/src/plugins/platforms/cocoa/qcocoasystemsettings.h deleted file mode 100644 index cf5688bbed..0000000000 --- a/src/plugins/platforms/cocoa/qcocoasystemsettings.h +++ /dev/null @@ -1,54 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QCOCOASYSTEMSETTINGS_H -#define QCOCOASYSTEMSETTINGS_H - -#include -#include -#include - -QT_BEGIN_NAMESPACE - -QPalette * qt_mac_createSystemPalette(); -QHash qt_mac_createRolePalettes(); - -QT_END_NAMESPACE - -#endif diff --git a/src/plugins/platforms/cocoa/qcocoasystemsettings.mm b/src/plugins/platforms/cocoa/qcocoasystemsettings.mm deleted file mode 100644 index cb25bd7d81..0000000000 --- a/src/plugins/platforms/cocoa/qcocoasystemsettings.mm +++ /dev/null @@ -1,245 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2019 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qcocoasystemsettings.h" - -#include "qcocoahelpers.h" - -#include -#include -#include - -#if !QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_14) -@interface NSColor (MojaveForwardDeclarations) -@property (class, strong, readonly) NSColor *selectedContentBackgroundColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSColor *unemphasizedSelectedTextBackgroundColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSColor *unemphasizedSelectedTextColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSColor *unemphasizedSelectedContentBackgroundColor NS_AVAILABLE_MAC(10_14); -@property (class, strong, readonly) NSArray *alternatingContentBackgroundColors NS_AVAILABLE_MAC(10_14); -// Missing from non-Mojave SDKs, even if introduced in 10.10 -@property (class, strong, readonly) NSColor *linkColor NS_AVAILABLE_MAC(10_10); -@end -#endif - -QT_BEGIN_NAMESPACE - -QPalette * qt_mac_createSystemPalette() -{ - QColor qc; - - // Standard palette initialization (copied from Qt 4 styles) - QBrush backgroundBrush = qt_mac_toQBrush([NSColor windowBackgroundColor]); - QColor background = backgroundBrush.color(); - QColor light(background.lighter(110)); - QColor dark(background.darker(160)); - QColor mid(background.darker(140)); - QPalette *palette = new QPalette(Qt::black, background, light, dark, mid, Qt::black, Qt::white); - - palette->setBrush(QPalette::Window, backgroundBrush); - - palette->setBrush(QPalette::Disabled, QPalette::WindowText, dark); - palette->setBrush(QPalette::Disabled, QPalette::Text, dark); - palette->setBrush(QPalette::Disabled, QPalette::ButtonText, dark); - palette->setBrush(QPalette::Disabled, QPalette::Base, backgroundBrush); - QBrush textBackgroundBrush = qt_mac_toQBrush([NSColor textBackgroundColor]); - palette->setBrush(QPalette::Active, QPalette::Base, textBackgroundBrush); - palette->setBrush(QPalette::Inactive, QPalette::Base, textBackgroundBrush); - palette->setColor(QPalette::Disabled, QPalette::Dark, QColor(191, 191, 191)); - palette->setColor(QPalette::Active, QPalette::Dark, QColor(191, 191, 191)); - palette->setColor(QPalette::Inactive, QPalette::Dark, QColor(191, 191, 191)); - - // System palette initialization: - QBrush br = qt_mac_toQBrush([NSColor selectedControlColor]); - palette->setBrush(QPalette::Active, QPalette::Highlight, br); - if (__builtin_available(macOS 10.14, *)) { - const auto inactiveHighlight = qt_mac_toQBrush([NSColor unemphasizedSelectedContentBackgroundColor]); - palette->setBrush(QPalette::Inactive, QPalette::Highlight, inactiveHighlight); - palette->setBrush(QPalette::Disabled, QPalette::Highlight, inactiveHighlight); - } else { - palette->setBrush(QPalette::Inactive, QPalette::Highlight, br); - palette->setBrush(QPalette::Disabled, QPalette::Highlight, br); - } - - palette->setBrush(QPalette::Shadow, qt_mac_toQColor([NSColor shadowColor])); - - qc = qt_mac_toQColor([NSColor controlTextColor]); - palette->setColor(QPalette::Active, QPalette::Text, qc); - palette->setColor(QPalette::Active, QPalette::WindowText, qc); - palette->setColor(QPalette::Active, QPalette::HighlightedText, qc); - palette->setColor(QPalette::Inactive, QPalette::Text, qc); - palette->setColor(QPalette::Inactive, QPalette::WindowText, qc); - palette->setColor(QPalette::Inactive, QPalette::HighlightedText, qc); - - qc = qt_mac_toQColor([NSColor disabledControlTextColor]); - palette->setColor(QPalette::Disabled, QPalette::Text, qc); - palette->setColor(QPalette::Disabled, QPalette::WindowText, qc); - palette->setColor(QPalette::Disabled, QPalette::HighlightedText, qc); - - palette->setBrush(QPalette::ToolTipBase, qt_mac_toQBrush([NSColor controlColor])); - - palette->setColor(QPalette::Normal, QPalette::Link, qt_mac_toQColor([NSColor linkColor])); - - return palette; -} - -struct QMacPaletteMap { - inline QMacPaletteMap(QPlatformTheme::Palette p, NSColor *a, NSColor *i) : - active(a), inactive(i), paletteRole(p) { } - - NSColor *active; - NSColor *inactive; - QPlatformTheme::Palette paletteRole; -}; - -#define MAC_PALETTE_ENTRY(pal, active, inactive) \ - QMacPaletteMap(pal, [NSColor active], [NSColor inactive]) -static QMacPaletteMap mac_widget_colors[] = { - MAC_PALETTE_ENTRY(QPlatformTheme::ToolButtonPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::ButtonPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::HeaderPalette, headerTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::ComboBoxPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::ItemViewPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::MessageBoxLabelPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::TabBarPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::LabelPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::GroupBoxPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::MenuPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::MenuBarPalette, controlTextColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::TextEditPalette, textColor, disabledControlTextColor), - MAC_PALETTE_ENTRY(QPlatformTheme::TextLineEditPalette, textColor, disabledControlTextColor) -}; -#undef MAC_PALETTE_ENTRY - -static const int mac_widget_colors_count = sizeof(mac_widget_colors) / sizeof(mac_widget_colors[0]); - -QHash qt_mac_createRolePalettes() -{ - QHash palettes; - QColor qc; - for (int i = 0; i < mac_widget_colors_count; i++) { - QPalette &pal = *qt_mac_createSystemPalette(); - if (mac_widget_colors[i].active) { - qc = qt_mac_toQColor(mac_widget_colors[i].active); - pal.setColor(QPalette::Active, QPalette::Text, qc); - pal.setColor(QPalette::Inactive, QPalette::Text, qc); - pal.setColor(QPalette::Active, QPalette::WindowText, qc); - pal.setColor(QPalette::Inactive, QPalette::WindowText, qc); - pal.setColor(QPalette::Active, QPalette::HighlightedText, qc); - pal.setColor(QPalette::Inactive, QPalette::HighlightedText, qc); - pal.setColor(QPalette::Active, QPalette::ButtonText, qc); - pal.setColor(QPalette::Inactive, QPalette::ButtonText, qc); - qc = qt_mac_toQColor(mac_widget_colors[i].inactive); - pal.setColor(QPalette::Disabled, QPalette::Text, qc); - pal.setColor(QPalette::Disabled, QPalette::WindowText, qc); - pal.setColor(QPalette::Disabled, QPalette::HighlightedText, qc); - pal.setColor(QPalette::Disabled, QPalette::ButtonText, qc); - } - if (mac_widget_colors[i].paletteRole == QPlatformTheme::MenuPalette - || mac_widget_colors[i].paletteRole == QPlatformTheme::MenuBarPalette) { - NSColor *selectedMenuItemColor = nil; - if (__builtin_available(macOS 10.14, *)) { - // Cheap approximation for NSVisualEffectView (see deprecation note for selectedMenuItemTextColor) - selectedMenuItemColor = [[NSColor selectedContentBackgroundColor] highlightWithLevel:0.4]; - } else { - // selectedMenuItemColor would presumably be the correct color to use as the background - // for selected menu items. But that color is always blue, and doesn't follow the - // appearance color in system preferences. So we therefore deliberatly choose to use - // keyboardFocusIndicatorColor instead, which appears to have the same color value. - selectedMenuItemColor = [NSColor keyboardFocusIndicatorColor]; - } - pal.setBrush(QPalette::Highlight, qt_mac_toQColor(selectedMenuItemColor)); - qc = qt_mac_toQColor([NSColor labelColor]); - pal.setBrush(QPalette::ButtonText, qc); - pal.setBrush(QPalette::Text, qc); - qc = qt_mac_toQColor([NSColor selectedMenuItemTextColor]); - pal.setBrush(QPalette::HighlightedText, qc); - qc = qt_mac_toQColor([NSColor disabledControlTextColor]); - pal.setBrush(QPalette::Disabled, QPalette::Text, qc); - } else if ((mac_widget_colors[i].paletteRole == QPlatformTheme::ButtonPalette) - || (mac_widget_colors[i].paletteRole == QPlatformTheme::HeaderPalette) - || (mac_widget_colors[i].paletteRole == QPlatformTheme::TabBarPalette)) { - pal.setColor(QPalette::Disabled, QPalette::ButtonText, - pal.color(QPalette::Disabled, QPalette::Text)); - pal.setColor(QPalette::Inactive, QPalette::ButtonText, - pal.color(QPalette::Inactive, QPalette::Text)); - pal.setColor(QPalette::Active, QPalette::ButtonText, - pal.color(QPalette::Active, QPalette::Text)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::ItemViewPalette) { - NSArray *baseColors = nil; - NSColor *activeHighlightColor = nil; - if (__builtin_available(macOS 10.14, *)) { - baseColors = [NSColor alternatingContentBackgroundColors]; - activeHighlightColor = [NSColor selectedContentBackgroundColor]; - pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, - qt_mac_toQBrush([NSColor unemphasizedSelectedTextColor])); - } else { - baseColors = [NSColor controlAlternatingRowBackgroundColors]; - activeHighlightColor = [NSColor alternateSelectedControlColor]; - pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, - pal.brush(QPalette::Active, QPalette::Text)); - } - pal.setBrush(QPalette::Base, qt_mac_toQBrush(baseColors[0])); - pal.setBrush(QPalette::AlternateBase, qt_mac_toQBrush(baseColors[1])); - pal.setBrush(QPalette::Active, QPalette::Highlight, - qt_mac_toQBrush(activeHighlightColor)); - pal.setBrush(QPalette::Active, QPalette::HighlightedText, - qt_mac_toQBrush([NSColor alternateSelectedControlTextColor])); - pal.setBrush(QPalette::Inactive, QPalette::Text, - pal.brush(QPalette::Active, QPalette::Text)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::TextEditPalette) { - pal.setBrush(QPalette::Active, QPalette::Base, qt_mac_toQColor([NSColor textBackgroundColor])); - pal.setBrush(QPalette::Inactive, QPalette::Text, - pal.brush(QPalette::Active, QPalette::Text)); - pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, - pal.brush(QPalette::Active, QPalette::Text)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::TextLineEditPalette - || mac_widget_colors[i].paletteRole == QPlatformTheme::ComboBoxPalette) { - pal.setBrush(QPalette::Active, QPalette::Base, qt_mac_toQColor([NSColor textBackgroundColor])); - pal.setBrush(QPalette::Disabled, QPalette::Base, - pal.brush(QPalette::Active, QPalette::Base)); - } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::LabelPalette) { - qc = qt_mac_toQColor([NSColor labelColor]); - pal.setBrush(QPalette::Inactive, QPalette::ToolTipText, qc); - } - palettes.insert(mac_widget_colors[i].paletteRole, &pal); - } - return palettes; -} - -QT_END_NAMESPACE diff --git a/src/plugins/platforms/cocoa/qcocoatheme.mm b/src/plugins/platforms/cocoa/qcocoatheme.mm index 387df65721..a76ba300e9 100644 --- a/src/plugins/platforms/cocoa/qcocoatheme.mm +++ b/src/plugins/platforms/cocoa/qcocoatheme.mm @@ -45,7 +45,6 @@ #include #include -#include "qcocoasystemsettings.h" #include "qcocoasystemtrayicon.h" #include "qcocoamenuitem.h" #include "qcocoamenu.h" @@ -80,8 +79,203 @@ #include +#if !QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_14) +@interface NSColor (MojaveForwardDeclarations) +@property (class, strong, readonly) NSColor *selectedContentBackgroundColor NS_AVAILABLE_MAC(10_14); +@property (class, strong, readonly) NSColor *unemphasizedSelectedTextBackgroundColor NS_AVAILABLE_MAC(10_14); +@property (class, strong, readonly) NSColor *unemphasizedSelectedTextColor NS_AVAILABLE_MAC(10_14); +@property (class, strong, readonly) NSColor *unemphasizedSelectedContentBackgroundColor NS_AVAILABLE_MAC(10_14); +@property (class, strong, readonly) NSArray *alternatingContentBackgroundColors NS_AVAILABLE_MAC(10_14); +// Missing from non-Mojave SDKs, even if introduced in 10.10 +@property (class, strong, readonly) NSColor *linkColor NS_AVAILABLE_MAC(10_10); +@end +#endif + QT_BEGIN_NAMESPACE +static QPalette *qt_mac_createSystemPalette() +{ + QColor qc; + + // Standard palette initialization (copied from Qt 4 styles) + QBrush backgroundBrush = qt_mac_toQBrush([NSColor windowBackgroundColor]); + QColor background = backgroundBrush.color(); + QColor light(background.lighter(110)); + QColor dark(background.darker(160)); + QColor mid(background.darker(140)); + QPalette *palette = new QPalette(Qt::black, background, light, dark, mid, Qt::black, Qt::white); + + palette->setBrush(QPalette::Window, backgroundBrush); + + palette->setBrush(QPalette::Disabled, QPalette::WindowText, dark); + palette->setBrush(QPalette::Disabled, QPalette::Text, dark); + palette->setBrush(QPalette::Disabled, QPalette::ButtonText, dark); + palette->setBrush(QPalette::Disabled, QPalette::Base, backgroundBrush); + QBrush textBackgroundBrush = qt_mac_toQBrush([NSColor textBackgroundColor]); + palette->setBrush(QPalette::Active, QPalette::Base, textBackgroundBrush); + palette->setBrush(QPalette::Inactive, QPalette::Base, textBackgroundBrush); + palette->setColor(QPalette::Disabled, QPalette::Dark, QColor(191, 191, 191)); + palette->setColor(QPalette::Active, QPalette::Dark, QColor(191, 191, 191)); + palette->setColor(QPalette::Inactive, QPalette::Dark, QColor(191, 191, 191)); + + // System palette initialization: + QBrush br = qt_mac_toQBrush([NSColor selectedControlColor]); + palette->setBrush(QPalette::Active, QPalette::Highlight, br); + if (__builtin_available(macOS 10.14, *)) { + const auto inactiveHighlight = qt_mac_toQBrush([NSColor unemphasizedSelectedContentBackgroundColor]); + palette->setBrush(QPalette::Inactive, QPalette::Highlight, inactiveHighlight); + palette->setBrush(QPalette::Disabled, QPalette::Highlight, inactiveHighlight); + } else { + palette->setBrush(QPalette::Inactive, QPalette::Highlight, br); + palette->setBrush(QPalette::Disabled, QPalette::Highlight, br); + } + + palette->setBrush(QPalette::Shadow, qt_mac_toQColor([NSColor shadowColor])); + + qc = qt_mac_toQColor([NSColor controlTextColor]); + palette->setColor(QPalette::Active, QPalette::Text, qc); + palette->setColor(QPalette::Active, QPalette::WindowText, qc); + palette->setColor(QPalette::Active, QPalette::HighlightedText, qc); + palette->setColor(QPalette::Inactive, QPalette::Text, qc); + palette->setColor(QPalette::Inactive, QPalette::WindowText, qc); + palette->setColor(QPalette::Inactive, QPalette::HighlightedText, qc); + + qc = qt_mac_toQColor([NSColor disabledControlTextColor]); + palette->setColor(QPalette::Disabled, QPalette::Text, qc); + palette->setColor(QPalette::Disabled, QPalette::WindowText, qc); + palette->setColor(QPalette::Disabled, QPalette::HighlightedText, qc); + + palette->setBrush(QPalette::ToolTipBase, qt_mac_toQBrush([NSColor controlColor])); + + palette->setColor(QPalette::Normal, QPalette::Link, qt_mac_toQColor([NSColor linkColor])); + + return palette; +} + +struct QMacPaletteMap { + inline QMacPaletteMap(QPlatformTheme::Palette p, NSColor *a, NSColor *i) : + active(a), inactive(i), paletteRole(p) { } + + NSColor *active; + NSColor *inactive; + QPlatformTheme::Palette paletteRole; +}; + +#define MAC_PALETTE_ENTRY(pal, active, inactive) \ + QMacPaletteMap(pal, [NSColor active], [NSColor inactive]) +static QMacPaletteMap mac_widget_colors[] = { + MAC_PALETTE_ENTRY(QPlatformTheme::ToolButtonPalette, controlTextColor, disabledControlTextColor), + MAC_PALETTE_ENTRY(QPlatformTheme::ButtonPalette, controlTextColor, disabledControlTextColor), + MAC_PALETTE_ENTRY(QPlatformTheme::HeaderPalette, headerTextColor, disabledControlTextColor), + MAC_PALETTE_ENTRY(QPlatformTheme::ComboBoxPalette, controlTextColor, disabledControlTextColor), + MAC_PALETTE_ENTRY(QPlatformTheme::ItemViewPalette, textColor, disabledControlTextColor), + MAC_PALETTE_ENTRY(QPlatformTheme::MessageBoxLabelPalette, textColor, disabledControlTextColor), + MAC_PALETTE_ENTRY(QPlatformTheme::TabBarPalette, controlTextColor, disabledControlTextColor), + MAC_PALETTE_ENTRY(QPlatformTheme::LabelPalette, textColor, disabledControlTextColor), + MAC_PALETTE_ENTRY(QPlatformTheme::GroupBoxPalette, textColor, disabledControlTextColor), + MAC_PALETTE_ENTRY(QPlatformTheme::MenuPalette, controlTextColor, disabledControlTextColor), + MAC_PALETTE_ENTRY(QPlatformTheme::MenuBarPalette, controlTextColor, disabledControlTextColor), + MAC_PALETTE_ENTRY(QPlatformTheme::TextEditPalette, textColor, disabledControlTextColor), + MAC_PALETTE_ENTRY(QPlatformTheme::TextLineEditPalette, textColor, disabledControlTextColor) +}; +#undef MAC_PALETTE_ENTRY + +static const int mac_widget_colors_count = sizeof(mac_widget_colors) / sizeof(mac_widget_colors[0]); + +static QHash qt_mac_createRolePalettes() +{ + QHash palettes; + QColor qc; + for (int i = 0; i < mac_widget_colors_count; i++) { + QPalette &pal = *qt_mac_createSystemPalette(); + if (mac_widget_colors[i].active) { + qc = qt_mac_toQColor(mac_widget_colors[i].active); + pal.setColor(QPalette::Active, QPalette::Text, qc); + pal.setColor(QPalette::Inactive, QPalette::Text, qc); + pal.setColor(QPalette::Active, QPalette::WindowText, qc); + pal.setColor(QPalette::Inactive, QPalette::WindowText, qc); + pal.setColor(QPalette::Active, QPalette::HighlightedText, qc); + pal.setColor(QPalette::Inactive, QPalette::HighlightedText, qc); + pal.setColor(QPalette::Active, QPalette::ButtonText, qc); + pal.setColor(QPalette::Inactive, QPalette::ButtonText, qc); + qc = qt_mac_toQColor(mac_widget_colors[i].inactive); + pal.setColor(QPalette::Disabled, QPalette::Text, qc); + pal.setColor(QPalette::Disabled, QPalette::WindowText, qc); + pal.setColor(QPalette::Disabled, QPalette::HighlightedText, qc); + pal.setColor(QPalette::Disabled, QPalette::ButtonText, qc); + } + if (mac_widget_colors[i].paletteRole == QPlatformTheme::MenuPalette + || mac_widget_colors[i].paletteRole == QPlatformTheme::MenuBarPalette) { + NSColor *selectedMenuItemColor = nil; + if (__builtin_available(macOS 10.14, *)) { + // Cheap approximation for NSVisualEffectView (see deprecation note for selectedMenuItemTextColor) + selectedMenuItemColor = [[NSColor selectedContentBackgroundColor] highlightWithLevel:0.4]; + } else { + // selectedMenuItemColor would presumably be the correct color to use as the background + // for selected menu items. But that color is always blue, and doesn't follow the + // appearance color in system preferences. So we therefore deliberatly choose to use + // keyboardFocusIndicatorColor instead, which appears to have the same color value. + selectedMenuItemColor = [NSColor keyboardFocusIndicatorColor]; + } + pal.setBrush(QPalette::Highlight, qt_mac_toQColor(selectedMenuItemColor)); + qc = qt_mac_toQColor([NSColor labelColor]); + pal.setBrush(QPalette::ButtonText, qc); + pal.setBrush(QPalette::Text, qc); + qc = qt_mac_toQColor([NSColor selectedMenuItemTextColor]); + pal.setBrush(QPalette::HighlightedText, qc); + qc = qt_mac_toQColor([NSColor disabledControlTextColor]); + pal.setBrush(QPalette::Disabled, QPalette::Text, qc); + } else if ((mac_widget_colors[i].paletteRole == QPlatformTheme::ButtonPalette) + || (mac_widget_colors[i].paletteRole == QPlatformTheme::HeaderPalette) + || (mac_widget_colors[i].paletteRole == QPlatformTheme::TabBarPalette)) { + pal.setColor(QPalette::Disabled, QPalette::ButtonText, + pal.color(QPalette::Disabled, QPalette::Text)); + pal.setColor(QPalette::Inactive, QPalette::ButtonText, + pal.color(QPalette::Inactive, QPalette::Text)); + pal.setColor(QPalette::Active, QPalette::ButtonText, + pal.color(QPalette::Active, QPalette::Text)); + } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::ItemViewPalette) { + NSArray *baseColors = nil; + NSColor *activeHighlightColor = nil; + if (__builtin_available(macOS 10.14, *)) { + baseColors = [NSColor alternatingContentBackgroundColors]; + activeHighlightColor = [NSColor selectedContentBackgroundColor]; + pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, + qt_mac_toQBrush([NSColor unemphasizedSelectedTextColor])); + } else { + baseColors = [NSColor controlAlternatingRowBackgroundColors]; + activeHighlightColor = [NSColor alternateSelectedControlColor]; + pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, + pal.brush(QPalette::Active, QPalette::Text)); + } + pal.setBrush(QPalette::Base, qt_mac_toQBrush(baseColors[0])); + pal.setBrush(QPalette::AlternateBase, qt_mac_toQBrush(baseColors[1])); + pal.setBrush(QPalette::Active, QPalette::Highlight, + qt_mac_toQBrush(activeHighlightColor)); + pal.setBrush(QPalette::Active, QPalette::HighlightedText, + qt_mac_toQBrush([NSColor alternateSelectedControlTextColor])); + pal.setBrush(QPalette::Inactive, QPalette::Text, + pal.brush(QPalette::Active, QPalette::Text)); + } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::TextEditPalette) { + pal.setBrush(QPalette::Active, QPalette::Base, qt_mac_toQColor([NSColor textBackgroundColor])); + pal.setBrush(QPalette::Inactive, QPalette::Text, + pal.brush(QPalette::Active, QPalette::Text)); + pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, + pal.brush(QPalette::Active, QPalette::Text)); + } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::TextLineEditPalette + || mac_widget_colors[i].paletteRole == QPlatformTheme::ComboBoxPalette) { + pal.setBrush(QPalette::Active, QPalette::Base, qt_mac_toQColor([NSColor textBackgroundColor])); + pal.setBrush(QPalette::Disabled, QPalette::Base, + pal.brush(QPalette::Active, QPalette::Base)); + } else if (mac_widget_colors[i].paletteRole == QPlatformTheme::LabelPalette) { + qc = qt_mac_toQColor([NSColor labelColor]); + pal.setBrush(QPalette::Inactive, QPalette::ToolTipText, qc); + } + palettes.insert(mac_widget_colors[i].paletteRole, &pal); + } + return palettes; +} + const char *QCocoaTheme::name = "cocoa"; QCocoaTheme::QCocoaTheme() -- cgit v1.2.3