Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
Matplotlib Device Scaling on Windows#30599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -602,6 +602,8 @@ def destroy(*args): | ||
self.window.protocol("WM_DELETE_WINDOW", destroy) | ||
self.window.deiconify() | ||
self.canvas._tkcanvas.focus_set() | ||
if self._window_dpi.get() != 96: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. why hard-code 96? | ||
self._update_window_dpi() | ||
else: | ||
self.canvas.draw_idle() | ||
if mpl.rcParams['figure.raise_window']: | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -152,6 +152,22 @@ mpl_tk_blit(py::object interp_obj, const char *photo_name, | ||
} | ||
#ifdef WIN32_DLL | ||
void update_tk_window_dpi(HWND hwnd, Tcl_Interp *interp, std::string dpi) { | ||
// This variable naming must match the name used in | ||
// lib/matplotlib/backends/_backend_tk.py:FigureManagerTk. | ||
std::string var_name("window_dpi"); | ||
var_name += std::to_string((unsigned long long)hwnd); | ||
if (TCL_SETVAR) { | ||
TCL_SETVAR(interp, var_name.c_str(), dpi.c_str(), 0); | ||
} else if (TCL_SETVAR2) { | ||
TCL_SETVAR2(interp, var_name.c_str(), NULL, dpi.c_str(), 0); | ||
} else { | ||
// This should be prevented at import time, and therefore unreachable. | ||
// But defensively throw just in case. | ||
throw std::runtime_error("Unable to call Tcl_SetVar or Tcl_SetVar2"); | ||
} | ||
} | ||
LRESULT CALLBACK | ||
DpiSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, | ||
UINT_PTR uIdSubclass, DWORD_PTR dwRefData) | ||
@@ -165,24 +181,10 @@ DpiSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, | ||
// call Python code, we must not also call *any* Tk code from Python. | ||
// So stay with Tcl calls in C only. | ||
{ | ||
// X is high word, Y is low word, but they are always equal. | ||
std::string dpi = std::to_string(LOWORD(wParam)); | ||
Tcl_Interp* interp = (Tcl_Interp*)dwRefData; | ||
update_tk_window_dpi(hwnd, interp, dpi); | ||
} | ||
return 0; | ||
case WM_NCDESTROY: | ||
@@ -235,6 +237,15 @@ mpl_tk_enable_dpi_awareness(py::object UNUSED_ON_NON_WINDOWS(frame_handle_obj), | ||
// Per monitor aware means we need to handle WM_DPICHANGED by wrapping | ||
// the Window Procedure, and the Python side needs to trace the Tk | ||
// window_dpi variable stored on interp. | ||
typedef UINT (WINAPI *GetDpiForWindow_t)(HWND); | ||
GetDpiForWindow_t GetDpiForWindowPtr = | ||
(GetDpiForWindow_t)GetProcAddress(user32, "GetDpiForWindow"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Please use the shorter style for casting as in#30615. | ||
if (GetDpiForWindowPtr == NULL) { | ||
FreeLibrary(user32); | ||
return py::cast(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Is completely returning here correct? Is there a case where we may not be able to get the methods for managing the dpi but still need to run the | ||
} | ||
UINT dpi = GetDpiForWindowPtr(frame_handle); | ||
update_tk_window_dpi(frame_handle, interp, std::to_string(dpi)); | ||
SetWindowSubclass(frame_handle, DpiSubclassProc, 0, (DWORD_PTR)interp); | ||
} | ||
FreeLibrary(user32); | ||
Uh oh!
There was an error while loading.Please reload this page.