From 3f0188baad3e7e98c331438752c0966e889681df Mon Sep 17 00:00:00 2001 From: Edward Evans Date: Mon, 27 May 2024 20:57:16 -0500 Subject: [PATCH] Add check if running on main thread for macOS Jaunch now enables a true interactive experince for macOS users, but only if PyImageJ is launch in a seperate thread (i.e. not the main thread). This commit adds an additional check for which thread we are currently on. If on the main thread, fail like before and prevent interactive mode on macOS. Otherwise, proceed as usual. --- src/imagej/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/imagej/__init__.py b/src/imagej/__init__.py index 25bf753d..8fc174bf 100644 --- a/src/imagej/__init__.py +++ b/src/imagej/__init__.py @@ -1205,7 +1205,7 @@ def init( macos = sys.platform == "darwin" - if macos and mode == Mode.INTERACTIVE: + if macos and mode == Mode.INTERACTIVE and _is_main_thread(): raise EnvironmentError("Sorry, the interactive mode is not available on macOS.") if not sj.jvm_started(): @@ -1517,6 +1517,14 @@ def _includes_imagej_legacy(items: list): return any(item.startswith("net.imagej:imagej-legacy") for item in items) +def _is_main_thread(): + """Detect if on main thread running on the main thread. + + :return: Boolean indicating if the current thread is the main thread. + """ + return threading.current_thread() == threading.main_thread() + + def _set_ij_env(ij_dir): """ Create a list of required jars and add to the java classpath.