Skip to content

Commit 5b52636

Browse files
authored
gh-142927: Tachyon: Start with user's default light/dark theme (#142987)
1 parent e46f28c commit 5b52636

File tree

5 files changed

+40
-53
lines changed

5 files changed

+40
-53
lines changed

Lib/profiling/sampling/_heatmap_assets/heatmap.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,13 @@ let coldCodeHidden = false;
1515
// ============================================================================
1616

1717
function toggleTheme() {
18-
const html = document.documentElement;
19-
const current = html.getAttribute('data-theme') || 'light';
20-
const next = current === 'light' ? 'dark' : 'light';
21-
html.setAttribute('data-theme', next);
22-
localStorage.setItem('heatmap-theme', next);
23-
24-
// Update theme button icon
25-
const btn = document.getElementById('theme-btn');
26-
if (btn) {
27-
btn.querySelector('.icon-moon').style.display = next === 'dark' ? 'none' : '';
28-
btn.querySelector('.icon-sun').style.display = next === 'dark' ? '' : 'none';
29-
}
18+
toggleAndSaveTheme();
3019
applyLineColors();
3120

3221
// Rebuild scroll marker with new theme colors
3322
buildScrollMarker();
3423
}
3524

36-
function restoreUIState() {
37-
// Restore theme
38-
const savedTheme = localStorage.getItem('heatmap-theme');
39-
if (savedTheme) {
40-
document.documentElement.setAttribute('data-theme', savedTheme);
41-
const btn = document.getElementById('theme-btn');
42-
if (btn) {
43-
btn.querySelector('.icon-moon').style.display = savedTheme === 'dark' ? 'none' : '';
44-
btn.querySelector('.icon-sun').style.display = savedTheme === 'dark' ? '' : 'none';
45-
}
46-
}
47-
}
48-
4925
// ============================================================================
5026
// Utility Functions
5127
// ============================================================================

Lib/profiling/sampling/_heatmap_assets/heatmap_index.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,10 @@ function applyHeatmapBarColors() {
1919
// ============================================================================
2020

2121
function toggleTheme() {
22-
const html = document.documentElement;
23-
const current = html.getAttribute('data-theme') || 'light';
24-
const next = current === 'light' ? 'dark' : 'light';
25-
html.setAttribute('data-theme', next);
26-
localStorage.setItem('heatmap-theme', next);
27-
28-
// Update theme button icon
29-
const btn = document.getElementById('theme-btn');
30-
if (btn) {
31-
btn.querySelector('.icon-moon').style.display = next === 'dark' ? 'none' : '';
32-
btn.querySelector('.icon-sun').style.display = next === 'dark' ? '' : 'none';
33-
}
34-
22+
toggleAndSaveTheme();
3523
applyHeatmapBarColors();
3624
}
3725

38-
function restoreUIState() {
39-
// Restore theme
40-
const savedTheme = localStorage.getItem('heatmap-theme');
41-
if (savedTheme) {
42-
document.documentElement.setAttribute('data-theme', savedTheme);
43-
const btn = document.getElementById('theme-btn');
44-
if (btn) {
45-
btn.querySelector('.icon-moon').style.display = savedTheme === 'dark' ? 'none' : '';
46-
btn.querySelector('.icon-sun').style.display = savedTheme === 'dark' ? '' : 'none';
47-
}
48-
}
49-
}
50-
5126
// ============================================================================
5227
// Type Section Toggle (stdlib, project, etc)
5328
// ============================================================================

Lib/profiling/sampling/_heatmap_assets/heatmap_index_template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!doctype html>
2-
<html lang="en" data-theme="light">
2+
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">

Lib/profiling/sampling/_heatmap_assets/heatmap_pyfile_template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!doctype html>
2-
<html lang="en" data-theme="light">
2+
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">

Lib/profiling/sampling/_heatmap_assets/heatmap_shared.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,42 @@ function intensityToColor(intensity) {
3939
return rootStyle.getPropertyValue(`--heat-${level}`).trim();
4040
}
4141

42+
// ============================================================================
43+
// Theme Support
44+
// ============================================================================
45+
46+
// Get the preferred theme from localStorage or browser preference
47+
function getPreferredTheme() {
48+
const saved = localStorage.getItem('heatmap-theme');
49+
if (saved) return saved;
50+
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
51+
}
52+
53+
// Apply theme and update UI. Returns the applied theme.
54+
function applyTheme(theme) {
55+
document.documentElement.setAttribute('data-theme', theme);
56+
const btn = document.getElementById('theme-btn');
57+
if (btn) {
58+
btn.querySelector('.icon-moon').style.display = theme === 'dark' ? 'none' : '';
59+
btn.querySelector('.icon-sun').style.display = theme === 'dark' ? '' : 'none';
60+
}
61+
return theme;
62+
}
63+
64+
// Toggle theme and save preference. Returns the new theme.
65+
function toggleAndSaveTheme() {
66+
const current = document.documentElement.getAttribute('data-theme') || 'light';
67+
const next = current === 'light' ? 'dark' : 'light';
68+
applyTheme(next);
69+
localStorage.setItem('heatmap-theme', next);
70+
return next;
71+
}
72+
73+
// Restore theme from localStorage, or use browser preference
74+
function restoreUIState() {
75+
applyTheme(getPreferredTheme());
76+
}
77+
4278
// ============================================================================
4379
// Favicon (Reuse logo image as favicon)
4480
// ============================================================================

0 commit comments

Comments
 (0)