Skip to content

Commit

Permalink
Use native menubar on macOS, not elsewhere (i.e. Linux) (#695)
Browse files Browse the repository at this point in the history
* Use native menubar on macOS, not elsewhere (i.e. Linux)

* Add menu roles for better mac menus

Adding "menuRole" properties to standard menu actions (About,
Preferences, Quit) lets Qt move them to their standard place in the
macOS menu bar (without having to rely on their text content, which
breaks when localizing). This also automatically sets the "Preferences"
shortcut to the standard "Cmd+," instead of "Cmd+P".

This should have no impact on other platforms.

See https://doc.qt.io/qt-6/qmenubar.html#qmenubar-as-a-global-menu-bar
  • Loading branch information
jonathanperret authored Aug 13, 2024
1 parent 889c638 commit a03774e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/main/python/main/ayab/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# https://github.com/AllYarnsAreBeautiful/ayab-desktop

from __future__ import annotations
from PySide6.QtCore import QOperatingSystemVersion
from PySide6.QtWidgets import QMenuBar

from .menu_gui import Ui_MenuBar
Expand All @@ -38,6 +39,11 @@ class Menu(QMenuBar):

def __init__(self, parent: GuiMain):
super().__init__(parent)

# Use native menubar on macOS, not elsewhere (i.e. Linux)
if QOperatingSystemVersion.currentType() != QOperatingSystemVersion.OSType.MacOS:
self.setNativeMenuBar(False)

self.ui = Ui_MenuBar()
self.ui.setupUi(self)
self.setup()
Expand Down
12 changes: 9 additions & 3 deletions src/main/python/main/ayab/menu_gui.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
<ui version="4.0">
<class>MenuBar</class>
<widget class="QMenuBar" name="menubar">
<property name="nativeMenuBar">
<bool>False</bool>
</property>
<widget class="QMenu" name="menu_file">
<property name="title">
<string>File</string>
Expand Down Expand Up @@ -68,6 +65,9 @@
<property name="shortcut">
<string notr="true">Ctrl+Q</string>
</property>
<property name="menuRole">
<enum>QAction::QuitRole</enum>
</property>
</action>
<action name="action_load_AYAB_firmware">
<property name="text">
Expand Down Expand Up @@ -99,6 +99,9 @@
<property name="text">
<string>Help – About</string>
</property>
<property name="menuRole">
<enum>QAction::AboutRole</enum>
</property>
</action>
<action name="action_help">
<property name="text">
Expand Down Expand Up @@ -192,6 +195,9 @@
<property name="shortcut">
<string notr="true">Ctrl+P</string>
</property>
<property name="menuRole">
<enum>QAction::PreferencesRole</enum>
</property>
</action>
</widget>
<resources/>
Expand Down

0 comments on commit a03774e

Please sign in to comment.