Source code for spacr.qt.menus
"""Assign explicit Qt menu roles, and name the macOS application menu.
On macOS, Qt may relocate actions according to text such as "Preferences" or
"Quit". Explicit roles prevent translated or renamed actions from moving to
the wrong system-menu slot. Other platforms retain their normal menu layout.
The menu those actions are relocated INTO is named after the application, so
this module also owns naming the application -- before the ``QApplication``
exists, which is the only moment early enough for macOS to read it.
"""
from __future__ import annotations
import os
import sys
from typing import Any, Iterable, Optional, Tuple
#: What the application calls itself everywhere a platform asks.
APPLICATION_NAME = "spaCR"
#: Used by ``QSettings`` for the preferences path, so it must not drift.
ORGANIZATION_NAME = "Olafsson Lab"
#: Set to ``1`` to skip the macOS bundle-name patch below without a code
#: change, should it ever misbehave on a future macOS.
BUNDLE_NAME_OPT_OUT = "SPACR_NO_MAC_APP_NAME"
[docs]
def name_the_application(name: str = APPLICATION_NAME,
organization: str = ORGANIZATION_NAME
) -> Tuple[str, str]:
"""Configure the Qt application and organization names.
Call this function before constructing ``QApplication`` so the macOS
application menu and Qt window-title fallbacks receive the configured
name.
:param name: Application and display name.
:param organization: Organization name used by ``QSettings``.
:returns: Effective ``(applicationName, applicationDisplayName)`` values.
"""
name_the_macos_application_menu(name)
try:
from PySide6.QtCore import QCoreApplication
from PySide6.QtGui import QGuiApplication
except Exception: # pragma: no cover - headless
return (name, name)
QCoreApplication.setApplicationName(name)
QCoreApplication.setOrganizationName(organization)
QGuiApplication.setApplicationDisplayName(name)
return (QCoreApplication.applicationName(),
QGuiApplication.applicationDisplayName())