Skip to content

Commit 18dda38

Browse files
hlwhlhaolinwei
andauthored
[Windows] make setMinimum/MaximumSize() dpi change awareable (#231)
* save pixel ratio * [Windows] makes setMin/MaxSize() dpi change awareable * use pre defined USER_DEFAULT_SCREEN_DPI instead of hard coded 96.0 --------- Co-authored-by: haolinwei <[email protected]>
1 parent ed22962 commit 18dda38

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

windows/window_manager.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class WindowManager {
5252
double aspect_ratio_ = 0;
5353
POINT minimum_size_ = {0, 0};
5454
POINT maximum_size_ = {-1, -1};
55+
double pixel_ratio_ = 1;
5556
bool is_resizable_ = true;
5657
bool is_skip_taskbar_ = true;
5758
std::string title_bar_style_ = "normal";
@@ -534,9 +535,10 @@ void WindowManager::SetMinimumSize(const flutter::EncodableMap& args) {
534535
double height = std::get<double>(args.at(flutter::EncodableValue("height")));
535536

536537
if (width >= 0 && height >= 0) {
538+
pixel_ratio_ = devicePixelRatio;
537539
POINT point = {};
538-
point.x = static_cast<LONG>(width * devicePixelRatio);
539-
point.y = static_cast<LONG>(height * devicePixelRatio);
540+
point.x = static_cast<LONG>(width);
541+
point.y = static_cast<LONG>(height);
540542
minimum_size_ = point;
541543
}
542544
}
@@ -548,9 +550,10 @@ void WindowManager::SetMaximumSize(const flutter::EncodableMap& args) {
548550
double height = std::get<double>(args.at(flutter::EncodableValue("height")));
549551

550552
if (width >= 0 && height >= 0) {
553+
pixel_ratio_ = devicePixelRatio;
551554
POINT point = {};
552-
point.x = static_cast<LONG>(width * devicePixelRatio);
553-
point.y = static_cast<LONG>(height * devicePixelRatio);
555+
point.x = static_cast<LONG>(width);
556+
point.y = static_cast<LONG>(height);
554557
maximum_size_ = point;
555558
}
556559
}

windows/window_manager_plugin.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ std::optional<LRESULT> WindowManagerPlugin::HandleWindowProc(HWND hWnd,
108108
LPARAM lParam) {
109109
std::optional<LRESULT> result = std::nullopt;
110110

111+
if (message == WM_DPICHANGED) {
112+
window_manager->pixel_ratio_ = (float) LOWORD(wParam) / USER_DEFAULT_SCREEN_DPI;
113+
}
114+
111115
if (message == WM_NCCALCSIZE) {
112116
// This must always be first or else the one of other two ifs will execute
113117
// when window is in full screen and we don't want that
@@ -163,13 +167,21 @@ std::optional<LRESULT> WindowManagerPlugin::HandleWindowProc(HWND hWnd,
163167
MINMAXINFO* info = reinterpret_cast<MINMAXINFO*>(lParam);
164168
// For the special "unconstrained" values, leave the defaults.
165169
if (window_manager->minimum_size_.x != 0)
166-
info->ptMinTrackSize.x = window_manager->minimum_size_.x;
170+
info->ptMinTrackSize.x =
171+
static_cast<LONG> (window_manager->minimum_size_.x *
172+
window_manager->pixel_ratio_);
167173
if (window_manager->minimum_size_.y != 0)
168-
info->ptMinTrackSize.y = window_manager->minimum_size_.y;
174+
info->ptMinTrackSize.y =
175+
static_cast<LONG> (window_manager->minimum_size_.y *
176+
window_manager->pixel_ratio_);
169177
if (window_manager->maximum_size_.x != -1)
170-
info->ptMaxTrackSize.x = window_manager->maximum_size_.x;
178+
info->ptMaxTrackSize.x =
179+
static_cast<LONG> (window_manager->maximum_size_.x *
180+
window_manager->pixel_ratio_);
171181
if (window_manager->maximum_size_.y != -1)
172-
info->ptMaxTrackSize.y = window_manager->maximum_size_.y;
182+
info->ptMaxTrackSize.y =
183+
static_cast<LONG> (window_manager->maximum_size_.y *
184+
window_manager->pixel_ratio_);
173185
result = 0;
174186
} else if (message == WM_NCACTIVATE) {
175187
if (wParam == TRUE) {

0 commit comments

Comments
 (0)