Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomoreno committed Nov 22, 2018
1 parent 70445c0 commit 9dcfa54
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/vs/base/browser/ui/contextview/contextview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const enum AnchorPosition {
export interface IDelegate {
getAnchor(): HTMLElement | IAnchor;
render(container: HTMLElement): IDisposable;
focus?(): void;
layout?(): void;
anchorAlignment?: AnchorAlignment; // default: left
anchorPosition?: AnchorPosition; // default: below
Expand Down Expand Up @@ -165,6 +166,11 @@ export class ContextView extends Disposable {

// Layout
this.doLayout();

// Focus
if (this.delegate.focus) {
this.delegate.focus();
}
}

public layout(): void {
Expand Down Expand Up @@ -223,7 +229,7 @@ export class ContextView extends Disposable {
const anchorPosition = this.delegate!.anchorPosition || AnchorPosition.BELOW;
const anchorAlignment = this.delegate!.anchorAlignment || AnchorAlignment.LEFT;

const verticalAnchor: ILayoutAnchor = { offset: around.top, size: around.height, position: anchorPosition === AnchorPosition.BELOW ? LayoutAnchorPosition.Before : LayoutAnchorPosition.After };
const verticalAnchor: ILayoutAnchor = { offset: around.top - window.pageYOffset, size: around.height, position: anchorPosition === AnchorPosition.BELOW ? LayoutAnchorPosition.Before : LayoutAnchorPosition.After };

let horizontalAnchor: ILayoutAnchor;

Expand All @@ -233,7 +239,7 @@ export class ContextView extends Disposable {
horizontalAnchor = { offset: around.left + around.width, size: 0, position: LayoutAnchorPosition.After };
}

const top = layout(window.innerHeight, viewSizeHeight, verticalAnchor);
const top = layout(window.innerHeight, viewSizeHeight, verticalAnchor) + window.pageYOffset;

// if view intersects vertically with anchor, shift it horizontally
if (Range.intersects({ start: top, end: top + viewSizeHeight }, { start: verticalAnchor.offset, end: verticalAnchor.offset + verticalAnchor.size })) {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/contrib/contextmenu/contextmenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class ContextMenuController implements IEditorContribution {
// Unless the user triggerd the context menu through Shift+F10, use the mouse position as menu position
let forcedPosition: IPosition;
if (e.target.type !== MouseTargetType.TEXTAREA) {
forcedPosition = { x: e.event.posx, y: e.event.posy + 1 };
forcedPosition = { x: e.event.posx + 2, y: e.event.posy + 1 };
}

// Show the context menu
Expand Down
10 changes: 6 additions & 4 deletions src/vs/platform/contextview/browser/contextMenuHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { combinedDisposable, IDisposable, dispose } from 'vs/base/common/lifecyc
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
import { ActionRunner, IAction, IRunEvent } from 'vs/base/common/actions';
import { Menu } from 'vs/base/browser/ui/menu/menu';

import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { INotificationService } from 'vs/platform/notification/common/notification';
Expand Down Expand Up @@ -57,6 +56,7 @@ export class ContextMenuHandler {

this.focusToReturn = document.activeElement as HTMLElement;

let menu: Menu | undefined;
this.contextViewService.showContextView({
getAnchor: () => delegate.getAnchor(),
canRelayout: false,
Expand All @@ -76,7 +76,7 @@ export class ContextMenuHandler {
actionRunner.onDidBeforeRun(this.onActionRun, this, menuDisposables);
actionRunner.onDidRun(this.onDidActionRun, this, menuDisposables);

const menu = new Menu(container, actions, {
menu = new Menu(container, actions, {
actionItemProvider: delegate.getActionItem,
context: delegate.getActionsContext ? delegate.getActionsContext() : null,
actionRunner,
Expand All @@ -89,11 +89,13 @@ export class ContextMenuHandler {
menu.onDidBlur(() => this.contextViewService.hideContextView(true), null, menuDisposables);
domEvent(window, EventType.BLUR)(() => { this.contextViewService.hideContextView(true); }, null, menuDisposables);

menu.focus(!!delegate.autoSelectFirstItem);

return combinedDisposable([...menuDisposables, menu]);
},

focus: () => {
menu.focus(!!delegate.autoSelectFirstItem);
},

onHide: (didCancel?: boolean) => {
if (delegate.onHide) {
delegate.onHide(didCancel);
Expand Down
1 change: 1 addition & 0 deletions src/vs/platform/contextview/browser/contextView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface IContextViewDelegate {
render(container: HTMLElement): IDisposable;
onDOMEvent?(e: any, activeElement: HTMLElement): void;
onHide?(data?: any): void;
focus?(): void;
}

export const IContextMenuService = createDecorator<IContextMenuService>('contextMenuService');
Expand Down

0 comments on commit 9dcfa54

Please sign in to comment.