Skip to content

Commit e314e9b

Browse files
committed
fix dark mode issues
1 parent 7abcc25 commit e314e9b

5 files changed

Lines changed: 29 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ docs/html
1212
/docs/.virtual_documents/
1313
/.virtual_documents/
1414
/tests/output/
15+
/docs/_static/webgpu_scenes/

docs/_static/showcase.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:4670cf7ee376ee7d5514a6f0b61862975febb5c83a9ff76aa8f076758900afa3
3-
size 345586
2+
oid sha256:e2c1ed6016aefde9f1047f0bf3cbea75275e4dc6a8bcf76ad53a9b8b3bf9fe2d
3+
size 346545

docs/create_showcase.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
@media (prefers-color-scheme: dark) {{
6161
#showcase_root {{ --webgpu-canvas-bg: #adadad; }}
6262
}}
63+
html[data-theme="dark"] #showcase_root {{ --webgpu-canvas-bg: #adadad; }}
6364
</style>
6465
<div id="showcase_root" style="width:min(800px,100%); max-width:100%; margin:0 auto 1em auto; position:relative;">
6566
<canvas id="{canvas_id}" width="800" height="400" style="background-color:var(--webgpu-canvas-bg,#ffffff); width:100%; max-width:100%; height:auto; aspect-ratio:800 / 400; touch-action:none; border-radius:8px; display:block;"></canvas>

webgpu/engine/engine.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,11 @@ class RenderEngine {
873873

874874
_isDarkMode() {
875875
try {
876+
// Prefer explicit page-level theme (e.g. pydata-sphinx-theme toggle)
877+
const htmlTheme = document.documentElement && document.documentElement.dataset && document.documentElement.dataset.theme;
878+
if (htmlTheme === 'dark') return true;
879+
if (htmlTheme === 'light') return false;
880+
// Fall back to OS-level preference
876881
return !!(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
877882
} catch {
878883
return false;
@@ -899,6 +904,17 @@ class RenderEngine {
899904
} else if (this._themeMediaQuery.addListener) {
900905
this._themeMediaQuery.addListener(this._onThemeChange);
901906
}
907+
// Also observe data-theme attribute changes (e.g. pydata-sphinx-theme)
908+
if (document.documentElement && typeof MutationObserver !== 'undefined') {
909+
this._themeAttrObserver = new MutationObserver(() => {
910+
this._applyTheme();
911+
this.render();
912+
});
913+
this._themeAttrObserver.observe(document.documentElement, {
914+
attributes: true,
915+
attributeFilter: ['data-theme'],
916+
});
917+
}
902918
}
903919

904920
_teardownThemeObserver() {
@@ -910,6 +926,10 @@ class RenderEngine {
910926
}
911927
this._themeMediaQuery = null;
912928
this._onThemeChange = null;
929+
if (this._themeAttrObserver) {
930+
this._themeAttrObserver.disconnect();
931+
this._themeAttrObserver = null;
932+
}
913933
}
914934

915935
dispose() {

webgpu/jupyter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def _init_html(scene, width, height, flex=None):
4141
@media (prefers-color-scheme: dark) {{
4242
#{id_}root {{ --webgpu-canvas-bg: #adadad; }}
4343
}}
44+
html[data-theme="dark"] #{id_}root {{ --webgpu-canvas-bg: #adadad; }}
4445
</style>
4546
<div id='{id_}root'
4647
style="position: relative; width: min({width}px, 100%); max-width: 100%; overflow: hidden;"
@@ -227,24 +228,23 @@ def _DrawHTMLLazy(scene, width=640, height=600):
227228
lazy_html = f"""
228229
<style>
229230
#{id_}root {{ --webgpu-canvas-bg: #ffffff; }}
230-
#{id_}img_light {{ display: block; }}
231-
#{id_}img_dark {{ display: none; }}
232231
@media (prefers-color-scheme: dark) {{
233232
#{id_}root {{ --webgpu-canvas-bg: #adadad; }}
234-
#{id_}img_light {{ display: none; }}
235-
#{id_}img_dark {{ display: block; }}
236233
}}
234+
html[data-theme="dark"] #{id_}root {{ --webgpu-canvas-bg: #adadad; }}
237235
</style>
238236
<div id='{id_}root'
239237
style="position: relative; width: min({width}px, 100%); max-width: 100%; overflow: hidden;"
240238
>
241239
<img id='{id_}img_light'
240+
class='only-light dark-light'
242241
src='{screenshot_light_url}'
243242
style='width: 100%; max-width: 100%; height: auto; aspect-ratio: {width} / {height}; display: block;'
244243
/>
245244
<img id='{id_}img_dark'
245+
class='only-dark dark-light'
246246
src='{screenshot_dark_url}'
247-
style='width: 100%; max-width: 100%; height: auto; aspect-ratio: {width} / {height}; display: none;'
247+
style='width: 100%; max-width: 100%; height: auto; aspect-ratio: {width} / {height}; display: block;'
248248
/>
249249
<div id='{id_}overlay'
250250
style='position: absolute; top: 0; left: 0; width: 100%; height: 100%;

0 commit comments

Comments
 (0)