diff --git a/CLAUDE.md b/CLAUDE.md index 8caba48..e62b096 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -259,6 +259,12 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal name matches (with all it holds) or something inside does, opened while filtering, and clearing restores the folders the user had open — so `rebuild` reads the pre-filter expansion while a filter is up. + **The menu bar can hide until Alt** (Round 116, `gui/menu_bar.py`, + Preferences › Appearance, pref `menu_bar`: `always` | `alt`): a tap of Alt + shows it until a menu closes, Esc, a click elsewhere or another tap; + Alt+ opens that menu straight away. Qt disables a hidden menu + bar's shortcuts with it, so `register_shortcuts` adds every menu action + to the window too — an action added to a menu later must be as well. `track_table.py` is the shared track grid (drag/drop, copy/paste, drop indicator). `playlist_ops.py::add_tracks_with_dup_check` is the single funnel for every add-to-playlist path. `theme.py` applies the palette (highlight diff --git a/lintunes/__init__.py b/lintunes/__init__.py index 44bae18..23c10a3 100644 --- a/lintunes/__init__.py +++ b/lintunes/__init__.py @@ -1,3 +1,3 @@ """LinTunes — iTunes-style music library manager and player for Linux.""" -__version__ = "0.83.0" +__version__ = "0.84.0" diff --git a/lintunes/gui/main_window.py b/lintunes/gui/main_window.py index 84b6110..1d133f4 100644 --- a/lintunes/gui/main_window.py +++ b/lintunes/gui/main_window.py @@ -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) diff --git a/lintunes/gui/menu_bar.py b/lintunes/gui/menu_bar.py new file mode 100644 index 0000000..a93c829 --- /dev/null +++ b/lintunes/gui/menu_bar.py @@ -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+ 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 diff --git a/lintunes/gui/preferences_dialog.py b/lintunes/gui/preferences_dialog.py index b793153..fb80155 100644 --- a/lintunes/gui/preferences_dialog.py +++ b/lintunes/gui/preferences_dialog.py @@ -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). diff --git a/lintunes/preferences.py b/lintunes/preferences.py index d8141f1..65070c1 100644 --- a/lintunes/preferences.py +++ b/lintunes/preferences.py @@ -48,6 +48,7 @@ DEFAULTS = { # Dark mode (Round 101): the same sliders again under dark_, 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", diff --git a/tests/test_round116.py b/tests/test_round116.py new file mode 100644 index 0000000..3076ecf --- /dev/null +++ b/tests/test_round116.py @@ -0,0 +1,106 @@ +"""Round 116: Preferences › Appearance can hide the menu bar until Alt.""" +import pytest +from PyQt6.QtCore import Qt +from PyQt6.QtTest import QTest +from PyQt6.QtWidgets import QApplication + +from lintunes.gui import menu_bar + +ALT = Qt.KeyboardModifier.AltModifier + + +@pytest.fixture +def window(qapp, tmp_path): + from lintunes.gui.main_window import MainWindow + from lintunes.library_manager import LibraryManager + from lintunes.models.library import Library + from lintunes.preferences import Preferences + prefs = Preferences(tmp_path / "data") + prefs.set("menu_bar", "alt") + w = MainWindow(LibraryManager(Library(), tmp_path / "data"), prefs) + w.show() + w.activateWindow() + qapp.processEvents() + yield w + QApplication.instance().removeEventFilter(w._menu_bar) + w.close() + + +def _tap_alt(qapp, widget): + QTest.keyPress(widget, Qt.Key.Key_Alt, ALT) + QTest.keyRelease(widget, Qt.Key.Key_Alt, Qt.KeyboardModifier.NoModifier) + qapp.processEvents() + + +def test_mnemonic(): + assert menu_bar._mnemonic("&File") == "f" + assert menu_bar._mnemonic("Tom && &Jerry") == "j" + assert menu_bar._mnemonic("Plain") == "" + + +def test_hidden_until_alt_and_a_second_tap_hides_it(qapp, window): + bar = window.menuBar() + assert not bar.isVisible() + _tap_alt(qapp, window) + assert bar.isVisible() + _tap_alt(qapp, window) + assert not bar.isVisible() + assert QApplication.focusWidget() is not bar # no keyboard mode left behind + + +def test_alt_with_another_key_is_not_a_tap(qapp, window): + QTest.keyPress(window, Qt.Key.Key_Alt, ALT) + QTest.keyClick(window, Qt.Key.Key_Tab, ALT) + QTest.keyRelease(window, Qt.Key.Key_Alt, Qt.KeyboardModifier.NoModifier) + qapp.processEvents() + assert not window.menuBar().isVisible() + + +def test_escape_hides_a_peeking_bar(qapp, window): + _tap_alt(qapp, window) + QTest.keyClick(window, Qt.Key.Key_Escape) + qapp.processEvents() + assert not window.menuBar().isVisible() + + +def test_alt_letter_opens_that_menu_and_closing_it_hides_the_bar(qapp, window): + bar = window.menuBar() + QTest.keyClick(window, Qt.Key.Key_F, ALT) + qapp.processEvents() + assert bar.isVisible() + assert bar.activeAction().text() == "&File" + bar.activeAction().menu().hide() + for _ in range(3): + qapp.processEvents() + assert not bar.isVisible() + + +def test_shortcuts_still_work_with_the_bar_hidden(qapp, window): + hits = [] + window._dark_action.toggled.connect(hits.append) + QTest.keyClick(window, Qt.Key.Key_D, + Qt.KeyboardModifier.ControlModifier | Qt.KeyboardModifier.ShiftModifier) + qapp.processEvents() + assert hits == [True] + + +def test_the_pref_applies_live(qapp, window): + window._prefs.set("menu_bar", "always") + assert window.menuBar().isVisible() + _tap_alt(qapp, window) # Alt does nothing of ours now + assert window.menuBar().isVisible() + window._prefs.set("menu_bar", "alt") + assert not window.menuBar().isVisible() + + +def test_preferences_checkbox_writes_the_pref(qapp, tmp_path): + from lintunes.gui.preferences_dialog import PreferencesDialog + from lintunes.preferences import Preferences + from tests.test_round40 import _FakeLastFm + prefs = Preferences(tmp_path) + dialog = PreferencesDialog(prefs, _FakeLastFm()) + assert not dialog._menu_bar_check.isChecked() + dialog._menu_bar_check.setChecked(True) + assert prefs.get("menu_bar") == "alt" + dialog._menu_bar_check.setChecked(False) + assert prefs.get("menu_bar") == "always"