Public repository: timo / schach

Reader

Ein aufgeräumtes Schach-Programm für GNOME

feat(explorer): hover preview, opening names per move

public

feat(explorer): hover preview, opening names per move

- Hovered row shows the move translucent on the board, castling
  with rook
- Name from eco.db under each move that reaches a named position
- Rows aligned in a grid; more English ECO names translated

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

modified schach/diagram.py +32 −1

@@ -124,6 +124,7 @@ class BoardPainter:
124124
         self.clocks = None  # {Farbe: Millisekunden}
125125
         self.clock_running = None  # Farbe, deren Uhr gerade läuft
126126
         self.turn_marker = None  # Farbe, deren Name fett steht (wer am Zug ist, bei zwei Spielern)
127
+        self.preview = None  # Zug, der halb durchsichtig gezeigt wird (Explorer: Maus über einer Zeile)
127128
         self.editing = False  # Stellung aufbauen: keine geschlagenen Figuren anzeigen
128129
         self.eval_bar = None  # Gewinnchance Weiß in Prozent: Balken links neben dem Brett
129130
         self._drag_sq = None
@@ -230,11 +231,19 @@ class BoardPainter:
230231
             self._draw_piece(cr, piece, x + off, y + off, sq * scale)
231232
             cr.pop_group_to_source()
232233
             cr.paint_with_alpha(1 - p)
234
+        ghosts = self._preview_ghosts()
235
+        faded = {src for _p, src, _dst in ghosts}
233236
         for square, piece in self.board.piece_map().items():
234237
             if (dragging and square == self._drag_sq) or square in hidden:
235238
                 continue
236239
             x, y = self._square_origin(square)
237
-            self._draw_piece(cr, piece, x, y, sq)
240
+            if square in faded:
241
+                self._draw_piece_alpha(cr, piece, x, y, sq, 0.35)
242
+            else:
243
+                self._draw_piece(cr, piece, x, y, sq)
244
+        for piece, _src, dst in ghosts:
245
+            x, y = self._square_origin(dst)
246
+            self._draw_piece_alpha(cr, piece, x, y, sq, 0.6)
238247
         if anim:
239248
             p = anim["progress"]
240249
             for piece, src, dst in anim["movers"]:
@@ -327,6 +336,28 @@ class BoardPainter:
327336
         cr.paint()
328337
         cr.restore()
329338
 
339
+    def _preview_ghosts(self):
340
+        """[(Figur, von, nach)] für den Vorschau-Zug, bei Rochade mit Turm."""
341
+        move = self.preview
342
+        if move is None or move not in self.board.legal_moves:
343
+            return []
344
+        piece = self.board.piece_at(move.from_square)
345
+        if move.promotion:
346
+            piece = chess.Piece(move.promotion, piece.color)
347
+        ghosts = [(piece, move.from_square, move.to_square)]
348
+        if self.board.is_castling(move):
349
+            rank = chess.square_rank(move.from_square)
350
+            kingside = self.board.is_kingside_castling(move)
351
+            ghosts.append((chess.Piece(chess.ROOK, piece.color),
352
+                           chess.square(7 if kingside else 0, rank), chess.square(5 if kingside else 3, rank)))
353
+        return ghosts
354
+
355
+    def _draw_piece_alpha(self, cr, piece, x, y, size, alpha):
356
+        cr.push_group()
357
+        self._draw_piece(cr, piece, x, y, size)
358
+        cr.pop_group_to_source()
359
+        cr.paint_with_alpha(alpha)
360
+
330361
     def _draw_piece(self, cr, piece, x, y, size):
331362
         rect = Rsvg.Rectangle()
332363
         rect.x, rect.y, rect.width, rect.height = x, y, size, size

modified schach/explorer.py +45 −8

@@ -37,6 +37,7 @@ class Line:
3737
     draws: int = 0
3838
     black: int = 0
3939
     share: float = 0.0  # Anteil an allen Partien bzw. am Buchgewicht, 0–1
40
+    name: str = ""  # Eröffnungsname nach dem Zug, wenn die Stellung einen eigenen hat
4041
 
4142
 
4243
 # --- Logik (ohne GTK, getestet) ---------------------------------------------
@@ -90,9 +91,10 @@ class ExplorerList(Gtk.Box):
9091
         super().__init__(orientation=Gtk.Orientation.VERTICAL)
9192
         self.with_results = with_results
9293
         self.on_play = None  # callback(Zug)
94
+        self.on_preview = None  # callback(Zug oder None) – Maus über einer Zeile
9395
         self.notation = "de"
9496
         self.name_label = Gtk.Label(xalign=0, wrap=True, css_classes=["dim-label", "caption"],
95
-                                    margin_start=12, margin_end=12, margin_bottom=6)
97
+                                    margin_start=12, margin_end=12, margin_top=8, margin_bottom=4)
9698
         self.append(self.name_label)
9799
         self.listbox = Gtk.ListBox(selection_mode=Gtk.SelectionMode.NONE, css_classes=["navigation-sidebar"])
98100
         self.listbox.connect("row-activated", self._on_activated)
@@ -123,6 +125,8 @@ class ExplorerList(Gtk.Box):
123125
             self.show_message("Keine Partien", "Diese Stellung kommt hier nicht vor.", "edit-find-symbolic")
124126
             self._key = key
125127
             return
128
+        if self.on_preview:
129
+            self.on_preview(None)  # Liste neu: alte Vorschau weg
126130
         self.listbox.remove_all()
127131
         for line in lines:
128132
             self.listbox.append(self._row(board, line, playable))
@@ -131,13 +135,20 @@ class ExplorerList(Gtk.Box):
131135
     def _row(self, board, line, playable):
132136
         row = Gtk.ListBoxRow(activatable=playable)
133137
         row.move = line.move
134
-        box = Gtk.Box(spacing=8, margin_top=4, margin_bottom=4)
138
+        hover = Gtk.EventControllerMotion()
139
+        hover.connect("enter", lambda *_: self.on_preview and self.on_preview(line.move))
140
+        hover.connect("leave", lambda *_: self.on_preview and self.on_preview(None))
141
+        row.add_controller(hover)
142
+        box = Gtk.Grid(column_spacing=8, margin_top=4, margin_bottom=4, hexpand=True)
135143
         san = Gtk.Label(label=notation.move(board, line.move, self.notation), xalign=0, width_chars=6,
136144
                         css_classes=["heading"])
137
-        box.append(san)
145
+        box.attach(san, 0, 0, 1, 1)
146
+        if line.name:  # eigene Zeile über die ganze Breite: Balken bleiben untereinander
147
+            box.attach(Gtk.Label(label=line.name, xalign=0, ellipsize=3, css_classes=["dim-label", "caption"],
148
+                                 tooltip_text=line.name), 0, 1, 3, 1)
138149
         bar = Gtk.DrawingArea(hexpand=True, content_height=14, valign=Gtk.Align.CENTER)
139150
         bar.set_draw_func(self._draw_bar, line)
140
-        box.append(bar)
151
+        box.attach(bar, 1, 0, 1, 1)
141152
         if self.with_results:
142153
             count = games_text(line.games)
143154
             total = line.games
@@ -148,7 +159,7 @@ class ExplorerList(Gtk.Box):
148159
         else:
149160
             count = f"{round(line.share * 100)} %" if line.share >= 0.005 else "<1 %"
150161
             row.set_tooltip_text(f"In {round(line.share * 100)} % der Buchpartien")
151
-        box.append(Gtk.Label(label=count, xalign=1, width_chars=8, css_classes=["dim-label", "numeric"]))
162
+        box.attach(Gtk.Label(label=count, xalign=1, width_chars=8, css_classes=["dim-label", "numeric"]), 2, 0, 1, 1)
152163
         row.set_child(box)
153164
         return row
154165
 
@@ -207,12 +218,13 @@ class ExplorerMixin:
207218
         self.lichess_list = ExplorerList(with_results=True)
208219
         for view in (self.book_list, self.lichess_list):
209220
             view.on_play = self._explorer_play
221
+            view.on_preview = self._explorer_preview
210222
         self.side_stack = Gtk.Stack(transition_type=Gtk.StackTransitionType.CROSSFADE, vexpand=True)
211223
         self.side_stack.add_named(movelist, "moves")
212224
         self.side_stack.add_named(self.book_list, "book")
213225
         self.side_stack.add_named(self.lichess_list, "lichess")
214226
         self.side_switch = Adw.ToggleGroup(margin_start=12, margin_end=12, margin_top=6, margin_bottom=6,
215
-                                           css_classes=["round"])
227
+                                           homogeneous=True)
216228
         for name, label, tip in (("moves", "Partie", "Züge und Bewertung"),
217229
                                  ("book", "Buch", "Eröffnungsbuch"),
218230
                                  ("lichess", "Lichess", "Was Spieler deiner Stärke auf Lichess ziehen")):
@@ -226,6 +238,7 @@ class ExplorerMixin:
226238
         return box
227239
 
228240
     def _explorer_switched(self, *_):
241
+        self._explorer_preview(None)
229242
         self.side_stack.set_visible_child_name(self.side_switch.get_active_name() or "moves")
230243
         self._explorer_refresh()
231244
 
@@ -255,6 +268,7 @@ class ExplorerMixin:
255268
 
256269
     def _explorer_refresh(self):
257270
         if not self.split.get_show_sidebar():
271
+            self._explorer_preview(None)  # Leiste zu, während die Maus noch über einer Zeile war
258272
             return
259273
         source = self.side_switch.get_active_name()
260274
         if source not in ("book", "lichess"):
@@ -270,12 +284,13 @@ class ExplorerMixin:
270284
         name = self._explorer_name(board)
271285
         if source == "book":
272286
             from .training import _book
273
-            view.show_lines(board, book_lines(_book(), board), name, playable)
287
+            view.show_lines(board, self._explorer_named(board, book_lines(_book(), board), name), name, playable)
274288
             return
275289
         ratings = ratings_near(self.elo or int(self.settings["elo"]))
276290
         key = (board.fen(), tuple(ratings))
277291
         if key in self.explorer_cache:
278
-            view.show_lines(board, lichess_lines(self.explorer_cache[key], board), name, playable)
292
+            lines = self._explorer_named(board, lichess_lines(self.explorer_cache[key], board), name)
293
+            view.show_lines(board, lines, name, playable)
279294
             return
280295
         view.show_message("Lädt …", f"Partien mit Wertung {ratings[0]}–{ratings[-1] + 199}", "content-loading-symbolic")
281296
         if self.explorer_pending:
@@ -307,6 +322,22 @@ class ExplorerMixin:
307322
         lichess._background(work, done)
308323
         return False
309324
 
325
+    def _explorer_named(self, board, lines, current):
326
+        """Eröffnungsnamen aus eco.db an die Zeilen hängen – nur neue, nicht den aktuellen."""
327
+        from .training import _names
328
+        for line in lines:
329
+            after = board.copy(stack=False)
330
+            after.push(line.move)
331
+            key = ("exact", after.fen())
332
+            if key not in self._name_cache:
333
+                self._name_cache[key] = _names().exact(after)
334
+            found = self._name_cache[key]
335
+            if found:
336
+                found = notation.text(found, self._notation())
337
+                if found != current:
338
+                    line.name = found
339
+        return lines
340
+
310341
     def _explorer_name(self, board):
311342
         from .training import _names
312343
         key = board.fen()
@@ -315,6 +346,12 @@ class ExplorerMixin:
315346
         name = self._name_cache[key]
316347
         return notation.text(name, self._notation()) if name else None
317348
 
349
+    def _explorer_preview(self, move):
350
+        if self.view.preview != move:
351
+            self.view.preview = move
352
+            self.view.queue_draw()
353
+
318354
     def _explorer_play(self, move):
355
+        self._explorer_preview(None)
319356
         if self._human_to_move() and not self._browsing() and move in self.board.legal_moves:
320357
             self._on_board_move(move.from_square, move.to_square) if not move.promotion else self._play_human(move)

modified schach/opening.py +3 −1

@@ -182,7 +182,9 @@ ENGLISH = [
182182
     ("Berlin Defense", "Berliner Verteidigung"), ("Morphy Defense", "Morphy-Verteidigung"),
183183
     ("Steinitz Defense", "Steinitz-Verteidigung"), ("Classical Defense", "Klassische Verteidigung"),
184184
     ("Philidor Defense", "Philidor-Verteidigung"), ("Petrov's Defense", "Russische Verteidigung"),
185
-    ("Sicilian Defense", "Sizilianisch"), ("French Defense", "Französisch"),
185
+    ("King's Pawn Game", "Königsbauernspiel"), ("Queen's Pawn Game", "Damenbauernspiel"),
186
+    ("King's Knight Opening", "Königsspringerspiel"), ("Three Knights", "Dreispringerspiel"),
187
+    ("Four Knights", "Vierspringerspiel"), ("Sicilian Defense", "Sizilianisch"), ("French Defense", "Französisch"),
186188
     ("Caro-Kann Defense", "Caro-Kann"), ("Queen's Gambit", "Damengambit"), ("King's Gambit", "Königsgambit"),
187189
     ("Closed Variations", "Geschlossene Varianten"), ("Closed Variation", "Geschlossene Variante"),
188190
     ("Open Variations", "Offene Varianten"), ("Open Variation", "Offene Variante"),