UI: Dynamic DPI scaling

This commit is contained in:
Dr. Chat 2017-08-03 21:17:57 -05:00
parent aeb0e2557c
commit b3b7acb7ca
3 changed files with 38 additions and 8 deletions

View File

@ -154,7 +154,9 @@ void Window::Layout() {
void Window::Invalidate() {} void Window::Invalidate() {}
void Window::OnDpiChanged(UIEvent* e) {} void Window::OnDpiChanged(UIEvent* e) {
// TODO(DrChat): Notify listeners.
}
void Window::OnResize(UIEvent* e) { void Window::OnResize(UIEvent* e) {
ForEachListener([e](auto listener) { listener->OnResize(e); }); ForEachListener([e](auto listener) { listener->OnResize(e); });

View File

@ -311,15 +311,25 @@ void Win32Window::Resize(int32_t width, int32_t height) {
return; return;
} }
RECT rc = {0, 0, width, height}; // Scale width and height
bool has_menu = !is_fullscreen() && (main_menu_ ? true : false); int32_t scaled_width, scaled_height;
AdjustWindowRect(&rc, GetWindowLong(hwnd_, GWL_STYLE), has_menu); scaled_width = int32_t(width * get_dpi_scale());
if (true) { scaled_height = int32_t(height * get_dpi_scale());
rc.right += 100 - rc.left;
rc.left = 100; RECT rc = {0, 0, 0, 0};
rc.bottom += 100 - rc.top; GetWindowRect(hwnd_, &rc);
if (rc.top < 0) {
rc.top = 100; rc.top = 100;
} }
if (rc.left < 0) {
rc.left = 100;
}
rc.right = rc.left + scaled_width;
rc.bottom = rc.top + scaled_height;
bool has_menu = !is_fullscreen() && (main_menu_ ? true : false);
AdjustWindowRect(&rc, GetWindowLong(hwnd_, GWL_STYLE), has_menu);
MoveWindow(hwnd_, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, MoveWindow(hwnd_, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
TRUE); TRUE);
@ -334,6 +344,11 @@ void Win32Window::Resize(int32_t left, int32_t top, int32_t right,
} }
RECT rc = {left, top, right, bottom}; RECT rc = {left, top, right, bottom};
// Scale width and height
rc.right = int32_t((right - left) * get_dpi_scale()) + left;
rc.bottom = int32_t((bottom - top) * get_dpi_scale()) + top;
bool has_menu = !is_fullscreen() && (main_menu_ ? true : false); bool has_menu = !is_fullscreen() && (main_menu_ ? true : false);
AdjustWindowRect(&rc, GetWindowLong(hwnd_, GWL_STYLE), has_menu); AdjustWindowRect(&rc, GetWindowLong(hwnd_, GWL_STYLE), has_menu);
MoveWindow(hwnd_, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, MoveWindow(hwnd_, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
@ -342,6 +357,11 @@ void Win32Window::Resize(int32_t left, int32_t top, int32_t right,
super::Resize(left, top, right, bottom); super::Resize(left, top, right, bottom);
} }
void Win32Window::RawReposition(const RECT& rc) {
MoveWindow(hwnd_, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
TRUE);
}
void Win32Window::OnResize(UIEvent* e) { void Win32Window::OnResize(UIEvent* e) {
RECT client_rect; RECT client_rect;
GetClientRect(hwnd_, &client_rect); GetClientRect(hwnd_, &client_rect);
@ -478,6 +498,11 @@ LRESULT Win32Window::WndProc(HWND hWnd, UINT message, WPARAM wParam,
case WM_DISPLAYCHANGE: case WM_DISPLAYCHANGE:
break; break;
case WM_DPICHANGED: { case WM_DPICHANGED: {
LPRECT rect = (LPRECT)lParam;
if (rect) {
RawReposition(*rect);
}
auto e = UIEvent(this); auto e = UIEvent(this);
OnDpiChanged(&e); OnDpiChanged(&e);
} break; } break;

View File

@ -49,6 +49,9 @@ class Win32Window : public Window {
void Resize(int32_t left, int32_t top, int32_t right, void Resize(int32_t left, int32_t top, int32_t right,
int32_t bottom) override; int32_t bottom) override;
// (raw) Resize the window, no DPI scaling applied.
void RawReposition(const RECT& rc);
bool Initialize() override; bool Initialize() override;
void Invalidate() override; void Invalidate() override;
void Close() override; void Close() override;