| @@ -37,6 +37,7 @@ class Line: |
| 37 | 37 | draws: int = 0 |
| 38 | 38 | black: int = 0 |
| 39 | 39 | 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 |
| 40 | 41 | |
| 41 | 42 | |
| 42 | 43 | # --- Logik (ohne GTK, getestet) --------------------------------------------- |
| @@ -90,9 +91,10 @@ class ExplorerList(Gtk.Box): |
| 90 | 91 | super().__init__(orientation=Gtk.Orientation.VERTICAL) |
| 91 | 92 | self.with_results = with_results |
| 92 | 93 | self.on_play = None # callback(Zug) |
| 94 | + self.on_preview = None # callback(Zug oder None) – Maus über einer Zeile |
| 93 | 95 | self.notation = "de" |
| 94 | 96 | 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) |
| 96 | 98 | self.append(self.name_label) |
| 97 | 99 | self.listbox = Gtk.ListBox(selection_mode=Gtk.SelectionMode.NONE, css_classes=["navigation-sidebar"]) |
| 98 | 100 | self.listbox.connect("row-activated", self._on_activated) |
| @@ -123,6 +125,8 @@ class ExplorerList(Gtk.Box): |
| 123 | 125 | self.show_message("Keine Partien", "Diese Stellung kommt hier nicht vor.", "edit-find-symbolic") |
| 124 | 126 | self._key = key |
| 125 | 127 | return |
| 128 | + if self.on_preview: |
| 129 | + self.on_preview(None) # Liste neu: alte Vorschau weg |
| 126 | 130 | self.listbox.remove_all() |
| 127 | 131 | for line in lines: |
| 128 | 132 | self.listbox.append(self._row(board, line, playable)) |
| @@ -131,13 +135,20 @@ class ExplorerList(Gtk.Box): |
| 131 | 135 | def _row(self, board, line, playable): |
| 132 | 136 | row = Gtk.ListBoxRow(activatable=playable) |
| 133 | 137 | 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) |
| 135 | 143 | san = Gtk.Label(label=notation.move(board, line.move, self.notation), xalign=0, width_chars=6, |
| 136 | 144 | 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) |
| 138 | 149 | bar = Gtk.DrawingArea(hexpand=True, content_height=14, valign=Gtk.Align.CENTER) |
| 139 | 150 | bar.set_draw_func(self._draw_bar, line) |
| 140 | | - box.append(bar) |
| 151 | + box.attach(bar, 1, 0, 1, 1) |
| 141 | 152 | if self.with_results: |
| 142 | 153 | count = games_text(line.games) |
| 143 | 154 | total = line.games |
| @@ -148,7 +159,7 @@ class ExplorerList(Gtk.Box): |
| 148 | 159 | else: |
| 149 | 160 | count = f"{round(line.share * 100)} %" if line.share >= 0.005 else "<1 %" |
| 150 | 161 | 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) |
| 152 | 163 | row.set_child(box) |
| 153 | 164 | return row |
| 154 | 165 | |
| @@ -207,12 +218,13 @@ class ExplorerMixin: |
| 207 | 218 | self.lichess_list = ExplorerList(with_results=True) |
| 208 | 219 | for view in (self.book_list, self.lichess_list): |
| 209 | 220 | view.on_play = self._explorer_play |
| 221 | + view.on_preview = self._explorer_preview |
| 210 | 222 | self.side_stack = Gtk.Stack(transition_type=Gtk.StackTransitionType.CROSSFADE, vexpand=True) |
| 211 | 223 | self.side_stack.add_named(movelist, "moves") |
| 212 | 224 | self.side_stack.add_named(self.book_list, "book") |
| 213 | 225 | self.side_stack.add_named(self.lichess_list, "lichess") |
| 214 | 226 | self.side_switch = Adw.ToggleGroup(margin_start=12, margin_end=12, margin_top=6, margin_bottom=6, |
| 215 | | - css_classes=["round"]) |
| 227 | + homogeneous=True) |
| 216 | 228 | for name, label, tip in (("moves", "Partie", "Züge und Bewertung"), |
| 217 | 229 | ("book", "Buch", "Eröffnungsbuch"), |
| 218 | 230 | ("lichess", "Lichess", "Was Spieler deiner Stärke auf Lichess ziehen")): |
| @@ -226,6 +238,7 @@ class ExplorerMixin: |
| 226 | 238 | return box |
| 227 | 239 | |
| 228 | 240 | def _explorer_switched(self, *_): |
| 241 | + self._explorer_preview(None) |
| 229 | 242 | self.side_stack.set_visible_child_name(self.side_switch.get_active_name() or "moves") |
| 230 | 243 | self._explorer_refresh() |
| 231 | 244 | |
| @@ -255,6 +268,7 @@ class ExplorerMixin: |
| 255 | 268 | |
| 256 | 269 | def _explorer_refresh(self): |
| 257 | 270 | if not self.split.get_show_sidebar(): |
| 271 | + self._explorer_preview(None) # Leiste zu, während die Maus noch über einer Zeile war |
| 258 | 272 | return |
| 259 | 273 | source = self.side_switch.get_active_name() |
| 260 | 274 | if source not in ("book", "lichess"): |
| @@ -270,12 +284,13 @@ class ExplorerMixin: |
| 270 | 284 | name = self._explorer_name(board) |
| 271 | 285 | if source == "book": |
| 272 | 286 | 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) |
| 274 | 288 | return |
| 275 | 289 | ratings = ratings_near(self.elo or int(self.settings["elo"])) |
| 276 | 290 | key = (board.fen(), tuple(ratings)) |
| 277 | 291 | 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) |
| 279 | 294 | return |
| 280 | 295 | view.show_message("Lädt …", f"Partien mit Wertung {ratings[0]}–{ratings[-1] + 199}", "content-loading-symbolic") |
| 281 | 296 | if self.explorer_pending: |
| @@ -307,6 +322,22 @@ class ExplorerMixin: |
| 307 | 322 | lichess._background(work, done) |
| 308 | 323 | return False |
| 309 | 324 | |
| 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 | + |
| 310 | 341 | def _explorer_name(self, board): |
| 311 | 342 | from .training import _names |
| 312 | 343 | key = board.fen() |
| @@ -315,6 +346,12 @@ class ExplorerMixin: |
| 315 | 346 | name = self._name_cache[key] |
| 316 | 347 | return notation.text(name, self._notation()) if name else None |
| 317 | 348 | |
| 349 | + def _explorer_preview(self, move): |
| 350 | + if self.view.preview != move: |
| 351 | + self.view.preview = move |
| 352 | + self.view.queue_draw() |
| 353 | + |
| 318 | 354 | def _explorer_play(self, move): |
| 355 | + self._explorer_preview(None) |
| 319 | 356 | if self._human_to_move() and not self._browsing() and move in self.board.legal_moves: |
| 320 | 357 | self._on_board_move(move.from_square, move.to_square) if not move.promotion else self._play_human(move) |