Skip to content

Commit

Permalink
[Automatic] Update albertosottile/darkdetect vendored module (#5394)
Browse files Browse the repository at this point in the history
Co-authored-by: Carreau <Carreau@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and Carreau committed Dec 12, 2022
1 parent 9eeab55 commit 58c0ca8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
10 changes: 8 additions & 2 deletions napari/_vendor/darkdetect/_linux_detect.py
Expand Up @@ -7,12 +7,18 @@
import subprocess

def theme():
# Here we just triage to GTK settings for now
try:
#Using the freedesktop specifications for checking dark mode
out = subprocess.run(
['gsettings', 'get', 'org.gnome.desktop.interface', 'gtk-theme'],
['gsettings', 'get', 'org.gnome.desktop.interface', 'color-scheme'],
capture_output=True)
stdout = out.stdout.decode()
#If not found then trying older gtk-theme method
if len(stdout)<1:
out = subprocess.run(
['gsettings', 'get', 'org.gnome.desktop.interface', 'gtk-theme'],
capture_output=True)
stdout = out.stdout.decode()
except Exception:
return 'Light'
# we have a string, now remove start and end quote
Expand Down
56 changes: 53 additions & 3 deletions napari/_vendor/darkdetect/_mac_detect.py
Expand Up @@ -6,6 +6,19 @@

import ctypes
import ctypes.util
import subprocess
import sys
import os
from pathlib import Path
from typing import Callable

try:
from Foundation import NSObject, NSKeyValueObservingOptionNew, NSKeyValueChangeNewKey, NSUserDefaults
from PyObjCTools import AppHelper
_can_listen = True
except ModuleNotFoundError:
_can_listen = False


try:
# macOS Big Sur+ use "a built-in dynamic linker cache of all system-provided libraries"
Expand Down Expand Up @@ -69,6 +82,43 @@ def isDark():
def isLight():
return theme() == 'Light'

#def listener(callback: typing.Callable[[str], None]) -> None:
def listener(callback):
raise NotImplementedError()

def _listen_child():
"""
Run by a child process, install an observer and print theme on change
"""
import signal
signal.signal(signal.SIGINT, signal.SIG_IGN)

OBSERVED_KEY = "AppleInterfaceStyle"

class Observer(NSObject):
def observeValueForKeyPath_ofObject_change_context_(
self, path, object, changeDescription, context
):
result = changeDescription[NSKeyValueChangeNewKey]
try:
print(f"{'Light' if result is None else result}", flush=True)
except IOError:
os._exit(1)

observer = Observer.new() # Keep a reference alive after installing
defaults = NSUserDefaults.standardUserDefaults()
defaults.addObserver_forKeyPath_options_context_(
observer, OBSERVED_KEY, NSKeyValueObservingOptionNew, 0
)

AppHelper.runConsoleEventLoop()


def listener(callback: Callable[[str], None]) -> None:
if not _can_listen:
raise NotImplementedError()
with subprocess.Popen(
(sys.executable, "-c", "import _mac_detect as m; m._listen_child()"),
stdout=subprocess.PIPE,
universal_newlines=True,
cwd=Path(__file__).parent,
) as p:
for line in p.stdout:
callback(line.strip())

0 comments on commit 58c0ca8

Please sign in to comment.