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 registerInitializeHook(const Global::InitializeHookCallback &cb);
FRAMELESSHELPER_CORE_API void registerUninitializeHook(const Global::UninitializeHookCallback &cb); FRAMELESSHELPER_CORE_API void registerUninitializeHook(const Global::UninitializeHookCallback &cb);
FRAMELESSHELPER_CORE_API void setApplicationOSThemeAware(); FRAMELESSHELPER_CORE_API void setApplicationOSThemeAware();
FRAMELESSHELPER_CORE_API void outputLogo();
} // namespace FramelessHelper::Core } // namespace FramelessHelper::Core
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -41,11 +41,11 @@
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
# include "registrykey_p.h" # include "registrykey_p.h"
#endif #endif
#include <QtCore/qmutex.h>
#include <QtGui/qguiapplication.h>
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
# include <gtk/gtk.h> # include <gtk/gtk.h>
#endif #endif
#include <QtCore/qmutex.h>
#include <QtGui/qguiapplication.h>
#ifndef COMPILER_STRING #ifndef COMPILER_STRING
# ifdef Q_CC_CLANG // Must be before GNU, because Clang claims to be GNU too. # 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"; [[maybe_unused]] static constexpr const char MAC_LAYER_ENV_VAR[] = "QT_MAC_WANTS_LAYER";
#endif #endif
[[maybe_unused]] static constexpr const char kNoLogoEnvVar[] = "FRAMELESSHELPER_NO_LOGO";
FRAMELESSHELPER_STRING_CONSTANT2(FramelessHelperLogPrefix, "wangwenx190.framelesshelper.") FRAMELESSHELPER_STRING_CONSTANT2(FramelessHelperLogPrefix, "wangwenx190.framelesshelper.")
struct CoreData struct CoreData
@ -145,6 +146,8 @@ void initialize()
coreData()->mutex.unlock(); coreData()->mutex.unlock();
#endif #endif
outputLogo();
#ifdef Q_OS_LINUX #ifdef Q_OS_LINUX
gtk_init(nullptr, nullptr); gtk_init(nullptr, nullptr);
#endif #endif
@ -340,6 +343,22 @@ void setApplicationOSThemeAware()
#endif #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 FRAMELESSHELPER_END_NAMESPACE

View File

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