8000 Merge pull request #17728 from anntzer/fw · matplotlib/matplotlib@4b22dca · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b22dca

Browse files
authored
Merge pull request #17728 from anntzer/fw
MNT: Move Win32_{Get,Set}ForegroundWindow to c_internal_utils.
2 parents 4a143b9 + 4d2bff8 commit 4b22dca

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import numpy as np
1212

1313
import matplotlib as mpl
14-
from matplotlib import backend_tools, cbook
14+
from matplotlib import backend_tools, cbook, _c_internal_utils
1515
from matplotlib.backend_bases import (
1616
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
1717
StatusbarBase, TimerBase, ToolContainerBase, cursors)
@@ -20,22 +20,6 @@
2020
from matplotlib.widgets import SubplotTool
2121
from . import _tkagg
2222

23-
try:
24-
from ._tkagg import Win32_GetForegroundWindow, Win32_SetForegroundWindow
25-
except ImportError:
26-
@contextmanager
27-
def _restore_foreground_window_at_end():
28-
yield
29-
else:
30-
@contextmanager
31-
def _restore_foreground_window_at_end():
32-
foreground = Win32_GetForegroundWindow()
33-
try:
34-
yield
35-
finally:
36-
if mpl.rcParams['tk.window_focus']:
37-
Win32_SetForegroundWindow(foreground)
38-
3923

4024
_log = logging.getLogger(__name__)
4125

@@ -50,6 +34,16 @@ def _restore_foreground_window_at_end():
5034
}
5135

5236

37+
@contextmanager
38+
def _restore_foreground_window_at_end():
39+
foreground = _c_internal_utils.Win32_GetForegroundWindow()
40+
try:
41+
yield
42+
finally:
43+
if mpl.rcParams['tk.window_focus']:
44+
_c_internal_utils.Win32_SetForegroundWindow(foreground)
45+
46+
5347
def blit(photoimage, aggimage, offsets, bbox=None):
5448
"""
5549
Blit *aggimage* to *photoimage*.

setupext.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ def get_extensions(self):
341341
# c_internal_utils
342342
ext = Extension(
343343
"matplotlib._c_internal_utils", ["src/_c_internal_utils.c"],
344-
libraries={"win32": ["ole32", "shell32"]}.get(sys.platform, []))
344+
libraries=({"win32": ["ole32", "shell32", "user32"]}
345+
.get(sys.platform, [])))
345346
yield ext
346347
# contour
347348
ext = Extension(
@@ -400,8 +401,7 @@ def get_extensions(self):
400401
],
401402
include_dirs=["src"],
402403
# psapi library needed for finding Tcl/Tk at run time.
403-
# user32 library needed for window manipulation functions.
404-
libraries=({"linux": ["dl"], "win32": ["psapi", "user32"],
404+
libraries=({"linux": ["dl"], "win32": ["psapi"],
405405
"cygwin": ["psapi"]}.get(sys.platform, [])),
406406
extra_link_args={"win32": ["-mwindows"]}.get(sys.platform, []))
407407
add_numpy_flags(ext)

src/_c_internal_utils.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifdef _WIN32
44
#include <Objbase.h>
55
#include <Shobjidl.h>
6+
#include <Windows.h>
67
#endif
78

89
static PyObject* mpl_GetCurrentProcessExplicitAppUserModelID(PyObject* module)
@@ -39,6 +40,31 @@ static PyObject* mpl_SetCurrentProcessExplicitAppUserModelID(PyObject* module, P
3940
#endif
4041
}
4142

43+
static PyObject* mpl_GetForegroundWindow(PyObject* module)
44+
{
45+
#ifdef _WIN32
46+
return PyLong_FromVoidPtr(GetForegroundWindow());
47+
#else
48+
Py_RETURN_NONE;
49+
#endif
50+
}
51+
52+
static PyObject* mpl_SetForegroundWindow(PyObject* module, PyObject *arg)
53+
{
54+
#ifdef _WIN32
55+
HWND handle = PyLong_AsVoidPtr(arg);
56+
if (PyErr_Occurred()) {
57+
return NULL;
58+
}
59+
if (!SetForegroundWindow(handle)) {
60+
return PyErr_Format(PyExc_RuntimeError, "Error setting window");
61+
}
62+
Py_RETURN_NONE;
63+
#else
64+
Py_RETURN_NONE;
65+
#endif
66+
}
67+
4268
static PyMethodDef functions[] = {
4369
{"Win32_GetCurrentProcessExplicitAppUserModelID",
4470
(PyCFunction)mpl_GetCurrentProcessExplicitAppUserModelID, METH_NOARGS,
@@ -50,6 +76,16 @@ static PyMethodDef functions[] = {
5076
"Win32_SetCurrentProcessExplicitAppUserModelID(appid, /)\n--\n\n"
5177
"Wrapper for Windows's SetCurrentProcessExplicitAppUserModelID. On \n"
5278
"non-Windows platforms, a no-op."},
79+
{"Win32_GetForegroundWindow",
80+
(PyCFunction)mpl_GetForegroundWindow, METH_NOARGS,
81+
"Win32_GetForegroundWindow()\n--\n\n"
82+
"Wrapper for Windows' GetForegroundWindow. On non-Windows platforms, \n"
83+
"always returns None."},
84+
{"Win32_SetForegroundWindow",
85+
(PyCFunction)mpl_SetForegroundWindow, METH_O,
86+
"Win32_SetForegroundWindow(hwnd)\n--\n\n"
87+
"Wrapper for Windows' SetForegroundWindow. On non-Windows platforms, \n"
88+
"a no-op."},
5389
{NULL, NULL}}; // sentinel.
5490
static PyModuleDef util_module = {
5591
PyModuleDef_HEAD_INIT, "_c_internal_utils", "", 0, functions, NULL, NULL, NULL, NULL};

src/_tkagg.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -89,38 +89,8 @@ static PyObject *mpl_tk_blit(PyObject *self, PyObject *args)
8989
}
9090
}
9191

92-
#ifdef _WIN32
93-
static PyObject *
94-
Win32_GetForegroundWindow(PyObject *module, PyObject *args)
95-
{
96-
HWND handle = GetForegroundWindow();
97-
if (!PyArg_ParseTuple(args, ":GetForegroundWindow")) {
98-
return NULL;
99-
}
100-
return PyLong_FromSize_t((size_t)handle);
101-
}
102-
103-
static PyObject *
104-
Win32_SetForegroundWindow(PyObject *module, PyObject *args)
105-
{
106-
HWND handle;
107-
if (!PyArg_ParseTuple(args, "n:SetForegroundWindow", &handle)) {
108-
return NULL;
109-
}
110-
if (!SetForegroundWindow(handle)) {
111-
return PyErr_Format(PyExc_RuntimeError, "Error setting window");
112-
}
113-
Py_INCREF(Py_None);
114-
return Py_None;
115-
}
116-
#endif
117-
11892
static PyMethodDef functions[] = {
11993
{ "blit", (PyCFunction)mpl_tk_blit, METH_VARARGS },
120-
#ifdef _WIN32
121-
{ "Win32_GetForegroundWindow", (PyCFunction)Win32_GetForegroundWindow, METH_VARARGS },
122-
{ "Win32_SetForegroundWindow", (PyCFunction)Win32_SetForegroundWindow, METH_VARARGS },
123-
#endif
12494
{ NULL, NULL } /* sentinel */
12595
};
12696

0 commit comments

Comments
 (0)
0