-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[theming] #4831: color contribution point
- Theia extensions can register new colors - All colors are translated into css variables with `.` replaced by `-` and `--theia` prefix - Theia extensions can use registered colors via css or programatically from `ColorRegistrsy.getCurrentColor` Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
- Loading branch information
Showing
6 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
packages/core/src/browser/color-application-contribution.ts
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,75 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2019 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 { injectable, inject, named } from 'inversify'; | ||
import { ColorRegistry } from './color-registry'; | ||
import { Emitter } from '../common/event'; | ||
import { ThemeService } from './theming'; | ||
import { FrontendApplicationContribution } from './frontend-application'; | ||
import { ContributionProvider } from '../common/contribution-provider'; | ||
import { Disposable, DisposableCollection } from '../common/disposable'; | ||
|
||
export const ColorContribution = Symbol('ColorContribution'); | ||
export interface ColorContribution { | ||
registerColors(colors: ColorRegistry): void; | ||
} | ||
|
||
@injectable() | ||
export class ColorApplicationContribution implements FrontendApplicationContribution { | ||
|
||
protected readonly onDidChangeEmitter = new Emitter<void>(); | ||
readonly onDidChange = this.onDidChangeEmitter.event; | ||
|
||
@inject(ColorRegistry) | ||
protected readonly colors: ColorRegistry; | ||
|
||
@inject(ContributionProvider) @named(ColorContribution) | ||
protected readonly colorContributions: ContributionProvider<ColorContribution>; | ||
|
||
onStart(): void { | ||
for (const contribution of this.colorContributions.getContributions()) { | ||
contribution.registerColors(this.colors); | ||
} | ||
|
||
this.update(); | ||
ThemeService.get().onThemeChange(() => this.update()); | ||
} | ||
|
||
protected readonly toUpdate = new DisposableCollection(); | ||
protected update(): void { | ||
if (!document) { | ||
return; | ||
} | ||
this.toUpdate.dispose(); | ||
const theme = 'theia-' + ThemeService.get().getCurrentTheme().type; | ||
document.body.classList.add(theme); | ||
this.toUpdate.push(Disposable.create(() => document.body.classList.remove(theme))); | ||
|
||
const documentElement = document.documentElement; | ||
if (documentElement) { | ||
for (const id of this.colors.getColors()) { | ||
const color = this.colors.getCurrentColor(id); | ||
if (color) { | ||
const propertyName = `--theia-${id.replace('.', '-')}`; | ||
documentElement.style.setProperty(propertyName, color); | ||
this.toUpdate.push(Disposable.create(() => documentElement.style.removeProperty(propertyName))); | ||
} | ||
} | ||
} | ||
this.onDidChangeEmitter.fire(undefined); | ||
} | ||
|
||
} |
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
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