From 6e79ba3f33ab723a268637df4ed428581aaf51f3 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Fri, 13 Jan 2023 13:39:21 +0800 Subject: [PATCH] minor tweaks Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- examples/dialog/dialog.cpp | 2 +- examples/dialog/main.cpp | 6 ++++++ examples/mainwindow/main.cpp | 6 ++++++ examples/mainwindow/mainwindow.cpp | 2 +- examples/openglwidget/mainwindow.cpp | 2 +- examples/quick/quicksettings.cpp | 2 +- examples/shared/log.cpp | 2 +- examples/widget/main.cpp | 6 ++++++ examples/widget/widget.cpp | 2 +- src/core/cmakehelper.cmake | 2 +- src/core/framelessconfig.cpp | 3 +-- src/core/framelesshelper_win.cpp | 11 ++++++++++- src/core/utils_win.cpp | 9 ++++----- 13 files changed, 40 insertions(+), 15 deletions(-) diff --git a/examples/dialog/dialog.cpp b/examples/dialog/dialog.cpp index dfd3a60..217ca55 100644 --- a/examples/dialog/dialog.cpp +++ b/examples/dialog/dialog.cpp @@ -136,7 +136,7 @@ void Dialog::setupUi() const auto savedGeometry = Settings::get({}, kGeometry); if (savedGeometry.isValid() && !parent()) { const auto savedDpr = Settings::get({}, kDevicePixelRatio); - // Qt doesn't support dpi < 1. + // Qt doesn't support dpr < 1. const qreal oldDpr = std::max(savedDpr, qreal(1)); const qreal scale = (devicePixelRatioF() / oldDpr); setGeometry({savedGeometry.topLeft() * scale, savedGeometry.size() * scale}); diff --git a/examples/dialog/main.cpp b/examples/dialog/main.cpp index 8cbacdb..d2b3b5b 100644 --- a/examples/dialog/main.cpp +++ b/examples/dialog/main.cpp @@ -40,6 +40,12 @@ int main(int argc, char *argv[]) // of any Q(Core|Gui)Application instances. FramelessHelper::Widgets::initialize(); +#if 0 + if (!qEnvironmentVariableIsSet("QT_WIDGETS_RHI")) { + qputenv("QT_WIDGETS_RHI", FRAMELESSHELPER_BYTEARRAY_LITERAL("1")); + } +#endif + const auto application = std::make_unique(argc, argv); // Must be called after QGuiApplication has been constructed, we are using diff --git a/examples/mainwindow/main.cpp b/examples/mainwindow/main.cpp index e24353b..122af8a 100644 --- a/examples/mainwindow/main.cpp +++ b/examples/mainwindow/main.cpp @@ -40,6 +40,12 @@ int main(int argc, char *argv[]) // of any Q(Core|Gui)Application instances. FramelessHelper::Widgets::initialize(); +#if 0 + if (!qEnvironmentVariableIsSet("QT_WIDGETS_RHI")) { + qputenv("QT_WIDGETS_RHI", FRAMELESSHELPER_BYTEARRAY_LITERAL("1")); + } +#endif + const auto application = std::make_unique(argc, argv); // Must be called after QGuiApplication has been constructed, we are using diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp index c45df6c..19142fa 100644 --- a/examples/mainwindow/mainwindow.cpp +++ b/examples/mainwindow/mainwindow.cpp @@ -109,7 +109,7 @@ QMenuBar::item:pressed { const auto savedGeometry = Settings::get({}, kGeometry); if (savedGeometry.isValid() && !parent()) { const auto savedDpr = Settings::get({}, kDevicePixelRatio); - // Qt doesn't support dpi < 1. + // Qt doesn't support dpr < 1. const qreal oldDpr = std::max(savedDpr, qreal(1)); const qreal scale = (devicePixelRatioF() / oldDpr); setGeometry({savedGeometry.topLeft() * scale, savedGeometry.size() * scale}); diff --git a/examples/openglwidget/mainwindow.cpp b/examples/openglwidget/mainwindow.cpp index 00eb7b9..ec9bd7e 100644 --- a/examples/openglwidget/mainwindow.cpp +++ b/examples/openglwidget/mainwindow.cpp @@ -84,7 +84,7 @@ void MainWindow::initialize() const auto savedGeometry = Settings::get({}, kGeometry); if (savedGeometry.isValid() && !parent()) { const auto savedDpr = Settings::get({}, kDevicePixelRatio); - // Qt doesn't support dpi < 1. + // Qt doesn't support dpr < 1. const qreal oldDpr = std::max(savedDpr, qreal(1)); const qreal scale = (devicePixelRatioF() / oldDpr); setGeometry({savedGeometry.topLeft() * scale, savedGeometry.size() * scale}); diff --git a/examples/quick/quicksettings.cpp b/examples/quick/quicksettings.cpp index c58e1ea..bf56ac6 100644 --- a/examples/quick/quicksettings.cpp +++ b/examples/quick/quicksettings.cpp @@ -63,7 +63,7 @@ bool QuickSettings::restoreGeometry(QWindow *window) return false; } const auto savedDpr = Settings::get(objName, kDevicePixelRatio); - // Qt doesn't support dpi < 1. + // Qt doesn't support dpr < 1. const qreal oldDpr = std::max(savedDpr, qreal(1)); const qreal scale = (window->devicePixelRatio() / oldDpr); window->setGeometry({savedGeometry.topLeft() * scale, savedGeometry.size() * scale}); diff --git a/examples/shared/log.cpp b/examples/shared/log.cpp index 5a465de..fd4f396 100644 --- a/examples/shared/log.cpp +++ b/examples/shared/log.cpp @@ -86,9 +86,9 @@ void Log::setup(const QString &app) } once = true; g_app = app; - qInstallMessageHandler(myMessageHandler); qSetMessagePattern(FRAMELESSHELPER_STRING_LITERAL( "[%{time yyyy/MM/dd hh:mm:ss.zzz}] <%{if-info}INFO%{endif}%{if-debug}DEBUG" "%{endif}%{if-warning}WARNING%{endif}%{if-critical}CRITICAL%{endif}%{if-fatal}" "FATAL%{endif}> %{if-category}%{category}: %{endif}%{message}")); + qInstallMessageHandler(myMessageHandler); } diff --git a/examples/widget/main.cpp b/examples/widget/main.cpp index 758e5a5..9561cf7 100644 --- a/examples/widget/main.cpp +++ b/examples/widget/main.cpp @@ -40,6 +40,12 @@ int main(int argc, char *argv[]) // of any Q(Core|Gui)Application instances. FramelessHelper::Widgets::initialize(); +#if 0 + if (!qEnvironmentVariableIsSet("QT_WIDGETS_RHI")) { + qputenv("QT_WIDGETS_RHI", FRAMELESSHELPER_BYTEARRAY_LITERAL("1")); + } +#endif + const auto application = std::make_unique(argc, argv); // Must be called after QGuiApplication has been constructed, we are using diff --git a/examples/widget/widget.cpp b/examples/widget/widget.cpp index 2d2998d..18c32a0 100644 --- a/examples/widget/widget.cpp +++ b/examples/widget/widget.cpp @@ -136,7 +136,7 @@ void Widget::initialize() const auto savedGeometry = Settings::get(objName, kGeometry); if (savedGeometry.isValid() && !parent()) { const auto savedDpr = Settings::get(objName, kDevicePixelRatio); - // Qt doesn't support dpi < 1. + // Qt doesn't support dpr < 1. const qreal oldDpr = std::max(savedDpr, qreal(1)); const qreal scale = (devicePixelRatioF() / oldDpr); setGeometry({savedGeometry.topLeft() * scale, savedGeometry.size() * scale}); diff --git a/src/core/cmakehelper.cmake b/src/core/cmakehelper.cmake index 0f9a81a..9092d3e 100644 --- a/src/core/cmakehelper.cmake +++ b/src/core/cmakehelper.cmake @@ -82,7 +82,7 @@ function(setup_compile_params arg_target) target_link_options(${arg_target} PRIVATE /SAFESEH) endif() if(CMAKE_SIZEOF_VOID_P EQUAL 8) - target_link_options(${arg_target} PRIVATE $<$>:/HIGHENTROPYVA>) + target_link_options(${arg_target} PRIVATE /HIGHENTROPYVA) endif() if(MSVC_VERSION GREATER_EQUAL 1915) # Visual Studio 2017 version 15.8 target_compile_options(${arg_target} PRIVATE $<$,$>:/JMC>) diff --git a/src/core/framelessconfig.cpp b/src/core/framelessconfig.cpp index 6d2c099..dc57276 100644 --- a/src/core/framelessconfig.cpp +++ b/src/core/framelessconfig.cpp @@ -83,7 +83,6 @@ struct ConfigData bool options[OptionCount] = {}; bool disableEnvVar = false; bool disableCfgFile = false; - QVariantHash internals = {}; }; Q_GLOBAL_STATIC(ConfigData, g_data) @@ -147,7 +146,7 @@ void FramelessConfig::reload(const bool force) } g_data()->loaded = true; - QTimer::singleShot(0, qApp, [](){ warnInappropriateOptions(); }); + QTimer::singleShot(0, this, [](){ warnInappropriateOptions(); }); } void FramelessConfig::set(const Option option, const bool on) diff --git a/src/core/framelesshelper_win.cpp b/src/core/framelesshelper_win.cpp index 80a4b9d..87ede19 100644 --- a/src/core/framelesshelper_win.cpp +++ b/src/core/framelesshelper_win.cpp @@ -530,11 +530,20 @@ void FramelessHelperWin::addWindow(const SystemParameters ¶ms) } g_win32Helper()->mutex.unlock(); DEBUG.noquote() << "The DPI of window" << hwnd2str(windowId) << "is" << data.dpi; +#if 0 + params.setWindowFlags(params.getWindowFlags() | Qt::FramelessWindowHint); + // We need some delay here, otherwise the window styles will be overwritten by + // QPA itself. But don't use QThread::sleep(), it doesn't help in our case. + QTimer::singleShot(0, qApp, [windowId](){ + Utils::maybeFixupQtInternals(windowId); + }); +#else // Qt maintains a frame margin internally, we need to update it accordingly // otherwise we'll get lots of warning messages when we change the window // geometry, it will also affect the final window geometry because QPA will // always take it into account when setting window size and position. Utils::updateInternalWindowFrameMargins(params.getWindowHandle(), true); +#endif // Tell DWM our preferred frame margin. Utils::updateWindowFrameMargins(windowId, false); // Tell DWM we don't use the window icon/caption/sysmenu, don't draw them. @@ -562,7 +571,7 @@ void FramelessHelperWin::addWindow(const SystemParameters ¶ms) // The fallback title bar window is only used to activate the Snap Layout feature // introduced in Windows 11, so it's not necessary to create it on systems below Win11. if (!FramelessConfig::instance()->isSet(Option::DisableWindowsSnapLayout)) { - if (!createFallbackTitleBarWindow(windowId, data.params.isWindowFixedSize())) { + if (!createFallbackTitleBarWindow(windowId, params.isWindowFixedSize())) { WARNING << "Failed to create the fallback title bar window."; } } diff --git a/src/core/utils_win.cpp b/src/core/utils_win.cpp index 9971f59..84fe886 100644 --- a/src/core/utils_win.cpp +++ b/src/core/utils_win.cpp @@ -1276,7 +1276,9 @@ void Utils::maybeFixupQtInternals(const WId windowId) } SetLastError(ERROR_SUCCESS); const auto windowStyle = static_cast(GetWindowLongPtrW(hwnd, GWL_STYLE)); - if (windowStyle != 0) { + if (windowStyle == 0) { + WARNING << getSystemErrorMessage(kGetWindowLongPtrW); + } else { // Qt by default adds the "WS_POPUP" flag to all Win32 windows it created and maintained, // which is not a good thing (although it won't cause any obvious issues in most cases // either), because popup windows have some different behavior with normal overlapped @@ -1285,8 +1287,7 @@ void Utils::maybeFixupQtInternals(const WId windowId) // and this will also break the normal functionalities for our windows, so we do the // correction here unconditionally. static constexpr const DWORD badWindowStyle = WS_POPUP; - static constexpr const DWORD goodWindowStyle = - (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME); + static constexpr const DWORD goodWindowStyle = WS_OVERLAPPEDWINDOW; if ((windowStyle & badWindowStyle) || !(windowStyle & goodWindowStyle)) { SetLastError(ERROR_SUCCESS); if (SetWindowLongPtrW(hwnd, GWL_STYLE, ((windowStyle & ~badWindowStyle) | goodWindowStyle)) == 0) { @@ -1295,8 +1296,6 @@ void Utils::maybeFixupQtInternals(const WId windowId) shouldUpdateFrame = true; } } - } else { - WARNING << getSystemErrorMessage(kGetWindowLongPtrW); } if (shouldUpdateFrame) { triggerFrameChange(windowId);