diff --git a/lintunes/__init__.py b/lintunes/__init__.py index 23c10a3..ba65ef6 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.84.0" +__version__ = "0.84.1" diff --git a/lintunes/gui/toolbar.py b/lintunes/gui/toolbar.py index 6ab2e1d..baf2f25 100644 --- a/lintunes/gui/toolbar.py +++ b/lintunes/gui/toolbar.py @@ -113,6 +113,48 @@ def shown_ids(rows) -> set[str]: return {e["id"] for row in rows for e in row} +def _fills(entry) -> bool: + size, base = entry.get("size", "auto"), base_id(entry["id"]) + return size == "fill" or (size == "auto" and (base in AUTO_FILL or base == "seek")) + + +def column_partners(rows) -> dict[str, str]: + """{row-1 item: row-0 item} it sits exactly under. ``ANCHORED_UNDER`` + first; then the buttons ending row 1 pair off, right to left, with the + buttons ending row 0, so a theme box over a speakers box lines up like + DJ over bpm. Only "auto" buttons in row 1 join (an exact width is the + user's call); a spacer or anything that fills ends either run.""" + top, bottom = rows + shown = {e["id"] for e in top} + partners = {e["id"]: ANCHORED_UNDER[base_id(e["id"])] for e in bottom + if base_id(e["id"]) in ANCHORED_UNDER + and e.get("size", "auto") == "auto" + and ANCHORED_UNDER[base_id(e["id"])] in shown} + + def run(row, ok): + out = [] + for entry in reversed(row): + if base_id(entry["id"]) in SPACERS or _fills(entry) or not ok(entry): + break + out.append(entry["id"]) + return out + + below = run(bottom, lambda e: e.get("size", "auto") == "auto" + and base_id(e["id"]) not in ("library", "view")) + above = run(top, lambda e: True) + for low, high in zip(below, above): + partners[low] = high + # The pairs must keep row 0's order, or the anchors would cross. + order = {e["id"]: i for i, e in enumerate(top)} + last, kept = -1, {} + for entry in bottom: + high = partners.get(entry["id"]) + if high is not None and order[high] > last and high not in kept.values(): + kept[entry["id"]] = high + last = order[high] + return kept + + def palette_ids(rows) -> list[str]: """What the palette offers: everything not shown, and always both spacers (dragging one in makes a new instance).""" diff --git a/lintunes/gui/transport.py b/lintunes/gui/transport.py index fb32d32..dd91e37 100644 --- a/lintunes/gui/transport.py +++ b/lintunes/gui/transport.py @@ -874,6 +874,7 @@ class TransportBar(QWidget): """The layout engine's slots for ``row``. Row 1's automatic items follow what they belong under: the sidebar, the view, row 0.""" split = self._split() + partners = toolbar.column_partners(self._rows) slots = [] for entry in self._rows[row]: item, size = entry["id"], entry.get("size", "auto") @@ -885,7 +886,7 @@ class TransportBar(QWidget): if size == "fill": slots.append(Slot(item, fill=True, min_width=least)) continue - above = self._rects[0].get(toolbar.ANCHORED_UNDER.get(base, "")) + above = self._rects[0].get(partners.get(item, "")) if row == 1 and base == "library" and split is not None: slots.append(Slot(item, anchor=(split[0], split[1]), min_width=least)) elif row == 1 and above is not None: diff --git a/tests/test_round117.py b/tests/test_round117.py new file mode 100644 index 0000000..addbc69 --- /dev/null +++ b/tests/test_round117.py @@ -0,0 +1,52 @@ +"""Round 117: automatic buttons ending the second row line up, column by +column, under the buttons ending the first.""" +from lintunes.gui import toolbar +from tests.test_round101 import _bar + + +def _rows(top, bottom): + return [[e if isinstance(e, dict) else {"id": e, "size": "auto"} for e in top], + [e if isinstance(e, dict) else {"id": e, "size": "auto"} for e in bottom]] + + +def test_the_default_pairs_are_unchanged(): + assert toolbar.column_partners(toolbar.default_config()) == { + "seek": "now_playing", "bpm": "dj"} + + +def test_trailing_buttons_pair_right_to_left(): + rows = _rows(["transport", "now_playing", "theme", "dj"], + ["library", "view", "seek", "speakers", "bpm"]) + assert toolbar.column_partners(rows) == { + "seek": "now_playing", "speakers": "theme", "bpm": "dj"} + + +def test_exact_widths_and_spacers_end_the_run(): + rows = _rows(["now_playing", "theme", "dj"], + ["library", "view", "seek", + {"id": "speakers", "size": "exact", "width": 60}, "bpm"]) + assert toolbar.column_partners(rows) == {"seek": "now_playing", "bpm": "dj"} + rows = _rows(["now_playing", "theme", "space:1", "dj"], + ["library", "view", "seek", "speakers", "bpm"]) + assert toolbar.column_partners(rows) == {"seek": "now_playing", "bpm": "dj"} + + +def test_pairs_never_cross(): + # positionally speakers would sit under dj and bpm under theme; bpm's + # own partner (dj) wins and speakers, which would cross it, stays free + rows = _rows(["now_playing", "theme", "dj"], + ["library", "view", "seek", "bpm", "speakers"]) + partners = toolbar.column_partners(rows) + assert partners["seek"] == "now_playing" + assert list(partners.values()) == sorted( + partners.values(), key=["now_playing", "theme", "dj"].index) + + +def test_the_window_lines_them_up(qapp, tmp_path): + rows = _rows(["transport", "volume", "now_playing", "theme", "dj"], + ["library", "view", "seek", "speakers", "bpm"]) + bar, _prefs = _bar(tmp_path, rows) + top, bottom = bar._rects[0], bar._rects[1] + for low, high in (("speakers", "theme"), ("bpm", "dj"), ("seek", "now_playing")): + assert bottom[low].left() == top[high].left(), low + assert bottom[low].right() == top[high].right(), low