@laserware/hoverboard
Version:
Better context menus for Electron.
108 lines (86 loc) • 2.9 kB
text/typescript
import type { MenuItemConstructorOptions } from "electron";
import type { ContextMenuItemType } from "../renderer/types.js";
import type { ContextMenuEventListenerOrEventListenerObject } from "./ContextMenuEvent.js";
import {
type BooleanAttribute,
type ContextMenuItemAttributes,
ContextMenuItemElement,
property,
} from "./ContextMenuItemElement.js";
export interface NormalMenuItemAttributes extends ContextMenuItemAttributes {
accelerator: string | null;
"accelerator-works-when-hidden": BooleanAttribute;
enabled: BooleanAttribute;
icon: string | null;
label: string | null;
"register-accelerator": BooleanAttribute;
tooltip: string | null;
}
export class NormalMenuItemElement<
A extends Record<string, any> = NormalMenuItemAttributes,
> extends ContextMenuItemElement<A> {
constructor(type: ContextMenuItemType = "normal") {
super(type);
}
({ type: String })
public accelerator: string | undefined;
({ attribute: "accelerator-works-when-hidden", type: Boolean })
public acceleratorWorksWhenHidden: boolean | undefined;
({ type: Boolean })
public enabled: boolean | undefined;
({ type: String })
public icon: string | undefined;
({ type: String })
public label: string | undefined;
({ attribute: "register-accelerator", type: Boolean })
public registerAccelerator: boolean | undefined;
({ attribute: "tooltip", type: String })
public toolTip: string | undefined;
public toTemplate(): MenuItemConstructorOptions {
const template = super.toTemplate();
if (this.accelerator !== undefined) {
template.accelerator = this.accelerator;
}
if (this.acceleratorWorksWhenHidden !== undefined) {
template.acceleratorWorksWhenHidden = this.acceleratorWorksWhenHidden;
}
if (this.enabled !== undefined) {
template.enabled = this.enabled;
}
if (this.icon !== undefined) {
template.icon = this.icon;
}
if (this.label !== undefined) {
template.label = this.label;
}
if (this.registerAccelerator !== undefined) {
template.registerAccelerator = this.registerAccelerator;
}
if (this.toolTip !== undefined) {
template.toolTip = this.toolTip;
}
return template;
}
public addEventListener(
type: "click",
listener: ContextMenuEventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions,
): void {
super.addEventListener(
type,
listener as EventListenerOrEventListenerObject,
options,
);
}
public removeEventListener(
type: "click",
listener: ContextMenuEventListenerOrEventListenerObject | null,
options?: boolean | AddEventListenerOptions,
): void {
super.removeEventListener(
type,
listener as EventListenerOrEventListenerObject,
options,
);
}
}