Repository navigation
Description
Summary
A longstanding issue that I've had with matplotlib is the lack of support for standard macOS keyboard shortcuts:
| Command | Shortcut |
|---|---|
| Quit | Command-Q |
| Minimize window | Command-M |
| Switch windows | Command-Grave Shift-Command-Grave |
| Hide all windows | Command-H |
| Hide other windows | Command-Shift-H |
And when dealing with text input (the save dialog):
| Command | Shortcut |
|---|---|
| Cut | Command-X |
| Copy | Command-C |
| Paste | Command-V |
| Select All | Command-A |
| Undo | Command-Z |
| Redo | Shift-Command-Z |
In addition, macOS Sequoia also added several keyboard shortcuts for dealing with tiling window management.
On macOS, most of these shortcuts are driven by menu items in the main menu bar. The matplotlib macOS backend sets up the shared NSApplication instance when it calls +[NSApplication sharedInstance]; however, it never fills the NSApplication.mainMenu property with a main menu.
Similarly, as NSApplication.windowsMenu is never set, macOS doesn't add the applicable menu items or shortcuts for dealing with the tiling window manager.
I'd like to address this by creating a main menu when the macOS backend is used.
There are several ways to solve this, and I'd like some guidance so I don't step on any toes :)
-
I can create the main menu programmatically in
_macosx.m. This is likely the most straightforward approach, but it would involve a lot of hardcoded strings for menu titles in the.mfile and ultimately lack localization support.From an architectural perspective, it seems like existing titles/tooltips are hardcoded at the Python layer, shipped to the backend, which then converts it into UI buttons. That's doable as well, but is a lot of complexity.
-
Create a standard
MainMenu.xibfile that would be edited as a standalone file with Xcode. Invokeibtoolfrom the build system to generate a.nibfile, package that at a similar level to existing image files, then load this from_macosx.m. This would require maintainers to use the Xcode app for editing the main menu, which might be a deal-breaker for some. -
Create a new Xcode project for macOS UI assets. For now, this would be the main menu xib. In the future, it could be localized UI strings, images, icons, etc. Have the project generate a
.bundlewith compiled.nibs/.car/.strings/ etc.This is likely overkill for now, but could simplify asset creation in the future as Apple pushes for even more UI variants to apps (light / dark / tinted / glass / sigh). Xcode can generate those variants automatically and also generate compatible versions for previous macOS releases.
I'm fine with any of these approaches.
There's also the approach of doing all of this without a main menu, but I think that would be harder to maintain in the future as it's not what macOS expects.
Proposed fix
No response
Activity
timhoffm commentedon May 30, 2026
I think 1. would be the way to go. While changes will always require a Mac to test, maintainers may not be familiar with Xcode and making this a hard requirement increases the barrier for working on that part of the code.
iccir commentedon Jun 27, 2026
Fixed in my
macosx-reworkbranch but requires more testing.Note that we should not set
mainMenuunless we have explicitly created the NSApplication's delegate (nor should we setdelegateif one is already set). There exists a possibility that we are being called as a library/framework in an application that already has a delegate - we don't want to clobber the main menu in this case.