Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wangwenx190 committed Aug 1, 2023
1 parent f2758ab commit 3342a28
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 98 deletions.
22 changes: 15 additions & 7 deletions FramelessHelperConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,25 @@ set(_@PROJECT_NAME@_supported_components Core Widgets Quick)

foreach(_comp ${@PROJECT_NAME@_FIND_COMPONENTS})
if(_comp IN_LIST _@PROJECT_NAME@_supported_components)
set(__targets_file "${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@${_comp}Targets.cmake")
if(EXISTS "${__targets_file}")
include("${__targets_file}")
set(__target @PROJECT_NAME@::${_comp})
if(TARGET ${__target})
continue()
else()
set(@PROJECT_NAME@_FOUND FALSE)
set(@PROJECT_NAME@_NOT_FOUND_MESSAGE "Can't find necessary configuration file for @PROJECT_NAME@::${_comp}, please make sure this component is built successfully and installed properly.")
break()
set(__target_full @PROJECT_NAME@${_comp})
set(__targets_file "${CMAKE_CURRENT_LIST_DIR}/${__target_full}Targets.cmake")
if(EXISTS "${__targets_file}")
include("${__targets_file}")
add_library(@PROJECT_NAME@::${__target_full} ALIAS ${__target_full})
add_library(${__target} ALIAS ${__target_full})
else()
set(@PROJECT_NAME@_FOUND FALSE)
set(@PROJECT_NAME@_NOT_FOUND_MESSAGE "Can't find necessary configuration file for ${__target}, please make sure this component is built successfully and installed properly.")
break()
endif()
endif()
else()
set(@PROJECT_NAME@_FOUND FALSE)
set(@PROJECT_NAME@_NOT_FOUND_MESSAGE "Unknown component: @PROJECT_NAME@::${_comp}.")
set(@PROJECT_NAME@_NOT_FOUND_MESSAGE "Unknown component: ${__target}.")
break()
endif()
endforeach()
Expand Down
3 changes: 1 addition & 2 deletions src/core/chromepalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ ChromePalettePrivate::ChromePalettePrivate(ChromePalette *q) : QObject(q)
return;
}
q_ptr = q;
connect(FramelessManager::instance(),
&FramelessManager::systemThemeChanged, this, &ChromePalettePrivate::refresh);
connect(FramelessManager::instance(), &FramelessManager::systemThemeChanged, this, &ChromePalettePrivate::refresh);
refresh();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/framelessconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

FRAMELESSHELPER_BEGIN_NAMESPACE

static Q_LOGGING_CATEGORY(lcFramelessConfig, "wangwenx190.framelesshelper.core.framelessconfig")
[[maybe_unused]] static Q_LOGGING_CATEGORY(lcFramelessConfig, "wangwenx190.framelesshelper.core.framelessconfig")

#ifdef FRAMELESSHELPER_CORE_NO_DEBUG_OUTPUT
# define INFO QT_NO_QDEBUG_MACRO()
Expand Down
21 changes: 9 additions & 12 deletions src/core/framelesshelper_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ struct FramelessQtHelperData
bool leftButtonPressed = false;
};

struct FramelessQtHelperInternal
{
QHash<WId, FramelessQtHelperData> data = {};
};
using FramelessQtHelperInternal = QHash<WId, FramelessQtHelperData>;

Q_GLOBAL_STATIC(FramelessQtHelperInternal, g_framelessQtHelperData)

Expand All @@ -76,16 +73,16 @@ void FramelessHelperQt::addWindow(FramelessParamsConst params)
return;
}
const WId windowId = params->getWindowId();
const auto it = g_framelessQtHelperData()->data.constFind(windowId);
if (it != g_framelessQtHelperData()->data.constEnd()) {
const auto it = g_framelessQtHelperData()->constFind(windowId);
if (it != g_framelessQtHelperData()->constEnd()) {
return;
}
FramelessQtHelperData data = {};
data.params = *params;
QWindow *window = params->getWindowHandle();
// Give it a parent so that it can be automatically deleted by Qt.
data.eventFilter = new FramelessHelperQt(window);
g_framelessQtHelperData()->data.insert(windowId, data);
g_framelessQtHelperData()->insert(windowId, data);
const auto shouldApplyFramelessFlag = []() -> bool {
#ifdef Q_OS_MACOS
return false;
Expand Down Expand Up @@ -117,11 +114,11 @@ void FramelessHelperQt::removeWindow(const WId windowId)
if (!windowId) {
return;
}
const auto it = g_framelessQtHelperData()->data.constFind(windowId);
if (it == g_framelessQtHelperData()->data.constEnd()) {
const auto it = g_framelessQtHelperData()->constFind(windowId);
if (it == g_framelessQtHelperData()->constEnd()) {
return;
}
g_framelessQtHelperData()->data.erase(it);
g_framelessQtHelperData()->erase(it);
#ifdef Q_OS_MACOS
Utils::removeWindowProxy(windowId);
#endif
Expand Down Expand Up @@ -165,8 +162,8 @@ bool FramelessHelperQt::eventFilter(QObject *object, QEvent *event)
}
const auto window = qobject_cast<QWindow *>(object);
const WId windowId = window->winId();
const auto it = g_framelessQtHelperData()->data.find(windowId);
if (it == g_framelessQtHelperData()->data.end()) {
const auto it = g_framelessQtHelperData()->find(windowId);
if (it == g_framelessQtHelperData()->end()) {
return QObject::eventFilter(object, event);
}
const FramelessQtHelperData &data = it.value();
Expand Down
5 changes: 4 additions & 1 deletion src/core/framelesshelper_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

FRAMELESSHELPER_BEGIN_NAMESPACE

static Q_LOGGING_CATEGORY(lcFramelessHelperWin, "wangwenx190.framelesshelper.core.impl.win")
[[maybe_unused]] static Q_LOGGING_CATEGORY(lcFramelessHelperWin, "wangwenx190.framelesshelper.core.impl.win")

#ifdef FRAMELESSHELPER_CORE_NO_DEBUG_OUTPUT
# define INFO QT_NO_QDEBUG_MACRO()
Expand Down Expand Up @@ -212,7 +212,10 @@ Q_GLOBAL_STATIC(FramelessWin32HelperInternal, g_framelessWin32HelperData)
case SystemButtonType::Close:
return HTCLOSE;
case SystemButtonType::Unknown:
QT_WARNING_PUSH
QT_WARNING_DISABLE_MSVC(4702)
Q_UNREACHABLE_RETURN(HTNOWHERE);
QT_WARNING_POP
}
}
// Returns "HTTRANSPARENT" to let the mouse event pass through this invisible
Expand Down
2 changes: 1 addition & 1 deletion src/core/framelesshelpercore_global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void framelesshelpercore_initResource()

FRAMELESSHELPER_BEGIN_NAMESPACE

static Q_LOGGING_CATEGORY(lcCoreGlobal, "wangwenx190.framelesshelper.core.global")
[[maybe_unused]] static Q_LOGGING_CATEGORY(lcCoreGlobal, "wangwenx190.framelesshelper.core.global")

#ifdef FRAMELESSHELPER_CORE_NO_DEBUG_OUTPUT
# define INFO QT_NO_QDEBUG_MACRO()
Expand Down
2 changes: 1 addition & 1 deletion src/core/framelessmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

FRAMELESSHELPER_BEGIN_NAMESPACE

static Q_LOGGING_CATEGORY(lcFramelessManager, "wangwenx190.framelesshelper.core.framelessmanager")
[[maybe_unused]] static Q_LOGGING_CATEGORY(lcFramelessManager, "wangwenx190.framelesshelper.core.framelessmanager")

#ifdef FRAMELESSHELPER_CORE_NO_DEBUG_OUTPUT
# define INFO QT_NO_QDEBUG_MACRO()
Expand Down
12 changes: 4 additions & 8 deletions src/core/micamaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

FRAMELESSHELPER_BEGIN_NAMESPACE

static Q_LOGGING_CATEGORY(lcMicaMaterial, "wangwenx190.framelesshelper.core.micamaterial")
[[maybe_unused]] static Q_LOGGING_CATEGORY(lcMicaMaterial, "wangwenx190.framelesshelper.core.micamaterial")

#ifdef FRAMELESSHELPER_CORE_NO_DEBUG_OUTPUT
# define INFO QT_NO_QDEBUG_MACRO()
Expand Down Expand Up @@ -157,16 +157,12 @@ static inline void qt_blurrow(QImage &im, const int line, const int alpha)

int zR = 0, zG = 0, zB = 0, zA = 0;

#ifdef Q_CC_MSVC
# pragma warning(push)
# pragma warning(disable:4127) // false alarm.
#endif // Q_CC_MSVC
QT_WARNING_PUSH
QT_WARNING_DISABLE_MSVC(4127) // false alarm.
if (alphaOnly && (im.format() != QImage::Format_Indexed8)) {
bptr += alphaIndex;
}
#ifdef Q_CC_MSVC
# pragma warning(pop)
#endif // Q_CC_MSVC
QT_WARNING_POP

const int stride = (im.depth() >> 3);
const int im_width = im.width();
Expand Down
35 changes: 18 additions & 17 deletions src/core/sysapiloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

FRAMELESSHELPER_BEGIN_NAMESPACE

static Q_LOGGING_CATEGORY(lcSysApiLoader, "wangwenx190.framelesshelper.core.sysapiloader")
[[maybe_unused]] static Q_LOGGING_CATEGORY(lcSysApiLoader, "wangwenx190.framelesshelper.core.sysapiloader")

#ifdef FRAMELESSHELPER_CORE_NO_DEBUG_OUTPUT
# define INFO QT_NO_QDEBUG_MACRO()
Expand All @@ -71,14 +71,15 @@ static Q_LOGGING_CATEGORY(lcSysApiLoader, "wangwenx190.framelesshelper.core.sysa
# define CRITICAL qCCritical(lcSysApiLoader)
#endif

struct SysApiLoaderData
{
QHash<QString, QFunctionPointer> functionCache = {};
};
using SysApiLoaderData = QHash<QString, QFunctionPointer>;

Q_GLOBAL_STATIC(SysApiLoaderData, g_sysApiLoaderData)

static const bool LoaderDebugFlag = qEnvironmentVariableIntValue("FRAMELESSHELPER_SYSAPILOADER_DEBUG");
[[nodiscard]] static inline bool isDebug()
{
static const bool flag = qEnvironmentVariableIntValue("FRAMELESSHELPER_SYSAPILOADER_DEBUG");
return flag;
}

SysApiLoader::SysApiLoader(QObject *parent) : QObject(parent)
{
Expand Down Expand Up @@ -113,11 +114,11 @@ QString SysApiLoader::platformSystemLibraryDirectory()
static const auto result = []() -> QString {
#ifdef Q_OS_WINDOWS
QVarLengthArray<wchar_t, MAX_PATH> buf = {};
const UINT len = GetSystemDirectoryW(buf.data(), MAX_PATH);
const UINT len = ::GetSystemDirectoryW(buf.data(), MAX_PATH);
if (len > MAX_PATH) {
// No need to +1 here, GetSystemDirectoryW() will always give us a null terminator.
buf.resize(len);
GetSystemDirectoryW(buf.data(), len);
::GetSystemDirectoryW(buf.data(), len);
}
return QString::fromWCharArray(buf.constData(), len);
#else
Expand Down Expand Up @@ -184,16 +185,16 @@ bool SysApiLoader::isAvailable(const QString &library, const QString &function)
return false;
}
const QString key = generateUniqueKey(library, function);
const auto it = g_sysApiLoaderData()->functionCache.constFind(key);
if (it != g_sysApiLoaderData()->functionCache.constEnd()) {
if (LoaderDebugFlag) {
const auto it = g_sysApiLoaderData()->constFind(key);
if (it != g_sysApiLoaderData()->constEnd()) {
if (isDebug()) {
DEBUG << Q_FUNC_INFO << "Function cache found:" << key;
}
return (it.value() != nullptr);
} else {
const QFunctionPointer symbol = SysApiLoader::resolve(library, function);
g_sysApiLoaderData()->functionCache.insert(key, symbol);
if (LoaderDebugFlag) {
g_sysApiLoaderData()->insert(key, symbol);
if (isDebug()) {
DEBUG << Q_FUNC_INFO << "New function cache:" << key << (symbol ? "[VALID]" : "[NULL]");
}
if (symbol) {
Expand All @@ -214,14 +215,14 @@ QFunctionPointer SysApiLoader::get(const QString &library, const QString &functi
return nullptr;
}
const QString key = generateUniqueKey(library, function);
const auto it = g_sysApiLoaderData()->functionCache.constFind(key);
if (it != g_sysApiLoaderData()->functionCache.constEnd()) {
if (LoaderDebugFlag) {
const auto it = g_sysApiLoaderData()->constFind(key);
if (it != g_sysApiLoaderData()->constEnd()) {
if (isDebug()) {
DEBUG << Q_FUNC_INFO << "Function cache found:" << key;
}
return it.value();
} else {
if (LoaderDebugFlag) {
if (isDebug()) {
DEBUG << Q_FUNC_INFO << "Function cache not found:" << key;
}
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

FRAMELESSHELPER_BEGIN_NAMESPACE

static Q_LOGGING_CATEGORY(lcUtilsCommon, "wangwenx190.framelesshelper.core.utils.common")
[[maybe_unused]] static Q_LOGGING_CATEGORY(lcUtilsCommon, "wangwenx190.framelesshelper.core.utils.common")

#ifdef FRAMELESSHELPER_CORE_NO_DEBUG_OUTPUT
# define INFO QT_NO_QDEBUG_MACRO()
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

FRAMELESSHELPER_BEGIN_NAMESPACE

static Q_LOGGING_CATEGORY(lcUtilsLinux, "wangwenx190.framelesshelper.core.utils.linux")
[[maybe_unused]] static Q_LOGGING_CATEGORY(lcUtilsLinux, "wangwenx190.framelesshelper.core.utils.linux")

#ifdef FRAMELESSHELPER_CORE_NO_DEBUG_OUTPUT
# define INFO QT_NO_QDEBUG_MACRO()
Expand Down
25 changes: 11 additions & 14 deletions src/core/utils_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object

FRAMELESSHELPER_BEGIN_NAMESPACE

static Q_LOGGING_CATEGORY(lcUtilsMac, "wangwenx190.framelesshelper.core.utils.mac")
[[maybe_unused]] static Q_LOGGING_CATEGORY(lcUtilsMac, "wangwenx190.framelesshelper.core.utils.mac")

#ifdef FRAMELESSHELPER_CORE_NO_DEBUG_OUTPUT
# define INFO QT_NO_QDEBUG_MACRO()
Expand Down Expand Up @@ -513,10 +513,7 @@ static void sendEvent(id obj, SEL sel, NSEvent *event)
static inline sendEventPtr oldSendEvent = nil;
};

struct MacUtilsData
{
QHash<WId, NSWindowProxy *> hash = {};
};
using MacUtilsData = QHash<WId, NSWindowProxy *>;

Q_GLOBAL_STATIC(MacUtilsData, g_macUtilsData);

Expand All @@ -536,17 +533,17 @@ static void sendEvent(id obj, SEL sel, NSEvent *event)

static inline void cleanupProxy()
{
if (g_macUtilsData()->hash.isEmpty()) {
if (g_macUtilsData()->isEmpty()) {
return;
}
for (auto &&proxy : std::as_const(g_macUtilsData()->hash)) {
for (auto &&proxy : std::as_const(g_macUtilsData())) {
Q_ASSERT(proxy);
if (!proxy) {
continue;
}
delete proxy;
}
g_macUtilsData()->hash.clear();
g_macUtilsData()->clear();
}

[[nodiscard]] static inline NSWindowProxy *ensureWindowProxy(const WId windowId)
Expand All @@ -555,8 +552,8 @@ static inline void cleanupProxy()
if (!windowId) {
return nil;
}
auto it = g_macUtilsData()->hash.find(windowId);
if (it == g_macUtilsData()->hash.end()) {
auto it = g_macUtilsData()->find(windowId);
if (it == g_macUtilsData()->end()) {
QWindow * const qwindow = Utils::findWindow(windowId);
Q_ASSERT(qwindow);
if (!qwindow) {
Expand All @@ -568,7 +565,7 @@ static inline void cleanupProxy()
return nil;
}
const auto proxy = new NSWindowProxy(qwindow, nswindow);
it = g_macUtilsData()->hash.insert(windowId, proxy);
it = g_macUtilsData()->insert(windowId, proxy);
}
static bool cleanerInstalled = false;
if (!cleanerInstalled) {
Expand Down Expand Up @@ -737,16 +734,16 @@ static inline void cleanupProxy()
if (!windowId) {
return;
}
const auto it = g_macUtilsData()->hash.constFind(windowId);
if (it == g_macUtilsData()->hash.constEnd()) {
const auto it = g_macUtilsData()->constFind(windowId);
if (it == g_macUtilsData()->constEnd()) {
return;
}
if (const auto proxy = it.value()) {
// We'll restore everything to default in the destructor,
// so no need to do it manually here.
delete proxy;
}
g_macUtilsData()->hash.erase(it);
g_macUtilsData()->erase(it);
}

QColor Utils::getFrameBorderColor(const bool active)
Expand Down
Loading

0 comments on commit 3342a28

Please sign in to comment.