Handle DPI change on Windows to avoid the window be resized by Qt

This commit is contained in:
Ilya Fedin 2022-05-26 17:20:53 +04:00 committed by John Preston
parent e72706f249
commit cdfa6ea2bf
2 changed files with 23 additions and 1 deletions

View file

@ -9,6 +9,7 @@
#include "ui/inactive_press.h" #include "ui/inactive_press.h"
#include "ui/platform/win/ui_window_title_win.h" #include "ui/platform/win/ui_window_title_win.h"
#include "ui/widgets/rp_window.h" #include "ui/widgets/rp_window.h"
#include "base/platform/win/base_windows_safe_library.h"
#include "base/platform/base_platform_info.h" #include "base/platform/base_platform_info.h"
#include "base/integration.h" #include "base/integration.h"
#include "base/debug_log.h" #include "base/debug_log.h"
@ -36,6 +37,18 @@ constexpr auto kDWMWA_WINDOW_CORNER_PREFERENCE = DWORD(33);
constexpr auto kDWMWA_CAPTION_COLOR = DWORD(35); constexpr auto kDWMWA_CAPTION_COLOR = DWORD(35);
constexpr auto kDWMWA_TEXT_COLOR = DWORD(36); constexpr auto kDWMWA_TEXT_COLOR = DWORD(36);
UINT(__stdcall *GetDpiForWindow)(_In_ HWND hwnd);
[[nodiscard]] bool GetDpiForWindowSupported() {
static const auto Result = [&] {
#define LOAD_SYMBOL(lib, name) base::Platform::LoadMethod(lib, #name, name)
const auto user32 = base::Platform::SafeLoadLibrary(L"User32.dll");
return LOAD_SYMBOL(user32, GetDpiForWindow);
#undef LOAD_SYMBOL
}();
return Result;
}
[[nodiscard]] bool IsCompositionEnabled() { [[nodiscard]] bool IsCompositionEnabled() {
auto result = BOOL(FALSE); auto result = BOOL(FALSE);
const auto success = (DwmIsCompositionEnabled(&result) == S_OK); const auto success = (DwmIsCompositionEnabled(&result) == S_OK);
@ -163,7 +176,8 @@ WindowHelper::WindowHelper(not_null<RpWidget*> window)
, _handle(ResolveWindowHandle(window)) , _handle(ResolveWindowHandle(window))
, _title(Ui::CreateChild<TitleWidget>(window.get())) , _title(Ui::CreateChild<TitleWidget>(window.get()))
, _body(Ui::CreateChild<RpWidget>(window.get())) , _body(Ui::CreateChild<RpWidget>(window.get()))
, _shadow(std::in_place, window, st::windowShadowFg->c) { , _shadow(std::in_place, window, st::windowShadowFg->c)
, _dpi(GetDpiForWindowSupported() ? GetDpiForWindow(_handle) : 0) {
Expects(_handle != nullptr); Expects(_handle != nullptr);
init(); init();
@ -595,6 +609,13 @@ bool WindowHelper::handleNativeEvent(
} }
} return true; } return true;
// should return true for Qt not to change window size
// when moving the window between screens
// change to false once runtime scale change would be supported
case WM_DPICHANGED: {
_dpi = LOWORD(wParam);
} return true;
} }
return false; return false;
} }

View file

@ -81,6 +81,7 @@ private:
rpl::event_stream<HitTestResult> _systemButtonOver; rpl::event_stream<HitTestResult> _systemButtonOver;
rpl::event_stream<HitTestResult> _systemButtonDown; rpl::event_stream<HitTestResult> _systemButtonDown;
std::optional<WindowShadow> _shadow; std::optional<WindowShadow> _shadow;
rpl::variable<uint> _dpi;
QMargins _marginsDelta; QMargins _marginsDelta;
HMENU _menu = nullptr; HMENU _menu = nullptr;
bool _updatingMargins = false; bool _updatingMargins = false;