Skip to content

Commit cc4bb85

Browse files
author
Loïc Mangeonjean
committed
feat: add a way to disable global keybindings
1 parent 4cd990c commit cc4bb85

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/service-override/keybindings.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import '../missing-services'
22
import { IEditorOverrideServices, StandaloneServices } from 'vs/editor/standalone/browser/standaloneServices'
33
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'
44
import { WorkbenchKeybindingService } from 'vs/workbench/services/keybinding/browser/keybindingService'
5-
import { IKeybindingService, IUserFriendlyKeybinding } from 'vs/platform/keybinding/common/keybinding'
5+
import { IKeybindingService, IKeyboardEvent, IUserFriendlyKeybinding } from 'vs/platform/keybinding/common/keybinding'
66
import { VSBuffer } from 'vs/base/common/buffer'
77
import { IUserDataProfilesService } from 'vs/platform/userDataProfile/common/userDataProfile'
88
import { IKeyboardLayoutService } from 'vs/platform/keyboardLayout/common/keyboardLayout'
@@ -13,6 +13,14 @@ import { CommandService } from 'vs/workbench/services/commands/common/commandSer
1313
import { ResolvedKeybindingItem } from 'vs/platform/keybinding/common/resolvedKeybindingItem'
1414
import { toDisposable } from 'vs/base/common/lifecycle'
1515
import { KeybindingResolver } from 'vs/platform/keybinding/common/keybindingResolver'
16+
import { IContextKeyService, IContextKeyServiceTarget } from 'vs/platform/contextkey/common/contextkey'
17+
import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity'
18+
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'
19+
import { INotificationService } from 'vs/platform/notification/common/notification'
20+
import { IUserDataProfileService } from 'vs/workbench/services/userDataProfile/common/userDataProfile'
21+
import { IHostService } from 'vs/workbench/services/host/browser/host'
22+
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions'
23+
import { ILogService } from 'vs/platform/log/common/log'
1624
import getFileServiceOverride from './files'
1725
import { DynamicKeybindingService } from '../monaco'
1826
import 'vs/workbench/browser/workbench.contribution'
@@ -26,6 +34,23 @@ async function updateUserKeybindings (keybindingsJson: string): Promise<void> {
2634
class DynamicWorkbenchKeybindingService extends WorkbenchKeybindingService implements DynamicKeybindingService {
2735
private keybindingProviders: (() => ResolvedKeybindingItem[])[] = []
2836

37+
constructor (
38+
private shouldUseGlobalKeybindings: () => boolean,
39+
@IContextKeyService contextKeyService: IContextKeyService,
40+
@ICommandService commandService: ICommandService,
41+
@ITelemetryService telemetryService: ITelemetryService,
42+
@INotificationService notificationService: INotificationService,
43+
@IUserDataProfileService userDataProfileService: IUserDataProfileService,
44+
@IHostService hostService: IHostService,
45+
@IExtensionService extensionService: IExtensionService,
46+
@IFileService fileService: IFileService,
47+
@IUriIdentityService uriIdentityService: IUriIdentityService,
48+
@ILogService logService: ILogService,
49+
@IKeyboardLayoutService keyboardLayoutService: IKeyboardLayoutService
50+
) {
51+
super(contextKeyService, commandService, telemetryService, notificationService, userDataProfileService, hostService, extensionService, fileService, uriIdentityService, logService, keyboardLayoutService)
52+
}
53+
2954
public registerKeybindingProvider (provider: () => ResolvedKeybindingItem[]) {
3055
this.keybindingProviders.push(provider)
3156
this.updateResolver()
@@ -43,15 +68,26 @@ class DynamicWorkbenchKeybindingService extends WorkbenchKeybindingService imple
4368
return super._getResolver()
4469
}
4570

71+
protected override _dispatch (e: IKeyboardEvent, target: IContextKeyServiceTarget): boolean {
72+
if (!this.shouldUseGlobalKeybindings()) {
73+
return false
74+
}
75+
return super._dispatch(e, target)
76+
}
77+
4678
protected override getUserKeybindingItems () {
4779
return [...super.getUserKeybindingItems(), ...this.keybindingProviders.flatMap(provider => provider())]
4880
}
4981
}
5082

51-
export default function getServiceOverride (): IEditorOverrideServices {
83+
interface KeybindingsProps {
84+
shouldUseGlobalKeybindings?: () => boolean
85+
}
86+
87+
export default function getServiceOverride ({ shouldUseGlobalKeybindings = () => false }: KeybindingsProps = {}): IEditorOverrideServices {
5288
return {
5389
...getFileServiceOverride(),
54-
[IKeybindingService.toString()]: new SyncDescriptor(DynamicWorkbenchKeybindingService, [], false),
90+
[IKeybindingService.toString()]: new SyncDescriptor(DynamicWorkbenchKeybindingService, [shouldUseGlobalKeybindings], false),
5591
[IKeyboardLayoutService.toString()]: new SyncDescriptor(BrowserKeyboardLayoutService, undefined, true),
5692
[ICommandService.toString()]: new SyncDescriptor(CommandService, [], true)
5793
}

0 commit comments

Comments
 (0)