From c79f436e2acb0f7652cad536fc73c4488ee99398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Thu, 16 Aug 2018 14:01:17 +0200 Subject: [PATCH] Improve macOS dark mode support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current theme support in Creator works well, also in macOS dark mode. This change makes sure that the native macOS appearance setting (for the Creator process) and the creator theme setting are in sync. This is required to avoid light/dark palette issues for the light Creator themes when the desktop is configured for dark appearance. Add a “DarkUserInterface” flag to the creatortheme files. This flag indicates if the theme is overall dark, with light text on dark background. Make setCreatorTheme() force the standard light aqua appearance for light Creator themes. Change-Id: I1a5c183230ab0e66641fd834df19d7e0ad1b6a53 Reviewed-by: Eike Ziller --- share/qtcreator/themes/dark.creatortheme | 1 + share/qtcreator/themes/default.creatortheme | 1 + share/qtcreator/themes/flat-dark.creatortheme | 1 + .../qtcreator/themes/flat-light.creatortheme | 1 + share/qtcreator/themes/flat.creatortheme | 1 + src/libs/utils/theme/theme.cpp | 11 +++++ src/libs/utils/theme/theme.h | 3 +- src/libs/utils/theme/theme_mac.h | 34 +++++++++++++ src/libs/utils/theme/theme_mac.mm | 48 +++++++++++++++++++ src/libs/utils/utils-lib.pri | 2 + src/libs/utils/utils.qbs | 9 ++++ 11 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/libs/utils/theme/theme_mac.h create mode 100644 src/libs/utils/theme/theme_mac.mm diff --git a/share/qtcreator/themes/dark.creatortheme b/share/qtcreator/themes/dark.creatortheme index 7f8b3cf5c17..edf7217aed0 100644 --- a/share/qtcreator/themes/dark.creatortheme +++ b/share/qtcreator/themes/dark.creatortheme @@ -244,6 +244,7 @@ FlatProjectsMode=true FlatMenuBar=true ToolBarIconShadow=true WindowColorAsBase=false +DarkUserInteface=true [Gradients] DetailsWidgetHeaderGradient\1\color=00000000 diff --git a/share/qtcreator/themes/default.creatortheme b/share/qtcreator/themes/default.creatortheme index d4668363767..7dced12a0a6 100644 --- a/share/qtcreator/themes/default.creatortheme +++ b/share/qtcreator/themes/default.creatortheme @@ -216,6 +216,7 @@ FlatProjectsMode=false FlatMenuBar=false ToolBarIconShadow=true WindowColorAsBase=false +DarkUserInteface=false [Gradients] DetailsWidgetHeaderGradient\1\color=ffffffff diff --git a/share/qtcreator/themes/flat-dark.creatortheme b/share/qtcreator/themes/flat-dark.creatortheme index ad2a0888187..632388bd783 100644 --- a/share/qtcreator/themes/flat-dark.creatortheme +++ b/share/qtcreator/themes/flat-dark.creatortheme @@ -248,6 +248,7 @@ FlatProjectsMode=true FlatMenuBar=true ToolBarIconShadow=true WindowColorAsBase=false +DarkUserInteface=true [Gradients] DetailsWidgetHeaderGradient\1\color=00000000 diff --git a/share/qtcreator/themes/flat-light.creatortheme b/share/qtcreator/themes/flat-light.creatortheme index 1390a3135e0..bd05799c410 100644 --- a/share/qtcreator/themes/flat-light.creatortheme +++ b/share/qtcreator/themes/flat-light.creatortheme @@ -225,6 +225,7 @@ FlatProjectsMode=false FlatMenuBar=false ToolBarIconShadow=false WindowColorAsBase=false +DarkUserInteface=false [Gradients] DetailsWidgetHeaderGradient\1\color=00000000 diff --git a/share/qtcreator/themes/flat.creatortheme b/share/qtcreator/themes/flat.creatortheme index f9cfa7b7f86..cfca28f143d 100644 --- a/share/qtcreator/themes/flat.creatortheme +++ b/share/qtcreator/themes/flat.creatortheme @@ -223,6 +223,7 @@ FlatProjectsMode=false FlatMenuBar=false ToolBarIconShadow=true WindowColorAsBase=false +DarkUserInteface=false [Gradients] DetailsWidgetHeaderGradient\1\color=00000000 diff --git a/src/libs/utils/theme/theme.cpp b/src/libs/utils/theme/theme.cpp index fc3018fc1dd..4d9aeae3565 100644 --- a/src/libs/utils/theme/theme.cpp +++ b/src/libs/utils/theme/theme.cpp @@ -27,6 +27,9 @@ #include "theme_p.h" #include "../algorithm.h" #include "../qtcassert.h" +#ifdef Q_OS_MACOS +#import "theme_mac.h" +#endif #include #include @@ -69,6 +72,14 @@ void setCreatorTheme(Theme *theme) return; delete m_creatorTheme; m_creatorTheme = theme; + +#ifdef Q_OS_MACOS + // Match the native UI theme and palette with the creator + // theme by forcing light aqua for light creator themes. + if (!theme->flag(Theme::DarkUserInteface)) + Internal::forceMacOSLightAquaApperance(); +#endif + setThemeApplicationPalette(); } diff --git a/src/libs/utils/theme/theme.h b/src/libs/utils/theme/theme.h index 49c7c881ad2..7bca48b9ea3 100644 --- a/src/libs/utils/theme/theme.h +++ b/src/libs/utils/theme/theme.h @@ -337,7 +337,8 @@ public: FlatProjectsMode, FlatMenuBar, ToolBarIconShadow, - WindowColorAsBase + WindowColorAsBase, + DarkUserInteface }; Q_INVOKABLE bool flag(Flag f) const; diff --git a/src/libs/utils/theme/theme_mac.h b/src/libs/utils/theme/theme_mac.h new file mode 100644 index 00000000000..55e6d12bc02 --- /dev/null +++ b/src/libs/utils/theme/theme_mac.h @@ -0,0 +1,34 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** 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 General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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-3.0.html. +** +****************************************************************************/ + +#pragma once + +namespace Utils { +namespace Internal { + +void forceMacOSLightAquaApperance(); + +} // Internal +} // Utils diff --git a/src/libs/utils/theme/theme_mac.mm b/src/libs/utils/theme/theme_mac.mm new file mode 100644 index 00000000000..6a940240f12 --- /dev/null +++ b/src/libs/utils/theme/theme_mac.mm @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** 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 General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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-3.0.html. +** +****************************************************************************/ + +#include "theme_mac.h" + +#include + +#include + +#if !QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_14) +@interface NSApplication (MojaveForwardDeclarations) +@property (strong) NSAppearance *appearance NS_AVAILABLE_MAC(10_14); +@end +#endif + +namespace Utils { +namespace Internal { + +void forceMacOSLightAquaApperance() +{ + if (__builtin_available(macOS 10.14, *)) + NSApp.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua]; +} + +} // Internal +} // Utils diff --git a/src/libs/utils/utils-lib.pri b/src/libs/utils/utils-lib.pri index 0cd5f68e123..41d8745baac 100644 --- a/src/libs/utils/utils-lib.pri +++ b/src/libs/utils/utils-lib.pri @@ -270,8 +270,10 @@ RESOURCES += $$PWD/utils.qrc osx { HEADERS += \ + $$PWD/theme/theme_mac.h \ $$PWD/fileutils_mac.h OBJECTIVE_SOURCES += \ + $$PWD/theme/theme_mac.mm \ $$PWD/fileutils_mac.mm \ $$PWD/processhandle_mac.mm LIBS += -framework Foundation -framework AppKit diff --git a/src/libs/utils/utils.qbs b/src/libs/utils/utils.qbs index 599e9972c3f..2ada52c17ee 100644 --- a/src/libs/utils/utils.qbs +++ b/src/libs/utils/utils.qbs @@ -314,6 +314,15 @@ Project { ] } + Group { + name: "Theme_macos" + condition: qbs.targetOS.contains("macos") + prefix: "theme/" + files: [ + "theme_mac.h", "theme_mac.mm", + ] + } + Group { name: "ProcessHandle_macos" condition: qbs.targetOS.contains("macos")