Skip to content

Commit

Permalink
Allow OS dark mode overrides for dynamic themes
Browse files Browse the repository at this point in the history
  • Loading branch information
aivarannamaa committed Jul 1, 2023
1 parent ed2ba96 commit 3901af8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
14 changes: 12 additions & 2 deletions thonny/plugins/base_ui_themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,14 @@ def enhanced_aqua() -> CompoundUiThemeSettings:
]


def enhanced_aqua_dark_overrides():
return [
{
"Url.TLabel": {"configure": {"foreground": "#6699FF"}},
}
]


def load_plugin() -> None:
from tkinter import ttk

Expand All @@ -730,11 +738,13 @@ def load_plugin() -> None:
"Enhanced Clam",
"clam",
enhanced_clam,
{"tab-close": "tab-close-clam", "tab-close-active": "tab-close-active-clam"},
images={"tab-close": "tab-close-clam", "tab-close-active": "tab-close-active-clam"},
)

if "vista" in original_themes:
get_workbench().add_ui_theme("Windows", "vista", windows)

if "aqua" in original_themes:
get_workbench().add_ui_theme("Kind of Aqua", "aqua", enhanced_aqua)
get_workbench().add_ui_theme(
"Kind of Aqua", "aqua", enhanced_aqua, enhanced_aqua_dark_overrides
)
6 changes: 3 additions & 3 deletions thonny/plugins/clean_ui_themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def load_plugin() -> None:
high_foreground="#eeeeee",
low_foreground="#666666",
),
dark_images,
images=dark_images,
)

dark_tip_background = ("#b8c28d",)
Expand All @@ -350,7 +350,7 @@ def load_plugin() -> None:
high_foreground="#eeeeee",
low_foreground="#5a725b",
),
dark_images,
images=dark_images,
)

get_workbench().add_ui_theme(
Expand All @@ -366,7 +366,7 @@ def load_plugin() -> None:
high_foreground="#eeeeee",
low_foreground="#5a5c72",
),
dark_images,
images=dark_images,
)

get_workbench().add_ui_theme(
Expand Down
6 changes: 4 additions & 2 deletions thonny/plugins/pi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,7 @@ def load_plugin():
for image in images:
theme_image_map[image] = os.path.join(res_dir, images[image])

get_workbench().add_ui_theme("Raspberry Pi", "Enhanced Clam", pix, theme_image_map)
get_workbench().add_ui_theme("Raspberry Pi Dark", "Clean Dark", pix_dark, theme_image_map)
get_workbench().add_ui_theme("Raspberry Pi", "Enhanced Clam", pix, images=theme_image_map)
get_workbench().add_ui_theme(
"Raspberry Pi Dark", "Clean Dark", pix_dark, images=theme_image_map
)
23 changes: 17 additions & 6 deletions thonny/workbench.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
get_hyperlink_cursor,
get_style_configuration,
lookup_style_option,
os_is_in_dark_mode,
register_latin_shortcut,
select_sequence,
sequence_to_accelerator,
Expand Down Expand Up @@ -918,9 +919,16 @@ def _on_backend_restart(self, event):

def _init_theming(self) -> None:
self._style = ttk.Style()
self._ui_themes = (
{}
) # type: Dict[str, Tuple[Optional[str], FlexibleUiThemeSettings, Dict[str, str]]] # value is (parent, settings, images)
# value is (parent, settings, overrides, images)
self._ui_themes: Dict[
str,
Tuple[
Optional[str],
FlexibleUiThemeSettings,
Optional[FlexibleUiThemeSettings],
Dict[str, str],
],
] = {}
self._syntax_themes = (
{}
) # type: Dict[str, Tuple[Optional[str], FlexibleSyntaxThemeSettings]] # value is (parent, settings)
Expand Down Expand Up @@ -1242,12 +1250,13 @@ def add_ui_theme(
name: str,
parent: Union[str, None],
settings: FlexibleUiThemeSettings,
dark_mode_overrides: Optional[FlexibleUiThemeSettings] = None,
images: Dict[str, str] = {},
) -> None:
if name in self._ui_themes:
warn(tr("Overwriting theme '%s'") % name)

self._ui_themes[name] = (parent, settings, images)
self._ui_themes[name] = (parent, settings, dark_mode_overrides, images)

def add_syntax_theme(
self, name: str, parent: Optional[str], settings: FlexibleSyntaxThemeSettings
Expand Down Expand Up @@ -1288,8 +1297,10 @@ def _register_ui_theme_as_tk_theme(self, name: str) -> None:
total_images = {} # type: Dict[str, str]
temp_name = name
while True:
parent, settings, images = self._ui_themes[temp_name]
parent, settings, overrides, images = self._ui_themes[temp_name]
total_settings.insert(0, settings)
if overrides and os_is_in_dark_mode():
total_settings.append(overrides)
for img_name in images:
total_images.setdefault(img_name, images[img_name])

Expand Down Expand Up @@ -1414,7 +1425,7 @@ def uses_dark_ui_theme(self) -> bool:
if "dark" in name.lower():
return True
try:
name, _, _ = self._ui_themes[name]
name, _, _, _ = self._ui_themes[name]
except KeyError:
return False

Expand Down

0 comments on commit 3901af8

Please sign in to comment.