iconsStyleSheet.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { asCSSPropertyValue, asCSSUrl } from '../../../base/browser/dom.js';
  6. import { Emitter } from '../../../base/common/event.js';
  7. import { getIconRegistry } from '../common/iconRegistry.js';
  8. import { ThemeIcon } from '../common/themeService.js';
  9. export function getIconsStyleSheet() {
  10. const onDidChangeEmmiter = new Emitter();
  11. const iconRegistry = getIconRegistry();
  12. iconRegistry.onDidChange(() => onDidChangeEmmiter.fire());
  13. return {
  14. onDidChange: onDidChangeEmmiter.event,
  15. getCSS() {
  16. const usedFontIds = {};
  17. const formatIconRule = (contribution) => {
  18. let definition = contribution.defaults;
  19. while (ThemeIcon.isThemeIcon(definition)) {
  20. const c = iconRegistry.getIcon(definition.id);
  21. if (!c) {
  22. return undefined;
  23. }
  24. definition = c.defaults;
  25. }
  26. const fontId = definition.fontId;
  27. if (fontId) {
  28. const fontContribution = iconRegistry.getIconFont(fontId);
  29. if (fontContribution) {
  30. usedFontIds[fontId] = fontContribution;
  31. return `.codicon-${contribution.id}:before { content: '${definition.fontCharacter}'; font-family: ${asCSSPropertyValue(fontId)}; }`;
  32. }
  33. }
  34. return `.codicon-${contribution.id}:before { content: '${definition.fontCharacter}'; }`;
  35. };
  36. const rules = [];
  37. for (let contribution of iconRegistry.getIcons()) {
  38. const rule = formatIconRule(contribution);
  39. if (rule) {
  40. rules.push(rule);
  41. }
  42. }
  43. for (let id in usedFontIds) {
  44. const fontContribution = usedFontIds[id];
  45. const src = fontContribution.definition.src.map(l => `${asCSSUrl(l.location)} format('${l.format}')`).join(', ');
  46. rules.push(`@font-face { src: ${src}; font-family: ${asCSSPropertyValue(id)}; font-display: block; }`);
  47. }
  48. return rules.join('\n');
  49. }
  50. };
  51. }