Skip to content

Commit 9d5cf25

Browse files
committed
Fix Windows context menu
1 parent 83c144f commit 9d5cf25

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

spatial.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,8 +868,7 @@ def text_label_deactivate(self):
868868

869869
def show_context_menu(self, pos):
870870
# On Windows, use windows_context_menu.py
871-
if sys.platform == "win32":
872-
import windows_context_menu
871+
if sys.platform == "win32" and self.path is not None:
873872
windows_context_menu.show_context_menu(self.path)
874873
else:
875874
context_menu = QMenu(self)

windows_context_menu.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def show_context_menu(paths: Sequence[os.PathLike | str]):
3636
paths = [_safe_path_parse(p) for p in paths]
3737
else:
3838
return
39+
40+
if not paths:
41+
print("No usable paths provided.")
42+
return
3943

4044
menu = QMenu()
4145

@@ -48,25 +52,39 @@ def show_context_menu(paths: Sequence[os.PathLike | str]):
4852
# FIXME: Handle multiple items; currently only the first item is used. This might mean that we need to get the Verbs in a different way?
4953
# TODO: Check if https://github.com/NickHugi/PyKotor/blob/master/Libraries/Utility/src/utility/system/windows_context_menu.py handles multiple items better
5054
# May need to take a look at SHMultiFileProperties and https://stackoverflow.com/a/34551988/1839209.
55+
if items[0] is None:
56+
print("Could not retrieve context menu items.")
57+
return
5158
verbs = items[0].Verbs()
52-
for verb in verbs:
53-
if verb.Name:
54-
app = QApplication.instance()
55-
action = QAction(verb.Name, app)
56-
# Copying the path does not work using the default context menu action,
57-
# hence we override it
58-
# FIXME: Find a way that works independently of the language
59-
if "path" or "Pfad" in verb.Name:
60-
# Copy path to clipboard
61-
action.triggered.connect(lambda _, p=paths[0]: app.clipboard().setText(str(p)))
59+
if verbs is not None:
60+
for verb in verbs:
61+
if verb.Name:
62+
app = QApplication.instance()
63+
action = QAction(verb.Name, app)
64+
# Copying the path does not work using the default context menu action,
65+
# hence we override it
66+
# FIXME: Find a way that works independently of the language
67+
if "path" in verb.Name or "Pfad" in verb.Name:
68+
# Copy path to clipboard
69+
action.triggered.connect(lambda _, p=paths[0]: copy_path_to_clipboard(p))
70+
else:
71+
action.triggered.connect(lambda _, v=verb: execute_verb(v))
72+
menu.addAction(action)
6273
else:
63-
action.triggered.connect(lambda _, v=verb: execute_verb(v))
64-
menu.addAction(action)
65-
else:
66-
menu.addSeparator()
74+
menu.addSeparator()
6775

6876
menu.exec(QCursor.pos())
6977

78+
def copy_path_to_clipboard(path):
79+
"""
80+
Copy the specified path to the clipboard.
81+
"""
82+
print(f"Copying path to clipboard: {path}")
83+
app = QApplication.instance()
84+
if app is None:
85+
app = QApplication([])
86+
app.clipboard().setText(str(path))
87+
7088

7189
def execute_verb(verb):
7290
"""
@@ -75,8 +93,8 @@ def execute_verb(verb):
7593
Args:
7694
verb: The verb to execute.
7795
"""
96+
print(f"Executing verb: {verb.Name}")
7897
try:
79-
print(f"Executing verb: {verb.Name}")
8098
verb.DoIt()
8199
except Exception as e:
82100
show_error_message(f"An error occurred while executing the action: {e}")

0 commit comments

Comments
 (0)