win: minor improvement

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-11-06 19:28:25 +08:00
parent 378f59f462
commit 07bd39d1aa
3 changed files with 66 additions and 29 deletions

View File

@ -616,6 +616,7 @@ FRAMELESSHELPER_CORE_API void uninitialize();
FRAMELESSHELPER_CORE_API void registerInitializeHook(const Global::InitializeHookCallback &cb);
FRAMELESSHELPER_CORE_API void registerUninitializeHook(const Global::UninitializeHookCallback &cb);
FRAMELESSHELPER_CORE_API void setApplicationOSThemeAware();
FRAMELESSHELPER_CORE_API void outputLogo();
} // namespace FramelessHelper::Core
FRAMELESSHELPER_END_NAMESPACE

View File

@ -41,11 +41,11 @@
#ifdef Q_OS_WINDOWS
# include "registrykey_p.h"
#endif
#include <QtCore/qmutex.h>
#include <QtGui/qguiapplication.h>
#ifdef Q_OS_LINUX
# include <gtk/gtk.h>
#endif
#include <QtCore/qmutex.h>
#include <QtGui/qguiapplication.h>
#ifndef COMPILER_STRING
# ifdef Q_CC_CLANG // Must be before GNU, because Clang claims to be GNU too.
@ -94,6 +94,7 @@ FRAMELESSHELPER_BYTEARRAY_CONSTANT(xcb)
[[maybe_unused]] static constexpr const char MAC_LAYER_ENV_VAR[] = "QT_MAC_WANTS_LAYER";
#endif
[[maybe_unused]] static constexpr const char kNoLogoEnvVar[] = "FRAMELESSHELPER_NO_LOGO";
FRAMELESSHELPER_STRING_CONSTANT2(FramelessHelperLogPrefix, "wangwenx190.framelesshelper.")
struct CoreData
@ -145,6 +146,8 @@ void initialize()
coreData()->mutex.unlock();
#endif
outputLogo();
#ifdef Q_OS_LINUX
gtk_init(nullptr, nullptr);
#endif
@ -340,6 +343,22 @@ void setApplicationOSThemeAware()
#endif
}
void outputLogo()
{
if (qEnvironmentVariableIntValue(kNoLogoEnvVar)) {
return;
}
const VersionInfo &ver = version();
QString message = {};
QTextStream stream(&message, QTextStream::WriteOnly);
stream << "FramelessHelper (" << (ver.isStatic ? "static" : "shared")
<< ", " << (ver.isDebug ? "debug" : "release") << ") version "
<< ver.version_str << ", author wangwenx190 (Yuhang Zhao).\n"
<< "Built by " << ver.compiler << " from " << ver.commit
<< " on " << ver.compileDateTime << '.';
INFO.nospace().noquote() << message;
}
}
FRAMELESSHELPER_END_NAMESPACE

View File

@ -143,6 +143,12 @@ _GetDpiAwarenessContextForProcess(
_In_ HANDLE hProcess
);
BOOL WINAPI
_AreDpiAwarenessContextsEqual(
_In_ _DPI_AWARENESS_CONTEXT dpiContextA,
_In_ _DPI_AWARENESS_CONTEXT dpiContextB
);
EXTERN_C_END
EXTERN_C [[nodiscard]] FRAMELESSHELPER_CORE_API BOOL WINAPI
@ -403,6 +409,9 @@ using namespace Global;
static constexpr const char kNoFixQtInternalEnvVar[] = "FRAMELESSHELPER_WINDOWS_DONT_FIX_QT";
static const QString qDwmColorKeyName = QString::fromWCharArray(kDwmColorKeyName);
static constexpr const char kDpiNoAccessErrorMessage[] =
"FramelessHelper doesn't have access to change the current process's DPI awareness mode,"
" most likely due to it has been set externally already. Eg: application manifest file.";
FRAMELESSHELPER_STRING_CONSTANT2(SuccessMessageText, "The operation completed successfully.")
FRAMELESSHELPER_STRING_CONSTANT2(EmptyMessageText, "FormatMessageW() returned empty string.")
FRAMELESSHELPER_STRING_CONSTANT2(ErrorMessageTemplate, "Function %1() failed with error code %2: %3.")
@ -500,6 +509,7 @@ FRAMELESSHELPER_STRING_CONSTANT(GetDpiAwarenessContextForProcess)
FRAMELESSHELPER_STRING_CONSTANT(GetCurrentProcess)
FRAMELESSHELPER_STRING_CONSTANT(GetProcessDpiAwareness)
FRAMELESSHELPER_STRING_CONSTANT(IsProcessDPIAware)
FRAMELESSHELPER_STRING_CONSTANT(AreDpiAwarenessContextsEqual)
struct Win32UtilsHelperData
{
@ -1654,6 +1664,7 @@ void Utils::tryToEnableHighestDpiAwarenessLevel()
{
bool isHighestAlready = false;
const DpiAwareness currentAwareness = getDpiAwarenessForCurrentProcess(&isHighestAlready);
DEBUG << "Current DPI awareness mode:" << currentAwareness;
if (isHighestAlready) {
return;
}
@ -1671,8 +1682,7 @@ void Utils::tryToEnableHighestDpiAwarenessLevel()
// Any attempt to change the DPI awareness level through API will always fail,
// so we treat this situation as succeeded.
if (dwError == ERROR_ACCESS_DENIED) {
DEBUG << "FramelessHelper doesn't have access to change the current process's DPI awareness mode,"
" most likely due to it has been set externally already.";
DEBUG << kDpiNoAccessErrorMessage;
return true;
}
WARNING << __getSystemErrorMessage(kSetProcessDpiAwarenessContext, dwError);
@ -1713,8 +1723,7 @@ void Utils::tryToEnableHighestDpiAwarenessLevel()
// Any attempt to change the DPI awareness level through API will always fail,
// so we treat this situation as succeeded.
if (hr == E_ACCESSDENIED) {
DEBUG << "FramelessHelper doesn't have access to change the current process's DPI awareness mode,"
" most likely due to it has been set externally already.";
DEBUG << kDpiNoAccessErrorMessage;
return true;
}
WARNING << __getSystemErrorMessage(kSetProcessDpiAwareness, hr);
@ -2296,6 +2305,7 @@ DpiAwareness Utils::getDpiAwarenessForCurrentProcess(bool *highest)
{
if ((API_USER_AVAILABLE(GetDpiAwarenessContextForProcess)
|| API_USER_AVAILABLE(GetThreadDpiAwarenessContext))
&& API_USER_AVAILABLE(AreDpiAwarenessContextsEqual)
&& API_USER_AVAILABLE(GetAwarenessFromDpiAwarenessContext)) {
const auto context = []() -> _DPI_AWARENESS_CONTEXT {
if (API_USER_AVAILABLE(GetDpiAwarenessContextForProcess)) {
@ -2320,9 +2330,17 @@ DpiAwareness Utils::getDpiAwarenessForCurrentProcess(bool *highest)
if (!context) {
return DpiAwareness::Unknown;
}
const _DPI_AWARENESS awareness = API_CALL_FUNCTION4(GetAwarenessFromDpiAwarenessContext, context);
static constexpr const auto highestAvailable = DpiAwareness::PerMonitorVersion2;
auto result = DpiAwareness::Unknown;
// We have to use another API to compare PMv2 and GdiScaled because it seems the
// GetAwarenessFromDpiAwarenessContext() function won't give us these two values.
if (API_CALL_FUNCTION4(AreDpiAwarenessContextsEqual, context,
_DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) != FALSE) {
result = DpiAwareness::PerMonitorVersion2;
} else if (API_CALL_FUNCTION4(AreDpiAwarenessContextsEqual, context,
_DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED) != FALSE) {
result = DpiAwareness::Unaware_GdiScaled;
} else {
const _DPI_AWARENESS awareness = API_CALL_FUNCTION4(GetAwarenessFromDpiAwarenessContext, context);
switch (awareness) {
case _DPI_AWARENESS_INVALID:
break;
@ -2342,8 +2360,9 @@ DpiAwareness Utils::getDpiAwarenessForCurrentProcess(bool *highest)
result = DpiAwareness::Unaware_GdiScaled;
break;
}
}
if (highest) {
*highest = (result == highestAvailable);
*highest = (result == DpiAwareness::PerMonitorVersion2);
}
return result;
}
@ -2354,7 +2373,6 @@ DpiAwareness Utils::getDpiAwarenessForCurrentProcess(bool *highest)
WARNING << __getSystemErrorMessage(kGetProcessDpiAwareness, hr);
return DpiAwareness::Unknown;
}
static constexpr const auto highestAvailable = DpiAwareness::PerMonitor;
auto result = DpiAwareness::Unknown;
switch (pda) {
case _PROCESS_DPI_UNAWARE:
@ -2374,16 +2392,15 @@ DpiAwareness Utils::getDpiAwarenessForCurrentProcess(bool *highest)
break;
}
if (highest) {
*highest = (result == highestAvailable);
*highest = (result == DpiAwareness::PerMonitor);
}
return result;
}
if (API_USER_AVAILABLE(IsProcessDPIAware)) {
const BOOL isAware = API_CALL_FUNCTION(IsProcessDPIAware);
static constexpr const auto highestAvailable = DpiAwareness::System;
const auto result = ((isAware == FALSE) ? DpiAwareness::Unaware : DpiAwareness::System);
if (highest) {
*highest = (result == highestAvailable);
*highest = (result == DpiAwareness::System);
}
return result;
}