feat: add navigable context menu to mixins

This commit is contained in:
aclist committed 2026-02-07 17:19:44 +09:00
1 parent ac7700ec23
commit 56b32a0048
4 files changed
+83 -104

No files matched your search

+36 -4
View File
@@ -1,6 +1,7 @@
import logging
from dzgui.views.trees.tree_base import TreeView
from dzgui.const.enum import ContextMenu
from dzgui.util.keys import is_navkey
from dzgui.views.trees.tree_base import TreeView
import gi
@@ -21,7 +22,7 @@ class ContextMixin(TreeView):
if self.is_selection_empty():
return
if event.type is Gdk.EventType.BUTTON_PRESS:
if event.type is Gdk.EventType.BUTTON_PRESS and event.button == 3:
try:
pathinfo = self.get_path_at_pos(int(event.x), int(event.y))
if pathinfo is None:
@@ -41,6 +42,8 @@ class ContextMixin(TreeView):
group = self.menu
self.context_menu = Gtk.Menu()
self.context_menu.connect("key-press-event", self._on_key)
for row in group.value:
item = Gtk.MenuItem(label=row.dict["label"])
item.connect("activate", self._on_menu_click, row)
@@ -63,11 +66,11 @@ class ContextMixin(TreeView):
Local mods page allows multi selection, so ensure that only focused row
is selected. Used by ContextMenu.OPEN_WORKSHOP and ContextMenu.DELETE_MOD.
Mod tree right panel supports multi-delete, but context menu enforces
ModTreeView supports multi-delete, but context menu enforces
single deletion on the focused row.
Debug log page allows multi selection and copy of rows, so special handling
is used.
is used per below.
"""
if enum == ContextMenu.COPY_LOG_CLIPBOARD:
model, records = self.get_selection().get_selected_rows()
@@ -76,3 +79,32 @@ class ContextMixin(TreeView):
else:
path = self.get_focused_row_path()
self.controller.menu_action(enum, path)
def _on_key(self, menu: Gtk.Menu, event: Gdk.EventKey) -> bool | None:
if not is_navkey(event.keyval):
return False
menu = self.context_menu
sel = menu.get_selected_item()
children = menu.get_children()
for i, child in enumerate(children):
if sel is child:
ind = i
break
match event.keyval:
case Gdk.KEY_j:
if ind == len(children) - 1:
return True
menu.select_item(children[ind + 1])
case Gdk.KEY_k:
if ind - 1 < 0:
return True
menu.select_item(children[ind - 1])
case Gdk.KEY_g:
menu.select_item(children[0])
case Gdk.KEY_G:
ind = len(children) - 1
menu.select_item(children[ind])
case _:
return False
return True