/********************************************************************************
 * Copyright (C) 2017 TypeFox and others.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the Eclipse
 * Public License v. 2.0 are satisfied: GNU General Public License, version 2
 * with the GNU Classpath Exception which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 ********************************************************************************/ import { Emitter , Event } from '../common/event' ; import { Disposable } from '../common/disposable' ; import { FrontendApplicationConfigProvider } from './frontend-application-config-provider' ; import { ApplicationProps } from '@theia/application-package/lib/application-props' ; import { Theme , ThemeChangeEvent } from '../common/theme' ; deprecated export * from '../common/theme' ; export const ThemeServiceSymbol = Symbol ( 'ThemeService' ) ; export class ThemeService { protected themes : { [ id ] : Theme ; } = { } ; protected activeTheme : Theme | undefined ; protected readonly themeChange = new Emitter < ThemeChangeEvent > ( ) ; readonly onDidColorThemeChange : Event < ThemeChangeEvent > = this . themeChange . event ; static get ( ) { const global = window as any ; if ( ! global [ ThemeServiceSymbol ] ) { const themeService = new ThemeService ( ) ; themeService . register ( ... BuiltinThemeProvider . themes ) ; themeService . startupTheme ( ) ; global [ ThemeServiceSymbol ] = themeService ; } return global [ ThemeServiceSymbol ] ; } register ( ... themes ) { for ( const theme of themes ) { this . themes [ theme . id ] = theme ; } this . validateActiveTheme ( ) ; return Disposable . create ( ( ) => { for ( const theme of themes ) { delete this . themes [ theme . id ] ; } this . validateActiveTheme ( ) ; } ) ; } protected validateActiveTheme ( ) { if ( ! this . activeTheme ) { return ; } const theme = this . themes [ this . activeTheme . id ] ; if ( ! theme ) { this . loadUserTheme ( ) ; } else if ( theme !== this . activeTheme ) { this . activeTheme = undefined ; this . setCurrentTheme ( theme . id ) ; } } getThemes ( ) { const result = [ ] ; for ( const o in this . themes ) { if ( this . themes . hasOwnProperty ( o ) ) { result . push ( this . themes [ o ] ) ; } } return result ; } getTheme ( themeId ) { return this . themes [ themeId ] || this . defaultTheme ; } startupTheme ( ) { const theme = this . getCurrentTheme ( ) ; theme . activate ( ) ; } loadUserTheme ( ) { const theme = this . getCurrentTheme ( ) ; this . setCurrentTheme ( theme . id ) ; } setCurrentTheme ( themeId ) { const newTheme = this . getTheme ( themeId ) ; const oldTheme = this . activeTheme ; if ( oldTheme ) { if ( oldTheme . id === newTheme . id ) { return ; } oldTheme . deactivate ( ) ; } newTheme . activate ( ) ; this . activeTheme = newTheme ; window . localStorage . setItem ( 'theme' , themeId ) ; this . themeChange . fire ( { newTheme , oldTheme } ) ; } getCurrentTheme ( ) { const themeId = window . localStorage . getItem ( 'theme' ) || this . defaultTheme . id ; return this . getTheme ( themeId ) ; } /**
     * The default theme. If that is not applicable, returns with the fallback theme.
     */ get defaultTheme ( ) : Theme { return this . themes [ FrontendApplicationConfigProvider . get ( ) . defaultTheme ] || this . themes [ ApplicationProps . DEFAULT . frontend . config . defaultTheme ] ; } /**
     * Resets the state to the user's default, or to the fallback theme. Also discards any persisted state in the local storage.
     */ reset ( ) { this . setCurrentTheme ( this . defaultTheme . id ) ; } } export class BuiltinThemeProvider { static readonly darkCss = require ( '../../src/browser/style/variables-dark.useable.css' ) ; static readonly lightCss = require ( '../../src/browser/style/variables-bright.useable.css' ) ; static readonly darkTheme : Theme = { id : 'dark' , type : 'dark' , label : 'Dark (Theia)' , editorTheme : 'dark-theia' , activate ( ) { BuiltinThemeProvider . darkCss . use ( ) ; } , deactivate ( ) { BuiltinThemeProvider . darkCss . unuse ( ) ; } } ; static readonly lightTheme : Theme = { id : 'light' , type : 'light' , label : 'Light (Theia)' , editorTheme : 'light-theia' , activate ( ) { BuiltinThemeProvider . lightCss . use ( ) ; } , deactivate ( ) { BuiltinThemeProvider . lightCss . unuse ( ) ; } } ; static readonly hcTheme : Theme = { id : 'hc-theia' , type : 'hc' , label : 'High Contrast (Theia)' , editorTheme : 'hc-theia' , activate ( ) { BuiltinThemeProvider . darkCss . use ( ) ; } , deactivate ( ) { BuiltinThemeProvider . darkCss . unuse ( ) ; } } ; static readonly themes = [ BuiltinThemeProvider . darkTheme , BuiltinThemeProvider . lightTheme , BuiltinThemeProvider . hcTheme ] ; }