"""Round 101: dark mode, a tint per mode, and the toolbar configurator.""" from PyQt6.QtGui import QColor, QPalette from PyQt6.QtWidgets import QApplication from lintunes import theme from lintunes.preferences import Preferences def _luma(color: QColor) -> float: return 0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue() def test_dark_mode_turns_every_surface_dark_and_text_light(qapp, tmp_path): prefs = Preferences(tmp_path) prefs.set("dark_mode", True) try: theme.apply_theme(qapp, prefs) palette = QApplication.palette() for role in (QPalette.ColorRole.Window, QPalette.ColorRole.Base, QPalette.ColorRole.AlternateBase, QPalette.ColorRole.Button): assert _luma(palette.color(role)) < 90, role for role in (QPalette.ColorRole.Text, QPalette.ColorRole.WindowText, QPalette.ColorRole.ButtonText): assert _luma(palette.color(role)) > 180, role assert palette.color(QPalette.ColorRole.Highlight) == theme.highlight_color(prefs) assert theme.is_dark() finally: theme.apply_theme(qapp, Preferences(tmp_path / "light")) assert not theme.is_dark() def test_each_mode_keeps_its_own_grays(tmp_path): prefs = Preferences(tmp_path) prefs.set("color_background", 200) prefs.set("dark_color_background", 20) assert theme.gray_value(prefs, "color_background") == 200 prefs.set("dark_mode", True) assert theme.gray_value(prefs, "color_background") == 20 assert theme.gray_value(prefs, "color_stripe") == theme.DARK_DEFAULTS["color_stripe"] assert theme.gray_value(prefs, "color_background", "light") == 200 def test_a_tint_keeps_the_gray_lightness(tmp_path): prefs = Preferences(tmp_path) prefs.set("dark_mode", True) prefs.set("dark_tint_color", "#ff0000") prefs.set("dark_tint_level", 50) color = theme.surface(prefs, "color_background") plain = theme.gray(theme.gray_value(prefs, "color_background")) assert abs(color.lightnessF() - plain.lightnessF()) < 0.01 assert color.red() > color.green() == color.blue() prefs.set("dark_tint_level", 0) assert theme.surface(prefs, "color_background") == plain def test_untouched_light_mode_keeps_the_base_palette(qapp, tmp_path): theme.apply_theme(qapp, Preferences(tmp_path)) assert (QApplication.palette().color(QPalette.ColorRole.Window) == theme._base().color(QPalette.ColorRole.Window)) def _dialog(tmp_path): from lintunes.gui.preferences_dialog import PreferencesDialog from tests.test_round40 import _FakeLastFm prefs = Preferences(tmp_path) return prefs, PreferencesDialog(prefs, _FakeLastFm()) def test_switching_the_edited_mode_moves_the_sliders(qapp, tmp_path): prefs, dialog = _dialog(tmp_path) slider = dialog._color_sliders["color_background"] slider.setValue(222) assert prefs.get("color_background") == 222 dialog._color_mode_buttons["dark"].setChecked(True) assert slider.value() == theme.DARK_DEFAULTS["color_background"] slider.setValue(11) assert prefs.get("dark_color_background") == 11 assert prefs.get("color_background") == 222 # light untouched dialog._color_mode_buttons["light"].setChecked(True) assert slider.value() == 222 dialog._tint_slider.setValue(40) assert prefs.get("tint_level") == 40 assert prefs.get("dark_tint_level") == 0 dialog.deleteLater() def test_the_dark_mode_box_follows_the_pref(qapp, tmp_path): prefs, dialog = _dialog(tmp_path) dialog._dark_check.setChecked(True) assert prefs.get("dark_mode") is True assert dialog._color_mode_buttons["dark"].isChecked() prefs.set("dark_mode", False) # e.g. the toolbar's button assert not dialog._dark_check.isChecked() dialog.deleteLater() theme.apply_theme(qapp, Preferences(tmp_path / "light"))