import { Injectable } from '@angular/core'
-import { AuthService } from '@app/core/auth'
-import { ServerService } from '@app/core/server'
+import { capitalizeFirstLetter } from '@root-helpers/string'
+import { UserLocalStorageKeys } from '@root-helpers/users'
+import { HTMLServerConfig, ServerConfigTheme } from '@shared/models'
import { environment } from '../../../environments/environment'
-import { PluginService } from '@app/core/plugins/plugin.service'
-import { ServerConfig, ServerConfigTheme } from '@shared/models'
-import { first } from 'rxjs/operators'
-import { User } from '@app/shared/users/user.model'
-import { UserService } from '@app/shared/users/user.service'
-import { LocalStorageService } from '@app/shared/misc/storage.service'
-import { peertubeLocalStorage } from '@app/shared/misc/peertube-web-storage'
+import { AuthService } from '../auth'
+import { PluginService } from '../plugins/plugin.service'
+import { ServerService } from '../server'
+import { UserService } from '../users/user.service'
+import { LocalStorageService } from '../wrappers/storage.service'
@Injectable()
export class ThemeService {
private themeFromLocalStorage: ServerConfigTheme
private themeDOMLinksFromLocalStorage: HTMLLinkElement[] = []
- private serverConfig: ServerConfig
+ private serverConfig: HTMLServerConfig
constructor (
private auth: AuthService,
// Try to load from local storage first, so we don't have to wait network requests
this.loadAndSetFromLocalStorage()
- this.serverConfig = this.server.getTmpConfig()
- this.server.getConfig()
- .subscribe(config => {
- this.serverConfig = config
+ this.serverConfig = this.server.getHTMLConfig()
+ const themes = this.serverConfig.theme.registered
- const themes = this.serverConfig.theme.registered
+ this.removeThemeFromLocalStorageIfNeeded(themes)
+ this.injectThemes(themes)
- this.removeThemeFromLocalStorageIfNeeded(themes)
- this.injectThemes(themes)
+ this.listenUserTheme()
+ }
+
+ getDefaultThemeLabel () {
+ if (this.hasDarkTheme()) {
+ return $localize`Light/Orange or Dark`
+ }
- this.listenUserTheme()
- })
+ return $localize`Light/Orange`
+ }
+
+ getAvailableThemeLabels () {
+ return this.serverConfig.theme.registered
+ .map(t => capitalizeFirstLetter(t.name))
}
private injectThemes (themes: ServerConfigTheme[], fromLocalStorage = false) {
: this.userService.getAnonymousUser().theme
if (theme !== 'instance-default') return theme
- return this.serverConfig.theme.default
+
+ const instanceTheme = this.serverConfig.theme.default
+ if (instanceTheme !== 'default') return instanceTheme
+
+ // Default to dark theme if available and wanted by the user
+ if (this.hasDarkTheme() && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
+ return 'dark'
+ }
+
+ return instanceTheme
}
private loadTheme (name: string) {
const links = document.getElementsByTagName('link')
for (let i = 0; i < links.length; i++) {
- const link = links[ i ]
- if (link.getAttribute('rel').indexOf('style') !== -1 && link.getAttribute('title')) {
+ const link = links[i]
+ if (link.getAttribute('rel').includes('style') && link.getAttribute('title')) {
link.disabled = link.getAttribute('title') !== name
}
}
const theme = this.getTheme(currentTheme)
if (theme) {
console.log('Adding scripts of theme %s.', currentTheme)
+
this.pluginService.addPlugin(theme, true)
this.pluginService.reloadLoadedScopes()
- this.localStorageService.setItem(User.KEYS.THEME, JSON.stringify(theme), false)
+ this.localStorageService.setItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, JSON.stringify(theme), false)
} else {
- this.localStorageService.removeItem(User.KEYS.THEME, false)
+ this.localStorageService.removeItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, false)
}
this.oldThemeName = currentTheme
if (!this.auth.isLoggedIn()) {
this.updateCurrentTheme()
- this.localStorageService.watch([User.KEYS.THEME]).subscribe(
+ this.localStorageService.watch([ UserLocalStorageKeys.THEME ]).subscribe(
() => this.updateCurrentTheme()
)
}
this.auth.userInformationLoaded
- .pipe(first())
.subscribe(() => this.updateCurrentTheme())
}
private loadAndSetFromLocalStorage () {
- const lastActiveThemeString = this.localStorageService.getItem(User.KEYS.THEME)
+ const lastActiveThemeString = this.localStorageService.getItem(UserLocalStorageKeys.LAST_ACTIVE_THEME)
if (!lastActiveThemeString) return
try {
private getTheme (name: string) {
return this.themes.find(t => t.name === name)
}
+
+ private hasDarkTheme () {
+ return this.serverConfig.theme.registered.some(t => t.name === 'dark')
+ }
}