keybindingService.ts 7.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

7
import {TPromise} from 'vs/base/common/winjs.base';
8
import {TypeConstraint} from 'vs/base/common/types';
9
import {createDecorator, ServiceIdentifier, ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
E
Erich Gamma 已提交
10
import {Keybinding} from 'vs/base/common/keyCodes';
11
import {IHTMLContentElement} from 'vs/base/common/htmlContent';
E
Erich Gamma 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

export interface IUserFriendlyKeybinding {
	key: string;
	command: string;
	when?: string;
}

export interface IKeybindings {
	primary: number;
	secondary?: number[];
	win?: {
		primary: number;
		secondary?: number[];
	};
	linux?: {
		primary: number;
		secondary?: number[];
	};
	mac?: {
		primary: number;
		secondary?: number[];
	};
}

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
export interface KbExpr {
	equals(other:KbExpr): boolean;
	evaluate(context:any): boolean;
	normalize(): KbExpr;
	serialize(): string;
}

export class KbDefinedExpression implements KbExpr {
	constructor(private key:string) {
	}

	public equals(other:KbExpr): boolean {
		if (other instanceof KbDefinedExpression) {
			return (this.key === other.key);
		}
		return false;
	}

	public evaluate(context:any): boolean {
		return (!!context[this.key]);
	}

	public normalize(): KbExpr {
		return this;
	}

	public serialize(): string {
		return this.key;
	}
}

export class KbEqualsExpression implements KbExpr {
	constructor(private key:string, private value:any) {
	}

	public equals(other:KbExpr): boolean {
		if (other instanceof KbEqualsExpression) {
			return (this.key === other.key && this.value === other.value);
		}
		return false;
	}

	public evaluate(context:any): boolean {
A
tslint  
Alex Dima 已提交
79
		// Intentional ==
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
		return (context[this.key] == this.value);
	}

	public normalize(): KbExpr {
		if (typeof this.value === 'boolean') {
			if (this.value) {
				return new KbDefinedExpression(this.key);
			}
			return new KbNotExpression(this.key);
		}
		return this;
	}

	public serialize(): string {
		if (typeof this.value === 'boolean') {
			return this.normalize().serialize();
		}

		return this.key + ' == \'' + this.value + '\'';
	}
}

export class KbNotEqualsExpression implements KbExpr {
	constructor(private key:string, private value:any) {
	}

	public equals(other:KbExpr): boolean {
		if (other instanceof KbNotEqualsExpression) {
			return (this.key === other.key && this.value === other.value);
		}
		return false;
	}

	public evaluate(context:any): boolean {
A
tslint  
Alex Dima 已提交
114
		// Intentional !=
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
		return (context[this.key] != this.value);
	}

	public normalize(): KbExpr {
		if (typeof this.value === 'boolean') {
			if (this.value) {
				return new KbNotExpression(this.key);
			}
			return new KbDefinedExpression(this.key);
		}
		return this;
	}

	public serialize(): string {
		if (typeof this.value === 'boolean') {
			return this.normalize().serialize();
		}

		return this.key + ' != \'' + this.value + '\'';
	}
}

export class KbNotExpression implements KbExpr {
	constructor(private key:string) {
	}

	public equals(other:KbExpr): boolean {
		if (other instanceof KbNotExpression) {
			return (this.key === other.key);
		}
		return false;
	}

	public evaluate(context:any): boolean {
		return (!context[this.key]);
	}

	public normalize(): KbExpr {
		return this;
	}

	public serialize(): string {
		return '!' + this.key;
	}
}

export class KbAndExpression implements KbExpr {
	private expr:KbExpr[];

	constructor(expr:KbExpr[]) {
		this.expr = expr || [];
	}

	public equals(other:KbExpr): boolean {
		return this === other;
	}

	public evaluate(context:any): boolean {
173
		for (let i = 0, len = this.expr.length; i < len; i++) {
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
			if (!this.expr[i].evaluate(context)) {
				return false;
			}
		}
		return true;
	}

	public normalize(): KbExpr {
		let expr:KbExpr[] = [];

		for (let i = 0, len = this.expr.length; i < len; i++) {
			let e = this.expr[i];
			if (!e) {
				continue;
			}

			e = e.normalize();
			if (!e) {
				continue;
			}

			if (e instanceof KbAndExpression) {
				expr = expr.concat(e.expr);
				continue;
			}

			expr.push(e);
		}

		if (expr.length === 0) {
			return null;
		}

		if (expr.length === 1) {
			return expr[0];
		}

		return new KbAndExpression(expr);
	}

	public serialize(): string {
		if (this.expr.length === 0) {
			return '';
		}
		if (this.expr.length === 1) {
			return this.normalize().serialize();
		}
		return this.expr.map(e => e.serialize()).join(' && ');
	}
}


226
export let KbExpr = {
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
	has: (key:string) => new KbDefinedExpression(key),
	equals: (key:string, value:any) => new KbEqualsExpression(key, value),
	notEquals: (key:string, value:any) => new KbNotEqualsExpression(key, value),
	not: (key:string) => new KbNotExpression(key),
	and: (...expr:KbExpr[]) => new KbAndExpression(expr),
	deserialize: (serialized:string): KbExpr => {
		if (!serialized) {
			return null;
		}

		let pieces = serialized.split('&&');
		let result = new KbAndExpression(pieces.map(p => KbExpr._deserializeOne(p)));
		return result.normalize();
	},

	_deserializeOne: (serializedOne:string): KbExpr => {
		serializedOne = serializedOne.trim();

		if (serializedOne.indexOf('!=') >= 0) {
			let pieces = serializedOne.split('!=');
			return new KbNotEqualsExpression(pieces[0].trim(), KbExpr._deserializeValue(pieces[1]));
		}

		if (serializedOne.indexOf('==') >= 0) {
			let pieces = serializedOne.split('==');
			return new KbEqualsExpression(pieces[0].trim(), KbExpr._deserializeValue(pieces[1]));
		}

		if (/^\!\s*/.test(serializedOne)) {
			return new KbNotExpression(serializedOne.substr(1).trim());
		}

		return new KbDefinedExpression(serializedOne);
	},

	_deserializeValue: (serializedValue:string): any => {
		serializedValue = serializedValue.trim();

		if (serializedValue === 'true') {
			return true;
		}

		if (serializedValue === 'false') {
			return false;
		}

273
		let m = /^'([^']*)'$/.exec(serializedValue);
274 275 276 277 278 279
		if (m) {
			return m[1].trim();
		}

		return serializedValue;
	}
A
tslint  
Alex Dima 已提交
280
};
E
Erich Gamma 已提交
281 282 283 284

export interface IKeybindingItem {
	keybinding: number;
	command: string;
285
	context: KbExpr;
E
Erich Gamma 已提交
286 287 288 289 290 291
	weight1: number;
	weight2: number;
}

export interface ICommandHandler {
	(accessor: ServicesAccessor, args: any): void;
292 293 294 295 296
	description?: string | ICommandHandlerDescription;
}

export interface ICommandHandlerDescription {
	description: string;
J
Johannes Rieken 已提交
297 298
	args: { name: string; description?: string; constraint?: TypeConstraint; }[];
	returns?: string;
E
Erich Gamma 已提交
299 300 301
}

export interface ICommandsMap {
A
tslint  
Alex Dima 已提交
302
	[id: string]: ICommandHandler;
E
Erich Gamma 已提交
303 304 305 306 307 308 309
}

export interface IKeybindingContextKey<T> {
	set(value: T): void;
	reset(): void;
}

310
export let IKeybindingService = createDecorator<IKeybindingService>('keybindingService');
E
Erich Gamma 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

export interface IKeybindingScopeLocation {
	setAttribute(attr:string, value:string): void;
	removeAttribute(attr:string): void;
}

export interface IKeybindingService {
	serviceId : ServiceIdentifier<any>;
	dispose(): void;

	createKey<T>(key: string, defaultValue: T): IKeybindingContextKey<T>;

	createScoped(domNode: IKeybindingScopeLocation): IKeybindingService;

	getDefaultKeybindings(): string;
	lookupKeybindings(commandId: string): Keybinding[];
	customKeybindingsCount(): number;

329
	getLabelFor(keybinding:Keybinding): string;
330
	getHTMLLabelFor(keybinding:Keybinding): IHTMLContentElement[];
331
	getElectronAcceleratorFor(keybinding:Keybinding): string;
332

333 334
	executeCommand<T>(commandId: string, args?: any): TPromise<T>;
	executeCommand(commandId: string, args?: any): TPromise<any>;
E
Erich Gamma 已提交
335
}