@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
109 lines • 3.92 kB
JavaScript
// *****************************************************************************
// 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-only WITH Classpath-exception-2.0
// *****************************************************************************
import { __decorate } from "tslib";
/* eslint-disable @typescript-eslint/no-explicit-any */
import { injectable, inject } from 'inversify';
import { GroupImpl, MenuModelRegistry } from '../common/menu/index.js';
import { DisposableCollection } from '../common/disposable.js';
import { ContextKeyService } from './context-key-service.js';
export const Coordinate = Symbol('Coordinate');
export function coordinateFromAnchor(anchor) {
const { x, y } = anchor instanceof MouseEvent ? { x: anchor.clientX, y: anchor.clientY } : anchor;
return { x, y };
}
export class ContextMenuAccess {
constructor(toClose) {
this.toDispose = new DisposableCollection();
this.onDispose = this.toDispose.onDispose;
this.toDispose.push(toClose);
}
get disposed() {
return this.toDispose.disposed;
}
dispose() {
this.toDispose.dispose();
}
}
let ContextMenuRenderer = class ContextMenuRenderer {
constructor() {
this.toDisposeOnSetCurrent = new DisposableCollection();
}
/**
* Currently opened context menu.
* Rendering a new context menu will close the current.
*/
get current() {
return this._current;
}
set current(current) {
this.setCurrent(current);
}
setCurrent(current) {
if (this._current === current) {
return;
}
this.toDisposeOnSetCurrent.dispose();
this._current = current;
if (current) {
this.toDisposeOnSetCurrent.push(current.onDispose(() => {
this._current = undefined;
}));
this.toDisposeOnSetCurrent.push(current);
}
}
render(options) {
let menu = options.menu;
if (!menu) {
menu = this.menuRegistry.getMenu(options.menuPath) || new GroupImpl('emtpyContextMenu');
}
const resolvedOptions = this.resolve(options);
if (resolvedOptions.skipSingleRootNode) {
menu = MenuModelRegistry.removeSingleRootNode(menu);
}
const access = this.doRender({
menuPath: options.menuPath,
menu,
anchor: resolvedOptions.anchor,
contextMatcher: options.contextKeyService || this.contextKeyService,
args: resolvedOptions.args,
context: resolvedOptions.context,
onHide: resolvedOptions.onHide
});
this.setCurrent(access);
return access;
}
resolve(options) {
const args = options.args ? options.args.slice() : [];
if (options.includeAnchorArg !== false) {
args.push(options.anchor);
}
return {
...options,
args
};
}
};
__decorate([
inject(MenuModelRegistry)
], ContextMenuRenderer.prototype, "menuRegistry", void 0);
__decorate([
inject(ContextKeyService)
], ContextMenuRenderer.prototype, "contextKeyService", void 0);
ContextMenuRenderer = __decorate([
injectable()
], ContextMenuRenderer);
export { ContextMenuRenderer };
//# sourceMappingURL=context-menu-renderer.js.map