Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
First pass at moving files from macosx-staging
  • Loading branch information
iccir committed Jul 31, 2026
commit b0b62e4f4f3c93873336352bf439b80a709c1124
4 changes: 2 additions & 2 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"GUI: MacOSX":
- changed-files:
- any-glob-to-any-file:
- 'lib/matplotlib/backends/*_macosx.py*'
- 'src/_macosx.m'
- 'lib/matplotlib/backends/backend_legacymac.py'
- 'src/_legacymac.m'
"GUI: nbagg":
- changed-files:
- any-glob-to-any-file:
Expand Down
22 changes: 11 additions & 11 deletions lib/matplotlib/backends/backend_legacymac.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
CloseEvent, ResizeEvent, TimerBase, _allow_interrupt)


class TimerMac(_macosx.Timer, TimerBase):
class TimerLegacyMac(_macosx.Timer, TimerBase):
"""Subclass of `.TimerBase` using CFRunLoop timer events."""
# completely implemented at the C-level (in _macosx.Timer)

Expand All @@ -21,7 +21,7 @@ def _allow_interrupt_macos():
lambda rsock: _macosx.wake_on_fd_write(rsock.fileno()), _macosx.stop)


class FigureCanvasMac(FigureCanvasAgg, _macosx.FigureCanvas, FigureCanvasBase):
class FigureCanvasLegacyMac(FigureCanvasAgg, _macosx.FigureCanvas, FigureCanvasBase):
# docstring inherited

# Ideally this class would be `class FCMacAgg(FCAgg, FCMac)`
Expand All @@ -36,8 +36,8 @@ class FigureCanvasMac(FigureCanvasAgg, _macosx.FigureCanvas, FigureCanvasBase):
# handled in C and events (MouseEvent, etc.) are triggered from there.

required_interactive_framework = "macosx"
_timer_cls = TimerMac
manager_class = _api.classproperty(lambda cls: FigureManagerMac)
_timer_cls = TimerLegacyMac
manager_class = _api.classproperty(lambda cls: FigureManagerLegacyMac)

def __init__(self, figure):
super().__init__(figure=figure)
Expand Down Expand Up @@ -113,7 +113,7 @@ def start_event_loop(self, timeout=0):
self._start_event_loop(timeout=timeout) # Forward to ObjC implementation.


class NavigationToolbar2Mac(_macosx.NavigationToolbar2, NavigationToolbar2):
class NavigationToolbar2LegacyMac(_macosx.NavigationToolbar2, NavigationToolbar2):

def __init__(self, canvas):
data_path = cbook._get_data_path('images')
Expand Down Expand Up @@ -145,8 +145,8 @@ def save_figure(self, *args):
return filename


class FigureManagerMac(_macosx.FigureManager, FigureManagerBase):
_toolbar2_class = NavigationToolbar2Mac
class FigureManagerLegacyMac(_macosx.FigureManager, FigureManagerBase):
_toolbar2_class = NavigationToolbar2LegacyMac

def __init__(self, canvas, num):
self._shown = False
Expand Down Expand Up @@ -193,7 +193,7 @@ def show(self):


@_Backend.export
class _BackendMac(_Backend):
FigureCanvas = FigureCanvasMac
FigureManager = FigureManagerMac
mainloop = FigureManagerMac.start_main_loop
class _BackendLegacyMac(_Backend):
FigureCanvas = FigureCanvasLegacyMac
FigureManager = FigureManagerLegacyMac
mainloop = FigureManagerLegacyMac.start_main_loop
199 changes: 199 additions & 0 deletions lib/matplotlib/backends/backend_macos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import os

import matplotlib as mpl
from matplotlib import _api, cbook
from matplotlib._pylab_helpers import Gcf
from . import _macos
from .backend_agg import FigureCanvasAgg
from matplotlib.backend_bases import (
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
CloseEvent, ResizeEvent, TimerBase, _allow_interrupt)


class TimerMac(_macos.Timer, TimerBase):
"""Subclass of `.TimerBase` using CFRunLoop timer events."""
# completely implemented at the C-level (in _macos.Timer)


def _allow_interrupt_macos():
"""A context manager that allows terminating a plot by sending a SIGINT."""
return _allow_interrupt(
lambda rsock: _macos.wake_on_fd_write(rsock.fileno()), _macos.stop)


class FigureCanvasMac(FigureCanvasAgg, _macos.FigureCanvas, FigureCanvasBase):
# docstring inherited

# Ideally this class would be `class FCMacAgg(FCAgg, FCMac)`
# (FC=FigureCanvas) where FCMac would be an ObjC-implemented mac-specific
# class also inheriting from FCBase (this is the approach with other GUI
# toolkits). However, writing an extension type inheriting from a Python
# base class is slightly tricky (the extension type must be a heap type),
# and we can just as well lift the FCBase base up one level, keeping it *at
# the end* to have the right method resolution order.

# Events such as button presses, mouse movements, and key presses are
# handled in C and events (MouseEvent, etc.) are triggered from there.

required_interactive_framework = "macosx"
_timer_cls = TimerMac
manager_class = _api.classproperty(lambda cls: FigureManagerMac)

def __init__(self, figure):
super().__init__(figure=figure)
self._draw_pending = False
self._is_drawing = False
# Keep track of the timers that are alive
self._timers = set()

def draw(self):
"""Render the figure and update the macosx canvas."""
# The renderer draw is done here; delaying causes problems with code
# that uses the result of the draw() to update plot elements.
if self._is_drawing:
return
with cbook._setattr_cm(self, _is_drawing=True):
super().draw()
self.update()

def draw_idle(self):
# docstring inherited
if not (getattr(self, '_draw_pending', False) or
getattr(self, '_is_drawing', False)):
self._draw_pending = True
# Add a singleshot timer to the eventloop that will call back
# into the Python method _draw_idle to take care of the draw
self._single_shot_timer(self._draw_idle)

def _single_shot_timer(self, callback):
"""Add a single shot timer with the given callback"""
def callback_func(callback, timer):
callback()
self._timers.remove(timer)
timer = self.new_timer(interval=0)
timer.single_shot = True
timer.add_callback(callback_func, callback, timer)
self._timers.add(timer)
timer.start()

def _draw_idle(self):
"""
Draw method for singleshot timer

This draw method can be added to a singleshot timer, which can
accumulate draws while the eventloop is spinning. This method will
then only draw the first time and short-circuit the others.
"""
with self._idle_draw_cntx():
if not self._draw_pending:
# Short-circuit because our draw request has already been
# taken care of
return
self._draw_pending = False
self.draw()

def blit(self, bbox=None):
# docstring inherited
super().blit(bbox)
self.update()

def resize(self, width, height):
# Size from macOS is logical pixels, dpi is physical.
scale = self.figure.dpi / self.device_pixel_ratio
width /= scale
height /= scale
self.figure.set_size_inches(width, height, forward=False)
ResizeEvent("resize_event", self)._process()
self.draw_idle()

def start_event_loop(self, timeout=0):
# docstring inherited
# Set up a SIGINT handler to allow terminating a plot via CTRL-C.
with _allow_interrupt_macos():
self._start_event_loop(timeout=timeout) # Forward to ObjC implementation.


class NavigationToolbar2Mac(_macos.NavigationToolbar2, NavigationToolbar2):

def __init__(self, canvas):
data_path = cbook._get_data_path('images')
_, tooltips, image_names, _ = zip(*NavigationToolbar2.toolitems)
_macos.NavigationToolbar2.__init__(
self, canvas,
tuple(str(data_path / image_name) + ".pdf"
for image_name in image_names if image_name is not None),
tuple(tooltip for tooltip in tooltips if tooltip is not None))
NavigationToolbar2.__init__(self, canvas)

def draw_rubberband(self, event, x0, y0, x1, y1):
self.canvas.set_rubberband(int(x0), int(y0), int(x1), int(y1))

def remove_rubberband(self):
self.canvas.remove_rubberband()

def save_figure(self, *args):
directory = os.path.expanduser(mpl.rcParams['savefig.directory'])
filename = _macos.choose_save_file('Save the figure',
directory,
self.canvas.get_default_filename())
if filename is None: # Cancel
return
# Save dir for next time, unless empty str (which means use cwd).
if mpl.rcParams['savefig.directory']:
mpl.rcParams['savefig.directory'] = os.path.dirname(filename)
self.canvas.figure.savefig(filename)
return filename


class FigureManagerMac(_macos.FigureManager, FigureManagerBase):
_toolbar2_class = NavigationToolbar2Mac

def __init__(self, canvas, num):
self._shown = False
_macos.FigureManager.__init__(self, canvas)
icon_path = str(cbook._get_data_path('images/matplotlib.pdf'))
_macos.FigureManager.set_icon(icon_path)
FigureManagerBase.__init__(self, canvas, num)
self._set_window_mode(mpl.rcParams["macosx.window_mode"])
if self.toolbar is not None:
self.toolbar.update()
if mpl.is_interactive():
self.show()
self.canvas.draw_idle()

def _handle_window_will_close(self):
CloseEvent("close_event", self.canvas)._process()

def _handle_window_should_close(self):
Gcf.destroy(self)
self.canvas.flush_events()

def destroy(self):
# We need to clear any pending timers that never fired, otherwise
# we get a memory leak from the timer callbacks holding a reference
while self.canvas._timers:
timer = self.canvas._timers.pop()
timer.stop()
super().destroy()

@classmethod
def start_main_loop(cls):
# Set up a SIGINT handler to allow terminating a plot via CTRL-C.
with _allow_interrupt_macos():
_macos.show()

def show(self):
if self.canvas.figure.stale:
self.canvas.draw_idle()
if not self._shown:
self._show()
self._shown = True
if mpl.rcParams["figure.raise_window"]:
self._raise()


@_Backend.export
class _BackendMac(_Backend):
FigureCanvas = FigureCanvasMac
FigureManager = FigureManagerMac
mainloop = FigureManagerMac.start_main_loop
21 changes: 21 additions & 0 deletions lib/matplotlib/backends/backend_macosx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys
from . import backend_legacymac as _legacymac
from matplotlib.backend_bases import _Backend

_RENAMES = {
'FigureCanvasMac': 'FigureCanvasLegacyMac',
'FigureManagerMac': 'FigureManagerLegacyMac',
'NavigationToolbar2': 'NavigationToolbar2LegacyMac'
}

_mod = sys.modules[__name__]
for _new, _old in _RENAMES.items():
setattr(_mod, _new, getattr(_legacymac, _old))
del _mod, _new, _old


@_Backend.export
class _BackendMac(_Backend):
FigureCanvas = _legacymac.FigureCanvasLegacyMac
FigureManager = _legacymac.FigureManagerLegacyMac
mainloop = _legacymac.FigureManagerLegacyMac.start_main_loop
2 changes: 2 additions & 0 deletions lib/matplotlib/backends/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ python_sources = [
'backend_gtk4agg.py',
'backend_gtk4cairo.py',
'backend_legacymac.py',
'backend_macosx.py',
'backend_macos.py',
'backend_mixed.py',
'backend_nbagg.py',
'_backend_pdf_ps.py',
Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/backends/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class BackendRegistry:
"gtk4agg": "gtk4",
"gtk4cairo": "gtk4",
"legacymac": "legacymac",
"macosx": "macosx",
"macos": "macos",
"nbagg": "nbagg",
"notebook": "nbagg",
"qtagg": "qt",
Expand Down Expand Up @@ -67,6 +69,8 @@ class BackendRegistry:
"gtk4": "gtk4agg",
"headless": "agg",
"legacymac": "legacymac",
"macos": "macos",
"macosx": "macosx",
"qt": "qtagg",
"qt5": "qt5agg",
"qt6": "qtagg",
Expand Down
5 changes: 4 additions & 1 deletion lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _get_running_interactive_framework():
-------
Optional[str]
One of the following values: "qt", "gtk3", "gtk4", "wx", "tk",
"macosx", "headless", ``None``.
"macos", "macosx", "headless", ``None``.
"""
# Use ``sys.modules.get(name)`` rather than ``name in sys.modules`` as
# entries can also have been explicitly set to None.
Expand Down Expand Up @@ -100,6 +100,9 @@ def _get_running_interactive_framework():
frame = frame.f_back
# Preemptively break reference cycle between locals and the frame.
del frame
macos = sys.modules.get("matplotlib.backends._macos")
if macos and macos.event_loop_is_running():
return "macos"
macosx = sys.modules.get("matplotlib.backends._macosx")
if macosx and macosx.event_loop_is_running():
return "macosx"
Expand Down
4 changes: 4 additions & 0 deletions src/macos/MPLAppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#import <AppKit/AppKit.h>

@interface MPLAppDelegate : NSObject <NSApplicationDelegate>
@end
8 changes: 8 additions & 0 deletions src/macos/MPLAppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import "MPLAppDelegate.h"


@implementation MPLAppDelegate
- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app {
return YES;
}
@end
37 changes: 37 additions & 0 deletions src/macos/MPLFigureCanvas.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#import <AppKit/AppKit.h>
#import <Python.h>

/* Keep track of the current mouse up/down state for open/closed cursor hand */
extern bool mpl_leftMouseGrabbing;

@interface MPLFigureCanvas : NSView <NSWindowDelegate>
{ NSRect rubberband;
@public double device_scale;
}
- (void)drawRect:(NSRect)rect;
- (void)updateDevicePixelRatio:(double)scale;
- (void)windowDidChangeBackingProperties:(NSNotification*)notification;
- (void)windowDidResize:(NSNotification*)notification;
- (instancetype)initWithFrame:(NSRect)rect;
- (void)mouseEntered:(NSEvent*)event;
- (void)mouseExited:(NSEvent*)event;
- (void)mouseDown:(NSEvent*)event;
- (void)mouseUp:(NSEvent*)event;
- (void)mouseDragged:(NSEvent*)event;
- (void)mouseMoved:(NSEvent*)event;
- (void)rightMouseDown:(NSEvent*)event;
- (void)rightMouseUp:(NSEvent*)event;
- (void)rightMouseDragged:(NSEvent*)event;
- (void)otherMouseDown:(NSEvent*)event;
- (void)otherMouseUp:(NSEvent*)event;
- (void)otherMouseDragged:(NSEvent*)event;
- (void)setRubberband:(NSRect)rect;
- (void)removeRubberband;
- (NSString*)convertKeyEvent:(NSEvent*)event;
- (void)keyDown:(NSEvent*)event;
- (void)keyUp:(NSEvent*)event;
- (void)scrollWheel:(NSEvent *)event;
- (BOOL)acceptsFirstResponder;
- (void)flagsChanged:(NSEvent*)event;
@property (nonatomic, assign) PyObject *pyObject;
@end
Loading