Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85949950
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/boot/after_store.js b/src/boot/after_store.js
index 3ab8d943ec..79c693a514 100644
--- a/src/boot/after_store.js
+++ b/src/boot/after_store.js
@@ -560,6 +560,15 @@ const afterStoreSetup = async ({ pinia, store, storageError, i18n }) => {
})
}
+ const darkModeMatcher = window.matchMedia?.('(prefers-color-scheme: dark)')
+ const darkModeHandler = ({ matches } = {}) => {
+ if (matches !== undefined) {
+ useInterfaceStore().setPrefersDarkMode(matches)
+ }
+ }
+ darkModeMatcher.addEventListener('change', darkModeHandler)
+ darkModeHandler(darkModeMatcher)
+
useInterfaceStore().setLayoutWidth(windowWidth())
useInterfaceStore().setLayoutHeight(windowHeight())
diff --git a/src/services/style_setter/style_setter.js b/src/services/style_setter/style_setter.js
index 03dc3743bb..933e06aaef 100644
--- a/src/services/style_setter/style_setter.js
+++ b/src/services/style_setter/style_setter.js
@@ -181,12 +181,8 @@ export const tryLoadCache = async () => {
export const applyTheme = (
input,
- onEagerFinish = () => {
- /* no-op */
- },
- onFinish = () => {
- /* no-op */
- },
+ onEagerFinish,
+ onFinish,
debug = false,
) => {
const eagerStyles = createStyleSheet(EAGER_STYLE_ID, 10)
@@ -208,7 +204,7 @@ export const applyTheme = (
onEagerFinished() {
eagerStyles.ready = true
adoptStyleSheets()
- onEagerFinish()
+ onEagerFinish?.()
console.info(
'Eager part of theme finished, waiting for lazy part to finish to store cache',
)
@@ -223,7 +219,7 @@ export const applyTheme = (
engineChecksum: getEngineChecksum(),
data,
}
- onFinish(cache)
+ onFinish?.(cache)
localforage.setItem('pleromafe-theme-cache', cache)
console.info('Theme cache stored')
},
diff --git a/src/stores/interface.js b/src/stores/interface.js
index 322885892d..a2b408a501 100644
--- a/src/stores/interface.js
+++ b/src/stores/interface.js
@@ -54,6 +54,7 @@ const GENERIC_FONT_NAMES = new Set([
export const useInterfaceStore = defineStore('interface', {
state: () => ({
localFonts: null,
+ prefersDarkMode: null,
themeApplied: false,
themeChangeInProgress: false,
themeVersion: 'v3',
@@ -328,6 +329,12 @@ export const useInterfaceStore = defineStore('interface', {
this.layoutType = wideLayout ? 'wide' : normalOrMobile
}
},
+ setPrefersDarkMode(value) {
+ if (this.prefersDark !== value) {
+ this.prefersDark = value
+ this.applyTheme()
+ }
+ },
setFontsList(value) {
this.localFonts = [...new Set(value.map(({ family }) => family)).values()]
},
@@ -453,11 +460,8 @@ export const useInterfaceStore = defineStore('interface', {
this.setPreference({ path: 'style', value, store })
this.pushPreferences({ store })
- this.useStylePalette = true
- this.applyTheme({ recompile: true, store }).then(() => {
- this.useStylePalette = false
- })
+ this.applyTheme({ recompile: true, store })
},
setStyleCustom(value, store) {
this.resetThemeV3(store)
@@ -471,10 +475,7 @@ export const useInterfaceStore = defineStore('interface', {
})
this.pushPreferences({ store })
- this.useStylePalette = true
- this.applyTheme({ recompile: true, store }).then(() => {
- this.useStylePalette = false
- })
+ this.applyTheme({ recompile: true, store })
},
async fetchThemesIndex() {
try {
@@ -593,8 +594,6 @@ export const useInterfaceStore = defineStore('interface', {
palette: instancePaletteName,
} = useInstanceStore().instanceIdentity
- let { themesIndex, stylesIndex, palettesIndex } = useInstanceStore()
-
const {
style: userStyleName,
styleCustomData: userStyleCustomData,
@@ -652,18 +651,13 @@ export const useInterfaceStore = defineStore('interface', {
}
if (majorVersionUsed === 'v3') {
- const result = await Promise.all([
+ await Promise.all([
this.fetchPalettesIndex(),
this.fetchStylesIndex(),
])
-
- palettesIndex = result[0]
- stylesIndex = result[1]
} else {
// Promise.all just to be uniform with v3
- const result = await Promise.all([this.fetchThemesIndex()])
-
- themesIndex = result[0]
+ await Promise.all([this.fetchThemesIndex()])
}
this.themeVersion = majorVersionUsed
@@ -676,37 +670,43 @@ export const useInterfaceStore = defineStore('interface', {
const style = await getData(
'style',
- stylesIndex,
+ useInstanceStore().stylesIndex,
userStyleCustomData,
userStyleName || instanceStyleName,
)
this.styleNameUsed = style.nameUsed
this.styleDataUsed = style.dataUsed
- let firstStylePaletteName = null
- style.dataUsed
- ?.filter((x) => x.component === '@palette')
- .map((x) => {
- const cleanDirectives = Object.fromEntries(
- Object.entries(x.directives).filter(([k]) => k),
- )
+ const stylePalettes = Object.fromEntries(
+ (style.dataUsed ?? [])
+ .filter((x) => x.component === '@palette')
+ .map((x) => {
+ const cleanDirectives = Object.fromEntries(
+ Object.entries(x.directives).filter(([k]) => k),
+ )
- return { name: x.variant, ...cleanDirectives }
- })
- .forEach((palette) => {
- const key =
- 'style.' + palette.name.toLowerCase().replaceAll(' ', '_')
- if (!firstStylePaletteName) firstStylePaletteName = key
- palettesIndex[key] = () => Promise.resolve(palette)
- })
+ return [
+ x.variant.replace(/\./g, '_'),
+ () => ({ name: x.variant, ...cleanDirectives })
+ ]
+ })
+ )
+
+ const allPalettes = {
+ ...stylePalettes,
+ ...useInstanceStore().palettesIndex,
+ }
+ const defaultPalette = (() => {
+ if (this.prefersDark === null) return null
+ const matchRegex = this.prefersDark ? /dark/i : /light/i
+ return Object.keys(allPalettes).find((x) => matchRegex.test(x))
+ })() ?? allPalettes[0]
const palette = await getData(
'palette',
- palettesIndex,
+ allPalettes,
userPaletteCustomData,
- this.useStylePalette
- ? firstStylePaletteName
- : userPaletteName || instancePaletteName,
+ userPaletteName ?? defaultPalette,
)
this.paletteNameUsed = palette.nameUsed
@@ -752,7 +752,7 @@ export const useInterfaceStore = defineStore('interface', {
const theme = await getData(
'theme',
- themesIndex,
+ useInstanceStore().themesIndex,
userThemeV2Source || userThemeV2Snapshot,
userThemeV2Name || instanceThemeName,
)
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Wed, Nov 18, 10:01 AM (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1840303
Default Alt Text
(6 KB)
Attached To
Mode
rPUFE pleroma-fe-upstream
Attached
Detach File
Event Timeline