Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing/profiles/context menu contribution #14028

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ class SampleElectronMainMenuFactory extends ElectronMainMenuFactory {
protected override fillMenuTemplate(parentItems: MenuDto[],
menu: MenuNode,
args: unknown[] = [],
options: ElectronMenuOptions
options: ElectronMenuOptions,
skipRoot: boolean
): MenuDto[] {
if (menu instanceof PlaceholderMenuNode) {
parentItems.push({ label: menu.label, enabled: false, visible: true });
} else {
super.fillMenuTemplate(parentItems, menu, args, options);
super.fillMenuTemplate(parentItems, menu, args, options, skipRoot);
}
return parentItems;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/browser/context-key-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { injectable } from 'inversify';
import { Emitter, Event } from '../common/event';
import { Disposable } from '../common';

export type ContextKeyValue = null | undefined | boolean | number | string
| Array<null | undefined | boolean | number | string>
Expand Down Expand Up @@ -83,11 +84,10 @@ export interface ContextKeyService extends ContextMatcher {
setContext(key: string, value: unknown): void;
}

export type ScopedValueStore = Omit<ContextKeyService, 'onDidChange' | 'match' | 'parseKeys' | 'with' | 'createOverlay'>;
export type ScopedValueStore = Omit<ContextKeyService, 'onDidChange' | 'match' | 'parseKeys' | 'with' | 'createOverlay'> & Disposable;

@injectable()
export class ContextKeyServiceDummyImpl implements ContextKeyService {

protected readonly onDidChangeEmitter = new Emitter<ContextKeyChangeEvent>();
readonly onDidChange = this.onDidChangeEmitter.event;
protected fireDidChange(event: ContextKeyChangeEvent): void {
Expand Down Expand Up @@ -122,7 +122,7 @@ export class ContextKeyServiceDummyImpl implements ContextKeyService {
/**
* Details should implemented by an extension, e.g. by the monaco extension.
*/
createScoped(target: HTMLElement): ContextKeyService {
createScoped(target: HTMLElement): ScopedValueStore {
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
// *****************************************************************************

import { MenuNode, MenuPath } from '../../../common';
import { NAVIGATION, TabBarToolbarItem } from './tab-bar-toolbar-types';
import { NAVIGATION, RenderedToolbarItem } from './tab-bar-toolbar-types';

export const TOOLBAR_WRAPPER_ID_SUFFIX = '-as-tabbar-toolbar-item';

export class ToolbarMenuNodeWrapper implements TabBarToolbarItem {
export class ToolbarMenuNodeWrapper implements RenderedToolbarItem {
constructor(protected readonly menuNode: MenuNode, readonly group?: string, readonly menuPath?: MenuPath) { }
get id(): string { return this.menuNode.id + TOOLBAR_WRAPPER_ID_SUFFIX; }
get command(): string { return this.menuNode.command ?? ''; };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import debounce = require('lodash.debounce');
import { inject, injectable, named } from 'inversify';
// eslint-disable-next-line max-len
import { CommandMenuNode, CommandRegistry, CompoundMenuNode, ContributionProvider, Disposable, DisposableCollection, Emitter, Event, MenuModelRegistry, MenuNode, MenuPath } from '../../../common';
import { CommandRegistry, ContributionProvider, Disposable, DisposableCollection, Emitter, Event, MenuModelRegistry, MenuNode, MenuPath } from '../../../common';
import { ContextKeyService } from '../../context-key-service';
import { FrontendApplicationContribution } from '../../frontend-application-contribution';
import { Widget } from '../../widgets';
import { AnyToolbarItem, ConditionalToolbarItem, MenuDelegate, MenuToolbarItem, ReactTabBarToolbarItem, TabBarToolbarItem } from './tab-bar-toolbar-types';
import { MenuDelegate, ReactTabBarToolbarItem, RenderedToolbarItem, TabBarToolbarItem } from './tab-bar-toolbar-types';
import { ToolbarMenuNodeWrapper } from './tab-bar-toolbar-menu-adapters';

/**
Expand Down Expand Up @@ -75,7 +75,7 @@ export class TabBarToolbarRegistry implements FrontendApplicationContribution {
*
* @param item the item to register.
*/
registerItem(item: TabBarToolbarItem | ReactTabBarToolbarItem): Disposable {
registerItem(item: RenderedToolbarItem | ReactTabBarToolbarItem): Disposable {
const { id } = item;
if (this.items.has(id)) {
throw new Error(`A toolbar item is already registered with the '${id}' ID.`);
Expand Down Expand Up @@ -110,24 +110,17 @@ export class TabBarToolbarRegistry implements FrontendApplicationContribution {
for (const delegate of this.menuDelegates.values()) {
if (delegate.isVisible(widget)) {
const menu = this.menuRegistry.getMenu(delegate.menuPath);
const children = CompoundMenuNode.getFlatChildren(menu.children);
for (const child of children) {
for (const child of menu.children) {
if (!child.when || this.contextKeyService.match(child.when, widget.node)) {
if (child.children) {
for (const grandchild of child.children) {
if (!grandchild.when || this.contextKeyService.match(grandchild.when, widget.node)) {
if (CommandMenuNode.is(grandchild)) {
result.push(new ToolbarMenuNodeWrapper(grandchild, child.id, delegate.menuPath));
} else if (CompoundMenuNode.is(grandchild)) {
let menuPath;
if (menuPath = this.menuRegistry.getPath(grandchild)) {
result.push(new ToolbarMenuNodeWrapper(grandchild, child.id, menuPath));
}
}
const menuPath = this.menuRegistry.getPath(grandchild);
result.push(new ToolbarMenuNodeWrapper(grandchild, child.id, menuPath));
}
}
} else if (child.command) {
result.push(new ToolbarMenuNodeWrapper(child, '', delegate.menuPath));
result.push(new ToolbarMenuNodeWrapper(child, ''));
}
}
}
Expand All @@ -145,15 +138,17 @@ export class TabBarToolbarRegistry implements FrontendApplicationContribution {
* @returns `false` if the `item` should be suppressed, otherwise `true`
*/
protected isItemVisible(item: TabBarToolbarItem | ReactTabBarToolbarItem, widget: Widget): boolean {
if (TabBarToolbarItem.is(item) && item.command && !this.isTabBarToolbarItemVisible(item, widget)) {
if (!this.isConditionalItemVisible(item, widget)) {
return false;
}
if (MenuToolbarItem.is(item) && !this.isMenuToolbarItemVisible(item, widget)) {

if (item.command && !this.commandRegistry.isVisible(item.command, widget)) {
return false;
}
if (AnyToolbarItem.isConditional(item) && !this.isConditionalItemVisible(item, widget)) {
if (item.menuPath && !this.isNonEmptyMenu(item, widget)) {
return false;
}

// The item is not vetoed. Accept it
return true;
}
Expand All @@ -166,7 +161,7 @@ export class TabBarToolbarRegistry implements FrontendApplicationContribution {
* @param widget the widget that is updating the toolbar
* @returns `false` if the `item` should be suppressed, otherwise `true`
*/
protected isConditionalItemVisible(item: ConditionalToolbarItem, widget: Widget): boolean {
protected isConditionalItemVisible(item: TabBarToolbarItem, widget: Widget): boolean {
if (item.isVisible && !item.isVisible(widget)) {
return false;
}
Expand All @@ -176,19 +171,6 @@ export class TabBarToolbarRegistry implements FrontendApplicationContribution {
return true;
}

/**
* Query whether a tab-bar toolbar `item` that has a command should be shown in the toolbar.
* This implementation returns `false` if the `item`'s command is not visible in the
* `widget` according to the command registry.
*
* @param item a tab-bar toolbar item that has a non-empty `command`
* @param widget the widget that is updating the toolbar
* @returns `false` if the `item` should be suppressed, otherwise `true`
*/
protected isTabBarToolbarItemVisible(item: TabBarToolbarItem, widget: Widget): boolean {
return this.commandRegistry.isVisible(item.command, widget);
}

/**
* Query whether a menu toolbar `item` should be shown in the toolbar.
* This implementation returns `false` if the `item` does not have any actual menu to show.
Expand All @@ -197,7 +179,10 @@ export class TabBarToolbarRegistry implements FrontendApplicationContribution {
* @param widget the widget that is updating the toolbar
* @returns `false` if the `item` should be suppressed, otherwise `true`
*/
protected isMenuToolbarItemVisible(item: MenuToolbarItem, widget: Widget): boolean {
isNonEmptyMenu(item: TabBarToolbarItem, widget: Widget | undefined): boolean {
if (!item.menuPath) {
return false;
}
const menu = this.menuRegistry.getMenu(item.menuPath);
const isVisible: (node: MenuNode) => boolean = node =>
node.children?.length
Expand All @@ -220,7 +205,7 @@ export class TabBarToolbarRegistry implements FrontendApplicationContribution {
}
}

registerMenuDelegate(menuPath: MenuPath, when?: string | ((widget: Widget) => boolean)): Disposable {
registerMenuDelegate(menuPath: MenuPath, when?: ((widget: Widget) => boolean)): Disposable {
const id = this.toElementId(menuPath);
if (!this.menuDelegates.has(id)) {
const isVisible: MenuDelegate['isVisible'] = !when
Expand Down
Loading
Loading