Public repository: timo / schach

Reader

Ein aufgeräumtes Schach-Programm für GNOME

fix(movelist): new bar values redraw the bars instead of rebuilding the list

public

fix(movelist): new bar values redraw the bars instead of rebuilding the list

After loading a game, Maia's win/draw/loss arrived move by move and each
result rebuilt the whole move list: expanding and collapsing missed, the
hover highlight flickered. The list now rebuilds only when its rows change
(moves, glyphs, variations, times, notation); new bar values update the
bars and tooltips of the affected rows. Loading 30 moves: 30 rebuilds
before, none now. Reported by the user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Changeset78d4a10ba1aef46c8c563a8f93e56e53a33fd0fe
Author (claimed)Timo Stolz <timo.stolz@nullachtvierzehn.de>,
Parents340f2d51619d
Children2587af5e7e29
Pushlog
by timo via ssh
by timo via ssh

modified schach/movelist.py +40 −2

@@ -53,6 +53,21 @@ DRAW_RGB = (0.62, 0.62, 0.64) # wie im
5353
 BLACK_RGB = (0.17, 0.17, 0.18)
5454
 
5555
 
56
+def _shape(branches):
57
+    """Zweige ohne Balkenwerte: was sich davon ändert, verlangt neue Zeilen (siehe MoveList.update)."""
58
+    return tuple(tuple((token, prefix, san, _shape(subs), name) for token, prefix, san, subs, _wdl, name in branch)
59
+                 for branch in branches)
60
+
61
+
62
+def _branch_wdls(groups):
63
+    """(Nummer, Balkenwert) für alle Variantenzüge in ``groups`` (Listen von Zweigen)."""
64
+    for branches in groups:
65
+        for branch in branches:
66
+            for token, _prefix, _san, subs, wdl, _name in branch:
67
+                yield token, wdl
68
+                yield from _branch_wdls([subs])
69
+
70
+
5671
 def _wdl_text(wdl):
5772
     win, draw, loss = (round(x * 100) for x in wdl)
5873
     if wdl[1] == 0:  # Stockfish: ohne Remis
@@ -130,13 +145,35 @@ class MoveList(Gtk.Box):
130145
         ``times``: {Halbzug: ms} – Bedenkzeit, rechts in der Zeile."""
131146
         moves = list(board.move_stack)
132147
         times = times or {}
133
-        signature = (tuple(moves), tuple(sorted(wdls.items())), tuple(sorted(symbols.items())), self.notation,
134
-                     tuple(sorted(dict(variations).items())), tuple(sorted(times.items())))
148
+        # Neu aufgebaut wird nur, wenn sich die Zeilen selbst ändern; neue Balkenwerte (Maia liefert
149
+        # sie Zug für Zug) zeichnen nur die Balken neu, sonst wäre die Liste beim Laden unbenutzbar.
150
+        signature = (tuple(moves), tuple(sorted(symbols.items())), self.notation,
151
+                     tuple(sorted((ply, _shape(branches)) for ply, branches in dict(variations).items())),
152
+                     tuple(sorted(times.items())))
135153
         if signature != self._signature:
136154
             self._signature = signature
137155
             self._rebuild(board, moves, wdls, symbols, variations, times)
156
+        else:
157
+            self._update_bars(wdls, variations)
138158
         self._apply_selection(selected, selected_variation)
139159
 
160
+    def _update_bars(self, wdls, variations):
161
+        """Nur die Balken nachführen (Zeilen bleiben, samt Aufgeklapptem und Mauszeiger)."""
162
+        for ply, row in enumerate(self._rows):
163
+            self._set_wdl(row, wdls.get(ply))
164
+        for token, wdl in _branch_wdls(dict(variations).values()):
165
+            row = self._token_rows.get(token)
166
+            if row is not None:
167
+                self._set_wdl(row, wdl)
168
+
169
+    def _set_wdl(self, row, wdl):
170
+        if row.wdl == wdl:
171
+            return
172
+        row.wdl = wdl
173
+        row.bar.set_draw_func(self._draw_bar, wdl)
174
+        row.bar.queue_draw()
175
+        row.overlay.set_tooltip_text(_wdl_text(wdl) if wdl is not None else None)
176
+
140177
     def _rebuild(self, board, moves, wdls, symbols, variations=(), times=None):
141178
         times = times or {}
142179
         after = dict(variations)
@@ -215,6 +252,7 @@ class MoveList(Gtk.Box):
215252
         if wdl is not None:
216253
             overlay.set_tooltip_text(_wdl_text(wdl))
217254
         row = Gtk.ListBoxRow(child=overlay)
255
+        row.bar, row.overlay, row.wdl = bar, overlay, wdl
218256
         row.ply = ply
219257
         row.variation = token
220258
         row.pill = pill

added tests/test_movelist_bars.py +69 −0

@@ -0,0 +1,69 @@
1
+"""Zugliste: neue Balkenwerte (Maia, Stockfish) zeichnen nur die Balken neu und bauen die Liste
2
+nicht um. Braucht einen Bildschirm (sonst übersprungen), z. B. ``xvfb-run -a python -m pytest``."""
3
+
4
+import unittest
5
+
6
+import chess
7
+import gi
8
+
9
+gi.require_version("Gtk", "4.0")
10
+gi.require_version("Adw", "1")
11
+from gi.repository import Adw  # noqa: E402
12
+
13
+try:
14
+    Adw.init()
15
+    HAVE_DISPLAY = True
16
+except Exception:  # noqa: BLE001 – ohne Bildschirm
17
+    HAVE_DISPLAY = False
18
+
19
+if HAVE_DISPLAY:
20
+    from schach.movelist import MoveList
21
+
22
+
23
+def played(*sans):
24
+    board = chess.Board()
25
+    for san in sans:
26
+        board.push_san(san)
27
+    return board
28
+
29
+
30
+@unittest.skipUnless(HAVE_DISPLAY, "kein Bildschirm")
31
+class BarsTest(unittest.TestCase):
32
+    def setUp(self):
33
+        self.board = played("e4", "e5", "Nf3", "Nc6")
34
+        self.list = MoveList()
35
+
36
+    def rows(self):
37
+        return list(self.list._rows) + list(self.list._variation_rows)
38
+
39
+    def test_neuer_wert_baut_nicht_um(self):
40
+        """Nutzerbericht 24.11.2026: Beim Laden eines Spielstands kam Maias Wahrscheinlichkeit Zug für
41
+        Zug, und jedes Ergebnis baute die ganze Liste neu; Auf- und Zuklappen ging ins Leere, die
42
+        Hover-Markierung flackerte."""
43
+        self.list.update(self.board, {}, {}, None)
44
+        before = self.rows()
45
+        self.list.update(self.board, {0: (0.5, 0.2, 0.3)}, {}, None)
46
+        self.assertEqual([id(r) for r in self.rows()], [id(r) for r in before])  # dieselben Zeilen
47
+        self.assertEqual(self.list._rows[0].wdl, (0.5, 0.2, 0.3))
48
+        self.assertIsNone(self.list._rows[1].wdl)
49
+
50
+    def test_variantenbalken_ebenso(self):
51
+        variation = {1: (((101, "2.", "Lc4", (), None, "Bester Zug"),),)}
52
+        self.list.update(self.board, {}, {}, None, variation)
53
+        before = self.rows()
54
+        variation = {1: (((101, "2.", "Lc4", (), (0.4, 0.3, 0.3), "Bester Zug"),),)}
55
+        self.list.update(self.board, {}, {}, None, variation)
56
+        self.assertEqual([id(r) for r in self.rows()], [id(r) for r in before])
57
+        self.assertEqual(self.list._token_rows[101].wdl, (0.4, 0.3, 0.3))
58
+
59
+    def test_neuer_zug_baut_um(self):
60
+        self.list.update(self.board, {}, {}, None)
61
+        before = self.rows()
62
+        self.board.push_san("Bb5")
63
+        self.list.update(self.board, {}, {}, None)
64
+        self.assertEqual(len(self.list._rows), 5)
65
+        self.assertNotEqual([id(r) for r in self.rows()[:4]], [id(r) for r in before])
66
+
67
+
68
+if __name__ == "__main__":
69
+    unittest.main()