1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import { injectable } from 'inversify';
|
18 | import { Disposable } from '../common';
|
19 | import { Emitter, Event } from '../common/event';
|
20 |
|
21 | export type ContextKeyValue = null | undefined | boolean | number | string
|
22 | | Array<null | undefined | boolean | number | string>
|
23 | | Record<string, null | undefined | boolean | number | string>;
|
24 |
|
25 | export interface ContextKey<T extends ContextKeyValue = ContextKeyValue> {
|
26 | set(value: T | undefined): void;
|
27 | reset(): void;
|
28 | get(): T | undefined;
|
29 | }
|
30 |
|
31 | export namespace ContextKey {
|
32 |
|
33 | export const None: ContextKey<any> = Object.freeze({
|
34 | set: () => { },
|
35 | reset: () => { },
|
36 | get: () => undefined
|
37 | });
|
38 | }
|
39 |
|
40 | export interface ContextKeyChangeEvent {
|
41 | affects(keys: { has(key: string): boolean }): boolean;
|
42 | }
|
43 |
|
44 | export const ContextKeyService = Symbol('ContextKeyService');
|
45 |
|
46 | export interface ContextMatcher extends Disposable {
|
47 | |
48 |
|
49 |
|
50 | match(expression: string, context?: HTMLElement): boolean;
|
51 | }
|
52 |
|
53 | export interface ContextKeyService extends ContextMatcher {
|
54 | readonly onDidChange: Event<ContextKeyChangeEvent>;
|
55 |
|
56 | createKey<T extends ContextKeyValue>(key: string, defaultValue: T | undefined): ContextKey<T>;
|
57 |
|
58 | |
59 |
|
60 |
|
61 | parseKeys(expression: string): Set<string> | undefined;
|
62 |
|
63 | |
64 |
|
65 |
|
66 |
|
67 | with<T>(values: Record<string, unknown>, callback: () => T): T;
|
68 |
|
69 | /**
|
70 | * Creates a child service with a separate context scoped to the HTML element passed in.
|
71 | * Useful for e.g. setting the {view} context value for particular widgets.
|
72 | */
|
73 | createScoped(target: HTMLElement): ScopedValueStore;
|
74 |
|
75 | /**
|
76 | * @param overlay values to be used in the new {@link ContextKeyService}. These values will be static.
|
77 | * Creates a child service with a separate context and a set of fixed values to override parent values.
|
78 | */
|
79 | createOverlay(overlay: Iterable<[string, unknown]>): ContextMatcher;
|
80 |
|
81 | /**
|
82 | * Set or modify a value in the service's context.
|
83 | */
|
84 | setContext(key: string, value: unknown): void;
|
85 | }
|
86 |
|
87 | export type ScopedValueStore = Omit<ContextKeyService, 'onDidChange' | 'match' | 'parseKeys' | 'with' | 'createOverlay'>;
|
88 |
|
89 | @injectable()
|
90 | export class ContextKeyServiceDummyImpl implements ContextKeyService {
|
91 |
|
92 | protected readonly onDidChangeEmitter = new Emitter<ContextKeyChangeEvent>();
|
93 | readonly onDidChange = this.onDidChangeEmitter.event;
|
94 | protected fireDidChange(event: ContextKeyChangeEvent): void {
|
95 | this.onDidChangeEmitter.fire(event);
|
96 | }
|
97 |
|
98 | createKey<T extends ContextKeyValue>(key: string, defaultValue: T | undefined): ContextKey<T> {
|
99 | return ContextKey.None;
|
100 | }
|
101 | /**
|
102 | * It should be implemented by an extension, e.g. by the monaco extension.
|
103 | */
|
104 | match(expression: string, context?: HTMLElement): boolean {
|
105 | return true;
|
106 | }
|
107 |
|
108 | /**
|
109 | * It should be implemented by an extension, e.g. by the monaco extension.
|
110 | */
|
111 | parseKeys(expression: string): Set<string> | undefined {
|
112 | return new Set<string>();
|
113 | }
|
114 |
|
115 | /**
|
116 | * Details should be implemented by an extension, e.g. by the monaco extension.
|
117 | * Callback must be synchronous.
|
118 | */
|
119 | with<T>(values: Record<string, unknown>, callback: () => T): T {
|
120 | return callback();
|
121 | }
|
122 |
|
123 | /**
|
124 | * Details should implemented by an extension, e.g. by the monaco extension.
|
125 | */
|
126 | createScoped(target: HTMLElement): ContextKeyService {
|
127 | return this;
|
128 | }
|
129 |
|
130 | /**
|
131 | * Details should be implemented by an extension, e.g. the monaco extension.
|
132 | */
|
133 | createOverlay(overlay: Iterable<[string, unknown]>): ContextMatcher {
|
134 | return this;
|
135 | }
|
136 |
|
137 | /**
|
138 | * Details should be implemented by an extension, e.g. by the monaco extension.
|
139 | */
|
140 | setContext(key: string, value: unknown): void { }
|
141 |
|
142 | dispose(): void { }
|
143 | }
|
144 |
|
\ | No newline at end of file |