diff --git a/spatial.py b/spatial.py index 82f35eb..e84aabe 100644 --- a/spatial.py +++ b/spatial.py @@ -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) diff --git a/windows_context_menu.py b/windows_context_menu.py index 22ce7f6..570bfb5 100644 --- a/windows_context_menu.py +++ b/windows_context_menu.py @@ -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() @@ -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): """ @@ -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}")