avoid potential deadlocks

And some other minor tweaks.

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-09-25 10:29:42 +08:00
parent c914992aa4
commit f97f000cc7
6 changed files with 40 additions and 18 deletions

View File

@ -97,6 +97,7 @@ private:
QColor m_savedWindowBackgroundColor = {}; QColor m_savedWindowBackgroundColor = {};
bool m_blurBehindWindowEnabled = false; bool m_blurBehindWindowEnabled = false;
std::optional<bool> m_extendIntoTitleBar = std::nullopt; std::optional<bool> m_extendIntoTitleBar = std::nullopt;
bool m_destroying = false;
}; };
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -92,6 +92,7 @@ private:
QColor m_savedWindowBackgroundColor = {}; QColor m_savedWindowBackgroundColor = {};
bool m_blurBehindWindowEnabled = false; bool m_blurBehindWindowEnabled = false;
QPointer<QWidget> m_window = nullptr; QPointer<QWidget> m_window = nullptr;
bool m_destroying = false;
}; };
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -25,6 +25,7 @@
#include "framelessmanager_p.h" #include "framelessmanager_p.h"
#include <QtCore/qdebug.h> #include <QtCore/qdebug.h>
#include <QtCore/qmutex.h> #include <QtCore/qmutex.h>
#include <QtCore/qcoreapplication.h>
#include <QtGui/qscreen.h> #include <QtGui/qscreen.h>
#include <QtGui/qwindow.h> #include <QtGui/qwindow.h>
#include <QtGui/qfontdatabase.h> #include <QtGui/qfontdatabase.h>
@ -73,14 +74,15 @@ Q_GLOBAL_STATIC(FramelessManagerHelper, g_helper)
Q_GLOBAL_STATIC(FramelessManager, g_manager) Q_GLOBAL_STATIC(FramelessManager, g_manager)
[[maybe_unused]] static constexpr const char kGlobalFlagVarName[] = "__FRAMELESSHELPER__";
FRAMELESSHELPER_STRING_CONSTANT2(IconFontFilePath, ":/org.wangwenx190.FramelessHelper/resources/fonts/Micon.ttf") FRAMELESSHELPER_STRING_CONSTANT2(IconFontFilePath, ":/org.wangwenx190.FramelessHelper/resources/fonts/Micon.ttf")
FRAMELESSHELPER_STRING_CONSTANT2(IconFontFamilyName_win11, "Segoe Fluent Icons") FRAMELESSHELPER_STRING_CONSTANT2(IconFontFamilyName_win11, "Segoe Fluent Icons")
FRAMELESSHELPER_STRING_CONSTANT2(IconFontFamilyName_win10, "Segoe MDL2 Assets") FRAMELESSHELPER_STRING_CONSTANT2(IconFontFamilyName_win10, "Segoe MDL2 Assets")
FRAMELESSHELPER_STRING_CONSTANT2(IconFontFamilyName_common, "micon_nb") FRAMELESSHELPER_STRING_CONSTANT2(IconFontFamilyName_common, "micon_nb")
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS
static constexpr const int kIconFontPointSize = 10; [[maybe_unused]] static constexpr const int kIconFontPointSize = 10;
#else #else
static constexpr const int kIconFontPointSize = 8; [[maybe_unused]] static constexpr const int kIconFontPointSize = 8;
#endif #endif
[[nodiscard]] static inline QString iconFontFamilyName() [[nodiscard]] static inline QString iconFontFamilyName()
@ -352,11 +354,21 @@ void FramelessManagerPrivate::initialize()
m_wallpaperAspectStyle = Utils::getWallpaperAspectStyle(); m_wallpaperAspectStyle = Utils::getWallpaperAspectStyle();
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
QStyleHints * const styleHints = QGuiApplication::styleHints(); QStyleHints * const styleHints = QGuiApplication::styleHints();
Q_ASSERT(styleHints);
if (styleHints) {
connect(styleHints, &QStyleHints::appearanceChanged, this, [this](const Qt::Appearance appearance){ connect(styleHints, &QStyleHints::appearanceChanged, this, [this](const Qt::Appearance appearance){
Q_UNUSED(appearance); Q_UNUSED(appearance);
notifySystemThemeHasChangedOrNot(); notifySystemThemeHasChangedOrNot();
}); });
}
#endif // (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) #endif // (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
static bool flagSet = false;
if (!flagSet) {
flagSet = true;
// Set a global flag so that people can check whether FramelessHelper is being
// used without actually accessing the FramelessHelper interface.
qApp->setProperty(kGlobalFlagVarName, FramelessHelper::Core::version().version);
}
} }
FramelessManager::FramelessManager(QObject *parent) : FramelessManager::FramelessManager(QObject *parent) :

View File

@ -282,7 +282,9 @@ struct SYSTEM_METRIC
if (code == ERROR_SUCCESS) { if (code == ERROR_SUCCESS) {
return kSuccessMessageText; return kSuccessMessageText;
} }
#if 0 // The following code works well, we commented it out just because we want to use as many Qt functionalities as possible. static constexpr const bool flag = false;
if (flag) {
// The following code works well, we commented it out just because we want to use as many Qt functionalities as possible.
LPWSTR buf = nullptr; LPWSTR buf = nullptr;
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPWSTR>(&buf), 0, nullptr) == 0) { nullptr, code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPWSTR>(&buf), 0, nullptr) == 0) {
@ -291,9 +293,9 @@ struct SYSTEM_METRIC
const QString errorText = QString::fromWCharArray(buf).trimmed(); const QString errorText = QString::fromWCharArray(buf).trimmed();
LocalFree(buf); LocalFree(buf);
buf = nullptr; buf = nullptr;
#else return kErrorMessageTemplate.arg(function, QString::number(code), errorText);
}
const QString errorText = QSystemError::windowsString(code); const QString errorText = QSystemError::windowsString(code);
#endif
return kErrorMessageTemplate.arg(function, QString::number(code), errorText); return kErrorMessageTemplate.arg(function, QString::number(code), errorText);
} }

View File

@ -103,6 +103,7 @@ FramelessQuickHelperPrivate::FramelessQuickHelperPrivate(FramelessQuickHelper *q
FramelessQuickHelperPrivate::~FramelessQuickHelperPrivate() FramelessQuickHelperPrivate::~FramelessQuickHelperPrivate()
{ {
m_destroying = true;
extendsContentIntoTitleBar(false); extendsContentIntoTitleBar(false);
m_extendIntoTitleBar = std::nullopt; m_extendIntoTitleBar = std::nullopt;
} }
@ -141,8 +142,10 @@ void FramelessQuickHelperPrivate::extendsContentIntoTitleBar(const bool value)
detach(); detach();
} }
m_extendIntoTitleBar = value; m_extendIntoTitleBar = value;
if (!m_destroying) {
emitSignalForAllInstances(FRAMELESSHELPER_BYTEARRAY_LITERAL("extendsContentIntoTitleBarChanged")); emitSignalForAllInstances(FRAMELESSHELPER_BYTEARRAY_LITERAL("extendsContentIntoTitleBarChanged"));
} }
}
QQuickItem *FramelessQuickHelperPrivate::getTitleBarItem() const QQuickItem *FramelessQuickHelperPrivate::getTitleBarItem() const
{ {

View File

@ -109,6 +109,7 @@ FramelessWidgetsHelperPrivate::FramelessWidgetsHelperPrivate(FramelessWidgetsHel
FramelessWidgetsHelperPrivate::~FramelessWidgetsHelperPrivate() FramelessWidgetsHelperPrivate::~FramelessWidgetsHelperPrivate()
{ {
m_destroying = true;
extendsContentIntoTitleBar(false); extendsContentIntoTitleBar(false);
} }
@ -448,8 +449,10 @@ void FramelessWidgetsHelperPrivate::extendsContentIntoTitleBar(const bool value)
} else { } else {
detach(); detach();
} }
if (!m_destroying) {
emitSignalForAllInstances(FRAMELESSHELPER_BYTEARRAY_LITERAL("extendsContentIntoTitleBarChanged")); emitSignalForAllInstances(FRAMELESSHELPER_BYTEARRAY_LITERAL("extendsContentIntoTitleBarChanged"));
} }
}
QWidget *FramelessWidgetsHelperPrivate::findTopLevelWindow() const QWidget *FramelessWidgetsHelperPrivate::findTopLevelWindow() const
{ {