Skip to content

Commit fca9783

Browse files
authored
Order Themes in Theme Picker (#10398)
This allows us to specify a weight for each theme. Themes will be ordered with higher weights at the bottom. If a theme does not have a weight, it is put last. I've also moved when themes are loaded to before the page is shown, so there is no time where the default theme will be visible (unless something goes wrong).
1 parent 071ea30 commit fca9783

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

cli/cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,7 @@ function updateColorThemes(cfg: pxt.TargetBundle) {
23082308
const theme: pxt.ColorThemeInfo = {
23092309
id: themeData.id,
23102310
name: themeData.name,
2311+
weight: themeData.weight,
23112312
monacoBaseTheme: themeData.monacoBaseTheme,
23122313
colors: themeData.colors
23132314
};

localtypings/pxtarget.d.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -614,11 +614,12 @@ declare namespace pxt {
614614
}
615615

616616
interface ColorThemeInfo {
617-
id: string;
618-
name: string;
619-
overrideCss?: string;
620-
monacoBaseTheme?: string; // https://code.visualstudio.com/docs/getstarted/themes
621-
colors: { [key: string]: string };
617+
id: string; // Unique identifier
618+
name: string; // Human-readable name
619+
weight?: number; // Lower weights appear first in theme list, no value = go to end
620+
overrideCss?: string; // Special css to apply for the theme
621+
monacoBaseTheme?: string; // Theme for monaco editor, see https://code.visualstudio.com/docs/getstarted/themes
622+
colors: { [key: string]: string }; // Values for theme variables
622623
}
623624

624625
interface ServiceWorkerEvent {

react-common/components/theming/themeManager.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@ export class ThemeManager {
3131
}
3232

3333
public getAllColorThemes(): pxt.ColorThemeInfo[] {
34-
return pxt.appTarget?.colorThemeMap ? Object.values(pxt.appTarget.colorThemeMap) : [];
34+
const allThemes = pxt.appTarget?.colorThemeMap ? Object.values(pxt.appTarget.colorThemeMap) : [];
35+
return allThemes.sort((a, b) => {
36+
// Lower weight at the front.
37+
if (a.weight !== b.weight) {
38+
return (a.weight ?? Infinity) - (b.weight ?? Infinity);
39+
}
40+
else {
41+
return a.id.localeCompare(b.id);
42+
}
43+
});
3544
}
3645

3746
public isHighContrast(themeId: string) {

theme/color-themes/high-contrast.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"id": "pxt-high-contrast",
33
"name": "High Contrast",
4+
"weight": 100,
45
"monacoBaseTheme": "hc-black",
56
"overrideFiles": [
67
"/overrides/high-contrast-overrides.css"

webapp/src/app.tsx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6224,6 +6224,28 @@ document.addEventListener("DOMContentLoaded", async () => {
62246224
.then(simStrings => simStrings && simulator.setTranslations(simStrings))
62256225
});
62266226
})
6227+
.then(() => {
6228+
// Load theme colors
6229+
let initialTheme = data.getData<string>(auth.THEMEID);
6230+
if (!initialTheme) {
6231+
initialTheme = pxt.appTarget?.appTheme?.defaultColorTheme;
6232+
}
6233+
6234+
// We have a legacy preference stored if the user has enabled high contrast.
6235+
// Respect it here by switching to the hc color theme.
6236+
const hcEnabled = data.getData<boolean>(auth.HIGHCONTRAST);
6237+
if (hcEnabled) {
6238+
initialTheme = pxt.appTarget?.appTheme?.highContrastColorTheme;
6239+
}
6240+
6241+
if (initialTheme) {
6242+
const themeManager = ThemeManager.getInstance(document);
6243+
if (initialTheme !== themeManager.getCurrentColorTheme()?.id) {
6244+
return themeManager.switchColorTheme(initialTheme);
6245+
}
6246+
}
6247+
return Promise.resolve();
6248+
})
62276249
.then(() => {
62286250
pxt.BrowserUtils.initTheme();
62296251
theme = pxteditor.experiments.syncTheme();
@@ -6256,28 +6278,6 @@ document.addEventListener("DOMContentLoaded", async () => {
62566278
socketbridge.tryInit();
62576279
electron.initElectron(theEditor);
62586280
})
6259-
.then(() => {
6260-
// Load theme colors
6261-
let initialTheme = data.getData<string>(auth.THEMEID);
6262-
if (!initialTheme) {
6263-
initialTheme = pxt.appTarget?.appTheme?.defaultColorTheme;
6264-
}
6265-
6266-
// We have a legacy preference stored if the user has enabled high contrast.
6267-
// Respect it here by switching to the hc color theme.
6268-
const hcEnabled = data.getData<boolean>(auth.HIGHCONTRAST);
6269-
if (hcEnabled) {
6270-
initialTheme = pxt.appTarget?.appTheme?.highContrastColorTheme;
6271-
}
6272-
6273-
if (initialTheme) {
6274-
const themeManager = ThemeManager.getInstance(document);
6275-
if (initialTheme !== themeManager.getCurrentColorTheme()?.id) {
6276-
return themeManager.switchColorTheme(initialTheme);
6277-
}
6278-
}
6279-
return Promise.resolve();
6280-
})
62816281
.then(() => {
62826282
const showHome = theEditor.shouldShowHomeScreen();
62836283
if (!showHome) {

0 commit comments

Comments
 (0)