anki/qt/aqt/_macos_helper.py
RumovZ 9c54f85be6
Import submodules directly (#1662)
* Use submodule imports in aqt

* Use submodule imports in pylib

* More submodule imports in pylib

These required removing some direct imports to get rid of import cycles.
2022-02-13 13:40:47 +10:00

57 lines
1.6 KiB
Python

# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from __future__ import annotations
import os
import sys
from ctypes import CDLL, CFUNCTYPE, c_char_p
from typing import Callable
import aqt
import aqt.utils
class _MacOSHelper:
def __init__(self) -> None:
if getattr(sys, "frozen", False):
path = os.path.join(sys.prefix, "libankihelper.dylib")
else:
path = os.path.join(
aqt.utils.aqt_data_folder(), "lib", "libankihelper.dylib"
)
self._dll = CDLL(path)
def system_is_dark(self) -> bool:
return self._dll.system_is_dark()
def set_darkmode_enabled(self, enabled: bool) -> bool:
return self._dll.set_darkmode_enabled(enabled)
def start_wav_record(self, path: str, on_error: Callable[[str], None]) -> None:
global _on_audio_error
_on_audio_error = on_error
self._dll.start_wav_record(path.encode("utf8"), _audio_error_callback)
def end_wav_record(self) -> None:
"On completion, file should be saved if no error has arrived."
self._dll.end_wav_record()
# this must not be overwritten or deallocated
@CFUNCTYPE(None, c_char_p) # type: ignore
def _audio_error_callback(msg: str) -> None:
if handler := _on_audio_error:
handler(msg)
_on_audio_error: Callable[[str], None] | None = None
macos_helper: _MacOSHelper | None = None
if sys.platform == "darwin":
try:
macos_helper = _MacOSHelper()
except Exception as e:
print("macos_helper:", e)