-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example of registering a custom menu node.
`MenuModelRegistry#registerMenuNode` Signed-off-by: Akos Kitta <kittaakos@typefox.io>
- Loading branch information
Akos Kitta
committed
Aug 23, 2020
1 parent
e5e96a0
commit b55b223
Showing
4 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
examples/api-samples/src/browser/menu/sample-browser-menu-module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2020 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 | ||
********************************************************************************/ | ||
|
||
import { injectable, ContainerModule } from 'inversify'; | ||
import { Menu as MenuWidget } from '@phosphor/widgets'; | ||
import { Disposable } from '@theia/core/lib/common/disposable'; | ||
import { MenuNode, CompositeMenuNode } from '@theia/core/lib/common/menu'; | ||
import { BrowserMainMenuFactory, MenuCommandRegistry, DynamicMenuWidget } from '@theia/core/lib/browser/menu/browser-menu-plugin'; | ||
import { PlaceholderMenuNode } from './sample-menu-contribution'; | ||
|
||
export default new ContainerModule((bind, unbind, isBound, rebind) => { | ||
rebind(BrowserMainMenuFactory).to(SampleBrowserMainMenuFactory).inSingletonScope(); | ||
}); | ||
|
||
@injectable() | ||
class SampleBrowserMainMenuFactory extends BrowserMainMenuFactory { | ||
|
||
protected handleDefault(menuCommandRegistry: MenuCommandRegistry, menuNode: MenuNode): void { | ||
if (menuNode instanceof PlaceholderMenuNode && menuCommandRegistry instanceof SampleMenuCommandRegistry) { | ||
menuCommandRegistry.registerPlaceholderMenu(menuNode); | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
protected createMenuCommandRegistry(menu: CompositeMenuNode, args: any[] = []): MenuCommandRegistry { | ||
const menuCommandRegistry = new SampleMenuCommandRegistry(this.services); | ||
this.registerMenu(menuCommandRegistry, menu, args); | ||
return menuCommandRegistry; | ||
} | ||
|
||
createMenuWidget(menu: CompositeMenuNode, options: MenuWidget.IOptions & { commands: MenuCommandRegistry }): DynamicMenuWidget { | ||
return new SampleDynamicMenuWidget(menu, options, this.services); | ||
} | ||
|
||
} | ||
|
||
class SampleMenuCommandRegistry extends MenuCommandRegistry { | ||
|
||
protected placeholders = new Map<string, PlaceholderMenuNode>(); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
registerPlaceholderMenu(menu: PlaceholderMenuNode): void { | ||
const { id } = menu; | ||
if (this.placeholders.has(id)) { | ||
return; | ||
} | ||
this.placeholders.set(id, menu); | ||
} | ||
|
||
snapshot(): this { | ||
super.snapshot(); | ||
for (const menu of this.placeholders.values()) { | ||
this.toDispose.push(this.registerPlaceholder(menu)); | ||
} | ||
return this; | ||
} | ||
|
||
protected registerPlaceholder(menu: PlaceholderMenuNode): Disposable { | ||
const { id } = menu; | ||
const unregisterCommand = this.addCommand(id, { | ||
execute: () => { /* NOOP */ }, | ||
label: menu.label, | ||
icon: menu.icon, | ||
isEnabled: () => false, | ||
isVisible: () => true | ||
}); | ||
return Disposable.create(() => unregisterCommand.dispose()); | ||
} | ||
|
||
} | ||
|
||
class SampleDynamicMenuWidget extends DynamicMenuWidget { | ||
|
||
protected handleDefault(menuNode: MenuNode): MenuWidget.IItemOptions[] { | ||
if (menuNode instanceof PlaceholderMenuNode) { | ||
return [{ | ||
command: menuNode.id, | ||
type: 'command' | ||
}]; | ||
} | ||
return []; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
examples/api-samples/src/electron-browser/menu/sample-electron-menu-module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2020 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 | ||
********************************************************************************/ | ||
|
||
import { injectable, ContainerModule } from 'inversify'; | ||
import { CompositeMenuNode } from '@theia/core/lib/common/menu'; | ||
import { ElectronMainMenuFactory, ElectronMenuOptions } from '@theia/core/lib/electron-browser/menu/electron-main-menu-factory'; | ||
import { PlaceholderMenuNode } from '../../browser/menu/sample-menu-contribution'; | ||
|
||
export default new ContainerModule((bind, unbind, isBound, rebind) => { | ||
rebind(ElectronMainMenuFactory).to(SampleElectronMainMenuFactory).inSingletonScope(); | ||
}); | ||
|
||
@injectable() | ||
class SampleElectronMainMenuFactory extends ElectronMainMenuFactory { | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
protected handleDefault(menuNode: CompositeMenuNode, args: any[] = [], options?: ElectronMenuOptions): Electron.MenuItemConstructorOptions[] { | ||
if (menuNode instanceof PlaceholderMenuNode) { | ||
return [{ | ||
label: menuNode.label, | ||
enabled: false, | ||
visible: true | ||
}]; | ||
} | ||
return []; | ||
} | ||
|
||
} |