从托盘恢复时未触发 Browserwindow 的 "restore" 事件
Browserwindow's "restore" event not triggered when restored from tray
BrowserWindow在我点击关闭按钮后隐藏了,但是当我点击托盘图标再次恢复时,既没有触发“restore”事件也没有触发“show”事件。我想知道当 BrowserWindow 被“win.restore()”恢复时触发的事件是什么。
main.js:
const createWindow = () => {
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
win.on('close', function (event) {
event.preventDefault();
win.hide()
})
win.on('restore', function (event) {
console.log('restore event') //not triggered
})
win.on('show', function (event) {
console.log('show event') //not triggered
})
}
app.whenReady().then(() => {
tray = new Tray('./icon.png')
tray.on('click', () => {
win.restore();
win.setAlwaysOnTop(true);
});
createWindow()
})
首先,您必须确保在实例化 Electron 的 Tray
时指向的图标路径是有效路径。如果它不是有效路径,则不会显示任何图标,左键单击功能将不起作用。
确保您的 win
和 tray
变量声明和作用域正确。
我已经将你的 Tray
的创建移动到它自己的函数中(即:createTray
),就像你的 createWindow
函数一样。
最后,如果您愿意,也可以使用win.show()
in place of both the win.restore()
and win.setAlwaysOnTop(true)
方法。 win.show()
将始终恢复并聚焦(显示)您的 window。
Minimising the window and left-clicking the tray icon will restore
and show
your window.
Closing the window and left-clicking the tray icon will (only) show
your window. Restore is not necessary as the window does not get minimised when closed.
main.js
(主线程)
const app = require('electron').app;
const BrowserWindow = require('electron').BrowserWindow;
const Tray = require('electron').Tray;
const path = require("path");
// Prevent garbage collection
let window;
let tray;
function createWindow() {
const window = new BrowserWindow({
x: 0,
y: 0,
width: 800,
height: 600,
show: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
});
window.loadFile('index.html')
.then(() => { window.show(); });
window.on('restore', (event) => {
console.log('restore event') // Triggered
})
window.on('show', (event) => {
console.log('show event') // Triggered
})
window.on('close', (event) => {
// Ensure you have another way for user to close app
event.preventDefault();
window.hide()
})
return window;
}
function createTray() {
let tray = new Tray('icon.png');
// Show the window on left-click.
tray.on('click', () => {
window.show();
});
return tray;
}
app.on('ready', () => {
window = createWindow();
tray = createTray();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
毫无疑问,Electron 的设置可能很挑剔。您需要按照正确的顺序并在正确的范围内获取所有内容才能正常工作。
BrowserWindow在我点击关闭按钮后隐藏了,但是当我点击托盘图标再次恢复时,既没有触发“restore”事件也没有触发“show”事件。我想知道当 BrowserWindow 被“win.restore()”恢复时触发的事件是什么。
main.js:
const createWindow = () => {
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
win.on('close', function (event) {
event.preventDefault();
win.hide()
})
win.on('restore', function (event) {
console.log('restore event') //not triggered
})
win.on('show', function (event) {
console.log('show event') //not triggered
})
}
app.whenReady().then(() => {
tray = new Tray('./icon.png')
tray.on('click', () => {
win.restore();
win.setAlwaysOnTop(true);
});
createWindow()
})
首先,您必须确保在实例化 Electron 的 Tray
时指向的图标路径是有效路径。如果它不是有效路径,则不会显示任何图标,左键单击功能将不起作用。
确保您的 win
和 tray
变量声明和作用域正确。
我已经将你的 Tray
的创建移动到它自己的函数中(即:createTray
),就像你的 createWindow
函数一样。
最后,如果您愿意,也可以使用win.show()
in place of both the win.restore()
and win.setAlwaysOnTop(true)
方法。 win.show()
将始终恢复并聚焦(显示)您的 window。
Minimising the window and left-clicking the tray icon will
restore
andshow
your window.Closing the window and left-clicking the tray icon will (only)
show
your window. Restore is not necessary as the window does not get minimised when closed.
main.js
(主线程)
const app = require('electron').app;
const BrowserWindow = require('electron').BrowserWindow;
const Tray = require('electron').Tray;
const path = require("path");
// Prevent garbage collection
let window;
let tray;
function createWindow() {
const window = new BrowserWindow({
x: 0,
y: 0,
width: 800,
height: 600,
show: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
});
window.loadFile('index.html')
.then(() => { window.show(); });
window.on('restore', (event) => {
console.log('restore event') // Triggered
})
window.on('show', (event) => {
console.log('show event') // Triggered
})
window.on('close', (event) => {
// Ensure you have another way for user to close app
event.preventDefault();
window.hide()
})
return window;
}
function createTray() {
let tray = new Tray('icon.png');
// Show the window on left-click.
tray.on('click', () => {
window.show();
});
return tray;
}
app.on('ready', () => {
window = createWindow();
tray = createTray();
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
毫无疑问,Electron 的设置可能很挑剔。您需要按照正确的顺序并在正确的范围内获取所有内容才能正常工作。