-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
38 lines (28 loc) · 999 Bytes
/
utils.py
File metadata and controls
38 lines (28 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Copyright © 2026 EddieChan1993. All rights reserved.
# Unauthorized commercial use is strictly prohibited.
"""
Thread-safe main-thread dispatcher and shared helpers.
"""
import threading
from Foundation import NSObject
import objc
class _Dispatcher(NSObject):
"""Tiny ObjC helper so we can bounce a Python callable to the main thread."""
def runBlock_(self, block):
block()
_dispatcher_lock = threading.Lock()
_dispatcher: "_Dispatcher | None" = None
def _get_dispatcher() -> "_Dispatcher":
global _dispatcher
with _dispatcher_lock:
if _dispatcher is None:
_dispatcher = _Dispatcher.alloc().init()
return _dispatcher
def run_on_main_thread(func, wait: bool = False):
"""
Schedule *func* to run on the main thread. Safe to call from any thread.
If *wait* is True the call blocks until *func* completes.
"""
_get_dispatcher().performSelectorOnMainThread_withObject_waitUntilDone_(
"runBlock:", func, wait
)