v0.70.0: customize the toolbar — drag items in and out, resize, fill or exact size; a light/dark button
This commit is contained in:
@@ -93,3 +93,164 @@ def test_the_dark_mode_box_follows_the_pref(qapp, tmp_path):
|
||||
assert not dialog._dark_check.isChecked()
|
||||
dialog.deleteLater()
|
||||
theme.apply_theme(qapp, Preferences(tmp_path / "light"))
|
||||
|
||||
|
||||
# ---- the toolbar configurator ----
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from PyQt6.QtCore import QPoint, QPointF, Qt
|
||||
from PyQt6.QtGui import QMouseEvent
|
||||
from PyQt6.QtCore import QEvent
|
||||
|
||||
from lintunes.gui import toolbar
|
||||
from lintunes.gui.toolbar import Slot, lay_out
|
||||
|
||||
|
||||
def test_normalize_keeps_what_must_stay_and_drops_junk():
|
||||
rows = toolbar.normalize([[{"id": "shuffle"}, {"id": "shuffle"}, {"id": "nope"},
|
||||
{"id": "library"}, "space:1"], []])
|
||||
assert [e["id"] for e in rows[0]] == ["shuffle", "space:1"]
|
||||
# library may only sit in row 1, and it and the view can't be removed
|
||||
assert {e["id"] for e in rows[1]} == {"library", "view"}
|
||||
assert toolbar.normalize("garbage") == toolbar.default_config()
|
||||
|
||||
|
||||
def test_move_remove_and_sizes():
|
||||
rows = toolbar.default_config()
|
||||
rows = toolbar.move(rows, "dj", 0, 0)
|
||||
assert rows[0][0]["id"] == "dj"
|
||||
rows = toolbar.move(rows, "theme", 1, 2)
|
||||
assert [e["id"] for e in rows[1]][:3] == ["library", "view", "theme"]
|
||||
assert toolbar.move(rows, "library", 0, 0) == rows # row 1 only
|
||||
assert toolbar.remove(rows, "view") == rows # required
|
||||
rows = toolbar.remove(rows, "theme")
|
||||
assert toolbar.find(rows, "theme") is None
|
||||
rows = toolbar.set_size(rows, "visualizer", "exact", 200)
|
||||
entry = rows[0][toolbar.find(rows, "visualizer")[1]]
|
||||
assert entry == {"id": "visualizer", "size": "exact", "width": 200}
|
||||
rows = toolbar.set_size(rows, "visualizer", "fill")
|
||||
assert "width" not in rows[0][toolbar.find(rows, "visualizer")[1]]
|
||||
|
||||
|
||||
def test_spacers_are_numbered_and_always_offered():
|
||||
rows = toolbar.default_config()
|
||||
first = toolbar.new_instance(rows, "space")
|
||||
rows = toolbar.move(rows, first, 0, 1)
|
||||
assert first == "space:1"
|
||||
assert toolbar.new_instance(rows, "space") == "space:2"
|
||||
assert "space" in toolbar.palette_ids(rows)
|
||||
assert "theme" in toolbar.palette_ids(rows)
|
||||
assert "shuffle" not in toolbar.palette_ids(rows)
|
||||
|
||||
|
||||
def test_lay_out_knolls_and_shares_fill():
|
||||
spots = lay_out([Slot("a", width=50), Slot("b", fill=True), Slot("c", width=30),
|
||||
Slot("d", fill=True)], 400, 10, 10)
|
||||
assert spots["a"] == (10, 50)
|
||||
# 400 - 2*10 margins - 80 fixed - 3*10 gaps = 270, split 135/135
|
||||
assert spots["b"] == (70, 135) and spots["d"][1] == 135
|
||||
assert spots["d"][0] + spots["d"][1] == 390
|
||||
|
||||
|
||||
def test_lay_out_anchors_and_falls_back():
|
||||
spots = lay_out([Slot("lib", anchor=(8, 200)), Slot("view", fill=True, start=208),
|
||||
Slot("seek", anchor=(500, 900))], 1000, 8, 6)
|
||||
assert spots["lib"] == (8, 192)
|
||||
assert spots["view"] == (208, 500 - 6 - 208)
|
||||
assert spots["seek"] == (500, 400)
|
||||
# an anchor the items before it overrun is just a fixed slot after them
|
||||
spots = lay_out([Slot("big", width=600), Slot("seek", anchor=(100, 300))], 1000, 0, 10)
|
||||
assert spots["seek"] == (610, 200)
|
||||
|
||||
|
||||
def _bar(tmp_path, rows=None):
|
||||
from lintunes.gui.transport import TransportBar
|
||||
player = MagicMock()
|
||||
player.repeat_mode.return_value = "off"
|
||||
prefs = Preferences(tmp_path)
|
||||
if rows is not None:
|
||||
prefs.set("toolbar", rows)
|
||||
bar = TransportBar(player, MagicMock(), prefs)
|
||||
bar.resize(1280, 140)
|
||||
bar.show()
|
||||
QApplication.processEvents()
|
||||
return bar, prefs
|
||||
|
||||
|
||||
def test_dropping_an_item_in_adds_it_and_saves(qapp, tmp_path):
|
||||
bar, prefs = _bar(tmp_path)
|
||||
assert not bar._theme_btn.parentWidget().isVisible()
|
||||
bar.drop_item("theme", 0, 3)
|
||||
QApplication.processEvents()
|
||||
assert [e["id"] for e in prefs.get("toolbar")[0]][3] == "theme"
|
||||
box = bar._theme_btn.parentWidget()
|
||||
assert box.isVisible() and box.parentWidget() is bar._top_row
|
||||
shuffle = bar._shuffle_btn.parentWidget().geometry()
|
||||
assert box.geometry().left() > shuffle.right()
|
||||
bar.remove_item("theme")
|
||||
assert not box.isVisible()
|
||||
|
||||
|
||||
def test_the_theme_button_flips_dark_mode(qapp, tmp_path):
|
||||
bar, prefs = _bar(tmp_path)
|
||||
bar._theme_btn.click()
|
||||
assert prefs.get("dark_mode") is True
|
||||
bar._theme_btn.click()
|
||||
assert prefs.get("dark_mode") is False
|
||||
|
||||
|
||||
def test_an_exact_width_and_fill(qapp, tmp_path):
|
||||
bar, prefs = _bar(tmp_path)
|
||||
bar._save_toolbar(toolbar.set_size(bar._rows, "visualizer", "exact", 300))
|
||||
assert bar._visualizer.width() == 300
|
||||
panel = bar._now_playing.width()
|
||||
bar._save_toolbar(toolbar.set_size(bar._rows, "volume", "fill"))
|
||||
# two fills now share what the fixed items leave
|
||||
assert abs(bar._volume_slider.width() - bar._now_playing.width()) <= 1
|
||||
assert bar._now_playing.width() < panel
|
||||
|
||||
|
||||
def _mouse(widget, kind, pos, button=Qt.MouseButton.LeftButton):
|
||||
event = QMouseEvent(kind, QPointF(pos), QPointF(widget.mapToGlobal(pos)), button,
|
||||
button if kind != QEvent.Type.MouseButtonRelease
|
||||
else Qt.MouseButton.NoButton,
|
||||
Qt.KeyboardModifier.NoModifier)
|
||||
QApplication.sendEvent(widget, event)
|
||||
|
||||
|
||||
def test_customize_mode_resizes_by_the_handle(qapp, tmp_path):
|
||||
bar, prefs = _bar(tmp_path)
|
||||
bar.set_customizing(True)
|
||||
QApplication.processEvents()
|
||||
assert bar.is_customizing() and len(bar._overlays) == 2
|
||||
overlay = bar._overlays[0]
|
||||
rect = bar._rects[0]["visualizer"]
|
||||
start = QPoint(rect.right() - 1, rect.center().y())
|
||||
_mouse(overlay, QEvent.Type.MouseButtonPress, start)
|
||||
_mouse(overlay, QEvent.Type.MouseMove, start + QPoint(40, 0))
|
||||
_mouse(overlay, QEvent.Type.MouseButtonRelease, start + QPoint(40, 0))
|
||||
entry = prefs.get("toolbar")[0][toolbar.find(bar._rows, "visualizer")[1]]
|
||||
assert entry["size"] == "exact" and entry["width"] == rect.width() + 40
|
||||
assert bar._visualizer.width() == rect.width() + 40
|
||||
# the overlay takes the clicks, so a button under it isn't pressed
|
||||
played = []
|
||||
bar.play_clicked.connect(lambda: played.append(1))
|
||||
play = bar._play_btn.mapTo(bar._top_row, bar._play_btn.rect().center())
|
||||
_mouse(overlay, QEvent.Type.MouseButtonPress, play)
|
||||
_mouse(overlay, QEvent.Type.MouseButtonRelease, play)
|
||||
assert played == []
|
||||
bar.set_customizing(False)
|
||||
QApplication.processEvents()
|
||||
assert bar._overlays == [] and bar._panel is None
|
||||
|
||||
|
||||
def test_restore_defaults(qapp, tmp_path):
|
||||
rows = toolbar.remove(toolbar.default_config(), "shuffle")
|
||||
bar, prefs = _bar(tmp_path, rows)
|
||||
assert not bar._shuffle_btn.parentWidget().isVisible()
|
||||
bar.set_customizing(True)
|
||||
bar._panel.restore.emit()
|
||||
assert prefs.get("toolbar") == toolbar.default_config()
|
||||
assert bar._shuffle_btn.parentWidget().isVisible()
|
||||
bar.set_customizing(False)
|
||||
|
||||
@@ -180,7 +180,10 @@ def test_dj_button_up_top_bpm_below_and_a_tall_volume(qapp, tmp_path):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
bar = TransportBar(player, MagicMock(), Preferences(Path(d)))
|
||||
assert bar.dj_button.parentWidget() is bar._dj_box
|
||||
assert bar._dj_box in bar._fixed_controls
|
||||
bar.resize(1280, 120)
|
||||
bar._place_header() # what a resize does
|
||||
assert bar._header_row.right is bar._bpm_button
|
||||
assert bar._dj_box.parentWidget() is bar._top_row
|
||||
assert bar._bpm_button.parentWidget() is bar._header_row
|
||||
dj, bpm = bar._dj_box.geometry(), bar._bpm_button.geometry()
|
||||
assert (bpm.left(), bpm.width()) == (dj.left(), dj.width())
|
||||
assert bar._volume_slider.property("tall") is True
|
||||
|
||||
Reference in New Issue
Block a user