|
5 | 5 | 'use strict'; |
6 | 6 |
|
7 | 7 | import * as nls from 'vs/nls'; |
8 | | -import { ResolvedKeybinding, Keybinding, SimpleKeybinding } from 'vs/base/common/keyCodes'; |
| 8 | +import { ResolvedKeybinding, Keybinding } from 'vs/base/common/keyCodes'; |
9 | 9 | import { IDisposable, dispose } from 'vs/base/common/lifecycle'; |
10 | 10 | import Severity from 'vs/base/common/severity'; |
11 | 11 | import { ICommandService } from 'vs/platform/commands/common/commands'; |
12 | 12 | import { KeybindingResolver, IResolveResult } from 'vs/platform/keybinding/common/keybindingResolver'; |
13 | | -import { IKeybindingEvent, IKeybindingService, IKeybindingItem2, KeybindingSource } from 'vs/platform/keybinding/common/keybinding'; |
| 13 | +import { IKeybindingEvent, IKeybindingService, IKeybindingItem2, KeybindingSource, IKeyboardEvent } from 'vs/platform/keybinding/common/keybinding'; |
14 | 14 | import { IContextKeyService, IContextKeyServiceTarget } from 'vs/platform/contextkey/common/contextkey'; |
15 | 15 | import { IStatusbarService } from 'vs/platform/statusbar/common/statusbar'; |
16 | 16 | import { IMessageService } from 'vs/platform/message/common/message'; |
@@ -56,16 +56,13 @@ export abstract class AbstractKeybindingService implements IKeybindingService { |
56 | 56 | this.toDispose = dispose(this.toDispose); |
57 | 57 | } |
58 | 58 |
|
59 | | - protected abstract _getResolver(): KeybindingResolver; |
60 | | - protected abstract _createResolvedKeybinding(kb: Keybinding): ResolvedKeybinding; |
61 | | - |
62 | 59 | get onDidUpdateKeybindings(): Event<IKeybindingEvent> { |
63 | 60 | return this._onDidUpdateKeybindings ? this._onDidUpdateKeybindings.event : Event.None; // Sinon stubbing walks properties on prototype |
64 | 61 | } |
65 | 62 |
|
66 | | - public resolveKeybinding(keybinding: Keybinding): ResolvedKeybinding { |
67 | | - return this._createResolvedKeybinding(keybinding); |
68 | | - } |
| 63 | + protected abstract _getResolver(): KeybindingResolver; |
| 64 | + public abstract resolveKeybinding(keybinding: Keybinding): ResolvedKeybinding; |
| 65 | + public abstract resolveKeyboardEvent(keyboardEvent: IKeyboardEvent): ResolvedKeybinding; |
69 | 66 |
|
70 | 67 | public getDefaultKeybindings(): string { |
71 | 68 | return ''; |
@@ -96,32 +93,40 @@ export abstract class AbstractKeybindingService implements IKeybindingService { |
96 | 93 | return result.resolvedKeybinding; |
97 | 94 | } |
98 | 95 |
|
99 | | - public resolve(keybinding: SimpleKeybinding, target: IContextKeyServiceTarget): IResolveResult { |
100 | | - if (keybinding.isModifierKey()) { |
| 96 | + public softDispatch(e: IKeyboardEvent, target: IContextKeyServiceTarget): IResolveResult { |
| 97 | + const keybinding = this.resolveKeyboardEvent(e); |
| 98 | + if (keybinding.isChord()) { |
| 99 | + console.warn('Unexpected keyboard event mapped to a chord'); |
| 100 | + return null; |
| 101 | + } |
| 102 | + const [firstPart,] = keybinding.getDispatchParts(); |
| 103 | + if (firstPart === null) { |
| 104 | + // cannot be dispatched, probably only modifier keys |
101 | 105 | return null; |
102 | 106 | } |
103 | 107 |
|
104 | 108 | const contextValue = this._contextKeyService.getContextValue(target); |
105 | 109 | const currentChord = this._currentChord ? this._currentChord.keypress : null; |
106 | | - const resolvedKeybinding = this._createResolvedKeybinding(keybinding); |
107 | | - const [firstPart,] = resolvedKeybinding.getDispatchParts(); |
108 | | - // We know for a fact the chordPart is null since we're using a single keypress |
109 | 110 | return this._getResolver().resolve(contextValue, currentChord, firstPart); |
110 | 111 | } |
111 | 112 |
|
112 | | - protected _dispatch(keybinding: SimpleKeybinding, target: IContextKeyServiceTarget): boolean { |
113 | | - // Check modifier key here and cancel early, it's also checked in resolve as the function |
114 | | - // is used externally. |
| 113 | + protected _dispatch(e: IKeyboardEvent, target: IContextKeyServiceTarget): boolean { |
115 | 114 | let shouldPreventDefault = false; |
116 | | - if (keybinding.isModifierKey()) { |
| 115 | + |
| 116 | + const keybinding = this.resolveKeyboardEvent(e); |
| 117 | + if (keybinding.isChord()) { |
| 118 | + console.warn('Unexpected keyboard event mapped to a chord'); |
| 119 | + return null; |
| 120 | + } |
| 121 | + const [firstPart,] = keybinding.getDispatchParts(); |
| 122 | + if (firstPart === null) { |
| 123 | + // cannot be dispatched, probably only modifier keys |
117 | 124 | return shouldPreventDefault; |
118 | 125 | } |
119 | 126 |
|
120 | 127 | const contextValue = this._contextKeyService.getContextValue(target); |
121 | 128 | const currentChord = this._currentChord ? this._currentChord.keypress : null; |
122 | | - const resolvedKeybinding = this._createResolvedKeybinding(keybinding); |
123 | | - const [firstPart,] = resolvedKeybinding.getDispatchParts(); |
124 | | - const keypressLabel = resolvedKeybinding.getLabel(); |
| 129 | + const keypressLabel = keybinding.getLabel(); |
125 | 130 | const resolveResult = this._getResolver().resolve(contextValue, currentChord, firstPart); |
126 | 131 |
|
127 | 132 | if (resolveResult && resolveResult.enterChord) { |
|
0 commit comments