Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle the "default" desktop color scheme setting gracefully #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ Fix auto dark mode for sublime v4 on Linux. Sublime text 4 supposedly has such a
- Edit your preferences and configure the following keys:
- `dark_color_scheme`
- `light_color_scheme`
- `default_color scheme` (`dark`, `light`, or a color scheme name)
- `dark_theme`
- `light_theme`
- `default_theme` (`dark`, `light`, or a theme name)
- Either from the context menu (under View), or from the command pallet (search for AutoDark)
- `System`: Sublime follows the system settings
- `Light`: Sublime sticks to only light mode
Expand Down
14 changes: 8 additions & 6 deletions helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sublime

from typing import Callable, Any, Literal, TypeVar, cast
from typing import Callable, Any, Final, Literal, Sequence, TypeVar, cast
from functools import wraps, reduce
from types import MappingProxyType
import operator
Expand Down Expand Up @@ -44,18 +44,20 @@ def wrapper():
return decorator


colorSchemeMap = MappingProxyType({1: "dark", 2: "light"})
ColorScheme = Literal["default", "dark", "light"]
colorSchemeMap: Final[Sequence[ColorScheme]] = ColorScheme.__args__
ColorSchemeEnum = Literal[range(len(colorSchemeMap))]


def parse_dbus_call(output: str) -> Literal["dark", "light"]:
def parse_dbus_call(output: str) -> ColorScheme:
result = json.loads(output)
system_scheme: Literal[1, 2] = result["data"][0]["data"]
system_scheme: ColorSchemeEnum = result["data"][0]["data"]
return cast(Any, colorSchemeMap[system_scheme])


def parse_dbus_monitor(output: str) -> Literal["dark", "light"]:
def parse_dbus_monitor(output: str) -> ColorScheme:
result = json.loads(output)
system_scheme: Literal[1, 2] = result["payload"]["data"][2]["data"]
system_scheme: ColorSchemeEnum = result["payload"]["data"][2]["data"]
return cast(Any, colorSchemeMap[system_scheme])


Expand Down
28 changes: 20 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import contextlib
import uuid

from .helpers import settings_watcher, read_system_theme, parse_dbus_monitor
from .helpers import (
settings_watcher, read_system_theme, parse_dbus_monitor,
ColorScheme)
from .lifecycle import lifecycle, CycleStage
from .logger import logger

Expand Down Expand Up @@ -175,15 +177,25 @@ def unmonitor():
logger.info("Daemon stopped")


def change_color_scheme(new_scheme: str, old_scheme: Optional[str] = None):
def change_color_scheme(new_scheme: ColorScheme, old_scheme: Optional[ColorScheme] = None):
settings = sublime.load_settings("Preferences.sublime-settings")
ui_info = sublime.ui_info()
if (theme := settings.get(f"{new_scheme}_theme")) != ui_info.get("theme").get("resolved_value"):
settings["theme"] = theme
if (color_scheme := settings.get(f"{new_scheme}_color_scheme")) != ui_info.get(
"color_scheme"
).get("resolved_value"):
settings["color_scheme"] = color_scheme
for what in ("color_scheme", "theme"):
value = settings.get(f"{new_scheme}_{what}")
if new_scheme == "default":
# Desktops may ask applications to use "default" UI colors. Let
# users choose what to do through configuration.
if not value:
# ...but if they don't we'll choose for them.
# In GNOME and Sublime Text the default would be "light".
value = "light"
if value in ("light", "dark"):
# These special aliases are resolved through their respective
# configuration settings.
new_scheme = value
value = settings.get(f"{new_scheme}_{what}")
if value != ui_info.get(what).get("resolved_value"):
settings[what] = value

if new_scheme != old_scheme:
logger.info(f"Color scheme change detected: previous={old_scheme}, new={new_scheme}")
Expand Down