[英]Electron menubar module not found
Here is the Code, The Tutorial Said I should use this syntax in order to use 'menubar', I've also tried including Menubar as a dependency from 'electron', but that responded with Menubar is not a constructor.这是代码,教程说我应该使用这种语法来使用“menubar”,我也尝试过将 Menubar 作为“electron”的依赖项,但是用 Menubar 不是构造函数来响应。
Here is the Error Message:这是错误消息:
Error: Cannot find module 'menubar'错误:找不到模块“菜单栏”
and here is the full code from the main process这是主流程的完整代码
const path = require('path');
const {
app,
clipboard,
Menu,
BrowserWindow,
globalShortcut,
Tray,
systemPreferences,
} = require('electron');
// Declare an empty Array
const clippings = [];
// Declare a variable in the global scope that eventually stores a reference to
// the tray instance
let tray = null;
// Instead of requiring the app module from Electron, we create an instance of menubar
const Menubar = require('menubar');
const menubar = new Menubar();
// menubar wraps up several common Electron modules. It fires its ready event
// when the app module fires its ready event
menubar.on('ready', () => {
console.log('Application is ready.');
})
// The getIcon() function checks the platform the application is running on and
// returns the appropriate filename
// Initialize a BrowserWindow instance
let browserWindow = null;
const getIcon = () => {
if (process.platform === 'win32') return 'icon-light@2x.ico';
// Use the sysemPreferences.isDarkMode() to detect if macOS is in dark mode
if (systemPreferences.isDarkMode()) return 'icon-light.png';
return 'icon-dark.png';
};
app.on('ready', () => {
// Hide the dock icon if running on macOS
if (app.dock) app.dock.hide();
// When creating a new tray instance, use getIcon() to get the correct filename
tray = new Tray(path.join(__dirname, getIcon()));
// Creates a tray instance by calling the constructor with a path to an image
// tray = new Tray(path.join(__dirname, '/Icon.png'));
// On Windows, we register a click event listener to open the menu
if (process.platform === 'win32') {
tray.on('click', tray.popUpContextMenu);
}
// Initialize browserWindow
browserWindow = new BrowserWindow({
show: false,
});
// Load alternative Icon
browserWindow.loadURL(path.join(__dirname, 'index.html'));
// Setting an alternate icon for when icon is pressed
tray.setPressedImage(path.join(__dirname, 'icon-light.png'));
// Passes a string defining the accelerator and an anonymous function that
// should be invoked whenever the accelerator is passed
const activationShortcut = globalShortcut.register(
'CommandOrControl+Option+C',
// We register a second shortcut to add a clipping to the array
() => { tray.popUpContextMenu(); }
);
// If registration fails, Electron does not throw an error. Instead, it
// returns undefined. In this line, we check if the activationShortcut is defined
if (!activationShortcut) {
// If either shortcut fails, we log the issue with console.error. In a
// more robust application, you might show the user that there was an issue
// or implement a fallback
console.error('Global activation shortcut failed to register');
}
const newClippingShortcut = globalShortcut.register(
'CommandOrControl+Shift+Option+C',
() => {
// addClipping() returns the string of the clipping that was added
// to the array
const clipping = addClipping();
if (clipping) {
browserWindow.webContents.send(
'show-notification',
// If there was a clipping saved, we send a notification to
// the renderer process, which triggers the notification
'Clipping Added',
clipping,
);
}
},
);
if (!newClippingShortcut) {
console.error('Global new clipping shortcut failed to register');
}
updateMenu();
// Define a tooltip to be shown when the ser hovers ove the tray icon
tray.setToolTip('Clipmaster');
});
const updateMenu = () => {
// Build a menu in the same fashion that we built the application and context menus
const menu = Menu.buildFromTemplate([
{
label: 'Create New Clipping',
// When a user clicks the Create New Clipping menu item, we call addClipping() function
click() { addClipping(); },
accelerator: 'CommandOrControl+Shift+C'
},
{ type: 'separator' },
...clippings.slice(0, 10).map(createClippingMenuItem),
{ type: 'separator'},
{
label: 'Quit',
click() { app.quit(); },
// accelerator for the Quit menu itme
accelerator: 'CommandOrControl+Q'
}
]);
// Take the menu created and set it as the menu that appears when the user
// clicks the icon in the menu or system tray in macOS and Windows, respectively.
tray.setContextMenu(menu);
};
const addClipping = () => {
// Uses Electron's clipboard module to read text from the system clipboard
const clipping = clipboard.readText();
// Checks if the clippings array already contains the current clippings. If so,
// returns early from the function
if (clippings.includes(clipping)) return;
// Unshift the text read from teh clipboard into the array of clippings
clippings.unshift(clipping);
// Regenerates the menu to display the new clipping as a menu item
updateMenu();
return clipping;
}
// Creates a function called createClippingMenuItem()
const createClippingMenuItem = (clipping, index) => {
return {
// if the length of the clipping is longer than 20 characters
label: clipping.length > 20
? clipping.slice(0, 20) + '_'
: clipping,
// When a user clicks on a given clipping, writes it to the clipboad
click() { clipboard.writeText(clipping); },
// Assign the menu item an accelerator based on its index
accelerator: `CommandOrControl+${index}`
};
};
First check if you actually installed the menubar repo.首先检查你是否真的安装了菜单栏仓库。
Or just npm i -S menubar
或者只是npm i -S menubar
Then try:然后尝试:
const { menubar } = require('menubar');
It seems that it is not a default export.它似乎不是默认导出。
I just assume that they refer to the npm module: https://github.com/maxogden/menubar我只是假设他们指的是 npm 模块: https ://github.com/maxogden/menubar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.