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(app): add system theme #2326

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions apps/app-frontend/src/store/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineStore } from 'pinia'

export const useTheming = defineStore('themeStore', {
state: () => ({
themeOptions: ['dark', 'light', 'oled'],
themeOptions: ['system', 'dark', 'light', 'oled'],
advancedRendering: true,
selectedTheme: 'dark',
}),
Expand All @@ -17,7 +17,15 @@ export const useTheming = defineStore('themeStore', {
for (const theme of this.themeOptions) {
document.getElementsByTagName('html')[0].classList.remove(`${theme}-mode`)
}
document.getElementsByTagName('html')[0].classList.add(`${this.selectedTheme}-mode`)
let theme = this.selectedTheme
if (theme === 'system') {
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)')
Copy link
Contributor

Choose a reason for hiding this comment

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

window.matchMedia('(prefers-color-scheme: dark)') always returns true for me. Maybe use Tauri's api. It has methods for getting current theme & adding event listener for it.

theme = prefersDark.matches ? 'dark' : 'light'
prefersDark.addEventListener('change', () => {
this.setThemeClass()
})
}
document.getElementsByTagName('html')[0].classList.add(`${theme}-mode`)
},
},
})
3 changes: 3 additions & 0 deletions packages/app-lib/src/state/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ pub enum Theme {
Dark,
Light,
Oled,
System,
}

impl Theme {
Expand All @@ -189,6 +190,7 @@ impl Theme {
Theme::Dark => "dark",
Theme::Light => "light",
Theme::Oled => "oled",
Theme::System => "system",
}
}

Expand All @@ -197,6 +199,7 @@ impl Theme {
"dark" => Theme::Dark,
"light" => Theme::Light,
"oled" => Theme::Oled,
"system" => Theme::System,
_ => Theme::Dark,
}
}
Expand Down