89ef1039d55224f802f203c6057b48dbf08c1fc6cf5fed5cb861c41351faeca668cada54c0259591f454cceb678ab620781bd579c02fe8da85040834ac3573 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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(themeService) {
  10. const onDidChangeEmmiter = new Emitter();
  11. const iconRegistry = getIconRegistry();
  12. iconRegistry.onDidChange(() => onDidChangeEmmiter.fire());
  13. themeService === null || themeService === void 0 ? void 0 : themeService.onDidProductIconThemeChange(() => onDidChangeEmmiter.fire());
  14. return {
  15. onDidChange: onDidChangeEmmiter.event,
  16. getCSS() {
  17. const productIconTheme = themeService ? themeService.getProductIconTheme() : new UnthemedProductIconTheme();
  18. const usedFontIds = {};
  19. const formatIconRule = (contribution) => {
  20. const definition = productIconTheme.getIcon(contribution);
  21. if (!definition) {
  22. return undefined;
  23. }
  24. const fontContribution = definition.font;
  25. if (fontContribution) {
  26. usedFontIds[fontContribution.id] = fontContribution.definition;
  27. return `.codicon-${contribution.id}:before { content: '${definition.fontCharacter}'; font-family: ${asCSSPropertyValue(fontContribution.id)}; }`;
  28. }
  29. // default font (codicon)
  30. return `.codicon-${contribution.id}:before { content: '${definition.fontCharacter}'; }`;
  31. };
  32. const rules = [];
  33. for (const contribution of iconRegistry.getIcons()) {
  34. const rule = formatIconRule(contribution);
  35. if (rule) {
  36. rules.push(rule);
  37. }
  38. }
  39. for (const id in usedFontIds) {
  40. const definition = usedFontIds[id];
  41. const fontWeight = definition.weight ? `font-weight: ${definition.weight};` : '';
  42. const fontStyle = definition.style ? `font-style: ${definition.style};` : '';
  43. const src = definition.src.map(l => `${asCSSUrl(l.location)} format('${l.format}')`).join(', ');
  44. rules.push(`@font-face { src: ${src}; font-family: ${asCSSPropertyValue(id)};${fontWeight}${fontStyle} font-display: block; }`);
  45. }
  46. return rules.join('\n');
  47. }
  48. };
  49. }
  50. export class UnthemedProductIconTheme {
  51. getIcon(contribution) {
  52. const iconRegistry = getIconRegistry();
  53. let definition = contribution.defaults;
  54. while (ThemeIcon.isThemeIcon(definition)) {
  55. const c = iconRegistry.getIcon(definition.id);
  56. if (!c) {
  57. return undefined;
  58. }
  59. definition = c.defaults;
  60. }
  61. return definition;
  62. }
  63. }