-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
starts to implement auto theme support
- Loading branch information
Showing
6 changed files
with
92 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,50 @@ | ||
<template> | ||
<q-btn id="change_theme" | ||
<q-btn :icon="themeIcon" | ||
round | ||
:icon="icon" | ||
class="q-mr-md" | ||
color="dark-grey" | ||
class="q-mr-md" | ||
:title="t('base.theme_switcher.title')" | ||
data-testid="change-theme-button" | ||
@click="store.toggleTheme" /> | ||
data-testid="change-theme-button"> | ||
<q-menu> | ||
<q-list> | ||
<q-item v-for="(icon, theme) in themes" | ||
:key="theme" | ||
v-close-popup | ||
clickable | ||
dense | ||
:active="themeStore.preference === theme" | ||
:title="theme" | ||
:data-testid="`change-theme__${theme}`" | ||
@click="changeTheme(theme)"> | ||
<q-item-section> | ||
<q-icon :name="icon" :alt="theme" /> | ||
</q-item-section> | ||
<q-item-section> | ||
{{ theme }} | ||
</q-item-section> | ||
</q-item> | ||
</q-list> | ||
</q-menu> | ||
</q-btn> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { useThemeStore } from '../../store/theme.js' | ||
import { computed } from 'vue' | ||
import { useTranslation } from '../../composables/i18n.ts' | ||
import { setAppThemeCss } from '../../helpers/theme.ts' | ||
const store = useThemeStore() | ||
const themeStore = useThemeStore() | ||
const t = useTranslation() | ||
const icon = computed(() => (store.dark ? 'light_mode' : 'nightlight')) | ||
</script> | ||
const themes = { | ||
light: 'light_mode', | ||
dark: 'dark_mode', | ||
auto: 'brightness_medium' | ||
} | ||
const changeTheme = (theme) => { | ||
themeStore.setPreference(theme) | ||
setAppThemeCss(themeStore.appTheme) | ||
} | ||
const themeIcon = computed(() => (themes[themeStore.preference])) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import white from '../assets/images/logo/white_96.png' | ||
import blue from '../assets/images/logo/blue_96.png' | ||
import { useThemeStore } from '../store/theme' | ||
import { AppThemes, useThemeStore } from '../store/theme.ts' | ||
import { computed } from 'vue' | ||
|
||
export const useLogo = () => { | ||
const themeStore = useThemeStore() | ||
|
||
return computed(() => { | ||
if (themeStore.dark) { | ||
return white | ||
} else { | ||
if (themeStore.appTheme === AppThemes.light) { | ||
return blue | ||
} else { | ||
return white | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { AppThemes, ThemePreferences, useThemeStore } from '../store/theme.ts' | ||
|
||
export const setAppThemeCss = (theme: AppThemes) => { | ||
if (theme === AppThemes.light) { | ||
document.body.classList.add('theme--light') | ||
document.body.classList.remove('theme--dark') | ||
} else { | ||
document.body.classList.add('theme--dark') | ||
document.body.classList.remove('theme--light') | ||
} | ||
} | ||
|
||
export const setupThemeListener = () => { | ||
const mediaQuery = window.matchMedia('(prefers-color-scheme: light)') | ||
const themeStore = useThemeStore() | ||
|
||
mediaQuery.addEventListener('change', (e) => { | ||
if (themeStore.preference !== ThemePreferences.auto) return | ||
|
||
const appTheme = e.matches ? AppThemes.light : AppThemes.dark | ||
themeStore.appTheme = appTheme | ||
|
||
setAppThemeCss(appTheme) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters