forked from trav/lintunes
v0.84.0: Preferences › Appearance can hide the menu bar until Alt is pressed
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
"""LinTunes — iTunes-style music library manager and player for Linux."""
|
||||
|
||||
__version__ = "0.83.0"
|
||||
__version__ = "0.84.0"
|
||||
|
||||
@@ -31,7 +31,7 @@ from lintunes.gui.album_art_dialog import AlbumArtDialog
|
||||
from lintunes.gui.art_ops import embed_artwork
|
||||
from lintunes.gui.cast_dialog import AirPlayDialog, ChromecastDialog
|
||||
from lintunes.gui.cassette_ui import CassetteUi
|
||||
from lintunes.gui import sounds
|
||||
from lintunes.gui import menu_bar, sounds
|
||||
from lintunes.gui.device_sync_dialog import (
|
||||
DeviceSyncSettingsDialog, resolve_sync_playlists,
|
||||
)
|
||||
@@ -397,6 +397,8 @@ class MainWindow(QMainWindow):
|
||||
lambda msg: self.statusBar().showMessage(msg, 4000))
|
||||
|
||||
self._build_menus()
|
||||
menu_bar.register_shortcuts(self, self.menuBar())
|
||||
self._menu_bar = menu_bar.AltMenuBar(self, self._prefs)
|
||||
self.apply_ui_metrics()
|
||||
self._update_totals()
|
||||
|
||||
@@ -447,6 +449,8 @@ class MainWindow(QMainWindow):
|
||||
self._transport.refresh_theme()
|
||||
self._version_button.refresh_theme()
|
||||
self._sidebar.refresh_style()
|
||||
if getattr(self, "_menu_bar", None) is not None:
|
||||
self._menu_bar.apply()
|
||||
dark = getattr(self, "_dark_action", None)
|
||||
if dark is not None and dark.isChecked() != (theme.mode(self._prefs) == "dark"):
|
||||
dark.blockSignals(True)
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
"""The menu bar, optionally hidden until Alt (Round 116).
|
||||
|
||||
Preferences' `menu_bar` is "always" (the default) or "alt": hidden, and a tap
|
||||
of Alt shows it until it's done with — the convention Firefox and others
|
||||
follow on Linux. Alt+<letter> while it's hidden opens that menu at once.
|
||||
|
||||
Qt disables a hidden menu bar's shortcuts along with it, so every action in
|
||||
it is also added to the window (`register_shortcuts`), or Ctrl+N and the rest
|
||||
would stop working the moment the bar hides.
|
||||
"""
|
||||
from PyQt6.QtCore import QEvent, QObject, Qt, QTimer
|
||||
from PyQt6.QtWidgets import QApplication, QMenu
|
||||
|
||||
PREF = "menu_bar"
|
||||
ALWAYS = "always"
|
||||
ALT = "alt"
|
||||
|
||||
|
||||
def hidden_by_pref(prefs) -> bool:
|
||||
return prefs.get(PREF, ALWAYS) == ALT
|
||||
|
||||
|
||||
def register_shortcuts(window, bar):
|
||||
"""Add every action in `bar`'s menus, submenus included, to `window`."""
|
||||
def walk(menu):
|
||||
for action in menu.actions():
|
||||
if action.menu() is not None:
|
||||
walk(action.menu())
|
||||
elif not action.isSeparator():
|
||||
window.addAction(action)
|
||||
for top in bar.actions():
|
||||
if top.menu() is not None:
|
||||
walk(top.menu())
|
||||
|
||||
|
||||
def _mnemonic(text: str) -> str:
|
||||
"""The letter after a single '&' in a menu title, lower-cased, or ""."""
|
||||
i = 0
|
||||
while i < len(text) - 1:
|
||||
if text[i] == "&":
|
||||
if text[i + 1] != "&":
|
||||
return text[i + 1].lower()
|
||||
i += 1
|
||||
i += 1
|
||||
return ""
|
||||
|
||||
|
||||
class AltMenuBar(QObject):
|
||||
"""Shows and hides `window`'s menu bar per the pref; an app event filter
|
||||
while installed, acting only on keys and clicks in `window`."""
|
||||
|
||||
def __init__(self, window, prefs):
|
||||
super().__init__(window)
|
||||
self._window = window
|
||||
self._prefs = prefs
|
||||
self._armed = False # Alt is down and nothing else has been pressed
|
||||
self._peeking = False # shown by Alt while the pref hides it
|
||||
for top in window.menuBar().actions():
|
||||
if top.menu() is not None:
|
||||
top.menu().aboutToHide.connect(self._after_menu_closed)
|
||||
QApplication.instance().installEventFilter(self)
|
||||
self.apply()
|
||||
|
||||
@property
|
||||
def bar(self):
|
||||
return self._window.menuBar()
|
||||
|
||||
def apply(self):
|
||||
"""Match the pref (Preferences changed it, or at startup)."""
|
||||
if not hidden_by_pref(self._prefs):
|
||||
self._peeking = False
|
||||
self.bar.show()
|
||||
elif not self._peeking:
|
||||
self.bar.hide()
|
||||
|
||||
def peek(self):
|
||||
self._peeking = True
|
||||
self.bar.show()
|
||||
|
||||
def end_peek(self):
|
||||
if not self._peeking:
|
||||
return
|
||||
self._peeking = False
|
||||
self.bar.setActiveAction(None)
|
||||
if hidden_by_pref(self._prefs):
|
||||
self.bar.hide()
|
||||
|
||||
def _after_menu_closed(self):
|
||||
# Moving from File to Edit closes File first; look once the next one
|
||||
# has opened.
|
||||
QTimer.singleShot(0, self._end_peek_if_idle)
|
||||
|
||||
def _end_peek_if_idle(self):
|
||||
if self.bar.activeAction() is None and QApplication.activePopupWidget() is None:
|
||||
self.end_peek()
|
||||
|
||||
def _menu_for(self, key: int):
|
||||
if not Qt.Key.Key_A <= key <= Qt.Key.Key_Z:
|
||||
return None
|
||||
letter = chr(key).lower()
|
||||
for top in self.bar.actions():
|
||||
if top.menu() is not None and top.isEnabled() \
|
||||
and _mnemonic(top.text()) == letter:
|
||||
return top
|
||||
return None
|
||||
|
||||
def eventFilter(self, obj, event):
|
||||
kind = event.type()
|
||||
if not hidden_by_pref(self._prefs):
|
||||
return False
|
||||
if kind == QEvent.Type.WindowDeactivate and obj is self._window:
|
||||
if QApplication.activePopupWidget() is None:
|
||||
self.end_peek()
|
||||
return False
|
||||
if kind == QEvent.Type.MouseButtonPress:
|
||||
self._armed = False
|
||||
if self._peeking and isinstance(obj, QObject) \
|
||||
and QApplication.activePopupWidget() is None \
|
||||
and not self._inside_bar(obj):
|
||||
self.end_peek()
|
||||
return False
|
||||
if kind not in (QEvent.Type.KeyPress, QEvent.Type.KeyRelease) \
|
||||
or not self._window.isActiveWindow() \
|
||||
or isinstance(QApplication.activePopupWidget(), QMenu):
|
||||
return False
|
||||
key = event.key()
|
||||
if kind == QEvent.Type.KeyPress:
|
||||
if event.isAutoRepeat():
|
||||
return False
|
||||
if key == Qt.Key.Key_Alt:
|
||||
# Whether Alt's own press carries AltModifier differs by
|
||||
# platform; only other modifiers held disarm it.
|
||||
self._armed = not (event.modifiers() & ~Qt.KeyboardModifier.AltModifier)
|
||||
return False
|
||||
self._armed = False
|
||||
if key == Qt.Key.Key_Escape and self._peeking:
|
||||
self.end_peek()
|
||||
return True
|
||||
if event.modifiers() == Qt.KeyboardModifier.AltModifier:
|
||||
top = self._menu_for(key)
|
||||
if top is not None:
|
||||
self.peek()
|
||||
self.bar.setActiveAction(top)
|
||||
return True
|
||||
return False
|
||||
# KeyRelease
|
||||
if key == Qt.Key.Key_Alt and self._armed and not event.isAutoRepeat():
|
||||
self._armed = False
|
||||
if self._peeking:
|
||||
self.end_peek()
|
||||
else:
|
||||
self.peek()
|
||||
return True
|
||||
return False
|
||||
|
||||
def _inside_bar(self, obj) -> bool:
|
||||
while obj is not None:
|
||||
if obj is self.bar:
|
||||
return True
|
||||
obj = obj.parent()
|
||||
return False
|
||||
@@ -572,6 +572,15 @@ class PreferencesDialog(QDialog):
|
||||
# The toolbar's light/dark button flips the same pref while this is open.
|
||||
self._prefs.changed.connect(self._sync_dark_check)
|
||||
|
||||
self._menu_bar_check = QCheckBox("Hide the menu bar until Alt is pressed")
|
||||
self._menu_bar_check.setToolTip(
|
||||
"Tap Alt to show it, or press Alt and a menu's letter (Alt+F for "
|
||||
"File); shortcuts like Ctrl+N work either way")
|
||||
self._menu_bar_check.setChecked(self._prefs.get("menu_bar", "always") == "alt")
|
||||
self._menu_bar_check.toggled.connect(
|
||||
lambda on: self._prefs.set("menu_bar", "alt" if on else "always"))
|
||||
form.addRow("Menu bar:", self._menu_bar_check)
|
||||
|
||||
# Highlight color: a radio per slot, and beside it the slot's swatch,
|
||||
# which opens a full color picker; the color picked replaces that
|
||||
# slot's (right-click the swatch to put the original back).
|
||||
|
||||
@@ -48,6 +48,7 @@ DEFAULTS = {
|
||||
# Dark mode (Round 101): the same sliders again under dark_<key>, and a
|
||||
# tint per mode — a hue laid over the grays at tint_level percent.
|
||||
"dark_mode": False,
|
||||
"menu_bar": "always", # always | alt (hidden until Alt; gui/menu_bar.py)
|
||||
"tint_color": "#3584E4",
|
||||
"tint_level": 0,
|
||||
"dark_tint_color": "#3584E4",
|
||||
|
||||
Reference in New Issue
Block a user