-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathcommon-frontend-contribution.ts
1879 lines (1774 loc) · 86.6 KB
/
common-frontend-contribution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
79
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
114
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
173
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
226
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************************
* Copyright (C) 2017 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
/* eslint-disable max-len, @typescript-eslint/indent */
import debounce = require('lodash.debounce');
import { injectable, inject } from 'inversify';
import { TabBar, Widget } from '@phosphor/widgets';
import { MAIN_MENU_BAR, SETTINGS_MENU, MenuContribution, MenuModelRegistry, ACCOUNTS_MENU } from '../common/menu';
import { KeybindingContribution, KeybindingRegistry } from './keybinding';
import { FrontendApplication, FrontendApplicationContribution } from './frontend-application';
import { CommandContribution, CommandRegistry, Command } from '../common/command';
import { UriAwareCommandHandler } from '../common/uri-command-handler';
import { SelectionService } from '../common/selection-service';
import { MessageService } from '../common/message-service';
import { OpenerService, open } from '../browser/opener-service';
import { ApplicationShell } from './shell/application-shell';
import { SHELL_TABBAR_CONTEXT_MENU } from './shell/tab-bars';
import { AboutDialog } from './about-dialog';
import * as browser from './browser';
import URI from '../common/uri';
import { ContextKeyService } from './context-key-service';
import { OS, isOSX, isWindows } from '../common/os';
import { ResourceContextKey } from './resource-context-key';
import { UriSelection } from '../common/selection';
import { StorageService } from './storage-service';
import { Navigatable } from './navigatable';
import { QuickViewService } from './quick-view-service';
import { PrefixQuickOpenService, QuickOpenItem, QuickOpenMode, QuickOpenService, QuickOpenGroupItem } from './quick-open';
import { environment } from '@theia/application-package/lib/environment';
import { IconThemeService } from './icon-theme-service';
import { ColorContribution } from './color-application-contribution';
import { ColorRegistry, Color } from './color-registry';
import { CorePreferences } from './core-preferences';
import { ThemeService } from './theming';
import { PreferenceService, PreferenceScope } from './preferences';
import { ClipboardService } from './clipboard-service';
import { EncodingRegistry } from './encoding-registry';
import { UTF8 } from '../common/encodings';
import { EnvVariablesServer } from '../common/env-variables';
import { AuthenticationService } from './authentication-service';
import { FormatType } from './saveable';
export namespace CommonMenus {
export const FILE = [...MAIN_MENU_BAR, '1_file'];
export const FILE_NEW = [...FILE, '1_new'];
export const FILE_OPEN = [...FILE, '2_open'];
export const FILE_SAVE = [...FILE, '3_save'];
export const FILE_AUTOSAVE = [...FILE, '4_autosave'];
export const FILE_SETTINGS = [...FILE, '5_settings'];
export const FILE_SETTINGS_SUBMENU = [...FILE_SETTINGS, '1_settings_submenu'];
export const FILE_SETTINGS_SUBMENU_OPEN = [...FILE_SETTINGS_SUBMENU, '1_settings_submenu_open'];
export const FILE_SETTINGS_SUBMENU_THEME = [...FILE_SETTINGS_SUBMENU, '2_settings_submenu_theme'];
export const FILE_CLOSE = [...FILE, '6_close'];
export const EDIT = [...MAIN_MENU_BAR, '2_edit'];
export const EDIT_UNDO = [...EDIT, '1_undo'];
export const EDIT_CLIPBOARD = [...EDIT, '2_clipboard'];
export const EDIT_FIND = [...EDIT, '3_find'];
export const VIEW = [...MAIN_MENU_BAR, '4_view'];
export const VIEW_PRIMARY = [...VIEW, '0_primary'];
export const VIEW_VIEWS = [...VIEW, '1_views'];
export const VIEW_LAYOUT = [...VIEW, '2_layout'];
export const VIEW_TOGGLE = [...VIEW, '3_toggle'];
export const SETTINGS_OPEN = [...SETTINGS_MENU, '1_settings_open'];
export const SETTINGS__THEME = [...SETTINGS_MENU, '2_settings_theme'];
// last menu item
export const HELP = [...MAIN_MENU_BAR, '9_help'];
}
export namespace CommonCommands {
const FILE_CATEGORY = 'File';
const VIEW_CATEGORY = 'View';
export const OPEN: Command = {
id: 'core.open',
};
export const CUT: Command = {
id: 'core.cut',
label: 'Cut'
};
export const COPY: Command = {
id: 'core.copy',
label: 'Copy'
};
export const PASTE: Command = {
id: 'core.paste',
label: 'Paste'
};
export const COPY_PATH: Command = {
id: 'core.copy.path',
label: 'Copy Path'
};
export const UNDO: Command = {
id: 'core.undo',
label: 'Undo'
};
export const REDO: Command = {
id: 'core.redo',
label: 'Redo'
};
export const SELECT_ALL: Command = {
id: 'core.selectAll',
label: 'Select All'
};
export const FIND: Command = {
id: 'core.find',
label: 'Find'
};
export const REPLACE: Command = {
id: 'core.replace',
label: 'Replace'
};
export const NEXT_TAB: Command = {
id: 'core.nextTab',
category: VIEW_CATEGORY,
label: 'Switch to Next Tab'
};
export const PREVIOUS_TAB: Command = {
id: 'core.previousTab',
category: VIEW_CATEGORY,
label: 'Switch to Previous Tab'
};
export const NEXT_TAB_IN_GROUP: Command = {
id: 'core.nextTabInGroup',
category: VIEW_CATEGORY,
label: 'Switch to Next Tab in Group'
};
export const PREVIOUS_TAB_IN_GROUP: Command = {
id: 'core.previousTabInGroup',
category: VIEW_CATEGORY,
label: 'Switch to Previous Tab in Group'
};
export const NEXT_TAB_GROUP: Command = {
id: 'core.nextTabGroup',
category: VIEW_CATEGORY,
label: 'Switch to Next Tab Group'
};
export const PREVIOUS_TAB_GROUP: Command = {
id: 'core.previousTabBar',
category: VIEW_CATEGORY,
label: 'Switch to Previous Tab Group'
};
export const CLOSE_TAB: Command = {
id: 'core.close.tab',
category: VIEW_CATEGORY,
label: 'Close Tab'
};
export const CLOSE_OTHER_TABS: Command = {
id: 'core.close.other.tabs',
category: VIEW_CATEGORY,
label: 'Close Other Tabs'
};
export const CLOSE_RIGHT_TABS: Command = {
id: 'core.close.right.tabs',
category: VIEW_CATEGORY,
label: 'Close Tabs to the Right'
};
export const CLOSE_ALL_TABS: Command = {
id: 'core.close.all.tabs',
category: VIEW_CATEGORY,
label: 'Close All Tabs'
};
export const CLOSE_MAIN_TAB: Command = {
id: 'core.close.main.tab',
category: VIEW_CATEGORY,
label: 'Close Tab in Main Area'
};
export const CLOSE_OTHER_MAIN_TABS: Command = {
id: 'core.close.other.main.tabs',
category: VIEW_CATEGORY,
label: 'Close Other Tabs in Main Area'
};
export const CLOSE_ALL_MAIN_TABS: Command = {
id: 'core.close.all.main.tabs',
category: VIEW_CATEGORY,
label: 'Close All Tabs in Main Area'
};
export const COLLAPSE_PANEL: Command = {
id: 'core.collapse.tab',
category: VIEW_CATEGORY,
label: 'Collapse Side Panel'
};
export const COLLAPSE_ALL_PANELS: Command = {
id: 'core.collapse.all.tabs',
category: VIEW_CATEGORY,
label: 'Collapse All Side Panels'
};
export const TOGGLE_BOTTOM_PANEL: Command = {
id: 'core.toggle.bottom.panel',
category: VIEW_CATEGORY,
label: 'Toggle Bottom Panel'
};
export const TOGGLE_MAXIMIZED: Command = {
id: 'core.toggleMaximized',
category: VIEW_CATEGORY,
label: 'Toggle Maximized'
};
export const OPEN_VIEW: Command = {
id: 'core.openView',
category: VIEW_CATEGORY,
label: 'Open View...'
};
export const SAVE: Command = {
id: 'core.save',
category: FILE_CATEGORY,
label: 'Save',
};
export const SAVE_WITHOUT_FORMATTING: Command = {
id: 'core.saveWithoutFormatting',
category: FILE_CATEGORY,
label: 'Save without Formatting',
};
export const SAVE_ALL: Command = {
id: 'core.saveAll',
category: FILE_CATEGORY,
label: 'Save All',
};
export const AUTO_SAVE: Command = {
id: 'textEditor.commands.autosave',
category: FILE_CATEGORY,
label: 'Auto Save',
};
export const ABOUT_COMMAND: Command = {
id: 'core.about',
label: 'About'
};
export const OPEN_PREFERENCES: Command = {
id: 'preferences:open',
category: 'Settings',
label: 'Open Preferences',
};
export const SELECT_COLOR_THEME: Command = {
id: 'workbench.action.selectTheme',
label: 'Color Theme',
category: 'Preferences'
};
export const SELECT_ICON_THEME: Command = {
id: 'workbench.action.selectIconTheme',
label: 'File Icon Theme',
category: 'Preferences'
};
}
export const supportCut = browser.isNative || document.queryCommandSupported('cut');
export const supportCopy = browser.isNative || document.queryCommandSupported('copy');
// Chrome incorrectly returns true for document.queryCommandSupported('paste')
// when the paste feature is available but the calling script has insufficient
// privileges to actually perform the action
export const supportPaste = browser.isNative || (!browser.isChrome && document.queryCommandSupported('paste'));
export const RECENT_COMMANDS_STORAGE_KEY = 'commands';
@injectable()
export class CommonFrontendContribution implements FrontendApplicationContribution, MenuContribution, CommandContribution, KeybindingContribution, ColorContribution {
constructor(
@inject(ApplicationShell) protected readonly shell: ApplicationShell,
@inject(SelectionService) protected readonly selectionService: SelectionService,
@inject(MessageService) protected readonly messageService: MessageService,
@inject(OpenerService) protected readonly openerService: OpenerService,
@inject(AboutDialog) protected readonly aboutDialog: AboutDialog
) { }
@inject(ContextKeyService)
protected readonly contextKeyService: ContextKeyService;
@inject(ResourceContextKey)
protected readonly resourceContextKey: ResourceContextKey;
@inject(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;
@inject(StorageService)
protected readonly storageService: StorageService;
@inject(QuickViewService)
protected readonly quickView: QuickViewService;
@inject(PrefixQuickOpenService)
protected readonly quickOpen: PrefixQuickOpenService;
@inject(IconThemeService)
protected readonly iconThemes: IconThemeService;
@inject(ThemeService)
protected readonly themeService: ThemeService;
@inject(QuickOpenService)
protected readonly quickOpenService: QuickOpenService;
@inject(CorePreferences)
protected readonly preferences: CorePreferences;
@inject(PreferenceService)
protected readonly preferenceService: PreferenceService;
@inject(ClipboardService)
protected readonly clipboardService: ClipboardService;
@inject(EncodingRegistry)
protected readonly encodingRegistry: EncodingRegistry;
@inject(EnvVariablesServer)
protected readonly environments: EnvVariablesServer;
@inject(AuthenticationService)
protected readonly authenticationService: AuthenticationService;
async configure(app: FrontendApplication): Promise<void> {
const configDirUri = await this.environments.getConfigDirUri();
// Global settings
this.encodingRegistry.registerOverride({
encoding: UTF8,
parent: new URI(configDirUri)
});
this.contextKeyService.createKey<boolean>('isLinux', OS.type() === OS.Type.Linux);
this.contextKeyService.createKey<boolean>('isMac', OS.type() === OS.Type.OSX);
this.contextKeyService.createKey<boolean>('isWindows', OS.type() === OS.Type.Windows);
this.contextKeyService.createKey<boolean>('isWeb', !this.isElectron());
this.initResourceContextKeys();
this.registerCtrlWHandling();
this.updateStyles();
this.updateThemeFromPreference('workbench.colorTheme');
this.updateThemeFromPreference('workbench.iconTheme');
this.preferences.onPreferenceChanged(e => {
if (e.preferenceName === 'workbench.editor.highlightModifiedTabs') {
this.updateStyles();
} else if (e.preferenceName === 'workbench.colorTheme' || e.preferenceName === 'workbench.iconTheme') {
this.updateThemeFromPreference(e.preferenceName);
}
});
this.themeService.onThemeChange(() => this.updateThemePreference('workbench.colorTheme'));
this.iconThemes.onDidChangeCurrent(() => this.updateThemePreference('workbench.iconTheme'));
app.shell.leftPanelHandler.addMenu({
id: 'settings-menu',
iconClass: 'codicon codicon-settings-gear',
title: 'Settings',
menuPath: SETTINGS_MENU,
order: 0,
});
const accountsMenu = {
id: 'accounts-menu',
iconClass: 'codicon codicon-person',
title: 'Accounts',
menuPath: ACCOUNTS_MENU,
order: 1,
};
this.authenticationService.onDidRegisterAuthenticationProvider(() => {
app.shell.leftPanelHandler.addMenu(accountsMenu);
});
this.authenticationService.onDidUnregisterAuthenticationProvider(() => {
if (this.authenticationService.getProviderIds().length === 0) {
app.shell.leftPanelHandler.removeMenu(accountsMenu.id);
}
});
}
protected updateStyles(): void {
document.body.classList.remove('theia-editor-highlightModifiedTabs');
if (this.preferences['workbench.editor.highlightModifiedTabs']) {
document.body.classList.add('theia-editor-highlightModifiedTabs');
}
}
protected updateThemePreference(preferenceName: 'workbench.colorTheme' | 'workbench.iconTheme'): void {
const inspect = this.preferenceService.inspect<string | null>(preferenceName);
const workspaceValue = inspect && inspect.workspaceValue;
const userValue = inspect && inspect.globalValue;
const value = workspaceValue || userValue;
const newValue = preferenceName === 'workbench.colorTheme' ? this.themeService.getCurrentTheme().id : this.iconThemes.current;
if (newValue !== value) {
const scope = workspaceValue !== undefined ? PreferenceScope.Workspace : PreferenceScope.User;
this.preferenceService.set(preferenceName, newValue, scope);
}
}
protected updateThemeFromPreference(preferenceName: 'workbench.colorTheme' | 'workbench.iconTheme'): void {
const inspect = this.preferenceService.inspect<string | null>(preferenceName);
const workspaceValue = inspect && inspect.workspaceValue;
const userValue = inspect && inspect.globalValue;
const value = workspaceValue || userValue;
if (value !== undefined) {
if (preferenceName === 'workbench.colorTheme') {
this.themeService.setCurrentTheme(value || this.themeService.defaultTheme.id);
} else {
this.iconThemes.current = value || this.iconThemes.default.id;
}
}
}
onStart(): void {
this.storageService.getData<{ recent: Command[] }>(RECENT_COMMANDS_STORAGE_KEY, { recent: [] })
.then(tasks => this.commandRegistry.recent = tasks.recent);
}
onStop(): void {
const recent = this.commandRegistry.recent;
this.storageService.setData<{ recent: Command[] }>(RECENT_COMMANDS_STORAGE_KEY, { recent });
}
protected initResourceContextKeys(): void {
const updateContextKeys = () => {
const selection = this.selectionService.selection;
const resourceUri = Navigatable.is(selection) && selection.getResourceUri() || UriSelection.getUri(selection);
this.resourceContextKey.set(resourceUri);
};
updateContextKeys();
this.selectionService.onSelectionChanged(updateContextKeys);
}
registerMenus(registry: MenuModelRegistry): void {
registry.registerSubmenu(CommonMenus.FILE, 'File');
registry.registerSubmenu(CommonMenus.EDIT, 'Edit');
registry.registerSubmenu(CommonMenus.VIEW, 'View');
registry.registerSubmenu(CommonMenus.HELP, 'Help');
registry.registerMenuAction(CommonMenus.FILE_SAVE, {
commandId: CommonCommands.SAVE.id
});
registry.registerMenuAction(CommonMenus.FILE_SAVE, {
commandId: CommonCommands.SAVE_ALL.id
});
registry.registerMenuAction(CommonMenus.FILE_AUTOSAVE, {
commandId: CommonCommands.AUTO_SAVE.id
});
registry.registerSubmenu(CommonMenus.FILE_SETTINGS_SUBMENU, 'Settings');
registry.registerMenuAction(CommonMenus.EDIT_UNDO, {
commandId: CommonCommands.UNDO.id,
order: '0'
});
registry.registerMenuAction(CommonMenus.EDIT_UNDO, {
commandId: CommonCommands.REDO.id,
order: '1'
});
registry.registerMenuAction(CommonMenus.EDIT_FIND, {
commandId: CommonCommands.FIND.id,
order: '0'
});
registry.registerMenuAction(CommonMenus.EDIT_FIND, {
commandId: CommonCommands.REPLACE.id,
order: '1'
});
registry.registerMenuAction(CommonMenus.EDIT_CLIPBOARD, {
commandId: CommonCommands.CUT.id,
order: '0'
});
registry.registerMenuAction(CommonMenus.EDIT_CLIPBOARD, {
commandId: CommonCommands.COPY.id,
order: '1'
});
registry.registerMenuAction(CommonMenus.EDIT_CLIPBOARD, {
commandId: CommonCommands.PASTE.id,
order: '2'
});
registry.registerMenuAction(CommonMenus.EDIT_CLIPBOARD, {
commandId: CommonCommands.COPY_PATH.id,
order: '3'
});
registry.registerMenuAction(CommonMenus.VIEW_LAYOUT, {
commandId: CommonCommands.TOGGLE_BOTTOM_PANEL.id,
order: '0'
});
registry.registerMenuAction(CommonMenus.VIEW_LAYOUT, {
commandId: CommonCommands.COLLAPSE_ALL_PANELS.id,
order: '1'
});
registry.registerMenuAction(SHELL_TABBAR_CONTEXT_MENU, {
commandId: CommonCommands.CLOSE_TAB.id,
label: 'Close',
order: '0'
});
registry.registerMenuAction(SHELL_TABBAR_CONTEXT_MENU, {
commandId: CommonCommands.CLOSE_OTHER_TABS.id,
label: 'Close Others',
order: '1'
});
registry.registerMenuAction(SHELL_TABBAR_CONTEXT_MENU, {
commandId: CommonCommands.CLOSE_RIGHT_TABS.id,
label: 'Close to the Right',
order: '2'
});
registry.registerMenuAction(SHELL_TABBAR_CONTEXT_MENU, {
commandId: CommonCommands.CLOSE_ALL_TABS.id,
label: 'Close All',
order: '3'
});
registry.registerMenuAction(SHELL_TABBAR_CONTEXT_MENU, {
commandId: CommonCommands.COLLAPSE_PANEL.id,
label: 'Collapse',
order: '4'
});
registry.registerMenuAction(SHELL_TABBAR_CONTEXT_MENU, {
commandId: CommonCommands.TOGGLE_MAXIMIZED.id,
label: 'Toggle Maximized',
order: '5'
});
registry.registerMenuAction(CommonMenus.HELP, {
commandId: CommonCommands.ABOUT_COMMAND.id,
label: 'About',
order: '9'
});
registry.registerMenuAction(CommonMenus.VIEW_PRIMARY, {
commandId: CommonCommands.OPEN_VIEW.id
});
registry.registerMenuAction(CommonMenus.FILE_SETTINGS_SUBMENU_THEME, {
commandId: CommonCommands.SELECT_COLOR_THEME.id
});
registry.registerMenuAction(CommonMenus.FILE_SETTINGS_SUBMENU_THEME, {
commandId: CommonCommands.SELECT_ICON_THEME.id
});
registry.registerMenuAction(CommonMenus.SETTINGS__THEME, {
commandId: CommonCommands.SELECT_COLOR_THEME.id
});
registry.registerMenuAction(CommonMenus.SETTINGS__THEME, {
commandId: CommonCommands.SELECT_ICON_THEME.id
});
}
registerCommands(commandRegistry: CommandRegistry): void {
commandRegistry.registerCommand(CommonCommands.OPEN, UriAwareCommandHandler.MultiSelect(this.selectionService, {
execute: uris => uris.map(uri => open(this.openerService, uri)),
}));
commandRegistry.registerCommand(CommonCommands.CUT, {
execute: () => {
if (supportCut) {
document.execCommand('cut');
} else {
this.messageService.warn("Please use the browser's cut command or shortcut.");
}
}
});
commandRegistry.registerCommand(CommonCommands.COPY, {
execute: () => {
if (supportCopy) {
document.execCommand('copy');
} else {
this.messageService.warn("Please use the browser's copy command or shortcut.");
}
}
});
commandRegistry.registerCommand(CommonCommands.PASTE, {
execute: () => {
if (supportPaste) {
document.execCommand('paste');
} else {
this.messageService.warn("Please use the browser's paste command or shortcut.");
}
}
});
commandRegistry.registerCommand(CommonCommands.COPY_PATH, UriAwareCommandHandler.MultiSelect(this.selectionService, {
execute: async uris => {
if (uris.length) {
const lineDelimiter = isWindows ? '\r\n' : '\n';
const text = uris.map(resource => resource.path).join(lineDelimiter);
await this.clipboardService.writeText(text);
} else {
await this.messageService.info('Open a file first to copy its path');
}
}
}));
commandRegistry.registerCommand(CommonCommands.UNDO, {
execute: () => document.execCommand('undo')
});
commandRegistry.registerCommand(CommonCommands.REDO, {
execute: () => document.execCommand('redo')
});
commandRegistry.registerCommand(CommonCommands.SELECT_ALL, {
execute: () => document.execCommand('selectAll')
});
commandRegistry.registerCommand(CommonCommands.FIND, {
execute: () => { /* no-op */ }
});
commandRegistry.registerCommand(CommonCommands.REPLACE, {
execute: () => { /* no-op */ }
});
commandRegistry.registerCommand(CommonCommands.NEXT_TAB, {
isEnabled: () => this.shell.currentTabBar !== undefined,
execute: () => this.shell.activateNextTab()
});
commandRegistry.registerCommand(CommonCommands.PREVIOUS_TAB, {
isEnabled: () => this.shell.currentTabBar !== undefined,
execute: () => this.shell.activatePreviousTab()
});
commandRegistry.registerCommand(CommonCommands.NEXT_TAB_IN_GROUP, {
isEnabled: () => this.shell.nextTabIndexInTabBar() !== -1,
execute: () => this.shell.activateNextTabInTabBar()
});
commandRegistry.registerCommand(CommonCommands.PREVIOUS_TAB_IN_GROUP, {
isEnabled: () => this.shell.previousTabIndexInTabBar() !== -1,
execute: () => this.shell.activatePreviousTabInTabBar()
});
commandRegistry.registerCommand(CommonCommands.NEXT_TAB_GROUP, {
isEnabled: () => this.shell.nextTabBar() !== undefined,
execute: () => this.shell.activateNextTabBar()
});
commandRegistry.registerCommand(CommonCommands.PREVIOUS_TAB_GROUP, {
isEnabled: () => this.shell.previousTabBar() !== undefined,
execute: () => this.shell.activatePreviousTabBar()
});
commandRegistry.registerCommand(CommonCommands.CLOSE_TAB, {
isEnabled: (event?: Event) => {
const tabBar = this.shell.findTabBar(event);
if (!tabBar) {
return false;
}
const currentTitle = this.shell.findTitle(tabBar, event);
return currentTitle !== undefined && currentTitle.closable;
},
execute: (event?: Event) => {
const tabBar = this.shell.findTabBar(event)!;
const currentTitle = this.shell.findTitle(tabBar, event);
this.shell.closeTabs(tabBar, title => title === currentTitle);
}
});
commandRegistry.registerCommand(CommonCommands.CLOSE_OTHER_TABS, {
isEnabled: (event?: Event) => {
const tabBar = this.shell.findTabBar(event);
if (!tabBar) {
return false;
}
const currentTitle = this.shell.findTitle(tabBar, event);
return tabBar.titles.some(title => title !== currentTitle && title.closable);
},
execute: (event?: Event) => {
const tabBar = this.shell.findTabBar(event)!;
const currentTitle = this.shell.findTitle(tabBar, event);
this.shell.closeTabs(tabBar, title => title !== currentTitle && title.closable);
}
});
commandRegistry.registerCommand(CommonCommands.CLOSE_RIGHT_TABS, {
isEnabled: (event?: Event) => {
const tabBar = this.shell.findTabBar(event);
if (!tabBar) {
return false;
}
const currentIndex = this.findTitleIndex(tabBar, event);
return tabBar.titles.some((title, index) => index > currentIndex && title.closable);
},
isVisible: (event?: Event) => {
const area = this.findTabArea(event);
return area !== undefined && area !== 'left' && area !== 'right';
},
execute: (event?: Event) => {
const tabBar = this.shell.findTabBar(event)!;
const currentIndex = this.findTitleIndex(tabBar, event);
this.shell.closeTabs(tabBar, (title, index) => index > currentIndex && title.closable);
}
});
commandRegistry.registerCommand(CommonCommands.CLOSE_ALL_TABS, {
isEnabled: (event?: Event) => {
const tabBar = this.shell.findTabBar(event);
return tabBar !== undefined && tabBar.titles.some(title => title.closable);
},
execute: (event?: Event) => this.shell.closeTabs(this.shell.findTabBar(event)!, title => title.closable)
});
commandRegistry.registerCommand(CommonCommands.CLOSE_MAIN_TAB, {
isEnabled: () => {
const currentWidget = this.shell.getCurrentWidget('main');
return currentWidget !== undefined && currentWidget.title.closable;
},
execute: () => this.shell.getCurrentWidget('main')!.close()
});
commandRegistry.registerCommand(CommonCommands.CLOSE_OTHER_MAIN_TABS, {
isEnabled: () => {
const currentWidget = this.shell.getCurrentWidget('main');
return currentWidget !== undefined &&
this.shell.mainAreaTabBars.some(tb => tb.titles.some(title => title.owner !== currentWidget && title.closable));
},
execute: () => {
const currentWidget = this.shell.getCurrentWidget('main');
this.shell.closeTabs('main', title => title.owner !== currentWidget && title.closable);
}
});
commandRegistry.registerCommand(CommonCommands.CLOSE_ALL_MAIN_TABS, {
isEnabled: () => this.shell.mainAreaTabBars.some(tb => tb.titles.some(title => title.closable)),
execute: () => this.shell.closeTabs('main', title => title.closable)
});
commandRegistry.registerCommand(CommonCommands.COLLAPSE_PANEL, {
isEnabled: (event?: Event) => ApplicationShell.isSideArea(this.findTabArea(event)),
isVisible: (event?: Event) => ApplicationShell.isSideArea(this.findTabArea(event)),
execute: (event?: Event) => this.shell.collapsePanel(this.findTabArea(event)!)
});
commandRegistry.registerCommand(CommonCommands.COLLAPSE_ALL_PANELS, {
execute: () => {
this.shell.collapsePanel('left');
this.shell.collapsePanel('right');
this.shell.collapsePanel('bottom');
}
});
commandRegistry.registerCommand(CommonCommands.TOGGLE_BOTTOM_PANEL, {
isEnabled: () => this.shell.getWidgets('bottom').length > 0,
execute: () => {
if (this.shell.isExpanded('bottom')) {
this.shell.collapsePanel('bottom');
} else {
this.shell.expandPanel('bottom');
}
}
});
commandRegistry.registerCommand(CommonCommands.TOGGLE_MAXIMIZED, {
isEnabled: (event?: Event) => this.canToggleMaximized(event),
isVisible: (event?: Event) => this.canToggleMaximized(event),
execute: (event?: Event) => this.toggleMaximized(event)
});
commandRegistry.registerCommand(CommonCommands.SAVE, {
execute: () => this.shell.save({ formatType: FormatType.ON })
});
commandRegistry.registerCommand(CommonCommands.SAVE_WITHOUT_FORMATTING, {
execute: () => this.shell.save({ formatType: FormatType.OFF })
});
commandRegistry.registerCommand(CommonCommands.SAVE_ALL, {
execute: () => this.shell.saveAll({ formatType: FormatType.DIRTY })
});
commandRegistry.registerCommand(CommonCommands.ABOUT_COMMAND, {
execute: () => this.openAbout()
});
commandRegistry.registerCommand(CommonCommands.OPEN_VIEW, {
execute: () => this.quickOpen.open(this.quickView.prefix)
});
commandRegistry.registerCommand(CommonCommands.SELECT_COLOR_THEME, {
execute: () => this.selectColorTheme()
});
commandRegistry.registerCommand(CommonCommands.SELECT_ICON_THEME, {
execute: () => this.selectIconTheme()
});
}
private findTabArea(event?: Event): ApplicationShell.Area | undefined {
const tabBar = this.shell.findTabBar(event);
if (tabBar) {
return this.shell.getAreaFor(tabBar);
}
return this.shell.currentTabArea;
}
/**
* Finds the index of the selected title from the tab-bar.
* @param tabBar: used for providing an array of titles.
* @returns the index of the selected title if it is available in the tab-bar, else returns the index of currently-selected title.
*/
private findTitleIndex(tabBar: TabBar<Widget>, event?: Event): number {
if (event) {
const targetTitle = this.shell.findTitle(tabBar, event);
return targetTitle ? tabBar.titles.indexOf(targetTitle) : tabBar.currentIndex;
}
return tabBar.currentIndex;
}
private canToggleMaximized(event?: Event): boolean {
if (event?.target instanceof HTMLElement) {
const widget = this.shell.findWidgetForElement(event.target);
if (widget) {
return this.shell.mainPanel.contains(widget) || this.shell.bottomPanel.contains(widget);
}
}
return this.shell.canToggleMaximized();
}
/**
* Maximize the bottom or the main dockpanel based on the widget.
* @param event used to find the selected widget.
*/
private toggleMaximized(event?: Event): void {
if (event?.target instanceof HTMLElement) {
const widget = this.shell.findWidgetForElement(event.target);
if (widget) {
if (this.shell.mainPanel.contains(widget)) {
this.shell.mainPanel.toggleMaximized();
} else if (this.shell.bottomPanel.contains(widget)) {
this.shell.bottomPanel.toggleMaximized();
}
if (widget instanceof TabBar) {
// reveals the widget when maximized.
const title = this.shell.findTitle(widget, event);
if (title) {
this.shell.revealWidget(title.owner.id);
}
}
}
} else {
this.shell.toggleMaximized();
}
}
private isElectron(): boolean {
return environment.electron.is();
}
registerKeybindings(registry: KeybindingRegistry): void {
if (supportCut) {
registry.registerKeybinding({
command: CommonCommands.CUT.id,
keybinding: 'ctrlcmd+x'
});
}
if (supportCopy) {
registry.registerKeybinding({
command: CommonCommands.COPY.id,
keybinding: 'ctrlcmd+c'
});
}
if (supportPaste) {
registry.registerKeybinding({
command: CommonCommands.PASTE.id,
keybinding: 'ctrlcmd+v'
});
}
registry.registerKeybinding({
command: CommonCommands.COPY_PATH.id,
keybinding: isWindows ? 'shift+alt+c' : 'ctrlcmd+alt+c',
when: '!editorFocus'
});
registry.registerKeybindings(
// Edition
{
command: CommonCommands.UNDO.id,
keybinding: 'ctrlcmd+z'
},
{
command: CommonCommands.REDO.id,
keybinding: 'ctrlcmd+shift+z'
},
{
command: CommonCommands.SELECT_ALL.id,
keybinding: 'ctrlcmd+a'
},
{
command: CommonCommands.FIND.id,
keybinding: 'ctrlcmd+f'
},
{
command: CommonCommands.REPLACE.id,
keybinding: 'ctrlcmd+alt+f'
},
// Tabs
{
command: CommonCommands.NEXT_TAB.id,
keybinding: 'ctrlcmd+tab'
},
{
command: CommonCommands.NEXT_TAB.id,
keybinding: 'ctrlcmd+alt+d'
},
{
command: CommonCommands.PREVIOUS_TAB.id,
keybinding: 'ctrlcmd+shift+tab'
},
{
command: CommonCommands.PREVIOUS_TAB.id,
keybinding: 'ctrlcmd+alt+a'
},
{
command: CommonCommands.CLOSE_MAIN_TAB.id,
keybinding: this.isElectron() ? (isWindows ? 'ctrl+f4' : 'ctrlcmd+w') : 'alt+w'
},
{
command: CommonCommands.CLOSE_OTHER_MAIN_TABS.id,
keybinding: 'ctrlcmd+alt+t'
},
{
command: CommonCommands.CLOSE_ALL_MAIN_TABS.id,
keybinding: this.isElectron() ? 'ctrlCmd+k ctrlCmd+w' : 'alt+shift+w'
},
// Panels
{
command: CommonCommands.COLLAPSE_PANEL.id,
keybinding: 'alt+c'
},
{
command: CommonCommands.TOGGLE_BOTTOM_PANEL.id,
keybinding: 'ctrlcmd+j',
},
{
command: CommonCommands.COLLAPSE_ALL_PANELS.id,
keybinding: 'alt+shift+c',
},
{
command: CommonCommands.TOGGLE_MAXIMIZED.id,
keybinding: 'alt+m',
},
// Saving
{
command: CommonCommands.SAVE.id,
keybinding: 'ctrlcmd+s'
},
{
command: CommonCommands.SAVE_WITHOUT_FORMATTING.id,
keybinding: 'ctrlcmd+k s'
},
{
command: CommonCommands.SAVE_ALL.id,
keybinding: 'ctrlcmd+alt+s'
},
// Theming
{
command: CommonCommands.SELECT_COLOR_THEME.id,
keybinding: 'ctrlcmd+k ctrlcmd+t'
}
);
}
protected async openAbout(): Promise<void> {
this.aboutDialog.open();
}
protected shouldPreventClose = false;
/**
* registers event listener which make sure that
* window doesn't get closed if CMD/CTRL W is pressed.
* Too many users have that in their muscle memory.
* Chrome doesn't let us rebind or prevent default the keybinding, so this
* at least doesn't close the window immediately.
*/
protected registerCtrlWHandling(): void {
function isCtrlCmd(event: KeyboardEvent): boolean {
return (isOSX && event.metaKey) || (!isOSX && event.ctrlKey);
}
window.document.addEventListener('keydown', event => {
this.shouldPreventClose = isCtrlCmd(event) && event.code === 'KeyW';
});
window.document.addEventListener('keyup', () => {
this.shouldPreventClose = false;
});
}
onWillStop(): true | undefined {
try {
if (this.shouldPreventClose || this.shell.canSaveAll()) {
return true;
}
} finally {
this.shouldPreventClose = false;
}
}
protected selectIconTheme(): void {
let resetTo: string | undefined = this.iconThemes.current;
const previewTheme = debounce((id: string) => this.iconThemes.current = id, 200);
let items: (QuickOpenItem & { id: string })[] = [];
for (const iconTheme of this.iconThemes.definitions) {
const item = Object.assign(new QuickOpenItem({
label: iconTheme.label,
description: iconTheme.description,
run: (mode: QuickOpenMode) => {
if (mode === QuickOpenMode.OPEN) {