| @@ -10,7 +10,7 @@ import chess |
| 10 | 10 | import gi |
| 11 | 11 | |
| 12 | 12 | gi.require_version("Gtk", "4.0") |
| 13 | | -from gi.repository import Gdk, GLib, Gtk # noqa: E402 |
| 13 | +from gi.repository import Gdk, Gio, GLib, Gtk # noqa: E402 |
| 14 | 14 | |
| 15 | 15 | from . import notation # noqa: E402 |
| 16 | 16 | from .board import BADGE # noqa: E402 |
| @@ -58,7 +58,11 @@ class MoveList(Gtk.Box): |
| 58 | 58 | self.on_copy = None # callback(halbzug) |
| 59 | 59 | self.on_continue = None # callback(halbzug) |
| 60 | 60 | self.on_why_not = None # callback(halbzug): „Warum nicht?“ bei ?!, ? und ?? |
| 61 | | - self.on_variation = None # callback(nummer): Zug einer Nebenvariante vom Analysebrett angeklickt |
| 61 | + self.on_variation = None # callback(nummer): Zug einer Nebenvariante angeklickt |
| 62 | + # Kontextmenü (Rechtsklick) an Variantenzügen; Entf löscht ebenfalls |
| 63 | + self.on_variation_delete = None # callback(nummer): diesen Zug samt allem danach löschen |
| 64 | + self.on_variation_continue = None # callback(nummer): ab hier gegen Maia weiterspielen |
| 65 | + self.on_variation_copy = None # callback(nummer): Stellung als FEN kopieren |
| 62 | 66 | |
| 63 | 67 | header = Gtk.Box(spacing=6, margin_start=12, margin_end=6, margin_top=6, margin_bottom=6) |
| 64 | 68 | header.append(Gtk.Box(hexpand=True)) # Überschrift steht schon im Umschalter „Partie | Buch | Lichess“ |
| @@ -74,6 +78,18 @@ class MoveList(Gtk.Box): |
| 74 | 78 | keys = Gtk.EventControllerKey() # ← und → klappen auf und zu; sonst weiter ans Fenster (Brett) |
| 75 | 79 | keys.connect("key-pressed", self._on_key) |
| 76 | 80 | self.listbox.add_controller(keys) |
| 81 | + menu_click = Gtk.GestureClick(button=Gdk.BUTTON_SECONDARY) |
| 82 | + menu_click.connect("pressed", self._on_menu_click) |
| 83 | + self.listbox.add_controller(menu_click) |
| 84 | + self._menu_row = None # Zeile, zu der das Kontextmenü gerade offen ist |
| 85 | + actions = Gio.SimpleActionGroup() |
| 86 | + for name, handler in (("continue", self._menu_continue), ("copy", self._menu_copy), |
| 87 | + ("delete", self._menu_delete)): |
| 88 | + action = Gio.SimpleAction.new(name, None) |
| 89 | + action.connect("activate", lambda _a, _p, handler=handler: handler()) |
| 90 | + actions.add_action(action) |
| 91 | + self.insert_action_group("zug", actions) |
| 92 | + self.menu_actions = actions |
| 77 | 93 | self.scroller = Gtk.ScrolledWindow(vexpand=True, hscrollbar_policy=Gtk.PolicyType.NEVER) |
| 78 | 94 | self.scroller.set_child(self.listbox) |
| 79 | 95 | self.append(self.scroller) |
| @@ -267,9 +283,60 @@ class MoveList(Gtk.Box): |
| 267 | 283 | def _children(self, row): |
| 268 | 284 | return [r for r in self._variation_rows if r.parent_row is row] |
| 269 | 285 | |
| 286 | + # --- Kontextmenü ------------------------------------------------------- |
| 287 | + |
| 288 | + def _on_menu_click(self, gesture, _n, x, y): |
| 289 | + row = self.listbox.get_row_at_y(int(y)) |
| 290 | + if row is None or (row.ply is None and row.variation is None): |
| 291 | + return # Kopfzeile „Variante n“ |
| 292 | + gesture.set_state(Gtk.EventSequenceState.CLAIMED) |
| 293 | + self._menu_row = row |
| 294 | + menu = Gio.Menu() |
| 295 | + menu.append("Ab hier _weiterspielen", "zug.continue") |
| 296 | + menu.append("Stellung als _FEN kopieren", "zug.copy") |
| 297 | + if row.variation is not None: # Züge der Partie bleiben; löschen kann man nur Varianten |
| 298 | + menu.append("Ab hier _löschen (Entf)", "zug.delete") |
| 299 | + popover = Gtk.PopoverMenu(menu_model=menu, has_arrow=False, halign=Gtk.Align.START) |
| 300 | + popover.set_parent(self.listbox) |
| 301 | + rect = Gdk.Rectangle() |
| 302 | + rect.x, rect.y, rect.width, rect.height = int(x), int(y), 1, 1 |
| 303 | + popover.set_pointing_to(rect) |
| 304 | + popover.connect("closed", lambda p: GLib.idle_add(p.unparent)) |
| 305 | + popover.popup() |
| 306 | + |
| 307 | + def _menu_target(self): |
| 308 | + row, self._menu_row = self._menu_row, None |
| 309 | + return row |
| 310 | + |
| 311 | + def _menu_continue(self): |
| 312 | + row = self._menu_target() |
| 313 | + if row is not None and row.variation is not None and self.on_variation_continue: |
| 314 | + GLib.idle_add(lambda: self.on_variation_continue(row.variation) and False) |
| 315 | + elif row is not None and row.ply is not None and self.on_continue: |
| 316 | + GLib.idle_add(lambda: self.on_continue(row.ply) and False) |
| 317 | + |
| 318 | + def _menu_copy(self): |
| 319 | + row = self._menu_target() |
| 320 | + if row is not None and row.variation is not None and self.on_variation_copy: |
| 321 | + self.on_variation_copy(row.variation) |
| 322 | + elif row is not None and row.ply is not None and self.on_copy: |
| 323 | + self.on_copy(row.ply) |
| 324 | + |
| 325 | + def _menu_delete(self, row=None): |
| 326 | + row = row or self._menu_target() |
| 327 | + if row is not None and row.variation is not None and self.on_variation_delete: |
| 328 | + token = row.variation |
| 329 | + GLib.idle_add(lambda: self.on_variation_delete(token) and False) # baut die Liste neu |
| 330 | + |
| 270 | 331 | def _on_key(self, _controller, keyval, _keycode, state): |
| 271 | 332 | if state & (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.ALT_MASK | Gdk.ModifierType.SHIFT_MASK): |
| 272 | 333 | return False |
| 334 | + if keyval in (Gdk.KEY_Delete, Gdk.KEY_KP_Delete): |
| 335 | + row = self.listbox.get_selected_row() |
| 336 | + if row is not None and row.variation is not None: |
| 337 | + self._menu_delete(row) |
| 338 | + return True |
| 339 | + return False |
| 273 | 340 | row = self.listbox.get_selected_row() |
| 274 | 341 | if row is None or keyval not in (Gdk.KEY_Right, Gdk.KEY_Left): |
| 275 | 342 | return False |