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
#if (QT_VERSION >= QT_VERSION_CHECK(6, 4, 0))
using namespace Qt::Literals::StringLiterals;
using namespace Qt::StringLiterals;
#endif
#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 Global::WallpaperAspectStyle getWallpaperAspectStyle();
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isBlurBehindWindowSupported();
FRAMELESSHELPER_CORE_API void registerThemeChangeNotification();
#ifdef Q_OS_WINDOWS
[[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
[[nodiscard]] FRAMELESSHELPER_CORE_API bool shouldAppsUseDarkMode_linux();
[[nodiscard]] FRAMELESSHELPER_CORE_API QColor getWmThemeColor();
FRAMELESSHELPER_CORE_API void registerThemeChangeNotification();
#endif // Q_OS_LINUX
#ifdef Q_OS_MACOS

View File

@ -66,16 +66,6 @@
# 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
Q_LOGGING_CATEGORY(lcCoreGlobal, "wangwenx190.framelesshelper.core.global")
@ -86,6 +76,15 @@ Q_LOGGING_CATEGORY(lcCoreGlobal, "wangwenx190.framelesshelper.core.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
{
QMutex mutex;
@ -115,10 +114,8 @@ void initialize()
qputenv(QT_QPA_ENV_VAR, kxcb);
#endif
#ifdef Q_OS_MACOS
// This has become the default setting since some unknown Qt version,
// check whether we can remove this hack safely or not.
qputenv(MAC_LAYER_ENV_VAR, kValueOne);
#if (defined(Q_OS_MACOS) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)))
qputenv(MAC_LAYER_ENV_VAR, FRAMELESSHELPER_BYTEARRAY_LITERAL("1"));
#endif
#ifdef Q_OS_WINDOWS
@ -276,9 +273,6 @@ void registerUninitializeHook(const UninitializeHookCallback &cb)
void setApplicationOSThemeAware(const bool enable, const bool pureQuick)
{
Q_UNUSED(enable);
Q_UNUSED(pureQuick);
static bool set = false;
if (set) {
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)))
Utils::setQtDarkModeAwareEnabled(enable, pureQuick);
#else
Q_UNUSED(enable);
Q_UNUSED(pureQuick);
#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();
#endif
}

View File

@ -219,7 +219,7 @@ bool Utils::isThemeChangeEvent(const QEvent * const event)
if (!event) {
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.
// QEvent::ThemeChange: Send by the Windows QPA.
// QEvent::ApplicationPaletteChange: All other platforms (Linux & macOS).

View File

@ -39,7 +39,8 @@
#include <AppKit/AppKit.h>
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
FRAMELESSHELPER_BEGIN_NAMESPACE
@ -499,9 +500,13 @@ bool Utils::isTitleBarColorized()
bool Utils::shouldAppsUseDarkMode_macos()
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
return qt_mac_applicationIsInDarkMode();
#else
const auto appearance = [NSApp.effectiveAppearance bestMatchFromAppearancesWithNames:
@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]];
return [appearance isEqualToString:NSAppearanceNameDarkAqua];
#endif
}
bool Utils::setBlurBehindWindowEnabled(const WId windowId, const BlurMode mode, const QColor &color)
@ -573,6 +578,11 @@ bool Utils::isBlurBehindWindowSupported()
return result;
}
void Utils::registerThemeChangeNotification()
{
// ### TODO
}
FRAMELESSHELPER_END_NAMESPACE
#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))
}
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