|
25 | 25 |
|
26 | 26 | import inspect
|
27 | 27 | import os
|
| 28 | +import platform |
28 | 29 | import sys
|
29 | 30 | import warnings
|
30 | 31 | from io import BytesIO
|
|
35 | 36 | import numpy as np # noqa
|
36 | 37 | import numpy.typing as npt # noqa
|
37 | 38 | import py5_tools
|
| 39 | +import py5_tools.environ # noqa |
38 | 40 | from jpype import JClass # noqa
|
39 | 41 | from jpype.types import JArray, JChar, JFloat, JInt, JString # noqa
|
40 | 42 | from PIL import Image # noqa
|
41 | 43 |
|
| 44 | +_environ = py5_tools.environ.Environment() |
| 45 | + |
42 | 46 | if not py5_tools.is_jvm_running():
|
43 | 47 | base_path = (
|
44 | 48 | Path(getattr(sys, "_MEIPASS")) / "py5"
|
45 | 49 | if hasattr(sys, "_MEIPASS")
|
46 | 50 | else Path(__file__).absolute().parent
|
47 | 51 | )
|
| 52 | + |
| 53 | + if platform.system() == "Darwin": |
| 54 | + # Make sure Python appears on the MacOS Dock |
| 55 | + # This is necessary, otherwise MacOS will not like to let JAVA2D Sketches get focus |
| 56 | + try: |
| 57 | + from AppKit import ( |
| 58 | + NSURL, |
| 59 | + NSApplication, |
| 60 | + NSApplicationActivationPolicyRegular, |
| 61 | + NSImage, |
| 62 | + ) |
| 63 | + |
| 64 | + # this adds a white square to the dock |
| 65 | + app = NSApplication.sharedApplication() |
| 66 | + app.setActivationPolicy_(NSApplicationActivationPolicyRegular) |
| 67 | + |
| 68 | + # set the dock icon to the py5 logo |
| 69 | + icon_path = base_path.parent / "py5_tools/resources/logo.icns" |
| 70 | + icon_url = NSURL.fileURLWithPath_(str(icon_path)) |
| 71 | + icon_image = NSImage.alloc().initWithContentsOfURL_(icon_url) |
| 72 | + app.setApplicationIconImage_(icon_image) |
| 73 | + |
| 74 | + # cleanup |
| 75 | + del app, icon_path, icon_url, icon_image |
| 76 | + del NSURL, NSApplication, NSApplicationActivationPolicyRegular, NSImage |
| 77 | + except: |
| 78 | + pass |
| 79 | + |
| 80 | + if platform.system() == "Windows": |
| 81 | + # This code is here so that later win32gui code works correctly. The |
| 82 | + # `focus_window(handle)` method in `Py5Bridge` is used to move Sketch |
| 83 | + # windows to the foreground |
| 84 | + try: |
| 85 | + from win32com import client as win32com_client |
| 86 | + |
| 87 | + shell = win32com_client.Dispatch("WScript.Shell") |
| 88 | + |
| 89 | + # send the most benign key possible. this can't possibly do anything |
| 90 | + shell.SendKeys(chr(0)) |
| 91 | + |
| 92 | + # cleanup |
| 93 | + del win32com_client, shell |
| 94 | + except: |
| 95 | + pass |
| 96 | + |
48 | 97 | # add py5 jars to the classpath first
|
49 | 98 | py5_tools.add_jars(str(base_path / "jars"))
|
50 | 99 | # if the cwd has a jars subdirectory, add that next
|
|
114 | 163 | pass
|
115 | 164 |
|
116 | 165 |
|
117 |
| -__version__ = "0.10.3a1" |
| 166 | +__version__ = "0.10.4a2" |
118 | 167 |
|
119 | 168 | _PY5_USE_IMPORTED_MODE = py5_tools.get_imported_mode()
|
120 | 169 | py5_tools._lock_imported_mode()
|
@@ -8019,6 +8068,32 @@ def image_mode(mode: int, /) -> None:
|
8019 | 8068 | return _py5sketch.image_mode(mode)
|
8020 | 8069 |
|
8021 | 8070 |
|
| 8071 | +def intercept_escape() -> None: |
| 8072 | + """Prevent the Escape key from causing the Sketch to exit. |
| 8073 | + |
| 8074 | + Notes |
| 8075 | + ----- |
| 8076 | + |
| 8077 | + Prevent the Escape key from causing the Sketch to exit. Normally hitting the |
| 8078 | + Escape key (`ESC`) will cause the Sketch to exit. In Processing, one can write |
| 8079 | + code to change the Escape key's behavior by changing the `key` value to |
| 8080 | + something else, perhaps with code similar to `py5.key = 'x'`. That code won't |
| 8081 | + work in py5 because py5 does not allow the user to alter the value of `key` like |
| 8082 | + Processing does. The `intercept_escape()` method was created to allow users to |
| 8083 | + achieve the same goal of preventing the Escape key from causing the Sketch to |
| 8084 | + exit. |
| 8085 | + |
| 8086 | + The `intercept_escape()` method will only do something when `key` already equals |
| 8087 | + `ESC`. This function should only be called from the user event functions |
| 8088 | + `key_pressed()`, `key_typed()`, and `key_released()`. |
| 8089 | + |
| 8090 | + This method will not alter the value of `key`. This method cannot prevent a |
| 8091 | + Sketch from exiting when the exit is triggered by any other means, such as a |
| 8092 | + call to `exit_sketch()` or the user closes the window. |
| 8093 | + """ |
| 8094 | + return _py5sketch.intercept_escape() |
| 8095 | + |
| 8096 | + |
8022 | 8097 | @overload
|
8023 | 8098 | def lerp_color(c1: int, c2: int, amt: float, /) -> int:
|
8024 | 8099 | """Calculates a color between two colors at a specific increment.
|
@@ -23104,3 +23179,11 @@ def _prepare_dynamic_variables(caller_locals, caller_globals):
|
23104 | 23179 |
|
23105 | 23180 |
|
23106 | 23181 | _prepare_dynamic_variables(locals(), globals())
|
| 23182 | + |
| 23183 | + |
| 23184 | +if platform.system() == "Darwin" and _environ.in_ipython_session: |
| 23185 | + if _environ.ipython_shell.active_eventloop != "osx": |
| 23186 | + print( |
| 23187 | + "Importing py5 on macOS but the necessary Jupyter macOS event loop has not been activated. I'll activate it for you, but next time, execute `%gui osx` before importing this library." |
| 23188 | + ) |
| 23189 | + _environ.ipython_shell.run_line_magic("gui", "osx") |
0 commit comments