Lighting menu in the window for pads that have backlight modes

A backend that lists lighting_modes gets a Lighting button in the footer:
one entry per mode and a colour picker for the custom one. The tick
follows what the pad reported in its last read, since every read of an
ITPROTECH pad carries the lighting along with the keys.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
2026-10-23 19:37:44 +02:00
parent 4f33161c70
commit 625c95f09c
4 changed files with 197 additions and 3 deletions

View File

@@ -334,3 +334,82 @@ class Registry(unittest.TestCase):
if __name__ == "__main__":
unittest.main()
class Lighting(unittest.TestCase):
def setUp(self):
self.pad = FakePad()
self.backend = Itprotech()
patch = mock.patch.object(core, "_exchange", self.pad.exchange)
patch.start()
self.addCleanup(patch.stop)
def test_the_backend_offers_its_modes_to_the_gui(self):
self.assertEqual(len(self.backend.lighting_modes), 15)
self.assertEqual(self.backend.lighting_modes[self.backend.lighting_custom],
"custom colour")
def test_a_read_remembers_the_lighting(self):
dev = fake_device()
self.assertIsNone(self.backend.last_lighting(dev))
self.backend.read_layer(dev)
self.assertEqual(self.backend.last_lighting(dev), (12, (255, 255, 255)))
def test_setting_updates_what_is_remembered(self):
dev = fake_device()
self.backend.read_layer(dev)
self.backend.set_lighting(dev, 3, _write=self.pad.write_report)
self.assertEqual(self.backend.last_lighting(dev), (3, (255, 255, 255)))
self.backend.set_lighting(dev, wire.RGB_CUSTOM, (1, 2, 3),
_write=self.pad.write_report)
self.assertEqual(self.backend.last_lighting(dev), (14, (1, 2, 3)))
class LightingMenu(unittest.TestCase):
"""The GUI's Lighting menu, offscreen, against the fake pad."""
def test_the_menu_appears_for_this_pad_and_writes_to_it(self):
import os
try:
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
from PySide6.QtWidgets import QApplication
from macropad_gui import app
except ImportError:
self.skipTest("PySide6 not installed")
import macropad as proto
pad = FakePad()
dev = fake_device()
ready = core.Diagnosis("ready", f"Ready on {dev.path}.", device=dev)
with tempfile.TemporaryDirectory() as d, \
mock.patch.dict(os.environ, {"XDG_CONFIG_HOME": d}), \
mock.patch.object(core, "diagnose", return_value=ready), \
mock.patch.object(core, "_exchange", pad.exchange), \
mock.patch.object(proto, "write_report", pad.write_report):
QApplication.instance() or QApplication([])
w = app.Window()
w.timer.stop()
w._read(quiet=True)
self.assertFalse(w.light_btn.isHidden())
self.assertEqual(w.state.device_id, "8808:660a")
self.assertEqual(w.state.layout.describe(), "9 keys in 3x3")
# the pad said mode 12, so that one is ticked
self.assertTrue(w.light_actions[12].isChecked())
self.assertIn("#ffffff", w.light_custom.text())
# choosing a mode sends it, twice, and ticks it
w.light_actions[3].trigger()
sent = [r for r in pad.written if r[1:3] == bytes([2, wire.CMD_RGB])]
self.assertEqual([r[3] for r in sent], [3, 3])
self.assertEqual([a[3] for a in pad.acted if a[2] == wire.CMD_RGB], [3])
self.assertTrue(w.light_actions[3].isChecked())
self.assertIn("press: green", w.message.text())
# a custom colour chosen without the dialog goes out as mode 14 + colour
w._set_lighting(wire.RGB_CUSTOM, (0x10, 0x20, 0x30))
colours = [a for a in pad.acted if a[2] == wire.CMD_COLOUR]
self.assertEqual(colours[-1][3:6], bytes([0x20, 0x10, 0x30]))
self.assertTrue(w.light_custom.isChecked())
self.assertIn("#102030", w.light_custom.text())
# no pad with lighting: the button goes away
w.diag = core.Diagnosis("unplugged", "No pad connected.")
w._follow_device()
self.assertTrue(w.light_btn.isHidden())