提交 2bf4a936 编写于 作者: J Johannes Rieken

prepare for editor context menu

上级 78b4ea28
...@@ -14,6 +14,7 @@ import {IKeyboardEvent} from 'vs/base/browser/keyboardEvent'; ...@@ -14,6 +14,7 @@ import {IKeyboardEvent} from 'vs/base/browser/keyboardEvent';
import {ActionItem, Separator} from 'vs/base/browser/ui/actionbar/actionbar'; import {ActionItem, Separator} from 'vs/base/browser/ui/actionbar/actionbar';
import {IContextMenuService, IContextViewService} from 'vs/platform/contextview/browser/contextView'; import {IContextMenuService, IContextViewService} from 'vs/platform/contextview/browser/contextView';
import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService'; import {IKeybindingService} from 'vs/platform/keybinding/common/keybindingService';
import {IMenuService, IMenu, MenuId} from 'vs/platform/actions/common/actions';
import {EditorAction} from 'vs/editor/common/editorAction'; import {EditorAction} from 'vs/editor/common/editorAction';
import {Behaviour} from 'vs/editor/common/editorActionEnablement'; import {Behaviour} from 'vs/editor/common/editorActionEnablement';
import {ICommonCodeEditor, IEditorActionDescriptorData, IEditorContribution, MouseTargetType} from 'vs/editor/common/editorCommon'; import {ICommonCodeEditor, IEditorActionDescriptorData, IEditorContribution, MouseTargetType} from 'vs/editor/common/editorCommon';
...@@ -22,34 +23,33 @@ import {ICodeEditor, IEditorMouseEvent} from 'vs/editor/browser/editorBrowser'; ...@@ -22,34 +23,33 @@ import {ICodeEditor, IEditorMouseEvent} from 'vs/editor/browser/editorBrowser';
import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions'; import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions';
interface IPosition { interface IPosition {
x:number; x: number;
y:number; y: number;
} }
class ContextMenuController implements IEditorContribution { class ContextMenuController implements IEditorContribution {
public static ID = 'editor.contrib.contextmenu'; public static ID = 'editor.contrib.contextmenu';
private contextMenuService:IContextMenuService; private _editor: ICodeEditor;
private contextViewService: IContextViewService; private _contextMenu: IMenu;
private keybindingService: IKeybindingService; private _toDispose: IDisposable[];
private _contextMenuIsBeingShownCount: number;
private _editor:ICodeEditor;
private _toDispose:IDisposable[]; constructor(
private _contextMenuIsBeingShownCount:number; editor: ICodeEditor,
@IContextMenuService private _contextMenuService: IContextMenuService,
constructor(editor:ICodeEditor, @IContextMenuService contextMenuService: IContextMenuService, @IContextViewService contextViewService: IContextViewService, @IKeybindingService keybindingService: IKeybindingService) { @IContextViewService private _contextViewService: IContextViewService,
this.contextMenuService = contextMenuService; @IKeybindingService private _keybindingService: IKeybindingService,
this.contextViewService = contextViewService; @IMenuService private _menuService: IMenuService
this.keybindingService = keybindingService; ) {
this._editor = editor; this._editor = editor;
this._toDispose = []; this._toDispose = [];
this._contextMenuIsBeingShownCount = 0; this._contextMenuIsBeingShownCount = 0;
this._toDispose.push(this._editor.onContextMenu((e:IEditorMouseEvent)=>this._onContextMenu(e))); this._toDispose.push(this._editor.onContextMenu((e: IEditorMouseEvent) => this._onContextMenu(e)));
this._toDispose.push(this._editor.onKeyDown((e:IKeyboardEvent)=> { this._toDispose.push(this._editor.onKeyDown((e: IKeyboardEvent) => {
if (e.keyCode === KeyCode.ContextMenu) { if (e.keyCode === KeyCode.ContextMenu) {
// Chrome is funny like that // Chrome is funny like that
e.preventDefault(); e.preventDefault();
...@@ -59,7 +59,7 @@ class ContextMenuController implements IEditorContribution { ...@@ -59,7 +59,7 @@ class ContextMenuController implements IEditorContribution {
})); }));
} }
private _onContextMenu(e:IEditorMouseEvent): void { private _onContextMenu(e: IEditorMouseEvent): void {
if (!this._editor.getConfiguration().contribInfo.contextmenu) { if (!this._editor.getConfiguration().contribInfo.contextmenu) {
this._editor.focus(); this._editor.focus();
// Ensure the cursor is at the position of the mouse click // Ensure the cursor is at the position of the mouse click
...@@ -79,6 +79,12 @@ class ContextMenuController implements IEditorContribution { ...@@ -79,6 +79,12 @@ class ContextMenuController implements IEditorContribution {
return; // only support mouse click into text or native context menu key for now return; // only support mouse click into text or native context menu key for now
} }
// Ensure menu is there
if (!this._contextMenu) {
this._contextMenu = this._menuService.createMenu(MenuId.EditorContext, this._editor.getDomNode());
this._toDispose.push(this._contextMenu);
}
// Ensure the editor gets focus if it hasn't, so the right events are being sent to other contributions // Ensure the editor gets focus if it hasn't, so the right events are being sent to other contributions
this._editor.focus(); this._editor.focus();
...@@ -88,7 +94,7 @@ class ContextMenuController implements IEditorContribution { ...@@ -88,7 +94,7 @@ class ContextMenuController implements IEditorContribution {
} }
// Unless the user triggerd the context menu through Shift+F10, use the mouse position as menu position // Unless the user triggerd the context menu through Shift+F10, use the mouse position as menu position
var forcedPosition:IPosition; var forcedPosition: IPosition;
if (e.target.type !== MouseTargetType.TEXTAREA) { if (e.target.type !== MouseTargetType.TEXTAREA) {
forcedPosition = { x: e.event.posx, y: e.event.posy + 1 }; forcedPosition = { x: e.event.posx, y: e.event.posy + 1 };
} }
...@@ -97,12 +103,12 @@ class ContextMenuController implements IEditorContribution { ...@@ -97,12 +103,12 @@ class ContextMenuController implements IEditorContribution {
this.showContextMenu(forcedPosition); this.showContextMenu(forcedPosition);
} }
public showContextMenu(forcedPosition?:IPosition): void { public showContextMenu(forcedPosition?: IPosition): void {
if (!this._editor.getConfiguration().contribInfo.contextmenu) { if (!this._editor.getConfiguration().contribInfo.contextmenu) {
return; // Context menu is turned off through configuration return; // Context menu is turned off through configuration
} }
if (!this.contextMenuService) { if (!this._contextMenuService) {
this._editor.focus(); this._editor.focus();
return; // We need the context menu service to function return; // We need the context menu service to function
} }
...@@ -123,12 +129,19 @@ class ContextMenuController implements IEditorContribution { ...@@ -123,12 +129,19 @@ class ContextMenuController implements IEditorContribution {
} }
private _getMenuActions(): IAction[] { private _getMenuActions(): IAction[] {
// const actions = this._contextMenu.getActions();
// if (actions.length > 0) {
// console.log(actions);
// return actions;
// }
const editorModel = this._editor.getModel(); const editorModel = this._editor.getModel();
if (!editorModel) { if (!editorModel) {
return []; return [];
} }
const contributedActions = <EditorAction[]> this._editor.getActions().filter(action => { const contributedActions = <EditorAction[]>this._editor.getActions().filter(action => {
if (action instanceof EditorAction) { if (action instanceof EditorAction) {
return action.shouldShowInContextMenu() && action.isSupported(); return action.shouldShowInContextMenu() && action.isSupported();
} }
...@@ -172,7 +185,7 @@ class ContextMenuController implements IEditorContribution { ...@@ -172,7 +185,7 @@ class ContextMenuController implements IEditorContribution {
return result; return result;
} }
private _doShowContextMenu(actions:IAction[], forcedPosition:IPosition = null): void { private _doShowContextMenu(actions: IAction[], forcedPosition: IPosition = null): void {
// Make the editor believe one of its widgets is focused // Make the editor believe one of its widgets is focused
this._editor.beginForcedWidgetFocus(); this._editor.beginForcedWidgetFocus();
...@@ -200,7 +213,7 @@ class ContextMenuController implements IEditorContribution { ...@@ -200,7 +213,7 @@ class ContextMenuController implements IEditorContribution {
} }
// Show menu // Show menu
this.contextMenuService.showContextMenu({ this._contextMenuService.showContextMenu({
getAnchor: () => menuPosition, getAnchor: () => menuPosition,
getActions: () => { getActions: () => {
...@@ -210,7 +223,7 @@ class ContextMenuController implements IEditorContribution { ...@@ -210,7 +223,7 @@ class ContextMenuController implements IEditorContribution {
getActionItem: (action) => { getActionItem: (action) => {
var keybinding = this._keybindingFor(action); var keybinding = this._keybindingFor(action);
if (keybinding) { if (keybinding) {
return new ActionItem(action, action, { label: true, keybinding: this.keybindingService.getLabelFor(keybinding) }); return new ActionItem(action, action, { label: true, keybinding: this._keybindingService.getLabelFor(keybinding) });
} }
var customActionItem = <any>action; var customActionItem = <any>action;
...@@ -225,7 +238,7 @@ class ContextMenuController implements IEditorContribution { ...@@ -225,7 +238,7 @@ class ContextMenuController implements IEditorContribution {
return this._keybindingFor(action); return this._keybindingFor(action);
}, },
onHide: (wasCancelled:boolean) => { onHide: (wasCancelled: boolean) => {
this._contextMenuIsBeingShownCount--; this._contextMenuIsBeingShownCount--;
this._editor.focus(); this._editor.focus();
this._editor.endForcedWidgetFocus(); this._editor.endForcedWidgetFocus();
...@@ -237,7 +250,7 @@ class ContextMenuController implements IEditorContribution { ...@@ -237,7 +250,7 @@ class ContextMenuController implements IEditorContribution {
} }
private _keybindingFor(action: IAction): Keybinding { private _keybindingFor(action: IAction): Keybinding {
var opts = this.keybindingService.lookupKeybindings(action.id); var opts = this._keybindingService.lookupKeybindings(action.id);
if (opts.length > 0) { if (opts.length > 0) {
return opts[0]; // only take the first one return opts[0]; // only take the first one
} }
...@@ -250,7 +263,7 @@ class ContextMenuController implements IEditorContribution { ...@@ -250,7 +263,7 @@ class ContextMenuController implements IEditorContribution {
public dispose(): void { public dispose(): void {
if (this._contextMenuIsBeingShownCount > 0) { if (this._contextMenuIsBeingShownCount > 0) {
this.contextViewService.hideContextView(); this._contextViewService.hideContextView();
} }
this._toDispose = dispose(this._toDispose); this._toDispose = dispose(this._toDispose);
...@@ -261,7 +274,7 @@ class ShowContextMenu extends EditorAction { ...@@ -261,7 +274,7 @@ class ShowContextMenu extends EditorAction {
public static ID = 'editor.action.showContextMenu'; public static ID = 'editor.action.showContextMenu';
constructor(descriptor:IEditorActionDescriptorData, editor:ICommonCodeEditor) { constructor(descriptor: IEditorActionDescriptorData, editor: ICommonCodeEditor) {
super(descriptor, editor, Behaviour.TextFocus); super(descriptor, editor, Behaviour.TextFocus);
} }
......
...@@ -261,7 +261,7 @@ export abstract class KeybindingService extends AbstractKeybindingService implem ...@@ -261,7 +261,7 @@ export abstract class KeybindingService extends AbstractKeybindingService implem
const ctx = Object.create(null); const ctx = Object.create(null);
this.getContext(this._findContextAttr(domNode)).fillInContext(ctx); this.getContext(this._findContextAttr(domNode)).fillInContext(ctx);
this._configurationContext.fillInContext(ctx); this._configurationContext.fillInContext(ctx);
// console.log(JSON.stringify(contextValue, null, '\t')); // console.log(JSON.stringify(ctx, null, '\t'));
return KeybindingResolver.contextMatchesRules(ctx, rules); return KeybindingResolver.contextMatchesRules(ctx, rules);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册