Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/vs/base/common/themables.ts
13405 views
1
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'
2
3
/*---------------------------------------------------------------------------------------------
4
* Copyright (c) Microsoft Corporation. All rights reserved.
5
* Licensed under the MIT License. See License.txt in the project root for license information.
6
*--------------------------------------------------------------------------------------------*/
7
8
import { Codicon } from './codicons';
9
10
export type ColorIdentifier = string;
11
12
export type IconIdentifier = string;
13
14
export interface ThemeColor {
15
id: string;
16
}
17
18
export namespace ThemeColor {
19
export function isThemeColor(obj: unknown): obj is ThemeColor {
20
return !!obj && typeof obj === 'object' && typeof (<ThemeColor>obj).id === 'string';
21
}
22
}
23
24
export function themeColorFromId(id: ColorIdentifier) {
25
return { id };
26
}
27
28
29
export interface ThemeIcon {
30
readonly id: string;
31
readonly color?: ThemeColor;
32
}
33
34
export namespace ThemeIcon {
35
export const iconNameSegment = '[A-Za-z0-9]+';
36
export const iconNameExpression = '[A-Za-z0-9-]+';
37
export const iconModifierExpression = '~[A-Za-z]+';
38
export const iconNameCharacter = '[A-Za-z0-9~-]';
39
40
const ThemeIconIdRegex = new RegExp(`^(${iconNameExpression})(${iconModifierExpression})?$`);
41
42
export function asClassNameArray(icon: ThemeIcon): string[] {
43
const match = ThemeIconIdRegex.exec(icon.id);
44
if (!match) {
45
return asClassNameArray(Codicon.error);
46
}
47
const [, id, modifier] = match;
48
const classNames = ['codicon', 'codicon-' + id];
49
if (modifier) {
50
classNames.push('codicon-modifier-' + modifier.substring(1));
51
}
52
return classNames;
53
}
54
55
export function asClassName(icon: ThemeIcon): string {
56
return asClassNameArray(icon).join(' ');
57
}
58
59
export function asCSSSelector(icon: ThemeIcon): string {
60
return '.' + asClassNameArray(icon).join('.');
61
}
62
63
export function isThemeIcon(obj: unknown): obj is ThemeIcon {
64
return !!obj && typeof obj === 'object' && typeof (<ThemeIcon>obj).id === 'string' && (typeof (<ThemeIcon>obj).color === 'undefined' || ThemeColor.isThemeColor((<ThemeIcon>obj).color));
65
}
66
67
const _regexFromString = new RegExp(`^\\$\\((${ThemeIcon.iconNameExpression}(?:${ThemeIcon.iconModifierExpression})?)\\)$`);
68
69
export function fromString(str: string): ThemeIcon | undefined {
70
const match = _regexFromString.exec(str);
71
if (!match) {
72
return undefined;
73
}
74
const [, name] = match;
75
return { id: name };
76
}
77
78
export function fromId(id: string): ThemeIcon {
79
return { id };
80
}
81
82
export function modify(icon: ThemeIcon, modifier: 'disabled' | 'spin' | undefined): ThemeIcon {
83
let id = icon.id;
84
const tildeIndex = id.lastIndexOf('~');
85
if (tildeIndex !== -1) {
86
id = id.substring(0, tildeIndex);
87
}
88
if (modifier) {
89
id = `${id}~${modifier}`;
90
}
91
return { id };
92
}
93
94
export function getModifier(icon: ThemeIcon): string | undefined {
95
const tildeIndex = icon.id.lastIndexOf('~');
96
if (tildeIndex !== -1) {
97
return icon.id.substring(tildeIndex + 1);
98
}
99
return undefined;
100
}
101
102
export function isEqual(ti1: ThemeIcon, ti2: ThemeIcon): boolean {
103
return ti1.id === ti2.id && ti1.color?.id === ti2.color?.id;
104
}
105
106
/**
107
* Returns whether specified icon is defined and has 'file' ID.
108
*/
109
export function isFile(icon: ThemeIcon | undefined): boolean {
110
return icon?.id === Codicon.file.id;
111
}
112
113
/**
114
* Returns whether specified icon is defined and has 'folder' ID.
115
*/
116
export function isFolder(icon: ThemeIcon | undefined): boolean {
117
return icon?.id === Codicon.folder.id;
118
}
119
}
120
121