add minimal theme detection on Linux

Closes #1116
This commit is contained in:
Damien Elmes 2021-11-25 08:41:15 +10:00
parent f2173fddb0
commit f0fedf6ab8
3 changed files with 40 additions and 6 deletions

View File

@ -31,7 +31,7 @@ from anki.decks import DeckDict, DeckId
from anki.hooks import runHook
from anki.notes import NoteId
from anki.sound import AVTag, SoundOrVideoTag
from anki.utils import devMode, ids2str, int_time, isMac, isWin, split_fields
from anki.utils import devMode, ids2str, int_time, isLin, isMac, isWin, split_fields
from aqt import gui_hooks
from aqt.addons import DownloadLogEntry, check_and_prompt_for_updates, show_log_to_user
from aqt.dbcheck import check_db
@ -1005,8 +1005,17 @@ title="{}" {}>{}</button>""".format(
def setupStyle(self) -> None:
theme_manager.apply_style()
if isLin:
# On Linux, the check requires invoking an external binary,
# which we don't want to be doing frequently
interval_secs = 300
else:
interval_secs = 5
self.progress.timer(
5 * 1000, theme_manager.apply_style_if_system_style_changed, True, False
interval_secs * 1000,
theme_manager.apply_style_if_system_style_changed,
True,
False,
)
def set_theme(self, theme: Theme) -> None:

View File

@ -6,11 +6,12 @@
from __future__ import annotations
import os
import subprocess
import sys
from ctypes import CDLL
import aqt.utils
from anki.utils import isMac, isWin
from anki.utils import isLin, isMac, isWin
def get_windows_dark_mode() -> bool:
@ -56,6 +57,26 @@ def get_macos_dark_mode() -> bool:
return False
def get_linux_dark_mode() -> bool:
"""True if Linux system is in dark mode.
This only works if the GTK theme name contains '-dark'"""
if not isLin:
return False
try:
process = subprocess.run(
["gsettings", "get", "org.gnome.desktop.interface", "gtk-theme"],
check=True,
capture_output=True,
encoding="utf8",
)
except FileNotFoundError as e:
# swallow exceptions, as gsettings may not be installed
print(e)
return False
return "-dark" in process.stdout.lower()
_ankihelper_dll: CDLL | None = None

View File

@ -10,7 +10,12 @@ from dataclasses import dataclass
import aqt
from anki.utils import isMac
from aqt import QApplication, colors, gui_hooks, isWin
from aqt.platform import get_macos_dark_mode, get_windows_dark_mode, set_macos_dark_mode
from aqt.platform import (
get_linux_dark_mode,
get_macos_dark_mode,
get_windows_dark_mode,
set_macos_dark_mode,
)
from aqt.qt import (
QColor,
QGuiApplication,
@ -164,8 +169,7 @@ class ThemeManager:
elif isMac:
return get_macos_dark_mode()
else:
# not supported on Linux
return False
return get_linux_dark_mode()
def apply_style_if_system_style_changed(self) -> None:
theme = aqt.mw.pm.theme()