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

feat: improve theme auto-selection #296

Merged
merged 6 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ipyvuetify/Themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Theme(Widget):

dark = Bool(None, allow_none=True).tag(sync=True)

dark_jlab = Bool(None, allow_none=True).tag(sync=True)
dark_effective = Bool(None, allow_none=True).tag(sync=True)

def __init__(self):
super().__init__()
Expand Down
51 changes: 39 additions & 12 deletions js/src/Themes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ThemeModel extends WidgetModel {
_view_module_version: "0.1.11",
_model_module_version: "0.1.11",
dark: null,
dark_jlab: null,
dark_effective: null,
},
};
}
Expand All @@ -28,32 +28,59 @@ export class ThemeModel extends WidgetModel {
if (ThemeModel.themeManager) {
ThemeModel.themeManager.themeChanged.connect(() => {
if (this.get("dark") === null) {
vuetify.framework.theme.dark =
document.body.dataset.jpThemeLight === "false";
this.set("dark_jlab", vuetify.framework.theme.dark);
this.set("dark_effective", vuetify.framework.theme.dark);
this.save_changes();
}
}, this);
}
this.setTheme();

this.on("change:dark", () => {
this.setTheme();
});

setInterval(() => {
this.vuetifyThemeChange();
}, 1000);
}

setTheme() {
if (this.get("dark") !== null) {
vuetify.framework.theme.dark = this.get("dark");
} else if (document.body.dataset.jpThemeLight) {
vuetify.framework.theme.dark =
document.body.dataset.jpThemeLight === "false";
this.set("dark_jlab", vuetify.framework.theme.dark);
this.save_changes();
} else if (document.body.classList.contains("theme-dark")) {
vuetify.framework.theme.dark = true;
this.set("dark", true);
this.save_changes();
} else if (document.body.classList.contains("theme-light")) {
this.set("dark", false);
vuetify.framework.theme.dark = false;
} else if (window.Jupyter) {
// Special case for Jupyter Notebook
vuetify.framework.theme.dark = false;
} else if (document.body.dataset.vscodeThemeKind === "vscode-dark") {
// Special case for VS Code
vuetify.framework.theme.dark = true;
} else if (document.body.dataset.vscodeThemeKind === "vscode-light") {
vuetify.framework.theme.dark = false;
} else if (document.documentElement.matches("[theme=dark]")) {
// Special case for Colab
vuetify.framework.theme.dark = true;
} else if (document.documentElement.matches("[theme=light]")) {
vuetify.framework.theme.dark = false;
}
this.set("dark_effective", vuetify.framework.theme.dark);
this.save_changes();
}

vuetifyThemeChange() {
if (
this.get("dark") !== null &&
this.get("dark") !== vuetify.framework.theme.dark
) {
this.set("dark", vuetify.framework.theme.dark);
this.set("dark_effective", vuetify.framework.theme.dark);
this.save_changes();
}
Comment on lines +80 to 88
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
vuetifyThemeChange() {
if (
this.get("dark") !== null &&
this.get("dark") !== vuetify.framework.theme.dark
) {
this.set("dark", vuetify.framework.theme.dark);
this.set("dark_effective", vuetify.framework.theme.dark);
this.save_changes();
}
vuetifyThemeChange() {
if (
this.get("dark") !== null &&
this.get("dark") !== vuetify.framework.theme.dark
) {
this.set("dark", vuetify.framework.theme.dark);
}
this.set("dark_effective", vuetify.framework.theme.dark);
this.save_changes();

I think we always want to set dark_effective right?

this.on("change:dark", () => {
vuetify.framework.theme.dark = this.get("dark");
});
}
}

Expand Down