Skip to content

Commit

Permalink
Add check if running on main thread for macOS
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
elevans committed May 28, 2024
1 parent 6db92e7 commit 3f0188b
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/imagej/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 3f0188b

Please sign in to comment.