From ee2c8482b57485b94ce9d3108084d9a7bbc45088 Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Sun, 4 Jun 2023 13:10:58 -0600 Subject: [PATCH 1/2] ENH: macosx allow figures can be opened in tabs or windows Allow users to select how new figures are created when opening new windows. The default is to keep using the system preferences, but one can also select "tab" or "window" to override the default system preferences. --- .../next_whats_new/macosx_windows_tabs.rst | 7 ++++++ lib/matplotlib/backends/backend_macosx.py | 1 + lib/matplotlib/mpl-data/matplotlibrc | 4 ++++ lib/matplotlib/rcsetup.py | 1 + src/_macosx.m | 23 +++++++++++++++++++ 5 files changed, 36 insertions(+) create mode 100644 doc/users/next_whats_new/macosx_windows_tabs.rst diff --git a/doc/users/next_whats_new/macosx_windows_tabs.rst b/doc/users/next_whats_new/macosx_windows_tabs.rst new file mode 100644 index 000000000000..85fd76b6cb6d --- /dev/null +++ b/doc/users/next_whats_new/macosx_windows_tabs.rst @@ -0,0 +1,7 @@ +macosx: New figures can be opened in either windows or tabs +----------------------------------------------------------- + +There is a new :rc:`macosx.window_mode`` rcParam to control how +new figures are opened with the macosx backend. The default is +**system** which uses the system settings, or one can specify either +**tab** or **window** to explicitly choose the mode used to open new figures. diff --git a/lib/matplotlib/backends/backend_macosx.py b/lib/matplotlib/backends/backend_macosx.py index b4cfcd480250..695667dc48c3 100644 --- a/lib/matplotlib/backends/backend_macosx.py +++ b/lib/matplotlib/backends/backend_macosx.py @@ -145,6 +145,7 @@ def __init__(self, canvas, num): icon_path = str(cbook._get_data_path('images/matplotlib.pdf')) _macosx.FigureManager.set_icon(icon_path) FigureManagerBase.__init__(self, canvas, num) + self._set_window_mode(mpl.rcParams.get("macosx.window_mode", "system")) if self.toolbar is not None: self.toolbar.update() if mpl.is_interactive(): diff --git a/lib/matplotlib/mpl-data/matplotlibrc b/lib/matplotlib/mpl-data/matplotlibrc index d951bfca3dc3..7b1ee1467a2e 100644 --- a/lib/matplotlib/mpl-data/matplotlibrc +++ b/lib/matplotlib/mpl-data/matplotlibrc @@ -701,6 +701,10 @@ # background by default #savefig.orientation: portrait # orientation of saved figure, for PostScript output only +### macosx backend params +#macosx.window_mode : system # How to open new figures (system, tab, window) + # system uses the MacOS system preferences + ### tk backend params #tk.window_focus: False # Maintain shell focus for TkAgg diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index e628692eae8a..3a7fcbb15ab2 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -1214,6 +1214,7 @@ def _convert_validator_spec(key, conv): "figure.autolayout": validate_bool, "figure.max_open_warning": validate_int, "figure.raise_window": validate_bool, + "macosx.window_mode": ["system", "tab", "window"], "figure.subplot.left": validate_float, "figure.subplot.right": validate_float, diff --git a/src/_macosx.m b/src/_macosx.m index 0b9dc593e614..5f8a4763322b 100755 --- a/src/_macosx.m +++ b/src/_macosx.m @@ -689,6 +689,25 @@ int mpl_check_modifier( return 0; } +static PyObject* +FigureManager__set_window_mode(FigureManager* self, PyObject* args) +{ + const char* window_mode; + if (!PyArg_ParseTuple(args, "s", &window_mode) || !self->window) { + return NULL; + } + + NSString* window_mode_str = [NSString stringWithUTF8String: window_mode]; + if ([window_mode_str isEqualToString: @"tab"]) { + [self->window setTabbingMode: NSWindowTabbingModePreferred]; + } else if ([window_mode_str isEqualToString: @"window"]) { + [self->window setTabbingMode: NSWindowTabbingModeDisallowed]; + } else { // system settings + [self->window setTabbingMode: NSWindowTabbingModeAutomatic]; + } + Py_RETURN_NONE; +} + static PyObject* FigureManager_repr(FigureManager* self) { @@ -832,6 +851,10 @@ int mpl_check_modifier( {"destroy", (PyCFunction)FigureManager_destroy, METH_NOARGS}, + {"_set_window_mode", + (PyCFunction)FigureManager__set_window_mode, + METH_VARARGS, + "Set the window open mode (auto, tab, window)"}, {"set_icon", (PyCFunction)FigureManager_set_icon, METH_STATIC | METH_VARARGS, From fc57f753e309e3b4fdf8b8e039bf66f920f50235 Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Fri, 21 Jul 2023 13:12:44 -0600 Subject: [PATCH 2/2] Update src/_macosx.m Co-authored-by: Kyle Sunden --- src/_macosx.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_macosx.m b/src/_macosx.m index 5f8a4763322b..aa76b48edc41 100755 --- a/src/_macosx.m +++ b/src/_macosx.m @@ -854,7 +854,7 @@ int mpl_check_modifier( {"_set_window_mode", (PyCFunction)FigureManager__set_window_mode, METH_VARARGS, - "Set the window open mode (auto, tab, window)"}, + "Set the window open mode (system, tab, window)"}, {"set_icon", (PyCFunction)FigureManager_set_icon, METH_STATIC | METH_VARARGS,