Skip to content

Commit

Permalink
Fix Windows context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
probonopd committed Jul 28, 2024
1 parent 83c144f commit 9d5cf25
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
3 changes: 1 addition & 2 deletions spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,7 @@ def text_label_deactivate(self):

def show_context_menu(self, pos):
# On Windows, use windows_context_menu.py
if sys.platform == "win32":
import windows_context_menu
if sys.platform == "win32" and self.path is not None:
windows_context_menu.show_context_menu(self.path)
else:
context_menu = QMenu(self)
Expand Down
48 changes: 33 additions & 15 deletions windows_context_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def show_context_menu(paths: Sequence[os.PathLike | str]):
paths = [_safe_path_parse(p) for p in paths]
else:
return

if not paths:
print("No usable paths provided.")
return

menu = QMenu()

Expand All @@ -48,25 +52,39 @@ def show_context_menu(paths: Sequence[os.PathLike | str]):
# 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?
# TODO: Check if https://github.com/NickHugi/PyKotor/blob/master/Libraries/Utility/src/utility/system/windows_context_menu.py handles multiple items better
# May need to take a look at SHMultiFileProperties and https://stackoverflow.com/a/34551988/1839209.
if items[0] is None:
print("Could not retrieve context menu items.")
return
verbs = items[0].Verbs()
for verb in verbs:
if verb.Name:
app = QApplication.instance()
action = QAction(verb.Name, app)
# Copying the path does not work using the default context menu action,
# hence we override it
# FIXME: Find a way that works independently of the language
if "path" or "Pfad" in verb.Name:
# Copy path to clipboard
action.triggered.connect(lambda _, p=paths[0]: app.clipboard().setText(str(p)))
if verbs is not None:
for verb in verbs:
if verb.Name:
app = QApplication.instance()
action = QAction(verb.Name, app)
# Copying the path does not work using the default context menu action,
# hence we override it
# FIXME: Find a way that works independently of the language
if "path" in verb.Name or "Pfad" in verb.Name:
# Copy path to clipboard
action.triggered.connect(lambda _, p=paths[0]: copy_path_to_clipboard(p))
else:
action.triggered.connect(lambda _, v=verb: execute_verb(v))
menu.addAction(action)
else:
action.triggered.connect(lambda _, v=verb: execute_verb(v))
menu.addAction(action)
else:
menu.addSeparator()
menu.addSeparator()

menu.exec(QCursor.pos())

def copy_path_to_clipboard(path):
"""
Copy the specified path to the clipboard.
"""
print(f"Copying path to clipboard: {path}")
app = QApplication.instance()
if app is None:
app = QApplication([])
app.clipboard().setText(str(path))


def execute_verb(verb):
"""
Expand All @@ -75,8 +93,8 @@ def execute_verb(verb):
Args:
verb: The verb to execute.
"""
print(f"Executing verb: {verb.Name}")
try:
print(f"Executing verb: {verb.Name}")
verb.DoIt()
except Exception as e:
show_error_message(f"An error occurred while executing the action: {e}")
Expand Down

0 comments on commit 9d5cf25

Please sign in to comment.