improve macos code [1/2]

1. Simplify code a little
2. Use Qt functionalities as many as possible.
3. Prepare for adding old Qt support.

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-09-05 16:55:55 +08:00
parent b9f65aa783
commit f2a6d35908
6 changed files with 37 additions and 22 deletions

View File

@ -108,7 +108,7 @@ QT_END_NAMESPACE
#endif #endif
#if (QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 4, 0))
using namespace Qt::Literals::StringLiterals; using namespace Qt::StringLiterals;
#endif #endif
#ifndef FRAMELESSHELPER_BYTEARRAY_LITERAL #ifndef FRAMELESSHELPER_BYTEARRAY_LITERAL

View File

@ -68,6 +68,7 @@ FRAMELESSHELPER_CORE_API void moveWindowToDesktopCenter(
[[nodiscard]] FRAMELESSHELPER_CORE_API QString getWallpaperFilePath(); [[nodiscard]] FRAMELESSHELPER_CORE_API QString getWallpaperFilePath();
[[nodiscard]] FRAMELESSHELPER_CORE_API Global::WallpaperAspectStyle getWallpaperAspectStyle(); [[nodiscard]] FRAMELESSHELPER_CORE_API Global::WallpaperAspectStyle getWallpaperAspectStyle();
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isBlurBehindWindowSupported(); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isBlurBehindWindowSupported();
FRAMELESSHELPER_CORE_API void registerThemeChangeNotification();
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowsVersionOrGreater(const Global::WindowsVersion version); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowsVersionOrGreater(const Global::WindowsVersion version);
@ -121,7 +122,6 @@ FRAMELESSHELPER_CORE_API void setQtDarkModeAwareEnabled(const bool enable, const
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
[[nodiscard]] FRAMELESSHELPER_CORE_API bool shouldAppsUseDarkMode_linux(); [[nodiscard]] FRAMELESSHELPER_CORE_API bool shouldAppsUseDarkMode_linux();
[[nodiscard]] FRAMELESSHELPER_CORE_API QColor getWmThemeColor(); [[nodiscard]] FRAMELESSHELPER_CORE_API QColor getWmThemeColor();
FRAMELESSHELPER_CORE_API void registerThemeChangeNotification();
#endif // Q_OS_LINUX #endif // Q_OS_LINUX
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS

View File

@ -66,16 +66,6 @@
# endif # endif
#endif #endif
#ifdef Q_OS_LINUX
static constexpr const char QT_QPA_ENV_VAR[] = "QT_QPA_PLATFORM";
FRAMELESSHELPER_BYTEARRAY_CONSTANT(xcb)
#endif
#ifdef Q_OS_MACOS
static constexpr const char MAC_LAYER_ENV_VAR[] = "QT_MAC_WANTS_LAYER";
FRAMELESSHELPER_BYTEARRAY_CONSTANT2(ValueOne, "1")
#endif
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
Q_LOGGING_CATEGORY(lcCoreGlobal, "wangwenx190.framelesshelper.core.global") Q_LOGGING_CATEGORY(lcCoreGlobal, "wangwenx190.framelesshelper.core.global")
@ -86,6 +76,15 @@ Q_LOGGING_CATEGORY(lcCoreGlobal, "wangwenx190.framelesshelper.core.global")
using namespace Global; using namespace Global;
#ifdef Q_OS_LINUX
[[maybe_unused]] static constexpr const char QT_QPA_ENV_VAR[] = "QT_QPA_PLATFORM";
FRAMELESSHELPER_BYTEARRAY_CONSTANT(xcb)
#endif
#ifdef Q_OS_MACOS
[[maybe_unused]] static constexpr const char MAC_LAYER_ENV_VAR[] = "QT_MAC_WANTS_LAYER";
#endif
struct CoreData struct CoreData
{ {
QMutex mutex; QMutex mutex;
@ -115,10 +114,8 @@ void initialize()
qputenv(QT_QPA_ENV_VAR, kxcb); qputenv(QT_QPA_ENV_VAR, kxcb);
#endif #endif
#ifdef Q_OS_MACOS #if (defined(Q_OS_MACOS) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)))
// This has become the default setting since some unknown Qt version, qputenv(MAC_LAYER_ENV_VAR, FRAMELESSHELPER_BYTEARRAY_LITERAL("1"));
// check whether we can remove this hack safely or not.
qputenv(MAC_LAYER_ENV_VAR, kValueOne);
#endif #endif
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
@ -276,9 +273,6 @@ void registerUninitializeHook(const UninitializeHookCallback &cb)
void setApplicationOSThemeAware(const bool enable, const bool pureQuick) void setApplicationOSThemeAware(const bool enable, const bool pureQuick)
{ {
Q_UNUSED(enable);
Q_UNUSED(pureQuick);
static bool set = false; static bool set = false;
if (set) { if (set) {
return; return;
@ -287,9 +281,13 @@ void setApplicationOSThemeAware(const bool enable, const bool pureQuick)
#if (defined(Q_OS_WINDOWS) && (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))) #if (defined(Q_OS_WINDOWS) && (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)))
Utils::setQtDarkModeAwareEnabled(enable, pureQuick); Utils::setQtDarkModeAwareEnabled(enable, pureQuick);
#else
Q_UNUSED(enable);
Q_UNUSED(pureQuick);
#endif #endif
#if (defined(Q_OS_LINUX) && (QT_VERSION < QT_VERSION_CHECK(6, 4, 0))) #if ((defined(Q_OS_LINUX) && (QT_VERSION < QT_VERSION_CHECK(6, 4, 0))) || \
(defined(Q_OS_MACOS) && (QT_VERSION < QT_VERSION_CHECK(5, 12, 0))))
Utils::registerThemeChangeNotification(); Utils::registerThemeChangeNotification();
#endif #endif
} }

View File

@ -219,7 +219,7 @@ bool Utils::isThemeChangeEvent(const QEvent * const event)
if (!event) { if (!event) {
return false; return false;
} }
// QGuiApplication will only deliver theme change events to top level QWindow(QQuickWindow)s, // QGuiApplication will only deliver theme change events to top level Q(Quick)Windows,
// QWidgets won't get such notifications, no matter whether it's top level widget or not. // QWidgets won't get such notifications, no matter whether it's top level widget or not.
// QEvent::ThemeChange: Send by the Windows QPA. // QEvent::ThemeChange: Send by the Windows QPA.
// QEvent::ApplicationPaletteChange: All other platforms (Linux & macOS). // QEvent::ApplicationPaletteChange: All other platforms (Linux & macOS).

View File

@ -39,7 +39,8 @@
#include <AppKit/AppKit.h> #include <AppKit/AppKit.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
[[nodiscard]] Q_GUI_EXPORT QColor qt_mac_toQColor(const NSColor *color); [[nodiscard]] Q_CORE_EXPORT bool qt_mac_applicationIsInDarkMode(); // Since 5.12
[[nodiscard]] Q_GUI_EXPORT QColor qt_mac_toQColor(const NSColor *color); // Since 5.8
QT_END_NAMESPACE QT_END_NAMESPACE
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
@ -499,9 +500,13 @@ bool Utils::isTitleBarColorized()
bool Utils::shouldAppsUseDarkMode_macos() bool Utils::shouldAppsUseDarkMode_macos()
{ {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
return qt_mac_applicationIsInDarkMode();
#else
const auto appearance = [NSApp.effectiveAppearance bestMatchFromAppearancesWithNames: const auto appearance = [NSApp.effectiveAppearance bestMatchFromAppearancesWithNames:
@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]]; @[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]];
return [appearance isEqualToString:NSAppearanceNameDarkAqua]; return [appearance isEqualToString:NSAppearanceNameDarkAqua];
#endif
} }
bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode, const QColor &color) bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode, const QColor &color)
@ -573,6 +578,11 @@ bool Utils::isBlurBehindWindowSupported()
return result; return result;
} }
void Utils::registerThemeChangeNotification()
{
// ### TODO
}
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE
#include "utils_mac.moc" #include "utils_mac.moc"

View File

@ -1870,4 +1870,11 @@ void Utils::setQtDarkModeAwareEnabled(const bool enable, const bool pureQuick)
#endif // (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #endif // (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
} }
void Utils::registerThemeChangeNotification()
{
// On Windows we don't need to subscribe to the theme change event
// manually. Windows will send the theme change notification to all
// top level windows by default.
}
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE